Bug 1274221 - Make FindOwnedDOMTrack look for (optionally) unique tracks. r?ctai draft
authorAndreas Pehrson <pehrsons@gmail.com>
Tue, 24 May 2016 13:26:25 +0200
changeset 370266 78e184e8cbc312b14dad6d5e4a0b993df7bcc67d
parent 370265 c539999a334d1f357da0d85b189883dfdd5394b8
child 370267 e84b908b5f388a840e7e7e0258a2341c9a91790d
child 371404 71f978fccecbc5a7d2885666724fb366e9a13d13
push id19025
push userpehrsons@gmail.com
push dateTue, 24 May 2016 12:18:19 +0000
reviewersctai
bugs1274221
milestone49.0a1
Bug 1274221 - Make FindOwnedDOMTrack look for (optionally) unique tracks. r?ctai MozReview-Commit-ID: 1y8YuwJWIS5
dom/media/DOMMediaStream.cpp
dom/media/DOMMediaStream.h
--- a/dom/media/DOMMediaStream.cpp
+++ b/dom/media/DOMMediaStream.cpp
@@ -137,17 +137,17 @@ public:
   {
     MOZ_ASSERT(NS_IsMainThread());
 
     if (!mStream) {
       return;
     }
 
     MediaStreamTrack* track =
-      mStream->FindOwnedDOMTrack(aInputStream, aInputTrackID);
+      mStream->FindOwnedDOMTrack(aInputStream, aInputTrackID, aTrackID);
     if (!track) {
       // Track had not been created on main thread before, create it now.
       NS_WARN_IF_FALSE(!mStream->mTracks.IsEmpty(),
                        "A new track was detected on the input stream; creating "
                        "a corresponding MediaStreamTrack. Initial tracks "
                        "should be added manually to immediately and "
                        "synchronously be available to JS.");
       RefPtr<MediaStreamTrackSource> source;
@@ -160,26 +160,27 @@ public:
         nsIDocument* doc = window ? window->GetExtantDoc() : nullptr;
         nsIPrincipal* principal = doc ? doc->NodePrincipal() : nullptr;
         source = new BasicUnstoppableTrackSource(principal);
       }
       track = mStream->CreateDOMTrack(aTrackID, aType, source);
     }
   }
 
-  void DoNotifyTrackEnded(MediaStream* aInputStream, TrackID aInputTrackID)
+  void DoNotifyTrackEnded(MediaStream* aInputStream, TrackID aInputTrackID,
+                          TrackID aTrackID)
   {
     MOZ_ASSERT(NS_IsMainThread());
 
     if (!mStream) {
       return;
     }
 
     RefPtr<MediaStreamTrack> track =
-      mStream->FindOwnedDOMTrack(aInputStream, aInputTrackID);
+      mStream->FindOwnedDOMTrack(aInputStream, aInputTrackID, aTrackID);
     NS_ASSERTION(track, "Owned MediaStreamTracks must be known by the DOMMediaStream");
     if (track) {
       LOG(LogLevel::Debug, ("DOMMediaStream %p MediaStreamTrack %p ended at the source. Marking it ended.",
                             mStream, track.get()));
       track->NotifyEnded();
     }
   }
 
@@ -192,19 +193,19 @@ public:
     if (aTrackEvents & TRACK_EVENT_CREATED) {
       nsCOMPtr<nsIRunnable> runnable =
         NewRunnableMethod<TrackID, MediaSegment::Type, MediaStream*, TrackID>(
           this, &OwnedStreamListener::DoNotifyTrackCreated,
           aID, aQueuedMedia.GetType(), aInputStream, aInputTrackID);
       aGraph->DispatchToMainThreadAfterStreamStateUpdate(runnable.forget());
     } else if (aTrackEvents & TRACK_EVENT_ENDED) {
       nsCOMPtr<nsIRunnable> runnable =
-        NewRunnableMethod<MediaStream*, TrackID>(
+        NewRunnableMethod<MediaStream*, TrackID, TrackID>(
           this, &OwnedStreamListener::DoNotifyTrackEnded,
-          aInputStream, aInputTrackID);
+          aInputStream, aInputTrackID, aID);
       aGraph->DispatchToMainThreadAfterStreamStateUpdate(runnable.forget());
     }
   }
 
 private:
   // These fields may only be accessed on the main thread
   DOMMediaStream* mStream;
 };
@@ -1057,24 +1058,26 @@ FindTrackPortAmongTracks(const MediaStre
       return info;
     }
   }
   return nullptr;
 }
 
 MediaStreamTrack*
 DOMMediaStream::FindOwnedDOMTrack(MediaStream* aInputStream,
-                                  TrackID aInputTrackID) const
+                                  TrackID aInputTrackID,
+                                  TrackID aTrackID) const
 {
   MOZ_RELEASE_ASSERT(mOwnedStream);
 
   for (const RefPtr<TrackPort>& info : mOwnedTracks) {
     if (info->GetInputPort() &&
         info->GetInputPort()->GetSource() == aInputStream &&
-        info->GetTrack()->mInputTrackID == aInputTrackID) {
+        info->GetTrack()->mInputTrackID == aInputTrackID &&
+        (aTrackID == TRACK_ANY || info->GetTrack()->mTrackID == aTrackID)) {
       // This track is owned externally but in our playback stream.
       return info->GetTrack();
     }
   }
   return nullptr;
 }
 
 DOMMediaStream::TrackPort*
--- a/dom/media/DOMMediaStream.h
+++ b/dom/media/DOMMediaStream.h
@@ -385,20 +385,27 @@ public:
 
   /**
    * Returns true if this DOMMediaStream owns aTrack.
    */
   bool OwnsTrack(const MediaStreamTrack& aTrack) const;
 
   /**
    * Returns the corresponding MediaStreamTrack if it's in our mOwnedStream.
-   * aInputTrackID should match the track's TrackID in its input stream.
+   * aInputTrackID should match the track's TrackID in its input stream,
+   * and aTrackID the TrackID in mOwnedStream.
+   *
+   * When aTrackID is not supplied or set to TRACK_ANY, we return the first
+   * MediaStreamTrack that matches the given input track. Note that there may
+   * be multiple MediaStreamTracks matching the same input track, but that they
+   * in that case all share the same MediaStreamTrackSource.
    */
   MediaStreamTrack* FindOwnedDOMTrack(MediaStream* aInputStream,
-                                      TrackID aInputTrackID) const;
+                                      TrackID aInputTrackID,
+                                      TrackID aTrackID = TRACK_ANY) const;
 
   /**
    * Returns the TrackPort connecting aTrack's input stream to mOwnedStream,
    * or nullptr if aTrack is not owned by this DOMMediaStream.
    */
   TrackPort* FindOwnedTrackPort(const MediaStreamTrack& aTrack) const;
 
   /**