You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
8.7 KiB

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