Configure media flow correctly with Unified Plan

This also changes RtpReceiver and RemoteAudioSource to have two-step
initialization, since in Unified Plan RtpReceivers are created much
earlier than in Plan B.

Bug: webrtc:7600
Change-Id: Ia135d25eb8bcab22969007b3a825a5a43ce62bf4
Reviewed-on: https://webrtc-review.googlesource.com/39382
Reviewed-by: Peter Thatcher <pthatcher@webrtc.org>
Commit-Queue: Steve Anton <steveanton@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21681}
diff --git a/pc/rtpreceiver.h b/pc/rtpreceiver.h
index 82c3134..887f828 100644
--- a/pc/rtpreceiver.h
+++ b/pc/rtpreceiver.h
@@ -34,6 +34,11 @@
  public:
   virtual void Stop() = 0;
 
+  // Configures the RtpReceiver with the underlying media channel, with the
+  // given SSRC as the stream identifier. If |ssrc| is 0, the receiver will
+  // receive packets on unsignaled SSRCs.
+  virtual void SetupMediaChannel(uint32_t ssrc) = 0;
+
   // This SSRC is used as an identifier for the receiver between the API layer
   // and the WebRtcVideoEngine, WebRtcVoiceEngine layer.
   virtual uint32_t ssrc() const = 0;
@@ -53,16 +58,10 @@
                          public AudioSourceInterface::AudioObserver,
                          public rtc::RefCountedObject<RtpReceiverInternal> {
  public:
-  // An SSRC of 0 will create a receiver that will match the first SSRC it
-  // sees.
-  // TODO(deadbeef): Use rtc::Optional, or have another constructor that
-  // doesn't take an SSRC, and make this one DCHECK(ssrc != 0).
   AudioRtpReceiver(
       rtc::Thread* worker_thread,
       const std::string& receiver_id,
-      const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
-      uint32_t ssrc,
-      cricket::VoiceMediaChannel* media_channel);
+      const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
   virtual ~AudioRtpReceiver();
 
   // ObserverInterface implementation
@@ -95,7 +94,8 @@
 
   // RtpReceiverInternal implementation.
   void Stop() override;
-  uint32_t ssrc() const override { return ssrc_; }
+  void SetupMediaChannel(uint32_t ssrc) override;
+  uint32_t ssrc() const override { return ssrc_.value_or(0); }
   void NotifyFirstPacketReceived() override;
   void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
                       streams) override;
@@ -115,9 +115,10 @@
 
   rtc::Thread* const worker_thread_;
   const std::string id_;
-  const uint32_t ssrc_;
-  cricket::VoiceMediaChannel* media_channel_ = nullptr;
+  const rtc::scoped_refptr<RemoteAudioSource> source_;
   const rtc::scoped_refptr<AudioTrackInterface> track_;
+  cricket::VoiceMediaChannel* media_channel_ = nullptr;
+  rtc::Optional<uint32_t> ssrc_;
   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams_;
   bool cached_track_enabled_;
   double cached_volume_ = 1;
@@ -134,9 +135,7 @@
   VideoRtpReceiver(
       rtc::Thread* worker_thread,
       const std::string& receiver_id,
-      const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
-      uint32_t ssrc,
-      cricket::VideoMediaChannel* media_channel);
+      const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams);
 
   virtual ~VideoRtpReceiver();
 
@@ -164,7 +163,8 @@
 
   // RtpReceiverInternal implementation.
   void Stop() override;
-  uint32_t ssrc() const override { return ssrc_; }
+  void SetupMediaChannel(uint32_t ssrc) override;
+  uint32_t ssrc() const override { return ssrc_.value_or(0); }
   void NotifyFirstPacketReceived() override;
   void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
                       streams) override;
@@ -180,8 +180,8 @@
 
   rtc::Thread* const worker_thread_;
   const std::string id_;
-  uint32_t ssrc_;
   cricket::VideoMediaChannel* media_channel_ = nullptr;
+  rtc::Optional<uint32_t> ssrc_;
   // |broadcaster_| is needed since the decoder can only handle one sink.
   // It might be better if the decoder can handle multiple sinks and consider
   // the VideoSinkWants.