blob: e83e558c7b20f2b1a7c0cda207cdb8ee86e525dd [file] [log] [blame]
Ruslan Burakov501bfba2019-02-11 10:29:19 +01001/*
2 * Copyright 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
11#include "pc/audio_rtp_receiver.h"
12
13#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Ruslan Burakov501bfba2019-02-11 10:29:19 +010015#include <utility>
16#include <vector>
17
18#include "api/media_stream_proxy.h"
19#include "api/media_stream_track_proxy.h"
20#include "pc/audio_track.h"
Ruslan Burakov428dcb22019-04-18 17:49:49 +020021#include "pc/jitter_buffer_delay.h"
22#include "pc/jitter_buffer_delay_proxy.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010023#include "pc/media_stream.h"
24#include "rtc_base/checks.h"
25#include "rtc_base/location.h"
26#include "rtc_base/logging.h"
27#include "rtc_base/trace_event.h"
28
29namespace webrtc {
30
31AudioRtpReceiver::AudioRtpReceiver(rtc::Thread* worker_thread,
32 std::string receiver_id,
33 std::vector<std::string> stream_ids)
34 : AudioRtpReceiver(worker_thread,
35 receiver_id,
36 CreateStreamsFromIds(std::move(stream_ids))) {}
37
38AudioRtpReceiver::AudioRtpReceiver(
39 rtc::Thread* worker_thread,
40 const std::string& receiver_id,
41 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams)
42 : worker_thread_(worker_thread),
43 id_(receiver_id),
44 source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)),
45 track_(AudioTrackProxy::Create(rtc::Thread::Current(),
46 AudioTrack::Create(receiver_id, source_))),
47 cached_track_enabled_(track_->enabled()),
Ruslan Burakov428dcb22019-04-18 17:49:49 +020048 attachment_id_(GenerateUniqueId()),
49 delay_(JitterBufferDelayProxy::Create(
50 rtc::Thread::Current(),
51 worker_thread_,
52 new rtc::RefCountedObject<JitterBufferDelay>(worker_thread))) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010053 RTC_DCHECK(worker_thread_);
54 RTC_DCHECK(track_->GetSource()->remote());
55 track_->RegisterObserver(this);
56 track_->GetSource()->RegisterAudioObserver(this);
57 SetStreams(streams);
58}
59
60AudioRtpReceiver::~AudioRtpReceiver() {
61 track_->GetSource()->UnregisterAudioObserver(this);
62 track_->UnregisterObserver(this);
63 Stop();
64}
65
66void AudioRtpReceiver::OnChanged() {
67 if (cached_track_enabled_ != track_->enabled()) {
68 cached_track_enabled_ = track_->enabled();
69 Reconfigure();
70 }
71}
72
73bool AudioRtpReceiver::SetOutputVolume(double volume) {
74 RTC_DCHECK_GE(volume, 0.0);
75 RTC_DCHECK_LE(volume, 10.0);
76 RTC_DCHECK(media_channel_);
Saurav Das7262fc22019-09-11 16:23:05 -070077 RTC_DCHECK(!stopped_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010078 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Saurav Das749f6602019-12-04 09:31:36 -080079 return ssrc_ ? media_channel_->SetOutputVolume(*ssrc_, volume)
80 : media_channel_->SetDefaultOutputVolume(volume);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010081 });
82}
83
84void AudioRtpReceiver::OnSetVolume(double volume) {
85 RTC_DCHECK_GE(volume, 0);
86 RTC_DCHECK_LE(volume, 10);
87 cached_volume_ = volume;
Saurav Das7262fc22019-09-11 16:23:05 -070088 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010089 RTC_LOG(LS_ERROR)
90 << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
91 return;
92 }
93 // When the track is disabled, the volume of the source, which is the
94 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
95 // setting the volume to the source when the track is disabled.
96 if (!stopped_ && track_->enabled()) {
97 if (!SetOutputVolume(cached_volume_)) {
98 RTC_NOTREACHED();
99 }
100 }
101}
102
103std::vector<std::string> AudioRtpReceiver::stream_ids() const {
104 std::vector<std::string> stream_ids(streams_.size());
105 for (size_t i = 0; i < streams_.size(); ++i)
106 stream_ids[i] = streams_[i]->id();
107 return stream_ids;
108}
109
110RtpParameters AudioRtpReceiver::GetParameters() const {
Saurav Das7262fc22019-09-11 16:23:05 -0700111 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100112 return RtpParameters();
113 }
114 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Saurav Das749f6602019-12-04 09:31:36 -0800115 return ssrc_ ? media_channel_->GetRtpReceiveParameters(*ssrc_)
116 : media_channel_->GetDefaultRtpReceiveParameters();
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100117 });
118}
119
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100120void AudioRtpReceiver::SetFrameDecryptor(
121 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
122 frame_decryptor_ = std::move(frame_decryptor);
123 // Special Case: Set the frame decryptor to any value on any existing channel.
124 if (media_channel_ && ssrc_.has_value() && !stopped_) {
125 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
126 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
127 });
128 }
129}
130
131rtc::scoped_refptr<FrameDecryptorInterface>
132AudioRtpReceiver::GetFrameDecryptor() const {
133 return frame_decryptor_;
134}
135
136void AudioRtpReceiver::Stop() {
137 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
138 if (stopped_) {
139 return;
140 }
Saurav Das7262fc22019-09-11 16:23:05 -0700141 if (media_channel_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100142 // Allow that SetOutputVolume fail. This is the normal case when the
143 // underlying media channel has already been deleted.
144 SetOutputVolume(0.0);
145 }
146 stopped_ = true;
147}
148
Saurav Das7262fc22019-09-11 16:23:05 -0700149void AudioRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
150 RTC_DCHECK(media_channel_);
151 if (!stopped_ && ssrc_ == ssrc) {
152 return;
153 }
154
155 if (!stopped_) {
Saurav Das749f6602019-12-04 09:31:36 -0800156 source_->Stop(media_channel_, ssrc_);
Saurav Das7262fc22019-09-11 16:23:05 -0700157 delay_->OnStop();
158 }
159 ssrc_ = ssrc;
160 stopped_ = false;
Saurav Das749f6602019-12-04 09:31:36 -0800161 source_->Start(media_channel_, ssrc);
Saurav Das7262fc22019-09-11 16:23:05 -0700162 delay_->OnStart(media_channel_, ssrc.value_or(0));
163 Reconfigure();
164}
165
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100166void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
167 if (!media_channel_) {
168 RTC_LOG(LS_ERROR)
169 << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
170 return;
171 }
Saurav Das7262fc22019-09-11 16:23:05 -0700172 RestartMediaChannel(ssrc);
173}
174
175void AudioRtpReceiver::SetupUnsignaledMediaChannel() {
176 if (!media_channel_) {
177 RTC_LOG(LS_ERROR) << "AudioRtpReceiver::SetupUnsignaledMediaChannel: No "
178 "audio channel exists.";
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100179 }
Saurav Das7262fc22019-09-11 16:23:05 -0700180 RestartMediaChannel(absl::nullopt);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100181}
182
183void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
184 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
185}
186
187void AudioRtpReceiver::SetStreams(
188 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
189 // Remove remote track from any streams that are going away.
190 for (const auto& existing_stream : streams_) {
191 bool removed = true;
192 for (const auto& stream : streams) {
193 if (existing_stream->id() == stream->id()) {
194 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
195 removed = false;
196 break;
197 }
198 }
199 if (removed) {
200 existing_stream->RemoveTrack(track_);
201 }
202 }
203 // Add remote track to any streams that are new.
204 for (const auto& stream : streams) {
205 bool added = true;
206 for (const auto& existing_stream : streams_) {
207 if (stream->id() == existing_stream->id()) {
208 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
209 added = false;
210 break;
211 }
212 }
213 if (added) {
214 stream->AddTrack(track_);
215 }
216 }
217 streams_ = streams;
218}
219
220std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
221 if (!media_channel_ || !ssrc_ || stopped_) {
222 return {};
223 }
224 return worker_thread_->Invoke<std::vector<RtpSource>>(
225 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
226}
227
228void AudioRtpReceiver::Reconfigure() {
Saurav Das7262fc22019-09-11 16:23:05 -0700229 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100230 RTC_LOG(LS_ERROR)
231 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
232 return;
233 }
234 if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
235 RTC_NOTREACHED();
236 }
237 // Reattach the frame decryptor if we were reconfigured.
238 MaybeAttachFrameDecryptorToMediaChannel(
239 ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
240}
241
242void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
243 observer_ = observer;
244 // Deliver any notifications the observer may have missed by being set late.
245 if (received_first_packet_ && observer_) {
246 observer_->OnFirstPacketReceived(media_type());
247 }
248}
249
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200250void AudioRtpReceiver::SetJitterBufferMinimumDelay(
251 absl::optional<double> delay_seconds) {
Ruslan Burakov428dcb22019-04-18 17:49:49 +0200252 delay_->Set(delay_seconds);
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200253}
254
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100255void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
256 RTC_DCHECK(media_channel == nullptr ||
257 media_channel->media_type() == media_type());
258 media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
259}
260
261void AudioRtpReceiver::NotifyFirstPacketReceived() {
262 if (observer_) {
263 observer_->OnFirstPacketReceived(media_type());
264 }
265 received_first_packet_ = true;
266}
267
268} // namespace webrtc