Bug 1054166: Mirror Add/RemoveListener in Add/RemoveDirectListener r=roc a=abillings
authorRandell Jesup <rjesup@jesup.org>
Sun, 17 Aug 2014 02:09:21 -0400
changeset 208315 6a2810252cf8
parent 208314 8e09aad61a79
child 208316 9e94aa2f0ae7
push id3820
push userrjesup@wgate.com
push date2014-08-17 07:37 +0000
treeherdermozilla-beta@6a2810252cf8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersroc, abillings
bugs1054166
milestone32.0
Bug 1054166: Mirror Add/RemoveListener in Add/RemoveDirectListener r=roc a=abillings
content/media/MediaStreamGraph.cpp
content/media/MediaStreamGraph.h
--- a/content/media/MediaStreamGraph.cpp
+++ b/content/media/MediaStreamGraph.cpp
@@ -2374,49 +2374,71 @@ SourceMediaStream::NotifyDirectConsumers
   for (uint32_t j = 0; j < mDirectListeners.Length(); ++j) {
     MediaStreamDirectListener* l = mDirectListeners[j];
     TrackTicks offset = 0; // FIX! need a separate TrackTicks.... or the end of the internal buffer
     l->NotifyRealtimeData(static_cast<MediaStreamGraph*>(GraphImpl()), aTrack->mID, aTrack->mOutputRate,
                           offset, aTrack->mCommands, *aSegment);
   }
 }
 
+// These handle notifying all the listeners of an event
+void
+SourceMediaStream::NotifyListenersEventImpl(MediaStreamListener::MediaStreamGraphEvent aEvent)
+{
+  for (uint32_t j = 0; j < mListeners.Length(); ++j) {
+    MediaStreamListener* l = mListeners[j];
+    l->NotifyEvent(GraphImpl(), aEvent);
+  }
+}
+
+void
+SourceMediaStream::NotifyListenersEvent(MediaStreamListener::MediaStreamGraphEvent aNewEvent)
+{
+  class Message : public ControlMessage {
+  public:
+    Message(SourceMediaStream* aStream, MediaStreamListener::MediaStreamGraphEvent aEvent) :
+      ControlMessage(aStream), mEvent(aEvent) {}
+    virtual void Run()
+      {
+        mStream->AsSourceStream()->NotifyListenersEventImpl(mEvent);
+      }
+    MediaStreamListener::MediaStreamGraphEvent mEvent;
+  };
+  GraphImpl()->AppendMessage(new Message(this, aNewEvent));
+}
+
 void
 SourceMediaStream::AddDirectListener(MediaStreamDirectListener* aListener)
 {
   bool wasEmpty;
   {
     MutexAutoLock lock(mMutex);
     wasEmpty = mDirectListeners.IsEmpty();
     mDirectListeners.AppendElement(aListener);
   }
 
   if (wasEmpty) {
-    for (uint32_t j = 0; j < mListeners.Length(); ++j) {
-      MediaStreamListener* l = mListeners[j];
-      l->NotifyEvent(GraphImpl(), MediaStreamListener::EVENT_HAS_DIRECT_LISTENERS);
-    }
+    // Async
+    NotifyListenersEvent(MediaStreamListener::EVENT_HAS_DIRECT_LISTENERS);
   }
 }
 
 void
 SourceMediaStream::RemoveDirectListener(MediaStreamDirectListener* aListener)
 {
   bool isEmpty;
   {
     MutexAutoLock lock(mMutex);
     mDirectListeners.RemoveElement(aListener);
     isEmpty = mDirectListeners.IsEmpty();
   }
 
   if (isEmpty) {
-    for (uint32_t j = 0; j < mListeners.Length(); ++j) {
-      MediaStreamListener* l = mListeners[j];
-      l->NotifyEvent(GraphImpl(), MediaStreamListener::EVENT_HAS_NO_DIRECT_LISTENERS);
-    }
+    // Async
+    NotifyListenersEvent(MediaStreamListener::EVENT_HAS_NO_DIRECT_LISTENERS);
   }
 }
 
 bool
 SourceMediaStream::HaveEnoughBuffered(TrackID aID)
 {
   MutexAutoLock lock(mMutex);
   TrackData *track = FindDataForTrack(aID);
--- a/content/media/MediaStreamGraph.h
+++ b/content/media/MediaStreamGraph.h
@@ -687,16 +687,23 @@ public:
    * Enable or disable pulling. When pulling is enabled, NotifyPull
    * gets called on MediaStreamListeners for this stream during the
    * MediaStreamGraph control loop. Pulling is initially disabled.
    * Due to unavoidable race conditions, after a call to SetPullEnabled(false)
    * it is still possible for a NotifyPull to occur.
    */
   void SetPullEnabled(bool aEnabled);
 
+  /**
+   * These add/remove DirectListeners, which allow bypassing the graph and any
+   * synchronization delays for e.g. PeerConnection, which wants the data ASAP
+   * and lets the far-end handle sync and playout timing.
+   */
+  void NotifyListenersEventImpl(MediaStreamListener::MediaStreamGraphEvent aEvent);
+  void NotifyListenersEvent(MediaStreamListener::MediaStreamGraphEvent aEvent);
   void AddDirectListener(MediaStreamDirectListener* aListener);
   void RemoveDirectListener(MediaStreamDirectListener* aListener);
 
   /**
    * Add a new track to the stream starting at the given base time (which
    * must be greater than or equal to the last time passed to
    * AdvanceKnownTracksTime). Takes ownership of aSegment. aSegment should
    * contain data starting after aStart.