From 9dbb997221692f1a8f1796064a2da54629356d5c Mon Sep 17 00:00:00 2001 From: Justin Martinez Date: Wed, 5 Dec 2018 09:54:13 -0700 Subject: [PATCH 01/14] fix for republishing in a unpolished form --- index.js | 349 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 338 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 7968d3b..71c5d4b 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ var BaseSession = require('jingle-session'); var RTCPeerConnection = require('rtcpeerconnection'); var queue = require('queue'); +var jmglobal; function filterContentSources(content, stream) { if (content.application.applicationType !== 'rtp') { @@ -20,9 +21,13 @@ function filterContentSources(content, stream) { if (source.parameters.length < 2) { return false; } - return (stream.id === source.parameters[1].value.split(' ')[0] || stream.label === source.parameters[1].value.split(' ')[0]); + + console.log('stream id: ', stream, source.parameters); + return (stream.id === source.parameters[1].value.split(' ')[0] + || stream.label === source.parameters[1].value.split(' ')[0]); }); } + // remove source groups not related to this stream if (content.application.sourceGroups) { content.application.sourceGroups = content.application.sourceGroups.filter(function (group) { @@ -89,6 +94,68 @@ function changeSendersIfNoMsids(content) { } } +// When we remove a source and need to add a recvonly source +function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { + if (newContent.application.applicationType !== 'rtp') { + return; + } + + delete newContent.transport; + delete newContent.application.payloads; + delete newContent.application.headerExtensions; + delete newContent.application.ssrc; + newContent.application.mux = false; + + console.log('filterAddRecvOnlyIfNotPresent'); + console.log('newContent shit:', newContent.senders); + console.log('oldShit', oldContent.senders); + //direction application.senders + if (newContent.application.sources && newContent.senders === 'initiator') { + newContent.application.sources = newContent.application.sources.filter(function (baseSource) { + + + // if there's a msid, ignore it because its not recvonly + // if (sourceHasMsid(baseSource)) { + // return false; + // } + + + // try to find correpsonding source in compareContent if it exists + var foundNewRecvonlySource = false; + if (oldContent) { + var compareSource = findMatchingSource(baseSource, oldContent.application.sources); + // if the source is new or the source is now read only + // if(!compareSource || (compareSource && sourceHasMsid(compareSource))) { + if (!compareSource && oldContent.senders === 'both') { + foundNewRecvonlySource = true; + } + } else { + foundNewRecvonlySource = true; + } + return foundNewRecvonlySource; + }); + } + // remove source groups not related to this stream + if (newContent.application.sourceGroups) { + newContent.application.sourceGroups = newContent.application.sourceGroups.filter(function (group) { + var found = false; + for (var i = 0; i < newContent.application.sources.length; i++) { + if (newContent.application.sources[i].ssrc === group.sources[0]) { + found = true; + break; + } + } + return found; + }); + } + return newContent.application.sources.length; + +}; + +function diffArray(a = [], b = []) { + return a.filter(x => !b.includes(x)); +} + // filters the sources in baseContent to only include sources which don't have an msid (recvonly) and are new // (not in compareContent sources) or that have a corresponding source in compareContent that has an msid (indicating) // that the source changed from recvonly to sendrecv. If no compareContent is passed in then it will filter the @@ -162,6 +229,7 @@ function MediaSession(opts) { this.pc.on('addStream', this.onAddStream.bind(this)); this.pc.on('removeStream', this.onRemoveStream.bind(this)); this.pc.on('addChannel', this.onAddChannel.bind(this)); + // this.pc.on('negotiationNeeded', this.onNegotiationNeeded.bind(this)); if (opts.stream) { this.addStream(opts.stream); @@ -172,6 +240,9 @@ function MediaSession(opts) { function queueOfferAnswer(self, errorMsg, jingleDesc, cb) { + + console.log('jm - queueOfferAnswer jingleDesc', jingleDesc); + self.q.push(function(qCb) { function done(err, answer) { qCb(); @@ -417,6 +488,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { // ---------------------------------------------------------------- addStream: function (stream, renegotiate, cb) { + console.log('jm addStream'); var self = this; var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); @@ -429,6 +501,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { } else if (typeof renegotiate === 'object') { self.constraints = renegotiate; } + var errorMsg = 'adding new stream'; queueOfferAnswer(this, errorMsg, self.pc.remoteDescription, function(err, answer) { if (err) { @@ -440,14 +513,19 @@ MediaSession.prototype = extend(MediaSession.prototype, { filterContentSources(content, stream); }); answer.jingle.contents = answer.jingle.contents.filter(function (content) { - return content.application.applicationType === 'rtp' && content.application.sources && content.application.sources.length; + return content.application.applicationType === 'rtp' + && content.application.sources + && content.application.sources.length; }); delete answer.jingle.groups; var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); + const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); - self.send('source-add', answer.jingle); + if (answer.jingle) { + self.send('source-add', answer.jingle); + } return cb(); }); @@ -458,6 +536,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { }, removeStream: function (stream, renegotiate, cb) { + console.log('jm removeStream'); var self = this; var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); @@ -479,7 +558,11 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); delete desc.groups; - this.send('source-remove', desc); + if (desc.contents.length) { + console.log('jm remove this source dewd: ', desc); + this.send('source-remove', desc); + } + this.pc.removeStream(stream); var errorMsg = 'removing stream'; @@ -489,7 +572,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); + const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + // self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); cb(); }); }, @@ -498,21 +582,245 @@ MediaSession.prototype = extend(MediaSession.prototype, { this.removeStream(stream, true, cb); }, + _removeShit: function(oldLocalDescription, newLocalDescription) { + const desc = oldLocalDescription; + delete desc.group; + function getContent(content) { + return content.application.applicationType === 'rtp' + && content.application.sources + && content.application.sources.length; + } + const oldContents = oldLocalDescription.contents.filter(getContent); + const newContents = newLocalDescription.contents.filter(getContent); + console.log('jm remove shit oldContents: ', oldContents, newContents); + + for (let i = 0; i < newContents; i++) { + + } + }, + // Justin's new functions + _doShit: function(oldLocalDescription, newLocalDescription) { + const desc = oldLocalDescription; + delete desc.group; + + function getContent(content) { + return content.application.applicationType === 'rtp' + && content.application.sources + && content.application.sources.length; + } + + const oldContents = oldLocalDescription.contents.filter(getContent); + const newContents = newLocalDescription.contents.filter(getContent); + console.log('jm oldContents: ', oldContents, newContents); + + const a = {}; + const sourcesRemoved = []; + const sourcesModified = []; + const sourcesToAddBack = []; + + for (let i = 0; i < newContents.length; i++) { + console.log('jm new contents', newContents[i].application.sources.length); + for(let j = 0; j < newContents[i].application.sources.length; j++) { + console.log('jm new content sources: ', newContents[i].application.sources[j].ssrc); + a[newContents[i].application.sources[j].ssrc] = { + source: newContents[i].application.sources[j], + direction: newContents[i].senders, + }; + } + }; + + for (let i = 0; i < oldContents.length; i++) { + for(let j = 0; j < oldContents[i].application.sources.length; j++) { + if (!a[oldContents[i].application.sources[j].ssrc]) { + // this IS a removed ssource + sourcesRemoved.push(oldContents[i].application.sources[j].ssrc); + } + else { + // this is a possible modified source + const oldContentHasMsid = sourceHasMsid(oldContents[i].application.sources[j]); + const oldContentSourceDirection = oldContents[i].senders; + const newContentHasMsid = sourceHasMsid(a[oldContents[i].application.sources[j].ssrc].source); + const newContentSourceDirection = a[oldContents[i].application.sources[j].ssrc].direction; + + if (newContentSourceDirection !== oldContentSourceDirection) { + // Direction has changed + delete oldContents[i].transport; + delete oldContents[i].application.payloads; + + console.log('jm direction change detected') + + switch (newContentSourceDirection) { + case 'both': + // if newContent does not have msid don't change direction. + if (!newContentHasMsid) { + console.log('jm trying to changed direction to sendrecv that does not have a msid: ', oldContents[i].application.sources[j].ssrc); + } + else { + console.log('jm newContent is now sendrecv -> send sr (old) sa(new)', oldContents[i].application.sources[j].ssrc); + sourcesModified.push(oldContents[i].application.sources[j].ssrc); + } + break; + case 'initiator': + console.log('jm newContent is now recvonly -> send sr (new) sa(old)', oldContents[i].application.sources[j].ssrc); + sourcesToAddBack.push(oldContents[i].application.sources[j].ssrc); + break; + default: + console.log('jm I should not be here'); + } + } + else { + if(oldContentHasMsid && !newContentHasMsid) { + console.log('jm the msid from the newcontent was created'); + } + } + } + delete a[oldContents[i].application.sources[j].ssrc]; + } + } + + console.log('jm b4 shit: ', Object.keys(a)); + const sourcesAdded = []; + + Object.keys(a).forEach(ssrc => { + for (let i = 0; i < newContents.length; i++) { + + newContents[i].application.sources = + newContents[i].application.sources.filter(source => { + return source.ssrc === ssrc + }); + + if (newContents[i].application.sources.length) { + delete newContents[i].transport; + delete newContents[i].ssrc; + delete newContents[i].application.payloads; + sourcesAdded.push(newContents[i]); + } + } + }); + + console.log('jm removedSources: ', sourcesRemoved); + console.log('jm sourcesAdded: ', sourcesAdded); + console.log('jm sourcesModifed: ', sourcesModified); + console.log('jm sourcesToAddBack: ', sourcesToAddBack) + + if (sourcesAdded.length) { + const new_desc = newLocalDescription; + delete new_desc.groups; + new_desc.contents = sourcesAdded; + this.send('source-add', new_desc); + } + + function getProperSSRCS(contents, ssrcList) { + const properContents = []; + for (let i = 0; i < contents.length; i++) { + const filteredSsrcs = + contents[i].application.sources.filter(source => ssrcList.includes(source.ssrc)); + + if (filteredSsrcs.length) { + contents[i].application.sources = filteredSsrcs; + delete contents[i].transport; + delete contents[i].ssrc; + delete contents[i].application.payloads; + contents[i].application.mux = false; + contents[i].application.headerExtensions = []; + properContents.push(contents[i]); + } + } + return properContents; + } + + if (sourcesModified.length) { + const new_desc = newLocalDescription; + + const jmContent = getProperSSRCS(oldContents, sourcesModified); + const jmContent2 = getProperSSRCS(newContents, sourcesModified); + + desc.contents = jmContent; + new_desc.contents = jmContent2; + delete desc.groups; + delete new_desc.groups; + desc.contents.forEach(content => { + content.application.sources.forEach(source => { + source.parameters = source.parameters.filter(param => { + if (param.key === 'msid') { + return false; + } + return true; + }); + }); + }); + + this.send('source-remove', desc); + this.send('source-add', new_desc); + } + if (sourcesToAddBack.length) { + const new_desc = newLocalDescription; + const jmContent = getProperSSRCS(oldContents, sourcesToAddBack); + const jmContent2 = getProperSSRCS(newContents, sourcesToAddBack); + + console.log('jmContents: ', jmContent, jmContent2); + desc.contents = jmContent; + new_desc.contents = jmContent2; + delete desc.groups; + delete new_desc.groups; + + new_desc.contents.forEach(content => { + content.application.sources.forEach(source => { + source.parameters = source.parameters.filter(param => { + if (param.key === 'msid') { + return false; + } + return true; + }); + }); + }); + // desc.contents = sourcesToAddBack; + this.send('source-remove', desc); + this.send('source-add', new_desc); + } + + }, + _removeRecvOnlySourceIfPresent: function(oldLocalDescription, newLocalDescription) { var desc = oldLocalDescription; + console.log('_removeRecvOnlySourceIfPresent: ', oldLocalDescription, newLocalDescription); + // filter to only sources that changed from recvonly to sendrecv desc.contents = desc.contents.filter(function(oldContent) { - if (oldContent.application.applicationType === 'rtp' && oldContent.application.sources && oldContent.application.sources.length) { + + if (oldContent.application.applicationType === 'rtp' + && oldContent.application.sources + && oldContent.application.sources.length) { var newContent = findMatchingContentBlock(oldContent, newLocalDescription); + console.log('oC sender: ' + oldContent.senders + ' nC sender: ' + newContent.senders) + // if (oldContent.senders === 'both' && newContent.senders === 'both') { + // return false; + // } // the filter function handles the case where oldContent is null return filterToMatchingRecvonly(oldContent, newContent); } }); delete desc.groups; if (desc.contents.length > 0) { + console.log('_removeRecvOnlySourceIfPresent found content remove', desc.contents.length); + // remove msids + desc.contents.forEach(content => { + content.application.sources.forEach(source => { + source.parameters = source.parameters.filter(param => { + if (param.key === 'msid') { + return false; + } + return true; + }); + }); + }) + console.log('jm send sr'); this.send('source-remove', desc); } + else { + console.log('_removeRecvOnlySourceIfPresent no recv-only content to remove'); + } }, _addRecvOnlySourceIfNotPresent: function(oldLocalDescription, newLocalDescription) { @@ -520,14 +828,30 @@ MediaSession.prototype = extend(MediaSession.prototype, { // filter to only sources that changed from recvonly to sendrecv desc.contents = desc.contents.filter(function(newContent) { - if (newContent.application.applicationType === 'rtp' && newContent.application.sources && newContent.application.sources.length) { + if (newContent.application.applicationType === 'rtp' + && newContent.application.media === 'video' + && newContent.application.sources + && newContent.application.sources.length) { var oldContent = findMatchingContentBlock(newContent, oldLocalDescription); // the filter function handles the case where oldContent is null - return filterToMatchingRecvonly(newContent, oldContent); + return filterAddRecvOnlyIfNotPresent(newContent, oldContent); + // return filterToMatchingRecvonly(newContent, oldContent); } }); delete desc.groups; if (desc.contents.length > 0) { + // remove msids + desc.contents.forEach(content => { + content.application.sources.forEach(source => { + source.parameters = source.parameters.filter(param => { + if (param.key === 'msid') { + return false; + } + return true; + }); + }); + }) + this.send('source-add', desc); } }, @@ -761,7 +1085,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); + const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription, true); + // self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); return cb(); }); }, @@ -836,7 +1161,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { return cb({condition: 'general-error'}); } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); + const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + + // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); return cb(); }); }, From f9648a37073223c4ab9534407e296e9938db6bb0 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Wed, 5 Dec 2018 11:49:03 -0700 Subject: [PATCH 02/14] changing includes to indexOf --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 71c5d4b..02a1572 100644 --- a/index.js +++ b/index.js @@ -153,7 +153,7 @@ function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { }; function diffArray(a = [], b = []) { - return a.filter(x => !b.includes(x)); + return a.filter(x => !(b.indexOf(x) > -1 )); } // filters the sources in baseContent to only include sources which don't have an msid (recvonly) and are new @@ -714,7 +714,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { const properContents = []; for (let i = 0; i < contents.length; i++) { const filteredSsrcs = - contents[i].application.sources.filter(source => ssrcList.includes(source.ssrc)); + contents[i].application.sources.filter(source => ssrcList.indexOf(source.ssrc) > -1 ); if (filteredSsrcs.length) { contents[i].application.sources = filteredSsrcs; From acfb8fd40cfcf9973969974d59e609f0faf102a1 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Wed, 5 Dec 2018 12:44:49 -0700 Subject: [PATCH 03/14] removing un used function --- index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/index.js b/index.js index 02a1572..7aee257 100644 --- a/index.js +++ b/index.js @@ -152,9 +152,6 @@ function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { }; -function diffArray(a = [], b = []) { - return a.filter(x => !(b.indexOf(x) > -1 )); -} // filters the sources in baseContent to only include sources which don't have an msid (recvonly) and are new // (not in compareContent sources) or that have a corresponding source in compareContent that has an msid (indicating) From 191bd00828e44e063ac9f44aba7591c4ed98acee Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Wed, 5 Dec 2018 13:36:04 -0700 Subject: [PATCH 04/14] chaning to es5 notation for functions. --- index.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 7aee257..650674a 100644 --- a/index.js +++ b/index.js @@ -678,12 +678,12 @@ MediaSession.prototype = extend(MediaSession.prototype, { console.log('jm b4 shit: ', Object.keys(a)); const sourcesAdded = []; - Object.keys(a).forEach(ssrc => { + Object.keys(a).forEach(function(ssrc) { for (let i = 0; i < newContents.length; i++) { newContents[i].application.sources = - newContents[i].application.sources.filter(source => { - return source.ssrc === ssrc + newContents[i].application.sources.filter(function(source) { + return source.ssrc === ssrc; }); if (newContents[i].application.sources.length) { @@ -711,7 +711,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { const properContents = []; for (let i = 0; i < contents.length; i++) { const filteredSsrcs = - contents[i].application.sources.filter(source => ssrcList.indexOf(source.ssrc) > -1 ); + contents[i].application.sources.filter(function(source) { + ssrcList.indexOf(source.ssrc) > -1; + }); if (filteredSsrcs.length) { contents[i].application.sources = filteredSsrcs; @@ -736,9 +738,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { new_desc.contents = jmContent2; delete desc.groups; delete new_desc.groups; - desc.contents.forEach(content => { - content.application.sources.forEach(source => { - source.parameters = source.parameters.filter(param => { + desc.contents.forEach(function(content) { + content.application.sources.forEach(function(source) { + source.parameters = source.parameters.filter(function(param) { if (param.key === 'msid') { return false; } @@ -761,9 +763,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete desc.groups; delete new_desc.groups; - new_desc.contents.forEach(content => { - content.application.sources.forEach(source => { - source.parameters = source.parameters.filter(param => { + new_desc.contents.forEach(function(content) { + content.application.sources.forEach(function(source) { + source.parameters = source.parameters.filter(function(param) { if (param.key === 'msid') { return false; } @@ -802,9 +804,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { if (desc.contents.length > 0) { console.log('_removeRecvOnlySourceIfPresent found content remove', desc.contents.length); // remove msids - desc.contents.forEach(content => { - content.application.sources.forEach(source => { - source.parameters = source.parameters.filter(param => { + desc.contents.forEach(function(content) { + content.application.sources.forEach(function(source) { + source.parameters = source.parameters.filter(function(param) { if (param.key === 'msid') { return false; } @@ -838,9 +840,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete desc.groups; if (desc.contents.length > 0) { // remove msids - desc.contents.forEach(content => { - content.application.sources.forEach(source => { - source.parameters = source.parameters.filter(param => { + desc.contents.forEach(function(content) { + content.application.sources.forEach(function(source) { + source.parameters = source.parameters.filter(function(param) { if (param.key === 'msid') { return false; } From 058336e16d34d3f1d26606146c6ff350b3633109 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Wed, 5 Dec 2018 13:48:35 -0700 Subject: [PATCH 05/14] changing let to var --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 650674a..50c904c 100644 --- a/index.js +++ b/index.js @@ -591,7 +591,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { const newContents = newLocalDescription.contents.filter(getContent); console.log('jm remove shit oldContents: ', oldContents, newContents); - for (let i = 0; i < newContents; i++) { + for (var i = 0; i < newContents; i++) { } }, @@ -615,9 +615,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { const sourcesModified = []; const sourcesToAddBack = []; - for (let i = 0; i < newContents.length; i++) { + for (var i = 0; i < newContents.length; i++) { console.log('jm new contents', newContents[i].application.sources.length); - for(let j = 0; j < newContents[i].application.sources.length; j++) { + for(var j = 0; j < newContents[i].application.sources.length; j++) { console.log('jm new content sources: ', newContents[i].application.sources[j].ssrc); a[newContents[i].application.sources[j].ssrc] = { source: newContents[i].application.sources[j], @@ -626,8 +626,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { } }; - for (let i = 0; i < oldContents.length; i++) { - for(let j = 0; j < oldContents[i].application.sources.length; j++) { + for (var i = 0; i < oldContents.length; i++) { + for(var j = 0; j < oldContents[i].application.sources.length; j++) { if (!a[oldContents[i].application.sources[j].ssrc]) { // this IS a removed ssource sourcesRemoved.push(oldContents[i].application.sources[j].ssrc); From aefb5f7c6adcfc293e116a80868f138719c8402c Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Wed, 5 Dec 2018 13:52:55 -0700 Subject: [PATCH 06/14] changing let to var --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 50c904c..0e1a913 100644 --- a/index.js +++ b/index.js @@ -679,7 +679,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { const sourcesAdded = []; Object.keys(a).forEach(function(ssrc) { - for (let i = 0; i < newContents.length; i++) { + for (var i = 0; i < newContents.length; i++) { newContents[i].application.sources = newContents[i].application.sources.filter(function(source) { @@ -709,7 +709,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { function getProperSSRCS(contents, ssrcList) { const properContents = []; - for (let i = 0; i < contents.length; i++) { + for (var i = 0; i < contents.length; i++) { const filteredSsrcs = contents[i].application.sources.filter(function(source) { ssrcList.indexOf(source.ssrc) > -1; From 0a2bf8d25fbc410ab4ce9ecddf123c4ff6e1c70b Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Wed, 5 Dec 2018 15:53:07 -0700 Subject: [PATCH 07/14] need to return value for filter to work. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 0e1a913..cb11a05 100644 --- a/index.js +++ b/index.js @@ -712,7 +712,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { for (var i = 0; i < contents.length; i++) { const filteredSsrcs = contents[i].application.sources.filter(function(source) { - ssrcList.indexOf(source.ssrc) > -1; + return ssrcList.indexOf(source.ssrc) > -1; }); if (filteredSsrcs.length) { From 43c2376663d6deced3337faeb47553f1f9a3546f Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Thu, 13 Dec 2018 11:00:25 -0700 Subject: [PATCH 08/14] starting refactor breaking down into smaller methods. --- index.js | 669 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 364 insertions(+), 305 deletions(-) diff --git a/index.js b/index.js index cb11a05..310daa9 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,7 @@ +// working but not refactored as of 11:20 Tuesday + +// can add and remove stream but doing so every other time you publish it has an error. + var util = require('util'); var extend = require('extend-object'); var BaseSession = require('jingle-session'); @@ -19,12 +23,12 @@ function filterContentSources(content, stream) { content.application.sources = content.application.sources.filter(function (source) { // if there's no msid, ignore it if (source.parameters.length < 2) { - return false; + return false; } - + console.log('stream id: ', stream, source.parameters); - return (stream.id === source.parameters[1].value.split(' ')[0] - || stream.label === source.parameters[1].value.split(' ')[0]); + return (stream.id === source.parameters[1].value.split(' ')[0] + || stream.label === source.parameters[1].value.split(' ')[0]); }); } @@ -43,6 +47,143 @@ function filterContentSources(content, stream) { } } +function getContent(content) { + return content.application.applicationType === 'rtp' + && content.application.sources + && content.application.sources.length; +} + +function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { + + const desc = oldLocalDescription; + delete desc.group; + + const oldContents = oldLocalDescription.contents.filter(getContent); + const newContents = newLocalDescription.contents.filter(getContent); + // console.log('wh oldContents: ', oldContents); + // console.log('wh newContents: ', newContents); + + const sourceMap = {}; + const sourcesRemoved = []; + const sourcesModified = []; + const sourcesToAddBack = []; + + for (var i = 0; i < newContents.length; i++) { + // console.log('wh new contents', newContents[i].application.sources.length); + for(var j = 0; j < newContents[i].application.sources.length; j++) { + // console.log('wh new content sources: ', newContents[i].application.sources[j].ssrc); + sourceMap[newContents[i].application.sources[j].ssrc] = { + source: newContents[i].application.sources[j], + direction: newContents[i].senders, + }; + } + }; + + // console.log('wh even more b4 shit: keys ', Object.keys(sourceMap)); + // console.log('wh even more b4 shit: values', Object.values(sourceMap)); + + for (var i = 0; i < oldContents.length; i++) { + for(var j = 0; j < oldContents[i].application.sources.length; j++) { + if (!sourceMap[oldContents[i].application.sources[j].ssrc]) { + // this IS a removed ssource + console.log(' wh pushing source to source removed in enew diff method', oldContents[i].application.sources[j].ssrc) + sourcesRemoved.push(oldContents[i].application.sources[j].ssrc); + } + else { + // this is a possible modified source + const oldContentHasMsid = sourceHasMsid(oldContents[i].application.sources[j]); + const oldContentSourceDirection = oldContents[i].senders; + const newContentHasMsid = sourceHasMsid(sourceMap[oldContents[i].application.sources[j].ssrc].source); + const newContentSourceDirection = sourceMap[oldContents[i].application.sources[j].ssrc].direction; + + if (newContentSourceDirection !== oldContentSourceDirection) { + // Direction has changed + delete oldContents[i].transport; + delete oldContents[i].application.payloads; + + console.log('wh direction change detected'); + + switch (newContentSourceDirection) { + case 'both': + // if newContent does not have msid don't change direction. + if (!newContentHasMsid) { + console.log('wh trying to changed direction to sendrecv that does not have a msid: ', oldContents[i].application.sources[j].ssrc); + } + else { + console.log('wh newContent is now sendrecv -> send sr (old) sa(new)', oldContents[i].application.sources[j].ssrc); + sourcesModified.push(oldContents[i].application.sources[j].ssrc); + } + break; + case 'initiator': + console.log('wh newContent is now recvonly -> send sr (new) sa(old)', oldContents[i].application.sources[j].ssrc); + sourcesToAddBack.push(oldContents[i].application.sources[j].ssrc); + break; + default: + console.log('wh I should not be here'); + } + } + else { + if(oldContentHasMsid !== newContentHasMsid) { + sourcesModified.push(oldContents[i].application.sources[j].ssrc); + console.log('wh the msid from the newcontent was created'); + } + } + } + delete sourceMap[oldContents[i].application.sources[j].ssrc]; + } + } + + const sourcesAdded = []; + + Object.keys(sourceMap).forEach(function(ssrc) { + for (var i = 0; i < newContents.length; i++) { + + newContents[i].application.sources = + newContents[i].application.sources.filter(function(source) { + return source.ssrc === ssrc; + }); + + if (newContents[i].application.sources.length) { + delete newContents[i].transport; + delete newContents[i].ssrc; + delete newContents[i].application.payloads; + sourcesAdded.push(newContents[i]); + } + } + }); + const diffObject = { sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack}; + console.log('wh end of generate differences returning sourcesRemoved', sourcesRemoved); + console.log('wh end of generate differences returning sourcesAdded', sourcesAdded); + console.log('wh end of generate {differences returning sourcesModified', sourcesModified); + console.log('wh end of generate differences returning sourcesToAddBack', sourcesToAddBack); + console.log(' wh end of generate diff object', diffObject); + return diffObject; +} + + +function getProperSSRCS(contents, ssrcList) { + const properContents = []; + for (var i = 0; i < contents.length; i++) { + const filteredSsrcs = + contents[i].application.sources.filter(function(source) { + return ssrcList.indexOf(source.ssrc) > -1; + }); + + if (filteredSsrcs.length) { + contents[i].application.sources = filteredSsrcs; + delete contents[i].transport; + delete contents[i].application.ssrc; + delete contents[i].application.payloads; + contents[i].application.mux = false; + delete contents[i].application.headerExtensions; + properContents.push(contents[i]); + } + else{ + console.log('filteredSsrcs is empty', filteredSsrcs); + } + } + return properContents; +} function filterUnusedLabels(content) { // Remove mslabel and label ssrc-specific attributes @@ -109,11 +250,11 @@ function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { console.log('filterAddRecvOnlyIfNotPresent'); console.log('newContent shit:', newContent.senders); console.log('oldShit', oldContent.senders); - //direction application.senders + //direction application.senders if (newContent.application.sources && newContent.senders === 'initiator') { newContent.application.sources = newContent.application.sources.filter(function (baseSource) { - - + + // if there's a msid, ignore it because its not recvonly // if (sourceHasMsid(baseSource)) { // return false; @@ -155,7 +296,7 @@ function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { // filters the sources in baseContent to only include sources which don't have an msid (recvonly) and are new // (not in compareContent sources) or that have a corresponding source in compareContent that has an msid (indicating) -// that the source changed from recvonly to sendrecv. If no compareContent is passed in then it will filter the +// that the source changed from recvonly to sendrecv. If no compareContent is passed in then it will filter the // content block to any sources without an msid // Returns a boolean indicating that there are recvonly sources function filterToMatchingRecvonly(baseContent, compareContent) { @@ -174,7 +315,7 @@ function filterToMatchingRecvonly(baseContent, compareContent) { baseContent.application.sources = baseContent.application.sources.filter(function (baseSource) { // if there's a msid, ignore it because its not recvonly if (sourceHasMsid(baseSource)) { - return false; + return false; } // try to find correpsonding source in compareContent if it exists @@ -216,8 +357,8 @@ function MediaSession(opts) { }, opts.constraints || {}); this.q = queue({ - autostart: true, - concurrency: 1 + autostart: true, + concurrency: 1 }); this.pc.on('ice', this.onIceCandidate.bind(this, opts)); @@ -238,80 +379,83 @@ function MediaSession(opts) { function queueOfferAnswer(self, errorMsg, jingleDesc, cb) { - console.log('jm - queueOfferAnswer jingleDesc', jingleDesc); - - self.q.push(function(qCb) { - function done(err, answer) { - qCb(); - return (err ? cb(err) : cb(null, answer)); - } + console.log('jm - queueOfferAnswer jingleDesc', jingleDesc, cb); - self.pc.handleOffer({ - type: 'offer', - jingle: jingleDesc - }, function (err) { - if (err) { - self._log('error', 'Could not create offer for ' + errorMsg); - return done(err); - } - - self.pc.answer(self.constraints, function (err, answer) { - if (err) { - self._log('error', 'Could not create answer for ' + errorMsg); - return done(err); + self.q.push(function(qCb) { + function done(err, answer) { + console.log('calling qCb()'); + qCb(); + return (err ? cb(err) : cb(null, answer)); } - // call the remaing logic in the cb - done(null, answer); - }); + self.pc.handleOffer({ + type: 'offer', + jingle: jingleDesc + }, function (err) { + console.log('in handle offer callback'); + if (err) { + self._log('error', 'Could not create offer for ' + errorMsg); + return done(err); + } + + self.pc.answer(self.constraints, function (err, answer) { + if (err) { + self._log('error', 'Could not create answer for ' + errorMsg); + return done(err); + } + + // call the remaing logic in the cb + console.log('at end of Queue offer self.pc.answer '); + done(null, answer); + }); + }); }); - }); } function queueOffer(self, errorMsg, jingleDesc, cb) { - self.q.push(function(qCb) { - function done(err) { - qCb(); - return (err ? cb(err) : cb(null)); - } + self.q.push(function(qCb) { + function done(err) { + qCb(); + return (err ? cb(err) : cb(null)); + } + + self.pc.handleOffer({ + type: 'offer', + jingle: jingleDesc + }, function (err) { + if (err) { + self._log('error', errorMsg); + return done(err); + } - self.pc.handleOffer({ - type: 'offer', - jingle: jingleDesc - }, function (err) { - if (err) { - self._log('error', errorMsg); - return done(err); - } - - // call the remaing logic in the cb - done(); + // call the remaing logic in the cb + done(); + }); }); - }); } function queueAnswer(self, errorMsg, jingleDesc, cb) { - self.q.push(function(qCb) { - function done(err, answer) { - qCb(); - return (err ? cb(err) : cb(null, answer)); - } + self.q.push(function(qCb) { + function done(err, answer) { + qCb(); + return (err ? cb(err) : cb(null, answer)); + } - self.pc.handleAnswer({ - type: 'answer', - jingle: jingleDesc - }, function (err) { - if (err) { - self._log('error', errorMsg); - return done(err); - } - - // call the remaing logic in the cb - done(); + self.pc.handleAnswer({ + type: 'answer', + jingle: jingleDesc + }, function (err) { + if (err) { + self._log('error', errorMsg); + return done(err); + } + + // call the remaing logic in the cb + done(); + }); }); - }); } @@ -355,39 +499,39 @@ MediaSession.prototype = extend(MediaSession.prototype, { this.pc.isInitiator = true; self.q.push(function(qCb) { - self.pc.offer(offerOptions, function (err, offer) { - if (err) { - self._log('error', 'Could not create WebRTC offer', err); - return self.end('failed-application', true); - } - - // a workaround for missing a=sendonly - // https://code.google.com/p/webrtc/issues/detail?id=1553 - if (offerOptions && offerOptions.mandatory) { - offer.jingle.contents.forEach(function (content) { - var mediaType = content.application.media; - - if (!content.description || content.application.applicationType !== 'rtp') { - return; - } - - if (!offerOptions.mandatory.OfferToReceiveAudio && mediaType === 'audio') { - content.senders = 'initiator'; - } - - if (!offerOptions.mandatory.OfferToReceiveVideo && mediaType === 'video') { - content.senders = 'initiator'; - } - }); - } - - offer.jingle.contents.forEach(filterUnusedLabels); - - self.send('session-initiate', offer.jingle); - - next(); - qCb(); - }); + self.pc.offer(offerOptions, function (err, offer) { + if (err) { + self._log('error', 'Could not create WebRTC offer', err); + return self.end('failed-application', true); + } + + // a workaround for missing a=sendonly + // https://code.google.com/p/webrtc/issues/detail?id=1553 + if (offerOptions && offerOptions.mandatory) { + offer.jingle.contents.forEach(function (content) { + var mediaType = content.application.media; + + if (!content.description || content.application.applicationType !== 'rtp') { + return; + } + + if (!offerOptions.mandatory.OfferToReceiveAudio && mediaType === 'audio') { + content.senders = 'initiator'; + } + + if (!offerOptions.mandatory.OfferToReceiveVideo && mediaType === 'video') { + content.senders = 'initiator'; + } + }); + } + + offer.jingle.contents.forEach(filterUnusedLabels); + + self.send('session-initiate', offer.jingle); + + next(); + qCb(); + }); }); }, @@ -414,23 +558,23 @@ MediaSession.prototype = extend(MediaSession.prototype, { self.state = 'active'; self.q.push(function(qCb) { - self.pc.answer(self.constraints, function (err, answer) { - if (err) { - self._log('error', 'Could not create WebRTC answer', err); - return self.end('failed-application'); - } + self.pc.answer(self.constraints, function (err, answer) { + if (err) { + self._log('error', 'Could not create WebRTC answer', err); + return self.end('failed-application'); + } - answer.jingle.contents.forEach(filterUnusedLabels); - // this isn't needed current because we are signaling a source-remove and then source-add when adding a stream - // leaving here since the source-remove, source-add solution breaks firefox -> chrome - // answer.jingle.contents.forEach(filterOutRecvonly); - answer.jingle.contents.forEach(changeSendersIfNoMsids); + answer.jingle.contents.forEach(filterUnusedLabels); + // this isn't needed current because we are signaling a source-remove and then source-add when adding a stream + // leaving here since the source-remove, source-add solution breaks firefox -> chrome + // answer.jingle.contents.forEach(filterOutRecvonly); + answer.jingle.contents.forEach(changeSendersIfNoMsids); - self.send('session-accept', answer.jingle); + self.send('session-accept', answer.jingle); - next(); - qCb(); - }); + next(); + qCb(); + }); }); }, @@ -485,7 +629,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { // ---------------------------------------------------------------- addStream: function (stream, renegotiate, cb) { - console.log('jm addStream'); + console.log('wh addStream'); var self = this; var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); @@ -501,29 +645,33 @@ MediaSession.prototype = extend(MediaSession.prototype, { var errorMsg = 'adding new stream'; queueOfferAnswer(this, errorMsg, self.pc.remoteDescription, function(err, answer) { - if (err) { - self._log('error', 'Could not create offer for ' + errorMsg); - return cb(err); - } + if (err) { + self._log('error', 'Could not create offer for ' + errorMsg); + return cb(err); + } - answer.jingle.contents.forEach(function (content) { - filterContentSources(content, stream); - }); - answer.jingle.contents = answer.jingle.contents.filter(function (content) { - return content.application.applicationType === 'rtp' - && content.application.sources - && content.application.sources.length; - }); - delete answer.jingle.groups; - var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); - // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); + //should be dead code just wanted to get tests working before deleting + answer.jingle.contents.forEach(function (content) { + filterContentSources(content, stream); + }); + answer.jingle.contents = answer.jingle.contents.filter(function (content) { + return content.application.applicationType === 'rtp' + && content.application.sources + && content.application.sources.length; + }); + delete answer.jingle.groups; + //end of dead code + + var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); + const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); - if (answer.jingle) { - self.send('source-add', answer.jingle); - } - return cb(); + if (answer.jingle) { + console.log('wh would be sending source add ', answer.jingle) + // self.send('source-add', answer.jingle); + } + return cb(); }); }, @@ -533,7 +681,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { }, removeStream: function (stream, renegotiate, cb) { - console.log('jm removeStream'); + console.log('wh removeStream'); var self = this; var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); @@ -546,6 +694,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { self.constraints = renegotiate; } + + //should be dead code just wanted to get tests working before deleting var desc = this.pc.localDescription; desc.contents.forEach(function (content) { filterContentSources(content, stream); @@ -556,10 +706,12 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete desc.groups; if (desc.contents.length) { - console.log('jm remove this source dewd: ', desc); - this.send('source-remove', desc); + console.log('wh remove this source dewd: ', desc); + // this.send('source-remove', desc); } - + + //end of dead code + this.pc.removeStream(stream); var errorMsg = 'removing stream'; @@ -579,161 +731,66 @@ MediaSession.prototype = extend(MediaSession.prototype, { this.removeStream(stream, true, cb); }, - _removeShit: function(oldLocalDescription, newLocalDescription) { - const desc = oldLocalDescription; - delete desc.group; - function getContent(content) { - return content.application.applicationType === 'rtp' - && content.application.sources - && content.application.sources.length; - } - const oldContents = oldLocalDescription.contents.filter(getContent); - const newContents = newLocalDescription.contents.filter(getContent); - console.log('jm remove shit oldContents: ', oldContents, newContents); - for (var i = 0; i < newContents; i++) { - } - }, // Justin's new functions _doShit: function(oldLocalDescription, newLocalDescription) { - const desc = oldLocalDescription; - delete desc.group; + + const oldDescCopy = JSON.parse(JSON.stringify(oldLocalDescription)); + const newDescCopy = JSON.parse(JSON.stringify(newLocalDescription)); + + + + var diffObject = generateDifferenceOfSources(oldDescCopy, newDescCopy); + + function getContent(content) { - return content.application.applicationType === 'rtp' - && content.application.sources - && content.application.sources.length; + return content.application.applicationType === 'rtp' + && content.application.sources + && content.application.sources.length; } const oldContents = oldLocalDescription.contents.filter(getContent); const newContents = newLocalDescription.contents.filter(getContent); - console.log('jm oldContents: ', oldContents, newContents); - const a = {}; - const sourcesRemoved = []; - const sourcesModified = []; - const sourcesToAddBack = []; + const sourcesRemoved = diffObject.sourcesRemoved; + const sourcesAdded = diffObject.sourcesAdded; + const sourcesModified = diffObject.sourcesModified; + const sourcesToAddBack = diffObject.sourcesToAddBack; + console.log('this.is ', this); + this._signalDifferenceiInSources({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription, newLocalDescription, oldContents, newContents }); - for (var i = 0; i < newContents.length; i++) { - console.log('jm new contents', newContents[i].application.sources.length); - for(var j = 0; j < newContents[i].application.sources.length; j++) { - console.log('jm new content sources: ', newContents[i].application.sources[j].ssrc); - a[newContents[i].application.sources[j].ssrc] = { - source: newContents[i].application.sources[j], - direction: newContents[i].senders, - }; - } - }; - - for (var i = 0; i < oldContents.length; i++) { - for(var j = 0; j < oldContents[i].application.sources.length; j++) { - if (!a[oldContents[i].application.sources[j].ssrc]) { - // this IS a removed ssource - sourcesRemoved.push(oldContents[i].application.sources[j].ssrc); - } - else { - // this is a possible modified source - const oldContentHasMsid = sourceHasMsid(oldContents[i].application.sources[j]); - const oldContentSourceDirection = oldContents[i].senders; - const newContentHasMsid = sourceHasMsid(a[oldContents[i].application.sources[j].ssrc].source); - const newContentSourceDirection = a[oldContents[i].application.sources[j].ssrc].direction; - - if (newContentSourceDirection !== oldContentSourceDirection) { - // Direction has changed - delete oldContents[i].transport; - delete oldContents[i].application.payloads; - - console.log('jm direction change detected') - - switch (newContentSourceDirection) { - case 'both': - // if newContent does not have msid don't change direction. - if (!newContentHasMsid) { - console.log('jm trying to changed direction to sendrecv that does not have a msid: ', oldContents[i].application.sources[j].ssrc); - } - else { - console.log('jm newContent is now sendrecv -> send sr (old) sa(new)', oldContents[i].application.sources[j].ssrc); - sourcesModified.push(oldContents[i].application.sources[j].ssrc); - } - break; - case 'initiator': - console.log('jm newContent is now recvonly -> send sr (new) sa(old)', oldContents[i].application.sources[j].ssrc); - sourcesToAddBack.push(oldContents[i].application.sources[j].ssrc); - break; - default: - console.log('jm I should not be here'); - } - } - else { - if(oldContentHasMsid && !newContentHasMsid) { - console.log('jm the msid from the newcontent was created'); - } - } - } - delete a[oldContents[i].application.sources[j].ssrc]; - } - } + console.log('wh end of do shit') - console.log('jm b4 shit: ', Object.keys(a)); - const sourcesAdded = []; + }, - Object.keys(a).forEach(function(ssrc) { - for (var i = 0; i < newContents.length; i++) { - - newContents[i].application.sources = - newContents[i].application.sources.filter(function(source) { - return source.ssrc === ssrc; - }); + _signalDifferenceiInSources: function({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription, newLocalDescription, oldContents, newContents }){ - if (newContents[i].application.sources.length) { - delete newContents[i].transport; - delete newContents[i].ssrc; - delete newContents[i].application.payloads; - sourcesAdded.push(newContents[i]); - } - } - }); - console.log('jm removedSources: ', sourcesRemoved); - console.log('jm sourcesAdded: ', sourcesAdded); - console.log('jm sourcesModifed: ', sourcesModified); - console.log('jm sourcesToAddBack: ', sourcesToAddBack) + console.log('wh start signalDifferenceiInSources sourcesRemoved', sourcesRemoved); + console.log('wh start signalDifferenceiInSources sourcesAdded', sourcesAdded); + console.log('wh start signalDifferenceiInSources sourcesModified', sourcesModified); + console.log('wh start signalDifferenceiInSources sourcesToAddBack', sourcesToAddBack); + + const desc = oldLocalDescription; + delete desc.group; if (sourcesAdded.length) { - const new_desc = newLocalDescription; - delete new_desc.groups; - new_desc.contents = sourcesAdded; - this.send('source-add', new_desc); + const new_desc = newLocalDescription; + delete new_desc.groups; + new_desc.contents = sourcesAdded; + this.send('source-add', new_desc); } - function getProperSSRCS(contents, ssrcList) { - const properContents = []; - for (var i = 0; i < contents.length; i++) { - const filteredSsrcs = - contents[i].application.sources.filter(function(source) { - return ssrcList.indexOf(source.ssrc) > -1; - }); - - if (filteredSsrcs.length) { - contents[i].application.sources = filteredSsrcs; - delete contents[i].transport; - delete contents[i].ssrc; - delete contents[i].application.payloads; - contents[i].application.mux = false; - contents[i].application.headerExtensions = []; - properContents.push(contents[i]); - } - } - return properContents; - } if (sourcesModified.length) { const new_desc = newLocalDescription; const jmContent = getProperSSRCS(oldContents, sourcesModified); const jmContent2 = getProperSSRCS(newContents, sourcesModified); - + + desc.contents = jmContent; new_desc.contents = jmContent2; delete desc.groups; @@ -749,6 +806,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); }); + this.send('source-remove', desc); this.send('source-add', new_desc); } @@ -762,7 +820,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { new_desc.contents = jmContent2; delete desc.groups; delete new_desc.groups; - + new_desc.contents.forEach(function(content) { content.application.sources.forEach(function(source) { source.parameters = source.parameters.filter(function(param) { @@ -775,9 +833,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); // desc.contents = sourcesToAddBack; this.send('source-remove', desc); - this.send('source-add', new_desc); + this.send('source-add', new_desc); } - }, _removeRecvOnlySourceIfPresent: function(oldLocalDescription, newLocalDescription) { @@ -787,10 +844,10 @@ MediaSession.prototype = extend(MediaSession.prototype, { // filter to only sources that changed from recvonly to sendrecv desc.contents = desc.contents.filter(function(oldContent) { - + if (oldContent.application.applicationType === 'rtp' - && oldContent.application.sources - && oldContent.application.sources.length) { + && oldContent.application.sources + && oldContent.application.sources.length) { var newContent = findMatchingContentBlock(oldContent, newLocalDescription); console.log('oC sender: ' + oldContent.senders + ' nC sender: ' + newContent.senders) // if (oldContent.senders === 'both' && newContent.senders === 'both') { @@ -829,7 +886,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { desc.contents = desc.contents.filter(function(newContent) { if (newContent.application.applicationType === 'rtp' && newContent.application.media === 'video' - && newContent.application.sources + && newContent.application.sources && newContent.application.sources.length) { var oldContent = findMatchingContentBlock(newContent, oldLocalDescription); // the filter function handles the case where oldContent is null @@ -850,7 +907,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); }); }) - + this.send('source-add', desc); } }, @@ -873,17 +930,17 @@ MediaSession.prototype = extend(MediaSession.prototype, { var errorMsg = 'switching streams'; queueOfferAnswer(self, errorMsg, this.pc.remoteDescription, function(err, answer) { - if (err) { - self._log('error', 'Could not create offer for ' + errorMsg); - return cb(err); - } + if (err) { + self._log('error', 'Could not create offer for ' + errorMsg); + return cb(err); + } - answer.jingle.contents.forEach(function (content) { - delete content.transport; - delete content.application.payloads; - }); - self.send('source-add', answer.jingle); - return err ? cb(err) : cb(); + answer.jingle.contents.forEach(function (content) { + delete content.transport; + delete content.application.payloads; + }); + self.send('source-add', answer.jingle); + return err ? cb(err) : cb(); }); }, @@ -964,7 +1021,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { this.pc.isInitiator = false; var errorMsg = 'Could not create WebRTC answer'; queueOffer(this, errorMsg, changes, function(err) { - return err ? cb({condition: 'general-error'}) : cb(); + return err ? cb({condition: 'general-error'}) : cb(); }); }, @@ -975,11 +1032,11 @@ MediaSession.prototype = extend(MediaSession.prototype, { var errorMsg = 'Could not process WebRTC answer'; queueAnswer(this, errorMsg, changes, function(err) { - if (err) { - return cb({condition: 'general-error'}); - } - self.emit('accepted', self); - cb(); + if (err) { + return cb({condition: 'general-error'}); + } + self.emit('accepted', self); + cb(); }); }, @@ -1032,17 +1089,17 @@ MediaSession.prototype = extend(MediaSession.prototype, { }, onTransportInfo: function (changes, cb) { - var self = this; - self.q.push(function(qCb) { - function done() { - qCb(); - return cb(); - } + var self = this; + self.q.push(function(qCb) { + function done() { + qCb(); + return cb(); + } - self.pc.processIce(changes, function () { - done(); + self.pc.processIce(changes, function () { + done(); + }); }); - }); }, onSourceAdd: function (changes, cb) { @@ -1077,12 +1134,13 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); var errorMsg = 'adding new stream source'; + console.log('wh in onSourceAdd triggering queueOfferAnswer'); var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); queueOfferAnswer(self, errorMsg, newDesc, function(err) { if (err) { return cb({condition: 'general-error'}); } - + var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription, true); // self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); @@ -1154,6 +1212,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); var errorMsg = 'removing stream source'; + console.log('wh in onSourceRemove triggering queueOfferAnswer'); var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); queueOfferAnswer(this, errorMsg, newDesc, function(err) { if (err) { @@ -1161,7 +1220,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); - + // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); return cb(); }); From 742154831c0321e34cfbfc4d1ad4104100fa26ee Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Thu, 13 Dec 2018 21:47:58 -0700 Subject: [PATCH 09/14] adding more logs and some progress towards test passing. --- index.js | 85 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 68 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 310daa9..1f6044b 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ var queue = require('queue'); var jmglobal; function filterContentSources(content, stream) { + console.log('content at start of filterContentSources', JSON.stringify(content, null , 2), stream); if (content.application.applicationType !== 'rtp') { return; } @@ -25,8 +26,6 @@ function filterContentSources(content, stream) { if (source.parameters.length < 2) { return false; } - - console.log('stream id: ', stream, source.parameters); return (stream.id === source.parameters[1].value.split(' ')[0] || stream.label === source.parameters[1].value.split(' ')[0]); }); @@ -45,6 +44,28 @@ function filterContentSources(content, stream) { return found; }); } + console.log('content at end of filterContentSources', JSON.stringify(content, null , 2) ); +} + +function filterMsidFromRecvonlySources(description) { + console.log('start of filter', JSON.stringify(description, null, 2)); + const modifiedDesc = {} + description.contents.forEach(function(content) { + content.application.sources.forEach(function(source) { + console.log('in filterMsidFromRecvonlySources source is ', JSON.stringify(source.parameters)); + source.parameters = source.parameters.filter(function(param) { + if (param.key === 'msid' && (description.senders ==='initiator' || description.senders ==='none')) { + consle.log('filtered an misd') + return false; + } + return true; + }); + console.log('in end of filter source is', JSON.stringify(source.parameters)); + }); + }); + + console.log('end of filter desc is', JSON.stringify(description, null, 2)); + return description; } function getContent(content) { @@ -143,11 +164,14 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { return source.ssrc === ssrc; }); + console.log(' new content checking for at i ', i , JSON.stringify(newContents[i], null, 2)); + + if (newContents[i].application.sources.length) { delete newContents[i].transport; delete newContents[i].ssrc; delete newContents[i].application.payloads; - sourcesAdded.push(newContents[i]); + sourcesAdded.push(newContents[i].application.sources[0].ssrc); } } }); @@ -664,11 +688,17 @@ MediaSession.prototype = extend(MediaSession.prototype, { //end of dead code var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); + + console.log('new local description before filtering ', JSON.stringify(newLocalDescription.contents)) + newLocalDescription.contents.forEach( function (content) { + filterContentSources(content, stream); + }); + console.log('new local description after filtering ', JSON.stringify(newLocalDescription.contents)) const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); if (answer.jingle) { - console.log('wh would be sending source add ', answer.jingle) + console.log('wh would be sending source add ', JSON.stringify(answer.jingle, null , 2)); // self.send('source-add', answer.jingle); } return cb(); @@ -758,7 +788,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { const sourcesAdded = diffObject.sourcesAdded; const sourcesModified = diffObject.sourcesModified; const sourcesToAddBack = diffObject.sourcesToAddBack; - console.log('this.is ', this); this._signalDifferenceiInSources({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription, newLocalDescription, oldContents, newContents }); console.log('wh end of do shit') @@ -774,15 +803,23 @@ MediaSession.prototype = extend(MediaSession.prototype, { console.log('wh start signalDifferenceiInSources sourcesToAddBack', sourcesToAddBack); const desc = oldLocalDescription; - delete desc.group; + delete desc.groups; if (sourcesAdded.length) { const new_desc = newLocalDescription; delete new_desc.groups; - new_desc.contents = sourcesAdded; + const whContent = getProperSSRCS(newContents, sourcesAdded); + new_desc.contents = whContent; + this.send('source-add', new_desc); } + if (sourcesRemoved.length && !sourcesModified.length) { // to avoid signaling remove twice + const whContent = getProperSSRCS(oldContents, sourcesRemoved) + desc.contents = whContent + this.send('source-remove', desc) + } + if (sourcesModified.length) { const new_desc = newLocalDescription; @@ -795,16 +832,23 @@ MediaSession.prototype = extend(MediaSession.prototype, { new_desc.contents = jmContent2; delete desc.groups; delete new_desc.groups; - desc.contents.forEach(function(content) { - content.application.sources.forEach(function(source) { - source.parameters = source.parameters.filter(function(param) { - if (param.key === 'msid') { - return false; - } - return true; - }); - }); - }); + // desc.contents.forEach(function(content) { + // content.application.sources.forEach(function(source) { + // source.parameters = source.parameters.filter(function(param) { + // if (param.key === 'msid') { + // return false; + // } + // return true; + // }); + // }); + // }); + + const filteredDesc = filterMsidFromRecvonlySources(desc); + const filteredNewDesc = filterMsidFromRecvonlySources(new_desc); + + console.log('at end of sources modified with filteredDesc', filteredDesc); + console.log('at end of sources modified with filteredNewDesc', filteredNewDesc); + this.send('source-remove', desc); @@ -1142,6 +1186,13 @@ MediaSession.prototype = extend(MediaSession.prototype, { } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); + newLocalDescription.contents.forEach( function(content) { + delete content.transport; + delete content.application.payloads; + delete content.application.headerExtensions; + }) + console.log('deleting content in new local desc') + const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription, true); // self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); return cb(); From f2f333b854f5038be28d2349d19dedf77ae3fc61 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Fri, 14 Dec 2018 15:14:29 -0700 Subject: [PATCH 10/14] got tests passing and republish working visually --- index.js | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 1f6044b..3f7ecb6 100644 --- a/index.js +++ b/index.js @@ -11,7 +11,6 @@ var queue = require('queue'); var jmglobal; function filterContentSources(content, stream) { - console.log('content at start of filterContentSources', JSON.stringify(content, null , 2), stream); if (content.application.applicationType !== 'rtp') { return; } @@ -44,11 +43,9 @@ function filterContentSources(content, stream) { return found; }); } - console.log('content at end of filterContentSources', JSON.stringify(content, null , 2) ); } function filterMsidFromRecvonlySources(description) { - console.log('start of filter', JSON.stringify(description, null, 2)); const modifiedDesc = {} description.contents.forEach(function(content) { content.application.sources.forEach(function(source) { @@ -60,11 +57,9 @@ function filterMsidFromRecvonlySources(description) { } return true; }); - console.log('in end of filter source is', JSON.stringify(source.parameters)); }); }); - console.log('end of filter desc is', JSON.stringify(description, null, 2)); return description; } @@ -164,7 +159,6 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { return source.ssrc === ssrc; }); - console.log(' new content checking for at i ', i , JSON.stringify(newContents[i], null, 2)); if (newContents[i].application.sources.length) { @@ -593,7 +587,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { // leaving here since the source-remove, source-add solution breaks firefox -> chrome // answer.jingle.contents.forEach(filterOutRecvonly); answer.jingle.contents.forEach(changeSendersIfNoMsids); - + console.log('raw: session accept is ', JSON.stringify(answer.jingle, null, 2)); self.send('session-accept', answer.jingle); next(); @@ -690,9 +684,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); console.log('new local description before filtering ', JSON.stringify(newLocalDescription.contents)) - newLocalDescription.contents.forEach( function (content) { - filterContentSources(content, stream); - }); + // newLocalDescription.contents.forEach( function (content) { + // filterContentSources(content, stream); + // }); console.log('new local description after filtering ', JSON.stringify(newLocalDescription.contents)) const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); @@ -766,6 +760,9 @@ MediaSession.prototype = extend(MediaSession.prototype, { // Justin's new functions _doShit: function(oldLocalDescription, newLocalDescription) { + + console.log('in start of do shit oldLocalDescription', oldLocalDescription); + console.log('in start of do shit newLocalDescription', newLocalDescription); const oldDescCopy = JSON.parse(JSON.stringify(oldLocalDescription)); const newDescCopy = JSON.parse(JSON.stringify(newLocalDescription)); @@ -781,14 +778,16 @@ MediaSession.prototype = extend(MediaSession.prototype, { && content.application.sources.length; } - const oldContents = oldLocalDescription.contents.filter(getContent); - const newContents = newLocalDescription.contents.filter(getContent); + const oldContents = oldDescCopy.contents.filter(getContent); + const newContents = newDescCopy.contents.filter(getContent); const sourcesRemoved = diffObject.sourcesRemoved; const sourcesAdded = diffObject.sourcesAdded; const sourcesModified = diffObject.sourcesModified; const sourcesToAddBack = diffObject.sourcesToAddBack; - this._signalDifferenceiInSources({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription, newLocalDescription, oldContents, newContents }); + console.log('passing oldDescCopy to _signalDifferenceiInSources', JSON.stringify(oldDescCopy, null, 2)); + console.log('passing newDescCopy to _signalDifferenceiInSources', JSON.stringify(newDescCopy, null, 2)); + this._signalDifferenceiInSources({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription:oldDescCopy, newLocalDescription:newDescCopy, oldContents, newContents }); console.log('wh end of do shit') @@ -796,12 +795,16 @@ MediaSession.prototype = extend(MediaSession.prototype, { _signalDifferenceiInSources: function({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription, newLocalDescription, oldContents, newContents }){ + console.log('wh start signalDifferenceiInSources oldContents', JSON.stringify(oldContents, null , 2)); + console.log('wh start signalDifferenceiInSources newContents', JSON.stringify(newContents, null , 2)); + console.log('foo2'); console.log('wh start signalDifferenceiInSources sourcesRemoved', sourcesRemoved); console.log('wh start signalDifferenceiInSources sourcesAdded', sourcesAdded); console.log('wh start signalDifferenceiInSources sourcesModified', sourcesModified); console.log('wh start signalDifferenceiInSources sourcesToAddBack', sourcesToAddBack); + const desc = oldLocalDescription; delete desc.groups; @@ -810,13 +813,14 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete new_desc.groups; const whContent = getProperSSRCS(newContents, sourcesAdded); new_desc.contents = whContent; - + console.log('in sources added sending source added' , new_desc); this.send('source-add', new_desc); } if (sourcesRemoved.length && !sourcesModified.length) { // to avoid signaling remove twice const whContent = getProperSSRCS(oldContents, sourcesRemoved) desc.contents = whContent + console.log('in sources removed sending source remove' , desc); this.send('source-remove', desc) } @@ -846,13 +850,11 @@ MediaSession.prototype = extend(MediaSession.prototype, { const filteredDesc = filterMsidFromRecvonlySources(desc); const filteredNewDesc = filterMsidFromRecvonlySources(new_desc); - console.log('at end of sources modified with filteredDesc', filteredDesc); - console.log('at end of sources modified with filteredNewDesc', filteredNewDesc); + console.log('in sources modified sending source remove ', filteredDesc ); + console.log('in sources modified sending source add ', filteredNewDesc ); - - - this.send('source-remove', desc); - this.send('source-add', new_desc); + this.send('source-remove', filteredDesc); + this.send('source-add', filteredNewDesc); } if (sourcesToAddBack.length) { const new_desc = newLocalDescription; @@ -875,6 +877,10 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); }); }); + + console.log('in sources to add back sending source remove ', desc ); + console.log('in sources to add back sending source add ', new_desc ); + // desc.contents = sourcesToAddBack; this.send('source-remove', desc); this.send('source-add', new_desc); From d2539edc3ed9cb4e8f30fc5bff87717360aca0a9 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Mon, 17 Dec 2018 13:07:19 -0700 Subject: [PATCH 11/14] removing es6 code and getting to es5 --- index.js | 177 ++++++------------------------------------------------- 1 file changed, 18 insertions(+), 159 deletions(-) diff --git a/index.js b/index.js index 3f7ecb6..ed89c78 100644 --- a/index.js +++ b/index.js @@ -52,7 +52,6 @@ function filterMsidFromRecvonlySources(description) { console.log('in filterMsidFromRecvonlySources source is ', JSON.stringify(source.parameters)); source.parameters = source.parameters.filter(function(param) { if (param.key === 'msid' && (description.senders ==='initiator' || description.senders ==='none')) { - consle.log('filtered an misd') return false; } return true; @@ -76,8 +75,6 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { const oldContents = oldLocalDescription.contents.filter(getContent); const newContents = newLocalDescription.contents.filter(getContent); - // console.log('wh oldContents: ', oldContents); - // console.log('wh newContents: ', newContents); const sourceMap = {}; const sourcesRemoved = []; @@ -85,9 +82,7 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { const sourcesToAddBack = []; for (var i = 0; i < newContents.length; i++) { - // console.log('wh new contents', newContents[i].application.sources.length); for(var j = 0; j < newContents[i].application.sources.length; j++) { - // console.log('wh new content sources: ', newContents[i].application.sources[j].ssrc); sourceMap[newContents[i].application.sources[j].ssrc] = { source: newContents[i].application.sources[j], direction: newContents[i].senders, @@ -95,14 +90,11 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { } }; - // console.log('wh even more b4 shit: keys ', Object.keys(sourceMap)); - // console.log('wh even more b4 shit: values', Object.values(sourceMap)); for (var i = 0; i < oldContents.length; i++) { for(var j = 0; j < oldContents[i].application.sources.length; j++) { if (!sourceMap[oldContents[i].application.sources[j].ssrc]) { // this IS a removed ssource - console.log(' wh pushing source to source removed in enew diff method', oldContents[i].application.sources[j].ssrc) sourcesRemoved.push(oldContents[i].application.sources[j].ssrc); } else { @@ -117,21 +109,17 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { delete oldContents[i].transport; delete oldContents[i].application.payloads; - console.log('wh direction change detected'); switch (newContentSourceDirection) { case 'both': // if newContent does not have msid don't change direction. if (!newContentHasMsid) { - console.log('wh trying to changed direction to sendrecv that does not have a msid: ', oldContents[i].application.sources[j].ssrc); } else { - console.log('wh newContent is now sendrecv -> send sr (old) sa(new)', oldContents[i].application.sources[j].ssrc); sourcesModified.push(oldContents[i].application.sources[j].ssrc); } break; case 'initiator': - console.log('wh newContent is now recvonly -> send sr (new) sa(old)', oldContents[i].application.sources[j].ssrc); sourcesToAddBack.push(oldContents[i].application.sources[j].ssrc); break; default: @@ -141,7 +129,6 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { else { if(oldContentHasMsid !== newContentHasMsid) { sourcesModified.push(oldContents[i].application.sources[j].ssrc); - console.log('wh the msid from the newcontent was created'); } } } @@ -169,13 +156,7 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { } } }); - const diffObject = { sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack}; - console.log('wh end of generate differences returning sourcesRemoved', sourcesRemoved); - console.log('wh end of generate differences returning sourcesAdded', sourcesAdded); - console.log('wh end of generate {differences returning sourcesModified', sourcesModified); - console.log('wh end of generate differences returning sourcesToAddBack', sourcesToAddBack); - console.log(' wh end of generate diff object', diffObject); - return diffObject; + return { sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified, sourcesToAddBack: sourcesToAddBack }; } @@ -265,20 +246,11 @@ function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { delete newContent.application.ssrc; newContent.application.mux = false; - console.log('filterAddRecvOnlyIfNotPresent'); - console.log('newContent shit:', newContent.senders); - console.log('oldShit', oldContent.senders); //direction application.senders if (newContent.application.sources && newContent.senders === 'initiator') { newContent.application.sources = newContent.application.sources.filter(function (baseSource) { - // if there's a msid, ignore it because its not recvonly - // if (sourceHasMsid(baseSource)) { - // return false; - // } - - // try to find correpsonding source in compareContent if it exists var foundNewRecvonlySource = false; if (oldContent) { @@ -397,11 +369,9 @@ function MediaSession(opts) { function queueOfferAnswer(self, errorMsg, jingleDesc, cb) { - console.log('jm - queueOfferAnswer jingleDesc', jingleDesc, cb); self.q.push(function(qCb) { function done(err, answer) { - console.log('calling qCb()'); qCb(); return (err ? cb(err) : cb(null, answer)); } @@ -410,7 +380,6 @@ function queueOfferAnswer(self, errorMsg, jingleDesc, cb) { type: 'offer', jingle: jingleDesc }, function (err) { - console.log('in handle offer callback'); if (err) { self._log('error', 'Could not create offer for ' + errorMsg); return done(err); @@ -423,7 +392,6 @@ function queueOfferAnswer(self, errorMsg, jingleDesc, cb) { } // call the remaing logic in the cb - console.log('at end of Queue offer self.pc.answer '); done(null, answer); }); }); @@ -587,7 +555,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { // leaving here since the source-remove, source-add solution breaks firefox -> chrome // answer.jingle.contents.forEach(filterOutRecvonly); answer.jingle.contents.forEach(changeSendersIfNoMsids); - console.log('raw: session accept is ', JSON.stringify(answer.jingle, null, 2)); self.send('session-accept', answer.jingle); next(); @@ -683,18 +650,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - console.log('new local description before filtering ', JSON.stringify(newLocalDescription.contents)) - // newLocalDescription.contents.forEach( function (content) { - // filterContentSources(content, stream); - // }); - console.log('new local description after filtering ', JSON.stringify(newLocalDescription.contents)) const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); - // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); - if (answer.jingle) { - console.log('wh would be sending source add ', JSON.stringify(answer.jingle, null , 2)); - // self.send('source-add', answer.jingle); - } return cb(); }); @@ -729,10 +686,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); delete desc.groups; - if (desc.contents.length) { - console.log('wh remove this source dewd: ', desc); - // this.send('source-remove', desc); - } //end of dead code @@ -746,7 +699,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); - // self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); cb(); }); }, @@ -761,8 +713,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { _doShit: function(oldLocalDescription, newLocalDescription) { - console.log('in start of do shit oldLocalDescription', oldLocalDescription); - console.log('in start of do shit newLocalDescription', newLocalDescription); const oldDescCopy = JSON.parse(JSON.stringify(oldLocalDescription)); const newDescCopy = JSON.parse(JSON.stringify(newLocalDescription)); @@ -785,51 +735,39 @@ MediaSession.prototype = extend(MediaSession.prototype, { const sourcesAdded = diffObject.sourcesAdded; const sourcesModified = diffObject.sourcesModified; const sourcesToAddBack = diffObject.sourcesToAddBack; - console.log('passing oldDescCopy to _signalDifferenceiInSources', JSON.stringify(oldDescCopy, null, 2)); - console.log('passing newDescCopy to _signalDifferenceiInSources', JSON.stringify(newDescCopy, null, 2)); - this._signalDifferenceiInSources({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription:oldDescCopy, newLocalDescription:newDescCopy, oldContents, newContents }); + this._signalDifferenceiInSources({sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified, + sourcesToAddBack: sourcesToAddBack, oldLocalDescription: oldDescCopy, newLocalDescription: newDescCopy, oldContents: oldContents, newContents: newContents }); - console.log('wh end of do shit') }, - _signalDifferenceiInSources: function({sourcesRemoved, sourcesAdded, sourcesModified, sourcesToAddBack, oldLocalDescription, newLocalDescription, oldContents, newContents }){ + _signalDifferenceiInSources: function(diffObject){ - console.log('wh start signalDifferenceiInSources oldContents', JSON.stringify(oldContents, null , 2)); - console.log('wh start signalDifferenceiInSources newContents', JSON.stringify(newContents, null , 2)); - - console.log('foo2'); - console.log('wh start signalDifferenceiInSources sourcesRemoved', sourcesRemoved); - console.log('wh start signalDifferenceiInSources sourcesAdded', sourcesAdded); - console.log('wh start signalDifferenceiInSources sourcesModified', sourcesModified); - console.log('wh start signalDifferenceiInSources sourcesToAddBack', sourcesToAddBack); - - - const desc = oldLocalDescription; + const desc = diffObject.oldLocalDescription; delete desc.groups; - if (sourcesAdded.length) { - const new_desc = newLocalDescription; + if (diffObject.sourcesAdded.length) { + const new_desc = diffObject.newLocalDescription; delete new_desc.groups; - const whContent = getProperSSRCS(newContents, sourcesAdded); + const whContent = getProperSSRCS(diffObject.newContents, diffObject.sourcesAdded); new_desc.contents = whContent; console.log('in sources added sending source added' , new_desc); this.send('source-add', new_desc); } - if (sourcesRemoved.length && !sourcesModified.length) { // to avoid signaling remove twice - const whContent = getProperSSRCS(oldContents, sourcesRemoved) + if (diffObject.sourcesRemoved.length && !diffObject.sourcesModified.length) { // to avoid signaling remove twice + const whContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesRemoved); desc.contents = whContent console.log('in sources removed sending source remove' , desc); this.send('source-remove', desc) } - if (sourcesModified.length) { - const new_desc = newLocalDescription; + if (diffObject.sourcesModified.length) { + const new_desc = diffObject.newLocalDescription; - const jmContent = getProperSSRCS(oldContents, sourcesModified); - const jmContent2 = getProperSSRCS(newContents, sourcesModified); + const jmContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesModified); + const jmContent2 = getProperSSRCS(diffObject.newContents, diffObject.sourcesModified); desc.contents = jmContent; @@ -856,10 +794,10 @@ MediaSession.prototype = extend(MediaSession.prototype, { this.send('source-remove', filteredDesc); this.send('source-add', filteredNewDesc); } - if (sourcesToAddBack.length) { - const new_desc = newLocalDescription; - const jmContent = getProperSSRCS(oldContents, sourcesToAddBack); - const jmContent2 = getProperSSRCS(newContents, sourcesToAddBack); + if (diffObject.sourcesToAddBack.length) { + const new_desc = diffObject.newLocalDescription; + const jmContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesToAddBack); + const jmContent2 = getProperSSRCS(diffObject.newContents, diffObject.sourcesToAddBack); console.log('jmContents: ', jmContent, jmContent2); desc.contents = jmContent; @@ -887,81 +825,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { } }, - _removeRecvOnlySourceIfPresent: function(oldLocalDescription, newLocalDescription) { - var desc = oldLocalDescription; - - console.log('_removeRecvOnlySourceIfPresent: ', oldLocalDescription, newLocalDescription); - - // filter to only sources that changed from recvonly to sendrecv - desc.contents = desc.contents.filter(function(oldContent) { - - if (oldContent.application.applicationType === 'rtp' - && oldContent.application.sources - && oldContent.application.sources.length) { - var newContent = findMatchingContentBlock(oldContent, newLocalDescription); - console.log('oC sender: ' + oldContent.senders + ' nC sender: ' + newContent.senders) - // if (oldContent.senders === 'both' && newContent.senders === 'both') { - // return false; - // } - // the filter function handles the case where oldContent is null - return filterToMatchingRecvonly(oldContent, newContent); - } - }); - delete desc.groups; - if (desc.contents.length > 0) { - console.log('_removeRecvOnlySourceIfPresent found content remove', desc.contents.length); - // remove msids - desc.contents.forEach(function(content) { - content.application.sources.forEach(function(source) { - source.parameters = source.parameters.filter(function(param) { - if (param.key === 'msid') { - return false; - } - return true; - }); - }); - }) - console.log('jm send sr'); - this.send('source-remove', desc); - } - else { - console.log('_removeRecvOnlySourceIfPresent no recv-only content to remove'); - } - }, - - _addRecvOnlySourceIfNotPresent: function(oldLocalDescription, newLocalDescription) { - var desc = newLocalDescription; - - // filter to only sources that changed from recvonly to sendrecv - desc.contents = desc.contents.filter(function(newContent) { - if (newContent.application.applicationType === 'rtp' - && newContent.application.media === 'video' - && newContent.application.sources - && newContent.application.sources.length) { - var oldContent = findMatchingContentBlock(newContent, oldLocalDescription); - // the filter function handles the case where oldContent is null - return filterAddRecvOnlyIfNotPresent(newContent, oldContent); - // return filterToMatchingRecvonly(newContent, oldContent); - } - }); - delete desc.groups; - if (desc.contents.length > 0) { - // remove msids - desc.contents.forEach(function(content) { - content.application.sources.forEach(function(source) { - source.parameters = source.parameters.filter(function(param) { - if (param.key === 'msid') { - return false; - } - return true; - }); - }); - }) - - this.send('source-add', desc); - } - }, - switchStream: function (oldStream, newStream, cb) { var self = this; @@ -1184,7 +1047,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); var errorMsg = 'adding new stream source'; - console.log('wh in onSourceAdd triggering queueOfferAnswer'); var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); queueOfferAnswer(self, errorMsg, newDesc, function(err) { if (err) { @@ -1197,10 +1059,8 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete content.application.payloads; delete content.application.headerExtensions; }) - console.log('deleting content in new local desc') const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription, true); - // self._addRecvOnlySourceIfNotPresent(oldLocalDescription, newLocalDescription); return cb(); }); }, @@ -1278,7 +1138,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); - // self._removeRecvOnlySourceIfPresent(oldLocalDescription, newLocalDescription); return cb(); }); }, From 78a8742a4dab4519458503e48772c12d7bd46310 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Mon, 17 Dec 2018 15:36:47 -0700 Subject: [PATCH 12/14] fixing add stream for chrome. --- index.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index ed89c78..dc19a5a 100644 --- a/index.js +++ b/index.js @@ -136,30 +136,32 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { } } + const sourcesAdded = []; - Object.keys(sourceMap).forEach(function(ssrc) { - for (var i = 0; i < newContents.length; i++) { - newContents[i].application.sources = - newContents[i].application.sources.filter(function(source) { - return source.ssrc === ssrc; - }); + for (var i = 0; i < newContents.length; i++) { + newContents[i].application.sources = + newContents[i].application.sources.filter(function(source) { + return sourceMap[source.ssrc] + }); - if (newContents[i].application.sources.length) { - delete newContents[i].transport; - delete newContents[i].ssrc; - delete newContents[i].application.payloads; - sourcesAdded.push(newContents[i].application.sources[0].ssrc); + if (newContents[i].application.sources.length) { + delete newContents[i].transport; + delete newContents[i].ssrc; + delete newContents[i].application.payloads; + for( var j = 0; j < newContents[i].application.sources.length; j++ ){ + sourcesAdded.push(newContents[i].application.sources[j].ssrc); } } - }); + }; return { sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified, sourcesToAddBack: sourcesToAddBack }; } + function getProperSSRCS(contents, ssrcList) { const properContents = []; for (var i = 0; i < contents.length; i++) { From e48fb05958a83069ae1fb6f0af79ad8c402003e9 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Mon, 17 Dec 2018 17:13:51 -0700 Subject: [PATCH 13/14] fixing add stream for firefox after breaking it when fixing chrome. --- index.js | 187 +++++++++---------------------------------------------- 1 file changed, 30 insertions(+), 157 deletions(-) diff --git a/index.js b/index.js index dc19a5a..a1ac423 100644 --- a/index.js +++ b/index.js @@ -137,26 +137,31 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { } - const sourcesAdded = []; - - - for (var i = 0; i < newContents.length; i++) { - - - newContents[i].application.sources = - newContents[i].application.sources.filter(function(source) { - return sourceMap[source.ssrc] - }); - - if (newContents[i].application.sources.length) { - delete newContents[i].transport; - delete newContents[i].ssrc; - delete newContents[i].application.payloads; - for( var j = 0; j < newContents[i].application.sources.length; j++ ){ - sourcesAdded.push(newContents[i].application.sources[j].ssrc); - } - } - }; + const sourcesAdded = Object.keys(sourceMap); + + // + // for (var i = 0; i < newContents.length; i++) { + // + // + // newContents[i].application.sources = + // newContents[i].application.sources.filter(function(source) { + // return sourceMap[source.ssrc] + // }); + // + // if (newContents[i].application.sources.length) { + // delete newContents[i].transport; + // delete newContents[i].ssrc; + // delete newContents[i].application.payloads; + // for( var j = 0; j < newContents[i].application.sources.length; j++ ){ + // sourcesAdded.push(newContents[i].application.sources[j].ssrc); + // } + // } + // }; + + console.log('wh end of generate differences returning sourcesRemoved', sourcesRemoved); + console.log('wh end of generate differences returning sourcesAdded', sourcesAdded); + console.log('wh end of generate {differences returning sourcesModified', sourcesModified); + console.log('wh end of generate differences returning sourcesToAddBack', sourcesToAddBack); return { sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified, sourcesToAddBack: sourcesToAddBack }; } @@ -179,9 +184,6 @@ function getProperSSRCS(contents, ssrcList) { delete contents[i].application.headerExtensions; properContents.push(contents[i]); } - else{ - console.log('filteredSsrcs is empty', filteredSsrcs); - } } return properContents; } @@ -196,18 +198,6 @@ function filterUnusedLabels(content) { }); } -function findMatchingContentBlock(content, jingleDescription) { - var contents = jingleDescription.contents || []; - var matchingContents = contents.filter(function (compareContent) { - return content.name === compareContent.name; - }); - // intentionally returns null if more than one is matched as that shouldn't normally happen - if (matchingContents.length === 1) { - return matchingContents[0]; - } - return null; -} - function findMatchingSource(baseSource, compareSources) { compareSources = compareSources || []; for (var i = 0; i < compareSources.length; i++) { @@ -236,109 +226,6 @@ function changeSendersIfNoMsids(content) { } } -// When we remove a source and need to add a recvonly source -function filterAddRecvOnlyIfNotPresent(newContent, oldContent) { - if (newContent.application.applicationType !== 'rtp') { - return; - } - - delete newContent.transport; - delete newContent.application.payloads; - delete newContent.application.headerExtensions; - delete newContent.application.ssrc; - newContent.application.mux = false; - - //direction application.senders - if (newContent.application.sources && newContent.senders === 'initiator') { - newContent.application.sources = newContent.application.sources.filter(function (baseSource) { - - - // try to find correpsonding source in compareContent if it exists - var foundNewRecvonlySource = false; - if (oldContent) { - var compareSource = findMatchingSource(baseSource, oldContent.application.sources); - // if the source is new or the source is now read only - // if(!compareSource || (compareSource && sourceHasMsid(compareSource))) { - if (!compareSource && oldContent.senders === 'both') { - foundNewRecvonlySource = true; - } - } else { - foundNewRecvonlySource = true; - } - return foundNewRecvonlySource; - }); - } - // remove source groups not related to this stream - if (newContent.application.sourceGroups) { - newContent.application.sourceGroups = newContent.application.sourceGroups.filter(function (group) { - var found = false; - for (var i = 0; i < newContent.application.sources.length; i++) { - if (newContent.application.sources[i].ssrc === group.sources[0]) { - found = true; - break; - } - } - return found; - }); - } - return newContent.application.sources.length; - -}; - - -// filters the sources in baseContent to only include sources which don't have an msid (recvonly) and are new -// (not in compareContent sources) or that have a corresponding source in compareContent that has an msid (indicating) -// that the source changed from recvonly to sendrecv. If no compareContent is passed in then it will filter the -// content block to any sources without an msid -// Returns a boolean indicating that there are recvonly sources -function filterToMatchingRecvonly(baseContent, compareContent) { - // if the content is not rtp, ignore it - if (baseContent.application.applicationType !== 'rtp') { - return; - } - - delete baseContent.transport; - delete baseContent.application.payloads; - delete baseContent.application.headerExtensions; - delete baseContent.application.ssrc; - baseContent.application.mux = false; - - if (baseContent.application.sources) { - baseContent.application.sources = baseContent.application.sources.filter(function (baseSource) { - // if there's a msid, ignore it because its not recvonly - if (sourceHasMsid(baseSource)) { - return false; - } - - // try to find correpsonding source in compareContent if it exists - var foundNewRecvonlySource = false; - if (compareContent) { - var compareSource = findMatchingSource(baseSource, compareContent.application.sources); - // if the source is new or the source is now read only - if(!compareSource || (compareSource && sourceHasMsid(compareSource))) { - foundNewRecvonlySource = true; - } - } else { - foundNewRecvonlySource = true; - } - return foundNewRecvonlySource; - }); - } - // remove source groups not related to this stream - if (baseContent.application.sourceGroups) { - baseContent.application.sourceGroups = baseContent.application.sourceGroups.filter(function (group) { - var found = false; - for (var i = 0; i < baseContent.application.sources.length; i++) { - if (baseContent.application.sources[i].ssrc === group.sources[0]) { - found = true; - break; - } - } - return found; - }); - } - return baseContent.application.sources.length; -} function MediaSession(opts) { BaseSession.call(this, opts); @@ -652,7 +539,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + self._doShit(oldLocalDescription, newLocalDescription); return cb(); }); @@ -700,7 +587,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + self._doShit(oldLocalDescription, newLocalDescription); cb(); }); }, @@ -749,19 +636,18 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete desc.groups; if (diffObject.sourcesAdded.length) { + const new_desc = diffObject.newLocalDescription; delete new_desc.groups; const whContent = getProperSSRCS(diffObject.newContents, diffObject.sourcesAdded); new_desc.contents = whContent; - console.log('in sources added sending source added' , new_desc); this.send('source-add', new_desc); } if (diffObject.sourcesRemoved.length && !diffObject.sourcesModified.length) { // to avoid signaling remove twice const whContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesRemoved); - desc.contents = whContent - console.log('in sources removed sending source remove' , desc); - this.send('source-remove', desc) + desc.contents = whContent; + this.send('source-remove', desc); } @@ -776,23 +662,10 @@ MediaSession.prototype = extend(MediaSession.prototype, { new_desc.contents = jmContent2; delete desc.groups; delete new_desc.groups; - // desc.contents.forEach(function(content) { - // content.application.sources.forEach(function(source) { - // source.parameters = source.parameters.filter(function(param) { - // if (param.key === 'msid') { - // return false; - // } - // return true; - // }); - // }); - // }); const filteredDesc = filterMsidFromRecvonlySources(desc); const filteredNewDesc = filterMsidFromRecvonlySources(new_desc); - console.log('in sources modified sending source remove ', filteredDesc ); - console.log('in sources modified sending source add ', filteredNewDesc ); - this.send('source-remove', filteredDesc); this.send('source-add', filteredNewDesc); } From a54c080aadefec401757c16e44c562d85ed44834 Mon Sep 17 00:00:00 2001 From: waverlyhinton Date: Tue, 18 Dec 2018 12:32:22 -0700 Subject: [PATCH 14/14] cleaning up logic --- index.js | 174 ++++++++++--------------------------------------------- 1 file changed, 30 insertions(+), 144 deletions(-) diff --git a/index.js b/index.js index a1ac423..38ea0d5 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -// working but not refactored as of 11:20 Tuesday // can add and remove stream but doing so every other time you publish it has an error. @@ -8,8 +7,6 @@ var BaseSession = require('jingle-session'); var RTCPeerConnection = require('rtcpeerconnection'); var queue = require('queue'); -var jmglobal; - function filterContentSources(content, stream) { if (content.application.applicationType !== 'rtp') { return; @@ -46,10 +43,8 @@ function filterContentSources(content, stream) { } function filterMsidFromRecvonlySources(description) { - const modifiedDesc = {} description.contents.forEach(function(content) { content.application.sources.forEach(function(source) { - console.log('in filterMsidFromRecvonlySources source is ', JSON.stringify(source.parameters)); source.parameters = source.parameters.filter(function(param) { if (param.key === 'msid' && (description.senders ==='initiator' || description.senders ==='none')) { return false; @@ -69,18 +64,14 @@ function getContent(content) { } function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { - - const desc = oldLocalDescription; - delete desc.group; - const oldContents = oldLocalDescription.contents.filter(getContent); const newContents = newLocalDescription.contents.filter(getContent); const sourceMap = {}; const sourcesRemoved = []; const sourcesModified = []; - const sourcesToAddBack = []; + //creating map of sources and directions for (var i = 0; i < newContents.length; i++) { for(var j = 0; j < newContents[i].application.sources.length; j++) { sourceMap[newContents[i].application.sources[j].ssrc] = { @@ -104,30 +95,10 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { const newContentHasMsid = sourceHasMsid(sourceMap[oldContents[i].application.sources[j].ssrc].source); const newContentSourceDirection = sourceMap[oldContents[i].application.sources[j].ssrc].direction; - if (newContentSourceDirection !== oldContentSourceDirection) { - // Direction has changed - delete oldContents[i].transport; - delete oldContents[i].application.payloads; - - - switch (newContentSourceDirection) { - case 'both': - // if newContent does not have msid don't change direction. - if (!newContentHasMsid) { - } - else { - sourcesModified.push(oldContents[i].application.sources[j].ssrc); - } - break; - case 'initiator': - sourcesToAddBack.push(oldContents[i].application.sources[j].ssrc); - break; - default: - console.log('wh I should not be here'); - } - } - else { - if(oldContentHasMsid !== newContentHasMsid) { + if (newContentHasMsid) { + if ((newContentSourceDirection !== oldContentSourceDirection) || (oldContentHasMsid !== newContentHasMsid)) { + delete oldContents[i].transport; + delete oldContents[i].application.payloads; sourcesModified.push(oldContents[i].application.sources[j].ssrc); } } @@ -135,34 +106,10 @@ function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) { delete sourceMap[oldContents[i].application.sources[j].ssrc]; } } - - + // remaning sources in map are new const sourcesAdded = Object.keys(sourceMap); - // - // for (var i = 0; i < newContents.length; i++) { - // - // - // newContents[i].application.sources = - // newContents[i].application.sources.filter(function(source) { - // return sourceMap[source.ssrc] - // }); - // - // if (newContents[i].application.sources.length) { - // delete newContents[i].transport; - // delete newContents[i].ssrc; - // delete newContents[i].application.payloads; - // for( var j = 0; j < newContents[i].application.sources.length; j++ ){ - // sourcesAdded.push(newContents[i].application.sources[j].ssrc); - // } - // } - // }; - - console.log('wh end of generate differences returning sourcesRemoved', sourcesRemoved); - console.log('wh end of generate differences returning sourcesAdded', sourcesAdded); - console.log('wh end of generate {differences returning sourcesModified', sourcesModified); - console.log('wh end of generate differences returning sourcesToAddBack', sourcesToAddBack); - return { sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified, sourcesToAddBack: sourcesToAddBack }; + return { sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified }; } @@ -246,7 +193,6 @@ function MediaSession(opts) { this.pc.on('addStream', this.onAddStream.bind(this)); this.pc.on('removeStream', this.onRemoveStream.bind(this)); this.pc.on('addChannel', this.onAddChannel.bind(this)); - // this.pc.on('negotiationNeeded', this.onNegotiationNeeded.bind(this)); if (opts.stream) { this.addStream(opts.stream); @@ -503,7 +449,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { // ---------------------------------------------------------------- addStream: function (stream, renegotiate, cb) { - console.log('wh addStream'); var self = this; var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); @@ -539,7 +484,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - self._doShit(oldLocalDescription, newLocalDescription); + self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription); return cb(); }); @@ -551,7 +496,6 @@ MediaSession.prototype = extend(MediaSession.prototype, { }, removeStream: function (stream, renegotiate, cb) { - console.log('wh removeStream'); var self = this; var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); @@ -587,7 +531,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - self._doShit(oldLocalDescription, newLocalDescription); + self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription); cb(); }); }, @@ -599,105 +543,48 @@ MediaSession.prototype = extend(MediaSession.prototype, { // Justin's new functions - _doShit: function(oldLocalDescription, newLocalDescription) { + _determineDifferencesAndSignal: function(oldLocalDescription, newLocalDescription) { const oldDescCopy = JSON.parse(JSON.stringify(oldLocalDescription)); const newDescCopy = JSON.parse(JSON.stringify(newLocalDescription)); - var diffObject = generateDifferenceOfSources(oldDescCopy, newDescCopy); - - - function getContent(content) { - return content.application.applicationType === 'rtp' - && content.application.sources - && content.application.sources.length; - } - - const oldContents = oldDescCopy.contents.filter(getContent); - const newContents = newDescCopy.contents.filter(getContent); - - const sourcesRemoved = diffObject.sourcesRemoved; - const sourcesAdded = diffObject.sourcesAdded; - const sourcesModified = diffObject.sourcesModified; - const sourcesToAddBack = diffObject.sourcesToAddBack; - this._signalDifferenceiInSources({sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified, - sourcesToAddBack: sourcesToAddBack, oldLocalDescription: oldDescCopy, newLocalDescription: newDescCopy, oldContents: oldContents, newContents: newContents }); - - + this._signalDifferenceiInSources({sourcesRemoved: diffObject.sourcesRemoved, sourcesAdded: diffObject.sourcesAdded, sourcesModified: diffObject.sourcesModified, + oldLocalDescription: oldDescCopy, newLocalDescription: newDescCopy }); }, _signalDifferenceiInSources: function(diffObject){ - - const desc = diffObject.oldLocalDescription; - delete desc.groups; + const oldContents = diffObject.oldLocalDescription.contents.filter(getContent); + const newContents = diffObject.newLocalDescription.contents.filter(getContent); + delete diffObject.oldLocalDescription.groups; + delete diffObject.newLocalDescription.groups; if (diffObject.sourcesAdded.length) { - - const new_desc = diffObject.newLocalDescription; - delete new_desc.groups; - const whContent = getProperSSRCS(diffObject.newContents, diffObject.sourcesAdded); - new_desc.contents = whContent; - this.send('source-add', new_desc); + diffObject.newLocalDescription.contents = getProperSSRCS(newContents, diffObject.sourcesAdded); + this._log('info', 'sending source add', diffObject.newLocalDescription); + this.send('source-add', diffObject.newLocalDescription); } if (diffObject.sourcesRemoved.length && !diffObject.sourcesModified.length) { // to avoid signaling remove twice - const whContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesRemoved); - desc.contents = whContent; - this.send('source-remove', desc); + diffObject.oldLocalDescription.contents = getProperSSRCS(oldContents, diffObject.sourcesRemoved); + this._log('info', 'sending source remove', diffObject.oldLocalDescription); + this.send('source-remove', diffObject.oldLocalDescription); } - if (diffObject.sourcesModified.length) { - const new_desc = diffObject.newLocalDescription; - - const jmContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesModified); - const jmContent2 = getProperSSRCS(diffObject.newContents, diffObject.sourcesModified); - - - desc.contents = jmContent; - new_desc.contents = jmContent2; - delete desc.groups; - delete new_desc.groups; - - const filteredDesc = filterMsidFromRecvonlySources(desc); - const filteredNewDesc = filterMsidFromRecvonlySources(new_desc); + diffObject.oldLocalDescription.contents = getProperSSRCS(oldContents, diffObject.sourcesModified); + diffObject.newLocalDescription.contents = getProperSSRCS(newContents, diffObject.sourcesModified);; + const filteredDesc = filterMsidFromRecvonlySources(diffObject.oldLocalDescription); + const filteredNewDesc = filterMsidFromRecvonlySources(diffObject.newLocalDescription); + this._log('info', 'sending source remove', filteredDesc); + this._log('info', 'sending source add', filteredNewDesc); this.send('source-remove', filteredDesc); this.send('source-add', filteredNewDesc); } - if (diffObject.sourcesToAddBack.length) { - const new_desc = diffObject.newLocalDescription; - const jmContent = getProperSSRCS(diffObject.oldContents, diffObject.sourcesToAddBack); - const jmContent2 = getProperSSRCS(diffObject.newContents, diffObject.sourcesToAddBack); - - console.log('jmContents: ', jmContent, jmContent2); - desc.contents = jmContent; - new_desc.contents = jmContent2; - delete desc.groups; - delete new_desc.groups; - - new_desc.contents.forEach(function(content) { - content.application.sources.forEach(function(source) { - source.parameters = source.parameters.filter(function(param) { - if (param.key === 'msid') { - return false; - } - return true; - }); - }); - }); - - console.log('in sources to add back sending source remove ', desc ); - console.log('in sources to add back sending source add ', new_desc ); - - // desc.contents = sourcesToAddBack; - this.send('source-remove', desc); - this.send('source-add', new_desc); - } }, switchStream: function (oldStream, newStream, cb) { @@ -935,7 +822,7 @@ MediaSession.prototype = extend(MediaSession.prototype, { delete content.application.headerExtensions; }) - const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription, true); + const newSsrcs = self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription, true); return cb(); }); }, @@ -1004,14 +891,13 @@ MediaSession.prototype = extend(MediaSession.prototype, { }); var errorMsg = 'removing stream source'; - console.log('wh in onSourceRemove triggering queueOfferAnswer'); var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); queueOfferAnswer(this, errorMsg, newDesc, function(err) { if (err) { return cb({condition: 'general-error'}); } var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription)); - const newSsrcs = self._doShit(oldLocalDescription, newLocalDescription); + const newSsrcs = self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription); return cb(); });