Bug 1208371 - Hook up MediaPipeline with PrincipalHandle. r?mt,bwc draft
authorAndreas Pehrson <pehrsons@gmail.com>
Thu, 03 Mar 2016 17:30:39 +0100
changeset 342174 a049665ffbf62f44964bd7e6bc0c45ca7668a195
parent 342173 5c80a236a26a1154431bf3c8998ce539cf00ea08
child 342175 092b59187d402e81da8007c1c569bb61da351a75
push id13352
push userpehrsons@gmail.com
push dateFri, 18 Mar 2016 13:49:47 +0000
reviewersmt, bwc
bugs1208371
milestone47.0a1
Bug 1208371 - Hook up MediaPipeline with PrincipalHandle. r?mt,bwc MozReview-Commit-ID: DLyLZu7kC3p
dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
media/webrtc/signaling/src/mediapipeline/MediaPipeline.h
media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
--- a/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
+++ b/dom/media/webrtc/MediaEngineRemoteVideoSource.cpp
@@ -282,21 +282,16 @@ MediaEngineRemoteVideoSource::NotifyPull
                                          const PrincipalHandle& aPrincipalHandle)
 {
   VideoSegment segment;
 
   MonitorAutoLock lock(mMonitor);
   StreamTime delta = aDesiredTime - aSource->GetEndOfAppendedData(aID);
 
   if (delta > 0) {
-    size_t i = mSources.IndexOf(aSource);
-    if (i == mSources.NoIndex) {
-      NS_ERROR("aSource not in mSources");
-      return;
-    }
     // nullptr images are allowed
     AppendToTrack(aSource, mImage, aID, delta, aPrincipalHandle);
   }
 }
 
 int
 MediaEngineRemoteVideoSource::FrameSizeChange(unsigned int w, unsigned int h,
                                               unsigned int streams)
--- a/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
+++ b/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
@@ -1400,16 +1400,45 @@ static void AddTrackAndListener(MediaStr
 }
 
 void GenericReceiveListener::AddSelf(MediaSegment* segment) {
   RefPtr<TrackAddedCallback> callback = new GenericReceiveCallback(this);
   AddTrackAndListener(source_, track_id_, track_rate_, this, segment, callback,
                       queue_track_);
 }
 
+#ifndef USE_FAKE_MEDIA_STREAMS
+void GenericReceiveListener::SetPrincipalHandle_m(const PrincipalHandle& principal_handle)
+{
+  class Message : public ControlMessage
+  {
+  public:
+    Message(GenericReceiveListener* listener,
+            MediaStream* stream,
+            const PrincipalHandle& principal_handle)
+      : ControlMessage(stream), listener_(listener), principal_handle_(principal_handle)
+    {}
+
+    void Run() override {
+      listener_->SetPrincipalHandle_msg(principal_handle_);
+    }
+
+    RefPtr<GenericReceiveListener> listener_;
+    PrincipalHandle principal_handle_;
+  };
+
+  source_->GraphImpl()->AppendMessage(MakeUnique<Message>(this, source_, principal_handle));
+}
+
+void GenericReceiveListener::SetPrincipalHandle_msg(const PrincipalHandle& principal_handle)
+{
+  principal_handle_ = principal_handle;
+}
+#endif // USE_FAKE_MEDIA_STREAMS
+
 MediaPipelineReceiveAudio::PipelineListener::PipelineListener(
     SourceMediaStream * source, TrackID track_id,
     const RefPtr<MediaSessionConduit>& conduit, bool queue_track)
   : GenericReceiveListener(source, track_id, DEFAULT_SAMPLE_RATE, queue_track), // XXX rate assumption
     conduit_(conduit)
 {
   MOZ_ASSERT(track_rate_%100 == 0);
 }
@@ -1478,17 +1507,17 @@ NotifyPull(MediaStreamGraph* graph, Stre
     DeinterleaveAndConvertBuffer(scratch_buffer,
                                  frames,
                                  channelCount,
                                  channels.Elements());
 
     outputChannels.AppendElements(channels);
 
     segment.AppendFrames(samples.forget(), outputChannels, frames,
-                         PRINCIPAL_HANDLE_NONE /* Fixed in later patch */);
+                         principal_handle_);
 
     // Handle track not actually added yet or removed/finished
     if (source_->AppendToTrack(track_id_, &segment)) {
       played_ticks_ += frames;
     } else {
       MOZ_MTLOG(ML_ERROR, "AppendToTrack failed");
       // we can't un-read the data, but that's ok since we don't want to
       // buffer - but don't i-loop!
@@ -1608,17 +1637,17 @@ NotifyPull(MediaStreamGraph* graph, Stre
   StreamTime delta = desired_time - played_ticks_;
 
   // Don't append if we've already provided a frame that supposedly
   // goes past the current aDesiredTime Doing so means a negative
   // delta and thus messes up handling of the graph
   if (delta > 0) {
     VideoSegment segment;
     segment.AppendFrame(image.forget(), delta, IntSize(width_, height_),
-                        PRINCIPAL_HANDLE_NONE /* Fixed in later patch */);
+                        principal_handle_);
     // Handle track not actually added yet or removed/finished
     if (source_->AppendToTrack(track_id_, &segment)) {
       played_ticks_ = desired_time;
     } else {
       MOZ_MTLOG(ML_ERROR, "AppendToTrack failed");
       return;
     }
   }
--- a/media/webrtc/signaling/src/mediapipeline/MediaPipeline.h
+++ b/media/webrtc/signaling/src/mediapipeline/MediaPipeline.h
@@ -306,36 +306,46 @@ class GenericReceiveListener : public Me
 {
  public:
   GenericReceiveListener(SourceMediaStream *source, TrackID track_id,
                          TrackRate track_rate, bool queue_track)
     : source_(source),
       track_id_(track_id),
       track_rate_(track_rate),
       played_ticks_(0),
-      queue_track_(queue_track) {}
+      queue_track_(queue_track),
+      principal_handle_(PRINCIPAL_HANDLE_NONE) {}
 
   virtual ~GenericReceiveListener() {}
 
   void AddSelf(MediaSegment* segment);
 
   void SetPlayedTicks(TrackTicks time) {
     played_ticks_ = time;
   }
 
   void EndTrack() {
     source_->EndTrack(track_id_);
   }
 
+#ifndef USE_FAKE_MEDIA_STREAMS
+  // Must be called on the main thread
+  void SetPrincipalHandle_m(const PrincipalHandle& aPrincipal);
+
+  // Must be called on the MediaStreamGraph thread
+  void SetPrincipalHandle_msg(const PrincipalHandle& aPrincipal);
+#endif // USE_FAKE_MEDIA_STREAMS
+
  protected:
   SourceMediaStream *source_;
   TrackID track_id_;
   TrackRate track_rate_;
   TrackTicks played_ticks_;
   bool queue_track_;
+  PrincipalHandle principal_handle_;
 };
 
 class TrackAddedCallback {
  public:
   virtual void TrackAdded(TrackTicks current_ticks) = 0;
 
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(TrackAddedCallback);
 
@@ -554,16 +564,21 @@ class MediaPipelineReceive : public Medi
                     rtcp_transport, filter),
       stream_(stream),
       segments_added_(0) {
     MOZ_ASSERT(stream_);
   }
 
   int segments_added() const { return segments_added_; }
 
+#ifndef USE_FAKE_MEDIA_STREAMS
+  // Sets the PrincipalHandle we set on the media chunks produced by this
+  // pipeline. Must be called on the main thread.
+  virtual void SetPrincipalHandle_m(const PrincipalHandle& principal_handle) = 0;
+#endif // USE_FAKE_MEDIA_STREAMS
  protected:
   ~MediaPipelineReceive() {
     MOZ_ASSERT(!stream_);  // Check that we have shut down already.
   }
 
   RefPtr<SourceMediaStream> stream_;
   int segments_added_;
 
@@ -605,16 +620,23 @@ class MediaPipelineReceiveAudio : public
       stream_->RemoveListener(listener_);
       stream_ = nullptr;
     }
   }
 
   virtual nsresult Init() override;
   virtual bool IsVideo() const override { return false; }
 
+#ifndef USE_FAKE_MEDIA_STREAMS
+  void SetPrincipalHandle_m(const PrincipalHandle& principal_handle) override
+  {
+    listener_->SetPrincipalHandle_m(principal_handle);
+  }
+#endif // USE_FAKE_MEDIA_STREAMS
+
  private:
   // Separate class to allow ref counting
   class PipelineListener : public GenericReceiveListener {
    public:
     PipelineListener(SourceMediaStream * source, TrackID track_id,
                      const RefPtr<MediaSessionConduit>& conduit,
                      bool queue_track);
 
@@ -695,16 +717,23 @@ class MediaPipelineReceiveVideo : public
       stream_->RemoveListener(listener_);
       stream_ = nullptr;
     }
   }
 
   virtual nsresult Init() override;
   virtual bool IsVideo() const override { return true; }
 
+#ifndef USE_FAKE_MEDIA_STREAMS
+  void SetPrincipalHandle_m(const PrincipalHandle& principal_handle) override
+  {
+    listener_->SetPrincipalHandle_m(principal_handle);
+  }
+#endif // USE_FAKE_MEDIA_STREAMS
+
  private:
   class PipelineRenderer : public VideoRenderer {
    public:
     explicit PipelineRenderer(MediaPipelineReceiveVideo *pipeline) :
       pipeline_(pipeline) {}
 
     void Detach() { pipeline_ = nullptr; }
 
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionMedia.cpp
@@ -1261,16 +1261,21 @@ void RemoteSourceStreamInfo::UpdatePrinc
   // This blasts away the existing principal.
   // We only do this when we become certain that the all tracks are safe to make
   // accessible to the script principal.
   for (auto& trackPair : mTracks) {
     MOZ_RELEASE_ASSERT(trackPair.second);
     RemoteTrackSource& source =
       static_cast<RemoteTrackSource&>(trackPair.second->GetSource());
     source.SetPrincipal(aPrincipal);
+
+    RefPtr<MediaPipeline> pipeline = GetPipelineByTrackId_m(trackPair.first);
+    MOZ_ASSERT(pipeline->direction() == MediaPipeline::RECEIVE);
+    static_cast<MediaPipelineReceive*>(pipeline.get())
+      ->SetPrincipalHandle_m(MakePrincipalHandle(aPrincipal));
   }
 }
 #endif // MOZILLA_INTERNAL_API
 
 bool
 PeerConnectionMedia::AnyCodecHasPluginID(uint64_t aPluginID)
 {
   for (uint32_t i=0; i < mLocalSourceStreams.Length(); ++i) {