Skip to content

Commit

Permalink
Merge pull request #773 from jerry2013/use-the-same-MediaStreamTrack-…
Browse files Browse the repository at this point in the history
…on-RTCRtpSender

use the same MediaStreamTrack on RTCRtpSender
  • Loading branch information
hthetiot authored Jun 6, 2023
2 parents 24d2021 + 9038a8a commit b6b5356
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
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

0 comments on commit b6b5356

Please sign in to comment.