修复视频空流

This commit is contained in:
kura 2026-05-07 21:21:45 +08:00
parent 19b9621dee
commit ae36cb183b

View File

@ -104,7 +104,7 @@ class Peer extends EventTarget {
}
call.answer(stream);
self.setupMediaConnection(call, type, stream);
self.setupMediaConnection(call, type, stream, type !== "desktop");
self.dispatchEvent(
new CustomEvent(
@ -199,18 +199,13 @@ class Peer extends EventTarget {
}
try {
// 创建一个静音的音频流作为占位
const emptyStream = new MediaStream();
// 将音频轨道静音
emptyStream.getAudioTracks().forEach((track) => {
track.enabled = false;
});
const offerStream = this.createDesktopOfferStream();
const call = this.peer.call(sign2peerid(id), emptyStream, {
const call = this.peer.call(sign2peerid(id), offerStream, {
metadata: { type: "desktop" },
});
this.setupMediaConnection(call, "desktop", emptyStream);
this.setupMediaConnection(call, "desktop", offerStream);
this.dispatchEvent(
new CustomEvent("desktop-started", {
@ -227,6 +222,22 @@ class Peer extends EventTarget {
}
}
private createDesktopOfferStream() {
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({
@ -435,6 +446,7 @@ class Peer extends EventTarget {
call: MediaConnection,
type: MediaType,
localStream: MediaStream | null = null,
acceptRemoteStream = true,
) {
const peerId = call.peer;
this.updateMediaConnection(peerId, type, {
@ -444,6 +456,7 @@ class Peer extends EventTarget {
call.on("stream", (remoteStream: MediaStream) => {
console.log("stream", remoteStream);
if (!acceptRemoteStream) return;
// 只有当流中包含相应类型的轨道时才保存连接
const hasVideo = remoteStream.getVideoTracks().length > 0;
const hasAudio = remoteStream.getAudioTracks().length > 0;