diff --git a/src/pages/file/utils/peer.ts b/src/pages/file/utils/peer.ts index 41bc9c3..9c75902 100644 --- a/src/pages/file/utils/peer.ts +++ b/src/pages/file/utils/peer.ts @@ -88,7 +88,11 @@ class Peer extends EventTarget { ? "摄像头请求" : "语音通话请求"; const mediaName = - type === "desktop" ? "屏幕共享" : type === "camera" ? "摄像头" : "语音通话"; + type === "desktop" + ? "屏幕共享" + : type === "camera" + ? "摄像头" + : "语音通话"; const content = `${call.peer} 请求与您进行${mediaName},是否接受?`; const permission = type === "desktop" @@ -300,6 +304,22 @@ class Peer extends EventTarget { return stream; } + private createCameraOfferStream() { + const canvas = document.createElement("canvas"); + canvas.width = 1; + canvas.height = 1; + const context = canvas.getContext("2d"); + context?.fillRect(0, 0, 1, 1); + + const stream = canvas.captureStream(1); + const [track] = stream.getVideoTracks(); + if (!track) { + throw new Error("无法创建摄像头协商视频轨"); + } + track.enabled = false; + return stream; + } + async requestCall(id: string) { if (!this.remoteConnection) { notification.error({ @@ -344,7 +364,7 @@ class Peer extends EventTarget { } try { - const offerStream = this.createDesktopOfferStream(); + const offerStream = this.createCameraOfferStream(); const call = this.peer.call(sign2peerid(id), offerStream, { metadata: { type: "camera" }, @@ -371,7 +391,9 @@ class Peer extends EventTarget { // 结束媒体连接 endMedia(peerId?: string, type?: MediaType) { if (peerId) { - const mediaTypes: MediaType[] = type ? [type] : ["desktop", "call", "camera"]; + const mediaTypes: MediaType[] = type + ? [type] + : ["desktop", "call", "camera"]; mediaTypes.forEach((mediaType) => { this.endMediaConnection(peerId, mediaType); if (this.remoteConnection) {