Bug 1497175 - Have DOMMediaStream::TrackPort::BlockSourceTrackId() return a promise. r=pehrsons
authorJan-Ivar Bruaroey <jib@mozilla.com>
Fri, 30 Nov 2018 05:14:40 +0000
changeset 505356 116803bacea74c7164b1cd9be3d10d3c185c09b7
parent 505355 a616c269bbccf381c45e6909417819103179a67a
child 505357 f4577848e2eeaa704731c9e3f3d69e7975b0bdfd
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspehrsons
bugs1497175
milestone65.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 1497175 - Have DOMMediaStream::TrackPort::BlockSourceTrackId() return a promise. r=pehrsons Differential Revision: https://phabricator.services.mozilla.com/D8010
dom/media/DOMMediaStream.cpp
dom/media/DOMMediaStream.h
dom/media/MediaStreamGraph.cpp
dom/media/MediaStreamGraph.h
dom/media/MediaStreamTrack.cpp
--- a/dom/media/DOMMediaStream.cpp
+++ b/dom/media/DOMMediaStream.cpp
@@ -85,24 +85,22 @@ void DOMMediaStream::TrackPort::DestroyI
 MediaStream* DOMMediaStream::TrackPort::GetSource() const {
   return mInputPort ? mInputPort->GetSource() : nullptr;
 }
 
 TrackID DOMMediaStream::TrackPort::GetSourceTrackId() const {
   return mInputPort ? mInputPort->GetSourceTrackId() : TRACK_INVALID;
 }
 
-already_AddRefed<Pledge<bool>> DOMMediaStream::TrackPort::BlockSourceTrackId(
+RefPtr<GenericPromise> DOMMediaStream::TrackPort::BlockSourceTrackId(
     TrackID aTrackId, BlockingMode aBlockingMode) {
-  if (mInputPort) {
-    return mInputPort->BlockSourceTrackId(aTrackId, aBlockingMode);
+  if (!mInputPort) {
+    return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
   }
-  auto rejected = MakeRefPtr<Pledge<bool>>();
-  rejected->Reject(NS_ERROR_FAILURE);
-  return rejected.forget();
+  return mInputPort->BlockSourceTrackId(aTrackId, aBlockingMode);
 }
 
 NS_IMPL_CYCLE_COLLECTION(DOMMediaStream::TrackPort, mTrack)
 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMMediaStream::TrackPort, AddRef)
 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMMediaStream::TrackPort, Release)
 
 class DOMMediaStream::PlaybackTrackListener : public MediaStreamTrackConsumer {
  public:
@@ -823,19 +821,18 @@ already_AddRefed<MediaStreamTrack> DOMMe
   newTrack->SetEnabled(aTrack.Enabled());
   newTrack->SetMuted(aTrack.Muted());
   newTrack->SetReadyState(aTrack.ReadyState());
 
   if (aTrack.Ended()) {
     // For extra suspenders, make sure that we don't forward data by mistake
     // to the clone when the original has already ended.
     // We only block END_EXISTING to allow any pending clones to end.
-    RefPtr<Pledge<bool, nsresult>> blockingPledge =
-        inputPort->BlockSourceTrackId(inputTrackID, BlockingMode::END_EXISTING);
-    Unused << blockingPledge;
+    Unused << inputPort->BlockSourceTrackId(inputTrackID,
+                                            BlockingMode::END_EXISTING);
   }
 
   return newTrack.forget();
 }
 
 static DOMMediaStream::TrackPort* FindTrackPortAmongTracks(
     const MediaStreamTrack& aTrack,
     const nsTArray<RefPtr<DOMMediaStream::TrackPort>>& aTracks) {
@@ -1037,23 +1034,24 @@ nsresult DOMMediaStream::DispatchTrackEv
       MediaStreamTrackEvent::Constructor(this, aName, init);
 
   return DispatchTrustedEvent(event);
 }
 
 void DOMMediaStream::BlockPlaybackTrack(TrackPort* aTrack) {
   MOZ_ASSERT(aTrack);
   ++mTracksPendingRemoval;
-  RefPtr<Pledge<bool>> p = aTrack->BlockSourceTrackId(
-      aTrack->GetTrack()->mTrackID, BlockingMode::CREATION);
-  RefPtr<DOMMediaStream> self = this;
-  p->Then([self](const bool& aIgnore) { self->NotifyPlaybackTrackBlocked(); },
-          [](const nsresult& aIgnore) {
-            NS_ERROR("Could not remove track from MSG");
-          });
+  RefPtr<DOMMediaStream> that = this;
+  aTrack
+      ->BlockSourceTrackId(aTrack->GetTrack()->mTrackID, BlockingMode::CREATION)
+      ->Then(GetCurrentThreadSerialEventTarget(), __func__,
+             [this, that](bool aIgnore) { NotifyPlaybackTrackBlocked(); },
+             [](const nsresult& aIgnore) {
+               NS_ERROR("Could not remove track from MSG");
+             });
 }
 
 void DOMMediaStream::NotifyPlaybackTrackBlocked() {
   MOZ_ASSERT(mTracksPendingRemoval > 0,
              "A track reported finished blocking more times than we asked for");
   if (--mTracksPendingRemoval == 0) {
     // The MediaStreamGraph has reported a track was blocked and we are not
     // waiting for any further tracks to get blocked. It is now safe to
--- a/dom/media/DOMMediaStream.h
+++ b/dom/media/DOMMediaStream.h
@@ -44,21 +44,16 @@ class VideoTrackList;
 class MediaTrackListListener;
 }  // namespace dom
 
 namespace layers {
 class ImageContainer;
 class OverlayImage;
 }  // namespace layers
 
-namespace media {
-template <typename V, typename E>
-class Pledge;
-}  // namespace media
-
 #define NS_DOMMEDIASTREAM_IID                        \
   {                                                  \
     0x8cb65468, 0x66c0, 0x444e, {                    \
       0x89, 0x9f, 0x89, 0x1d, 0x9e, 0xd2, 0xbe, 0x7c \
     }                                                \
   }
 
 /**
@@ -271,21 +266,21 @@ class DOMMediaStream
      */
     TrackID GetSourceTrackId() const;
 
     MediaInputPort* GetInputPort() const { return mInputPort; }
     MediaStreamTrack* GetTrack() const { return mTrack; }
 
     /**
      * Blocks aTrackId from going into mInputPort unless the port has been
-     * destroyed. Returns a pledge that gets resolved when the MediaStreamGraph
+     * destroyed. Returns a promise that gets resolved when the MediaStreamGraph
      * has applied the block in the playback stream.
      */
-    already_AddRefed<media::Pledge<bool, nsresult>> BlockSourceTrackId(
-        TrackID aTrackId, BlockingMode aBlockingMode);
+    RefPtr<GenericPromise> BlockSourceTrackId(TrackID aTrackId,
+                                              BlockingMode aBlockingMode);
 
    private:
     RefPtr<MediaInputPort> mInputPort;
     RefPtr<MediaStreamTrack> mTrack;
 
     // Defines if we've been given ownership of the input port or if it's owned
     // externally. The owner is responsible for destroying the port.
     const InputPortOwnership mOwnership;
--- a/dom/media/MediaStreamGraph.cpp
+++ b/dom/media/MediaStreamGraph.cpp
@@ -3006,17 +3006,17 @@ void MediaInputPort::SetGraphImpl(MediaS
 }
 
 void MediaInputPort::BlockSourceTrackIdImpl(TrackID aTrackId,
                                             BlockingMode aBlockingMode) {
   mBlockedTracks.AppendElement(
       Pair<TrackID, BlockingMode>(aTrackId, aBlockingMode));
 }
 
-already_AddRefed<Pledge<bool>> MediaInputPort::BlockSourceTrackId(
+RefPtr<GenericPromise> MediaInputPort::BlockSourceTrackId(
     TrackID aTrackId, BlockingMode aBlockingMode) {
   class Message : public ControlMessage {
    public:
     Message(MediaInputPort* aPort, TrackID aTrackId, BlockingMode aBlockingMode,
             already_AddRefed<nsIRunnable> aRunnable)
         : ControlMessage(aPort->GetDestination()),
           mPort(aPort),
           mTrackId(aTrackId),
@@ -3033,25 +3033,27 @@ already_AddRefed<Pledge<bool>> MediaInpu
     RefPtr<MediaInputPort> mPort;
     TrackID mTrackId;
     BlockingMode mBlockingMode;
     nsCOMPtr<nsIRunnable> mRunnable;
   };
 
   MOZ_ASSERT(IsTrackIDExplicit(aTrackId), "Only explicit TrackID is allowed");
 
-  auto pledge = MakeRefPtr<Pledge<bool>>();
-  nsCOMPtr<nsIRunnable> runnable = NewRunnableFrom([pledge]() {
-    MOZ_ASSERT(NS_IsMainThread());
-    pledge->Resolve(true);
-    return NS_OK;
-  });
+  MozPromiseHolder<GenericPromise> holder;
+  RefPtr<GenericPromise> p = holder.Ensure(__func__);
+  nsCOMPtr<nsIRunnable> runnable =
+      NewRunnableFrom([h = std::move(holder)]() mutable {
+        MOZ_ASSERT(NS_IsMainThread());
+        h.Resolve(true, __func__);
+        return NS_OK;
+      });
   GraphImpl()->AppendMessage(
       MakeUnique<Message>(this, aTrackId, aBlockingMode, runnable.forget()));
-  return pledge.forget();
+  return p;
 }
 
 already_AddRefed<MediaInputPort> ProcessedMediaStream::AllocateInputPort(
     MediaStream* aStream, TrackID aTrackID, TrackID aDestTrackID,
     uint16_t aInputNumber, uint16_t aOutputNumber,
     nsTArray<TrackID>* aBlockedTracks) {
   // This method creates two references to the MediaInputPort: one for
   // the main thread, and one for the MediaStreamGraph.
--- a/dom/media/MediaStreamGraph.h
+++ b/dom/media/MediaStreamGraph.h
@@ -44,21 +44,16 @@ class nsAutoRefTraits<SpeexResamplerStat
 namespace mozilla {
 
 extern LazyLogModule gMediaStreamGraphLog;
 
 namespace dom {
 enum class AudioContextOperation;
 }
 
-namespace media {
-template <typename V, typename E>
-class Pledge;
-}
-
 /*
  * MediaStreamGraph is a framework for synchronized audio/video processing
  * and playback. It is designed to be used by other browser components such as
  * HTML media elements, media capture APIs, real-time media streaming APIs,
  * multitrack media APIs, and advanced audio APIs.
  *
  * The MediaStreamGraph uses a dedicated thread to process media --- the media
  * graph thread. This ensures that we can process media through the graph
@@ -961,18 +956,18 @@ class MediaInputPort final {
   TrackID GetDestinationTrackId() const { return mDestTrack; }
 
   /**
    * Block aTrackId in the source stream from being passed through the port.
    * Consumers will interpret this track as ended.
    * Returns a pledge that resolves on the main thread after the track block has
    * been applied by the MSG.
    */
-  already_AddRefed<media::Pledge<bool, nsresult>> BlockSourceTrackId(
-      TrackID aTrackId, BlockingMode aBlockingMode);
+  RefPtr<GenericPromise> BlockSourceTrackId(TrackID aTrackId,
+                                            BlockingMode aBlockingMode);
 
  private:
   void BlockSourceTrackIdImpl(TrackID aTrackId, BlockingMode aBlockingMode);
 
  public:
   // Returns true if aTrackId has not been blocked for any reason and this port
   // has not been locked to another track.
   bool PassTrackThrough(TrackID aTrackId) const {
--- a/dom/media/MediaStreamTrack.cpp
+++ b/dom/media/MediaStreamTrack.cpp
@@ -303,19 +303,17 @@ void MediaStreamTrack::Stop() {
 
   mSource->UnregisterSink(mSink.get());
 
   MOZ_ASSERT(mOwningStream,
              "Every MediaStreamTrack needs an owning DOMMediaStream");
   DOMMediaStream::TrackPort* port = mOwningStream->FindOwnedTrackPort(*this);
   MOZ_ASSERT(port,
              "A MediaStreamTrack must exist in its owning DOMMediaStream");
-  RefPtr<Pledge<bool>> p =
-      port->BlockSourceTrackId(mInputTrackID, BlockingMode::CREATION);
-  Unused << p;
+  Unused << port->BlockSourceTrackId(mInputTrackID, BlockingMode::CREATION);
 
   mReadyState = MediaStreamTrackState::Ended;
 
   NotifyEnded();
 }
 
 void MediaStreamTrack::GetConstraints(dom::MediaTrackConstraints& aResult) {
   aResult = mConstraints;