Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the same MediaStreamTrack on RTCRtpSender #773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions js/MediaStreamTrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var debug = require('debug')('iosrtc:MediaStreamTrack'),
// Save original MediaStreamTrack
var originalMediaStreamTrack = window.MediaStreamTrack || function dummyMediaStreamTrack() {};

// All "active" tracks, to ensure RTCRtpSender gets the same object
var activeMediaStreamTracks = new Map();

function newMediaStreamTrackId() {
return window.crypto.getRandomValues(new Uint32Array(4)).join('-');
}
Expand Down Expand Up @@ -51,6 +54,8 @@ function MediaStreamTrack(dataFromEvent) {

this.dataFromEvent = dataFromEvent;

activeMediaStreamTracks.set(this.id, this);

function onResultOK(data) {
onEvent.call(self, data);
}
Expand Down Expand Up @@ -134,6 +139,13 @@ MediaStreamTrack.getSources = function () {
return enumerateDevices.apply(this, arguments);
};

MediaStreamTrack.findOrCreate = function (trackData) {
var id = trackData.id;
debug('findOrCreate()', id);

return activeMediaStreamTracks.get(id) || new MediaStreamTrack(trackData);
};

/**
* Private API.
*/
Expand All @@ -154,6 +166,7 @@ function onEvent(data) {
case 'live':
break;
case 'ended':
activeMediaStreamTracks.delete(this.id);
this._ended = true;
this.dispatchEvent(new Event('ended'));
break;
Expand Down
2 changes: 1 addition & 1 deletion js/RTCPeerConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ RTCPeerConnection.prototype.getOrCreateTrack = function (trackInput) {
if (trackInput instanceof MediaStreamTrack) {
track = trackInput;
} else {
track = new MediaStreamTrack(trackInput);
track = MediaStreamTrack.findOrCreate(trackInput);
}

this.tracks[id] = track;
Expand Down
2 changes: 1 addition & 1 deletion js/RTCRtpSender.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ RTCRtpSender.prototype.replaceTrack = function (withTrack) {

return new Promise((resolve, reject) => {
function onResultOK(result) {
self.track = result.track ? new MediaStreamTrack(result.track) : null;
self.track = result.track ? MediaStreamTrack.findOrCreate(result.track) : null;
resolve();
}

Expand Down
17 changes: 15 additions & 2 deletions www/cordova-plugin-iosrtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,9 @@ var debug = _dereq_('debug')('iosrtc:MediaStreamTrack'),
// Save original MediaStreamTrack
var originalMediaStreamTrack = window.MediaStreamTrack || function dummyMediaStreamTrack() {};

// All "active" tracks, to ensure RTCRtpSender gets the same object
var activeMediaStreamTracks = new Map();

function newMediaStreamTrackId() {
return window.crypto.getRandomValues(new Uint32Array(4)).join('-');
}
Expand Down Expand Up @@ -1178,6 +1181,8 @@ function MediaStreamTrack(dataFromEvent) {

this.dataFromEvent = dataFromEvent;

activeMediaStreamTracks.set(this.id, this);

function onResultOK(data) {
onEvent.call(self, data);
}
Expand Down Expand Up @@ -1261,6 +1266,13 @@ MediaStreamTrack.getSources = function () {
return enumerateDevices.apply(this, arguments);
};

MediaStreamTrack.findOrCreate = function (trackData) {
var id = trackData.id;
debug('findOrCreate()', id);

return activeMediaStreamTracks.get(id) || new MediaStreamTrack(trackData);
};

/**
* Private API.
*/
Expand All @@ -1281,6 +1293,7 @@ function onEvent(data) {
case 'live':
break;
case 'ended':
activeMediaStreamTracks.delete(this.id);
this._ended = true;
this.dispatchEvent(new Event('ended'));
break;
Expand Down Expand Up @@ -2515,7 +2528,7 @@ RTCPeerConnection.prototype.getOrCreateTrack = function (trackInput) {
if (trackInput instanceof MediaStreamTrack) {
track = trackInput;
} else {
track = new MediaStreamTrack(trackInput);
track = MediaStreamTrack.findOrCreate(trackInput);
}

this.tracks[id] = track;
Expand Down Expand Up @@ -3008,7 +3021,7 @@ RTCRtpSender.prototype.replaceTrack = function (withTrack) {

return new Promise((resolve, reject) => {
function onResultOK(result) {
self.track = result.track ? new MediaStreamTrack(result.track) : null;
self.track = result.track ? MediaStreamTrack.findOrCreate(result.track) : null;
resolve();
}

Expand Down