blob: 4968ac1d678c2909aaf3ab4699f036c4a9d62148 [file] [log] [blame]
deadbeef6979b022015-09-24 23:47:531/*
kjellanderb24317b2016-02-10 15:54:432 * Copyright 2015 The WebRTC project authors. All Rights Reserved.
deadbeef6979b022015-09-24 23:47:533 *
kjellanderb24317b2016-02-10 15:54:434 * 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 23:47:539 */
10
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "pc/rtpreceiver.h"
deadbeef6979b022015-09-24 23:47:5312
Henrik Boström9e6fd2b2017-11-21 12:41:5113#include <utility>
Steve Anton36b29d12017-10-30 16:57:4214#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#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 23:53:5521
22namespace webrtc {
23
Henrik Boström9e6fd2b2017-11-21 12:41:5124AudioRtpReceiver::AudioRtpReceiver(
Steve Anton9158ef62017-11-27 21:01:5225 const std::string& receiver_id,
Henrik Boström9e6fd2b2017-11-21 12:41:5126 std::vector<rtc::scoped_refptr<MediaStreamInterface>> streams,
27 uint32_t ssrc,
28 cricket::VoiceChannel* channel)
Steve Anton9158ef62017-11-27 21:01:5229 : id_(receiver_id),
deadbeef70ab1a12015-09-28 23:53:5530 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 23:30:3531 channel_(channel),
perkjd61bf802016-03-24 10:16:1932 track_(AudioTrackProxy::Create(
33 rtc::Thread::Current(),
Steve Anton9158ef62017-11-27 21:01:5234 AudioTrack::Create(receiver_id,
Taylor Brandstetterba29c6a2016-06-27 23:30:3535 RemoteAudioSource::Create(ssrc, channel)))),
Henrik Boström9e6fd2b2017-11-21 12:41:5136 streams_(std::move(streams)),
perkjd61bf802016-03-24 10:16:1937 cached_track_enabled_(track_->enabled()) {
tommi6eca7e32015-12-15 12:27:1138 RTC_DCHECK(track_->GetSource()->remote());
deadbeef70ab1a12015-09-28 23:53:5539 track_->RegisterObserver(this);
40 track_->GetSource()->RegisterAudioObserver(this);
41 Reconfigure();
Taylor Brandstetterba29c6a2016-06-27 23:30:3542 if (channel_) {
43 channel_->SignalFirstPacketReceived.connect(
44 this, &AudioRtpReceiver::OnFirstPacketReceived);
45 }
deadbeef70ab1a12015-09-28 23:53:5546}
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-10 00:22:0162 RTC_DCHECK_GE(volume, 0);
63 RTC_DCHECK_LE(volume, 10);
Taylor Brandstetterba29c6a2016-06-27 23:30:3564 cached_volume_ = volume;
65 if (!channel_) {
Mirko Bonadei675513b2017-11-09 10:09:2566 RTC_LOG(LS_ERROR)
67 << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
Taylor Brandstetterba29c6a2016-06-27 23:30:3568 return;
69 }
deadbeef70ab1a12015-09-28 23:53:5570 // 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 23:30:3573 if (!stopped_ && track_->enabled()) {
74 if (!channel_->SetOutputVolume(ssrc_, cached_volume_)) {
nisseeb4ca4e2017-01-12 10:24:2775 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 23:30:3576 }
77 }
deadbeef70ab1a12015-09-28 23:53:5578}
79
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3080RtpParameters AudioRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 23:30:3581 if (!channel_ || stopped_) {
82 return RtpParameters();
83 }
84 return channel_->GetRtpReceiveParameters(ssrc_);
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3085}
86
87bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
88 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 23:30:3589 if (!channel_ || stopped_) {
90 return false;
91 }
92 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
Taylor Brandstetterdb0cd9e2016-05-16 18:40:3093}
94
deadbeefa601f5c2016-06-06 21:27:3995void AudioRtpReceiver::Stop() {
96 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 23:30:3597 if (stopped_) {
deadbeefa601f5c2016-06-06 21:27:3998 return;
99 }
Taylor Brandstetterba29c6a2016-06-27 23:30:35100 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 21:27:39106}
107
hbos8d609f62017-04-10 14:39:05108std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
109 return channel_->GetSources(ssrc_);
110}
111
deadbeef70ab1a12015-09-28 23:53:55112void AudioRtpReceiver::Reconfigure() {
Taylor Brandstetterba29c6a2016-06-27 23:30:35113 RTC_DCHECK(!stopped_);
114 if (!channel_) {
Mirko Bonadei675513b2017-11-09 10:09:25115 RTC_LOG(LS_ERROR)
116 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
deadbeef70ab1a12015-09-28 23:53:55117 return;
118 }
Taylor Brandstetterba29c6a2016-06-27 23:30:35119 if (!channel_->SetOutputVolume(ssrc_,
120 track_->enabled() ? cached_volume_ : 0)) {
nisseeb4ca4e2017-01-12 10:24:27121 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 23:30:35122 }
deadbeef70ab1a12015-09-28 23:53:55123}
124
zhihuang184a3fd2016-06-14 18:47:14125void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
126 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 23:30:35127 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 18:36:40128 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 18:47:14129 observer_->OnFirstPacketReceived(media_type());
130 }
131}
132
Taylor Brandstetterba29c6a2016-06-27 23:30:35133void 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 18:47:14145 if (observer_) {
146 observer_->OnFirstPacketReceived(media_type());
147 }
148 received_first_packet_ = true;
149}
150
Henrik Boström9e6fd2b2017-11-21 12:41:51151VideoRtpReceiver::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 17:32:00157 : id_(track_id),
158 ssrc_(ssrc),
Taylor Brandstetterba29c6a2016-06-27 23:30:35159 channel_(channel),
perkjf0dcfe22016-03-10 17:32:00160 source_(new RefCountedObject<VideoTrackSource>(&broadcaster_,
perkjf0dcfe22016-03-10 17:32:00161 true /* remote */)),
162 track_(VideoTrackProxy::Create(
163 rtc::Thread::Current(),
nisse5b68ab52016-04-07 14:45:54164 worker_thread,
165 VideoTrack::Create(
166 track_id,
167 VideoTrackSourceProxy::Create(rtc::Thread::Current(),
168 worker_thread,
perkj773be362017-08-01 06:22:01169 source_),
Henrik Boström9e6fd2b2017-11-21 12:41:51170 worker_thread))),
171 streams_(std::move(streams)) {
perkjf0dcfe22016-03-10 17:32:00172 source_->SetState(MediaSourceInterface::kLive);
Taylor Brandstetterba29c6a2016-06-27 23:30:35173 if (!channel_) {
Mirko Bonadei675513b2017-11-09 10:09:25174 RTC_LOG(LS_ERROR)
Taylor Brandstetterba29c6a2016-06-27 23:30:35175 << "VideoRtpReceiver::VideoRtpReceiver: No video channel exists.";
176 } else {
177 if (!channel_->SetSink(ssrc_, &broadcaster_)) {
nisseeb4ca4e2017-01-12 10:24:27178 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 23:30:35179 }
180 }
Taylor Brandstetterba29c6a2016-06-27 23:30:35181 if (channel_) {
182 channel_->SignalFirstPacketReceived.connect(
183 this, &VideoRtpReceiver::OnFirstPacketReceived);
184 }
deadbeef70ab1a12015-09-28 23:53:55185}
186
187VideoRtpReceiver::~VideoRtpReceiver() {
188 // Since cricket::VideoRenderer is not reference counted,
Taylor Brandstetterba29c6a2016-06-27 23:30:35189 // we need to remove it from the channel before we are deleted.
deadbeef70ab1a12015-09-28 23:53:55190 Stop();
191}
192
deadbeefa601f5c2016-06-06 21:27:39193RtpParameters VideoRtpReceiver::GetParameters() const {
Taylor Brandstetterba29c6a2016-06-27 23:30:35194 if (!channel_ || stopped_) {
195 return RtpParameters();
196 }
197 return channel_->GetRtpReceiveParameters(ssrc_);
deadbeefa601f5c2016-06-06 21:27:39198}
199
200bool VideoRtpReceiver::SetParameters(const RtpParameters& parameters) {
201 TRACE_EVENT0("webrtc", "VideoRtpReceiver::SetParameters");
Taylor Brandstetterba29c6a2016-06-27 23:30:35202 if (!channel_ || stopped_) {
203 return false;
204 }
205 return channel_->SetRtpReceiveParameters(ssrc_, parameters);
deadbeefa601f5c2016-06-06 21:27:39206}
207
deadbeef70ab1a12015-09-28 23:53:55208void VideoRtpReceiver::Stop() {
209 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
Taylor Brandstetterba29c6a2016-06-27 23:30:35210 if (stopped_) {
deadbeef70ab1a12015-09-28 23:53:55211 return;
212 }
perkjf0dcfe22016-03-10 17:32:00213 source_->SetState(MediaSourceInterface::kEnded);
214 source_->OnSourceDestroyed();
Taylor Brandstetterba29c6a2016-06-27 23:30:35215 if (!channel_) {
Mirko Bonadei675513b2017-11-09 10:09:25216 RTC_LOG(LS_WARNING) << "VideoRtpReceiver::Stop: No video channel exists.";
Taylor Brandstetterba29c6a2016-06-27 23:30:35217 } 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 23:53:55223}
224
zhihuang184a3fd2016-06-14 18:47:14225void VideoRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
226 observer_ = observer;
Taylor Brandstetterba29c6a2016-06-27 23:30:35227 // Deliver any notifications the observer may have missed by being set late.
zhihuangc4adabf2016-12-07 18:36:40228 if (received_first_packet_ && observer_) {
zhihuang184a3fd2016-06-14 18:47:14229 observer_->OnFirstPacketReceived(media_type());
230 }
231}
232
Taylor Brandstetterba29c6a2016-06-27 23:30:35233void 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 10:24:27241 RTC_NOTREACHED();
Taylor Brandstetterba29c6a2016-06-27 23:30:35242 }
243 channel_->SignalFirstPacketReceived.connect(
244 this, &VideoRtpReceiver::OnFirstPacketReceived);
245 }
246}
247
248void VideoRtpReceiver::OnFirstPacketReceived(cricket::BaseChannel* channel) {
zhihuang184a3fd2016-06-14 18:47:14249 if (observer_) {
250 observer_->OnFirstPacketReceived(media_type());
251 }
252 received_first_packet_ = true;
253}
254
deadbeef70ab1a12015-09-28 23:53:55255} // namespace webrtc