blob: e69a9a82152441a905f899b1b019a3299aeb6555 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import data from "../../stores/data";
import { on, send } from "../channel/connection";
import type { ShareIceCandidateMessage } from "../channel/messages/messages";
import {
createTransfer,
onIncomingIceCandidate,
Transfer,
unregisterIceOnComplete,
} from "./transfer";
export async function createAnswerTransfer(
offer: RTCSessionDescriptionInit
): Promise<Transfer> {
const transfer = createTransfer(onChannel);
const offerDescription = new RTCSessionDescription(offer);
transfer.pc.setRemoteDescription(offerDescription);
const answer = await transfer.pc.createAnswer();
transfer.pc.setLocalDescription(answer);
transfer.pc.onicecandidate = event => {
const candidate = event.candidate;
if (event.candidate !== null) {
send("ice_candidate", { candidate });
}
};
const unregisterIce = on(
"share_ice_candidate",
(message: ShareIceCandidateMessage) =>
onIncomingIceCandidate(transfer, message)
);
unregisterIceOnComplete(transfer, unregisterIce);
send("accept_share", {
sdp: answer.sdp,
type: answer.type,
});
return transfer;
}
function onChannel(channel: RTCDataChannel, completeTransfer: () => void) {
channel.onmessage = event => {
data.set(event.data);
// TODO: Disconnect from channel
completeTransfer();
};
}
|