blob: c6a2128dfb314ed5a450d217c2fb61e0b350845d [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
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/rtpreceiver.h"
deadbeef6979b022015-09-24 16:47:53 -070012
perkjf0dcfe22016-03-10 18:32:00 +010013#include "webrtc/api/mediastreamtrackproxy.h"
nisse5b68ab52016-04-07 07:45:54 -070014#include "webrtc/api/videosourceproxy.h"
ossu7bb87ee2017-01-23 04:56:25 -080015#include "webrtc/pc/audiotrack.h"
16#include "webrtc/pc/videotrack.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020017#include "webrtc/rtc_base/trace_event.h"
deadbeef70ab1a12015-09-28 16:53:55 -070018
19namespace webrtc {
20
deadbeefe814a0d2017-02-25 18:15:09 -080021AudioRtpReceiver::AudioRtpReceiver(const std::string& track_id,
Peter Boström0c4e06b2015-10-07 12:23:21 +020022 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070023 cricket::VoiceChannel* channel)
perkjd61bf802016-03-24 03:16:19 -070024 : id_(track_id),
deadbeef70ab1a12015-09-28 16:53:55 -070025 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070026 channel_(channel),
perkjd61bf802016-03-24 03:16:19 -070027 track_(AudioTrackProxy::Create(
28 rtc::Thread::Current(),
29 AudioTrack::Create(track_id,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070030 RemoteAudioSource::Create(ssrc, channel)))),
perkjd61bf802016-03-24 03:16:19 -070031 cached_track_enabled_(track_->enabled()) {
tommi6eca7e32015-12-15 04:27:11 -080032 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 16:53:55 -070033 track_->RegisterObserver(this);
34 track_->GetSource()->RegisterAudioObserver(this);
35 Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070036 if (channel_) {
37 channel_->SignalFirstPacketReceived.connect(
38 this, &AudioRtpReceiver::OnFirstPacketReceived);
39 }
deadbeef70ab1a12015-09-28 16:53:55 -070040}
41
42AudioRtpReceiver::~AudioRtpReceiver() {
43 track_->GetSource()->UnregisterAudioObserver(this);
44 track_->UnregisterObserver(this);
45 Stop();
46}
47
48void AudioRtpReceiver::OnChanged() {
49 if (cached_track_enabled_ != track_->enabled()) {
50 cached_track_enabled_ = track_->enabled();
51 Reconfigure();
52 }
53}
54
55void AudioRtpReceiver::OnSetVolume(double volume) {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070056 RTC_DCHECK(volume >= 0 && volume <= 10);
57 cached_volume_ = volume;
58 if (!channel_) {
59 LOG(LS_ERROR) << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
60 return;
61 }
deadbeef70ab1a12015-09-28 16:53:55 -070062 // When the track is disabled, the volume of the source, which is the
63 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
64 // setting the volume to the source when the track is disabled.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070065 if (!stopped_ && track_->enabled()) {
66 if (!channel_->SetOutputVolume(ssrc_, cached_volume_)) {
nisseeb4ca4e2017-01-12 02:24:27 -080067 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070068 }
69 }
deadbeef70ab1a12015-09-28 16:53:55 -070070}
71
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070072RtpParameters AudioRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070073 if (!channel_ || stopped_) {
74 return RtpParameters();
75 }
76 return channel_->GetRtpReceiveParameters(ssrc_);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070077}
78
79bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
80 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070081 if (!channel_ || stopped_) {
82 return false;
83 }
84 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070085}
86
deadbeefa601f5c2016-06-06 14:27:39 -070087void AudioRtpReceiver::Stop() {
88 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070089 if (stopped_) {
deadbeefa601f5c2016-06-06 14:27:39 -070090 return;
91 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -070092 if (channel_) {
93 // Allow that SetOutputVolume fail. This is the normal case when the
94 // underlying media channel has already been deleted.
95 channel_->SetOutputVolume(ssrc_, 0);
96 }
97 stopped_ = true;
deadbeefa601f5c2016-06-06 14:27:39 -070098}
99
hbos8d609f62017-04-10 07:39:05 -0700100std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
101 return channel_->GetSources(ssrc_);
102}
103
deadbeef70ab1a12015-09-28 16:53:55 -0700104void AudioRtpReceiver::Reconfigure() {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700105 RTC_DCHECK(!stopped_);
106 if (!channel_) {
107 LOG(LS_ERROR) << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
deadbeef70ab1a12015-09-28 16:53:55 -0700108 return;
109 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700110 if (!channel_->SetOutputVolume(ssrc_,
111 track_->enabled() ? cached_volume_ : 0)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800112 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700113 }
deadbeef70ab1a12015-09-28 16:53:55 -0700114}
115
zhihuang184a3fd2016-06-14 11:47:14 -0700116void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
117 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700118 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800119 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700120 observer_->OnFirstPacketReceived(media_type());
121 }
122}
123
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700124void AudioRtpReceiver::SetChannel(cricket::VoiceChannel* channel) {
125 if (channel_) {
126 channel_->SignalFirstPacketReceived.disconnect(this);
127 }
128 channel_ = channel;
129 if (channel_) {
130 channel_->SignalFirstPacketReceived.connect(
131 this, &AudioRtpReceiver::OnFirstPacketReceived);
132 }
133}
134
135void AudioRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 11:47:14 -0700136 if (observer_) {
137 observer_->OnFirstPacketReceived(media_type());
138 }
139 received_first_packet_ = true;
140}
141
deadbeefe814a0d2017-02-25 18:15:09 -0800142VideoRtpReceiver::VideoRtpReceiver(const std::string& track_id,
perkjf0dcfe22016-03-10 18:32:00 +0100143 rtc::Thread* worker_thread,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200144 uint32_t ssrc,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700145 cricket::VideoChannel* channel)
perkjf0dcfe22016-03-10 18:32:00 +0100146 : id_(track_id),
147 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700148 channel_(channel),
perkjf0dcfe22016-03-10 18:32:00 +0100149 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
perkjf0dcfe22016-03-10 18:32:00 +0100150 true /* remote */)),
151 track_(VideoTrackProxy::Create(
152 rtc::Thread::Current(),
nisse5b68ab52016-04-07 07:45:54 -0700153 worker_thread,
154 VideoTrack::Create(
155 track_id,
156 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
157 worker_thread,
perkj773be362017-07-31 23:22:01 -0700158 source_),
159 worker_thread))) {
perkjf0dcfe22016-03-10 18:32:00 +0100160 source_->SetState(MediaSourceInterface::kLive);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700161 if (!channel_) {
162 LOG(LS_ERROR)
163 << "VideoRtpReceiver::VideoRtpReceiver: No video channel exists.";
164 } else {
165 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800166 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700167 }
168 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700169 if (channel_) {
170 channel_->SignalFirstPacketReceived.connect(
171 this, &VideoRtpReceiver::OnFirstPacketReceived);
172 }
deadbeef70ab1a12015-09-28 16:53:55 -0700173}
174
175VideoRtpReceiver::~VideoRtpReceiver() {
176 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700177 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700178 Stop();
179}
180
deadbeefa601f5c2016-06-06 14:27:39 -0700181RtpParameters VideoRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700182 if (!channel_ || stopped_) {
183 return RtpParameters();
184 }
185 return channel_->GetRtpReceiveParameters(ssrc_);
deadbeefa601f5c2016-06-06 14:27:39 -0700186}
187
188bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
189 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700190 if (!channel_ || stopped_) {
191 return false;
192 }
193 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
deadbeefa601f5c2016-06-06 14:27:39 -0700194}
195
deadbeef70ab1a12015-09-28 16:53:55 -0700196void VideoRtpReceiver::Stop() {
197 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700198 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700199 return;
200 }
perkjf0dcfe22016-03-10 18:32:00 +0100201 source_->SetState(MediaSourceInterface::kEnded);
202 source_->OnSourceDestroyed();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700203 if (!channel_) {
204 LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
205 } else {
206 // Allow that SetSink fail. This is the normal case when the underlying
207 // media channel has already been deleted.
208 channel_->SetSink(ssrc_, nullptr);
209 }
210 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700211}
212
zhihuang184a3fd2016-06-14 11:47:14 -0700213void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
214 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700215 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800216 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700217 observer_->OnFirstPacketReceived(media_type());
218 }
219}
220
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700221void VideoRtpReceiver::SetChannel(cricket::VideoChannel* channel) {
222 if (channel_) {
223 channel_->SignalFirstPacketReceived.disconnect(this);
224 channel_->SetSink(ssrc_, nullptr);
225 }
226 channel_ = channel;
227 if (channel_) {
228 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800229 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700230 }
231 channel_->SignalFirstPacketReceived.connect(
232 this, &VideoRtpReceiver::OnFirstPacketReceived);
233 }
234}
235
236void VideoRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 11:47:14 -0700237 if (observer_) {
238 observer_->OnFirstPacketReceived(media_type());
239 }
240 received_first_packet_ = true;
241}
242
deadbeef70ab1a12015-09-28 16:53:55 -0700243} // namespace webrtc