blob: 05990f4e03258521b765d4263aaa61ad3d008fc4 [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::SetMediaChannel(
206 cricket::VoiceMediaChannel* media_channel) {
207 media_channel_ = media_channel;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700208}
209
Steve Anton60776752018-01-10 11:51:34 -0800210void AudioRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700211 if (observer_) {
212 observer_->OnFirstPacketReceived(media_type());
213 }
214 received_first_packet_ = true;
215}
216
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100217VideoRtpReceiver::VideoRtpReceiver(
Steve Anton60776752018-01-10 11:51:34 -0800218 rtc::Thread* worker_thread,
Steve Antonef65ef12018-01-10 17:15:20 -0800219 const std::string& receiver_id,
Steve Antond3679212018-01-17 17:41:02 -0800220 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
Steve Anton60776752018-01-10 11:51:34 -0800221 : worker_thread_(worker_thread),
Steve Antonef65ef12018-01-10 17:15:20 -0800222 id_(receiver_id),
perkjf0dcfe22016-03-10 18:32:00 +0100223 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
perkjf0dcfe22016-03-10 18:32:00 +0100224 true /* remote */)),
225 track_(VideoTrackProxy::Create(
226 rtc::Thread::Current(),
nisse5b68ab52016-04-07 07:45:54 -0700227 worker_thread,
228 VideoTrack::Create(
Steve Antonef65ef12018-01-10 17:15:20 -0800229 receiver_id,
nisse5b68ab52016-04-07 07:45:54 -0700230 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
231 worker_thread,
perkj773be362017-07-31 23:22:01 -0700232 source_),
Harald Alvestrandc72af932018-01-11 17:18:19 +0100233 worker_thread))),
234 attachment_id_(GenerateUniqueId()) {
Steve Anton60776752018-01-10 11:51:34 -0800235 RTC_DCHECK(worker_thread_);
Steve Antonef65ef12018-01-10 17:15:20 -0800236 SetStreams(streams);
perkjf0dcfe22016-03-10 18:32:00 +0100237 source_->SetState(MediaSourceInterface::kLive);
deadbeef70ab1a12015-09-28 16:53:55 -0700238}
239
240VideoRtpReceiver::~VideoRtpReceiver() {
241 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700242 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700243 Stop();
244}
245
Steve Anton60776752018-01-10 11:51:34 -0800246bool VideoRtpReceiver::SetSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
247 RTC_DCHECK(media_channel_);
Steve Antond3679212018-01-17 17:41:02 -0800248 RTC_DCHECK(ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800249 return worker_thread_->Invoke<bool>(
Steve Antond3679212018-01-17 17:41:02 -0800250 RTC_FROM_HERE, [&] { return media_channel_->SetSink(*ssrc_, sink); });
Steve Anton60776752018-01-10 11:51:34 -0800251}
252
deadbeefa601f5c2016-06-06 14:27:39 -0700253RtpParameters VideoRtpReceiver::GetParameters() const {
Steve Antond3679212018-01-17 17:41:02 -0800254 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700255 return RtpParameters();
256 }
Steve Anton60776752018-01-10 11:51:34 -0800257 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800258 return media_channel_->GetRtpReceiveParameters(*ssrc_);
Steve Anton60776752018-01-10 11:51:34 -0800259 });
deadbeefa601f5c2016-06-06 14:27:39 -0700260}
261
262bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
263 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Steve Antond3679212018-01-17 17:41:02 -0800264 if (!media_channel_ || !ssrc_ || stopped_) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700265 return false;
266 }
Steve Anton60776752018-01-10 11:51:34 -0800267 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Steve Antond3679212018-01-17 17:41:02 -0800268 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
Steve Anton60776752018-01-10 11:51:34 -0800269 });
deadbeefa601f5c2016-06-06 14:27:39 -0700270}
271
deadbeef70ab1a12015-09-28 16:53:55 -0700272void VideoRtpReceiver::Stop() {
273 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700274 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700275 return;
276 }
perkjf0dcfe22016-03-10 18:32:00 +0100277 source_->SetState(MediaSourceInterface::kEnded);
278 source_->OnSourceDestroyed();
Steve Antond3679212018-01-17 17:41:02 -0800279 if (!media_channel_ || !ssrc_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100280 RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700281 } else {
282 // Allow that SetSink fail. This is the normal case when the underlying
283 // media channel has already been deleted.
Steve Anton60776752018-01-10 11:51:34 -0800284 SetSink(nullptr);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700285 }
286 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700287}
288
Steve Antond3679212018-01-17 17:41:02 -0800289void VideoRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
290 if (!media_channel_) {
291 RTC_LOG(LS_ERROR)
292 << "VideoRtpReceiver::SetupMediaChannel: No video channel exists.";
293 }
294 if (ssrc_ == ssrc) {
295 return;
296 }
297 if (ssrc_) {
298 SetSink(nullptr);
299 }
300 ssrc_ = ssrc;
301 SetSink(&broadcaster_);
302}
303
Steve Antonef65ef12018-01-10 17:15:20 -0800304void VideoRtpReceiver::SetStreams(
305 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
306 // Remove remote track from any streams that are going away.
307 for (auto existing_stream : streams_) {
308 bool removed = true;
309 for (auto stream : streams) {
310 if (existing_stream->label() == stream->label()) {
311 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
312 removed = false;
313 break;
314 }
315 }
316 if (removed) {
317 existing_stream->RemoveTrack(track_);
318 }
319 }
320 // Add remote track to any streams that are new.
321 for (auto stream : streams) {
322 bool added = true;
323 for (auto existing_stream : streams_) {
324 if (stream->label() == existing_stream->label()) {
325 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
326 added = false;
327 break;
328 }
329 }
330 if (added) {
331 stream->AddTrack(track_);
332 }
333 }
334 streams_ = streams;
335}
336
zhihuang184a3fd2016-06-14 11:47:14 -0700337void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
338 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700339 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800340 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700341 observer_->OnFirstPacketReceived(media_type());
342 }
343}
344
Steve Anton60776752018-01-10 11:51:34 -0800345void VideoRtpReceiver::SetMediaChannel(
346 cricket::VideoMediaChannel* media_channel) {
Steve Anton60776752018-01-10 11:51:34 -0800347 media_channel_ = media_channel;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700348}
349
Steve Anton60776752018-01-10 11:51:34 -0800350void VideoRtpReceiver::NotifyFirstPacketReceived() {
zhihuang184a3fd2016-06-14 11:47:14 -0700351 if (observer_) {
352 observer_->OnFirstPacketReceived(media_type());
353 }
354 received_first_packet_ = true;
355}
356
deadbeef70ab1a12015-09-28 16:53:55 -0700357} // namespace webrtc