blob: bf0b1d68da0262b67aba098b391dc6dd67a3f9dd [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/mediastreamtrackproxy.h"
17#include "api/videosourceproxy.h"
18#include "pc/audiotrack.h"
19#include "pc/videotrack.h"
20#include "rtc_base/trace_event.h"
deadbeef70ab1a12015-09-28 16:53:55 -070021
22namespace webrtc {
23
Harald Alvestrandc72af932018-01-11 17:18:19 +010024namespace {
25
26// This function is only expected to be called on the signalling thread.
27int GenerateUniqueId() {
28 static int g_unique_id = 0;
29
30 return ++g_unique_id;
31}
32
33} // namespace
34
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010035AudioRtpReceiver::AudioRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -080036 rtc::Thread* worker_thread,
Steve Anton9158ef62017-11-27 13:01:52 -080037 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -080038 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
Steve Anton60776752018-01-10 11:51:34 -080039 : worker_thread_(worker_thread),
40 id_(receiver_id),
Steve Antond3679212018-01-17 17:41:02 -080041 source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)),
42 track_(AudioTrackProxy::Create(rtc::Thread::Current(),
43 AudioTrack::Create(receiver_id, source_))),
Harald Alvestrandc72af932018-01-11 17:18:19 +010044 cached_track_enabled_(track_->enabled()),
45 attachment_id_(GenerateUniqueId()) {
Steve Anton60776752018-01-10 11:51:34 -080046 RTC_DCHECK(worker_thread_);
tommi6eca7e32015-12-15 04:27:11 -080047 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 16:53:55 -070048 track_->RegisterObserver(this);
49 track_->GetSource()->RegisterAudioObserver(this);
Steve Antonef65ef12018-01-10 17:15:20 -080050 SetStreams(streams);
deadbeef70ab1a12015-09-28 16:53:55 -070051}
52
53AudioRtpReceiver::~AudioRtpReceiver() {
54 track_->GetSource()->UnregisterAudioObserver(this);
55 track_->UnregisterObserver(this);
56 Stop();
57}
58
59void AudioRtpReceiver::OnChanged() {
60 if (cached_track_enabled_ != track_->enabled()) {
61 cached_track_enabled_ = track_->enabled();
62 Reconfigure();
63 }
64}
65
Steve Anton60776752018-01-10 11:51:34 -080066bool AudioRtpReceiver::SetOutputVolume(double volume) {
67 RTC_DCHECK_GE(volume, 0.0);
68 RTC_DCHECK_LE(volume, 10.0);
69 RTC_DCHECK(media_channel_);
Steve Antond3679212018-01-17 17:41:02 -080070 RTC_DCHECK(ssrc_);
Steve Anton60776752018-01-10 11:51:34 -080071 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -080072 return media_channel_->SetOutputVolume(*ssrc_, volume);
Steve Anton60776752018-01-10 11:51:34 -080073 });
74}
75
deadbeef70ab1a12015-09-28 16:53:55 -070076void AudioRtpReceiver::OnSetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070077 RTC_DCHECK_GE(volume, 0);
78 RTC_DCHECK_LE(volume, 10);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070079 cached_volume_ = volume;
Steve Antond3679212018-01-17 17:41:02 -080080 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010081 RTC_LOG(LS_ERROR)
82 << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070083 return;
84 }
deadbeef70ab1a12015-09-28 16:53:55 -070085 // When the track is disabled, the volume of the source, which is the
86 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
87 // setting the volume to the source when the track is disabled.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070088 if (!stopped_ && track_->enabled()) {
Steve Anton60776752018-01-10 11:51:34 -080089 if (!SetOutputVolume(cached_volume_)) {
nisseeb4ca4e2017-01-12 02:24:27 -080090 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070091 }
92 }
deadbeef70ab1a12015-09-28 16:53:55 -070093}
94
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070095RtpParameters AudioRtpReceiver::GetParameters() const {
Steve Antond3679212018-01-17 17:41:02 -080096 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070097 return RtpParameters();
98 }
Steve Anton60776752018-01-10 11:51:34 -080099 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800100 return media_channel_->GetRtpReceiveParameters(*ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800101 });
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700102}
103
104bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
105 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Steve Antond3679212018-01-17 17:41:02 -0800106 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700107 return false;
108 }
Steve Anton60776752018-01-10 11:51:34 -0800109 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800110 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
Steve Anton60776752018-01-10 11:51:34 -0800111 });
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -0700112}
113
deadbeefa601f5c2016-06-06 14:27:39 -0700114void AudioRtpReceiver::Stop() {
115 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700116 if (stopped_) {
deadbeefa601f5c2016-06-06 14:27:39 -0700117 return;
118 }
Steve Antond3679212018-01-17 17:41:02 -0800119 if (media_channel_ && ssrc_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700120 // Allow that SetOutputVolume fail. This is the normal case when the
121 // underlying media channel has already been deleted.
Steve Anton60776752018-01-10 11:51:34 -0800122 SetOutputVolume(0.0);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700123 }
124 stopped_ = true;
deadbeefa601f5c2016-06-06 14:27:39 -0700125}
126
Steve Antond3679212018-01-17 17:41:02 -0800127void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
128 if (!media_channel_) {
129 RTC_LOG(LS_ERROR)
130 << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
131 return;
132 }
133 if (ssrc_ == ssrc) {
134 return;
135 }
136 if (ssrc_) {
137 source_->Stop(media_channel_, *ssrc_);
138 }
139 ssrc_ = ssrc;
140 source_->Start(media_channel_, *ssrc_);
141 Reconfigure();
142}
143
Steve Antonef65ef12018-01-10 17:15:20 -0800144void AudioRtpReceiver::SetStreams(
145 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
146 // Remove remote track from any streams that are going away.
147 for (auto existing_stream : streams_) {
148 bool removed = true;
149 for (auto stream : streams) {
150 if (existing_stream->label() == stream->label()) {
151 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
152 removed = false;
153 break;
154 }
155 }
156 if (removed) {
157 existing_stream->RemoveTrack(track_);
158 }
159 }
160 // Add remote track to any streams that are new.
161 for (auto stream : streams) {
162 bool added = true;
163 for (auto existing_stream : streams_) {
164 if (stream->label() == existing_stream->label()) {
165 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
166 added = false;
167 break;
168 }
169 }
170 if (added) {
171 stream->AddTrack(track_);
172 }
173 }
174 streams_ = streams;
175}
176
hbos8d609f62017-04-10 07:39:05 -0700177std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
Steve Antond3679212018-01-17 17:41:02 -0800178 if (!media_channel_ || !ssrc_ || stopped_) {
179 return {};
180 }
Steve Anton60776752018-01-10 11:51:34 -0800181 return worker_thread_->Invoke<std::vector<RtpSource>>(
Steve Antond3679212018-01-17 17:41:02 -0800182 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
hbos8d609f62017-04-10 07:39:05 -0700183}
184
deadbeef70ab1a12015-09-28 16:53:55 -0700185void AudioRtpReceiver::Reconfigure() {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700186 RTC_DCHECK(!stopped_);
Steve Antond3679212018-01-17 17:41:02 -0800187 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100188 RTC_LOG(LS_ERROR)
189 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
deadbeef70ab1a12015-09-28 16:53:55 -0700190 return;
191 }
Steve Anton60776752018-01-10 11:51:34 -0800192 if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800193 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700194 }
deadbeef70ab1a12015-09-28 16:53:55 -0700195}
196
zhihuang184a3fd2016-06-14 11:47:14 -0700197void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
198 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700199 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800200 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700201 observer_->OnFirstPacketReceived(media_type());
202 }
203}
204
Steve Anton60776752018-01-10 11:51:34 -0800205void AudioRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700206 if (observer_) {
207 observer_->OnFirstPacketReceived(media_type());
208 }
209 received_first_packet_ = true;
210}
211
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100212VideoRtpReceiver::VideoRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -0800213 rtc::Thread* worker_thread,
Steve Antonef65ef12018-01-10 17:15:20 -0800214 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -0800215 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
Steve Anton60776752018-01-10 11:51:34 -0800216 : worker_thread_(worker_thread),
Steve Antonef65ef12018-01-10 17:15:20 -0800217 id_(receiver_id),
perkjf0dcfe22016-03-10 18:32:00 +0100218 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
perkjf0dcfe22016-03-10 18:32:00 +0100219 true /* remote */)),
220 track_(VideoTrackProxy::Create(
221 rtc::Thread::Current(),
nisse5b68ab52016-04-07 07:45:54 -0700222 worker_thread,
223 VideoTrack::Create(
Steve Antonef65ef12018-01-10 17:15:20 -0800224 receiver_id,
nisse5b68ab52016-04-07 07:45:54 -0700225 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
226 worker_thread,
perkj773be362017-07-31 23:22:01 -0700227 source_),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100228 worker_thread))),
229 attachment_id_(GenerateUniqueId()) {
Steve Anton60776752018-01-10 11:51:34 -0800230 RTC_DCHECK(worker_thread_);
Steve Antonef65ef12018-01-10 17:15:20 -0800231 SetStreams(streams);
perkjf0dcfe22016-03-10 18:32:00 +0100232 source_->SetState(MediaSourceInterface::kLive);
deadbeef70ab1a12015-09-28 16:53:55 -0700233}
234
235VideoRtpReceiver::~VideoRtpReceiver() {
236 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700237 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700238 Stop();
239}
240
Steve Anton60776752018-01-10 11:51:34 -0800241bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
242 RTC_DCHECK(media_channel_);
Steve Antond3679212018-01-17 17:41:02 -0800243 RTC_DCHECK(ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800244 return worker_thread_->Invoke<bool>(
Steve Antond3679212018-01-17 17:41:02 -0800245 RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); });
Steve Anton60776752018-01-10 11:51:34 -0800246}
247
deadbeefa601f5c2016-06-06 14:27:39 -0700248RtpParameters VideoRtpReceiver::GetParameters() const {
Steve Antond3679212018-01-17 17:41:02 -0800249 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700250 return RtpParameters();
251 }
Steve Anton60776752018-01-10 11:51:34 -0800252 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800253 return media_channel_->GetRtpReceiveParameters(*ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800254 });
deadbeefa601f5c2016-06-06 14:27:39 -0700255}
256
257bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
258 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Steve Antond3679212018-01-17 17:41:02 -0800259 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700260 return false;
261 }
Steve Anton60776752018-01-10 11:51:34 -0800262 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800263 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
Steve Anton60776752018-01-10 11:51:34 -0800264 });
deadbeefa601f5c2016-06-06 14:27:39 -0700265}
266
deadbeef70ab1a12015-09-28 16:53:55 -0700267void VideoRtpReceiver::Stop() {
268 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700269 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700270 return;
271 }
perkjf0dcfe22016-03-10 18:32:00 +0100272 source_->SetState(MediaSourceInterface::kEnded);
273 source_->OnSourceDestroyed();
Steve Antond3679212018-01-17 17:41:02 -0800274 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100275 RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700276 } else {
277 // Allow that SetSink fail. This is the normal case when the underlying
278 // media channel has already been deleted.
Steve Anton60776752018-01-10 11:51:34 -0800279 SetSink(nullptr);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700280 }
281 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700282}
283
Steve Antond3679212018-01-17 17:41:02 -0800284void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
285 if (!media_channel_) {
286 RTC_LOG(LS_ERROR)
287 << "VideoRtpReceiver::SetupMediaChannel: No video channel exists.";
288 }
289 if (ssrc_ == ssrc) {
290 return;
291 }
292 if (ssrc_) {
293 SetSink(nullptr);
294 }
295 ssrc_ = ssrc;
296 SetSink(&broadcaster_);
297}
298
Steve Antonef65ef12018-01-10 17:15:20 -0800299void VideoRtpReceiver::SetStreams(
300 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
301 // Remove remote track from any streams that are going away.
302 for (auto existing_stream : streams_) {
303 bool removed = true;
304 for (auto stream : streams) {
305 if (existing_stream->label() == stream->label()) {
306 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
307 removed = false;
308 break;
309 }
310 }
311 if (removed) {
312 existing_stream->RemoveTrack(track_);
313 }
314 }
315 // Add remote track to any streams that are new.
316 for (auto stream : streams) {
317 bool added = true;
318 for (auto existing_stream : streams_) {
319 if (stream->label() == existing_stream->label()) {
320 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
321 added = false;
322 break;
323 }
324 }
325 if (added) {
326 stream->AddTrack(track_);
327 }
328 }
329 streams_ = streams;
330}
331
zhihuang184a3fd2016-06-14 11:47:14 -0700332void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
333 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700334 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800335 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700336 observer_->OnFirstPacketReceived(media_type());
337 }
338}
339
Steve Anton60776752018-01-10 11:51:34 -0800340void VideoRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700341 if (observer_) {
342 observer_->OnFirstPacketReceived(media_type());
343 }
344 received_first_packet_ = true;
345}
346
deadbeef70ab1a12015-09-28 16:53:55 -0700347} // namespace webrtc