Bug 1208371 - Remove MediaStreamTrack::GetStream. r?jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Mon, 01 Feb 2016 22:48:05 +0800
changeset 342117 9acdab4e4bb1c643b5e2f9dc7f18251e0e253c28
parent 342116 67770eae8a882d170a56d2906b8a3b8907a1ca1e
child 342118 52c37d3e6a4e629522034dc83a239f62cf888988
push id13352
push userpehrsons@gmail.com
push dateFri, 18 Mar 2016 13:49:47 +0000
reviewersjib
bugs1208371
milestone47.0a1
Bug 1208371 - Remove MediaStreamTrack::GetStream. r?jib MozReview-Commit-ID: GP0Mo3CZZ8E
dom/media/DOMMediaStream.cpp
dom/media/MediaStreamTrack.cpp
dom/media/MediaStreamTrack.h
dom/media/imagecapture/CaptureTask.h
dom/media/imagecapture/ImageCapture.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.h
media/webrtc/signaling/test/FakeMediaStreams.h
--- a/dom/media/DOMMediaStream.cpp
+++ b/dom/media/DOMMediaStream.cpp
@@ -521,17 +521,17 @@ DOMMediaStream::AddTrack(MediaStreamTrac
 
   RefPtr<ProcessedMediaStream> dest = mPlaybackStream->AsProcessedStream();
   MOZ_ASSERT(dest);
   if (!dest) {
     return;
   }
 
   LOG(LogLevel::Info, ("DOMMediaStream %p Adding track %p (from stream %p with ID %d)",
-                       this, &aTrack, aTrack.GetStream(), aTrack.GetTrackID()));
+                       this, &aTrack, aTrack.mOwningStream.get(), aTrack.mTrackID));
 
   if (mPlaybackStream->Graph() != aTrack.Graph()) {
     NS_ASSERTION(false, "Cannot combine tracks from different MediaStreamGraphs");
     LOG(LogLevel::Error, ("DOMMediaStream %p Own MSG %p != aTrack's MSG %p",
                          this, mPlaybackStream->Graph(), aTrack.Graph()));
 
     nsAutoString trackId;
     aTrack.GetId(trackId);
@@ -551,42 +551,42 @@ DOMMediaStream::AddTrack(MediaStreamTrac
     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.GetTrackID());
+                                           aTrack.mTrackID);
   RefPtr<TrackPort> trackPort =
     new TrackPort(inputPort, &aTrack, TrackPort::InputPortOwnership::OWNED);
   mTracks.AppendElement(trackPort.forget());
   NotifyTrackAdded(&aTrack);
 
   LOG(LogLevel::Debug, ("DOMMediaStream %p Added track %p", this, &aTrack));
 }
 
 void
 DOMMediaStream::RemoveTrack(MediaStreamTrack& aTrack)
 {
   LOG(LogLevel::Info, ("DOMMediaStream %p Removing track %p (from stream %p with ID %d)",
-                       this, &aTrack, aTrack.GetStream(), aTrack.GetTrackID()));
+                       this, &aTrack, aTrack.mOwningStream.get(), aTrack.mTrackID));
 
   RefPtr<TrackPort> toRemove = FindPlaybackTrackPort(aTrack);
   if (!toRemove) {
     LOG(LogLevel::Debug, ("DOMMediaStream %p does not contain track %p", this, &aTrack));
     return;
   }
 
   // If the track comes from a TRACK_ANY input port (i.e., mOwnedPort), we need
   // to block it in the port. Doing this for a locked track is still OK as it
   // will first block the track, then destroy the port. Both cause the track to
   // end.
-  toRemove->BlockTrackId(aTrack.GetTrackID());
+  toRemove->BlockTrackId(aTrack.mTrackID);
 
   DebugOnly<bool> removed = mTracks.RemoveElement(toRemove);
   MOZ_ASSERT(removed);
 
   NotifyTrackRemoved(&aTrack);
 
   LOG(LogLevel::Debug, ("DOMMediaStream %p Removed track %p", this, &aTrack));
 }
@@ -875,17 +875,17 @@ MediaStreamTrack*
 DOMMediaStream::FindOwnedDOMTrack(MediaStream* aInputStream,
                                   TrackID aInputTrackID) const
 {
   MOZ_RELEASE_ASSERT(mOwnedStream);
 
   for (const RefPtr<TrackPort>& info : mOwnedTracks) {
     if (info->GetInputPort() &&
         info->GetInputPort()->GetSource() == aInputStream &&
-        info->GetTrack()->GetInputTrackID() == aInputTrackID) {
+        info->GetTrack()->mInputTrackID == aInputTrackID) {
       // This track is owned externally but in our playback stream.
       return info->GetTrack();
     }
   }
   return nullptr;
 }
 
 DOMMediaStream::TrackPort*
@@ -903,17 +903,17 @@ DOMMediaStream::FindOwnedTrackPort(const
 MediaStreamTrack*
 DOMMediaStream::FindPlaybackDOMTrack(MediaStream* aInputStream, TrackID aInputTrackID) const
 {
   MOZ_RELEASE_ASSERT(mPlaybackStream);
 
   for (const RefPtr<TrackPort>& info : mTracks) {
     if (info->GetInputPort() == mPlaybackPort &&
         aInputStream == mOwnedStream &&
-        info->GetTrack()->GetInputTrackID() == aInputTrackID) {
+        info->GetTrack()->mInputTrackID == aInputTrackID) {
       // This track is in our owned and playback streams.
       return info->GetTrack();
     }
     if (info->GetInputPort() &&
         info->GetInputPort()->GetSource() == aInputStream &&
         info->GetSourceTrackId() == aInputTrackID) {
       // This track is owned externally but in our playback stream.
       MOZ_ASSERT(IsTrackIDExplicit(aInputTrackID));
--- a/dom/media/MediaStreamTrack.cpp
+++ b/dom/media/MediaStreamTrack.cpp
@@ -79,16 +79,23 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOriginalTrack)
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_IMPL_ADDREF_INHERITED(MediaStreamTrack, DOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(MediaStreamTrack, DOMEventTargetHelper)
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaStreamTrack)
 NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
 
+nsPIDOMWindowInner*
+MediaStreamTrack::GetParentObject() const
+{
+  MOZ_RELEASE_ASSERT(mOwningStream);
+  return mOwningStream->GetParentObject();
+}
+
 void
 MediaStreamTrack::GetId(nsAString& aID) const
 {
   aID = mID;
 }
 
 void
 MediaStreamTrack::SetEnabled(bool aEnabled)
@@ -132,17 +139,17 @@ MediaStreamTrack::ApplyConstraints(const
   if (MOZ_LOG_TEST(gMediaStreamTrackLog, LogLevel::Info)) {
     nsString str;
     aConstraints.ToJSON(str);
 
     LOG(LogLevel::Info, ("MediaStreamTrack %p ApplyConstraints() with "
                          "constraints %s", this, NS_ConvertUTF16toUTF8(str).get()));
   }
 
-  return GetStream()->ApplyConstraintsToTrack(mTrackID, aConstraints, aRv);
+  return mOwningStream->ApplyConstraintsToTrack(mTrackID, aConstraints, aRv);
 }
 
 MediaStreamGraph*
 MediaStreamTrack::Graph()
 {
   return GetOwnedStream()->Graph();
 }
 
@@ -175,32 +182,32 @@ MediaStreamTrack::RemovePrincipalChangeO
   return mPrincipalChangeObservers.RemoveElement(aObserver);
 }
 
 DOMMediaStream*
 MediaStreamTrack::GetInputDOMStream()
 {
   MediaStreamTrack* originalTrack =
     mOriginalTrack ? mOriginalTrack.get() : this;
-  MOZ_RELEASE_ASSERT(originalTrack->GetStream());
-  return originalTrack->GetStream();
+  MOZ_RELEASE_ASSERT(originalTrack->mOwningStream);
+  return originalTrack->mOwningStream;
 }
 
 MediaStream*
 MediaStreamTrack::GetInputStream()
 {
   DOMMediaStream* inputDOMStream = GetInputDOMStream();
   MOZ_RELEASE_ASSERT(inputDOMStream->GetInputStream());
   return inputDOMStream->GetInputStream();
 }
 
 ProcessedMediaStream*
 MediaStreamTrack::GetOwnedStream()
 {
-  return GetStream()->GetOwnedStream();
+  return mOwningStream->GetOwnedStream();
 }
 
 void
 MediaStreamTrack::AddListener(MediaStreamTrackListener* aListener)
 {
   LOG(LogLevel::Debug, ("MediaStreamTrack %p adding listener %p",
                         this, aListener));
 
--- a/dom/media/MediaStreamTrack.h
+++ b/dom/media/MediaStreamTrack.h
@@ -17,16 +17,17 @@ namespace mozilla {
 
 class DOMMediaStream;
 class MediaEnginePhotoCallback;
 class MediaStream;
 class MediaStreamGraph;
 class MediaStreamTrackListener;
 class PeerConnectionImpl;
 class ProcessedMediaStream;
+class RemoteSourceStreamInfo;
 
 namespace dom {
 
 class AudioStreamTrack;
 class VideoStreamTrack;
 
 /**
  * Common interface through which a MediaStreamTrack can communicate with its
@@ -165,51 +166,36 @@ protected:
  */
 class MediaStreamTrack : public DOMEventTargetHelper,
                          public MediaStreamTrackSource::Sink
 {
   // DOMMediaStream owns MediaStreamTrack instances, and requires access to
   // some internal state, e.g., GetInputStream(), GetOwnedStream().
   friend class mozilla::DOMMediaStream;
 
-  // PeerConnectionImpl.cpp needs to know our owning DOMStream and bound track id.
+  // PeerConnection and friends need to know our owning DOMStream and track id.
   friend class mozilla::PeerConnectionImpl;
+  friend class mozilla::RemoteSourceStreamInfo;
 
 public:
   /**
    * aTrackID is the MediaStreamGraph track ID for the track in the
    * MediaStream owned by aStream.
    */
   MediaStreamTrack(DOMMediaStream* aStream, TrackID aTrackID,
                    TrackID aInputTrackID, const nsString& aLabel,
                    MediaStreamTrackSource* aSource);
 
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaStreamTrack,
                                            DOMEventTargetHelper)
 
-  DOMMediaStream* GetParentObject() const { return mOwningStream; }
+  nsPIDOMWindowInner* GetParentObject() const;
   virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override = 0;
 
-  /**
-   * Returns the DOMMediaStream owning this track.
-   */
-  DOMMediaStream* GetStream() const { return mOwningStream; }
-
-  /**
-   * Returns the TrackID this stream has in its owning DOMMediaStream's Owned
-   * stream.
-   */
-  TrackID GetTrackID() const { return mTrackID; }
-
-  /**
-   * Returns the TrackID this MediaStreamTrack has in its input MSG-MediaStream.
-   */
-  TrackID GetInputTrackID() const { return mInputTrackID; }
-
   virtual AudioStreamTrack* AsAudioStreamTrack() { return nullptr; }
   virtual VideoStreamTrack* AsVideoStreamTrack() { return nullptr; }
 
   // WebIDL
   virtual void GetKind(nsAString& aKind) = 0;
   void GetId(nsAString& aID) const;
   void GetLabel(nsAString& aLabel) { aLabel.Assign(mLabel); }
   bool Enabled() { return mEnabled; }
--- a/dom/media/imagecapture/CaptureTask.h
+++ b/dom/media/imagecapture/CaptureTask.h
@@ -55,36 +55,33 @@ public:
   // It should be on main thread only.
   void AttachTrack();
 
   // Remove listeners from MediaStreamTrack and PrincipalChangeObserver.
   // It should be on main thread only.
   void DetachTrack();
 
   // CaptureTask should be created on main thread.
-  CaptureTask(dom::ImageCapture* aImageCapture, TrackID aTrackID)
+  explicit CaptureTask(dom::ImageCapture* aImageCapture)
     : mImageCapture(aImageCapture)
-    , mTrackID(aTrackID)
     , mImageGrabbedOrTrackEnd(false)
     , mPrincipalChanged(false) {}
 
 protected:
   virtual ~CaptureTask() {}
 
   // Post a runnable on main thread to end this task and call TaskComplete to post
   // error event to script. It is called off-main-thread.
   void PostTrackEndEvent();
 
   // The ImageCapture associates with this task. This reference count should not
   // change in this class unless it clears this reference after a blob or error
   // event to script.
   RefPtr<dom::ImageCapture> mImageCapture;
 
-  TrackID mTrackID;
-
   // True when an image is retrieved from MediaStreamGraph or MediaStreamGraph
   // sends a track finish, end, or removed event.
   bool mImageGrabbedOrTrackEnd;
 
   // True after MediaStreamTrack principal changes while waiting for a photo
   // to finish and we should raise a security error.
   bool mPrincipalChanged;
 };
--- a/dom/media/imagecapture/ImageCapture.cpp
+++ b/dom/media/imagecapture/ImageCapture.cpp
@@ -143,17 +143,17 @@ ImageCapture::TakePhoto(ErrorResult& aRe
   // Try if MediaEngine supports taking photo.
   nsresult rv = TakePhotoByMediaEngine();
 
   // It falls back to MediaStreamGraph image capture if MediaEngine doesn't
   // support TakePhoto().
   if (rv == NS_ERROR_NOT_IMPLEMENTED) {
     IC_LOG("MediaEngine doesn't support TakePhoto(), it falls back to MediaStreamGraph.");
     RefPtr<CaptureTask> task =
-      new CaptureTask(this, mVideoStreamTrack->GetTrackID());
+      new CaptureTask(this);
 
     // It adds itself into MediaStreamGraph, so ImageCapture doesn't need to hold
     // the reference.
     task->AttachTrack();
   }
 }
 
 nsresult
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
@@ -210,23 +210,22 @@ public:
 
     for (size_t i = 0; i < tracks.Length(); i++) {
       std::string trackId;
       // This is the first chance we get to set the string track id on this
       // track. It would be nice if we could specify this along with the numeric
       // track id from the start, but we're stuck doing this fixup after the
       // fact.
       nsresult rv = wrapper.impl()->GetRemoteTrackId(streamId,
-                                                     tracks[i]->GetTrackID(),
+                                                     *tracks[i],
                                                      &trackId);
 
       if (NS_FAILED(rv)) {
-        CSFLogError(logTag, "%s: Failed to get string track id for %u, rv = %u",
-                            __FUNCTION__,
-                            static_cast<unsigned>(tracks[i]->GetTrackID()),
+        CSFLogError(logTag, "%s: Failed to get string track id for %p, rv = %u",
+                            __FUNCTION__, tracks[i].get(),
                             static_cast<unsigned>(rv));
         MOZ_ASSERT(false);
         continue;
       }
 
       std::string origTrackId = PeerConnectionImpl::GetTrackId(*tracks[i]);
 
       if (origTrackId == trackId) {
@@ -2181,24 +2180,24 @@ PeerConnectionImpl::PrincipalChanged(DOM
     CSFLogInfo(logTag, "Can't update sink principal; document gone");
   }
 }
 #endif
 
 #if !defined(MOZILLA_EXTERNAL_LINKAGE)
 nsresult
 PeerConnectionImpl::GetRemoteTrackId(const std::string streamId,
-                                     TrackID numericTrackId,
+                                     const MediaStreamTrack& aTrack,
                                      std::string* trackId) const
 {
   if (IsClosed()) {
     return NS_ERROR_UNEXPECTED;
   }
 
-  return mMedia->GetRemoteTrackId(streamId, numericTrackId, trackId);
+  return mMedia->GetRemoteTrackId(streamId, aTrack, trackId);
 }
 #endif
 
 std::string
 PeerConnectionImpl::GetTrackId(const MediaStreamTrack& aTrack)
 {
 #if !defined(MOZILLA_EXTERNAL_LINKAGE)
   nsString wideTrackId;
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
@@ -649,17 +649,17 @@ public:
 
   static nsresult ExecuteStatsQuery_s(RTCStatsQuery *query);
 
   // for monitoring changes in stream ownership
   // PeerConnectionMedia can't do it because it doesn't know about principals
   virtual void PrincipalChanged(DOMMediaStream* aMediaStream) override;
 
   nsresult GetRemoteTrackId(const std::string streamId,
-                            TrackID numericTrackId,
+                            const dom::MediaStreamTrack& track,
                             std::string* trackId) const;
 #endif
 
   static std::string GetStreamId(const DOMMediaStream& aStream);
   static std::string GetTrackId(const dom::MediaStreamTrack& track);
 
   void OnMediaError(const std::string& aError);
 
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
@@ -744,29 +744,29 @@ PeerConnectionMedia::RemoveRemoteTrack(c
   if (!remoteSourceStream->GetTrackCount()) {
     mRemoteSourceStreams.RemoveElement(remoteSourceStream);
   }
   return NS_OK;
 }
 
 nsresult
 PeerConnectionMedia::GetRemoteTrackId(const std::string streamId,
-                                      TrackID numericTrackId,
+                                      const MediaStreamTrack& track,
                                       std::string* trackId) const
 {
   auto* ncThis = const_cast<PeerConnectionMedia*>(this);
   const RemoteSourceStreamInfo* info =
     ncThis->GetRemoteStreamById(streamId);
 
   if (!info) {
     CSFLogError(logTag, "%s: Could not find stream info", __FUNCTION__);
     return NS_ERROR_NOT_AVAILABLE;
   }
 
-  return info->GetTrackId(numericTrackId, trackId);
+  return info->GetTrackId(track, trackId);
 }
 
 void
 PeerConnectionMedia::SelfDestruct()
 {
   ASSERT_ON_THREAD(mMainThread);
 
   CSFLogDebug(logTag, "%s: ", __FUNCTION__);
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.h
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.h
@@ -213,18 +213,20 @@ class RemoteSourceStreamInfo : public So
     for (size_t i = 0; i < mTrackIdMap.size(); ++i) {
       if (mTrackIdMap[i] == trackId) {
         return static_cast<TrackID>(i + 1);
       }
     }
     return TRACK_INVALID;
   }
 
-  nsresult GetTrackId(TrackID numericTrackId, std::string* trackId) const
+  nsresult GetTrackId(const dom::MediaStreamTrack& track, std::string* trackId) const
   {
+    TrackID numericTrackId = track.mTrackID;
+
     if (numericTrackId <= 0 ||
         static_cast<size_t>(numericTrackId) > mTrackIdMap.size()) {
       return NS_ERROR_INVALID_ARG;;
     }
 
     *trackId = mTrackIdMap[numericTrackId - 1];
     return NS_OK;
   }
@@ -310,17 +312,17 @@ class PeerConnectionMedia : public sigsl
                     const std::string& trackId);
 
   nsresult RemoveLocalTrack(const std::string& streamId,
                             const std::string& trackId);
   nsresult RemoveRemoteTrack(const std::string& streamId,
                             const std::string& trackId);
 
   nsresult GetRemoteTrackId(const std::string streamId,
-                            TrackID numericTrackId,
+                            const dom::MediaStreamTrack& track,
                             std::string* trackId) const;
 
   // Get a specific local stream
   uint32_t LocalStreamsLength()
   {
     return mLocalSourceStreams.Length();
   }
   LocalSourceStreamInfo* GetLocalStreamByIndex(int index);
--- a/media/webrtc/signaling/test/FakeMediaStreams.h
+++ b/media/webrtc/signaling/test/FakeMediaStreams.h
@@ -27,16 +27,17 @@
 #include "nsISupportsImpl.h"
 
 class nsPIDOMWindowInner;
 
 namespace mozilla {
    class MediaStreamGraphImpl;
    class MediaSegment;
    class PeerConnectionImpl;
+   class RemoteSourceStreamInfo;
 };
 
 
 namespace mozilla {
 
 class MediaStreamGraph;
 
 static MediaStreamGraph* gGraph;
@@ -293,30 +294,31 @@ class Fake_MediaStreamTrackSource
 
 protected:
   virtual ~Fake_MediaStreamTrackSource() {}
 };
 
 class Fake_MediaStreamTrack
 {
   friend class mozilla::PeerConnectionImpl;
+  friend class mozilla::RemoteSourceStreamInfo;
 public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Fake_MediaStreamTrack)
 
   Fake_MediaStreamTrack(bool aIsVideo, Fake_DOMMediaStream* aOwningStream) :
     mIsVideo (aIsVideo),
-    mOwningStream (aOwningStream)
+    mOwningStream (aOwningStream),
+    mTrackID(mIsVideo ? 1 : 0)
   {
     static size_t counter = 0;
     std::ostringstream os;
     os << counter++;
     mID = os.str();
   }
 
-  mozilla::TrackID GetTrackID() { return mIsVideo ? 1 : 0; }
   std::string GetId() const { return mID; }
   void AssignId(const std::string& id) { mID = id; }
   const Fake_MediaStreamTrack* AsVideoStreamTrack() const
   {
     return mIsVideo? this : nullptr;
   }
   const Fake_MediaStreamTrack* AsAudioStreamTrack() const
   {
@@ -329,19 +331,18 @@ public:
   const char* typeName () const
   {
     return "Fake_MediaStreamTrack";
   }
 private:
   ~Fake_MediaStreamTrack() {}
 
   const bool mIsVideo;
-protected:
   Fake_DOMMediaStream* mOwningStream;
-private:
+  mozilla::TrackID mTrackID;
   std::string mID;
 };
 
 class Fake_DOMMediaStream : public nsISupports
 {
 protected:
   virtual ~Fake_DOMMediaStream() {
     // Note: memory leak
@@ -506,13 +507,14 @@ namespace mozilla {
 typedef Fake_MediaStream MediaStream;
 typedef Fake_SourceMediaStream SourceMediaStream;
 typedef Fake_MediaStreamListener MediaStreamListener;
 typedef Fake_MediaStreamDirectListener MediaStreamDirectListener;
 typedef Fake_DOMMediaStream DOMMediaStream;
 typedef Fake_DOMMediaStream DOMLocalMediaStream;
 
 namespace dom {
+typedef Fake_MediaStreamTrack MediaStreamTrack;
 typedef Fake_MediaStreamTrackSource MediaStreamTrackSource;
 }
 }
 
 #endif