Bug 1423241 - Remove DOMMediaStream::PlaybackStreamListener. r=padenot
authorAndreas Pehrson <apehrson@mozilla.com>
Fri, 23 Nov 2018 15:02:48 +0000
changeset 507061 47958cf495da3a425773827f276ee8161d137cc6
parent 507060 d4767402cbb352274a930b0c800626c5e34b49ae
child 507062 929905c7782b6f0ba4cb57ade1cb1daa7429043e
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1423241
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 1423241 - Remove DOMMediaStream::PlaybackStreamListener. r=padenot Differential Revision: https://phabricator.services.mozilla.com/D12282
dom/media/DOMMediaStream.cpp
dom/media/DOMMediaStream.h
--- a/dom/media/DOMMediaStream.cpp
+++ b/dom/media/DOMMediaStream.cpp
@@ -99,59 +99,16 @@ already_AddRefed<Pledge<bool>> DOMMediaS
   rejected->Reject(NS_ERROR_FAILURE);
   return rejected.forget();
 }
 
 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)
 
-/**
- * Listener registered on the Playback stream to detect when tracks end and when
- * all new tracks this iteration have been created - for when several tracks are
- * queued by the source and committed all at once.
- */
-class DOMMediaStream::PlaybackStreamListener : public MediaStreamListener {
- public:
-  explicit PlaybackStreamListener(DOMMediaStream* aStream) : mStream(aStream) {}
-
-  void Forget() {
-    MOZ_ASSERT(NS_IsMainThread());
-    mStream = nullptr;
-  }
-
-  void DoNotifyFinishedTrackCreation() {
-    MOZ_ASSERT(NS_IsMainThread());
-
-    if (!mStream) {
-      return;
-    }
-
-    // The owned stream listener adds its tracks after another main thread
-    // dispatch. We have to do the same to notify of created tracks to stay
-    // in sync. (Or NotifyTracksCreated is called before tracks are added).
-    MOZ_ASSERT(mStream->GetPlaybackStream());
-    mStream->GetPlaybackStream()->Graph()->AbstractMainThread()->Dispatch(
-        NewRunnableMethod("DOMMediaStream::NotifyTracksCreated", mStream,
-                          &DOMMediaStream::NotifyTracksCreated));
-  }
-
-  // The methods below are called on the MediaStreamGraph thread.
-
-  void NotifyFinishedTrackCreation(MediaStreamGraph* aGraph) override {
-    aGraph->DispatchToMainThreadAfterStreamStateUpdate(NewRunnableMethod(
-        "DOMMediaStream::PlaybackStreamListener::DoNotifyFinishedTrackCreation",
-        this, &PlaybackStreamListener::DoNotifyFinishedTrackCreation));
-  }
-
- private:
-  // These fields may only be accessed on the main thread
-  DOMMediaStream* mStream;
-};
-
 class DOMMediaStream::PlaybackTrackListener : public MediaStreamTrackConsumer {
  public:
   explicit PlaybackTrackListener(DOMMediaStream* aStream) : mStream(aStream) {}
 
   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(PlaybackTrackListener)
   NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(PlaybackTrackListener)
 
   void NotifyEnded(MediaStreamTrack* aTrack) override {
@@ -249,23 +206,16 @@ DOMMediaStream::DOMMediaStream(nsPIDOMWi
     }
   }
 }
 
 DOMMediaStream::~DOMMediaStream() { Destroy(); }
 
 void DOMMediaStream::Destroy() {
   LOG(LogLevel::Debug, ("DOMMediaStream %p Being destroyed.", this));
-  if (mPlaybackListener) {
-    if (mPlaybackStream) {
-      mPlaybackStream->RemoveListener(mPlaybackListener);
-    }
-    mPlaybackListener->Forget();
-    mPlaybackListener = nullptr;
-  }
   for (const RefPtr<TrackPort>& info : mTracks) {
     // We must remove ourselves from each track's principal change observer list
     // before we die. CC may have cleared info->mTrack so guard against it.
     MediaStreamTrack* track = info->GetTrack();
     if (track) {
       track->RemovePrincipalChangeObserver(this);
       if (!track->Ended()) {
         track->RemoveConsumer(mPlaybackTrackListener);
@@ -287,17 +237,16 @@ void DOMMediaStream::Destroy() {
   if (mOwnedStream) {
     mOwnedStream->UnregisterUser();
     mOwnedStream = nullptr;
   }
   if (mInputStream) {
     mInputStream->UnregisterUser();
     mInputStream = nullptr;
   }
-  mRunOnTracksAvailable.Clear();
   mTrackListeners.Clear();
 }
 
 JSObject* DOMMediaStream::WrapObject(JSContext* aCx,
                                      JS::Handle<JSObject*> aGivenProto) {
   return dom::MediaStream_Binding::Wrap(aCx, this, aGivenProto);
 }
 
@@ -681,19 +630,16 @@ void DOMMediaStream::InitOwnedStreamComm
 void DOMMediaStream::InitPlaybackStreamCommon(MediaStreamGraph* aGraph) {
   mPlaybackStream = aGraph->CreateTrackUnionStream();
   mPlaybackStream->QueueSetAutofinish(true);
   mPlaybackStream->RegisterUser();
   if (mOwnedStream) {
     mPlaybackPort = mPlaybackStream->AllocateInputPort(mOwnedStream);
   }
 
-  mPlaybackListener = new PlaybackStreamListener(this);
-  mPlaybackStream->AddListener(mPlaybackListener);
-
   LOG(LogLevel::Debug, ("DOMMediaStream %p Initiated with mInputStream=%p, "
                         "mOwnedStream=%p, mPlaybackStream=%p",
                         this, mInputStream, mOwnedStream, mPlaybackStream));
 }
 
 already_AddRefed<DOMMediaStream> DOMMediaStream::CreateSourceStreamAsInput(
     nsPIDOMWindowInner* aWindow, MediaStreamGraph* aGraph) {
   auto stream = MakeRefPtr<DOMMediaStream>(aWindow);
@@ -948,31 +894,16 @@ MediaStreamTrack* DOMMediaStream::FindPl
   return nullptr;
 }
 
 DOMMediaStream::TrackPort* DOMMediaStream::FindPlaybackTrackPort(
     const MediaStreamTrack& aTrack) const {
   return FindTrackPortAmongTracks(aTrack, mTracks);
 }
 
-void DOMMediaStream::OnTracksAvailable(OnTracksAvailableCallback* aRunnable) {
-  if (mNotifiedOfMediaStreamGraphShutdown) {
-    // No more tracks will ever be added, so just delete the callback now.
-    delete aRunnable;
-    return;
-  }
-  mRunOnTracksAvailable.AppendElement(aRunnable);
-  CheckTracksAvailable();
-}
-
-void DOMMediaStream::NotifyTracksCreated() {
-  mTracksCreated = true;
-  CheckTracksAvailable();
-}
-
 void DOMMediaStream::NotifyActive() {
   LOG(LogLevel::Info, ("DOMMediaStream %p NotifyActive(). ", this));
 
   MOZ_ASSERT(mActive);
   for (int32_t i = mTrackListeners.Length() - 1; i >= 0; --i) {
     mTrackListeners[i]->NotifyActive();
   }
 }
@@ -981,28 +912,16 @@ void DOMMediaStream::NotifyInactive() {
   LOG(LogLevel::Info, ("DOMMediaStream %p NotifyInactive(). ", this));
 
   MOZ_ASSERT(!mActive);
   for (int32_t i = mTrackListeners.Length() - 1; i >= 0; --i) {
     mTrackListeners[i]->NotifyInactive();
   }
 }
 
-void DOMMediaStream::CheckTracksAvailable() {
-  if (!mTracksCreated) {
-    return;
-  }
-  nsTArray<nsAutoPtr<OnTracksAvailableCallback>> callbacks;
-  callbacks.SwapElements(mRunOnTracksAvailable);
-
-  for (uint32_t i = 0; i < callbacks.Length(); ++i) {
-    callbacks[i]->NotifyTracksAvailable(this);
-  }
-}
-
 void DOMMediaStream::RegisterTrackListener(TrackListener* aListener) {
   MOZ_ASSERT(NS_IsMainThread());
 
   if (mNotifiedOfMediaStreamGraphShutdown) {
     // No more tracks will ever be added, so just do nothing.
     return;
   }
   mTrackListeners.AppendElement(aListener);
--- a/dom/media/DOMMediaStream.h
+++ b/dom/media/DOMMediaStream.h
@@ -56,22 +56,16 @@ class Pledge;
 
 #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;
-};
-
 /**
 
 // clang-format off
  * DOM wrapper for MediaStreams.
  *
  * To account for track operations such as clone(), addTrack() and
  * removeTrack(), a DOMMediaStream wraps three internal (and chained)
  * MediaStreams:
@@ -487,26 +481,16 @@ class DOMMediaStream
   /**
    * Creates a MediaStreamTrack cloned from aTrack, adds it to mTracks and
    * returns it.
    * aCloneTrackID is the TrackID the new track will get in mOwnedStream.
    */
   already_AddRefed<MediaStreamTrack> CloneDOMTrack(MediaStreamTrack& aTrack,
                                                    TrackID aCloneTrackID);
 
-  // When the initial set of tracks has been added, run
-  // aCallback->NotifyTracksAvailable.
-  // It is allowed to do anything, including run script.
-  // aCallback may run immediately during this call if tracks are already
-  // available!
-  // We only care about track additions, we'll fire the notification even if
-  // some of the tracks have been removed.
-  // Takes ownership of aCallback.
-  void OnTracksAvailable(OnTracksAvailableCallback* aCallback);
-
   /**
    * Add an nsISupports object that this stream will keep alive as long as
    * the stream itself is alive.
    */
   void AddConsumerToKeepAlive(nsISupports* aConsumer) {
     mConsumersToKeepAlive.AppendElement(aConsumer);
   }
 
@@ -545,41 +529,32 @@ class DOMMediaStream
   void InitOwnedStreamCommon(MediaStreamGraph* aGraph);
 
   // Sets up a new TrackUnionStream as mPlaybackStream and connects it to
   // mOwnedStream with a TRACK_ANY MediaInputPort if available.
   // If this DOMMediaStream should have an owned stream (producer or clone),
   // it has to be initiated before the playback stream.
   void InitPlaybackStreamCommon(MediaStreamGraph* aGraph);
 
-  void CheckTracksAvailable();
-
-  // Called when MediaStreamGraph has finished an iteration where tracks were
-  // created.
-  void NotifyTracksCreated();
-
   // Dispatches NotifyActive() to all registered track listeners.
   void NotifyActive();
 
   // Dispatches NotifyInactive() to all registered track listeners.
   void NotifyInactive();
 
   // Dispatches NotifyTrackAdded() to all registered track listeners.
   void NotifyTrackAdded(const RefPtr<MediaStreamTrack>& aTrack);
 
   // Dispatches NotifyTrackRemoved() to all registered track listeners.
   void NotifyTrackRemoved(const RefPtr<MediaStreamTrack>& aTrack);
 
   // Dispatches "addtrack" or "removetrack".
   nsresult DispatchTrackEvent(const nsAString& aName,
                               const RefPtr<MediaStreamTrack>& aTrack);
 
-  class PlaybackStreamListener;
-  friend class PlaybackStreamListener;
-
   class PlaybackTrackListener;
   friend class PlaybackTrackListener;
 
   /**
    * Block a track in our playback stream. Calls NotifyPlaybackTrackBlocked()
    * after the MediaStreamGraph has applied the block and the track is no longer
    * live.
    */
@@ -627,25 +602,19 @@ class DOMMediaStream
 
   // MediaStreamTracks corresponding to tracks in our mPlaybackStream.
   AutoTArray<RefPtr<TrackPort>, 2> mTracks;
 
   // Number of MediaStreamTracks that have been removed on main thread but are
   // waiting to be removed on MediaStreamGraph thread.
   size_t mTracksPendingRemoval;
 
-  // Listener tracking changes to mPlaybackStream. This drives state changes
-  // in this DOMMediaStream and notifications to mTrackListeners.
-  RefPtr<PlaybackStreamListener> mPlaybackListener;
-
   // Listener tracking when live MediaStreamTracks in mTracks end.
   RefPtr<PlaybackTrackListener> mPlaybackTrackListener;
 
-  nsTArray<nsAutoPtr<OnTracksAvailableCallback>> mRunOnTracksAvailable;
-
   // Set to true after MediaStreamGraph has created tracks for mPlaybackStream.
   bool mTracksCreated;
 
   nsString mID;
 
   // Keep these alive while the stream is alive.
   nsTArray<nsCOMPtr<nsISupports>> mConsumersToKeepAlive;