独立摄像头创建

This commit is contained in:
kura 2026-05-07 22:58:01 +08:00
parent 6b5e402f3a
commit ecadc3933c

View File

@ -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) {