Skip to content

Commit

Permalink
WebRTC.MediaHandler/MediaStreamManager: support multiple MediaStreams
Browse files Browse the repository at this point in the history
MediaHandler:
  #addStream -> #addStreams: takes (Array of) MediaStream
  #render: renders only first local/remote stream (see #76)

MediaStreamManager:
  .render:
    * takes (Array of) MediaStream
    * renders only the first (see #76)

  #acquire:
    * returns a Promise of Array of MediaStream
    * mediaHint.stream is (Array of) MediaStream

  #release: takes (Array of) MediaStream
  • Loading branch information
joseph-onsip committed Oct 8, 2014
1 parent 3a996be commit 502184c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 28 deletions.
27 changes: 13 additions & 14 deletions src/WebRTC/MediaHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ MediaHandler.prototype = Object.create(SIP.MediaHandler.prototype, {
this.mediaHint = mediaHint;

/*
* 1. acquire stream (skip if MediaStream passed in)
* 2. addStream
* 1. acquire streams (skip if MediaStreams passed in)
* 2. addStreams
* 3. createOffer/createAnswer
* 4. call onSuccess()
*/

var streamPromise;
Expand All @@ -207,18 +206,18 @@ MediaHandler.prototype = Object.create(SIP.MediaHandler.prototype, {
else {
self.logger.log('acquiring local media');
streamPromise = self.mediaStreamManager.acquire(mediaHint)
.then(function acquireSucceeded(stream) {
self.logger.log('acquired local media stream');
self.localMedia = stream;
.then(function acquireSucceeded(streams) {
self.logger.log('acquired local media streams');
self.localMedia = streams;
self.session.connecting();
return stream;
return streams;
}, function acquireFailed(err) {
self.logger.error('unable to acquire stream');
self.logger.error('unable to acquire streams');
self.logger.error(err);
self.session.connecting();
throw err;
})
.then(this.addStream.bind(this))
.then(this.addStreams.bind(this))
;
}

Expand Down Expand Up @@ -393,9 +392,7 @@ MediaHandler.prototype = Object.create(SIP.MediaHandler.prototype, {
Object.keys(streamGetters).forEach(function (loc) {
var streamGetter = streamGetters[loc];
var streams = this[streamGetter]();
if (streams.length) {
SIP.WebRTC.MediaStreamManager.render(streams[0], renderHint[loc]);
}
SIP.WebRTC.MediaStreamManager.render(streams, renderHint[loc]);
}.bind(this));
}},

Expand Down Expand Up @@ -452,9 +449,11 @@ MediaHandler.prototype = Object.create(SIP.MediaHandler.prototype, {
;
}},

addStream: {writable: true, value: function addStream (stream) {
addStreams: {writable: true, value: function addStreams (streams) {
try {
this.peerConnection.addStream(stream);
streams.forEach(function (stream) {
this.peerConnection.addStream(stream);
}, this);
} catch(e) {
this.logger.error('error adding stream');
this.logger.error(e);
Expand Down
32 changes: 20 additions & 12 deletions src/WebRTC/MediaStreamManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ MediaStreamManager.streamId = function (stream) {
.join('');
};

MediaStreamManager.render = function render (stream, elements) {
if (!elements) {
MediaStreamManager.render = function render (streams, elements) {
// only render first stream, see pull request #76
var stream = streams[0];
if (!elements || !stream) {
return false;
}

Expand Down Expand Up @@ -89,10 +91,13 @@ MediaStreamManager.prototype = Object.create(SIP.EventEmitter.prototype, {
'acquire': {value: function acquire (mediaHint) {
mediaHint = Object.keys(mediaHint || {}).length ? mediaHint : this.mediaHint;

var saveSuccess = function (isHintStream, stream) {
var streamId = MediaStreamManager.streamId(stream);
this.acquisitions[streamId] = !!isHintStream;
return SIP.Utils.Promise.resolve(stream);
var saveSuccess = function (isHintStream, streams) {
streams = [].concat(streams);
streams.forEach(function (stream) {
var streamId = MediaStreamManager.streamId(stream);
this.acquisitions[streamId] = !!isHintStream;
}, this);
return SIP.Utils.Promise.resolve(streams);
}.bind(this);

if (mediaHint.stream) {
Expand Down Expand Up @@ -135,12 +140,15 @@ MediaStreamManager.prototype = Object.create(SIP.EventEmitter.prototype, {
}
}},

'release': {value: function release (stream) {
var streamId = MediaStreamManager.streamId(stream);
if (this.acquisitions[streamId] === false) {
stream.stop();
}
delete this.acquisitions[streamId];
'release': {value: function release (streams) {
streams = [].concat(streams);
streams.forEach(function (stream) {
var streamId = MediaStreamManager.streamId(stream);
if (this.acquisitions[streamId] === false) {
stream.stop();
}
delete this.acquisitions[streamId];
}, this);
}},
});

Expand Down
5 changes: 3 additions & 2 deletions test/spec/MediaStreamManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ describe('MediaStreamManager', function () {
beforeEach(function(done) {
mediaStreamManager.acquire(
{constraints: {audio: true}}).then(
function onSuccess (stream) {
function onSuccess (streams) {
var stream = streams[0];
acquiredStream = stream;
mediaStreamManager.release(stream);
done();
Expand Down Expand Up @@ -156,7 +157,7 @@ describe('MediaStreamManager', function () {
it('.acquire ignores constraints and succeeds with the stream', function (done) {
mediaStreamManager.acquire(mediaHint).then(onSuccess, onFailure)
.then(function () {
expect(onSuccess).toHaveBeenCalledWith(mediaHint.stream);
expect(onSuccess).toHaveBeenCalledWith([mediaHint.stream]);
done();
});
});
Expand Down

0 comments on commit 502184c

Please sign in to comment.