blob: d67b249630b3391ec80b4ef40322052d9d8c56df [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020014
Ruslan Burakov501bfba2019-02-11 10:29:19 +010015#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 Burakov428dcb22019-04-18 17:49:49 +020021#include "pc/jitter_buffer_delay.h"
22#include "pc/jitter_buffer_delay_proxy.h"
Ruslan Burakov501bfba2019-02-11 10:29:19 +010023#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
29namespace webrtc {
30
31AudioRtpReceiver::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
38AudioRtpReceiver::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 Burakov428dcb22019-04-18 17:49:49 +020048 attachment_id_(GenerateUniqueId()),
49 delay_(JitterBufferDelayProxy::Create(
50 rtc::Thread::Current(),
51 worker_thread_,
52 new rtc::RefCountedObject<JitterBufferDelay>(worker_thread))) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010053 RTC_DCHECK(worker_thread_);
54 RTC_DCHECK(track_->GetSource()->remote());
55 track_->RegisterObserver(this);
56 track_->GetSource()->RegisterAudioObserver(this);
57 SetStreams(streams);
58}
59
60AudioRtpReceiver::~AudioRtpReceiver() {
61 track_->GetSource()->UnregisterAudioObserver(this);
62 track_->UnregisterObserver(this);
63 Stop();
64}
65
66void AudioRtpReceiver::OnChanged() {
67 if (cached_track_enabled_ != track_->enabled()) {
68 cached_track_enabled_ = track_->enabled();
69 Reconfigure();
70 }
71}
72
73bool AudioRtpReceiver::SetOutputVolume(double volume) {
74 RTC_DCHECK_GE(volume, 0.0);
75 RTC_DCHECK_LE(volume, 10.0);
76 RTC_DCHECK(media_channel_);
Saurav Das7262fc22019-09-11 16:23:05 -070077 RTC_DCHECK(!stopped_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010078 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Saurav Das7262fc22019-09-11 16:23:05 -070079 // TODO(bugs.webrtc.org/8694): Stop using 0 to mean unsignalled SSRC value.
80 return media_channel_->SetOutputVolume(ssrc_.value_or(0), volume);
Ruslan Burakov501bfba2019-02-11 10:29:19 +010081 });
82}
83
84void AudioRtpReceiver::OnSetVolume(double volume) {
85 RTC_DCHECK_GE(volume, 0);
86 RTC_DCHECK_LE(volume, 10);
87 cached_volume_ = volume;
Saurav Das7262fc22019-09-11 16:23:05 -070088 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +010089 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
103std::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
110RtpParameters AudioRtpReceiver::GetParameters() const {
Saurav Das7262fc22019-09-11 16:23:05 -0700111 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100112 return RtpParameters();
113 }
114 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
Saurav Das7262fc22019-09-11 16:23:05 -0700115 // TODO(bugs.webrtc.org/8694): Stop using 0 to mean unsignalled SSRC value.
116 return media_channel_->GetRtpReceiveParameters(ssrc_.value_or(0));
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100117 });
118}
119
120bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
121 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
Saurav Das7262fc22019-09-11 16:23:05 -0700122 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100123 return false;
124 }
125 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
Saurav Das7262fc22019-09-11 16:23:05 -0700126 return media_channel_->SetRtpReceiveParameters(ssrc_.value_or(0),
127 parameters);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100128 });
129}
130
131void AudioRtpReceiver::SetFrameDecryptor(
132 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
133 frame_decryptor_ = std::move(frame_decryptor);
134 // Special Case: Set the frame decryptor to any value on any existing channel.
135 if (media_channel_ && ssrc_.has_value() && !stopped_) {
136 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
137 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
138 });
139 }
140}
141
142rtc::scoped_refptr<FrameDecryptorInterface>
143AudioRtpReceiver::GetFrameDecryptor() const {
144 return frame_decryptor_;
145}
146
147void AudioRtpReceiver::Stop() {
148 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
149 if (stopped_) {
150 return;
151 }
Saurav Das7262fc22019-09-11 16:23:05 -0700152 if (media_channel_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100153 // Allow that SetOutputVolume fail. This is the normal case when the
154 // underlying media channel has already been deleted.
155 SetOutputVolume(0.0);
156 }
157 stopped_ = true;
158}
159
Saurav Das7262fc22019-09-11 16:23:05 -0700160void AudioRtpReceiver::RestartMediaChannel(absl::optional<uint32_t> ssrc) {
161 RTC_DCHECK(media_channel_);
162 if (!stopped_ && ssrc_ == ssrc) {
163 return;
164 }
165
166 if (!stopped_) {
167 source_->Stop(media_channel_, ssrc_.value_or(0));
168 delay_->OnStop();
169 }
170 ssrc_ = ssrc;
171 stopped_ = false;
172 source_->Start(media_channel_, ssrc.value_or(0));
173 delay_->OnStart(media_channel_, ssrc.value_or(0));
174 Reconfigure();
175}
176
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100177void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
178 if (!media_channel_) {
179 RTC_LOG(LS_ERROR)
180 << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
181 return;
182 }
Saurav Das7262fc22019-09-11 16:23:05 -0700183 RestartMediaChannel(ssrc);
184}
185
186void AudioRtpReceiver::SetupUnsignaledMediaChannel() {
187 if (!media_channel_) {
188 RTC_LOG(LS_ERROR) << "AudioRtpReceiver::SetupUnsignaledMediaChannel: No "
189 "audio channel exists.";
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100190 }
Saurav Das7262fc22019-09-11 16:23:05 -0700191 RestartMediaChannel(absl::nullopt);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100192}
193
194void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
195 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
196}
197
198void AudioRtpReceiver::SetStreams(
199 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
200 // Remove remote track from any streams that are going away.
201 for (const auto& existing_stream : streams_) {
202 bool removed = true;
203 for (const auto& stream : streams) {
204 if (existing_stream->id() == stream->id()) {
205 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
206 removed = false;
207 break;
208 }
209 }
210 if (removed) {
211 existing_stream->RemoveTrack(track_);
212 }
213 }
214 // Add remote track to any streams that are new.
215 for (const auto& stream : streams) {
216 bool added = true;
217 for (const auto& existing_stream : streams_) {
218 if (stream->id() == existing_stream->id()) {
219 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
220 added = false;
221 break;
222 }
223 }
224 if (added) {
225 stream->AddTrack(track_);
226 }
227 }
228 streams_ = streams;
229}
230
231std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
232 if (!media_channel_ || !ssrc_ || stopped_) {
233 return {};
234 }
235 return worker_thread_->Invoke<std::vector<RtpSource>>(
236 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
237}
238
239void AudioRtpReceiver::Reconfigure() {
Saurav Das7262fc22019-09-11 16:23:05 -0700240 if (!media_channel_ || stopped_) {
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100241 RTC_LOG(LS_ERROR)
242 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
243 return;
244 }
245 if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
246 RTC_NOTREACHED();
247 }
248 // Reattach the frame decryptor if we were reconfigured.
249 MaybeAttachFrameDecryptorToMediaChannel(
250 ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
251}
252
253void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
254 observer_ = observer;
255 // Deliver any notifications the observer may have missed by being set late.
256 if (received_first_packet_ && observer_) {
257 observer_->OnFirstPacketReceived(media_type());
258 }
259}
260
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200261void AudioRtpReceiver::SetJitterBufferMinimumDelay(
262 absl::optional<double> delay_seconds) {
Ruslan Burakov428dcb22019-04-18 17:49:49 +0200263 delay_->Set(delay_seconds);
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200264}
265
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100266void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
267 RTC_DCHECK(media_channel == nullptr ||
268 media_channel->media_type() == media_type());
269 media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
270}
271
272void AudioRtpReceiver::NotifyFirstPacketReceived() {
273 if (observer_) {
274 observer_->OnFirstPacketReceived(media_type());
275 }
276 received_first_packet_ = true;
277}
278
279} // namespace webrtc