blob: 9bf66594b084c10978b74ee80dd6baf020db3179 [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_);
77 RTC_DCHECK(ssrc_);
78 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
79 return media_channel_->SetOutputVolume(*ssrc_, volume);
80 });
81}
82
83void AudioRtpReceiver::OnSetVolume(double volume) {
84 RTC_DCHECK_GE(volume, 0);
85 RTC_DCHECK_LE(volume, 10);
86 cached_volume_ = volume;
87 if (!media_channel_ || !ssrc_) {
88 RTC_LOG(LS_ERROR)
89 << "AudioRtpReceiver::OnSetVolume: No audio channel exists.";
90 return;
91 }
92 // When the track is disabled, the volume of the source, which is the
93 // corresponding WebRtc Voice Engine channel will be 0. So we do not allow
94 // setting the volume to the source when the track is disabled.
95 if (!stopped_ && track_->enabled()) {
96 if (!SetOutputVolume(cached_volume_)) {
97 RTC_NOTREACHED();
98 }
99 }
100}
101
102std::vector<std::string> AudioRtpReceiver::stream_ids() const {
103 std::vector<std::string> stream_ids(streams_.size());
104 for (size_t i = 0; i < streams_.size(); ++i)
105 stream_ids[i] = streams_[i]->id();
106 return stream_ids;
107}
108
109RtpParameters AudioRtpReceiver::GetParameters() const {
110 if (!media_channel_ || !ssrc_ || stopped_) {
111 return RtpParameters();
112 }
113 return worker_thread_->Invoke<RtpParameters>(RTC_FROM_HERE, [&] {
114 return media_channel_->GetRtpReceiveParameters(*ssrc_);
115 });
116}
117
118bool AudioRtpReceiver::SetParameters(const RtpParameters& parameters) {
119 TRACE_EVENT0("webrtc", "AudioRtpReceiver::SetParameters");
120 if (!media_channel_ || !ssrc_ || stopped_) {
121 return false;
122 }
123 return worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
124 return media_channel_->SetRtpReceiveParameters(*ssrc_, parameters);
125 });
126}
127
128void AudioRtpReceiver::SetFrameDecryptor(
129 rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
130 frame_decryptor_ = std::move(frame_decryptor);
131 // Special Case: Set the frame decryptor to any value on any existing channel.
132 if (media_channel_ && ssrc_.has_value() && !stopped_) {
133 worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
134 media_channel_->SetFrameDecryptor(*ssrc_, frame_decryptor_);
135 });
136 }
137}
138
139rtc::scoped_refptr<FrameDecryptorInterface>
140AudioRtpReceiver::GetFrameDecryptor() const {
141 return frame_decryptor_;
142}
143
144void AudioRtpReceiver::Stop() {
145 // TODO(deadbeef): Need to do more here to fully stop receiving packets.
146 if (stopped_) {
147 return;
148 }
149 if (media_channel_ && ssrc_) {
150 // Allow that SetOutputVolume fail. This is the normal case when the
151 // underlying media channel has already been deleted.
152 SetOutputVolume(0.0);
153 }
154 stopped_ = true;
155}
156
157void AudioRtpReceiver::SetupMediaChannel(uint32_t ssrc) {
158 if (!media_channel_) {
159 RTC_LOG(LS_ERROR)
160 << "AudioRtpReceiver::SetupMediaChannel: No audio channel exists.";
161 return;
162 }
163 if (ssrc_ == ssrc) {
164 return;
165 }
166 if (ssrc_) {
167 source_->Stop(media_channel_, *ssrc_);
Ruslan Burakov428dcb22019-04-18 17:49:49 +0200168 delay_->OnStop();
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100169 }
170 ssrc_ = ssrc;
171 source_->Start(media_channel_, *ssrc_);
Ruslan Burakov428dcb22019-04-18 17:49:49 +0200172 delay_->OnStart(media_channel_, *ssrc_);
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100173 Reconfigure();
174}
175
176void AudioRtpReceiver::set_stream_ids(std::vector<std::string> stream_ids) {
177 SetStreams(CreateStreamsFromIds(std::move(stream_ids)));
178}
179
180void AudioRtpReceiver::SetStreams(
181 const std::vector<rtc::scoped_refptr<MediaStreamInterface>>& streams) {
182 // Remove remote track from any streams that are going away.
183 for (const auto& existing_stream : streams_) {
184 bool removed = true;
185 for (const auto& stream : streams) {
186 if (existing_stream->id() == stream->id()) {
187 RTC_DCHECK_EQ(existing_stream.get(), stream.get());
188 removed = false;
189 break;
190 }
191 }
192 if (removed) {
193 existing_stream->RemoveTrack(track_);
194 }
195 }
196 // Add remote track to any streams that are new.
197 for (const auto& stream : streams) {
198 bool added = true;
199 for (const auto& existing_stream : streams_) {
200 if (stream->id() == existing_stream->id()) {
201 RTC_DCHECK_EQ(stream.get(), existing_stream.get());
202 added = false;
203 break;
204 }
205 }
206 if (added) {
207 stream->AddTrack(track_);
208 }
209 }
210 streams_ = streams;
211}
212
213std::vector<RtpSource> AudioRtpReceiver::GetSources() const {
214 if (!media_channel_ || !ssrc_ || stopped_) {
215 return {};
216 }
217 return worker_thread_->Invoke<std::vector<RtpSource>>(
218 RTC_FROM_HERE, [&] { return media_channel_->GetSources(*ssrc_); });
219}
220
221void AudioRtpReceiver::Reconfigure() {
222 RTC_DCHECK(!stopped_);
223 if (!media_channel_ || !ssrc_) {
224 RTC_LOG(LS_ERROR)
225 << "AudioRtpReceiver::Reconfigure: No audio channel exists.";
226 return;
227 }
228 if (!SetOutputVolume(track_->enabled() ? cached_volume_ : 0)) {
229 RTC_NOTREACHED();
230 }
231 // Reattach the frame decryptor if we were reconfigured.
232 MaybeAttachFrameDecryptorToMediaChannel(
233 ssrc_, worker_thread_, frame_decryptor_, media_channel_, stopped_);
234}
235
236void AudioRtpReceiver::SetObserver(RtpReceiverObserverInterface* observer) {
237 observer_ = observer;
238 // Deliver any notifications the observer may have missed by being set late.
239 if (received_first_packet_ && observer_) {
240 observer_->OnFirstPacketReceived(media_type());
241 }
242}
243
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200244void AudioRtpReceiver::SetJitterBufferMinimumDelay(
245 absl::optional<double> delay_seconds) {
Ruslan Burakov428dcb22019-04-18 17:49:49 +0200246 delay_->Set(delay_seconds);
Ruslan Burakov4bac79e2019-04-03 19:55:33 +0200247}
248
Ruslan Burakov501bfba2019-02-11 10:29:19 +0100249void AudioRtpReceiver::SetMediaChannel(cricket::MediaChannel* media_channel) {
250 RTC_DCHECK(media_channel == nullptr ||
251 media_channel->media_type() == media_type());
252 media_channel_ = static_cast<cricket::VoiceMediaChannel*>(media_channel);
253}
254
255void AudioRtpReceiver::NotifyFirstPacketReceived() {
256 if (observer_) {
257 observer_->OnFirstPacketReceived(media_type());
258 }
259 received_first_packet_ = true;
260}
261
262} // namespace webrtc