| 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 <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "api/sequence_checker.h" |
| 20 | #include "pc/audio_track.h" |
| 21 | #include "pc/media_stream_track_proxy.h" |
| 22 | #include "rtc_base/checks.h" |
| 23 |
| 24 | namespace webrtc { |
| 25 |
| 26 | AudioRtpReceiver::AudioRtpReceiver( |
| 27 | rtc::Thread* worker_thread, |
| 28 | std::string receiver_id, |
| 29 | std::vector<std::string> stream_ids, |
| 30 | bool is_unified_plan, |
| 31 | cricket::VoiceMediaReceiveChannelInterface* voice_channel /*= nullptr*/) |
| 32 | : AudioRtpReceiver(worker_thread, |
| 33 | receiver_id, |
| 34 | CreateStreamsFromIds(std::move(stream_ids)), |
| 35 | is_unified_plan, |
| 36 | voice_channel) {} |
| 37 |
| 38 | AudioRtpReceiver::AudioRtpReceiver( |
| 39 | rtc::Thread* worker_thread, |
| 40 | const std::string& receiver_id, |
| 41 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams, |
| 42 | bool is_unified_plan, |
| 43 | cricket::VoiceMediaReceiveChannelInterface* voice_channel /*= nullptr*/) |
| 44 | : worker_thread_(worker_thread), |
| 45 | id_(receiver_id), |
| 46 | source_(rtc::make_ref_counted<RemoteAudioSource>( |
| 47 | worker_thread, |
| 48 | is_unified_plan |
| 49 | ? RemoteAudioSource::OnAudioChannelGoneAction::kSurvive |
| 50 | : RemoteAudioSource::OnAudioChannelGoneAction::kEnd)), |
| 51 | track_(AudioTrackProxyWithInternal<AudioTrack>::Create( |
| 52 | rtc::Thread::Current(), |
| 53 | AudioTrack::Create(receiver_id, source_))), |
| 54 | media_channel_(voice_channel), |
| 55 | cached_track_enabled_(track_->internal()->enabled()), |
| 56 | attachment_id_(GenerateUniqueId()), |
| 57 | worker_thread_safety_(PendingTaskSafetyFlag::CreateDetachedInactive()) { |
| 58 | RTC_DCHECK(worker_thread_); |
| 59 | RTC_DCHECK(track_->GetSource()->remote()); |
| 60 | track_->RegisterObserver(this); |
| 61 | track_->GetSource()->RegisterAudioObserver(this); |
| 62 | SetStreams(streams); |
| 63 | } |
| 64 |
| 65 | AudioRtpReceiver::~AudioRtpReceiver() { |
| 66 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 67 | RTC_DCHECK(!media_channel_); |
| 68 |
| 69 | track_->GetSource()->UnregisterAudioObserver(this); |
| 70 | track_->UnregisterObserver(this); |
| 71 | } |
| 72 |
| 73 | void AudioRtpReceiver::OnChanged() { |
| 74 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 75 | const bool enabled = track_->internal()->enabled(); |
| 76 | if (cached_track_enabled_ == enabled) |
| 77 | return; |
| 78 | cached_track_enabled_ = enabled; |
| 79 | worker_thread_->PostTask(SafeTask(worker_thread_safety_, [this, enabled]() { |
| 80 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 81 | Reconfigure(enabled); |
| 82 | })); |
| 83 | } |
| 84 |
| 85 | void AudioRtpReceiver::SetOutputVolume_w(double volume) { |
| 86 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 87 | RTC_DCHECK_GE(volume, 0.0); |
| 88 | RTC_DCHECK_LE(volume, 10.0); |
| 89 |
| 90 | if (!media_channel_) |
| 91 | return; |
| 92 |
| 93 | signaled_ssrc_ ? media_channel_->SetOutputVolume(*signaled_ssrc_, volume) |
| 94 | : media_channel_->SetDefaultOutputVolume(volume); |
| 95 | } |
| 96 |
| 97 | void AudioRtpReceiver::OnSetVolume(double volume) { |
| 98 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 99 | RTC_DCHECK_GE(volume, 0); |
| 100 | RTC_DCHECK_LE(volume, 10); |
| 101 |
| 102 | bool track_enabled = track_->internal()->enabled(); |
| 103 | worker_thread_->BlockingCall([&]() { |
| 104 | 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 | }); |
| 114 | } |
| 115 |
| 116 | rtc::scoped_refptr<DtlsTransportInterface> AudioRtpReceiver::dtls_transport() |
| 117 | const { |
| 118 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 119 | return dtls_transport_; |
| 120 | } |
| 121 |
| 122 | std::vector<std::string> AudioRtpReceiver::stream_ids() const { |
| 123 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 124 | 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 |
| 130 | std::vector<rtc::scoped_refptr<MediaStreamInterface>> |
| 131 | AudioRtpReceiver::streams() const { |
| 132 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 133 | return streams_; |
| 134 | } |
| 135 |
| 136 | RtpParameters AudioRtpReceiver::GetParameters() const { |
| 137 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 138 | if (!media_channel_) |
| 139 | return RtpParameters(); |
| 140 | auto current_ssrc = ssrc(); |
| 141 | return current_ssrc.has_value() |
| 142 | ? media_channel_->GetRtpReceiverParameters(current_ssrc.value()) |
| 143 | : media_channel_->GetDefaultRtpReceiveParameters(); |
| 144 | } |
| 145 |
| 146 | void AudioRtpReceiver::SetFrameDecryptor( |
| 147 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) { |
| 148 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 149 | frame_decryptor_ = std::move(frame_decryptor); |
| 150 | // Special Case: Set the frame decryptor to any value on any existing channel. |
| 151 | if (media_channel_ && signaled_ssrc_) { |
| 152 | media_channel_->SetFrameDecryptor(*signaled_ssrc_, frame_decryptor_); |
| 153 | } |
| 154 | } |
| 155 |
| 156 | rtc::scoped_refptr<FrameDecryptorInterface> |
| 157 | AudioRtpReceiver::GetFrameDecryptor() const { |
| 158 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 159 | return frame_decryptor_; |
| 160 | } |
| 161 |
| 162 | void AudioRtpReceiver::Stop() { |
| 163 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 164 | source_->SetState(MediaSourceInterface::kEnded); |
| 165 | track_->internal()->set_ended(); |
| 166 | } |
| 167 |
| 168 | void AudioRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) { |
| 169 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 170 | bool enabled = track_->internal()->enabled(); |
| 171 | MediaSourceInterface::SourceState state = source_->state(); |
| 172 | worker_thread_->BlockingCall([&]() { |
| 173 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 174 | RestartMediaChannel_w(std::move(ssrc), enabled, state); |
| 175 | }); |
| 176 | source_->SetState(MediaSourceInterface::kLive); |
| 177 | } |
| 178 |
| 179 | void AudioRtpReceiver::RestartMediaChannel_w( |
| 180 | absl::optional<uint32_t> ssrc, |
| 181 | bool track_enabled, |
| 182 | MediaSourceInterface::SourceState state) { |
| 183 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 184 | if (!media_channel_) |
| 185 | return; // Can't restart. |
| 186 |
| 187 | // Make sure the safety flag is marked as `alive` for cases where the media |
| 188 | // channel was provided via the ctor and not an explicit call to |
| 189 | // SetMediaChannel. |
| 190 | worker_thread_safety_->SetAlive(); |
| 191 |
| 192 | if (state != MediaSourceInterface::kInitializing) { |
| 193 | if (signaled_ssrc_ == ssrc) |
| 194 | return; |
| 195 | source_->Stop(media_channel_, signaled_ssrc_); |
| 196 | } |
| 197 |
| 198 | signaled_ssrc_ = std::move(ssrc); |
| 199 | source_->Start(media_channel_, signaled_ssrc_); |
| 200 | if (signaled_ssrc_) { |
| 201 | media_channel_->SetBaseMinimumPlayoutDelayMs(*signaled_ssrc_, |
| 202 | delay_.GetMs()); |
| 203 | } |
| 204 |
| 205 | Reconfigure(track_enabled); |
| 206 | } |
| 207 |
| 208 | void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) { |
| 209 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 210 | RestartMediaChannel(ssrc); |
| 211 | } |
| 212 |
| 213 | void AudioRtpReceiver::SetupUnsignaledMediaChannel() { |
| 214 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 215 | RestartMediaChannel(absl::nullopt); |
| 216 | } |
| 217 |
| 218 | absl::optional<uint32_t> AudioRtpReceiver::ssrc() const { |
| 219 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 220 | if (!signaled_ssrc_.has_value() && media_channel_) { |
| 221 | return media_channel_->GetUnsignaledSsrc(); |
| 222 | } |
| 223 | return signaled_ssrc_; |
| 224 | } |
| 225 |
| 226 | void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) { |
| 227 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 228 | SetStreams(CreateStreamsFromIds(std::move(stream_ids))); |
| 229 | } |
| 230 |
| 231 | void AudioRtpReceiver::set_transport( |
| 232 | rtc::scoped_refptr<DtlsTransportInterface> dtls_transport) { |
| 233 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 234 | dtls_transport_ = std::move(dtls_transport); |
| 235 | } |
| 236 |
| 237 | void AudioRtpReceiver::SetStreams( |
| 238 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 239 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 240 | // Remove remote track from any streams that are going away. |
| 241 | for (const auto& existing_stream : streams_) { |
| 242 | bool removed = true; |
| 243 | for (const auto& stream : streams) { |
| 244 | if (existing_stream->id() == stream->id()) { |
| 245 | RTC_DCHECK_EQ(existing_stream.get(), stream.get()); |
| 246 | removed = false; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | if (removed) { |
| 251 | existing_stream->RemoveTrack(audio_track()); |
| 252 | } |
| 253 | } |
| 254 | // Add remote track to any streams that are new. |
| 255 | for (const auto& stream : streams) { |
| 256 | bool added = true; |
| 257 | for (const auto& existing_stream : streams_) { |
| 258 | if (stream->id() == existing_stream->id()) { |
| 259 | RTC_DCHECK_EQ(stream.get(), existing_stream.get()); |
| 260 | added = false; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | if (added) { |
| 265 | stream->AddTrack(audio_track()); |
| 266 | } |
| 267 | } |
| 268 | streams_ = streams; |
| 269 | } |
| 270 |
| 271 | std::vector<RtpSource> AudioRtpReceiver::GetSources() const { |
| 272 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 273 | auto current_ssrc = ssrc(); |
| 274 | if (!media_channel_ || !current_ssrc.has_value()) { |
| 275 | return {}; |
| 276 | } |
| 277 | return media_channel_->GetSources(current_ssrc.value()); |
| 278 | } |
| 279 |
| 280 | void AudioRtpReceiver::SetDepacketizerToDecoderFrameTransformer( |
| 281 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer) { |
| 282 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 283 | if (media_channel_) { |
| 284 | media_channel_->SetDepacketizerToDecoderFrameTransformer( |
| 285 | signaled_ssrc_.value_or(0), frame_transformer); |
| 286 | } |
| 287 | frame_transformer_ = std::move(frame_transformer); |
| 288 | } |
| 289 |
| 290 | void AudioRtpReceiver::Reconfigure(bool track_enabled) { |
| 291 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 292 | RTC_DCHECK(media_channel_); |
| 293 |
| 294 | SetOutputVolume_w(track_enabled ? cached_volume_ : 0); |
| 295 |
| 296 | if (signaled_ssrc_ && frame_decryptor_) { |
| 297 | // Reattach the frame decryptor if we were reconfigured. |
| 298 | media_channel_->SetFrameDecryptor(*signaled_ssrc_, frame_decryptor_); |
| 299 | } |
| 300 |
| 301 | if (frame_transformer_) { |
| 302 | media_channel_->SetDepacketizerToDecoderFrameTransformer( |
| 303 | signaled_ssrc_.value_or(0), frame_transformer_); |
| 304 | } |
| 305 | } |
| 306 |
| 307 | void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 308 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 309 | observer_ = observer; |
| 310 | // Deliver any notifications the observer may have missed by being set late. |
| 311 | if (received_first_packet_ && observer_) { |
| 312 | observer_->OnFirstPacketReceived(media_type()); |
| 313 | } |
| 314 | } |
| 315 |
| 316 | void AudioRtpReceiver::SetJitterBufferMinimumDelay( |
| 317 | absl::optional<double> delay_seconds) { |
| 318 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 319 | delay_.Set(delay_seconds); |
| 320 | if (media_channel_ && signaled_ssrc_) |
| 321 | media_channel_->SetBaseMinimumPlayoutDelayMs(*signaled_ssrc_, |
| 322 | delay_.GetMs()); |
| 323 | } |
| 324 |
| 325 | void AudioRtpReceiver::SetMediaChannel( |
| 326 | cricket::MediaReceiveChannelInterface* media_channel) { |
| 327 | RTC_DCHECK_RUN_ON(worker_thread_); |
| 328 | RTC_DCHECK(media_channel == nullptr || |
| 329 | media_channel->media_type() == media_type()); |
| 330 | if (!media_channel && media_channel_) |
| 331 | SetOutputVolume_w(0.0); |
| 332 |
| 333 | media_channel ? worker_thread_safety_->SetAlive() |
| 334 | : worker_thread_safety_->SetNotAlive(); |
| 335 | media_channel_ = |
| 336 | static_cast<cricket::VoiceMediaReceiveChannelInterface*>(media_channel); |
| 337 | } |
| 338 |
| 339 | void AudioRtpReceiver::NotifyFirstPacketReceived() { |
| 340 | RTC_DCHECK_RUN_ON(&signaling_thread_checker_); |
| 341 | if (observer_) { |
| 342 | observer_->OnFirstPacketReceived(media_type()); |
| 343 | } |
| 344 | received_first_packet_ = true; |
| 345 | } |
| 346 |
| 347 | } // namespace webrtc |
| 348 |