blob: 9e687b9b55d0d0e135f6f15227db56d4ec9b44f5 [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
Harald Alvestrandc24a2182022-02-23 13:44:59 +000015#include <string>
Ruslan Burakov501bfba2019-02-11 10:29:19 +010016#include <utility>
17#include <vector>
18
Artem Titovd15a5752021-02-10 14:31:24 +010019#include "api/sequence_checker.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010020#include "pc/audio_track.h"
Markus Handella1b82012021-05-26 18:56:30 +020021#include "pc/media_stream_track_proxy.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010022#include "rtc_base/checks.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010023
24namespace webrtc {
25
Tommi6589def2022-02-17 23:36:47 +010026AudioRtpReceiver::AudioRtpReceiver(
27 rtc::Thread* worker_thread,
28 std::string receiver_id,
29 std::vector<std::string> stream_ids,
30 bool is_unified_plan,
Harald Alvestrandc0d44d92022-12-13 12:57:24 +000031 cricket::VoiceMediaReceiveChannelInterface* voice_channel /*= nullptr*/)
Ruslan Burakov501bfba2019-02-11 10:29:19 +010032 : AudioRtpReceiver(worker_thread,
33 receiver_id,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020034 CreateStreamsFromIds(std::move(stream_ids)),
Tommi6589def2022-02-17 23:36:47 +010035 is_unified_plan,
36 voice_channel) {}
Ruslan Burakov501bfba2019-02-11 10:29:19 +010037
38AudioRtpReceiver::AudioRtpReceiver(
39 rtc::Thread* worker_thread,
40 const std::string& receiver_id,
Henrik Boströmc335b0e2021-04-08 07:25:38 +020041 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
Tommi6589def2022-02-17 23:36:47 +010042 bool is_unified_plan,
Harald Alvestrandc0d44d92022-12-13 12:57:24 +000043 cricket::VoiceMediaReceiveChannelInterface* voice_channel /*= nullptr*/)
Ruslan Burakov501bfba2019-02-11 10:29:19 +010044 : worker_thread_(worker_thread),
45 id_(receiver_id),
Tommi87f70902021-04-27 14:43:08 +020046 source_(rtc::make_ref_counted<RemoteAudioSource>(
Henrik Boströmc335b0e2021-04-08 07:25:38 +020047 worker_thread,
48 is_unified_plan
49 ? RemoteAudioSource::OnAudioChannelGoneAction::kSurvive
50 : RemoteAudioSource::OnAudioChannelGoneAction::kEnd)),
Harald Alvestrand1ee33252020-09-24 13:31:15 +000051 track_(AudioTrackProxyWithInternal<AudioTrack>::Create(
52 rtc::Thread::Current(),
53 AudioTrack::Create(receiver_id, source_))),
Tommi6589def2022-02-17 23:36:47 +010054 media_channel_(voice_channel),
55 cached_track_enabled_(track_->internal()->enabled()),
Ruslan Burakov428dcb22019-04-18 17:49:49 +020056 attachment_id_(GenerateUniqueId()),
Tommi4ccdf932021-05-17 14:50:10 +020057 worker_thread_safety_(PendingTaskSafetyFlag::CreateDetachedInactive()) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010058 RTC_DCHECK(worker_thread_);
59 RTC_DCHECK(track_->GetSource()->remote());
60 track_->RegisterObserver(this);
61 track_->GetSource()->RegisterAudioObserver(this);
62 SetStreams(streams);
63}
64
65AudioRtpReceiver::~AudioRtpReceiver() {
Tommi4ccdf932021-05-17 14:50:10 +020066 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Tommi4ccdf932021-05-17 14:50:10 +020067 RTC_DCHECK(!media_channel_);
68
Ruslan Burakov501bfba2019-02-11 10:29:19 +010069 track_->GetSource()->UnregisterAudioObserver(this);
70 track_->UnregisterObserver(this);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010071}
72
73void AudioRtpReceiver::OnChanged() {
Tommi4ccdf932021-05-17 14:50:10 +020074 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Tommi6589def2022-02-17 23:36:47 +010075 const bool enabled = track_->internal()->enabled();
76 if (cached_track_enabled_ == enabled)
77 return;
78 cached_track_enabled_ = enabled;
Danil Chapovalova30439b2022-07-07 10:08:49 +020079 worker_thread_->PostTask(SafeTask(worker_thread_safety_, [this, enabled]() {
80 RTC_DCHECK_RUN_ON(worker_thread_);
81 Reconfigure(enabled);
82 }));
Ruslan Burakov501bfba2019-02-11 10:29:19 +010083}
84
Tommi4ccdf932021-05-17 14:50:10 +020085void AudioRtpReceiver::SetOutputVolume_w(double volume) {
Danil Chapovalov6e7c2682022-07-25 15:58:28 +020086 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010087 RTC_DCHECK_GE(volume, 0.0);
88 RTC_DCHECK_LE(volume, 10.0);
Tommi20d8d912022-02-08 21:12:15 +010089
90 if (!media_channel_)
91 return;
92
Tommi4ccdf932021-05-17 14:50:10 +020093 ssrc_ ? media_channel_->SetOutputVolume(*ssrc_, volume)
94 : media_channel_->SetDefaultOutputVolume(volume);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010095}
96
97void AudioRtpReceiver::OnSetVolume(double volume) {
Tommi4ccdf932021-05-17 14:50:10 +020098 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010099 RTC_DCHECK_GE(volume, 0);
100 RTC_DCHECK_LE(volume, 10);
Tony Herref05f2822021-11-22 19:10:19 +0100101
Tommi6589def2022-02-17 23:36:47 +0100102 bool track_enabled = track_->internal()->enabled();
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200103 worker_thread_->BlockingCall([&]() {
Tommi6589def2022-02-17 23:36:47 +0100104 RTC_DCHECK_RUN_ON(worker_thread_);
105 // Update the cached_volume_ even when stopped, to allow clients to set
106 // the volume before starting/restarting, eg see crbug.com/1272566.
107 cached_volume_ = volume;
108 // When the track is disabled, the volume of the source, which is the
109 // corresponding WebRtc Voice Engine channel will be 0. So we do not
110 // allow setting the volume to the source when the track is disabled.
111 if (track_enabled)
112 SetOutputVolume_w(volume);
113 });
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100114}
115
Tommi4ccdf932021-05-17 14:50:10 +0200116rtc::scoped_refptr<DtlsTransportInterface> AudioRtpReceiver::dtls_transport()
117 const {
118 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
119 return dtls_transport_;
120}
121
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100122std::vector<std::string> AudioRtpReceiver::stream_ids() const {
Tommi4ccdf932021-05-17 14:50:10 +0200123 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100124 std::vector<std::string> stream_ids(streams_.size());
125 for (size_t i = 0; i < streams_.size(); ++i)
126 stream_ids[i] = streams_[i]->id();
127 return stream_ids;
128}
129
Tommi4ccdf932021-05-17 14:50:10 +0200130std::vector<rtc::scoped_refptr<MediaStreamInterface>>
131AudioRtpReceiver::streams() const {
132 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
133 return streams_;
134}
135
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100136RtpParameters AudioRtpReceiver::GetParameters() const {
Tommi4ccdf932021-05-17 14:50:10 +0200137 RTC_DCHECK_RUN_ON(worker_thread_);
138 if (!media_channel_)
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100139 return RtpParameters();
Tommi4ccdf932021-05-17 14:50:10 +0200140 return ssrc_ ? media_channel_->GetRtpReceiveParameters(*ssrc_)
141 : media_channel_->GetDefaultRtpReceiveParameters();
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100142}
143
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100144void AudioRtpReceiver::SetFrameDecryptor(
145 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
Tommi4ccdf932021-05-17 14:50:10 +0200146 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100147 frame_decryptor_ = std::move(frame_decryptor);
148 // Special Case: Set the frame decryptor to any value on any existing channel.
Tommi4ccdf932021-05-17 14:50:10 +0200149 if (media_channel_ && ssrc_) {
150 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100151 }
152}
153
154rtc::scoped_refptr<FrameDecryptorInterface>
155AudioRtpReceiver::GetFrameDecryptor() const {
Tommi4ccdf932021-05-17 14:50:10 +0200156 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100157 return frame_decryptor_;
158}
159
160void AudioRtpReceiver::Stop() {
Tommi4ccdf932021-05-17 14:50:10 +0200161 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Tommi20d8d912022-02-08 21:12:15 +0100162 source_->SetState(MediaSourceInterface::kEnded);
Harald Alvestrand1ee33252020-09-24 13:31:15 +0000163 track_->internal()->set_ended();
164}
165
Tommi6589def2022-02-17 23:36:47 +0100166void AudioRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
Danil Chapovalov6e7c2682022-07-25 15:58:28 +0200167 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Tommi6589def2022-02-17 23:36:47 +0100168 bool enabled = track_->internal()->enabled();
Tommi20d8d912022-02-08 21:12:15 +0100169 MediaSourceInterface::SourceState state = source_->state();
Danil Chapovalov9e09a1f2022-09-08 18:38:10 +0200170 worker_thread_->BlockingCall([&]() {
Tommi6589def2022-02-17 23:36:47 +0100171 RTC_DCHECK_RUN_ON(worker_thread_);
172 RestartMediaChannel_w(std::move(ssrc), enabled, state);
173 });
Tommi20d8d912022-02-08 21:12:15 +0100174 source_->SetState(MediaSourceInterface::kLive);
Saurav Das7262fc22019-09-11 16:23:05 -0700175}
176
Tommi6589def2022-02-17 23:36:47 +0100177void AudioRtpReceiver::RestartMediaChannel_w(
178 absl::optional<uint32_t> ssrc,
179 bool track_enabled,
180 MediaSourceInterface::SourceState state) {
Danil Chapovalov6e7c2682022-07-25 15:58:28 +0200181 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi6589def2022-02-17 23:36:47 +0100182 if (!media_channel_)
183 return; // Can't restart.
184
Tommied3832b2022-03-22 11:54:09 +0100185 // Make sure the safety flag is marked as `alive` for cases where the media
186 // channel was provided via the ctor and not an explicit call to
187 // SetMediaChannel.
188 worker_thread_safety_->SetAlive();
189
Tommi6589def2022-02-17 23:36:47 +0100190 if (state != MediaSourceInterface::kInitializing) {
191 if (ssrc_ == ssrc)
192 return;
193 source_->Stop(media_channel_, ssrc_);
194 }
195
196 ssrc_ = std::move(ssrc);
197 source_->Start(media_channel_, ssrc_);
198 if (ssrc_) {
199 media_channel_->SetBaseMinimumPlayoutDelayMs(*ssrc_, delay_.GetMs());
200 }
201
202 Reconfigure(track_enabled);
203}
204
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100205void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
Tommi4ccdf932021-05-17 14:50:10 +0200206 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Saurav Das7262fc22019-09-11 16:23:05 -0700207 RestartMediaChannel(ssrc);
208}
209
210void AudioRtpReceiver::SetupUnsignaledMediaChannel() {
Tommi4ccdf932021-05-17 14:50:10 +0200211 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Saurav Das7262fc22019-09-11 16:23:05 -0700212 RestartMediaChannel(absl::nullopt);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100213}
214
Henrik Boström175f06f2023-01-05 08:53:16 +0100215absl::optional<uint32_t> AudioRtpReceiver::ssrc() const {
Tommi4ccdf932021-05-17 14:50:10 +0200216 RTC_DCHECK_RUN_ON(worker_thread_);
Henrik Boström175f06f2023-01-05 08:53:16 +0100217 if (!ssrc_.has_value() && media_channel_) {
218 return media_channel_->GetUnsignaledSsrc();
219 }
220 return ssrc_;
Tommi4ccdf932021-05-17 14:50:10 +0200221}
222
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100223void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
Tommi4ccdf932021-05-17 14:50:10 +0200224 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100225 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
226}
227
Tommi4ccdf932021-05-17 14:50:10 +0200228void AudioRtpReceiver::set_transport(
229 rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) {
230 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
231 dtls_transport_ = std::move(dtls_transport);
232}
233
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100234void AudioRtpReceiver::SetStreams(
235 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
Tommi4ccdf932021-05-17 14:50:10 +0200236 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100237 // Remove remote track from any streams that are going away.
238 for (const auto& existing_stream : streams_) {
239 bool removed = true;
240 for (const auto& stream : streams) {
241 if (existing_stream->id() == stream->id()) {
242 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
243 removed = false;
244 break;
245 }
246 }
247 if (removed) {
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000248 existing_stream->RemoveTrack(audio_track());
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100249 }
250 }
251 // Add remote track to any streams that are new.
252 for (const auto& stream : streams) {
253 bool added = true;
254 for (const auto& existing_stream : streams_) {
255 if (stream->id() == existing_stream->id()) {
256 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
257 added = false;
258 break;
259 }
260 }
261 if (added) {
Harald Alvestrand2f7ad282022-04-21 11:35:43 +0000262 stream->AddTrack(audio_track());
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100263 }
264 }
265 streams_ = streams;
266}
267
268std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
Tommi4ccdf932021-05-17 14:50:10 +0200269 RTC_DCHECK_RUN_ON(worker_thread_);
270 if (!media_channel_ || !ssrc_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100271 return {};
272 }
Tommi4ccdf932021-05-17 14:50:10 +0200273 return media_channel_->GetSources(*ssrc_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100274}
275
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200276void AudioRtpReceiver::SetDepacketizerToDecoderFrameTransformer(
277 rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
Tommi4ccdf932021-05-17 14:50:10 +0200278 RTC_DCHECK_RUN_ON(worker_thread_);
279 if (media_channel_) {
280 media_channel_->SetDepacketizerToDecoderFrameTransformer(ssrc_.value_or(0),
281 frame_transformer);
282 }
283 frame_transformer_ = std::move(frame_transformer);
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200284}
285
Tommi6589def2022-02-17 23:36:47 +0100286void AudioRtpReceiver::Reconfigure(bool track_enabled) {
Danil Chapovalov6e7c2682022-07-25 15:58:28 +0200287 RTC_DCHECK_RUN_ON(worker_thread_);
Tommi4ccdf932021-05-17 14:50:10 +0200288 RTC_DCHECK(media_channel_);
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200289
Tommi6589def2022-02-17 23:36:47 +0100290 SetOutputVolume_w(track_enabled ? cached_volume_ : 0);
Tommi4ccdf932021-05-17 14:50:10 +0200291
292 if (ssrc_ && frame_decryptor_) {
293 // Reattach the frame decryptor if we were reconfigured.
294 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
295 }
296
297 if (frame_transformer_) {
298 media_channel_->SetDepacketizerToDecoderFrameTransformer(
299 ssrc_.value_or(0), frame_transformer_);
Marina Ciocea3e9af7f2020-04-01 07:46:16 +0200300 }
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100301}
302
303void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
Tommi4ccdf932021-05-17 14:50:10 +0200304 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100305 observer_ = observer;
306 // Deliver any notifications the observer may have missed by being set late.
307 if (received_first_packet_ && observer_) {
308 observer_->OnFirstPacketReceived(media_type());
309 }
310}
311
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200312void AudioRtpReceiver::SetJitterBufferMinimumDelay(
313 absl::optional<double> delay_seconds) {
Tommi4ccdf932021-05-17 14:50:10 +0200314 RTC_DCHECK_RUN_ON(worker_thread_);
315 delay_.Set(delay_seconds);
316 if (media_channel_ && ssrc_)
317 media_channel_->SetBaseMinimumPlayoutDelayMs(*ssrc_, delay_.GetMs());
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200318}
319
Harald Alvestrand36fafc82022-12-08 08:47:42 +0000320void AudioRtpReceiver::SetMediaChannel(
321 cricket::MediaReceiveChannelInterface* media_channel) {
Tommi6589def2022-02-17 23:36:47 +0100322 RTC_DCHECK_RUN_ON(worker_thread_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100323 RTC_DCHECK(media_channel == nullptr ||
324 media_channel->media_type() == media_type());
Tommi6589def2022-02-17 23:36:47 +0100325 if (!media_channel && media_channel_)
326 SetOutputVolume_w(0.0);
Tommi4ccdf932021-05-17 14:50:10 +0200327
Tommi4ccdf932021-05-17 14:50:10 +0200328 media_channel ? worker_thread_safety_->SetAlive()
329 : worker_thread_safety_->SetNotAlive();
Harald Alvestrandc0d44d92022-12-13 12:57:24 +0000330 media_channel_ =
331 static_cast<cricket::VoiceMediaReceiveChannelInterface*>(media_channel);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100332}
333
334void AudioRtpReceiver::NotifyFirstPacketReceived() {
Tommi4ccdf932021-05-17 14:50:10 +0200335 RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100336 if (observer_) {
337 observer_->OnFirstPacketReceived(media_type());
338 }
339 received_first_packet_ = true;
340}
341
342} // namespace webrtc