summary refs log tree commit diff
path: root/webrtc/src/opcodes/SelectProtocol.ts
blob: 98899caf97055a953b1e49a7afc5992153250458 (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { WebSocket } from "@fosscord/gateway";
import { Payload } from "./index";
import { VoiceOPCodes } from "@fosscord/util";
import { Server } from "../Server";
import * as mediasoup from "mediasoup";
import { RtpCodecCapability } from "mediasoup/node/lib/RtpParameters";
import * as sdpTransform from 'sdp-transform';

/*

	Sent by client:
{
	"op": 1,
	"d": {
		"protocol": "webrtc",
		"data": "
			a=extmap-allow-mixed
			a=ice-ufrag:vNxb
			a=ice-pwd:tZvpbVPYEKcnW0gGRPq0OOnh
			a=ice-options:trickle
			a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level
			a=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time
			a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01
			a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid
			a=rtpmap:111 opus/48000/2
			a=extmap:14 urn:ietf:params:rtp-hdrext:toffset
			a=extmap:13 urn:3gpp:video-orientation
			a=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay
			a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type
			a=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing
			a=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space
			a=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id
			a=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id
			a=rtpmap:96 VP8/90000
			a=rtpmap:97 rtx/90000
		",
		"codecs": [
			{
				"name": "opus",
				"type": "audio",
				"priority": 1000,
				"payload_type": 111,
				"rtx_payload_type": null
			},
			{
				"name": "H264",
				"type": "video",
				"priority": 1000,
				"payload_type": 102,
				"rtx_payload_type": 121
			},
			{
				"name": "VP8",
				"type": "video",
				"priority": 2000,
				"payload_type": 96,
				"rtx_payload_type": 97
			},
			{
				"name": "VP9",
				"type": "video",
				"priority": 3000,
				"payload_type": 98,
				"rtx_payload_type": 99
			}
		],
		"rtc_connection_id": "3faa0b80-b3e2-4bae-b291-273801fbb7ab"
	}
}
*/

export async function onSelectProtocol(this: Server, socket: WebSocket, data: Payload) {
	const rtpCapabilities = this.mediasoupRouters[0].rtpCapabilities;
	const codecs = rtpCapabilities.codecs as RtpCodecCapability[];

	const transport = this.mediasoupTransports[0];	//whatever

	const res = sdpTransform.parse(data.d.sdp);

	const videoCodec = this.mediasoupRouters[0].rtpCapabilities.codecs!.find((x: any) => x.kind === "video");
	const audioCodec = this.mediasoupRouters[0].rtpCapabilities.codecs!.find((x: any) => x.kind === "audio");

	const producer = this.mediasoupProducers[0] || await transport.produce({
		kind: "audio",
		rtpParameters: {
			mid: "audio",
			codecs: [{
				clockRate: audioCodec!.clockRate,
				payloadType: audioCodec!.preferredPayloadType as number,
				mimeType: audioCodec!.mimeType,
				channels: audioCodec?.channels,
			}],
			headerExtensions: res.ext?.map(x => ({
				id: x.value,
				uri: x.uri,
			})),
		},
		paused: false,
	});

	console.log("can consume: " + this.mediasoupRouters[0].canConsume({ producerId: producer.id, rtpCapabilities: rtpCapabilities }));

	// const consumer = this.mediasoupConsumers[0] || await transport.consume({
	// 	producerId: producer.id,
	// 	paused: false,
	// 	rtpCapabilities,
	// });

	/*
		{
			"video_codec":"H264",
			"sdp":
			"
				m=audio 50010 ICE/SDP
				a=fingerprint:sha-256 4A:79:94:16:44:3F:BD:05:41:5A:C7:20:F3:12:54:70:00:73:5D:33:00:2D:2C:80:9B:39:E1:9F:2D:A7:49:87
				c=IN IP4 109.200.214.158
				a=rtcp:50010
				a=ice-ufrag:+npq
				a=ice-pwd:+jf7jAesMeHHby43FRqWTy
				a=fingerprint:sha-256 4A:79:94:16:44:3F:BD:05:41:5A:C7:20:F3:12:54:70:00:73:5D:33:00:2D:2C:80:9B:39:E1:9F:2D:A7:49:87
				a=candidate:1 1 UDP 4261412862 109.200.214.158 50010 typ host",
				"media_session_id":"59265c94fa13e313492c372c4c8da801
			",
			"audio_codec":"opus"
		}
	*/

	socket.send(JSON.stringify({
		op: VoiceOPCodes.SESSION_DESCRIPTION,
		d: {
			video_codec: videoCodec?.mimeType?.substring(6) || undefined,
			// mode: "xsalsa20_poly1305_lite",
			media_session_id: transport.id,
			audio_codec: audioCodec?.mimeType.substring(6),
			sdp: `m=audio ${transport.iceCandidates[0].port} ICE/SDP\n`
				+ `a=fingerprint:sha-256 ${transport.dtlsParameters.fingerprints.find(x => x.algorithm === "sha-256")?.value}\n`
				+ `c=IN IP4 ${transport.iceCandidates[0].ip}\n`
				+ `t=0 0\n`
				+ `a=ice-lite\n`
				+ `a=rtcp-mux\n`
				+ `a=rtcp:${transport.iceCandidates[0].port}\n`
				+ `a=ice-ufrag:${transport.iceParameters.usernameFragment}\n`
				+ `a=ice-pwd:${transport.iceParameters.password}\n`
				+ `a=fingerprint:sha-256 ${transport.dtlsParameters.fingerprints.find(x => x.algorithm === "sha-256")?.value}\n`
				+ `a=candidate:1 1 ${transport.iceCandidates[0].protocol.toUpperCase()} ${transport.iceCandidates[0].priority} ${transport.iceCandidates[0].ip} ${transport.iceCandidates[0].port} typ ${transport.iceCandidates[0].type}`
		}
	}));
}