blob: 96974ee9f74002ce998f4a6c1dc45a26e4081cd9 [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"
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -070015#include "webrtc/base/trace_event.h"
ossu7bb87ee2017-01-23 04:56:25 -080016#include "webrtc/pc/audiotrack.h"
17#include "webrtc/pc/videotrack.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,
158 source_)))) {
perkjf0dcfe22016-03-10 18:32:00 +0100159 source_->SetState(MediaSourceInterface::kLive);
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700160 if (!channel_) {
161 LOG(LS_ERROR)
162 << "VideoRtpReceiver::VideoRtpReceiver: No video channel exists.";
163 } else {
164 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800165 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700166 }
167 }
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700168 if (channel_) {
169 channel_->SignalFirstPacketReceived.connect(
170 this, &VideoRtpReceiver::OnFirstPacketReceived);
171 }
deadbeef70ab1a12015-09-28 16:53:55 -0700172}
173
174VideoRtpReceiver::~VideoRtpReceiver() {
175 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700176 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 16:53:55 -0700177 Stop();
178}
179
deadbeefa601f5c2016-06-06 14:27:39 -0700180RtpParameters VideoRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700181 if (!channel_ || stopped_) {
182 return RtpParameters();
183 }
184 return channel_->GetRtpReceiveParameters(ssrc_);
deadbeefa601f5c2016-06-06 14:27:39 -0700185}
186
187bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
188 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700189 if (!channel_ || stopped_) {
190 return false;
191 }
192 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
deadbeefa601f5c2016-06-06 14:27:39 -0700193}
194
deadbeef70ab1a12015-09-28 16:53:55 -0700195void VideoRtpReceiver::Stop() {
196 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700197 if (stopped_) {
deadbeef70ab1a12015-09-28 16:53:55 -0700198 return;
199 }
perkjf0dcfe22016-03-10 18:32:00 +0100200 source_->SetState(MediaSourceInterface::kEnded);
201 source_->OnSourceDestroyed();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700202 if (!channel_) {
203 LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
204 } else {
205 // Allow that SetSink fail. This is the normal case when the underlying
206 // media channel has already been deleted.
207 channel_->SetSink(ssrc_, nullptr);
208 }
209 stopped_ = true;
deadbeef70ab1a12015-09-28 16:53:55 -0700210}
211
zhihuang184a3fd2016-06-14 11:47:14 -0700212void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
213 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700214 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 10:36:40 -0800215 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 11:47:14 -0700216 observer_->OnFirstPacketReceived(media_type());
217 }
218}
219
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700220void VideoRtpReceiver::SetChannel(cricket::VideoChannel* channel) {
221 if (channel_) {
222 channel_->SignalFirstPacketReceived.disconnect(this);
223 channel_->SetSink(ssrc_, nullptr);
224 }
225 channel_ = channel;
226 if (channel_) {
227 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 02:24:27 -0800228 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 16:30:35 -0700229 }
230 channel_->SignalFirstPacketReceived.connect(
231 this, &VideoRtpReceiver::OnFirstPacketReceived);
232 }
233}
234
235void VideoRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 11:47:14 -0700236 if (observer_) {
237 observer_->OnFirstPacketReceived(media_type());
238 }
239 received_first_packet_ = true;
240}
241
deadbeef70ab1a12015-09-28 16:53:55 -0700242} // namespace webrtc