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.

170 lines
5.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2014 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/remote_audio_source.h"
  11. #include <stddef.h>
  12. #include <memory>
  13. #include <string>
  14. #include "absl/algorithm/container.h"
  15. #include "api/scoped_refptr.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/constructor_magic.h"
  18. #include "rtc_base/location.h"
  19. #include "rtc_base/logging.h"
  20. #include "rtc_base/numerics/safe_conversions.h"
  21. #include "rtc_base/thread.h"
  22. #include "rtc_base/thread_checker.h"
  23. namespace webrtc {
  24. // This proxy is passed to the underlying media engine to receive audio data as
  25. // they come in. The data will then be passed back up to the RemoteAudioSource
  26. // which will fan it out to all the sinks that have been added to it.
  27. class RemoteAudioSource::AudioDataProxy : public AudioSinkInterface {
  28. public:
  29. explicit AudioDataProxy(RemoteAudioSource* source) : source_(source) {
  30. RTC_DCHECK(source);
  31. }
  32. ~AudioDataProxy() override { source_->OnAudioChannelGone(); }
  33. // AudioSinkInterface implementation.
  34. void OnData(const AudioSinkInterface::Data& audio) override {
  35. source_->OnData(audio);
  36. }
  37. private:
  38. const rtc::scoped_refptr<RemoteAudioSource> source_;
  39. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(AudioDataProxy);
  40. };
  41. RemoteAudioSource::RemoteAudioSource(rtc::Thread* worker_thread)
  42. : main_thread_(rtc::Thread::Current()),
  43. worker_thread_(worker_thread),
  44. state_(MediaSourceInterface::kLive) {
  45. RTC_DCHECK(main_thread_);
  46. RTC_DCHECK(worker_thread_);
  47. }
  48. RemoteAudioSource::~RemoteAudioSource() {
  49. RTC_DCHECK(main_thread_->IsCurrent());
  50. RTC_DCHECK(audio_observers_.empty());
  51. RTC_DCHECK(sinks_.empty());
  52. }
  53. void RemoteAudioSource::Start(cricket::VoiceMediaChannel* media_channel,
  54. uint32_t ssrc) {
  55. RTC_DCHECK_RUN_ON(main_thread_);
  56. RTC_DCHECK(media_channel);
  57. // Register for callbacks immediately before AddSink so that we always get
  58. // notified when a channel goes out of scope (signaled when "AudioDataProxy"
  59. // is destroyed).
  60. worker_thread_->Invoke<void>(RTC_FROM_HERE, [&] {
  61. media_channel->SetRawAudioSink(ssrc,
  62. std::make_unique<AudioDataProxy>(this));
  63. });
  64. }
  65. void RemoteAudioSource::Stop(cricket::VoiceMediaChannel* media_channel,
  66. uint32_t ssrc) {
  67. RTC_DCHECK_RUN_ON(main_thread_);
  68. RTC_DCHECK(media_channel);
  69. worker_thread_->Invoke<void>(
  70. RTC_FROM_HERE, [&] { media_channel->SetRawAudioSink(ssrc, nullptr); });
  71. }
  72. MediaSourceInterface::SourceState RemoteAudioSource::state() const {
  73. RTC_DCHECK(main_thread_->IsCurrent());
  74. return state_;
  75. }
  76. bool RemoteAudioSource::remote() const {
  77. RTC_DCHECK(main_thread_->IsCurrent());
  78. return true;
  79. }
  80. void RemoteAudioSource::SetVolume(double volume) {
  81. RTC_DCHECK_GE(volume, 0);
  82. RTC_DCHECK_LE(volume, 10);
  83. for (auto* observer : audio_observers_) {
  84. observer->OnSetVolume(volume);
  85. }
  86. }
  87. void RemoteAudioSource::RegisterAudioObserver(AudioObserver* observer) {
  88. RTC_DCHECK(observer != NULL);
  89. RTC_DCHECK(!absl::c_linear_search(audio_observers_, observer));
  90. audio_observers_.push_back(observer);
  91. }
  92. void RemoteAudioSource::UnregisterAudioObserver(AudioObserver* observer) {
  93. RTC_DCHECK(observer != NULL);
  94. audio_observers_.remove(observer);
  95. }
  96. void RemoteAudioSource::AddSink(AudioTrackSinkInterface* sink) {
  97. RTC_DCHECK(main_thread_->IsCurrent());
  98. RTC_DCHECK(sink);
  99. if (state_ != MediaSourceInterface::kLive) {
  100. RTC_LOG(LS_ERROR) << "Can't register sink as the source isn't live.";
  101. return;
  102. }
  103. rtc::CritScope lock(&sink_lock_);
  104. RTC_DCHECK(!absl::c_linear_search(sinks_, sink));
  105. sinks_.push_back(sink);
  106. }
  107. void RemoteAudioSource::RemoveSink(AudioTrackSinkInterface* sink) {
  108. RTC_DCHECK(main_thread_->IsCurrent());
  109. RTC_DCHECK(sink);
  110. rtc::CritScope lock(&sink_lock_);
  111. sinks_.remove(sink);
  112. }
  113. void RemoteAudioSource::OnData(const AudioSinkInterface::Data& audio) {
  114. // Called on the externally-owned audio callback thread, via/from webrtc.
  115. rtc::CritScope lock(&sink_lock_);
  116. for (auto* sink : sinks_) {
  117. sink->OnData(audio.data, 16, audio.sample_rate, audio.channels,
  118. audio.samples_per_channel);
  119. }
  120. }
  121. void RemoteAudioSource::OnAudioChannelGone() {
  122. // Called when the audio channel is deleted. It may be the worker thread
  123. // in libjingle or may be a different worker thread.
  124. // This object needs to live long enough for the cleanup logic in OnMessage to
  125. // run, so take a reference to it as the data. Sometimes the message may not
  126. // be processed (because the thread was destroyed shortly after this call),
  127. // but that is fine because the thread destructor will take care of destroying
  128. // the message data which will release the reference on RemoteAudioSource.
  129. main_thread_->Post(RTC_FROM_HERE, this, 0,
  130. new rtc::ScopedRefMessageData<RemoteAudioSource>(this));
  131. }
  132. void RemoteAudioSource::OnMessage(rtc::Message* msg) {
  133. RTC_DCHECK(main_thread_->IsCurrent());
  134. sinks_.clear();
  135. state_ = MediaSourceInterface::kEnded;
  136. FireOnChanged();
  137. // Will possibly delete this RemoteAudioSource since it is reference counted
  138. // in the message.
  139. delete msg->pdata;
  140. }
  141. } // namespace webrtc