xref: /aosp_15_r20/external/webrtc/pc/audio_rtp_receiver.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2019 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "pc/audio_rtp_receiver.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
14*d9f75844SAndroid Build Coastguard Worker 
15*d9f75844SAndroid Build Coastguard Worker #include <string>
16*d9f75844SAndroid Build Coastguard Worker #include <utility>
17*d9f75844SAndroid Build Coastguard Worker #include <vector>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
20*d9f75844SAndroid Build Coastguard Worker #include "pc/audio_track.h"
21*d9f75844SAndroid Build Coastguard Worker #include "pc/media_stream_track_proxy.h"
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
25*d9f75844SAndroid Build Coastguard Worker 
AudioRtpReceiver(rtc::Thread * worker_thread,std::string receiver_id,std::vector<std::string> stream_ids,bool is_unified_plan,cricket::VoiceMediaChannel * voice_channel)26*d9f75844SAndroid Build Coastguard Worker AudioRtpReceiver::AudioRtpReceiver(
27*d9f75844SAndroid Build Coastguard Worker     rtc::Thread* worker_thread,
28*d9f75844SAndroid Build Coastguard Worker     std::string receiver_id,
29*d9f75844SAndroid Build Coastguard Worker     std::vector<std::string> stream_ids,
30*d9f75844SAndroid Build Coastguard Worker     bool is_unified_plan,
31*d9f75844SAndroid Build Coastguard Worker     cricket::VoiceMediaChannel* voice_channel /*= nullptr*/)
32*d9f75844SAndroid Build Coastguard Worker     : AudioRtpReceiver(worker_thread,
33*d9f75844SAndroid Build Coastguard Worker                        receiver_id,
34*d9f75844SAndroid Build Coastguard Worker                        CreateStreamsFromIds(std::move(stream_ids)),
35*d9f75844SAndroid Build Coastguard Worker                        is_unified_plan,
36*d9f75844SAndroid Build Coastguard Worker                        voice_channel) {}
37*d9f75844SAndroid Build Coastguard Worker 
AudioRtpReceiver(rtc::Thread * worker_thread,const std::string & receiver_id,const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams,bool is_unified_plan,cricket::VoiceMediaChannel * voice_channel)38*d9f75844SAndroid Build Coastguard Worker AudioRtpReceiver::AudioRtpReceiver(
39*d9f75844SAndroid Build Coastguard Worker     rtc::Thread* worker_thread,
40*d9f75844SAndroid Build Coastguard Worker     const std::string& receiver_id,
41*d9f75844SAndroid Build Coastguard Worker     const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams,
42*d9f75844SAndroid Build Coastguard Worker     bool is_unified_plan,
43*d9f75844SAndroid Build Coastguard Worker     cricket::VoiceMediaChannel* voice_channel /*= nullptr*/)
44*d9f75844SAndroid Build Coastguard Worker     : worker_thread_(worker_thread),
45*d9f75844SAndroid Build Coastguard Worker       id_(receiver_id),
46*d9f75844SAndroid Build Coastguard Worker       source_(rtc::make_ref_counted<RemoteAudioSource>(
47*d9f75844SAndroid Build Coastguard Worker           worker_thread,
48*d9f75844SAndroid Build Coastguard Worker           is_unified_plan
49*d9f75844SAndroid Build Coastguard Worker               ? RemoteAudioSource::OnAudioChannelGoneAction::kSurvive
50*d9f75844SAndroid Build Coastguard Worker               : RemoteAudioSource::OnAudioChannelGoneAction::kEnd)),
51*d9f75844SAndroid Build Coastguard Worker       track_(AudioTrackProxyWithInternal<AudioTrack>::Create(
52*d9f75844SAndroid Build Coastguard Worker           rtc::Thread::Current(),
53*d9f75844SAndroid Build Coastguard Worker           AudioTrack::Create(receiver_id, source_))),
54*d9f75844SAndroid Build Coastguard Worker       media_channel_(voice_channel),
55*d9f75844SAndroid Build Coastguard Worker       cached_track_enabled_(track_->internal()->enabled()),
56*d9f75844SAndroid Build Coastguard Worker       attachment_id_(GenerateUniqueId()),
57*d9f75844SAndroid Build Coastguard Worker       worker_thread_safety_(PendingTaskSafetyFlag::CreateDetachedInactive()) {
58*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(worker_thread_);
59*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(track_->GetSource()->remote());
60*d9f75844SAndroid Build Coastguard Worker   track_->RegisterObserver(this);
61*d9f75844SAndroid Build Coastguard Worker   track_->GetSource()->RegisterAudioObserver(this);
62*d9f75844SAndroid Build Coastguard Worker   SetStreams(streams);
63*d9f75844SAndroid Build Coastguard Worker }
64*d9f75844SAndroid Build Coastguard Worker 
~AudioRtpReceiver()65*d9f75844SAndroid Build Coastguard Worker AudioRtpReceiver::~AudioRtpReceiver() {
66*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
67*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(!media_channel_);
68*d9f75844SAndroid Build Coastguard Worker 
69*d9f75844SAndroid Build Coastguard Worker   track_->GetSource()->UnregisterAudioObserver(this);
70*d9f75844SAndroid Build Coastguard Worker   track_->UnregisterObserver(this);
71*d9f75844SAndroid Build Coastguard Worker }
72*d9f75844SAndroid Build Coastguard Worker 
OnChanged()73*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::OnChanged() {
74*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
75*d9f75844SAndroid Build Coastguard Worker   const bool enabled = track_->internal()->enabled();
76*d9f75844SAndroid Build Coastguard Worker   if (cached_track_enabled_ == enabled)
77*d9f75844SAndroid Build Coastguard Worker     return;
78*d9f75844SAndroid Build Coastguard Worker   cached_track_enabled_ = enabled;
79*d9f75844SAndroid Build Coastguard Worker   worker_thread_->PostTask(SafeTask(worker_thread_safety_, [this, enabled]() {
80*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(worker_thread_);
81*d9f75844SAndroid Build Coastguard Worker     Reconfigure(enabled);
82*d9f75844SAndroid Build Coastguard Worker   }));
83*d9f75844SAndroid Build Coastguard Worker }
84*d9f75844SAndroid Build Coastguard Worker 
SetOutputVolume_w(double volume)85*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetOutputVolume_w(double volume) {
86*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
87*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(volume, 0.0);
88*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(volume, 10.0);
89*d9f75844SAndroid Build Coastguard Worker 
90*d9f75844SAndroid Build Coastguard Worker   if (!media_channel_)
91*d9f75844SAndroid Build Coastguard Worker     return;
92*d9f75844SAndroid Build Coastguard Worker 
93*d9f75844SAndroid Build Coastguard Worker   ssrc_ ? media_channel_->SetOutputVolume(*ssrc_, volume)
94*d9f75844SAndroid Build Coastguard Worker         : media_channel_->SetDefaultOutputVolume(volume);
95*d9f75844SAndroid Build Coastguard Worker }
96*d9f75844SAndroid Build Coastguard Worker 
OnSetVolume(double volume)97*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::OnSetVolume(double volume) {
98*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
99*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_GE(volume, 0);
100*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_LE(volume, 10);
101*d9f75844SAndroid Build Coastguard Worker 
102*d9f75844SAndroid Build Coastguard Worker   bool track_enabled = track_->internal()->enabled();
103*d9f75844SAndroid Build Coastguard Worker   worker_thread_->BlockingCall([&]() {
104*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(worker_thread_);
105*d9f75844SAndroid Build Coastguard Worker     // Update the cached_volume_ even when stopped, to allow clients to set
106*d9f75844SAndroid Build Coastguard Worker     // the volume before starting/restarting, eg see crbug.com/1272566.
107*d9f75844SAndroid Build Coastguard Worker     cached_volume_ = volume;
108*d9f75844SAndroid Build Coastguard Worker     // When the track is disabled, the volume of the source, which is the
109*d9f75844SAndroid Build Coastguard Worker     // corresponding WebRtc Voice Engine channel will be 0. So we do not
110*d9f75844SAndroid Build Coastguard Worker     // allow setting the volume to the source when the track is disabled.
111*d9f75844SAndroid Build Coastguard Worker     if (track_enabled)
112*d9f75844SAndroid Build Coastguard Worker       SetOutputVolume_w(volume);
113*d9f75844SAndroid Build Coastguard Worker   });
114*d9f75844SAndroid Build Coastguard Worker }
115*d9f75844SAndroid Build Coastguard Worker 
dtls_transport() const116*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<DtlsTransportInterface> AudioRtpReceiver::dtls_transport()
117*d9f75844SAndroid Build Coastguard Worker     const {
118*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
119*d9f75844SAndroid Build Coastguard Worker   return dtls_transport_;
120*d9f75844SAndroid Build Coastguard Worker }
121*d9f75844SAndroid Build Coastguard Worker 
stream_ids() const122*d9f75844SAndroid Build Coastguard Worker std::vector<std::string> AudioRtpReceiver::stream_ids() const {
123*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
124*d9f75844SAndroid Build Coastguard Worker   std::vector<std::string> stream_ids(streams_.size());
125*d9f75844SAndroid Build Coastguard Worker   for (size_t i = 0; i < streams_.size(); ++i)
126*d9f75844SAndroid Build Coastguard Worker     stream_ids[i] = streams_[i]->id();
127*d9f75844SAndroid Build Coastguard Worker   return stream_ids;
128*d9f75844SAndroid Build Coastguard Worker }
129*d9f75844SAndroid Build Coastguard Worker 
130*d9f75844SAndroid Build Coastguard Worker std::vector<rtc::scoped_refptr<MediaStreamInterface>>
streams() const131*d9f75844SAndroid Build Coastguard Worker AudioRtpReceiver::streams() const {
132*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
133*d9f75844SAndroid Build Coastguard Worker   return streams_;
134*d9f75844SAndroid Build Coastguard Worker }
135*d9f75844SAndroid Build Coastguard Worker 
GetParameters() const136*d9f75844SAndroid Build Coastguard Worker RtpParameters AudioRtpReceiver::GetParameters() const {
137*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
138*d9f75844SAndroid Build Coastguard Worker   if (!media_channel_)
139*d9f75844SAndroid Build Coastguard Worker     return RtpParameters();
140*d9f75844SAndroid Build Coastguard Worker   return ssrc_ ? media_channel_->GetRtpReceiveParameters(*ssrc_)
141*d9f75844SAndroid Build Coastguard Worker                : media_channel_->GetDefaultRtpReceiveParameters();
142*d9f75844SAndroid Build Coastguard Worker }
143*d9f75844SAndroid Build Coastguard Worker 
SetFrameDecryptor(rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)144*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetFrameDecryptor(
145*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
146*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
147*d9f75844SAndroid Build Coastguard Worker   frame_decryptor_ = std::move(frame_decryptor);
148*d9f75844SAndroid Build Coastguard Worker   // Special Case: Set the frame decryptor to any value on any existing channel.
149*d9f75844SAndroid Build Coastguard Worker   if (media_channel_ && ssrc_) {
150*d9f75844SAndroid Build Coastguard Worker     media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
151*d9f75844SAndroid Build Coastguard Worker   }
152*d9f75844SAndroid Build Coastguard Worker }
153*d9f75844SAndroid Build Coastguard Worker 
154*d9f75844SAndroid Build Coastguard Worker rtc::scoped_refptr<FrameDecryptorInterface>
GetFrameDecryptor() const155*d9f75844SAndroid Build Coastguard Worker AudioRtpReceiver::GetFrameDecryptor() const {
156*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
157*d9f75844SAndroid Build Coastguard Worker   return frame_decryptor_;
158*d9f75844SAndroid Build Coastguard Worker }
159*d9f75844SAndroid Build Coastguard Worker 
Stop()160*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::Stop() {
161*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
162*d9f75844SAndroid Build Coastguard Worker   source_->SetState(MediaSourceInterface::kEnded);
163*d9f75844SAndroid Build Coastguard Worker   track_->internal()->set_ended();
164*d9f75844SAndroid Build Coastguard Worker }
165*d9f75844SAndroid Build Coastguard Worker 
RestartMediaChannel(absl::optional<uint32_t> ssrc)166*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
167*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
168*d9f75844SAndroid Build Coastguard Worker   bool enabled = track_->internal()->enabled();
169*d9f75844SAndroid Build Coastguard Worker   MediaSourceInterface::SourceState state = source_->state();
170*d9f75844SAndroid Build Coastguard Worker   worker_thread_->BlockingCall([&]() {
171*d9f75844SAndroid Build Coastguard Worker     RTC_DCHECK_RUN_ON(worker_thread_);
172*d9f75844SAndroid Build Coastguard Worker     RestartMediaChannel_w(std::move(ssrc), enabled, state);
173*d9f75844SAndroid Build Coastguard Worker   });
174*d9f75844SAndroid Build Coastguard Worker   source_->SetState(MediaSourceInterface::kLive);
175*d9f75844SAndroid Build Coastguard Worker }
176*d9f75844SAndroid Build Coastguard Worker 
RestartMediaChannel_w(absl::optional<uint32_t> ssrc,bool track_enabled,MediaSourceInterface::SourceState state)177*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::RestartMediaChannel_w(
178*d9f75844SAndroid Build Coastguard Worker     absl::optional<uint32_t> ssrc,
179*d9f75844SAndroid Build Coastguard Worker     bool track_enabled,
180*d9f75844SAndroid Build Coastguard Worker     MediaSourceInterface::SourceState state) {
181*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
182*d9f75844SAndroid Build Coastguard Worker   if (!media_channel_)
183*d9f75844SAndroid Build Coastguard Worker     return;  // Can't restart.
184*d9f75844SAndroid Build Coastguard Worker 
185*d9f75844SAndroid Build Coastguard Worker   // Make sure the safety flag is marked as `alive` for cases where the media
186*d9f75844SAndroid Build Coastguard Worker   // channel was provided via the ctor and not an explicit call to
187*d9f75844SAndroid Build Coastguard Worker   // SetMediaChannel.
188*d9f75844SAndroid Build Coastguard Worker   worker_thread_safety_->SetAlive();
189*d9f75844SAndroid Build Coastguard Worker 
190*d9f75844SAndroid Build Coastguard Worker   if (state != MediaSourceInterface::kInitializing) {
191*d9f75844SAndroid Build Coastguard Worker     if (ssrc_ == ssrc)
192*d9f75844SAndroid Build Coastguard Worker       return;
193*d9f75844SAndroid Build Coastguard Worker     source_->Stop(media_channel_, ssrc_);
194*d9f75844SAndroid Build Coastguard Worker   }
195*d9f75844SAndroid Build Coastguard Worker 
196*d9f75844SAndroid Build Coastguard Worker   ssrc_ = std::move(ssrc);
197*d9f75844SAndroid Build Coastguard Worker   source_->Start(media_channel_, ssrc_);
198*d9f75844SAndroid Build Coastguard Worker   if (ssrc_) {
199*d9f75844SAndroid Build Coastguard Worker     media_channel_->SetBaseMinimumPlayoutDelayMs(*ssrc_, delay_.GetMs());
200*d9f75844SAndroid Build Coastguard Worker   }
201*d9f75844SAndroid Build Coastguard Worker 
202*d9f75844SAndroid Build Coastguard Worker   Reconfigure(track_enabled);
203*d9f75844SAndroid Build Coastguard Worker }
204*d9f75844SAndroid Build Coastguard Worker 
SetupMediaChannel(uint32_t ssrc)205*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
206*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
207*d9f75844SAndroid Build Coastguard Worker   RestartMediaChannel(ssrc);
208*d9f75844SAndroid Build Coastguard Worker }
209*d9f75844SAndroid Build Coastguard Worker 
SetupUnsignaledMediaChannel()210*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetupUnsignaledMediaChannel() {
211*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
212*d9f75844SAndroid Build Coastguard Worker   RestartMediaChannel(absl::nullopt);
213*d9f75844SAndroid Build Coastguard Worker }
214*d9f75844SAndroid Build Coastguard Worker 
ssrc() const215*d9f75844SAndroid Build Coastguard Worker uint32_t AudioRtpReceiver::ssrc() const {
216*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
217*d9f75844SAndroid Build Coastguard Worker   return ssrc_.value_or(0);
218*d9f75844SAndroid Build Coastguard Worker }
219*d9f75844SAndroid Build Coastguard Worker 
set_stream_ids(std::vector<std::string> stream_ids)220*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
221*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
222*d9f75844SAndroid Build Coastguard Worker   SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
223*d9f75844SAndroid Build Coastguard Worker }
224*d9f75844SAndroid Build Coastguard Worker 
set_transport(rtc::scoped_refptr<DtlsTransportInterface> dtls_transport)225*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::set_transport(
226*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) {
227*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
228*d9f75844SAndroid Build Coastguard Worker   dtls_transport_ = std::move(dtls_transport);
229*d9f75844SAndroid Build Coastguard Worker }
230*d9f75844SAndroid Build Coastguard Worker 
SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams)231*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetStreams(
232*d9f75844SAndroid Build Coastguard Worker     const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
233*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
234*d9f75844SAndroid Build Coastguard Worker   // Remove remote track from any streams that are going away.
235*d9f75844SAndroid Build Coastguard Worker   for (const auto& existing_stream : streams_) {
236*d9f75844SAndroid Build Coastguard Worker     bool removed = true;
237*d9f75844SAndroid Build Coastguard Worker     for (const auto& stream : streams) {
238*d9f75844SAndroid Build Coastguard Worker       if (existing_stream->id() == stream->id()) {
239*d9f75844SAndroid Build Coastguard Worker         RTC_DCHECK_EQ(existing_stream.get(), stream.get());
240*d9f75844SAndroid Build Coastguard Worker         removed = false;
241*d9f75844SAndroid Build Coastguard Worker         break;
242*d9f75844SAndroid Build Coastguard Worker       }
243*d9f75844SAndroid Build Coastguard Worker     }
244*d9f75844SAndroid Build Coastguard Worker     if (removed) {
245*d9f75844SAndroid Build Coastguard Worker       existing_stream->RemoveTrack(audio_track());
246*d9f75844SAndroid Build Coastguard Worker     }
247*d9f75844SAndroid Build Coastguard Worker   }
248*d9f75844SAndroid Build Coastguard Worker   // Add remote track to any streams that are new.
249*d9f75844SAndroid Build Coastguard Worker   for (const auto& stream : streams) {
250*d9f75844SAndroid Build Coastguard Worker     bool added = true;
251*d9f75844SAndroid Build Coastguard Worker     for (const auto& existing_stream : streams_) {
252*d9f75844SAndroid Build Coastguard Worker       if (stream->id() == existing_stream->id()) {
253*d9f75844SAndroid Build Coastguard Worker         RTC_DCHECK_EQ(stream.get(), existing_stream.get());
254*d9f75844SAndroid Build Coastguard Worker         added = false;
255*d9f75844SAndroid Build Coastguard Worker         break;
256*d9f75844SAndroid Build Coastguard Worker       }
257*d9f75844SAndroid Build Coastguard Worker     }
258*d9f75844SAndroid Build Coastguard Worker     if (added) {
259*d9f75844SAndroid Build Coastguard Worker       stream->AddTrack(audio_track());
260*d9f75844SAndroid Build Coastguard Worker     }
261*d9f75844SAndroid Build Coastguard Worker   }
262*d9f75844SAndroid Build Coastguard Worker   streams_ = streams;
263*d9f75844SAndroid Build Coastguard Worker }
264*d9f75844SAndroid Build Coastguard Worker 
GetSources() const265*d9f75844SAndroid Build Coastguard Worker std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
266*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
267*d9f75844SAndroid Build Coastguard Worker   if (!media_channel_ || !ssrc_) {
268*d9f75844SAndroid Build Coastguard Worker     return {};
269*d9f75844SAndroid Build Coastguard Worker   }
270*d9f75844SAndroid Build Coastguard Worker   return media_channel_->GetSources(*ssrc_);
271*d9f75844SAndroid Build Coastguard Worker }
272*d9f75844SAndroid Build Coastguard Worker 
SetDepacketizerToDecoderFrameTransformer(rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)273*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetDepacketizerToDecoderFrameTransformer(
274*d9f75844SAndroid Build Coastguard Worker     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
275*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
276*d9f75844SAndroid Build Coastguard Worker   if (media_channel_) {
277*d9f75844SAndroid Build Coastguard Worker     media_channel_->SetDepacketizerToDecoderFrameTransformer(ssrc_.value_or(0),
278*d9f75844SAndroid Build Coastguard Worker                                                              frame_transformer);
279*d9f75844SAndroid Build Coastguard Worker   }
280*d9f75844SAndroid Build Coastguard Worker   frame_transformer_ = std::move(frame_transformer);
281*d9f75844SAndroid Build Coastguard Worker }
282*d9f75844SAndroid Build Coastguard Worker 
Reconfigure(bool track_enabled)283*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::Reconfigure(bool track_enabled) {
284*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
285*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(media_channel_);
286*d9f75844SAndroid Build Coastguard Worker 
287*d9f75844SAndroid Build Coastguard Worker   SetOutputVolume_w(track_enabled ? cached_volume_ : 0);
288*d9f75844SAndroid Build Coastguard Worker 
289*d9f75844SAndroid Build Coastguard Worker   if (ssrc_ && frame_decryptor_) {
290*d9f75844SAndroid Build Coastguard Worker     // Reattach the frame decryptor if we were reconfigured.
291*d9f75844SAndroid Build Coastguard Worker     media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
292*d9f75844SAndroid Build Coastguard Worker   }
293*d9f75844SAndroid Build Coastguard Worker 
294*d9f75844SAndroid Build Coastguard Worker   if (frame_transformer_) {
295*d9f75844SAndroid Build Coastguard Worker     media_channel_->SetDepacketizerToDecoderFrameTransformer(
296*d9f75844SAndroid Build Coastguard Worker         ssrc_.value_or(0), frame_transformer_);
297*d9f75844SAndroid Build Coastguard Worker   }
298*d9f75844SAndroid Build Coastguard Worker }
299*d9f75844SAndroid Build Coastguard Worker 
SetObserver(RtpReceiverObserverInterface * observer)300*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
301*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
302*d9f75844SAndroid Build Coastguard Worker   observer_ = observer;
303*d9f75844SAndroid Build Coastguard Worker   // Deliver any notifications the observer may have missed by being set late.
304*d9f75844SAndroid Build Coastguard Worker   if (received_first_packet_ && observer_) {
305*d9f75844SAndroid Build Coastguard Worker     observer_->OnFirstPacketReceived(media_type());
306*d9f75844SAndroid Build Coastguard Worker   }
307*d9f75844SAndroid Build Coastguard Worker }
308*d9f75844SAndroid Build Coastguard Worker 
SetJitterBufferMinimumDelay(absl::optional<double> delay_seconds)309*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetJitterBufferMinimumDelay(
310*d9f75844SAndroid Build Coastguard Worker     absl::optional<double> delay_seconds) {
311*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
312*d9f75844SAndroid Build Coastguard Worker   delay_.Set(delay_seconds);
313*d9f75844SAndroid Build Coastguard Worker   if (media_channel_ && ssrc_)
314*d9f75844SAndroid Build Coastguard Worker     media_channel_->SetBaseMinimumPlayoutDelayMs(*ssrc_, delay_.GetMs());
315*d9f75844SAndroid Build Coastguard Worker }
316*d9f75844SAndroid Build Coastguard Worker 
SetMediaChannel(cricket::MediaChannel * media_channel)317*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
318*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(worker_thread_);
319*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(media_channel == nullptr ||
320*d9f75844SAndroid Build Coastguard Worker              media_channel->media_type() == media_type());
321*d9f75844SAndroid Build Coastguard Worker   if (!media_channel && media_channel_)
322*d9f75844SAndroid Build Coastguard Worker     SetOutputVolume_w(0.0);
323*d9f75844SAndroid Build Coastguard Worker 
324*d9f75844SAndroid Build Coastguard Worker   media_channel ? worker_thread_safety_->SetAlive()
325*d9f75844SAndroid Build Coastguard Worker                 : worker_thread_safety_->SetNotAlive();
326*d9f75844SAndroid Build Coastguard Worker   media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
327*d9f75844SAndroid Build Coastguard Worker }
328*d9f75844SAndroid Build Coastguard Worker 
NotifyFirstPacketReceived()329*d9f75844SAndroid Build Coastguard Worker void AudioRtpReceiver::NotifyFirstPacketReceived() {
330*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK_RUN_ON(&signaling_thread_checker_);
331*d9f75844SAndroid Build Coastguard Worker   if (observer_) {
332*d9f75844SAndroid Build Coastguard Worker     observer_->OnFirstPacketReceived(media_type());
333*d9f75844SAndroid Build Coastguard Worker   }
334*d9f75844SAndroid Build Coastguard Worker   received_first_packet_ = true;
335*d9f75844SAndroid Build Coastguard Worker }
336*d9f75844SAndroid Build Coastguard Worker 
337*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
338