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

Commit d48b59c

Browse files
authored
Publication.rtpTransceivers returns transceivers in P2P mode. (#509)
This change also adds an API to get PeerConnection for a given remote endpoint. Since the P2P mode heavily depends on remote endpoint's implementation. Using WebRTC APIs directly in P2P mode is not recommended.
1 parent c2549dc commit d48b59c

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

docs/design/owt_with_webrtc_apis.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# OWT SDK with WebRTC APIs
22
## Introduction
3-
OWT(Open WebRTC Toolkit) Client SDKs provide convenient APIs to create, publish, and subscribe streams. Most of these APIs are wrappers of WebRTC APIs with signaling support. It helps WebRTC beginners easily involve WebRTC technology into their applications without too much knowledge of WebRTC evolution and browser differences. As WebRTC 1.0 is moving to PR, which means it is quite stable, we are planning to expose more WebRTC APIs to developers to enable advanced and custom usages with OWT.
3+
OWT(Open WebRTC Toolkit) Client SDKs provide convenient APIs to create, publish, and subscribe streams. Most of these APIs are wrappers of WebRTC APIs with signaling support. It helps WebRTC beginners easily involve WebRTC technology into their applications without too much knowledge of WebRTC evolution and browser differences. As WebRTC 1.0 was officially made a W3C Recommendation, which means it is stable, we are planning to expose more WebRTC APIs to developers to enable advanced and custom usages with OWT.
44
## Potential Usages
55
- Replace a track in the middle of a call.
66
- Set custom encoding parameters, perhaps for simulcast.

src/sdk/p2p/p2pclient.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,28 @@ const P2PClient = function(configuration, signalingChannel) {
257257
return channels.get(remoteId).getStats();
258258
};
259259

260-
const sendSignalingMessage = function(
261-
remoteId, connectionId, type, message) {
260+
/**
261+
* @function getPeerConnection
262+
* @instance
263+
* @desc Get underlying PeerConnection.
264+
* @memberof Owt.P2P.P2PClient
265+
* @param {string} remoteId Remote endpoint's ID.
266+
* @return {Promise<RTCPeerConnection, Error>} It returns a promise resolved
267+
* with an RTCPeerConnection or reject with an Error if there is no
268+
* connection with specific user.
269+
* @private
270+
*/
271+
this.getPeerConnection = function(remoteId) {
272+
if (!channels.has(remoteId)) {
273+
return Promise.reject(new ErrorModule.P2PError(
274+
ErrorModule.errors.P2P_CLIENT_INVALID_STATE,
275+
'No PeerConnection between current endpoint and specific remote ' +
276+
'endpoint.'));
277+
}
278+
return channels.get(remoteId).peerConnection;
279+
};
280+
281+
const sendSignalingMessage = function(remoteId, connectionId, type, message) {
262282
const msg = {
263283
type,
264284
connectionId,

src/sdk/p2p/peerconnection-channel.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as Utils from '../base/utils.js';
1616
import * as ErrorModule from './error.js';
1717
import * as StreamModule from '../base/stream.js';
1818
import * as SdpUtils from '../base/sdputils.js';
19+
import {TransportSettings, TransportType} from '../base/transport.js';
1920

2021
/**
2122
* @class P2PPeerConnectionChannelEvent
@@ -100,6 +101,10 @@ class P2PPeerConnectionChannel extends EventDispatcher {
100101
this._sendUa(sysInfo);
101102
}
102103

104+
get peerConnection() {
105+
return this._pc;
106+
}
107+
103108
/**
104109
* @function publish
105110
* @desc Publish a stream to the remote endpoint.
@@ -288,7 +293,7 @@ class P2PPeerConnectionChannel extends EventDispatcher {
288293
_tracksAddedHandler(ids) {
289294
// Currently, |ids| contains all track IDs of a MediaStream. Following algorithm also handles |ids| is a part of a MediaStream's tracks.
290295
for (const id of ids) {
291-
// It could be a problem if there is a track published with different MediaStreams.
296+
// It could be a problem if there is a track published with different MediaStreams, moving to mid.
292297
this._publishingStreamTracks.forEach((mediaTrackIds, mediaStreamId) => {
293298
for (let i = 0; i < mediaTrackIds.length; i++) {
294299
if (mediaTrackIds[i] === id) {
@@ -311,9 +316,20 @@ class P2PPeerConnectionChannel extends EventDispatcher {
311316
(element) => element.mediaStream.id == mediaStreamId);
312317
const targetStream = this._publishingStreams[targetStreamIndex];
313318
this._publishingStreams.splice(targetStreamIndex, 1);
314-
// TODO: Set transceivers for Publication.
319+
320+
// Set transceivers for Publication.
321+
const transport =
322+
new TransportSettings(TransportType.WEBRTC, undefined);
323+
transport.rtpTransceivers = [];
324+
for (const transceiver of this._pc.getTransceivers()) {
325+
if (transceiver.sender?.track in
326+
this._publishedStreamTracks.get(mediaStreamId)) {
327+
transport.rtpTransceivers.push(transceiver);
328+
}
329+
}
330+
315331
const publication = new Publication(
316-
id, undefined, () => {
332+
id, transport, () => {
317333
this._unpublish(targetStream).then(() => {
318334
publication.dispatchEvent(new OwtEvent('ended'));
319335
}, (err) => {

test/unit/resources/scripts/p2p.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import P2PClient from '../../../../src/sdk/p2p/p2pclient.js';
88
import SignalingChannel from './fake-p2p-signaling.js';
99
import * as StreamModule from '../../../../src/sdk/base/stream.js';
10-
import * as EventModule from '../../../../src/sdk/base/event.js'
10+
import * as EventModule from '../../../../src/sdk/base/event.js';
11+
import {TransportSettings, TransportType} from '../../../../src/sdk/base/transport.js';
1112

1213
const expect = chai.expect;
1314
const screenSharingExtensionId = 'jniliohjdiikfjjdlpapmngebedgigjn';
@@ -118,6 +119,10 @@ describe('Unit tests for P2PClient', function() {
118119
p2pclient1.getStats('user2').then((stats)=>{
119120
console.info('Stats: '+JSON.stringify(stats));
120121
});
122+
expect(p2pclient1.getPeerConnection('user2'))
123+
.to.be.an.instanceof(RTCPeerConnection);
124+
expect(publication.transport.type === TransportType.WEBRTC);
125+
expect(publication.transport.rtpTransceivers.length === 1);
121126
expect(publication.stop()).to.be.undefined;
122127
done();
123128
});

0 commit comments

Comments
 (0)