Bug 1423241 - Move CanvasCaptureMediaStream MSG cleanup to MediaStreamTrackListener. r=padenot
authorAndreas Pehrson <apehrson@mozilla.com>
Fri, 23 Nov 2018 15:00:02 +0000
changeset 507034 24d6a327d6a2c8badd1082ab6791b217e7343ba4
parent 507033 6eda96338365abb919e615df7fb8a20d5261efaa
child 507035 acd70715684a851db640fae307c111cc6c6b1417
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 - Move CanvasCaptureMediaStream MSG cleanup to MediaStreamTrackListener. r=padenot Differential Revision: https://phabricator.services.mozilla.com/D12265
dom/media/CanvasCaptureMediaStream.cpp
dom/media/CanvasCaptureMediaStream.h
--- a/dom/media/CanvasCaptureMediaStream.cpp
+++ b/dom/media/CanvasCaptureMediaStream.cpp
@@ -19,19 +19,18 @@
 using namespace mozilla::layers;
 using namespace mozilla::gfx;
 
 namespace mozilla {
 namespace dom {
 
 class OutputStreamDriver::StreamListener : public MediaStreamListener {
  public:
-  explicit StreamListener(OutputStreamDriver* aDriver, TrackID aTrackId,
-                          PrincipalHandle aPrincipalHandle,
-                          SourceMediaStream* aSourceStream)
+  StreamListener(TrackID aTrackId, const PrincipalHandle& aPrincipalHandle,
+                 SourceMediaStream* aSourceStream)
       : mEnded(false),
         mSourceStream(aSourceStream),
         mTrackId(aTrackId),
         mPrincipalHandle(aPrincipalHandle),
         mMutex("CanvasCaptureMediaStream OutputStreamDriver::StreamListener") {
     MOZ_ASSERT(mSourceStream);
   }
 
@@ -62,25 +61,22 @@ class OutputStreamDriver::StreamListener
       mSourceStream->AppendToTrack(mTrackId, &segment);
     }
 
     if (mEnded) {
       mSourceStream->EndAllTrackAndFinish();
     }
   }
 
-  void NotifyEvent(MediaStreamGraph* aGraph,
-                   MediaStreamGraphEvent aEvent) override {
-    if (aEvent == MediaStreamGraphEvent::EVENT_REMOVED) {
-      EndStream();
-      mSourceStream->EndAllTrackAndFinish();
+  void Forget() {
+    EndStream();
+    mSourceStream->EndAllTrackAndFinish();
 
-      MutexAutoLock lock(mMutex);
-      mImage = nullptr;
-    }
+    MutexAutoLock lock(mMutex);
+    mImage = nullptr;
   }
 
  protected:
   ~StreamListener() {}
 
  private:
   Atomic<bool> mEnded;
   const RefPtr<SourceMediaStream> mSourceStream;
@@ -88,27 +84,43 @@ class OutputStreamDriver::StreamListener
   const PrincipalHandle mPrincipalHandle;
 
   Mutex mMutex;
   // The below members are protected by mMutex.
   RefPtr<layers::Image> mImage;
   TimeStamp mImageTime;
 };
 
+class OutputStreamDriver::TrackListener : public MediaStreamTrackListener {
+ public:
+  explicit TrackListener(const RefPtr<StreamListener>& aStreamListener)
+      : mStreamListener(aStreamListener) {}
+
+  void NotifyRemoved() override { mStreamListener->Forget(); }
+
+ protected:
+  ~TrackListener() = default;
+
+ private:
+  const RefPtr<StreamListener> mStreamListener;
+};
+
 OutputStreamDriver::OutputStreamDriver(SourceMediaStream* aSourceStream,
                                        const TrackID& aTrackId,
                                        const PrincipalHandle& aPrincipalHandle)
     : FrameCaptureListener(),
       mSourceStream(aSourceStream),
       mStreamListener(
-          new StreamListener(this, aTrackId, aPrincipalHandle, aSourceStream)) {
+          new StreamListener(aTrackId, aPrincipalHandle, aSourceStream)),
+      mTrackListener(new TrackListener(mStreamListener)) {
   MOZ_ASSERT(NS_IsMainThread());
   MOZ_ASSERT(mSourceStream);
   mSourceStream->AddListener(mStreamListener);
   mSourceStream->AddTrack(aTrackId, 0, new VideoSegment());
+  mSourceStream->AddTrackListener(mTrackListener, aTrackId);
   mSourceStream->AdvanceKnownTracksTime(STREAM_TIME_MAX);
   mSourceStream->SetPullEnabled(true);
 
   // All CanvasCaptureMediaStreams shall at least get one frame.
   mFrameCaptureRequested = true;
 }
 
 OutputStreamDriver::~OutputStreamDriver() {
--- a/dom/media/CanvasCaptureMediaStream.h
+++ b/dom/media/CanvasCaptureMediaStream.h
@@ -41,21 +41,21 @@ class OutputStreamFrameListener;
  *                                       ________________________
  *  ________   FrameCaptureRequested?   |                        |
  * |        | ------------------------> |   OutputStreamDriver   |
  * | Canvas |  SetFrameCapture()        | (FrameCaptureListener) |
  * |________| ------------------------> |________________________|
  *                                                  |
  *                                                  | SetImage()
  *                                                  v
- *                                         ___________________
- *                                        |   StreamListener  |
- * ---------------------------------------| (All image access |----------------
- *     === MediaStreamGraph Thread ===    |   Mutex Guarded)  |
- *                                        |___________________|
+ *                                         ____________________
+ *                                        |Stream/TrackListener|
+ * ---------------------------------------| (All image access  |---------------
+ *     === MediaStreamGraph Thread ===    |   Mutex Guarded)   |
+ *                                        |____________________|
  *                                              ^       |
  *                                 NotifyPull() |       | AppendToTrack()
  *                                              |       v
  *                                      ___________________________
  *                                     |                           |
  *                                     |  MSG / SourceMediaStream  |
  *                                     |___________________________|
  * ----------------------------------------------------------------------------
@@ -83,20 +83,22 @@ class OutputStreamDriver : public FrameC
    * Makes sure any internal resources this driver is holding that may create
    * reference cycles are released.
    */
   virtual void Forget() {}
 
  protected:
   virtual ~OutputStreamDriver();
   class StreamListener;
+  class TrackListener;
 
  private:
   RefPtr<SourceMediaStream> mSourceStream;
   RefPtr<StreamListener> mStreamListener;
+  RefPtr<TrackListener> mTrackListener;
 };
 
 class CanvasCaptureMediaStream : public DOMMediaStream {
  public:
   CanvasCaptureMediaStream(nsPIDOMWindowInner* aWindow,
                            HTMLCanvasElement* aCanvas);
 
   NS_DECL_ISUPPORTS_INHERITED