@gjsify/webrtc 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/esm/get-user-media.js +93 -0
- package/lib/esm/gst-enum-maps.js +88 -0
- package/lib/esm/gst-init.js +34 -0
- package/lib/esm/gst-stats-parser.js +79 -0
- package/lib/esm/gst-utils.js +16 -0
- package/lib/esm/index.js +53 -0
- package/lib/esm/media-device-info.js +23 -0
- package/lib/esm/media-devices.js +147 -0
- package/lib/esm/media-stream-track.js +142 -0
- package/lib/esm/media-stream.js +78 -0
- package/lib/esm/register/data-channel.js +8 -0
- package/lib/esm/register/error.js +8 -0
- package/lib/esm/register/media-devices.js +7 -0
- package/lib/esm/register/media.js +12 -0
- package/lib/esm/register/peer-connection.js +16 -0
- package/lib/esm/register.js +5 -0
- package/lib/esm/rtc-certificate.js +70 -0
- package/lib/esm/rtc-data-channel.js +266 -0
- package/lib/esm/rtc-dtls-transport.js +41 -0
- package/lib/esm/rtc-dtmf-sender.js +109 -0
- package/lib/esm/rtc-error.js +24 -0
- package/lib/esm/rtc-events.js +35 -0
- package/lib/esm/rtc-ice-candidate.js +75 -0
- package/lib/esm/rtc-ice-transport.js +96 -0
- package/lib/esm/rtc-peer-connection.js +855 -0
- package/lib/esm/rtc-rtp-receiver.js +91 -0
- package/lib/esm/rtc-rtp-sender.js +298 -0
- package/lib/esm/rtc-rtp-transceiver.js +97 -0
- package/lib/esm/rtc-sctp-transport.js +40 -0
- package/lib/esm/rtc-session-description.js +57 -0
- package/lib/esm/rtc-stats-report.js +35 -0
- package/lib/esm/rtc-track-event.js +29 -0
- package/lib/esm/rtp-capabilities.js +41 -0
- package/lib/esm/tee-multiplexer.js +62 -0
- package/lib/esm/wpt-helpers.js +122 -0
- package/lib/types/get-user-media.d.ts +14 -0
- package/lib/types/gst-enum-maps.d.ts +10 -0
- package/lib/types/gst-init.d.ts +5 -0
- package/lib/types/gst-stats-parser.d.ts +16 -0
- package/lib/types/gst-utils.d.ts +11 -0
- package/lib/types/index.d.ts +41 -0
- package/lib/types/media-device-info.d.ts +14 -0
- package/lib/types/media-devices.d.ts +12 -0
- package/lib/types/media-stream-track.d.ts +59 -0
- package/lib/types/media-stream.d.ts +28 -0
- package/lib/types/register/data-channel.d.ts +1 -0
- package/lib/types/register/error.d.ts +1 -0
- package/lib/types/register/media-devices.d.ts +1 -0
- package/lib/types/register/media.d.ts +1 -0
- package/lib/types/register/peer-connection.d.ts +1 -0
- package/lib/types/register.d.ts +5 -0
- package/lib/types/register.spec.d.ts +3 -0
- package/lib/types/rtc-certificate.d.ts +23 -0
- package/lib/types/rtc-data-channel.d.ts +64 -0
- package/lib/types/rtc-dtls-transport.d.ts +20 -0
- package/lib/types/rtc-dtmf-sender.d.ts +31 -0
- package/lib/types/rtc-error.d.ts +19 -0
- package/lib/types/rtc-events.d.ts +27 -0
- package/lib/types/rtc-ice-candidate.d.ts +28 -0
- package/lib/types/rtc-ice-transport.d.ts +56 -0
- package/lib/types/rtc-peer-connection.d.ts +165 -0
- package/lib/types/rtc-rtp-receiver.d.ts +45 -0
- package/lib/types/rtc-rtp-sender.d.ts +98 -0
- package/lib/types/rtc-rtp-transceiver.d.ts +20 -0
- package/lib/types/rtc-sctp-transport.d.ts +20 -0
- package/lib/types/rtc-session-description.d.ts +18 -0
- package/lib/types/rtc-stats-report.d.ts +22 -0
- package/lib/types/rtc-track-event.d.ts +18 -0
- package/lib/types/rtp-capabilities.d.ts +3 -0
- package/lib/types/tee-multiplexer.d.ts +25 -0
- package/lib/types/webrtc.spec.d.ts +2 -0
- package/lib/types/wpt-helpers.d.ts +30 -0
- package/lib/types/wpt-media.spec.d.ts +2 -0
- package/lib/types/wpt.spec.d.ts +2 -0
- package/package.json +74 -0
- package/src/get-user-media.ts +131 -0
- package/src/gst-enum-maps.ts +125 -0
- package/src/gst-init.ts +52 -0
- package/src/gst-stats-parser.ts +137 -0
- package/src/gst-utils.ts +41 -0
- package/src/index.ts +104 -0
- package/src/media-device-info.ts +33 -0
- package/src/media-devices.ts +191 -0
- package/src/media-stream-track.ts +159 -0
- package/src/media-stream.ts +96 -0
- package/src/register/data-channel.ts +11 -0
- package/src/register/error.ts +11 -0
- package/src/register/media-devices.ts +10 -0
- package/src/register/media.ts +15 -0
- package/src/register/peer-connection.ts +20 -0
- package/src/register.spec.ts +55 -0
- package/src/register.ts +10 -0
- package/src/rtc-certificate.ts +110 -0
- package/src/rtc-data-channel.ts +284 -0
- package/src/rtc-dtls-transport.ts +48 -0
- package/src/rtc-dtmf-sender.ts +146 -0
- package/src/rtc-error.ts +49 -0
- package/src/rtc-events.ts +64 -0
- package/src/rtc-ice-candidate.ts +115 -0
- package/src/rtc-ice-transport.ts +104 -0
- package/src/rtc-peer-connection.ts +1017 -0
- package/src/rtc-rtp-receiver.ts +122 -0
- package/src/rtc-rtp-sender.ts +444 -0
- package/src/rtc-rtp-transceiver.ts +127 -0
- package/src/rtc-sctp-transport.ts +48 -0
- package/src/rtc-session-description.ts +64 -0
- package/src/rtc-stats-report.ts +39 -0
- package/src/rtc-track-event.ts +45 -0
- package/src/rtp-capabilities.ts +48 -0
- package/src/tee-multiplexer.ts +75 -0
- package/src/test.mts +11 -0
- package/src/webrtc.spec.ts +1186 -0
- package/src/wpt-helpers.ts +156 -0
- package/src/wpt-media.spec.ts +1154 -0
- package/src/wpt.spec.ts +1136 -0
- package/tsconfig.json +36 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
import GLib from "gi://GLib?version=2.0";
|
|
2
|
+
import GObject from "gi://GObject?version=2.0";
|
|
3
|
+
import GstWebRTC from "gi://GstWebRTC?version=1.0";
|
|
4
|
+
import {
|
|
5
|
+
WebrtcbinBridge
|
|
6
|
+
} from "@gjsify/webrtc-native";
|
|
7
|
+
import { ensureWebrtcbinAvailable, Gst } from "./gst-init.js";
|
|
8
|
+
import { withGstPromise } from "./gst-utils.js";
|
|
9
|
+
import {
|
|
10
|
+
gstToSignalingState,
|
|
11
|
+
gstToConnectionState,
|
|
12
|
+
gstToIceConnectionState,
|
|
13
|
+
gstToIceGatheringState,
|
|
14
|
+
w3cDirectionToGst
|
|
15
|
+
} from "./gst-enum-maps.js";
|
|
16
|
+
import { RTCSessionDescription } from "./rtc-session-description.js";
|
|
17
|
+
import { RTCIceCandidate } from "./rtc-ice-candidate.js";
|
|
18
|
+
import { RTCDataChannel } from "./rtc-data-channel.js";
|
|
19
|
+
import { RTCPeerConnectionIceEvent, RTCDataChannelEvent } from "./rtc-events.js";
|
|
20
|
+
import { RTCRtpSender } from "./rtc-rtp-sender.js";
|
|
21
|
+
import { RTCRtpReceiver } from "./rtc-rtp-receiver.js";
|
|
22
|
+
import { RTCRtpTransceiver } from "./rtc-rtp-transceiver.js";
|
|
23
|
+
import { MediaStream } from "./media-stream.js";
|
|
24
|
+
import { MediaStreamTrack } from "./media-stream-track.js";
|
|
25
|
+
import { RTCTrackEvent } from "./rtc-track-event.js";
|
|
26
|
+
import { parseGstStats, filterStatsByTrackId } from "./gst-stats-parser.js";
|
|
27
|
+
import { RTCIceTransport } from "./rtc-ice-transport.js";
|
|
28
|
+
import { RTCDtlsTransport } from "./rtc-dtls-transport.js";
|
|
29
|
+
import { RTCSctpTransport } from "./rtc-sctp-transport.js";
|
|
30
|
+
import { RTCCertificate, generateCertificate } from "./rtc-certificate.js";
|
|
31
|
+
function coerceUnsignedShort(name, raw) {
|
|
32
|
+
const n = Number(raw);
|
|
33
|
+
if (!Number.isFinite(n)) {
|
|
34
|
+
throw new TypeError(`createDataChannel: ${name} must be a finite number, got ${String(raw)}`);
|
|
35
|
+
}
|
|
36
|
+
const truncated = Math.trunc(n);
|
|
37
|
+
if (truncated < 0 || truncated > 65535) {
|
|
38
|
+
throw new TypeError(`createDataChannel: ${name}=${truncated} is outside the [0, 65535] range`);
|
|
39
|
+
}
|
|
40
|
+
return truncated;
|
|
41
|
+
}
|
|
42
|
+
let globalCounter = 0;
|
|
43
|
+
class RTCPeerConnection extends EventTarget {
|
|
44
|
+
_pipeline;
|
|
45
|
+
_webrtcbin;
|
|
46
|
+
_bridge;
|
|
47
|
+
_conf;
|
|
48
|
+
_closed = false;
|
|
49
|
+
_iceRestartNeeded = false;
|
|
50
|
+
_hasNegotiated = false;
|
|
51
|
+
_dataChannels = /* @__PURE__ */ new Map();
|
|
52
|
+
_transceivers = /* @__PURE__ */ new Map();
|
|
53
|
+
_senders = [];
|
|
54
|
+
_receivers = [];
|
|
55
|
+
_iceTransport = null;
|
|
56
|
+
_dtlsTransport = null;
|
|
57
|
+
_sctpTransport = null;
|
|
58
|
+
canTrickleIceCandidates = true;
|
|
59
|
+
constructor(configuration) {
|
|
60
|
+
super();
|
|
61
|
+
ensureWebrtcbinAvailable();
|
|
62
|
+
const [major, minor] = Gst.version();
|
|
63
|
+
if (major < 1 || major === 1 && minor < 20) {
|
|
64
|
+
const DOMExc = globalThis.DOMException;
|
|
65
|
+
const msg = `@gjsify/webrtc requires GStreamer >= 1.20 (you have ${major}.${minor}). webrtcbin is only stable from 1.20 onward.`;
|
|
66
|
+
if (DOMExc) throw new DOMExc(msg, "NotSupportedError");
|
|
67
|
+
throw new Error(msg);
|
|
68
|
+
}
|
|
69
|
+
const id = ++globalCounter;
|
|
70
|
+
this._pipeline = new Gst.Pipeline({ name: `gjsify-webrtc-pipeline-${id}` });
|
|
71
|
+
const bin = Gst.ElementFactory.make("webrtcbin", `gjsify-webrtcbin-${id}`);
|
|
72
|
+
if (!bin) {
|
|
73
|
+
throw new Error("Failed to create webrtcbin element");
|
|
74
|
+
}
|
|
75
|
+
this._webrtcbin = bin;
|
|
76
|
+
this._conf = { ...configuration };
|
|
77
|
+
if (configuration?.certificates) {
|
|
78
|
+
for (const cert of configuration.certificates) {
|
|
79
|
+
if (cert instanceof RTCCertificate && cert.expires <= Date.now()) {
|
|
80
|
+
throw new DOMException(
|
|
81
|
+
"RTCPeerConnection: one of the provided certificates has expired",
|
|
82
|
+
"InvalidAccessError"
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
this._applyIceServers(configuration?.iceServers ?? []);
|
|
88
|
+
this._applyIceTransportPolicy(configuration?.iceTransportPolicy);
|
|
89
|
+
this._applyBundlePolicy(configuration?.bundlePolicy);
|
|
90
|
+
this._pipeline.add(this._webrtcbin);
|
|
91
|
+
this._bridge = new WebrtcbinBridge({ bin: this._webrtcbin });
|
|
92
|
+
this._bridge.connect("negotiation-needed", () => this._handleNegotiationNeeded());
|
|
93
|
+
this._bridge.connect("icecandidate", (_b, mlineIndex, candidate) => this._handleIceCandidate(mlineIndex, candidate));
|
|
94
|
+
this._bridge.connect("datachannel", (_b, channelBridge) => this._handleDataChannel(channelBridge));
|
|
95
|
+
this._bridge.connect("new-transceiver", (_b, gstTrans) => this._handleNewTransceiver(gstTrans));
|
|
96
|
+
this._bridge.connect("pad-added", (_b, pad) => this._handlePadAdded(pad));
|
|
97
|
+
this._bridge.connect("connection-state-changed", () => this._dispatchStateChange("connectionstatechange"));
|
|
98
|
+
this._bridge.connect("ice-connection-state-changed", () => this._dispatchStateChange("iceconnectionstatechange"));
|
|
99
|
+
this._bridge.connect("ice-gathering-state-changed", () => this._dispatchStateChange("icegatheringstatechange"));
|
|
100
|
+
this._bridge.connect("signaling-state-changed", () => this._dispatchStateChange("signalingstatechange"));
|
|
101
|
+
this._pipeline.set_state(Gst.State.PLAYING);
|
|
102
|
+
}
|
|
103
|
+
// ---- ICE server / policy config ---------------------------------------
|
|
104
|
+
_applyIceServers(iceServers) {
|
|
105
|
+
let stunSet = false;
|
|
106
|
+
for (const server of iceServers) {
|
|
107
|
+
const urls = Array.isArray(server.urls) ? server.urls : [server.urls];
|
|
108
|
+
if (urls.length === 0) {
|
|
109
|
+
throw new SyntaxError("RTCIceServer.urls must not be empty");
|
|
110
|
+
}
|
|
111
|
+
for (const url of urls) {
|
|
112
|
+
if (typeof url !== "string" || url.length === 0) {
|
|
113
|
+
throw new TypeError("RTCIceServer.urls entries must be non-empty strings");
|
|
114
|
+
}
|
|
115
|
+
const colonIdx = url.indexOf(":");
|
|
116
|
+
if (colonIdx < 0) {
|
|
117
|
+
throw new TypeError(`Invalid ICE server URL "${url}"`);
|
|
118
|
+
}
|
|
119
|
+
const proto = url.slice(0, colonIdx + 1);
|
|
120
|
+
const hostPort = url.slice(colonIdx + 1);
|
|
121
|
+
if (proto === "stun:" || proto === "stuns:") {
|
|
122
|
+
if (stunSet) continue;
|
|
123
|
+
this._webrtcbin.stun_server = `${proto}//${hostPort}`;
|
|
124
|
+
stunSet = true;
|
|
125
|
+
} else if (proto === "turn:" || proto === "turns:") {
|
|
126
|
+
if (typeof server.username !== "string" || typeof server.credential !== "string") {
|
|
127
|
+
throw new TypeError(`TURN server credential for ${url} missing`);
|
|
128
|
+
}
|
|
129
|
+
const encUser = encodeURIComponent(server.username);
|
|
130
|
+
const encCred = encodeURIComponent(server.credential);
|
|
131
|
+
const turnUrl = `${proto}//${encUser}:${encCred}@${hostPort}`;
|
|
132
|
+
try {
|
|
133
|
+
this._webrtcbin.emit("add-turn-server", turnUrl);
|
|
134
|
+
} catch {
|
|
135
|
+
this._webrtcbin.turn_server = turnUrl;
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
throw new TypeError(`Unsupported ICE server protocol "${proto}"`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
_applyIceTransportPolicy(policy) {
|
|
144
|
+
if (!policy) return;
|
|
145
|
+
const gstPolicy = policy === "relay" ? GstWebRTC.WebRTCICETransportPolicy.RELAY : GstWebRTC.WebRTCICETransportPolicy.ALL;
|
|
146
|
+
try {
|
|
147
|
+
this._webrtcbin.ice_transport_policy = gstPolicy;
|
|
148
|
+
} catch {
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
_applyBundlePolicy(policy) {
|
|
152
|
+
if (!policy) return;
|
|
153
|
+
let gstPolicy;
|
|
154
|
+
switch (policy) {
|
|
155
|
+
case "balanced":
|
|
156
|
+
gstPolicy = GstWebRTC.WebRTCBundlePolicy.BALANCED;
|
|
157
|
+
break;
|
|
158
|
+
case "max-compat":
|
|
159
|
+
gstPolicy = GstWebRTC.WebRTCBundlePolicy.MAX_COMPAT;
|
|
160
|
+
break;
|
|
161
|
+
case "max-bundle":
|
|
162
|
+
gstPolicy = GstWebRTC.WebRTCBundlePolicy.MAX_BUNDLE;
|
|
163
|
+
break;
|
|
164
|
+
default:
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
try {
|
|
168
|
+
this._webrtcbin.bundle_policy = gstPolicy;
|
|
169
|
+
} catch {
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
// ---- Properties --------------------------------------------------------
|
|
173
|
+
get signalingState() {
|
|
174
|
+
if (this._closed) return "closed";
|
|
175
|
+
try {
|
|
176
|
+
return gstToSignalingState(this._webrtcbin.signaling_state);
|
|
177
|
+
} catch {
|
|
178
|
+
return "stable";
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
get connectionState() {
|
|
182
|
+
if (this._closed) return "closed";
|
|
183
|
+
try {
|
|
184
|
+
return gstToConnectionState(this._webrtcbin.connection_state);
|
|
185
|
+
} catch {
|
|
186
|
+
return "new";
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
get iceConnectionState() {
|
|
190
|
+
if (this._closed) return "closed";
|
|
191
|
+
try {
|
|
192
|
+
return gstToIceConnectionState(this._webrtcbin.ice_connection_state);
|
|
193
|
+
} catch {
|
|
194
|
+
return "new";
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
get iceGatheringState() {
|
|
198
|
+
try {
|
|
199
|
+
return gstToIceGatheringState(this._webrtcbin.ice_gathering_state);
|
|
200
|
+
} catch {
|
|
201
|
+
return "new";
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
_descProp(prop) {
|
|
205
|
+
try {
|
|
206
|
+
const desc = this._webrtcbin[prop];
|
|
207
|
+
if (!desc) return null;
|
|
208
|
+
return RTCSessionDescription.fromGstDesc(desc);
|
|
209
|
+
} catch {
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
get localDescription() {
|
|
214
|
+
return this._descProp("local_description");
|
|
215
|
+
}
|
|
216
|
+
get remoteDescription() {
|
|
217
|
+
return this._descProp("remote_description");
|
|
218
|
+
}
|
|
219
|
+
get currentLocalDescription() {
|
|
220
|
+
return this._descProp("current_local_description");
|
|
221
|
+
}
|
|
222
|
+
get currentRemoteDescription() {
|
|
223
|
+
return this._descProp("current_remote_description");
|
|
224
|
+
}
|
|
225
|
+
get pendingLocalDescription() {
|
|
226
|
+
return this._descProp("pending_local_description");
|
|
227
|
+
}
|
|
228
|
+
get pendingRemoteDescription() {
|
|
229
|
+
return this._descProp("pending_remote_description");
|
|
230
|
+
}
|
|
231
|
+
get sctp() {
|
|
232
|
+
return this._sctpTransport;
|
|
233
|
+
}
|
|
234
|
+
get peerIdentity() {
|
|
235
|
+
return Promise.reject(new TypeError("peerIdentity assertions are not implemented"));
|
|
236
|
+
}
|
|
237
|
+
get idpErrorInfo() {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
get idpLoginUrl() {
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
// ---- Core methods ------------------------------------------------------
|
|
244
|
+
_rejectIfClosed(method) {
|
|
245
|
+
if (!this._closed) return;
|
|
246
|
+
const DOMExc = globalThis.DOMException;
|
|
247
|
+
const msg = `RTCPeerConnection.${method}: connection is closed`;
|
|
248
|
+
throw DOMExc ? new DOMExc(msg, "InvalidStateError") : new Error(msg);
|
|
249
|
+
}
|
|
250
|
+
async createOffer(_options) {
|
|
251
|
+
this._rejectIfClosed("createOffer");
|
|
252
|
+
const opts = Gst.Structure.new_empty("offer-options");
|
|
253
|
+
if (this._iceRestartNeeded) {
|
|
254
|
+
this._setStructureField(opts, "ice-restart", "boolean", true);
|
|
255
|
+
this._iceRestartNeeded = false;
|
|
256
|
+
}
|
|
257
|
+
const reply = await withGstPromise((p) => {
|
|
258
|
+
this._webrtcbin.emit("create-offer", opts, p);
|
|
259
|
+
});
|
|
260
|
+
const desc = reply.get_value("offer");
|
|
261
|
+
return RTCSessionDescription.fromGstDesc(desc).toJSON();
|
|
262
|
+
}
|
|
263
|
+
async createAnswer(_options) {
|
|
264
|
+
this._rejectIfClosed("createAnswer");
|
|
265
|
+
const opts = Gst.Structure.new_empty("answer-options");
|
|
266
|
+
const reply = await withGstPromise((p) => {
|
|
267
|
+
this._webrtcbin.emit("create-answer", opts, p);
|
|
268
|
+
});
|
|
269
|
+
const desc = reply.get_value("answer");
|
|
270
|
+
return RTCSessionDescription.fromGstDesc(desc).toJSON();
|
|
271
|
+
}
|
|
272
|
+
async setLocalDescription(description) {
|
|
273
|
+
this._rejectIfClosed("setLocalDescription");
|
|
274
|
+
if (!description || !description.type || !description.sdp) {
|
|
275
|
+
const state = this.signalingState;
|
|
276
|
+
if (state === "stable" || state === "have-local-offer") {
|
|
277
|
+
description = await this.createOffer();
|
|
278
|
+
} else if (state === "have-remote-offer" || state === "have-remote-pranswer") {
|
|
279
|
+
description = await this.createAnswer();
|
|
280
|
+
} else {
|
|
281
|
+
const DOMExc = globalThis.DOMException;
|
|
282
|
+
const msg = `setLocalDescription: cannot auto-create SDP in signalingState '${state}'`;
|
|
283
|
+
throw DOMExc ? new DOMExc(msg, "InvalidStateError") : new Error(msg);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
this._pipeline.set_state(Gst.State.PLAYING);
|
|
287
|
+
const gstDesc = new RTCSessionDescription(description).toGstDesc();
|
|
288
|
+
await withGstPromise((p) => {
|
|
289
|
+
this._webrtcbin.emit("set-local-description", gstDesc, p);
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
async setRemoteDescription(description) {
|
|
293
|
+
this._rejectIfClosed("setRemoteDescription");
|
|
294
|
+
if (!description || !description.sdp || !description.type) {
|
|
295
|
+
throw new TypeError("setRemoteDescription requires an RTCSessionDescriptionInit with sdp and type");
|
|
296
|
+
}
|
|
297
|
+
this._pipeline.set_state(Gst.State.PLAYING);
|
|
298
|
+
const gstDesc = new RTCSessionDescription(description).toGstDesc();
|
|
299
|
+
await withGstPromise((p) => {
|
|
300
|
+
this._webrtcbin.emit("set-remote-description", gstDesc, p);
|
|
301
|
+
});
|
|
302
|
+
if (this.signalingState === "stable") {
|
|
303
|
+
this._hasNegotiated = true;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
async addIceCandidate(candidate) {
|
|
307
|
+
this._rejectIfClosed("addIceCandidate");
|
|
308
|
+
if (!candidate) return;
|
|
309
|
+
const { candidate: cand, sdpMLineIndex } = candidate;
|
|
310
|
+
if (typeof cand !== "string" || typeof sdpMLineIndex !== "number") return;
|
|
311
|
+
this._webrtcbin.emit("add-ice-candidate", sdpMLineIndex, cand);
|
|
312
|
+
}
|
|
313
|
+
createDataChannel(label, options = {}) {
|
|
314
|
+
if (this._closed) {
|
|
315
|
+
const DOMExc = globalThis.DOMException;
|
|
316
|
+
const msg = "Cannot create a data channel on a closed RTCPeerConnection";
|
|
317
|
+
if (DOMExc) throw new DOMExc(msg, "InvalidStateError");
|
|
318
|
+
throw new Error(msg);
|
|
319
|
+
}
|
|
320
|
+
if (typeof label !== "string") {
|
|
321
|
+
throw new TypeError("createDataChannel: label must be a string");
|
|
322
|
+
}
|
|
323
|
+
if (new TextEncoder().encode(label).byteLength > 65535) {
|
|
324
|
+
throw new TypeError("createDataChannel: label too long (> 65535 bytes)");
|
|
325
|
+
}
|
|
326
|
+
const maxPacketLifeTime = options.maxPacketLifeTime == null ? void 0 : coerceUnsignedShort("maxPacketLifeTime", options.maxPacketLifeTime);
|
|
327
|
+
const maxRetransmits = options.maxRetransmits == null ? void 0 : coerceUnsignedShort("maxRetransmits", options.maxRetransmits);
|
|
328
|
+
const id = options.id == null ? void 0 : coerceUnsignedShort("id", options.id);
|
|
329
|
+
if (maxPacketLifeTime !== void 0 && maxRetransmits !== void 0) {
|
|
330
|
+
throw new TypeError("createDataChannel: maxPacketLifeTime and maxRetransmits are mutually exclusive");
|
|
331
|
+
}
|
|
332
|
+
if (options.negotiated === true && id === void 0) {
|
|
333
|
+
throw new TypeError("createDataChannel: negotiated=true requires an id");
|
|
334
|
+
}
|
|
335
|
+
if (id === 65535) {
|
|
336
|
+
throw new TypeError("createDataChannel: id 65535 is reserved");
|
|
337
|
+
}
|
|
338
|
+
const gstOpts = Gst.Structure.new_empty("data-channel-opts");
|
|
339
|
+
this._setStructureField(gstOpts, "ordered", "boolean", options.ordered);
|
|
340
|
+
this._setStructureField(gstOpts, "max-packet-lifetime", "int", maxPacketLifeTime);
|
|
341
|
+
this._setStructureField(gstOpts, "max-retransmits", "int", maxRetransmits);
|
|
342
|
+
this._setStructureField(gstOpts, "protocol", "string", options.protocol);
|
|
343
|
+
this._setStructureField(gstOpts, "negotiated", "boolean", options.negotiated);
|
|
344
|
+
this._setStructureField(gstOpts, "id", "int", id);
|
|
345
|
+
let native = null;
|
|
346
|
+
try {
|
|
347
|
+
native = this._webrtcbin.emit("create-data-channel", label, gstOpts);
|
|
348
|
+
} catch (err) {
|
|
349
|
+
throw new Error(`create-data-channel failed: ${err?.message ?? err}`);
|
|
350
|
+
}
|
|
351
|
+
if (!native) {
|
|
352
|
+
throw new Error("webrtcbin returned null data channel (check id/label/options)");
|
|
353
|
+
}
|
|
354
|
+
this._ensureSctpTransport();
|
|
355
|
+
const js = new RTCDataChannel(native);
|
|
356
|
+
this._dataChannels.set(native, js);
|
|
357
|
+
js.addEventListener("close", () => {
|
|
358
|
+
this._dataChannels.delete(native);
|
|
359
|
+
});
|
|
360
|
+
return js;
|
|
361
|
+
}
|
|
362
|
+
_setStructureField(structure, name, type, value) {
|
|
363
|
+
if (value == null) return;
|
|
364
|
+
const gvalue = new GObject.Value();
|
|
365
|
+
if (type === "boolean") {
|
|
366
|
+
gvalue.init(GObject.TYPE_BOOLEAN);
|
|
367
|
+
gvalue.set_boolean(Boolean(value));
|
|
368
|
+
} else if (type === "int") {
|
|
369
|
+
gvalue.init(GObject.TYPE_INT);
|
|
370
|
+
gvalue.set_int(Number(value));
|
|
371
|
+
} else if (type === "string") {
|
|
372
|
+
gvalue.init(GObject.TYPE_STRING);
|
|
373
|
+
gvalue.set_string(String(value));
|
|
374
|
+
}
|
|
375
|
+
structure.set_value(name, gvalue);
|
|
376
|
+
gvalue.unset();
|
|
377
|
+
}
|
|
378
|
+
getConfiguration() {
|
|
379
|
+
return { ...this._conf };
|
|
380
|
+
}
|
|
381
|
+
close() {
|
|
382
|
+
if (this._closed) return;
|
|
383
|
+
this._closed = true;
|
|
384
|
+
GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
385
|
+
try {
|
|
386
|
+
this._pipeline.set_state(Gst.State.NULL);
|
|
387
|
+
} catch {
|
|
388
|
+
}
|
|
389
|
+
for (const ch of this._dataChannels.values()) {
|
|
390
|
+
try {
|
|
391
|
+
ch._disconnectSignals();
|
|
392
|
+
} catch {
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
this._dataChannels.clear();
|
|
396
|
+
for (const s of this._senders) {
|
|
397
|
+
try {
|
|
398
|
+
s._teardownPipeline();
|
|
399
|
+
} catch {
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
for (const r of this._receivers) {
|
|
403
|
+
try {
|
|
404
|
+
r._dispose();
|
|
405
|
+
} catch {
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
this._transceivers.clear();
|
|
409
|
+
this._senders.length = 0;
|
|
410
|
+
this._receivers.length = 0;
|
|
411
|
+
if (this._dtlsTransport) this._dtlsTransport._setState("closed");
|
|
412
|
+
if (this._iceTransport) this._iceTransport._setState("closed");
|
|
413
|
+
if (this._sctpTransport) this._sctpTransport._setState("closed");
|
|
414
|
+
try {
|
|
415
|
+
this._bridge.dispose_bridge();
|
|
416
|
+
} catch {
|
|
417
|
+
}
|
|
418
|
+
return GLib.SOURCE_REMOVE;
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
// ---- Media / Transceiver API (Phase 2) ----------------------------------
|
|
422
|
+
addTransceiver(trackOrKind, init) {
|
|
423
|
+
this._rejectIfClosed("addTransceiver");
|
|
424
|
+
let kind;
|
|
425
|
+
if (typeof trackOrKind === "string") {
|
|
426
|
+
if (trackOrKind !== "audio" && trackOrKind !== "video") {
|
|
427
|
+
throw new TypeError(
|
|
428
|
+
`Failed to execute 'addTransceiver' on 'RTCPeerConnection': The provided value '${trackOrKind}' is not a valid enum value of type MediaStreamTrackKind.`
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
kind = trackOrKind;
|
|
432
|
+
} else if (trackOrKind instanceof MediaStreamTrack) {
|
|
433
|
+
kind = trackOrKind.kind;
|
|
434
|
+
} else {
|
|
435
|
+
throw new TypeError(
|
|
436
|
+
"Failed to execute 'addTransceiver' on 'RTCPeerConnection': parameter 1 is not of type 'MediaStreamTrack' or a valid MediaStreamTrackKind."
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
if (init?.sendEncodings) {
|
|
440
|
+
const rids = /* @__PURE__ */ new Set();
|
|
441
|
+
for (const enc of init.sendEncodings) {
|
|
442
|
+
if (enc.rid !== void 0) {
|
|
443
|
+
if (typeof enc.rid !== "string" || enc.rid.length === 0 || enc.rid.length > 16 || !/^[a-zA-Z0-9]+$/.test(enc.rid)) {
|
|
444
|
+
throw new TypeError(`Invalid RID value: ${enc.rid}`);
|
|
445
|
+
}
|
|
446
|
+
if (rids.has(enc.rid)) {
|
|
447
|
+
throw new TypeError(`Duplicate RID: ${enc.rid}`);
|
|
448
|
+
}
|
|
449
|
+
rids.add(enc.rid);
|
|
450
|
+
}
|
|
451
|
+
if (enc.scaleResolutionDownBy !== void 0 && enc.scaleResolutionDownBy < 1) {
|
|
452
|
+
throw new RangeError("scaleResolutionDownBy must be >= 1.0");
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
const direction = init?.direction ?? "sendrecv";
|
|
457
|
+
const validDirections = ["sendrecv", "sendonly", "recvonly", "inactive"];
|
|
458
|
+
if (!validDirections.includes(direction)) {
|
|
459
|
+
throw new TypeError(
|
|
460
|
+
`Failed to execute 'addTransceiver' on 'RTCPeerConnection': The provided value '${direction}' is not a valid enum value of type RTCRtpTransceiverDirection.`
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
const hasGstSource = trackOrKind instanceof MediaStreamTrack && trackOrKind._gstSource;
|
|
464
|
+
const wantsSend = direction === "sendrecv" || direction === "sendonly";
|
|
465
|
+
let gstTrans;
|
|
466
|
+
let jsTrans;
|
|
467
|
+
if (hasGstSource && wantsSend) {
|
|
468
|
+
const track = trackOrKind;
|
|
469
|
+
const sender = new RTCRtpSender(null, this._pipeline, this._webrtcbin);
|
|
470
|
+
sender._kind = kind;
|
|
471
|
+
sender._onPipelineChanged = (newPipeline) => {
|
|
472
|
+
this._pipeline = newPipeline;
|
|
473
|
+
};
|
|
474
|
+
sender._setTrack(track);
|
|
475
|
+
sender._wirePipeline(track);
|
|
476
|
+
gstTrans = this._findNewGstTransceiver();
|
|
477
|
+
if (!gstTrans) {
|
|
478
|
+
throw new Error("webrtcbin did not create a transceiver for the send pad");
|
|
479
|
+
}
|
|
480
|
+
const gstReceiver = gstTrans.receiver ?? null;
|
|
481
|
+
const receiver = new RTCRtpReceiver(kind, gstReceiver, this._pipeline);
|
|
482
|
+
const statsDelegate = (t) => this.getStats(t);
|
|
483
|
+
sender._getStatsForTrack = statsDelegate;
|
|
484
|
+
receiver._getStatsForTrack = statsDelegate;
|
|
485
|
+
const dtls = this._ensureTransports();
|
|
486
|
+
sender._transport = dtls;
|
|
487
|
+
receiver._transport = dtls;
|
|
488
|
+
jsTrans = new RTCRtpTransceiver(gstTrans, sender, receiver);
|
|
489
|
+
sender._transceiver = jsTrans;
|
|
490
|
+
this._transceivers.set(gstTrans, jsTrans);
|
|
491
|
+
this._senders.push(sender);
|
|
492
|
+
this._receivers.push(receiver);
|
|
493
|
+
gstTrans.direction = w3cDirectionToGst(direction);
|
|
494
|
+
} else {
|
|
495
|
+
const caps = Gst.Caps.from_string(`application/x-rtp,media=${kind}`);
|
|
496
|
+
const createDirection = direction === "inactive" ? w3cDirectionToGst("sendrecv") : w3cDirectionToGst(direction);
|
|
497
|
+
gstTrans = this._webrtcbin.emit("add-transceiver", createDirection, caps);
|
|
498
|
+
if (!gstTrans) {
|
|
499
|
+
throw new Error("webrtcbin did not create a transceiver");
|
|
500
|
+
}
|
|
501
|
+
jsTrans = this._transceivers.get(gstTrans);
|
|
502
|
+
if (!jsTrans) {
|
|
503
|
+
jsTrans = this._createTransceiverWrapper(gstTrans);
|
|
504
|
+
}
|
|
505
|
+
gstTrans.direction = w3cDirectionToGst(direction);
|
|
506
|
+
if (trackOrKind instanceof MediaStreamTrack) {
|
|
507
|
+
jsTrans.sender._setTrack(trackOrKind);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return jsTrans;
|
|
511
|
+
}
|
|
512
|
+
addTrack(track, ..._streams) {
|
|
513
|
+
this._rejectIfClosed("addTrack");
|
|
514
|
+
if (!(track instanceof MediaStreamTrack)) {
|
|
515
|
+
throw new TypeError(
|
|
516
|
+
"Failed to execute 'addTrack' on 'RTCPeerConnection': parameter 1 is not a MediaStreamTrack"
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
const existing = this._senders.find((s) => s.track === track);
|
|
520
|
+
if (existing) {
|
|
521
|
+
throw new DOMException(
|
|
522
|
+
"Track already exists in a sender of this connection",
|
|
523
|
+
"InvalidAccessError"
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
let reusable;
|
|
527
|
+
for (const t of this._transceivers.values()) {
|
|
528
|
+
if (t.sender.track === null && !t.stopped && t.direction !== "stopped" && t.receiver.track.kind === track.kind) {
|
|
529
|
+
const dir = t.direction;
|
|
530
|
+
if (dir === "recvonly" || dir === "inactive") {
|
|
531
|
+
reusable = t;
|
|
532
|
+
break;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (reusable) {
|
|
537
|
+
const dir = reusable.direction;
|
|
538
|
+
reusable.direction = dir === "recvonly" ? "sendrecv" : "sendonly";
|
|
539
|
+
reusable.sender._setTrack(track);
|
|
540
|
+
return reusable.sender;
|
|
541
|
+
}
|
|
542
|
+
const transceiver = this.addTransceiver(track, { direction: "sendrecv" });
|
|
543
|
+
return transceiver.sender;
|
|
544
|
+
}
|
|
545
|
+
removeTrack(sender) {
|
|
546
|
+
this._rejectIfClosed("removeTrack");
|
|
547
|
+
if (!this._senders.includes(sender)) {
|
|
548
|
+
throw new DOMException(
|
|
549
|
+
"sender was not created by this connection",
|
|
550
|
+
"InvalidAccessError"
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
sender._setTrack(null);
|
|
554
|
+
}
|
|
555
|
+
getSenders() {
|
|
556
|
+
return [...this._senders];
|
|
557
|
+
}
|
|
558
|
+
getReceivers() {
|
|
559
|
+
return [...this._receivers];
|
|
560
|
+
}
|
|
561
|
+
getTransceivers() {
|
|
562
|
+
return [...this._transceivers.values()];
|
|
563
|
+
}
|
|
564
|
+
async getStats(selector) {
|
|
565
|
+
this._rejectIfClosed("getStats");
|
|
566
|
+
if (selector != null && selector instanceof MediaStreamTrack) {
|
|
567
|
+
const hasSender = this._senders.some((s) => s.track === selector);
|
|
568
|
+
const hasReceiver = this._receivers.some((r) => r.track === selector);
|
|
569
|
+
if (!hasSender && !hasReceiver) {
|
|
570
|
+
const DOMExc = globalThis.DOMException;
|
|
571
|
+
const msg = "The selector track is not associated with a sender or receiver of this connection";
|
|
572
|
+
throw DOMExc ? new DOMExc(msg, "InvalidAccessError") : new Error(msg);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
const reply = await withGstPromise((p) => {
|
|
576
|
+
this._webrtcbin.emit("get-stats", null, p);
|
|
577
|
+
});
|
|
578
|
+
const report = parseGstStats(reply);
|
|
579
|
+
if (selector != null && selector instanceof MediaStreamTrack) {
|
|
580
|
+
return filterStatsByTrackId(report, selector.id);
|
|
581
|
+
}
|
|
582
|
+
return report;
|
|
583
|
+
}
|
|
584
|
+
// ---- ICE restart / reconfiguration (Phase 4.4) ---------------------------
|
|
585
|
+
restartIce() {
|
|
586
|
+
if (this._closed) return;
|
|
587
|
+
this._iceRestartNeeded = true;
|
|
588
|
+
if (this._hasNegotiated) {
|
|
589
|
+
Promise.resolve().then(() => {
|
|
590
|
+
if (this._closed) return;
|
|
591
|
+
this._handleNegotiationNeeded();
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
setConfiguration(configuration) {
|
|
596
|
+
this._rejectIfClosed("setConfiguration");
|
|
597
|
+
if (configuration.bundlePolicy && configuration.bundlePolicy !== (this._conf.bundlePolicy ?? "balanced")) {
|
|
598
|
+
const DOMExc = globalThis.DOMException;
|
|
599
|
+
const msg = "setConfiguration: bundlePolicy cannot be changed";
|
|
600
|
+
throw DOMExc ? new DOMExc(msg, "InvalidModificationError") : new Error(msg);
|
|
601
|
+
}
|
|
602
|
+
if (configuration.rtcpMuxPolicy && configuration.rtcpMuxPolicy !== (this._conf.rtcpMuxPolicy ?? "require")) {
|
|
603
|
+
const DOMExc = globalThis.DOMException;
|
|
604
|
+
const msg = "setConfiguration: rtcpMuxPolicy cannot be changed";
|
|
605
|
+
throw DOMExc ? new DOMExc(msg, "InvalidModificationError") : new Error(msg);
|
|
606
|
+
}
|
|
607
|
+
if (configuration.iceServers) {
|
|
608
|
+
this._applyIceServers(configuration.iceServers);
|
|
609
|
+
}
|
|
610
|
+
if (configuration.iceTransportPolicy) {
|
|
611
|
+
this._applyIceTransportPolicy(configuration.iceTransportPolicy);
|
|
612
|
+
}
|
|
613
|
+
this._conf = { ...this._conf, ...configuration };
|
|
614
|
+
}
|
|
615
|
+
getIdentityAssertion() {
|
|
616
|
+
return Promise.reject(new Error("getIdentityAssertion is not implemented"));
|
|
617
|
+
}
|
|
618
|
+
// ---- Transceiver helper -------------------------------------------------
|
|
619
|
+
/** Find a GstWebRTCRTPTransceiver not yet in our map (created by request_pad_simple). */
|
|
620
|
+
_findNewGstTransceiver() {
|
|
621
|
+
for (let i = 0; ; i++) {
|
|
622
|
+
const gt = this._webrtcbin.emit("get-transceiver", i);
|
|
623
|
+
if (!gt) return null;
|
|
624
|
+
if (!this._transceivers.has(gt)) return gt;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
/** Lazily create the shared DTLS and ICE transport instances (max-bundle → one pair). */
|
|
628
|
+
_ensureTransports() {
|
|
629
|
+
if (!this._dtlsTransport) {
|
|
630
|
+
this._iceTransport = new RTCIceTransport();
|
|
631
|
+
this._dtlsTransport = new RTCDtlsTransport(this._iceTransport);
|
|
632
|
+
}
|
|
633
|
+
return this._dtlsTransport;
|
|
634
|
+
}
|
|
635
|
+
/** Create the SCTP transport when a data channel is first negotiated. */
|
|
636
|
+
_ensureSctpTransport() {
|
|
637
|
+
if (this._sctpTransport) return;
|
|
638
|
+
const dtls = this._ensureTransports();
|
|
639
|
+
this._sctpTransport = new RTCSctpTransport(dtls);
|
|
640
|
+
}
|
|
641
|
+
_createTransceiverWrapper(gstTrans) {
|
|
642
|
+
let kind = "audio";
|
|
643
|
+
try {
|
|
644
|
+
const gstKind = gstTrans.kind;
|
|
645
|
+
if (gstKind === GstWebRTC.WebRTCKind.VIDEO) kind = "video";
|
|
646
|
+
} catch {
|
|
647
|
+
}
|
|
648
|
+
const gstReceiver = gstTrans.receiver ?? null;
|
|
649
|
+
const gstSender = gstTrans.sender ?? null;
|
|
650
|
+
const receiver = new RTCRtpReceiver(kind, gstReceiver, this._pipeline);
|
|
651
|
+
const sender = new RTCRtpSender(gstSender, this._pipeline, this._webrtcbin);
|
|
652
|
+
sender._kind = kind;
|
|
653
|
+
sender._onPipelineChanged = (newPipeline) => {
|
|
654
|
+
this._pipeline = newPipeline;
|
|
655
|
+
};
|
|
656
|
+
const statsDelegate = (track) => this.getStats(track);
|
|
657
|
+
sender._getStatsForTrack = statsDelegate;
|
|
658
|
+
receiver._getStatsForTrack = statsDelegate;
|
|
659
|
+
const dtls = this._ensureTransports();
|
|
660
|
+
sender._transport = dtls;
|
|
661
|
+
receiver._transport = dtls;
|
|
662
|
+
try {
|
|
663
|
+
const mline = gstTrans.mlineindex;
|
|
664
|
+
if (typeof mline === "number" && mline >= 0) {
|
|
665
|
+
sender._setMlineIndex(mline);
|
|
666
|
+
}
|
|
667
|
+
} catch {
|
|
668
|
+
}
|
|
669
|
+
const transceiver = new RTCRtpTransceiver(gstTrans, sender, receiver);
|
|
670
|
+
sender._transceiver = transceiver;
|
|
671
|
+
this._transceivers.set(gstTrans, transceiver);
|
|
672
|
+
this._senders.push(sender);
|
|
673
|
+
this._receivers.push(receiver);
|
|
674
|
+
return transceiver;
|
|
675
|
+
}
|
|
676
|
+
// ---- Signal handlers ---------------------------------------------------
|
|
677
|
+
// The WebrtcbinBridge (webrtc-native) has already marshalled these from
|
|
678
|
+
// the GStreamer streaming thread onto the GLib main context, so we can
|
|
679
|
+
// synchronously dispatch from here.
|
|
680
|
+
_handleNegotiationNeeded() {
|
|
681
|
+
const ev = new Event("negotiationneeded");
|
|
682
|
+
this._onnegotiationneeded?.call(this, ev);
|
|
683
|
+
this.dispatchEvent(ev);
|
|
684
|
+
}
|
|
685
|
+
_handleIceCandidate(sdpMLineIndex, candidate) {
|
|
686
|
+
const cand = new RTCIceCandidate({ candidate, sdpMLineIndex });
|
|
687
|
+
const ev = new RTCPeerConnectionIceEvent("icecandidate", { candidate: cand });
|
|
688
|
+
this._onicecandidate?.call(this, ev);
|
|
689
|
+
this.dispatchEvent(ev);
|
|
690
|
+
}
|
|
691
|
+
_handleNewTransceiver(gstTrans) {
|
|
692
|
+
if (this._closed) return;
|
|
693
|
+
if (this._transceivers.has(gstTrans)) return;
|
|
694
|
+
this._createTransceiverWrapper(gstTrans);
|
|
695
|
+
}
|
|
696
|
+
_handlePadAdded(pad) {
|
|
697
|
+
if (this._closed) return;
|
|
698
|
+
if (pad.direction !== Gst.PadDirection.SRC) return;
|
|
699
|
+
const gstTrans = pad.transceiver;
|
|
700
|
+
if (!gstTrans) return;
|
|
701
|
+
let jsTrans = this._transceivers.get(gstTrans);
|
|
702
|
+
if (!jsTrans) {
|
|
703
|
+
jsTrans = this._createTransceiverWrapper(gstTrans);
|
|
704
|
+
}
|
|
705
|
+
jsTrans.receiver._connectToPad(pad);
|
|
706
|
+
const stream = new MediaStream([jsTrans.receiver.track]);
|
|
707
|
+
const ev = new RTCTrackEvent("track", {
|
|
708
|
+
receiver: jsTrans.receiver,
|
|
709
|
+
track: jsTrans.receiver.track,
|
|
710
|
+
streams: [stream],
|
|
711
|
+
transceiver: jsTrans
|
|
712
|
+
});
|
|
713
|
+
this._ontrack?.call(this, ev);
|
|
714
|
+
this.dispatchEvent(ev);
|
|
715
|
+
}
|
|
716
|
+
_handleDataChannel(channelBridge) {
|
|
717
|
+
this._ensureSctpTransport();
|
|
718
|
+
const native = channelBridge.channel;
|
|
719
|
+
let js = this._dataChannels.get(native);
|
|
720
|
+
if (!js) {
|
|
721
|
+
js = new RTCDataChannel(channelBridge);
|
|
722
|
+
this._dataChannels.set(native, js);
|
|
723
|
+
js.addEventListener("close", () => {
|
|
724
|
+
this._dataChannels.delete(native);
|
|
725
|
+
});
|
|
726
|
+
}
|
|
727
|
+
const ev = new RTCDataChannelEvent("datachannel", { channel: js });
|
|
728
|
+
this._ondatachannel?.call(this, ev);
|
|
729
|
+
this.dispatchEvent(ev);
|
|
730
|
+
}
|
|
731
|
+
_dispatchStateChange(type) {
|
|
732
|
+
if (type === "connectionstatechange") {
|
|
733
|
+
this._syncDtlsState();
|
|
734
|
+
} else if (type === "iceconnectionstatechange") {
|
|
735
|
+
this._syncIceState();
|
|
736
|
+
} else if (type === "icegatheringstatechange") {
|
|
737
|
+
this._syncIceGatheringState();
|
|
738
|
+
}
|
|
739
|
+
const ev = new Event(type);
|
|
740
|
+
switch (type) {
|
|
741
|
+
case "connectionstatechange":
|
|
742
|
+
this._onconnectionstatechange?.call(this, ev);
|
|
743
|
+
break;
|
|
744
|
+
case "iceconnectionstatechange":
|
|
745
|
+
this._oniceconnectionstatechange?.call(this, ev);
|
|
746
|
+
break;
|
|
747
|
+
case "icegatheringstatechange":
|
|
748
|
+
this._onicegatheringstatechange?.call(this, ev);
|
|
749
|
+
break;
|
|
750
|
+
case "signalingstatechange":
|
|
751
|
+
this._onsignalingstatechange?.call(this, ev);
|
|
752
|
+
break;
|
|
753
|
+
}
|
|
754
|
+
this.dispatchEvent(ev);
|
|
755
|
+
}
|
|
756
|
+
/** Map PC connection state → DTLS transport state. */
|
|
757
|
+
_syncDtlsState() {
|
|
758
|
+
if (!this._dtlsTransport) return;
|
|
759
|
+
const pcState = this.connectionState;
|
|
760
|
+
const dtlsMap = {
|
|
761
|
+
"new": "new",
|
|
762
|
+
"connecting": "connecting",
|
|
763
|
+
"connected": "connected",
|
|
764
|
+
"disconnected": "connected",
|
|
765
|
+
// DTLS stays connected even if ICE disconnects
|
|
766
|
+
"failed": "failed",
|
|
767
|
+
"closed": "closed"
|
|
768
|
+
};
|
|
769
|
+
this._dtlsTransport._setState(dtlsMap[pcState] ?? "new");
|
|
770
|
+
if (pcState === "connected" && this._sctpTransport) {
|
|
771
|
+
this._sctpTransport._setState("connected");
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
/** Map PC ICE connection state → ICE transport state. */
|
|
775
|
+
_syncIceState() {
|
|
776
|
+
if (!this._iceTransport) return;
|
|
777
|
+
const iceState = this.iceConnectionState;
|
|
778
|
+
this._iceTransport._setState(iceState);
|
|
779
|
+
}
|
|
780
|
+
/** Map PC ICE gathering state → ICE transport gathering state. */
|
|
781
|
+
_syncIceGatheringState() {
|
|
782
|
+
if (!this._iceTransport) return;
|
|
783
|
+
const gatheringState = this.iceGatheringState;
|
|
784
|
+
this._iceTransport._setGatheringState(gatheringState);
|
|
785
|
+
}
|
|
786
|
+
// ---- on<event> attribute handlers --------------------------------------
|
|
787
|
+
_onconnectionstatechange = null;
|
|
788
|
+
_ondatachannel = null;
|
|
789
|
+
_onicecandidate = null;
|
|
790
|
+
_oniceconnectionstatechange = null;
|
|
791
|
+
_onicegatheringstatechange = null;
|
|
792
|
+
_onnegotiationneeded = null;
|
|
793
|
+
_onsignalingstatechange = null;
|
|
794
|
+
get onconnectionstatechange() {
|
|
795
|
+
return this._onconnectionstatechange;
|
|
796
|
+
}
|
|
797
|
+
set onconnectionstatechange(v) {
|
|
798
|
+
this._onconnectionstatechange = v;
|
|
799
|
+
}
|
|
800
|
+
get ondatachannel() {
|
|
801
|
+
return this._ondatachannel;
|
|
802
|
+
}
|
|
803
|
+
set ondatachannel(v) {
|
|
804
|
+
this._ondatachannel = v;
|
|
805
|
+
}
|
|
806
|
+
get onicecandidate() {
|
|
807
|
+
return this._onicecandidate;
|
|
808
|
+
}
|
|
809
|
+
set onicecandidate(v) {
|
|
810
|
+
this._onicecandidate = v;
|
|
811
|
+
}
|
|
812
|
+
get oniceconnectionstatechange() {
|
|
813
|
+
return this._oniceconnectionstatechange;
|
|
814
|
+
}
|
|
815
|
+
set oniceconnectionstatechange(v) {
|
|
816
|
+
this._oniceconnectionstatechange = v;
|
|
817
|
+
}
|
|
818
|
+
get onicegatheringstatechange() {
|
|
819
|
+
return this._onicegatheringstatechange;
|
|
820
|
+
}
|
|
821
|
+
set onicegatheringstatechange(v) {
|
|
822
|
+
this._onicegatheringstatechange = v;
|
|
823
|
+
}
|
|
824
|
+
get onnegotiationneeded() {
|
|
825
|
+
return this._onnegotiationneeded;
|
|
826
|
+
}
|
|
827
|
+
set onnegotiationneeded(v) {
|
|
828
|
+
this._onnegotiationneeded = v;
|
|
829
|
+
}
|
|
830
|
+
get onsignalingstatechange() {
|
|
831
|
+
return this._onsignalingstatechange;
|
|
832
|
+
}
|
|
833
|
+
set onsignalingstatechange(v) {
|
|
834
|
+
this._onsignalingstatechange = v;
|
|
835
|
+
}
|
|
836
|
+
_ontrack = null;
|
|
837
|
+
get ontrack() {
|
|
838
|
+
return this._ontrack;
|
|
839
|
+
}
|
|
840
|
+
set ontrack(v) {
|
|
841
|
+
this._ontrack = v;
|
|
842
|
+
}
|
|
843
|
+
get onicecandidateerror() {
|
|
844
|
+
return null;
|
|
845
|
+
}
|
|
846
|
+
set onicecandidateerror(_v) {
|
|
847
|
+
}
|
|
848
|
+
// ---- Certificate management (Phase 4.7) --------------------------------
|
|
849
|
+
static generateCertificate(keygenAlgorithm) {
|
|
850
|
+
return generateCertificate(keygenAlgorithm);
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
export {
|
|
854
|
+
RTCPeerConnection
|
|
855
|
+
};
|