blob: 4968ac1d678c2909aaf3ab4699f036c4a9d62148 [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
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010024AudioRtpReceiver::AudioRtpReceiver(
Steve Anton9158ef62017-11-27 13:01:52 -080025 const std::string& receiver_id,
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010026 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams,
27 uint32_t ssrc,
28 cricket::VoiceChannel* channel)
Steve Anton9158ef62017-11-27 13:01:52 -080029 : id_(receiver_id),
deadbeef70ab1a12015-09-28 16:53:55 -070030 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070031 channel_(channel),
perkjd61bf802016-03-24 03:16:19 -070032 track_(AudioTrackProxy::Create(
33 rtc::Thread::Current(),
Steve Anton9158ef62017-11-27 13:01:52 -080034 AudioTrack::Create(receiver_id,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070035 RemoteAudioSource::Create(ssrc, channel)))),
Henrik Boström9e6fd2b2017-11-21 13:41:51 +010036 streams_(std::move(streams)),
perkjd61bf802016-03-24 03:16:19 -070037 cached_track_enabled_(track_->enabled()) {
tommi6eca7e32015-12-15 04:27:11 -080038 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 16:53:55 -070039 track_->RegisterObserver(this);
40 track_->GetSource()->RegisterAudioObserver(this);
41 Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070042 if (channel_) {
43 channel_->SignalFirstPacketReceived.connect(
44 this, &AudioRtpReceiver::OnFirstPacketReceived);
45 }
deadbeef70ab1a12015-09-28 16:53:55 -070046}
47
48AudioRtpReceiver::~AudioRtpReceiver() {
49 track_->GetSource()->UnregisterAudioObserver(this);
50 track_->UnregisterObserver(this);
51 Stop();
52}
53
54void AudioRtpReceiver::OnChanged() {
55 if (cached_track_enabled_ != track_->enabled()) {
56 cached_track_enabled_ = track_->enabled();
57 Reconfigure();
58 }
59}
60
61void AudioRtpReceiver::OnSetVolume(double volume) {
kwibergee89e782017-08-09 17:22:01 -070062 RTC_DCHECK_GE(volume, 0);
63 RTC_DCHECK_LE(volume, 10);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070064 cached_volume_ = volume;
65 if (!channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010066 RTC_LOG(LS_ERROR)
67 << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070068 return;
69 }
deadbeef70ab1a12015-09-28 16:53:55 -070070 // When the track is disabled, the volume of the source, which is the
71 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
72 // setting the volume to the source when the track is disabled.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070073 if (!stopped_ && track_->enabled()) {
74 if (!channel_->SetOutputVolume(ssrc_, cached_volume_)) {
nisseeb4ca4e2017-01-12 02:24:27 -080075 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070076 }
77 }
deadbeef70ab1a12015-09-28 16:53:55 -070078}
79
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070080RtpParameters AudioRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070081 if (!channel_ || stopped_) {
82 return RtpParameters();
83 }
84 return channel_->GetRtpReceiveParameters(ssrc_);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070085}
86
87bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
88 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070089 if (!channel_ || stopped_) {
90 return false;
91 }
92 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070093}
94
deadbeefa601f5c2016-06-06 14:27:39 -070095void AudioRtpReceiver::Stop() {
96 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070097 if (stopped_) {
deadbeefa601f5c2016-06-06 14:27:39 -070098 return;
99 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700100 if (channel_) {
101 // Allow that SetOutputVolume fail. This is the normal case when the
102 // underlying media channel has already been deleted.
103 channel_->SetOutputVolume(ssrc_, 0);
104 }
105 stopped_ = true;
deadbeefa601f5c2016-06-06 14:27:39 -0700106}
107
hbos8d609f62017-04-10 07:39:05 -0700108std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
109 return channel_->GetSources(ssrc_);
110}
111
deadbeef70ab1a12015-09-28 16:53:55 -0700112void AudioRtpReceiver::Reconfigure() {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700113 RTC_DCHECK(!stopped_);
114 if (!channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100115 RTC_LOG(LS_ERROR)
116 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
deadbeef70ab1a12015-09-28 16:53:55 -0700117 return;
118 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700119 if (!channel_->SetOutputVolume(ssrc_,
120 track_->enabled() ? cached_volume_ : 0)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800121 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700122 }
deadbeef70ab1a12015-09-28 16:53:55 -0700123}
124
zhihuang184a3fd2016-06-14 11:47:14 -0700125void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
126 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700127 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800128 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700129 observer_->OnFirstPacketReceived(media_type());
130 }
131}
132
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700133void AudioRtpReceiver::SetChannel(cricket::VoiceChannel* channel) {
134 if (channel_) {
135 channel_->SignalFirstPacketReceived.disconnect(this);
136 }
137 channel_ = channel;
138 if (channel_) {
139 channel_->SignalFirstPacketReceived.connect(
140 this, &AudioRtpReceiver::OnFirstPacketReceived);
141 }
142}
143
144void AudioRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 11:47:14 -0700145 if (observer_) {
146 observer_->OnFirstPacketReceived(media_type());
147 }
148 received_first_packet_ = true;
149}
150
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100151VideoRtpReceiver::VideoRtpReceiver(
152 const std::string& track_id,
153 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams,
154 rtc::Thread* worker_thread,
155 uint32_t ssrc,
156 cricket::VideoChannel* channel)
perkjf0dcfe22016-03-10 18:32:00 +0100157 : id_(track_id),
158 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700159 channel_(channel),
perkjf0dcfe22016-03-10 18:32:00 +0100160 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
perkjf0dcfe22016-03-10 18:32:00 +0100161 true /* remote */)),
162 track_(VideoTrackProxy::Create(
163 rtc::Thread::Current(),
nisse5b68ab52016-04-07 07:45:54 -0700164 worker_thread,
165 VideoTrack::Create(
166 track_id,
167 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
168 worker_thread,
perkj773be362017-07-31 23:22:01 -0700169 source_),
Henrik Boström9e6fd2b2017-11-21 13:41:51 +0100170 worker_thread))),
171 streams_(std::move(streams)) {
perkjf0dcfe22016-03-10 18:32:00 +0100172 source_->SetState(MediaSourceInterface::kLive);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700173 if (!channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100174 RTC_LOG(LS_ERROR)
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700175 << "VideoRtpReceiver::VideoRtpReceiver: No video channel exists.";
176 } else {
177 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800178 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700179 }
180 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700181 if (channel_) {
182 channel_->SignalFirstPacketReceived.connect(
183 this, &VideoRtpReceiver::OnFirstPacketReceived);
184 }
deadbeef70ab1a12015-09-28 16:53:55 -0700185}
186
187VideoRtpReceiver::~VideoRtpReceiver() {
188 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700189 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700190 Stop();
191}
192
deadbeefa601f5c2016-06-06 14:27:39 -0700193RtpParameters VideoRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700194 if (!channel_ || stopped_) {
195 return RtpParameters();
196 }
197 return channel_->GetRtpReceiveParameters(ssrc_);
deadbeefa601f5c2016-06-06 14:27:39 -0700198}
199
200bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
201 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700202 if (!channel_ || stopped_) {
203 return false;
204 }
205 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
deadbeefa601f5c2016-06-06 14:27:39 -0700206}
207
deadbeef70ab1a12015-09-28 16:53:55 -0700208void VideoRtpReceiver::Stop() {
209 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700210 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700211 return;
212 }
perkjf0dcfe22016-03-10 18:32:00 +0100213 source_->SetState(MediaSourceInterface::kEnded);
214 source_->OnSourceDestroyed();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700215 if (!channel_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100216 RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700217 } else {
218 // Allow that SetSink fail. This is the normal case when the underlying
219 // media channel has already been deleted.
220 channel_->SetSink(ssrc_, nullptr);
221 }
222 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700223}
224
zhihuang184a3fd2016-06-14 11:47:14 -0700225void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
226 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700227 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800228 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700229 observer_->OnFirstPacketReceived(media_type());
230 }
231}
232
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700233void VideoRtpReceiver::SetChannel(cricket::VideoChannel* channel) {
234 if (channel_) {
235 channel_->SignalFirstPacketReceived.disconnect(this);
236 channel_->SetSink(ssrc_, nullptr);
237 }
238 channel_ = channel;
239 if (channel_) {
240 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800241 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700242 }
243 channel_->SignalFirstPacketReceived.connect(
244 this, &VideoRtpReceiver::OnFirstPacketReceived);
245 }
246}
247
248void VideoRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 11:47:14 -0700249 if (observer_) {
250 observer_->OnFirstPacketReceived(media_type());
251 }
252 received_first_packet_ = true;
253}
254
deadbeef70ab1a12015-09-28 16:53:55 -0700255} // namespace webrtc