Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 14 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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" |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 21 | #include "pc/jitter_buffer_delay.h" |
| 22 | #include "pc/jitter_buffer_delay_proxy.h" |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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 | |
| 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 | |
| 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()), |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 48 | attachment_id_(GenerateUniqueId()), |
| 49 | delay_(JitterBufferDelayProxy::Create( |
| 50 | rtc::Thread::Current(), |
| 51 | worker_thread_, |
| 52 | new rtc::RefCountedObject<JitterBufferDelay>(worker_thread))) { |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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 | |
| 60 | AudioRtpReceiver::~AudioRtpReceiver() { |
| 61 | track_->GetSource()->UnregisterAudioObserver(this); |
| 62 | track_->UnregisterObserver(this); |
| 63 | Stop(); |
| 64 | } |
| 65 | |
| 66 | void AudioRtpReceiver::OnChanged() { |
| 67 | if (cached_track_enabled_ != track_->enabled()) { |
| 68 | cached_track_enabled_ = track_->enabled(); |
| 69 | Reconfigure(); |
| 70 | } |
| 71 | } |
| 72 | |
| 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_); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 77 | RTC_DCHECK(!stopped_); |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 78 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 79 | return ssrc_ ? media_channel_->SetOutputVolume(*ssrc_, volume) |
| 80 | : media_channel_->SetDefaultOutputVolume(volume); |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 81 | }); |
| 82 | } |
| 83 | |
| 84 | void AudioRtpReceiver::OnSetVolume(double volume) { |
| 85 | RTC_DCHECK_GE(volume, 0); |
| 86 | RTC_DCHECK_LE(volume, 10); |
| 87 | cached_volume_ = volume; |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 88 | if (!media_channel_ || stopped_) { |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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 | |
| 103 | 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 | |
| 110 | RtpParameters AudioRtpReceiver::GetParameters() const { |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 111 | if (!media_channel_ || stopped_) { |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 112 | return RtpParameters(); |
| 113 | } |
| 114 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 115 | return ssrc_ ? media_channel_->GetRtpReceiveParameters(*ssrc_) |
| 116 | : media_channel_->GetDefaultRtpReceiveParameters(); |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 117 | }); |
| 118 | } |
| 119 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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> |
| 132 | AudioRtpReceiver::GetFrameDecryptor() const { |
| 133 | return frame_decryptor_; |
| 134 | } |
| 135 | |
| 136 | void AudioRtpReceiver::Stop() { |
| 137 | // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
| 138 | if (stopped_) { |
| 139 | return; |
| 140 | } |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 141 | if (media_channel_) { |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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 | |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 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_) { |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 156 | source_->Stop(media_channel_, ssrc_); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 157 | delay_->OnStop(); |
| 158 | } |
| 159 | ssrc_ = ssrc; |
| 160 | stopped_ = false; |
Saurav Das | 749f660 | 2019-12-04 09:31:36 -0800 | [diff] [blame] | 161 | source_->Start(media_channel_, ssrc); |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 162 | delay_->OnStart(media_channel_, ssrc.value_or(0)); |
| 163 | Reconfigure(); |
| 164 | } |
| 165 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 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 | } |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 172 | RestartMediaChannel(ssrc); |
| 173 | } |
| 174 | |
| 175 | void AudioRtpReceiver::SetupUnsignaledMediaChannel() { |
| 176 | if (!media_channel_) { |
| 177 | RTC_LOG(LS_ERROR) << "AudioRtpReceiver::SetupUnsignaledMediaChannel: No " |
| 178 | "audio channel exists."; |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 179 | } |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 180 | RestartMediaChannel(absl::nullopt); |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) { |
| 184 | SetStreams(CreateStreamsFromIds(std::move(stream_ids))); |
| 185 | } |
| 186 | |
| 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 | |
| 220 | 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 | |
| 228 | void AudioRtpReceiver::Reconfigure() { |
Saurav Das | 7262fc2 | 2019-09-11 16:23:05 -0700 | [diff] [blame] | 229 | if (!media_channel_ || stopped_) { |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 230 | 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 | |
| 242 | void 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 Burakov | 4bac79e | 2019-04-03 19:55:33 +0200 | [diff] [blame] | 250 | void AudioRtpReceiver::SetJitterBufferMinimumDelay( |
| 251 | absl::optional<double> delay_seconds) { |
Ruslan Burakov | 428dcb2 | 2019-04-18 17:49:49 +0200 | [diff] [blame] | 252 | delay_->Set(delay_seconds); |
Ruslan Burakov | 4bac79e | 2019-04-03 19:55:33 +0200 | [diff] [blame] | 253 | } |
| 254 | |
Ruslan Burakov | 501bfba | 2019-02-11 10:29:19 +0100 | [diff] [blame] | 255 | void 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 | |
| 261 | void AudioRtpReceiver::NotifyFirstPacketReceived() { |
| 262 | if (observer_) { |
| 263 | observer_->OnFirstPacketReceived(media_type()); |
| 264 | } |
| 265 | received_first_packet_ = true; |
| 266 | } |
| 267 | |
| 268 | } // namespace webrtc |