Bug 1208373 - Implement MediaStreamTrack's "ended" event and onended EventHandler. r?smaug,jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Mon, 02 May 2016 15:48:39 +0200
changeset 363330 138292ebbe29687b6492e23f1f497f3dd5e15759
parent 363329 2460adefd6f543a0674f66d79a5eb241ebf15694
child 363331 8e6926c28e46eb7142e34fda3ca5f3abc8897f80
push id17172
push userpehrsons@gmail.com
push dateWed, 04 May 2016 14:35:55 +0000
reviewerssmaug, jib
bugs1208373
milestone49.0a1
Bug 1208373 - Implement MediaStreamTrack's "ended" event and onended EventHandler. r?smaug,jib MozReview-Commit-ID: DtqzY5nIdPI
dom/media/MediaStreamTrack.cpp
dom/media/MediaStreamTrack.h
dom/webidl/MediaStreamTrack.webidl
--- a/dom/media/MediaStreamTrack.cpp
+++ b/dom/media/MediaStreamTrack.cpp
@@ -335,16 +335,35 @@ MediaStreamTrack::Clone()
 
   MediaStreamGraph* graph = Graph();
   newStream->InitOwnedStreamCommon(graph);
   newStream->InitPlaybackStreamCommon(graph);
 
   return newStream->CloneDOMTrack(*this, mTrackID);
 }
 
+void
+MediaStreamTrack::NotifyEnded()
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  if (mEnded) {
+    return;
+  }
+
+  LOG(LogLevel::Info, ("MediaStreamTrack %p ended", this));
+
+  RefPtr<MediaStreamTrack> self = this;
+  NS_DispatchToMainThread(NewRunnableFrom([self]() {
+    MOZ_ALWAYS_SUCCEEDS(self->DispatchTrustedEvent(NS_LITERAL_STRING("ended")));
+    return NS_OK;
+  }));
+
+  mEnded = true;
+}
+
 DOMMediaStream*
 MediaStreamTrack::GetInputDOMStream()
 {
   MediaStreamTrack* originalTrack =
     mOriginalTrack ? mOriginalTrack.get() : this;
   MOZ_RELEASE_ASSERT(originalTrack->mOwningStream);
   return originalTrack->mOwningStream;
 }
--- a/dom/media/MediaStreamTrack.h
+++ b/dom/media/MediaStreamTrack.h
@@ -253,19 +253,21 @@ public:
   void GetLabel(nsAString& aLabel) { GetSource().GetLabel(aLabel); }
   bool Enabled() { return mEnabled; }
   void SetEnabled(bool aEnabled);
   void Stop();
   already_AddRefed<Promise>
   ApplyConstraints(const dom::MediaTrackConstraints& aConstraints, ErrorResult &aRv);
   already_AddRefed<MediaStreamTrack> Clone();
 
+  IMPL_EVENT_HANDLER(ended)
+
   bool Ended() const { return mEnded; }
   // Notifications from the MediaStreamGraph
-  void NotifyEnded() { mEnded = true; }
+  void NotifyEnded();
 
   /**
    * Get this track's principal.
    */
   nsIPrincipal* GetPrincipal() const { return mPrincipal; }
 
   /**
    * Called by the PrincipalHandleListener when this track's PrincipalHandle changes on
--- a/dom/webidl/MediaStreamTrack.webidl
+++ b/dom/webidl/MediaStreamTrack.webidl
@@ -70,17 +70,17 @@ interface MediaStreamTrack : EventTarget
     readonly    attribute DOMString             label;
                 attribute boolean               enabled;
 //  readonly    attribute boolean               muted;
 //              attribute EventHandler          onmute;
 //              attribute EventHandler          onunmute;
 //  readonly    attribute boolean               _readonly;
 //  readonly    attribute boolean               remote;
 //  readonly    attribute MediaStreamTrackState readyState;
-//                attribute EventHandler          onended;
+                attribute EventHandler          onended;
     MediaStreamTrack       clone ();
     void                   stop ();
 //  MediaTrackCapabilities getCapabilities ();
 //  MediaTrackConstraints  getConstraints ();
 //  MediaTrackSettings     getSettings ();
 
     [Throws]
     Promise<void>          applyConstraints (optional MediaTrackConstraints constraints);