blob: acea930be3ad6d71f7ec99512e1a6f4047af0c17 [file] [log] [blame]
deadbeef6979b022015-09-24 16:47:53 -07001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 16:47:53 -07003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
deadbeef6979b022015-09-24 16:47:53 -07009 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "pc/rtpreceiver.h"
deadbeef6979b022015-09-24 16:47:53 -070012
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010013#include <utility>
Steve Anton36b29d12017-10-30 09:57:42 -070014#include <vector>
15
Henrik Boström199e27b2018-07-04 20:51:53 +020016#include "api/mediastreamproxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/mediastreamtrackproxy.h"
18#include "api/videosourceproxy.h"
19#include "pc/audiotrack.h"
Henrik Boström199e27b2018-07-04 20:51:53 +020020#include "pc/mediastream.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "pc/videotrack.h"
22#include "rtc_base/trace_event.h"
deadbeef70ab1a12015-09-28 16:53:55 -070023
24namespace webrtc {
25
Harald Alvestrandc72af932018-01-11 17:18:19 +010026namespace {
27
28// This function is only expected to be called on the signalling thread.
29int GenerateUniqueId() {
30 static int g_unique_id = 0;
31
32 return ++g_unique_id;
33}
34
Henrik Boström199e27b2018-07-04 20:51:53 +020035std::vector<rtc::scoped_refptr<MediaStreamInterface>> CreateStreamsFromIds(
36 std::vector<std::string> stream_ids) {
37 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams(
38 stream_ids.size());
39 for (size_t i = 0; i < stream_ids.size(); ++i) {
40 streams[i] = MediaStreamProxy::Create(
41 rtc::Thread::Current(), MediaStream::Create(std::move(stream_ids[i])));
42 }
43 return streams;
44}
45
Benjamin Wright84583f62018-10-04 14:22:34 -070046// Attempt to attach the frame decryptor to the current media channel on the
47// correct worker thread only if both the media channel exists and a ssrc has
48// been allocated to the stream.
49void MaybeAttachFrameDecryptorToMediaChannel(
50 const absl::optional<uint32_t>& ssrc,
Benjamin Wrightbfd412e2018-09-10 14:06:02 -070051 rtc::Thread* worker_thread,
Benjamin Wright84583f62018-10-04 14:22:34 -070052 rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor,
Benjamin Wrightc462a6e2018-10-26 13:16:16 -070053 cricket::MediaChannel* media_channel,
54 bool stopped) {
55 if (media_channel && frame_decryptor && ssrc.has_value() && !stopped) {
Benjamin Wright6cc9cca2018-10-09 17:29:54 -070056 worker_thread->Invoke<void>(RTC_FROM_HERE, [&] {
Benjamin Wright84583f62018-10-04 14:22:34 -070057 media_channel->SetFrameDecryptor(*ssrc, frame_decryptor);
Benjamin Wrightbfd412e2018-09-10 14:06:02 -070058 });
59 }
60}
61
Harald Alvestrandc72af932018-01-11 17:18:19 +010062} // namespace
63
Henrik Boström199e27b2018-07-04 20:51:53 +020064AudioRtpReceiver::AudioRtpReceiver(rtc::Thread* worker_thread,
65 std::string receiver_id,
66 std::vector<std::string> stream_ids)
67 : AudioRtpReceiver(worker_thread,
68 receiver_id,
69 CreateStreamsFromIds(std::move(stream_ids))) {}
70
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010071AudioRtpReceiver::AudioRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -080072 rtc::Thread* worker_thread,
Steve Anton9158ef62017-11-27 13:01:52 -080073 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -080074 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
Steve Anton60776752018-01-10 11:51:34 -080075 : worker_thread_(worker_thread),
76 id_(receiver_id),
Steve Antond3679212018-01-17 17:41:02 -080077 source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)),
78 track_(AudioTrackProxy::Create(rtc::Thread::Current(),
79 AudioTrack::Create(receiver_id, source_))),
Harald Alvestrandc72af932018-01-11 17:18:19 +010080 cached_track_enabled_(track_->enabled()),
81 attachment_id_(GenerateUniqueId()) {
Steve Anton60776752018-01-10 11:51:34 -080082 RTC_DCHECK(worker_thread_);
tommi6eca7e32015-12-15 04:27:11 -080083 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 16:53:55 -070084 track_->RegisterObserver(this);
85 track_->GetSource()->RegisterAudioObserver(this);
Steve Antonef65ef12018-01-10 17:15:20 -080086 SetStreams(streams);
deadbeef70ab1a12015-09-28 16:53:55 -070087}
88
89AudioRtpReceiver::~AudioRtpReceiver() {
90 track_->GetSource()->UnregisterAudioObserver(this);
91 track_->UnregisterObserver(this);
92 Stop();
93}
94
95void AudioRtpReceiver::OnChanged() {
96 if (cached_track_enabled_ != track_->enabled()) {
97 cached_track_enabled_ = track_->enabled();
98 Reconfigure();
99 }
100}
101
Steve Anton60776752018-01-10 11:51:34 -0800102bool AudioRtpReceiver::SetOutputVolume(double volume) {
103 RTC_DCHECK_GE(volume, 0.0);
104 RTC_DCHECK_LE(volume, 10.0);
105 RTC_DCHECK(media_channel_);
Steve Antond3679212018-01-17 17:41:02 -0800106 RTC_DCHECK(ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800107 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800108 return media_channel_->SetOutputVolume(*ssrc_, volume);
Steve Anton60776752018-01-10 11:51:34 -0800109 });
110}
111
deadbeef70ab1a12015-09-28 16:53:55 -0700112void AudioRtpReceiver::OnSetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -0700113 RTC_DCHECK_GE(volume, 0);
114 RTC_DCHECK_LE(volume, 10);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700115 cached_volume_ = volume;
Steve Antond3679212018-01-17 17:41:02 -0800116 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100117 RTC_LOG(LS_ERROR)
118 << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700119 return;
120 }
deadbeef70ab1a12015-09-28 16:53:55 -0700121 // When the track is disabled, the volume of the source, which is the
122 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
123 // setting the volume to the source when the track is disabled.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700124 if (!stopped_ && track_->enabled()) {
Steve Anton60776752018-01-10 11:51:34 -0800125 if (!SetOutputVolume(cached_volume_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800126 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700127 }
128 }
deadbeef70ab1a12015-09-28 16:53:55 -0700129}
130
Henrik Boström199e27b2018-07-04 20:51:53 +0200131std::vector<std::string> AudioRtpReceiver::stream_ids() const {
132 std::vector<std::string> stream_ids(streams_.size());
133 for (size_t i = 0; i < streams_.size(); ++i)
134 stream_ids[i] = streams_[i]->id();
135 return stream_ids;
136}
137
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700138RtpParameters AudioRtpReceiver::GetParameters() const {
Steve Antond3679212018-01-17 17:41:02 -0800139 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700140 return RtpParameters();
141 }
Steve Anton60776752018-01-10 11:51:34 -0800142 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800143 return media_channel_->GetRtpReceiveParameters(*ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800144 });
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700145}
146
147bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
148 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Steve Antond3679212018-01-17 17:41:02 -0800149 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700150 return false;
151 }
Steve Anton60776752018-01-10 11:51:34 -0800152 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800153 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
Steve Anton60776752018-01-10 11:51:34 -0800154 });
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700155}
156
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700157void AudioRtpReceiver::SetFrameDecryptor(
158 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
159 frame_decryptor_ = std::move(frame_decryptor);
Benjamin Wright6cc9cca2018-10-09 17:29:54 -0700160 // Special Case: Set the frame decryptor to any value on any existing channel.
Benjamin Wrightc462a6e2018-10-26 13:16:16 -0700161 if (media_channel_ && ssrc_.has_value() && !stopped_) {
Benjamin Wright6cc9cca2018-10-09 17:29:54 -0700162 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
163 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
164 });
165 }
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700166}
167
168rtc::scoped_refptr<FrameDecryptorInterface>
169AudioRtpReceiver::GetFrameDecryptor() const {
170 return frame_decryptor_;
171}
172
deadbeefa601f5c2016-06-06 14:27:39 -0700173void AudioRtpReceiver::Stop() {
174 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700175 if (stopped_) {
deadbeefa601f5c2016-06-06 14:27:39 -0700176 return;
177 }
Steve Antond3679212018-01-17 17:41:02 -0800178 if (media_channel_ && ssrc_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700179 // Allow that SetOutputVolume fail. This is the normal case when the
180 // underlying media channel has already been deleted.
Steve Anton60776752018-01-10 11:51:34 -0800181 SetOutputVolume(0.0);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700182 }
183 stopped_ = true;
deadbeefa601f5c2016-06-06 14:27:39 -0700184}
185
Steve Antond3679212018-01-17 17:41:02 -0800186void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
187 if (!media_channel_) {
188 RTC_LOG(LS_ERROR)
189 << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
190 return;
191 }
192 if (ssrc_ == ssrc) {
193 return;
194 }
195 if (ssrc_) {
196 source_->Stop(media_channel_, *ssrc_);
197 }
198 ssrc_ = ssrc;
199 source_->Start(media_channel_, *ssrc_);
200 Reconfigure();
201}
202
Henrik Boström199e27b2018-07-04 20:51:53 +0200203void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
204 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
205}
206
Steve Antonef65ef12018-01-10 17:15:20 -0800207void AudioRtpReceiver::SetStreams(
208 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
209 // Remove remote track from any streams that are going away.
210 for (auto existing_stream : streams_) {
211 bool removed = true;
212 for (auto stream : streams) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700213 if (existing_stream->id() == stream->id()) {
Steve Antonef65ef12018-01-10 17:15:20 -0800214 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
215 removed = false;
216 break;
217 }
218 }
219 if (removed) {
220 existing_stream->RemoveTrack(track_);
221 }
222 }
223 // Add remote track to any streams that are new.
224 for (auto stream : streams) {
225 bool added = true;
226 for (auto existing_stream : streams_) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700227 if (stream->id() == existing_stream->id()) {
Steve Antonef65ef12018-01-10 17:15:20 -0800228 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
229 added = false;
230 break;
231 }
232 }
233 if (added) {
234 stream->AddTrack(track_);
235 }
236 }
237 streams_ = streams;
238}
239
hbos8d609f62017-04-10 07:39:05 -0700240std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
Steve Antond3679212018-01-17 17:41:02 -0800241 if (!media_channel_ || !ssrc_ || stopped_) {
242 return {};
243 }
Steve Anton60776752018-01-10 11:51:34 -0800244 return worker_thread_->Invoke<std::vector<RtpSource>>(
Steve Antond3679212018-01-17 17:41:02 -0800245 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
hbos8d609f62017-04-10 07:39:05 -0700246}
247
deadbeef70ab1a12015-09-28 16:53:55 -0700248void AudioRtpReceiver::Reconfigure() {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700249 RTC_DCHECK(!stopped_);
Steve Antond3679212018-01-17 17:41:02 -0800250 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_ERROR)
252 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
deadbeef70ab1a12015-09-28 16:53:55 -0700253 return;
254 }
Steve Anton60776752018-01-10 11:51:34 -0800255 if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800256 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700257 }
Benjamin Wright84583f62018-10-04 14:22:34 -0700258 // Reattach the frame decryptor if we were reconfigured.
Benjamin Wrightc462a6e2018-10-26 13:16:16 -0700259 MaybeAttachFrameDecryptorToMediaChannel(
260 ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
deadbeef70ab1a12015-09-28 16:53:55 -0700261}
262
zhihuang184a3fd2016-06-14 11:47:14 -0700263void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
264 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700265 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800266 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700267 observer_->OnFirstPacketReceived(media_type());
268 }
269}
270
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700271void AudioRtpReceiver::SetVoiceMediaChannel(
272 cricket::VoiceMediaChannel* voice_media_channel) {
273 media_channel_ = voice_media_channel;
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700274}
275
Steve Anton60776752018-01-10 11:51:34 -0800276void AudioRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700277 if (observer_) {
278 observer_->OnFirstPacketReceived(media_type());
279 }
280 received_first_packet_ = true;
281}
282
Henrik Boström199e27b2018-07-04 20:51:53 +0200283VideoRtpReceiver::VideoRtpReceiver(rtc::Thread* worker_thread,
284 std::string receiver_id,
285 std::vector<std::string> stream_ids)
286 : VideoRtpReceiver(worker_thread,
287 receiver_id,
288 CreateStreamsFromIds(std::move(stream_ids))) {}
289
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100290VideoRtpReceiver::VideoRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -0800291 rtc::Thread* worker_thread,
Steve Antonef65ef12018-01-10 17:15:20 -0800292 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -0800293 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
Steve Anton60776752018-01-10 11:51:34 -0800294 : worker_thread_(worker_thread),
Steve Antonef65ef12018-01-10 17:15:20 -0800295 id_(receiver_id),
Niels Möller5d67f822018-05-23 16:28:17 +0200296 source_(new RefCountedObject<VideoRtpTrackSource>()),
perkjf0dcfe22016-03-10 18:32:00 +0100297 track_(VideoTrackProxy::Create(
298 rtc::Thread::Current(),
nisse5b68ab52016-04-07 07:45:54 -0700299 worker_thread,
300 VideoTrack::Create(
Steve Antonef65ef12018-01-10 17:15:20 -0800301 receiver_id,
nisse5b68ab52016-04-07 07:45:54 -0700302 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
303 worker_thread,
perkj773be362017-07-31 23:22:01 -0700304 source_),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100305 worker_thread))),
306 attachment_id_(GenerateUniqueId()) {
Steve Anton60776752018-01-10 11:51:34 -0800307 RTC_DCHECK(worker_thread_);
Steve Antonef65ef12018-01-10 17:15:20 -0800308 SetStreams(streams);
perkjf0dcfe22016-03-10 18:32:00 +0100309 source_->SetState(MediaSourceInterface::kLive);
deadbeef70ab1a12015-09-28 16:53:55 -0700310}
311
312VideoRtpReceiver::~VideoRtpReceiver() {
313 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700314 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700315 Stop();
316}
317
Henrik Boström199e27b2018-07-04 20:51:53 +0200318std::vector<std::string> VideoRtpReceiver::stream_ids() const {
319 std::vector<std::string> stream_ids(streams_.size());
320 for (size_t i = 0; i < streams_.size(); ++i)
321 stream_ids[i] = streams_[i]->id();
322 return stream_ids;
323}
324
Steve Anton60776752018-01-10 11:51:34 -0800325bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
326 RTC_DCHECK(media_channel_);
Steve Antond3679212018-01-17 17:41:02 -0800327 RTC_DCHECK(ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800328 return worker_thread_->Invoke<bool>(
Steve Antond3679212018-01-17 17:41:02 -0800329 RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); });
Steve Anton60776752018-01-10 11:51:34 -0800330}
331
deadbeefa601f5c2016-06-06 14:27:39 -0700332RtpParameters VideoRtpReceiver::GetParameters() const {
Steve Antond3679212018-01-17 17:41:02 -0800333 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700334 return RtpParameters();
335 }
Steve Anton60776752018-01-10 11:51:34 -0800336 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800337 return media_channel_->GetRtpReceiveParameters(*ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800338 });
deadbeefa601f5c2016-06-06 14:27:39 -0700339}
340
341bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
342 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Steve Antond3679212018-01-17 17:41:02 -0800343 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700344 return false;
345 }
Steve Anton60776752018-01-10 11:51:34 -0800346 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800347 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
Steve Anton60776752018-01-10 11:51:34 -0800348 });
deadbeefa601f5c2016-06-06 14:27:39 -0700349}
350
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700351void VideoRtpReceiver::SetFrameDecryptor(
352 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
353 frame_decryptor_ = std::move(frame_decryptor);
Benjamin Wright6cc9cca2018-10-09 17:29:54 -0700354 // Special Case: Set the frame decryptor to any value on any existing channel.
Benjamin Wrightc462a6e2018-10-26 13:16:16 -0700355 if (media_channel_ && ssrc_.has_value() && !stopped_) {
Benjamin Wright6cc9cca2018-10-09 17:29:54 -0700356 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
357 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
358 });
359 }
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700360}
361
362rtc::scoped_refptr<FrameDecryptorInterface>
363VideoRtpReceiver::GetFrameDecryptor() const {
364 return frame_decryptor_;
365}
366
deadbeef70ab1a12015-09-28 16:53:55 -0700367void VideoRtpReceiver::Stop() {
368 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700369 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700370 return;
371 }
perkjf0dcfe22016-03-10 18:32:00 +0100372 source_->SetState(MediaSourceInterface::kEnded);
Steve Antond3679212018-01-17 17:41:02 -0800373 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100374 RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700375 } else {
376 // Allow that SetSink fail. This is the normal case when the underlying
377 // media channel has already been deleted.
Steve Anton60776752018-01-10 11:51:34 -0800378 SetSink(nullptr);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700379 }
380 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700381}
382
Steve Antond3679212018-01-17 17:41:02 -0800383void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
384 if (!media_channel_) {
385 RTC_LOG(LS_ERROR)
386 << "VideoRtpReceiver::SetupMediaChannel: No video channel exists.";
387 }
388 if (ssrc_ == ssrc) {
389 return;
390 }
391 if (ssrc_) {
392 SetSink(nullptr);
393 }
394 ssrc_ = ssrc;
Niels Möller5d67f822018-05-23 16:28:17 +0200395 SetSink(source_->sink());
Benjamin Wright84583f62018-10-04 14:22:34 -0700396 // Attach any existing frame decryptor to the media channel.
Benjamin Wrightc462a6e2018-10-26 13:16:16 -0700397 MaybeAttachFrameDecryptorToMediaChannel(
398 ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
Steve Antond3679212018-01-17 17:41:02 -0800399}
400
Henrik Boström199e27b2018-07-04 20:51:53 +0200401void VideoRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
402 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
403}
404
Steve Antonef65ef12018-01-10 17:15:20 -0800405void VideoRtpReceiver::SetStreams(
406 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
407 // Remove remote track from any streams that are going away.
408 for (auto existing_stream : streams_) {
409 bool removed = true;
410 for (auto stream : streams) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700411 if (existing_stream->id() == stream->id()) {
Steve Antonef65ef12018-01-10 17:15:20 -0800412 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
413 removed = false;
414 break;
415 }
416 }
417 if (removed) {
418 existing_stream->RemoveTrack(track_);
419 }
420 }
421 // Add remote track to any streams that are new.
422 for (auto stream : streams) {
423 bool added = true;
424 for (auto existing_stream : streams_) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700425 if (stream->id() == existing_stream->id()) {
Steve Antonef65ef12018-01-10 17:15:20 -0800426 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
427 added = false;
428 break;
429 }
430 }
431 if (added) {
432 stream->AddTrack(track_);
433 }
434 }
435 streams_ = streams;
436}
437
zhihuang184a3fd2016-06-14 11:47:14 -0700438void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
439 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700440 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800441 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700442 observer_->OnFirstPacketReceived(media_type());
443 }
444}
445
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700446void VideoRtpReceiver::SetVideoMediaChannel(
447 cricket::VideoMediaChannel* video_media_channel) {
448 media_channel_ = video_media_channel;
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700449}
450
Steve Anton60776752018-01-10 11:51:34 -0800451void VideoRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700452 if (observer_) {
453 observer_->OnFirstPacketReceived(media_type());
454 }
455 received_first_packet_ = true;
456}
457
Jonas Oreland49ac5952018-09-26 16:04:32 +0200458std::vector<RtpSource> VideoRtpReceiver::GetSources() const {
459 if (!media_channel_ || !ssrc_ || stopped_) {
460 return {};
461 }
462 return worker_thread_->Invoke<std::vector<RtpSource>>(
463 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
464}
465
deadbeef70ab1a12015-09-28 16:53:55 -0700466} // namespace webrtc