Compare commits

..

No commits in common. "e98cd2844d4284260306fb658422a84e9b4b90c2" and "19b9621dee34bbce9d0b84b9c5577bc35497e72e" have entirely different histories.

3 changed files with 159 additions and 1295 deletions

File diff suppressed because it is too large Load Diff

View File

@ -83,7 +83,7 @@ const updateTasks = () => {
tasks.value = Array.from(
fileTransferMgrInstance
.getAllFileTransfers()
.flatMap((transfer) => transfer.getTasks()),
.flatMap((transfer) => transfer.getTasks())
);
// newTasks.forEach((task) => {
// const index = tasks.value.findIndex(
@ -109,7 +109,7 @@ updateTasks();
right: 0;
background: white;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
z-index: 10;
z-index: 1000;
}
.transfer-header {

View File

@ -7,7 +7,7 @@ import {
TransferStatus,
TransferTask,
} from "./fileTransfer";
import { message, notification } from "ant-design-vue";
import { message, Modal, notification } from "ant-design-vue";
import {
randomChars,
sign2peerid,
@ -86,10 +86,7 @@ class Peer extends EventTarget {
// 保存 this 引用
const self = this;
let handled = false;
const onOk = async () => {
if (handled) return;
handled = true;
try {
let stream: MediaStream;
@ -107,7 +104,7 @@ class Peer extends EventTarget {
}
call.answer(stream);
self.setupMediaConnection(call, type, stream, type !== "desktop");
self.setupMediaConnection(call, type, stream);
self.dispatchEvent(
new CustomEvent(
@ -115,7 +112,6 @@ class Peer extends EventTarget {
{
detail: {
peerId: call.peer,
role: type === "desktop" ? "sharer" : "caller",
},
},
),
@ -130,34 +126,20 @@ class Peer extends EventTarget {
message.error("无法访问" + (type === "desktop" ? "屏幕" : "麦克风"));
}
};
const onReject = () => {
if (handled) return;
handled = true;
call.close();
message.info(
"已拒绝" + (type === "desktop" ? "屏幕共享" : "语音通话") + "请求",
);
};
if (getPermissionSet()[permission]) {
await onOk();
} else if (type === "call") {
this.dispatchEvent(
new CustomEvent("media-request", {
detail: {
peerId: call.peer,
type,
accept: onOk,
reject: onReject,
},
}),
);
} else {
confirmWin(title, content, "接受", "拒绝")
.then(async () => {
await onOk();
})
.catch(() => {
onReject();
call.close();
message.info(
"已拒绝" +
(type === "desktop" ? "屏幕共享" : "语音通话") +
"请求",
);
});
}
});
@ -217,19 +199,23 @@ class Peer extends EventTarget {
}
try {
const offerStream = this.createDesktopOfferStream();
// 创建一个静音的音频流作为占位
const emptyStream = new MediaStream();
// 将音频轨道静音
emptyStream.getAudioTracks().forEach((track) => {
track.enabled = false;
});
const call = this.peer.call(sign2peerid(id), offerStream, {
const call = this.peer.call(sign2peerid(id), emptyStream, {
metadata: { type: "desktop" },
});
this.setupMediaConnection(call, "desktop", offerStream);
this.setupMediaConnection(call, "desktop", emptyStream);
this.dispatchEvent(
new CustomEvent("desktop-started", {
detail: {
peerId: call.peer,
role: "viewer",
},
}),
);
@ -241,22 +227,6 @@ 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({
@ -465,7 +435,6 @@ class Peer extends EventTarget {
call: MediaConnection,
type: MediaType,
localStream: MediaStream | null = null,
acceptRemoteStream = true,
) {
const peerId = call.peer;
this.updateMediaConnection(peerId, type, {
@ -475,7 +444,6 @@ 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;