Bug 1548097 - sort getContributingSources and getSynchronizationSources results r=jib
authorNico Grunbaum <na-g@nostrum.com>
Thu, 02 May 2019 19:57:43 +0000
changeset 531741 4d5a0aca43c2f8303078cdc003848ce4930c6b10
parent 531740 90e7019e0fa8e062f4b887d2802e203e4bd39300
child 531742 6ad2b2b28addd2d74dd85e21caac7e2cb631e776
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjib
bugs1548097
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1548097 - sort getContributingSources and getSynchronizationSources results r=jib sort getContributingSources and getSynchronizationSources results Differential Revision: https://phabricator.services.mozilla.com/D29441
dom/media/PeerConnection.jsm
dom/media/tests/mochitest/test_peerConnection_audioContributingSources.html
--- a/dom/media/PeerConnection.jsm
+++ b/dom/media/PeerConnection.jsm
@@ -2194,33 +2194,32 @@ class RTCRtpReceiver {
     for (let delKey of removeKeys) {
       this._rtpSources.delete(delKey);
     }
   }
 
   _getRtpSourcesByType(type) {
     this._fetchRtpSources();
     // Only return the values from within the last 10 seconds as per the spec
-    let cutoffTime = this._rtpSourcesJsTimestamp - 10 * 1000;
-    let sources = [...this._rtpSources.values()].filter(
+    const cutoffTime = this._rtpSourcesJsTimestamp - 10 * 1000;
+    return [...this._rtpSources.values()].filter(
       (entry) => {
         return entry.sourceType == type &&
             (entry.timestamp + entry.sourceClockOffset) >= cutoffTime;
       }).map(e => {
-        let newEntry = {
+        const newEntry = {
           source: e.source,
           timestamp: e.timestamp + e.sourceClockOffset,
           audioLevel: e.audioLevel,
         };
         if (e.voiceActivityFlag !== undefined) {
           Object.assign(newEntry, {voiceActivityFlag: e.voiceActivityFlag});
         }
         return newEntry;
-      });
-      return sources;
+      }).sort((a, b) => b.timestamp - a.timestamp);
   }
 
   getContributingSources() {
     return this._getRtpSourcesByType("contributing");
   }
 
   getSynchronizationSources() {
     return this._getRtpSourcesByType("synchronization");
--- a/dom/media/tests/mochitest/test_peerConnection_audioContributingSources.html
+++ b/dom/media/tests/mochitest/test_peerConnection_audioContributingSources.html
@@ -106,16 +106,34 @@
     let source2 = contributingSources.find(c => c.source == csrc2);
     ok(source2, "third csrc was found");
     is(source2.audioLevel, 0,
       `Contributing source has audio level of 0 when RTP audio level is 127`);
     // Check caching
     is(JSON.stringify(contributingSources),
        JSON.stringify(remoteReceiver.getContributingSources()),
        "getContributingSources is cached");
+    // Check that sources are sorted in descending order by time stamp
+    const timestamp3 = SpWrap(test.pcLocal).mozGetNowInRtpSourceReferenceTime();
+    // Larger offsets are further back in time
+    const testOffsets = [3, 7, 5, 6, 1, 4];
+    for (const offset of testOffsets) {
+      SpWrap(test.pcLocal).mozInsertAudioLevelForContributingSource(
+          localReceiver,
+          offset, // Using offset for SSRC for convenience
+          timestamp3 - offset,
+          true,
+          offset);
+    }
+    const sources = localReceiver.getContributingSources();
+    const sourceOffsets = sources.map(s => s.source);
+    is(JSON.stringify(sourceOffsets),
+       JSON.stringify([...testOffsets].sort((a, b) => a - b)),
+          `Contributing sources are sorted in descending order by timestamp:`
+          + ` ${JSON.stringify(sources)}`);
   };
 
   var test;
   runNetworkTest(function(options) {
     test = new PeerConnectionTest(options);
     test.chain.insertAfter("PC_REMOTE_WAIT_FOR_MEDIA_FLOW",
       [testGetContributingSources]);
     test.setMediaConstraints([{audio: true}], [{audio: true}]);