• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
14 
15 #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"
21 #include "pc/jitter_buffer_delay.h"
22 #include "pc/jitter_buffer_delay_proxy.h"
23 #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 
29 namespace webrtc {
30 
AudioRtpReceiver(rtc::Thread * worker_thread,std::string receiver_id,std::vector<std::string> stream_ids)31 AudioRtpReceiver::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 
AudioRtpReceiver(rtc::Thread * worker_thread,const std::string & receiver_id,const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams)38 AudioRtpReceiver::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()),
48       attachment_id_(GenerateUniqueId()),
49       delay_(JitterBufferDelayProxy::Create(
50           rtc::Thread::Current(),
51           worker_thread_,
52           new rtc::RefCountedObject<JitterBufferDelay>(worker_thread))) {
53   RTC_DCHECK(worker_thread_);
54   RTC_DCHECK(track_->GetSource()->remote());
55   track_->RegisterObserver(this);
56   track_->GetSource()->RegisterAudioObserver(this);
57   SetStreams(streams);
58 }
59 
~AudioRtpReceiver()60 AudioRtpReceiver::~AudioRtpReceiver() {
61   track_->GetSource()->UnregisterAudioObserver(this);
62   track_->UnregisterObserver(this);
63   Stop();
64 }
65 
OnChanged()66 void AudioRtpReceiver::OnChanged() {
67   if (cached_track_enabled_ != track_->enabled()) {
68     cached_track_enabled_ = track_->enabled();
69     Reconfigure();
70   }
71 }
72 
SetOutputVolume(double volume)73 bool AudioRtpReceiver::SetOutputVolume(double volume) {
74   RTC_DCHECK_GE(volume, 0.0);
75   RTC_DCHECK_LE(volume, 10.0);
76   RTC_DCHECK(media_channel_);
77   RTC_DCHECK(!stopped_);
78   return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
79     return ssrc_ ? media_channel_->SetOutputVolume(*ssrc_, volume)
80                  : media_channel_->SetDefaultOutputVolume(volume);
81   });
82 }
83 
OnSetVolume(double volume)84 void AudioRtpReceiver::OnSetVolume(double volume) {
85   RTC_DCHECK_GE(volume, 0);
86   RTC_DCHECK_LE(volume, 10);
87   cached_volume_ = volume;
88   if (!media_channel_ || stopped_) {
89     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 
stream_ids() const103 std::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 
GetParameters() const110 RtpParameters AudioRtpReceiver::GetParameters() const {
111   if (!media_channel_ || stopped_) {
112     return RtpParameters();
113   }
114   return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
115     return ssrc_ ? media_channel_->GetRtpReceiveParameters(*ssrc_)
116                  : media_channel_->GetDefaultRtpReceiveParameters();
117   });
118 }
119 
SetFrameDecryptor(rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor)120 void 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 
131 rtc::scoped_refptr<FrameDecryptorInterface>
GetFrameDecryptor() const132 AudioRtpReceiver::GetFrameDecryptor() const {
133   return frame_decryptor_;
134 }
135 
Stop()136 void AudioRtpReceiver::Stop() {
137   // TODO(deadbeef): Need to do more here to fully stop receiving packets.
138   if (stopped_) {
139     return;
140   }
141   if (media_channel_) {
142     // 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 
RestartMediaChannel(absl::optional<uint32_t> ssrc)149 void AudioRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
150   RTC_DCHECK(media_channel_);
151   if (!stopped_ && ssrc_ == ssrc) {
152     return;
153   }
154 
155   if (!stopped_) {
156     source_->Stop(media_channel_, ssrc_);
157     delay_->OnStop();
158   }
159   ssrc_ = ssrc;
160   stopped_ = false;
161   source_->Start(media_channel_, ssrc);
162   delay_->OnStart(media_channel_, ssrc.value_or(0));
163   Reconfigure();
164 }
165 
SetupMediaChannel(uint32_t ssrc)166 void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
167   if (!media_channel_) {
168     RTC_LOG(LS_ERROR)
169         << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
170     return;
171   }
172   RestartMediaChannel(ssrc);
173 }
174 
SetupUnsignaledMediaChannel()175 void AudioRtpReceiver::SetupUnsignaledMediaChannel() {
176   if (!media_channel_) {
177     RTC_LOG(LS_ERROR) << "AudioRtpReceiver::SetupUnsignaledMediaChannel: No "
178                          "audio channel exists.";
179   }
180   RestartMediaChannel(absl::nullopt);
181 }
182 
set_stream_ids(std::vector<std::string> stream_ids)183 void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
184   SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
185 }
186 
SetStreams(const std::vector<rtc::scoped_refptr<MediaStreamInterface>> & streams)187 void 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 
GetSources() const220 std::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 
SetDepacketizerToDecoderFrameTransformer(rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer)228 void AudioRtpReceiver::SetDepacketizerToDecoderFrameTransformer(
229     rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) {
230   worker_thread_->Invoke<void>(
231       RTC_FROM_HERE, [this, frame_transformer = std::move(frame_transformer)] {
232         RTC_DCHECK_RUN_ON(worker_thread_);
233         frame_transformer_ = frame_transformer;
234         if (media_channel_ && ssrc_.has_value() && !stopped_) {
235           media_channel_->SetDepacketizerToDecoderFrameTransformer(
236               *ssrc_, frame_transformer);
237         }
238       });
239 }
240 
Reconfigure()241 void AudioRtpReceiver::Reconfigure() {
242   if (!media_channel_ || stopped_) {
243     RTC_LOG(LS_ERROR)
244         << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
245     return;
246   }
247   if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
248     RTC_NOTREACHED();
249   }
250   // Reattach the frame decryptor if we were reconfigured.
251   MaybeAttachFrameDecryptorToMediaChannel(
252       ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
253 
254   if (media_channel_ && ssrc_.has_value() && !stopped_) {
255     worker_thread_->Invoke<void>(RTC_FROM_HERE, [this] {
256       RTC_DCHECK_RUN_ON(worker_thread_);
257       if (!frame_transformer_)
258         return;
259       media_channel_->SetDepacketizerToDecoderFrameTransformer(
260           *ssrc_, frame_transformer_);
261     });
262   }
263 }
264 
SetObserver(RtpReceiverObserverInterface * observer)265 void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
266   observer_ = observer;
267   // Deliver any notifications the observer may have missed by being set late.
268   if (received_first_packet_ && observer_) {
269     observer_->OnFirstPacketReceived(media_type());
270   }
271 }
272 
SetJitterBufferMinimumDelay(absl::optional<double> delay_seconds)273 void AudioRtpReceiver::SetJitterBufferMinimumDelay(
274     absl::optional<double> delay_seconds) {
275   delay_->Set(delay_seconds);
276 }
277 
SetMediaChannel(cricket::MediaChannel * media_channel)278 void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
279   RTC_DCHECK(media_channel == nullptr ||
280              media_channel->media_type() == media_type());
281   media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
282 }
283 
NotifyFirstPacketReceived()284 void AudioRtpReceiver::NotifyFirstPacketReceived() {
285   if (observer_) {
286     observer_->OnFirstPacketReceived(media_type());
287   }
288   received_first_packet_ = true;
289 }
290 
291 }  // namespace webrtc
292