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> |
| 14 | #include <utility> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "api/media_stream_proxy.h" |
| 18 | #include "api/media_stream_track_proxy.h" |
| 19 | #include "pc/audio_track.h" |
| 20 | #include "pc/media_stream.h" |
| 21 | #include "rtc_base/checks.h" |
| 22 | #include "rtc_base/location.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/trace_event.h" |
| 25 | |
| 26 | namespace webrtc { |
| 27 | |
| 28 | AudioRtpReceiver::AudioRtpReceiver(rtc::Thread* worker_thread, |
| 29 | std::string receiver_id, |
| 30 | std::vector<std::string> stream_ids) |
| 31 | : AudioRtpReceiver(worker_thread, |
| 32 | receiver_id, |
| 33 | CreateStreamsFromIds(std::move(stream_ids))) {} |
| 34 | |
| 35 | AudioRtpReceiver::AudioRtpReceiver( |
| 36 | rtc::Thread* worker_thread, |
| 37 | const std::string& receiver_id, |
| 38 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) |
| 39 | : worker_thread_(worker_thread), |
| 40 | id_(receiver_id), |
| 41 | source_(new rtc::RefCountedObject<RemoteAudioSource>(worker_thread)), |
| 42 | track_(AudioTrackProxy::Create(rtc::Thread::Current(), |
| 43 | AudioTrack::Create(receiver_id, source_))), |
| 44 | cached_track_enabled_(track_->enabled()), |
| 45 | attachment_id_(GenerateUniqueId()) { |
| 46 | RTC_DCHECK(worker_thread_); |
| 47 | RTC_DCHECK(track_->GetSource()->remote()); |
| 48 | track_->RegisterObserver(this); |
| 49 | track_->GetSource()->RegisterAudioObserver(this); |
| 50 | SetStreams(streams); |
| 51 | } |
| 52 | |
| 53 | AudioRtpReceiver::~AudioRtpReceiver() { |
| 54 | track_->GetSource()->UnregisterAudioObserver(this); |
| 55 | track_->UnregisterObserver(this); |
| 56 | Stop(); |
| 57 | } |
| 58 | |
| 59 | void AudioRtpReceiver::OnChanged() { |
| 60 | if (cached_track_enabled_ != track_->enabled()) { |
| 61 | cached_track_enabled_ = track_->enabled(); |
| 62 | Reconfigure(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | bool AudioRtpReceiver::SetOutputVolume(double volume) { |
| 67 | RTC_DCHECK_GE(volume, 0.0); |
| 68 | RTC_DCHECK_LE(volume, 10.0); |
| 69 | RTC_DCHECK(media_channel_); |
| 70 | RTC_DCHECK(ssrc_); |
| 71 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 72 | return media_channel_->SetOutputVolume(*ssrc_, volume); |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | void AudioRtpReceiver::OnSetVolume(double volume) { |
| 77 | RTC_DCHECK_GE(volume, 0); |
| 78 | RTC_DCHECK_LE(volume, 10); |
| 79 | cached_volume_ = volume; |
| 80 | if (!media_channel_ || !ssrc_) { |
| 81 | RTC_LOG(LS_ERROR) |
| 82 | << "AudioRtpReceiver::OnSetVolume: No audio channel exists."; |
| 83 | return; |
| 84 | } |
| 85 | // When the track is disabled, the volume of the source, which is the |
| 86 | // corresponding WebRtc Voice Engine channel will be 0. So we do not allow |
| 87 | // setting the volume to the source when the track is disabled. |
| 88 | if (!stopped_ && track_->enabled()) { |
| 89 | if (!SetOutputVolume(cached_volume_)) { |
| 90 | RTC_NOTREACHED(); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | std::vector<std::string> AudioRtpReceiver::stream_ids() const { |
| 96 | std::vector<std::string> stream_ids(streams_.size()); |
| 97 | for (size_t i = 0; i < streams_.size(); ++i) |
| 98 | stream_ids[i] = streams_[i]->id(); |
| 99 | return stream_ids; |
| 100 | } |
| 101 | |
| 102 | RtpParameters AudioRtpReceiver::GetParameters() const { |
| 103 | if (!media_channel_ || !ssrc_ || stopped_) { |
| 104 | return RtpParameters(); |
| 105 | } |
| 106 | return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] { |
| 107 | return media_channel_->GetRtpReceiveParameters(*ssrc_); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) { |
| 112 | TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters"); |
| 113 | if (!media_channel_ || !ssrc_ || stopped_) { |
| 114 | return false; |
| 115 | } |
| 116 | return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] { |
| 117 | return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | void AudioRtpReceiver::SetFrameDecryptor( |
| 122 | rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) { |
| 123 | frame_decryptor_ = std::move(frame_decryptor); |
| 124 | // Special Case: Set the frame decryptor to any value on any existing channel. |
| 125 | if (media_channel_ && ssrc_.has_value() && !stopped_) { |
| 126 | worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] { |
| 127 | media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_); |
| 128 | }); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | rtc::scoped_refptr<FrameDecryptorInterface> |
| 133 | AudioRtpReceiver::GetFrameDecryptor() const { |
| 134 | return frame_decryptor_; |
| 135 | } |
| 136 | |
| 137 | void AudioRtpReceiver::Stop() { |
| 138 | // TODO(deadbeef): Need to do more here to fully stop receiving packets. |
| 139 | if (stopped_) { |
| 140 | return; |
| 141 | } |
| 142 | if (media_channel_ && ssrc_) { |
| 143 | // Allow that SetOutputVolume fail. This is the normal case when the |
| 144 | // underlying media channel has already been deleted. |
| 145 | SetOutputVolume(0.0); |
| 146 | } |
| 147 | stopped_ = true; |
| 148 | } |
| 149 | |
| 150 | void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) { |
| 151 | if (!media_channel_) { |
| 152 | RTC_LOG(LS_ERROR) |
| 153 | << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists."; |
| 154 | return; |
| 155 | } |
| 156 | if (ssrc_ == ssrc) { |
| 157 | return; |
| 158 | } |
| 159 | if (ssrc_) { |
| 160 | source_->Stop(media_channel_, *ssrc_); |
| 161 | } |
| 162 | ssrc_ = ssrc; |
| 163 | source_->Start(media_channel_, *ssrc_); |
| 164 | Reconfigure(); |
| 165 | } |
| 166 | |
| 167 | void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) { |
| 168 | SetStreams(CreateStreamsFromIds(std::move(stream_ids))); |
| 169 | } |
| 170 | |
| 171 | void AudioRtpReceiver::SetStreams( |
| 172 | const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) { |
| 173 | // Remove remote track from any streams that are going away. |
| 174 | for (const auto& existing_stream : streams_) { |
| 175 | bool removed = true; |
| 176 | for (const auto& stream : streams) { |
| 177 | if (existing_stream->id() == stream->id()) { |
| 178 | RTC_DCHECK_EQ(existing_stream.get(), stream.get()); |
| 179 | removed = false; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | if (removed) { |
| 184 | existing_stream->RemoveTrack(track_); |
| 185 | } |
| 186 | } |
| 187 | // Add remote track to any streams that are new. |
| 188 | for (const auto& stream : streams) { |
| 189 | bool added = true; |
| 190 | for (const auto& existing_stream : streams_) { |
| 191 | if (stream->id() == existing_stream->id()) { |
| 192 | RTC_DCHECK_EQ(stream.get(), existing_stream.get()); |
| 193 | added = false; |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | if (added) { |
| 198 | stream->AddTrack(track_); |
| 199 | } |
| 200 | } |
| 201 | streams_ = streams; |
| 202 | } |
| 203 | |
| 204 | std::vector<RtpSource> AudioRtpReceiver::GetSources() const { |
| 205 | if (!media_channel_ || !ssrc_ || stopped_) { |
| 206 | return {}; |
| 207 | } |
| 208 | return worker_thread_->Invoke<std::vector<RtpSource>>( |
| 209 | RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); }); |
| 210 | } |
| 211 | |
| 212 | void AudioRtpReceiver::Reconfigure() { |
| 213 | RTC_DCHECK(!stopped_); |
| 214 | if (!media_channel_ || !ssrc_) { |
| 215 | RTC_LOG(LS_ERROR) |
| 216 | << "AudioRtpReceiver::Reconfigure: No audio channel exists."; |
| 217 | return; |
| 218 | } |
| 219 | if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) { |
| 220 | RTC_NOTREACHED(); |
| 221 | } |
| 222 | // Reattach the frame decryptor if we were reconfigured. |
| 223 | MaybeAttachFrameDecryptorToMediaChannel( |
| 224 | ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_); |
| 225 | } |
| 226 | |
| 227 | void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) { |
| 228 | observer_ = observer; |
| 229 | // Deliver any notifications the observer may have missed by being set late. |
| 230 | if (received_first_packet_ && observer_) { |
| 231 | observer_->OnFirstPacketReceived(media_type()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) { |
| 236 | RTC_DCHECK(media_channel == nullptr || |
| 237 | media_channel->media_type() == media_type()); |
| 238 | media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel); |
| 239 | } |
| 240 | |
| 241 | void AudioRtpReceiver::NotifyFirstPacketReceived() { |
| 242 | if (observer_) { |
| 243 | observer_->OnFirstPacketReceived(media_type()); |
| 244 | } |
| 245 | received_first_packet_ = true; |
| 246 | } |
| 247 | |
| 248 | } // namespace webrtc |