Bug 1208371 - Make it possible to look up stream id by track in PeerConnectionImpl. r?jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Fri, 15 Jan 2016 12:48:51 +0800
changeset 342115 1d84aea4dfc5986fe319a00e8b6dfdde58495151
parent 342114 206354fbd858b7c1ab510a948a44d5ba4d806c69
child 342116 67770eae8a882d170a56d2906b8a3b8907a1ca1e
push id13352
push userpehrsons@gmail.com
push dateFri, 18 Mar 2016 13:49:47 +0000
reviewersjib
bugs1208371
milestone47.0a1
Bug 1208371 - Make it possible to look up stream id by track in PeerConnectionImpl. r?jib This attempts to get rid of uses of MediaStreamTrack::GetStream() in PeerConnectionImpl but does unfortunately not go all the way. There's still a use case in ReplaceTrack() so we handle it for now by making PeerConnectionImpl a friend of MediaStreamTrack. MozReview-Commit-ID: K4sKjWqjOSI
dom/media/MediaStreamTrack.h
media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.h
media/webrtc/signaling/test/FakeMediaStreams.h
--- a/dom/media/MediaStreamTrack.h
+++ b/dom/media/MediaStreamTrack.h
@@ -15,16 +15,17 @@
 
 namespace mozilla {
 
 class DOMMediaStream;
 class MediaEnginePhotoCallback;
 class MediaStream;
 class MediaStreamGraph;
 class MediaStreamTrackListener;
+class PeerConnectionImpl;
 class ProcessedMediaStream;
 
 namespace dom {
 
 class AudioStreamTrack;
 class VideoStreamTrack;
 
 /**
@@ -164,16 +165,19 @@ 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.
+  friend class mozilla::PeerConnectionImpl;
+
 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);
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.cpp
@@ -2310,33 +2310,24 @@ PeerConnectionImpl::SelectSsrc(MediaStre
   }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 PeerConnectionImpl::RemoveTrack(MediaStreamTrack& aTrack) {
   PC_AUTO_ENTER_API_CALL(true);
 
-  DOMMediaStream* stream = aTrack.GetStream();
-
-  if (!stream) {
-    CSFLogError(logTag, "%s: Track has no stream", __FUNCTION__);
-    return NS_ERROR_INVALID_ARG;
-  }
-
-  std::string streamId = PeerConnectionImpl::GetStreamId(*stream);
-  RefPtr<LocalSourceStreamInfo> info = media()->GetLocalStreamById(streamId);
+  std::string trackId = PeerConnectionImpl::GetTrackId(aTrack);
+  RefPtr<LocalSourceStreamInfo> info = media()->GetLocalStreamByTrackId(trackId);
 
   if (!info) {
     CSFLogError(logTag, "%s: Unknown stream", __FUNCTION__);
     return NS_ERROR_INVALID_ARG;
   }
 
-  std::string trackId(PeerConnectionImpl::GetTrackId(aTrack));
-
   nsresult rv =
     mJsepSession->RemoveTrack(info->GetId(), trackId);
 
   if (NS_FAILED(rv)) {
     CSFLogError(logTag, "%s: Unknown stream/track ids %s %s",
                 __FUNCTION__,
                 info->GetId().c_str(),
                 trackId.c_str());
@@ -2385,20 +2376,26 @@ PeerConnectionImpl::ReplaceTrack(MediaSt
       return NS_ERROR_UNEXPECTED;
     }
     return NS_OK;
   }
 #endif
   std::string origTrackId = PeerConnectionImpl::GetTrackId(aThisTrack);
   std::string newTrackId = PeerConnectionImpl::GetTrackId(aWithTrack);
 
-  std::string origStreamId =
-    PeerConnectionImpl::GetStreamId(*aThisTrack.GetStream());
+  RefPtr<LocalSourceStreamInfo> info =
+    media()->GetLocalStreamByTrackId(origTrackId);
+  if (!info) {
+    CSFLogError(logTag, "Could not find stream from trackId");
+    return NS_ERROR_UNEXPECTED;
+  }
+
+  std::string origStreamId = info->GetId();
   std::string newStreamId =
-    PeerConnectionImpl::GetStreamId(*aWithTrack.GetStream());
+    PeerConnectionImpl::GetStreamId(*aWithTrack.mOwningStream);
 
   nsresult rv = mJsepSession->ReplaceTrack(origStreamId,
                                            origTrackId,
                                            newStreamId,
                                            newTrackId);
   if (NS_FAILED(rv)) {
     pco->OnReplaceTrackError(kInvalidMediastreamTrack,
                              ObString(mJsepSession->GetLastError().c_str()),
@@ -2407,17 +2404,17 @@ PeerConnectionImpl::ReplaceTrack(MediaSt
       CSFLogError(logTag, "Error firing replaceTrack error callback");
       return NS_ERROR_UNEXPECTED;
     }
     return NS_OK;
   }
 
   rv = media()->ReplaceTrack(origStreamId,
                              origTrackId,
-                             aWithTrack.GetStream(),
+                             aWithTrack.mOwningStream,
                              newStreamId,
                              newTrackId);
 
   if (NS_FAILED(rv)) {
     CSFLogError(logTag, "Unexpected error in ReplaceTrack: %d",
                         static_cast<int>(rv));
     pco->OnReplaceTrackError(kInvalidMediastreamTrack,
                              ObString("Failed to replace track"),
@@ -2462,17 +2459,22 @@ PeerConnectionImpl::SetParameters(MediaS
 #endif
 
 nsresult
 PeerConnectionImpl::SetParameters(
     MediaStreamTrack& aTrack,
     const std::vector<JsepTrack::JsConstraints>& aConstraints)
 {
   std::string trackId = PeerConnectionImpl::GetTrackId(aTrack);
-  std::string streamId = PeerConnectionImpl::GetStreamId(*aTrack.GetStream());
+  RefPtr<LocalSourceStreamInfo> info = media()->GetLocalStreamByTrackId(trackId);
+  if (!info) {
+    CSFLogError(logTag, "%s: Unknown stream", __FUNCTION__);
+    return NS_ERROR_INVALID_ARG;
+  }
+  std::string streamId = info->GetId();
 
   return mJsepSession->SetParameters(streamId, trackId, aConstraints);
 }
 
 #if !defined(MOZILLA_EXTERNAL_LINKAGE)
 NS_IMETHODIMP
 PeerConnectionImpl::GetParameters(MediaStreamTrack& aTrack,
                                   RTCRtpParameters& aOutParameters) {
@@ -2496,17 +2498,22 @@ PeerConnectionImpl::GetParameters(MediaS
 #endif
 
 nsresult
 PeerConnectionImpl::GetParameters(
     MediaStreamTrack& aTrack,
     std::vector<JsepTrack::JsConstraints>* aOutConstraints)
 {
   std::string trackId = PeerConnectionImpl::GetTrackId(aTrack);
-  std::string streamId = PeerConnectionImpl::GetStreamId(*aTrack.GetStream());
+  RefPtr<LocalSourceStreamInfo> info = media()->GetLocalStreamByTrackId(trackId);
+  if (!info) {
+    CSFLogError(logTag, "%s: Unknown stream", __FUNCTION__);
+    return NS_ERROR_INVALID_ARG;
+  }
+  std::string streamId = info->GetId();
 
   return mJsepSession->GetParameters(streamId, trackId, aOutConstraints);
 }
 
 nsresult
 PeerConnectionImpl::CalculateFingerprint(
     const std::string& algorithm,
     std::vector<uint8_t>* fingerprint) const {
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
@@ -856,16 +856,29 @@ PeerConnectionMedia::GetLocalStreamById(
     if (id == mLocalSourceStreams[i]->GetId()) {
       return mLocalSourceStreams[i];
     }
   }
 
   return nullptr;
 }
 
+LocalSourceStreamInfo*
+PeerConnectionMedia::GetLocalStreamByTrackId(const std::string& id)
+{
+  ASSERT_ON_THREAD(mMainThread);
+  for (RefPtr<LocalSourceStreamInfo>& info : mLocalSourceStreams) {
+    if (info->HasTrack(id)) {
+      return info;
+    }
+  }
+
+  return nullptr;
+}
+
 RemoteSourceStreamInfo*
 PeerConnectionMedia::GetRemoteStreamByIndex(size_t aIndex)
 {
   ASSERT_ON_THREAD(mMainThread);
   MOZ_ASSERT(mRemoteSourceStreams.SafeElementAt(aIndex));
   return mRemoteSourceStreams.SafeElementAt(aIndex);
 }
 
@@ -877,16 +890,30 @@ PeerConnectionMedia::GetRemoteStreamById
     if (id == mRemoteSourceStreams[i]->GetId()) {
       return mRemoteSourceStreams[i];
     }
   }
 
   return nullptr;
 }
 
+RemoteSourceStreamInfo*
+PeerConnectionMedia::GetRemoteStreamByTrackId(const std::string& id)
+{
+  ASSERT_ON_THREAD(mMainThread);
+  for (RefPtr<RemoteSourceStreamInfo>& info : mRemoteSourceStreams) {
+    if (info->HasTrack(id)) {
+      return info;
+    }
+  }
+
+  return nullptr;
+}
+
+
 nsresult
 PeerConnectionMedia::AddRemoteStream(RefPtr<RemoteSourceStreamInfo> aInfo)
 {
   ASSERT_ON_THREAD(mMainThread);
 
   mRemoteSourceStreams.AppendElement(aInfo);
 
   return NS_OK;
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.h
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.h
@@ -320,25 +320,27 @@ class PeerConnectionMedia : public sigsl
 
   // Get a specific local stream
   uint32_t LocalStreamsLength()
   {
     return mLocalSourceStreams.Length();
   }
   LocalSourceStreamInfo* GetLocalStreamByIndex(int index);
   LocalSourceStreamInfo* GetLocalStreamById(const std::string& id);
+  LocalSourceStreamInfo* GetLocalStreamByTrackId(const std::string& id);
 
   // Get a specific remote stream
   uint32_t RemoteStreamsLength()
   {
     return mRemoteSourceStreams.Length();
   }
 
   RemoteSourceStreamInfo* GetRemoteStreamByIndex(size_t index);
   RemoteSourceStreamInfo* GetRemoteStreamById(const std::string& id);
+  RemoteSourceStreamInfo* GetRemoteStreamByTrackId(const std::string& id);
 
   // Add a remote stream.
   nsresult AddRemoteStream(RefPtr<RemoteSourceStreamInfo> aInfo);
 
   nsresult ReplaceTrack(const std::string& oldStreamId,
                         const std::string& oldTrackId,
                         DOMMediaStream* aNewStream,
                         const std::string& newStreamId,
--- a/media/webrtc/signaling/test/FakeMediaStreams.h
+++ b/media/webrtc/signaling/test/FakeMediaStreams.h
@@ -26,16 +26,17 @@
 #include "nsIRunnable.h"
 #include "nsISupportsImpl.h"
 
 class nsPIDOMWindowInner;
 
 namespace mozilla {
    class MediaStreamGraphImpl;
    class MediaSegment;
+   class PeerConnectionImpl;
 };
 
 
 namespace mozilla {
 
 class MediaStreamGraph;
 
 static MediaStreamGraph* gGraph;
@@ -291,33 +292,33 @@ class Fake_MediaStreamTrackSource
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Fake_MediaStreamTrackSource)
 
 protected:
   virtual ~Fake_MediaStreamTrackSource() {}
 };
 
 class Fake_MediaStreamTrack
 {
+  friend class mozilla::PeerConnectionImpl;
 public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Fake_MediaStreamTrack)
 
-  Fake_MediaStreamTrack(bool aIsVideo, Fake_DOMMediaStream* aStream) :
+  Fake_MediaStreamTrack(bool aIsVideo, Fake_DOMMediaStream* aOwningStream) :
     mIsVideo (aIsVideo),
-    mStream (aStream)
+    mOwningStream (aOwningStream)
   {
     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; }
-  Fake_DOMMediaStream *GetStream() { return mStream; }
   const Fake_MediaStreamTrack* AsVideoStreamTrack() const
   {
     return mIsVideo? this : nullptr;
   }
   const Fake_MediaStreamTrack* AsAudioStreamTrack() const
   {
     return mIsVideo? nullptr : this;
   }
@@ -328,17 +329,19 @@ public:
   const char* typeName () const
   {
     return "Fake_MediaStreamTrack";
   }
 private:
   ~Fake_MediaStreamTrack() {}
 
   const bool mIsVideo;
-  Fake_DOMMediaStream* mStream;
+protected:
+  Fake_DOMMediaStream* mOwningStream;
+private:
   std::string mID;
 };
 
 class Fake_DOMMediaStream : public nsISupports
 {
 protected:
   virtual ~Fake_DOMMediaStream() {
     // Note: memory leak