Bug 1208371 - Various cleanups in DOMMediaStream/MediaStreamTrack. r?jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Thu, 21 Jan 2016 19:05:53 +0800
changeset 342123 38455c4cd4e95d13b5df33e4b171afd165afa8a0
parent 342122 b660314897043e67ac94d1d3d97fb7ffe23fb8fd
child 342124 c87f87c359e64bbb6287597b5adfeba94b2e9de4
push id13352
push userpehrsons@gmail.com
push dateFri, 18 Mar 2016 13:49:47 +0000
reviewersjib
bugs1208371
milestone47.0a1
Bug 1208371 - Various cleanups in DOMMediaStream/MediaStreamTrack. r?jib MozReview-Commit-ID: DE0Gqto3Led
dom/media/DOMMediaStream.cpp
dom/media/DOMMediaStream.h
--- a/dom/media/DOMMediaStream.cpp
+++ b/dom/media/DOMMediaStream.cpp
@@ -325,17 +325,16 @@ NS_IMPL_RELEASE_INHERITED(DOMAudioNodeMe
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMAudioNodeMediaStream)
 NS_INTERFACE_MAP_END_INHERITING(DOMMediaStream)
 
 DOMMediaStream::DOMMediaStream(nsPIDOMWindowInner* aWindow,
                                MediaStreamTrackSourceGetter* aTrackSourceGetter)
   : mLogicalStreamStartTime(0), mWindow(aWindow),
     mInputStream(nullptr), mOwnedStream(nullptr), mPlaybackStream(nullptr),
-    mOwnedPort(nullptr), mPlaybackPort(nullptr),
     mTrackSourceGetter(aTrackSourceGetter), mTracksCreated(false),
     mNotifiedOfMediaStreamGraphShutdown(false), mCORSMode(CORS_NONE)
 {
   nsresult rv;
   nsCOMPtr<nsIUUIDGenerator> uuidgen =
     do_GetService("@mozilla.org/uuid-generator;1", &rv);
 
   if (NS_SUCCEEDED(rv) && uuidgen) {
@@ -545,17 +544,16 @@ DOMMediaStream::AddTrack(MediaStreamTrac
     return;
   }
 
   if (HasTrack(aTrack)) {
     LOG(LogLevel::Debug, ("DOMMediaStream %p already contains track %p", this, &aTrack));
     return;
   }
 
-
   // Hook up the underlying track with our underlying playback stream.
   RefPtr<MediaInputPort> inputPort =
     GetPlaybackStream()->AllocateInputPort(aTrack.GetOwnedStream(),
                                            aTrack.mTrackID);
   RefPtr<TrackPort> trackPort =
     new TrackPort(inputPort, &aTrack, TrackPort::InputPortOwnership::OWNED);
   mTracks.AppendElement(trackPort.forget());
   NotifyTrackAdded(&aTrack);
@@ -1001,16 +999,28 @@ DOMMediaStream::CreateClonedDOMTrack(Med
 
   NotifyTrackAdded(newTrack);
 
   newTrack->SetEnabled(aTrack.Enabled());
 
   return newTrack.forget();
 }
 
+static DOMMediaStream::TrackPort*
+FindTrackPortAmongTracks(const MediaStreamTrack& aTrack,
+                         const nsTArray<RefPtr<DOMMediaStream::TrackPort>>& aTracks)
+{
+  for (const RefPtr<DOMMediaStream::TrackPort>& info : aTracks) {
+    if (info->GetTrack() == &aTrack) {
+      return info;
+    }
+  }
+  return nullptr;
+}
+
 MediaStreamTrack*
 DOMMediaStream::FindOwnedDOMTrack(MediaStream* aInputStream,
                                   TrackID aInputTrackID) const
 {
   MOZ_RELEASE_ASSERT(mOwnedStream);
 
   for (const RefPtr<TrackPort>& info : mOwnedTracks) {
     if (info->GetInputPort() &&
@@ -1021,22 +1031,17 @@ DOMMediaStream::FindOwnedDOMTrack(MediaS
     }
   }
   return nullptr;
 }
 
 DOMMediaStream::TrackPort*
 DOMMediaStream::FindOwnedTrackPort(const MediaStreamTrack& aTrack) const
 {
-  for (const RefPtr<TrackPort>& info : mOwnedTracks) {
-    if (info->GetTrack() == &aTrack) {
-      return info;
-    }
-  }
-  return nullptr;
+  return FindTrackPortAmongTracks(aTrack, mOwnedTracks);
 }
 
 
 MediaStreamTrack*
 DOMMediaStream::FindPlaybackDOMTrack(MediaStream* aInputStream, TrackID aInputTrackID) const
 {
   if (!mPlaybackStream) {
     // One would think we can assert mPlaybackStream here, but track clones have
@@ -1060,22 +1065,17 @@ DOMMediaStream::FindPlaybackDOMTrack(Med
     }
   }
   return nullptr;
 }
 
 DOMMediaStream::TrackPort*
 DOMMediaStream::FindPlaybackTrackPort(const MediaStreamTrack& aTrack) const
 {
-  for (const RefPtr<TrackPort>& info : mTracks) {
-    if (info->GetTrack() == &aTrack) {
-      return info;
-    }
-  }
-  return nullptr;
+  return FindTrackPortAmongTracks(aTrack, mTracks);
 }
 
 void
 DOMMediaStream::NotifyMediaStreamGraphShutdown()
 {
   // No more tracks will ever be added, so just clear these callbacks now
   // to prevent leaks.
   mNotifiedOfMediaStreamGraphShutdown = true;
@@ -1139,34 +1139,32 @@ DOMMediaStream::RegisterTrackListener(Tr
 void
 DOMMediaStream::UnregisterTrackListener(TrackListener* aListener)
 {
   MOZ_ASSERT(NS_IsMainThread());
   mTrackListeners.RemoveElement(aListener);
 }
 
 void
-DOMMediaStream::NotifyTrackAdded(
-    const RefPtr<MediaStreamTrack>& aTrack)
+DOMMediaStream::NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   RecomputePrincipal();
 
   aTrack->AddPrincipalChangeObserver(this);
 
   for (int32_t i = mTrackListeners.Length() - 1; i >= 0; --i) {
     const RefPtr<TrackListener>& listener = mTrackListeners[i];
     listener->NotifyTrackAdded(aTrack);
   }
 }
 
 void
-DOMMediaStream::NotifyTrackRemoved(
-    const RefPtr<MediaStreamTrack>& aTrack)
+DOMMediaStream::NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   aTrack->RemovePrincipalChangeObserver(this);
 
   for (int32_t i = mTrackListeners.Length() - 1; i >= 0; --i) {
     const RefPtr<TrackListener>& listener = mTrackListeners[i];
     listener->NotifyTrackRemoved(aTrack);
--- a/dom/media/DOMMediaStream.h
+++ b/dom/media/DOMMediaStream.h
@@ -32,16 +32,17 @@
 
 namespace mozilla {
 
 class DOMHwMediaStream;
 class DOMLocalMediaStream;
 class DOMMediaStream;
 class MediaStream;
 class MediaInputPort;
+class MediaStreamDirectListener;
 class MediaStreamGraph;
 class ProcessedMediaStream;
 
 namespace dom {
 class AudioNode;
 class HTMLCanvasElement;
 class MediaStreamTrack;
 class MediaStreamTrackSource;
@@ -55,18 +56,16 @@ class MediaTrackListListener;
 struct MediaTrackConstraints;
 } // namespace dom
 
 namespace layers {
 class ImageContainer;
 class OverlayImage;
 } // namespace layers
 
-class MediaStreamDirectListener;
-
 #define NS_DOMMEDIASTREAM_IID \
 { 0x8cb65468, 0x66c0, 0x444e, \
   { 0x89, 0x9f, 0x89, 0x1d, 0x9e, 0xd2, 0xbe, 0x7c } }
 
 class OnTracksAvailableCallback {
 public:
   virtual ~OnTracksAvailableCallback() {}
   virtual void NotifyTracksAvailable(DOMMediaStream* aStream) = 0;
@@ -394,30 +393,30 @@ 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.
    */
-  MediaStreamTrack* FindOwnedDOMTrack(MediaStream* aOwningStream,
+  MediaStreamTrack* FindOwnedDOMTrack(MediaStream* aInputStream,
                                       TrackID aInputTrackID) 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;
 
   /**
    * Returns the corresponding MediaStreamTrack if it's in our mPlaybackStream.
    * aInputTrackID should match the track's TrackID in its owned stream.
    */
-  MediaStreamTrack* FindPlaybackDOMTrack(MediaStream* aOwningStream,
+  MediaStreamTrack* FindPlaybackDOMTrack(MediaStream* aInputStream,
                                          TrackID aInputTrackID) const;
 
   /**
    * Returns the TrackPort connecting mOwnedStream to mPlaybackStream for aTrack.
    */
   TrackPort* FindPlaybackTrackPort(const MediaStreamTrack& aTrack) const;
 
   MediaStream* GetInputStream() const { return mInputStream; }