blob: 1916a7367032c4bb1f81a6510d2eef11c45a9279 [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
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800271void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
272 RTC_DCHECK(media_channel == nullptr ||
273 media_channel->media_type() == media_type());
274 media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700275}
276
Steve Anton60776752018-01-10 11:51:34 -0800277void AudioRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700278 if (observer_) {
279 observer_->OnFirstPacketReceived(media_type());
280 }
281 received_first_packet_ = true;
282}
283
Henrik Boström199e27b2018-07-04 20:51:53 +0200284VideoRtpReceiver::VideoRtpReceiver(rtc::Thread* worker_thread,
285 std::string receiver_id,
286 std::vector<std::string> stream_ids)
287 : VideoRtpReceiver(worker_thread,
288 receiver_id,
289 CreateStreamsFromIds(std::move(stream_ids))) {}
290
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100291VideoRtpReceiver::VideoRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -0800292 rtc::Thread* worker_thread,
Steve Antonef65ef12018-01-10 17:15:20 -0800293 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -0800294 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
Steve Anton60776752018-01-10 11:51:34 -0800295 : worker_thread_(worker_thread),
Steve Antonef65ef12018-01-10 17:15:20 -0800296 id_(receiver_id),
Niels Möller5d67f822018-05-23 16:28:17 +0200297 source_(new RefCountedObject<VideoRtpTrackSource>()),
perkjf0dcfe22016-03-10 18:32:00 +0100298 track_(VideoTrackProxy::Create(
299 rtc::Thread::Current(),
nisse5b68ab52016-04-07 07:45:54 -0700300 worker_thread,
301 VideoTrack::Create(
Steve Antonef65ef12018-01-10 17:15:20 -0800302 receiver_id,
nisse5b68ab52016-04-07 07:45:54 -0700303 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
304 worker_thread,
perkj773be362017-07-31 23:22:01 -0700305 source_),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100306 worker_thread))),
307 attachment_id_(GenerateUniqueId()) {
Steve Anton60776752018-01-10 11:51:34 -0800308 RTC_DCHECK(worker_thread_);
Steve Antonef65ef12018-01-10 17:15:20 -0800309 SetStreams(streams);
perkjf0dcfe22016-03-10 18:32:00 +0100310 source_->SetState(MediaSourceInterface::kLive);
deadbeef70ab1a12015-09-28 16:53:55 -0700311}
312
313VideoRtpReceiver::~VideoRtpReceiver() {
314 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700315 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700316 Stop();
317}
318
Henrik Boström199e27b2018-07-04 20:51:53 +0200319std::vector<std::string> VideoRtpReceiver::stream_ids() const {
320 std::vector<std::string> stream_ids(streams_.size());
321 for (size_t i = 0; i < streams_.size(); ++i)
322 stream_ids[i] = streams_[i]->id();
323 return stream_ids;
324}
325
Steve Anton60776752018-01-10 11:51:34 -0800326bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
327 RTC_DCHECK(media_channel_);
Steve Antond3679212018-01-17 17:41:02 -0800328 RTC_DCHECK(ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800329 return worker_thread_->Invoke<bool>(
Steve Antond3679212018-01-17 17:41:02 -0800330 RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); });
Steve Anton60776752018-01-10 11:51:34 -0800331}
332
deadbeefa601f5c2016-06-06 14:27:39 -0700333RtpParameters VideoRtpReceiver::GetParameters() const {
Steve Antond3679212018-01-17 17:41:02 -0800334 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700335 return RtpParameters();
336 }
Steve Anton60776752018-01-10 11:51:34 -0800337 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800338 return media_channel_->GetRtpReceiveParameters(*ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800339 });
deadbeefa601f5c2016-06-06 14:27:39 -0700340}
341
342bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
343 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Steve Antond3679212018-01-17 17:41:02 -0800344 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700345 return false;
346 }
Steve Anton60776752018-01-10 11:51:34 -0800347 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800348 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
Steve Anton60776752018-01-10 11:51:34 -0800349 });
deadbeefa601f5c2016-06-06 14:27:39 -0700350}
351
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700352void VideoRtpReceiver::SetFrameDecryptor(
353 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
354 frame_decryptor_ = std::move(frame_decryptor);
Benjamin Wright6cc9cca2018-10-09 17:29:54 -0700355 // Special Case: Set the frame decryptor to any value on any existing channel.
Benjamin Wrightc462a6e2018-10-26 13:16:16 -0700356 if (media_channel_ && ssrc_.has_value() && !stopped_) {
Benjamin Wright6cc9cca2018-10-09 17:29:54 -0700357 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
358 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
359 });
360 }
Benjamin Wrightd81ac952018-08-29 17:02:10 -0700361}
362
363rtc::scoped_refptr<FrameDecryptorInterface>
364VideoRtpReceiver::GetFrameDecryptor() const {
365 return frame_decryptor_;
366}
367
deadbeef70ab1a12015-09-28 16:53:55 -0700368void VideoRtpReceiver::Stop() {
369 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700370 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700371 return;
372 }
perkjf0dcfe22016-03-10 18:32:00 +0100373 source_->SetState(MediaSourceInterface::kEnded);
Steve Antond3679212018-01-17 17:41:02 -0800374 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100375 RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700376 } else {
377 // Allow that SetSink fail. This is the normal case when the underlying
378 // media channel has already been deleted.
Steve Anton60776752018-01-10 11:51:34 -0800379 SetSink(nullptr);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700380 }
381 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700382}
383
Steve Antond3679212018-01-17 17:41:02 -0800384void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
385 if (!media_channel_) {
386 RTC_LOG(LS_ERROR)
387 << "VideoRtpReceiver::SetupMediaChannel: No video channel exists.";
388 }
389 if (ssrc_ == ssrc) {
390 return;
391 }
392 if (ssrc_) {
393 SetSink(nullptr);
394 }
395 ssrc_ = ssrc;
Niels Möller5d67f822018-05-23 16:28:17 +0200396 SetSink(source_->sink());
Benjamin Wright84583f62018-10-04 14:22:34 -0700397 // Attach any existing frame decryptor to the media channel.
Benjamin Wrightc462a6e2018-10-26 13:16:16 -0700398 MaybeAttachFrameDecryptorToMediaChannel(
399 ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
Steve Antond3679212018-01-17 17:41:02 -0800400}
401
Henrik Boström199e27b2018-07-04 20:51:53 +0200402void VideoRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
403 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
404}
405
Steve Antonef65ef12018-01-10 17:15:20 -0800406void VideoRtpReceiver::SetStreams(
407 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
408 // Remove remote track from any streams that are going away.
409 for (auto existing_stream : streams_) {
410 bool removed = true;
411 for (auto stream : streams) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700412 if (existing_stream->id() == stream->id()) {
Steve Antonef65ef12018-01-10 17:15:20 -0800413 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
414 removed = false;
415 break;
416 }
417 }
418 if (removed) {
419 existing_stream->RemoveTrack(track_);
420 }
421 }
422 // Add remote track to any streams that are new.
423 for (auto stream : streams) {
424 bool added = true;
425 for (auto existing_stream : streams_) {
Seth Hampson13b8bad2018-03-13 16:05:28 -0700426 if (stream->id() == existing_stream->id()) {
Steve Antonef65ef12018-01-10 17:15:20 -0800427 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
428 added = false;
429 break;
430 }
431 }
432 if (added) {
433 stream->AddTrack(track_);
434 }
435 }
436 streams_ = streams;
437}
438
zhihuang184a3fd2016-06-14 11:47:14 -0700439void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
440 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700441 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800442 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700443 observer_->OnFirstPacketReceived(media_type());
444 }
445}
446
Amit Hilbuchdd9390c2018-11-13 16:26:05 -0800447void VideoRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
448 RTC_DCHECK(media_channel == nullptr ||
449 media_channel->media_type() == media_type());
450 media_channel_ = static_cast<cricket::VideoMediaChannel*>(media_channel);
Benjamin Wrightbfd412e2018-09-10 14:06:02 -0700451}
452
Steve Anton60776752018-01-10 11:51:34 -0800453void VideoRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700454 if (observer_) {
455 observer_->OnFirstPacketReceived(media_type());
456 }
457 received_first_packet_ = true;
458}
459
Jonas Oreland49ac5952018-09-26 16:04:32 +0200460std::vector<RtpSource> VideoRtpReceiver::GetSources() const {
461 if (!media_channel_ || !ssrc_ || stopped_) {
462 return {};
463 }
464 return worker_thread_->Invoke<std::vector<RtpSource>>(
465 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
466}
467
deadbeef70ab1a12015-09-28 16:53:55 -0700468} // namespace webrtc