RtpReceiverInterface::stream_ids() added.

This is the first step to removing streams from third_party/webrtc.
RtpReceiverInterface::streams() will have to be removed separately.
See https://crbug.com/webrtc/9480 for more information.

Bug: webrtc:9480
Change-Id: I6f9e6ddcda5e2245cc601d2cc6205b7b363f73ef
Reviewed-on: https://webrtc-review.googlesource.com/86840
Reviewed-by: Seth Hampson <shampson@webrtc.org>
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23929}
diff --git a/api/rtpreceiverinterface.cc b/api/rtpreceiverinterface.cc
index 96815a9..fcdca69 100644
--- a/api/rtpreceiverinterface.cc
+++ b/api/rtpreceiverinterface.cc
@@ -32,6 +32,10 @@
 RtpSource& RtpSource::operator=(const RtpSource&) = default;
 RtpSource::~RtpSource() = default;
 
+std::vector<std::string> RtpReceiverInterface::stream_ids() const {
+  return {};
+}
+
 std::vector<rtc::scoped_refptr<MediaStreamInterface>>
 RtpReceiverInterface::streams() const {
   return {};
diff --git a/api/rtpreceiverinterface.h b/api/rtpreceiverinterface.h
index 1801dc1..b2499b4 100644
--- a/api/rtpreceiverinterface.h
+++ b/api/rtpreceiverinterface.h
@@ -93,8 +93,12 @@
   virtual rtc::scoped_refptr<MediaStreamTrackInterface> track() const = 0;
   // The list of streams that |track| is associated with. This is the same as
   // the [[AssociatedRemoteMediaStreams]] internal slot in the spec.
-  // https://w3c.github.io/webrtc-pc/#dfn-x%5B%5Bassociatedremotemediastreams%5D%5D
+  // https://w3c.github.io/webrtc-pc/#dfn-associatedremotemediastreams
   // TODO(hbos): Make pure virtual as soon as Chromium's mock implements this.
+  // TODO(https://crbug.com/webrtc/9480): Remove streams() in favor of
+  // stream_ids() as soon as downstream projects are no longer dependent on
+  // stream objects.
+  virtual std::vector<std::string> stream_ids() const;
   virtual std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams() const;
 
   // Audio or video receiver?
diff --git a/ortc/ortcrtpreceiveradapter.cc b/ortc/ortcrtpreceiveradapter.cc
index 8e05a85..6894122 100644
--- a/ortc/ortcrtpreceiveradapter.cc
+++ b/ortc/ortcrtpreceiveradapter.cc
@@ -10,7 +10,9 @@
 
 #include "ortc/ortcrtpreceiveradapter.h"
 
+#include <string>
 #include <utility>
+#include <vector>
 
 #include "media/base/mediaconstants.h"
 #include "ortc/rtptransportadapter.h"
@@ -151,9 +153,9 @@
   internal_receiver_ = nullptr;
   switch (kind_) {
     case cricket::MEDIA_TYPE_AUDIO: {
-      auto* audio_receiver =
-          new AudioRtpReceiver(rtp_transport_controller_->worker_thread(),
-                               rtc::CreateRandomUuid(), {});
+      auto* audio_receiver = new AudioRtpReceiver(
+          rtp_transport_controller_->worker_thread(), rtc::CreateRandomUuid(),
+          std::vector<std::string>({}));
       auto* voice_channel = rtp_transport_controller_->voice_channel();
       RTC_DCHECK(voice_channel);
       audio_receiver->SetVoiceMediaChannel(voice_channel->media_channel());
@@ -161,9 +163,9 @@
       break;
     }
     case cricket::MEDIA_TYPE_VIDEO: {
-      auto* video_receiver =
-          new VideoRtpReceiver(rtp_transport_controller_->worker_thread(),
-                               rtc::CreateRandomUuid(), {});
+      auto* video_receiver = new VideoRtpReceiver(
+          rtp_transport_controller_->worker_thread(), rtc::CreateRandomUuid(),
+          std::vector<std::string>({}));
       auto* video_channel = rtp_transport_controller_->video_channel();
       RTC_DCHECK(video_channel);
       video_receiver->SetVideoMediaChannel(video_channel->media_channel());
diff --git a/pc/peerconnection.cc b/pc/peerconnection.cc
index ea5d922..c560e05 100644
--- a/pc/peerconnection.cc
+++ b/pc/peerconnection.cc
@@ -1478,14 +1478,14 @@
       receiver;
   if (media_type == cricket::MEDIA_TYPE_AUDIO) {
     receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
-        signaling_thread(),
-        new AudioRtpReceiver(worker_thread(), receiver_id, {}));
+        signaling_thread(), new AudioRtpReceiver(worker_thread(), receiver_id,
+                                                 std::vector<std::string>({})));
     NoteUsageEvent(UsageEvent::AUDIO_ADDED);
   } else {
     RTC_DCHECK_EQ(media_type, cricket::MEDIA_TYPE_VIDEO);
     receiver = RtpReceiverProxyWithInternal<RtpReceiverInternal>::Create(
-        signaling_thread(),
-        new VideoRtpReceiver(worker_thread(), receiver_id, {}));
+        signaling_thread(), new VideoRtpReceiver(worker_thread(), receiver_id,
+                                                 std::vector<std::string>({})));
     NoteUsageEvent(UsageEvent::VIDEO_ADDED);
   }
   return receiver;
@@ -2442,6 +2442,8 @@
           media_streams.push_back(stream);
         }
         // This will add the remote track to the streams.
+        // TODO(hbos): When we remove remote_streams(), use set_stream_ids()
+        // instead. https://crbug.com/webrtc/9480
         transceiver->internal()->receiver_internal()->SetStreams(media_streams);
         now_receiving_transceivers.push_back(transceiver);
       }
@@ -2456,9 +2458,12 @@
         std::vector<rtc::scoped_refptr<MediaStreamInterface>> media_streams =
             transceiver->internal()->receiver_internal()->streams();
         // This will remove the remote track from the streams.
-        transceiver->internal()->receiver_internal()->SetStreams({});
+        transceiver->internal()->receiver_internal()->set_stream_ids({});
         no_longer_receiving_transceivers.push_back(transceiver);
         // Remove any streams that no longer have tracks.
+        // TODO(https://crbug.com/webrtc/9480): When we use stream IDs instead
+        // of streams, see if the stream was removed by checking if this was the
+        // last receiver with that stream ID.
         for (auto stream : media_streams) {
           if (stream->GetAudioTracks().empty() &&
               stream->GetVideoTracks().empty()) {
@@ -3370,6 +3375,8 @@
     const RtpSenderInfo& remote_sender_info) {
   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
   streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
+  // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
+  // the constructor taking stream IDs instead.
   auto* audio_receiver = new AudioRtpReceiver(
       worker_thread(), remote_sender_info.sender_id, streams);
   audio_receiver->SetVoiceMediaChannel(voice_media_channel());
@@ -3386,6 +3393,8 @@
     const RtpSenderInfo& remote_sender_info) {
   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams;
   streams.push_back(rtc::scoped_refptr<MediaStreamInterface>(stream));
+  // TODO(https://crbug.com/webrtc/9480): When we remove remote_streams(), use
+  // the constructor taking stream IDs instead.
   auto* video_receiver = new VideoRtpReceiver(
       worker_thread(), remote_sender_info.sender_id, streams);
   video_receiver->SetVideoMediaChannel(video_media_channel());
diff --git a/pc/rtpreceiver.cc b/pc/rtpreceiver.cc
index 341261b..da2f81b 100644
--- a/pc/rtpreceiver.cc
+++ b/pc/rtpreceiver.cc
@@ -13,9 +13,11 @@
 #include <utility>
 #include <vector>
 
+#include "api/mediastreamproxy.h"
 #include "api/mediastreamtrackproxy.h"
 #include "api/videosourceproxy.h"
 #include "pc/audiotrack.h"
+#include "pc/mediastream.h"
 #include "pc/videotrack.h"
 #include "rtc_base/trace_event.h"
 
@@ -30,8 +32,26 @@
   return ++g_unique_id;
 }
 
+std::vector<rtc::scoped_refptr<MediaStreamInterface>> CreateStreamsFromIds(
+    std::vector<std::string> stream_ids) {
+  std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams(
+      stream_ids.size());
+  for (size_t i = 0; i < stream_ids.size(); ++i) {
+    streams[i] = MediaStreamProxy::Create(
+        rtc::Thread::Current(), MediaStream::Create(std::move(stream_ids[i])));
+  }
+  return streams;
+}
+
 }  // namespace
 
+AudioRtpReceiver::AudioRtpReceiver(rtc::Thread* worker_thread,
+                                   std::string receiver_id,
+                                   std::vector<std::string> stream_ids)
+    : AudioRtpReceiver(worker_thread,
+                       receiver_id,
+                       CreateStreamsFromIds(std::move(stream_ids))) {}
+
 AudioRtpReceiver::AudioRtpReceiver(
     rtc::Thread* worker_thread,
     const std::string& receiver_id,
@@ -92,6 +112,13 @@
   }
 }
 
+std::vector<std::string> AudioRtpReceiver::stream_ids() const {
+  std::vector<std::string> stream_ids(streams_.size());
+  for (size_t i = 0; i < streams_.size(); ++i)
+    stream_ids[i] = streams_[i]->id();
+  return stream_ids;
+}
+
 RtpParameters AudioRtpReceiver::GetParameters() const {
   if (!media_channel_ || !ssrc_ || stopped_) {
     return RtpParameters();
@@ -141,6 +168,10 @@
   Reconfigure();
 }
 
+void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
+  SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
+}
+
 void AudioRtpReceiver::SetStreams(
     const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
   // Remove remote track from any streams that are going away.
@@ -209,6 +240,13 @@
   received_first_packet_ = true;
 }
 
+VideoRtpReceiver::VideoRtpReceiver(rtc::Thread* worker_thread,
+                                   std::string receiver_id,
+                                   std::vector<std::string> stream_ids)
+    : VideoRtpReceiver(worker_thread,
+                       receiver_id,
+                       CreateStreamsFromIds(std::move(stream_ids))) {}
+
 VideoRtpReceiver::VideoRtpReceiver(
     rtc::Thread* worker_thread,
     const std::string& receiver_id,
@@ -237,6 +275,13 @@
   Stop();
 }
 
+std::vector<std::string> VideoRtpReceiver::stream_ids() const {
+  std::vector<std::string> stream_ids(streams_.size());
+  for (size_t i = 0; i < streams_.size(); ++i)
+    stream_ids[i] = streams_[i]->id();
+  return stream_ids;
+}
+
 bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
   RTC_DCHECK(media_channel_);
   RTC_DCHECK(ssrc_);
@@ -294,6 +339,10 @@
   SetSink(source_->sink());
 }
 
+void VideoRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
+  SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
+}
+
 void VideoRtpReceiver::SetStreams(
     const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
   // Remove remote track from any streams that are going away.
diff --git a/pc/rtpreceiver.h b/pc/rtpreceiver.h
index 964a8f5..263ad94 100644
--- a/pc/rtpreceiver.h
+++ b/pc/rtpreceiver.h
@@ -59,6 +59,10 @@
   // Set the associated remote media streams for this receiver. The remote track
   // will be removed from any streams that are no longer present and added to
   // any new streams.
+  virtual void set_stream_ids(std::vector<std::string> stream_ids) = 0;
+  // TODO(https://crbug.com/webrtc/9480): Remove SetStreams() in favor of
+  // set_stream_ids() as soon as downstream projects are no longer dependent on
+  // stream objects.
   virtual void SetStreams(
       const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) = 0;
 
@@ -72,6 +76,10 @@
                          public AudioSourceInterface::AudioObserver,
                          public rtc::RefCountedObject<RtpReceiverInternal> {
  public:
+  AudioRtpReceiver(rtc::Thread* worker_thread,
+                   std::string receiver_id,
+                   std::vector<std::string> stream_ids);
+  // TODO(https://crbug.com/webrtc/9480): Remove this when streams() is removed.
   AudioRtpReceiver(
       rtc::Thread* worker_thread,
       const std::string& receiver_id,
@@ -92,6 +100,7 @@
   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
     return track_.get();
   }
+  std::vector<std::string> stream_ids() const override;
   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
       const override {
     return streams_;
@@ -111,6 +120,7 @@
   void SetupMediaChannel(uint32_t ssrc) override;
   uint32_t ssrc() const override { return ssrc_.value_or(0); }
   void NotifyFirstPacketReceived() override;
+  void set_stream_ids(std::vector<std::string> stream_ids) override;
   void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
                       streams) override;
 
@@ -151,6 +161,11 @@
  public:
   // An SSRC of 0 will create a receiver that will match the first SSRC it
   // sees.
+  VideoRtpReceiver(rtc::Thread* worker_thread,
+                   std::string receiver_id,
+                   std::vector<std::string> streams_ids);
+  // TODO(hbos): Remove this when streams() is removed.
+  // https://crbug.com/webrtc/9480
   VideoRtpReceiver(
       rtc::Thread* worker_thread,
       const std::string& receiver_id,
@@ -166,6 +181,7 @@
   rtc::scoped_refptr<MediaStreamTrackInterface> track() const override {
     return track_.get();
   }
+  std::vector<std::string> stream_ids() const override;
   std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams()
       const override {
     return streams_;
@@ -185,6 +201,7 @@
   void SetupMediaChannel(uint32_t ssrc) override;
   uint32_t ssrc() const override { return ssrc_.value_or(0); }
   void NotifyFirstPacketReceived() override;
+  void set_stream_ids(std::vector<std::string> stream_ids) override;
   void SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&
                       streams) override;
 
diff --git a/pc/test/mock_rtpreceiverinternal.h b/pc/test/mock_rtpreceiverinternal.h
index 8fb93b4..8b12e8f 100644
--- a/pc/test/mock_rtpreceiverinternal.h
+++ b/pc/test/mock_rtpreceiverinternal.h
@@ -25,6 +25,7 @@
   // RtpReceiverInterface methods.
   MOCK_METHOD1(SetTrack, void(MediaStreamTrackInterface*));
   MOCK_CONST_METHOD0(track, rtc::scoped_refptr<MediaStreamTrackInterface>());
+  MOCK_CONST_METHOD0(stream_ids, std::vector<std::string>());
   MOCK_CONST_METHOD0(streams,
                      std::vector<rtc::scoped_refptr<MediaStreamInterface>>());
   MOCK_CONST_METHOD0(media_type, cricket::MediaType());
@@ -41,6 +42,7 @@
   MOCK_METHOD1(SetupMediaChannel, void(uint32_t));
   MOCK_CONST_METHOD0(ssrc, uint32_t());
   MOCK_METHOD0(NotifyFirstPacketReceived, void());
+  MOCK_METHOD1(set_stream_ids, void(std::vector<std::string>));
   MOCK_METHOD1(
       SetStreams,
       void(const std::vector<rtc::scoped_refptr<MediaStreamInterface>>&));