修复视频空流
This commit is contained in:
parent
19b9621dee
commit
ae36cb183b
@ -104,7 +104,7 @@ class Peer extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
call.answer(stream);
|
call.answer(stream);
|
||||||
self.setupMediaConnection(call, type, stream);
|
self.setupMediaConnection(call, type, stream, type !== "desktop");
|
||||||
|
|
||||||
self.dispatchEvent(
|
self.dispatchEvent(
|
||||||
new CustomEvent(
|
new CustomEvent(
|
||||||
@ -199,18 +199,13 @@ class Peer extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 创建一个静音的音频流作为占位
|
const offerStream = this.createDesktopOfferStream();
|
||||||
const emptyStream = new MediaStream();
|
|
||||||
// 将音频轨道静音
|
|
||||||
emptyStream.getAudioTracks().forEach((track) => {
|
|
||||||
track.enabled = false;
|
|
||||||
});
|
|
||||||
|
|
||||||
const call = this.peer.call(sign2peerid(id), emptyStream, {
|
const call = this.peer.call(sign2peerid(id), offerStream, {
|
||||||
metadata: { type: "desktop" },
|
metadata: { type: "desktop" },
|
||||||
});
|
});
|
||||||
|
|
||||||
this.setupMediaConnection(call, "desktop", emptyStream);
|
this.setupMediaConnection(call, "desktop", offerStream);
|
||||||
|
|
||||||
this.dispatchEvent(
|
this.dispatchEvent(
|
||||||
new CustomEvent("desktop-started", {
|
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) {
|
async requestCall(id: string) {
|
||||||
if (!this.remoteConnection) {
|
if (!this.remoteConnection) {
|
||||||
notification.error({
|
notification.error({
|
||||||
@ -435,6 +446,7 @@ class Peer extends EventTarget {
|
|||||||
call: MediaConnection,
|
call: MediaConnection,
|
||||||
type: MediaType,
|
type: MediaType,
|
||||||
localStream: MediaStream | null = null,
|
localStream: MediaStream | null = null,
|
||||||
|
acceptRemoteStream = true,
|
||||||
) {
|
) {
|
||||||
const peerId = call.peer;
|
const peerId = call.peer;
|
||||||
this.updateMediaConnection(peerId, type, {
|
this.updateMediaConnection(peerId, type, {
|
||||||
@ -444,6 +456,7 @@ class Peer extends EventTarget {
|
|||||||
|
|
||||||
call.on("stream", (remoteStream: MediaStream) => {
|
call.on("stream", (remoteStream: MediaStream) => {
|
||||||
console.log("stream", remoteStream);
|
console.log("stream", remoteStream);
|
||||||
|
if (!acceptRemoteStream) return;
|
||||||
// 只有当流中包含相应类型的轨道时才保存连接
|
// 只有当流中包含相应类型的轨道时才保存连接
|
||||||
const hasVideo = remoteStream.getVideoTracks().length > 0;
|
const hasVideo = remoteStream.getVideoTracks().length > 0;
|
||||||
const hasAudio = remoteStream.getAudioTracks().length > 0;
|
const hasAudio = remoteStream.getAudioTracks().length > 0;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user