Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

Commit 81ed393

Browse files
authored
Create PeerConnection when PeerConnectionChannel is created. (#532)
* Create PeerConnection when PeerConnectionChannel is created. This change allows application to get PeerConnection before publishing or subscribing. Then the application can create RTCRtpTransceivers on the PeerConnection returned, and publish a stream with RTCRtpTransceivers pre-configurated. * Notify ConferenceClient when a new publication ID is ready. This is actually not needed because conference server supports multiple streams on a single PeerConnection, so only a single PeerConnectionChannel is created. But let's keep the old behavior until ConferenceClient is updated.
1 parent 192a39b commit 81ed393

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/sdk/conference/channel.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
6161
this._sdpResolvers = []; // [{finish, resolve, reject}]
6262
this._sdpResolveNum = 0;
6363
this._remoteMediaStreams = new Map(); // Key is subscription ID, value is MediaStream.
64+
65+
this._createPeerConnection();
6466
}
6567

6668
/**
@@ -129,14 +131,29 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
129131
mid: t.mid,
130132
};
131133
});
132-
const publicationId =
133-
await this._signaling.sendSignalingMessage('publish', {
134-
media: {tracks: trackOptions},
135-
attributes: stream.attributes,
136-
transport: {id: this._id, type: 'webrtc'},
137-
}).id;
134+
const publicationResp =
135+
await this._signaling.sendSignalingMessage('publish', {
136+
media: {tracks: trackOptions},
137+
attributes: stream.attributes,
138+
transport: {id: this._id, type: 'webrtc'},
139+
});
140+
const publicationId = publicationResp.id;
138141
this._publishTransceivers.get(internalId).id = publicationId;
139142
this._reverseIdMap.set(publicationId, internalId);
143+
144+
if (this._id && this._id !== publicationResp.transportId) {
145+
Logger.warning('Server returns conflict ID: ' + publicationResp
146+
.transportId);
147+
return;
148+
}
149+
this._id = publicationResp.transportId;
150+
151+
const messageEvent = new MessageEvent('id', {
152+
message: publicationId,
153+
origin: this._remoteId,
154+
});
155+
this.dispatchEvent(messageEvent);
156+
140157
await this._signaling.sendSignalingMessage(
141158
'soac', {id: this._id, signaling: offer});
142159
return new Promise((resolve, reject) => {
@@ -223,7 +240,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
223240
}
224241
}
225242
const mediaOptions = {};
226-
this._createPeerConnection();
227243
if (stream.mediaStream.getAudioTracks().length > 0 && options.audio !==
228244
false && options.audio !== null) {
229245
if (stream.mediaStream.getAudioTracks().length > 1) {
@@ -373,20 +389,21 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
373389
});
374390
}).then((data) => {
375391
const publicationId = data.id;
376-
const messageEvent = new MessageEvent('id', {
377-
message: publicationId,
378-
origin: this._remoteId,
379-
});
380-
this.dispatchEvent(messageEvent);
381-
382392
this._publishTransceivers.get(internalId).id = publicationId;
383393
this._reverseIdMap.set(publicationId, internalId);
384394

385395
if (this._id && this._id !== data.transportId) {
386396
Logger.warning('Server returns conflict ID: ' + data.transportId);
397+
return;
387398
}
388399
this._id = data.transportId;
389400

401+
const messageEvent = new MessageEvent('id', {
402+
message: publicationId,
403+
origin: this._remoteId,
404+
});
405+
this.dispatchEvent(messageEvent);
406+
390407
// Modify local SDP before sending
391408
if (options) {
392409
transceivers.forEach(({type, transceiver, option}) => {
@@ -511,7 +528,6 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
511528

512529
const offerOptions = {};
513530
const transceivers = [];
514-
this._createPeerConnection();
515531
if (typeof this.pc.addTransceiver === 'function') {
516532
// |direction| seems not working on Safari.
517533
if (mediaOptions.audio) {
@@ -577,19 +593,20 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
577593
});
578594
}).then((data) => {
579595
const subscriptionId = data.id;
580-
const messageEvent = new MessageEvent('id', {
581-
message: subscriptionId,
582-
origin: this._remoteId,
583-
});
584-
this.dispatchEvent(messageEvent);
585-
586596
this._subscribeTransceivers.get(internalId).id = subscriptionId;
587597
this._reverseIdMap.set(subscriptionId, internalId);
588598
if (this._id && this._id !== data.transportId) {
589599
Logger.warning('Server returns conflict ID: ' + data.transportId);
600+
return;
590601
}
591602
this._id = data.transportId;
592603

604+
const messageEvent = new MessageEvent('id', {
605+
message: subscriptionId,
606+
origin: this._remoteId,
607+
});
608+
this.dispatchEvent(messageEvent);
609+
593610
// Modify local SDP before sending
594611
if (options) {
595612
transceivers.forEach(({type, transceiver, option}) => {
@@ -898,6 +915,8 @@ export class ConferencePeerConnectionChannel extends EventDispatcher {
898915

899916
_createPeerConnection() {
900917
if (this.pc) {
918+
Logger.warning('A PeerConnection was created. Cannot create again for ' +
919+
'the same PeerConnectionChannel.');
901920
return;
902921
}
903922

src/sdk/conference/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ export const ConferenceClient = function(config, signalingImpl) {
335335
}
336336

337337
// eslint-disable-next-line require-jsdoc
338-
function sendSignalingMessage(type, message) {
338+
async function sendSignalingMessage(type, message) {
339339
return signaling.send(type, message);
340340
}
341341

test/unit/resources/scripts/conference.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('Unit tests for ConferencePeerConnectionChannel.', () => {
8888
false, true
8989
]
9090
];
91-
const channel = new ConferencePeerConnectionChannel();
91+
const channel = new ConferencePeerConnectionChannel({});
9292
for (const [p, isRtpEncodingParameters, isOwtEncodingParameters] of
9393
parameters) {
9494
expect(channel._isRtpEncodingParameters(p))

0 commit comments

Comments
 (0)