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