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.

190 lines
5.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2015 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 "audio/audio_state.h"
  11. #include <algorithm>
  12. #include <memory>
  13. #include <utility>
  14. #include <vector>
  15. #include "audio/audio_receive_stream.h"
  16. #include "modules/audio_device/include/audio_device.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/logging.h"
  19. #include "rtc_base/ref_counted_object.h"
  20. #include "rtc_base/thread.h"
  21. namespace webrtc {
  22. namespace internal {
  23. AudioState::AudioState(const AudioState::Config& config)
  24. : config_(config),
  25. audio_transport_(config_.audio_mixer, config_.audio_processing.get()) {
  26. process_thread_checker_.Detach();
  27. RTC_DCHECK(config_.audio_mixer);
  28. RTC_DCHECK(config_.audio_device_module);
  29. }
  30. AudioState::~AudioState() {
  31. RTC_DCHECK(thread_checker_.IsCurrent());
  32. RTC_DCHECK(receiving_streams_.empty());
  33. RTC_DCHECK(sending_streams_.empty());
  34. }
  35. AudioProcessing* AudioState::audio_processing() {
  36. RTC_DCHECK(config_.audio_processing);
  37. return config_.audio_processing.get();
  38. }
  39. AudioTransport* AudioState::audio_transport() {
  40. return &audio_transport_;
  41. }
  42. bool AudioState::typing_noise_detected() const {
  43. RTC_DCHECK(thread_checker_.IsCurrent());
  44. return audio_transport_.typing_noise_detected();
  45. }
  46. void AudioState::AddReceivingStream(webrtc::AudioReceiveStream* stream) {
  47. RTC_DCHECK(thread_checker_.IsCurrent());
  48. RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
  49. receiving_streams_.insert(stream);
  50. if (!config_.audio_mixer->AddSource(
  51. static_cast<internal::AudioReceiveStream*>(stream))) {
  52. RTC_DLOG(LS_ERROR) << "Failed to add source to mixer.";
  53. }
  54. // Make sure playback is initialized; start playing if enabled.
  55. UpdateNullAudioPollerState();
  56. auto* adm = config_.audio_device_module.get();
  57. if (!adm->Playing()) {
  58. if (adm->InitPlayout() == 0) {
  59. if (playout_enabled_) {
  60. adm->StartPlayout();
  61. }
  62. } else {
  63. RTC_DLOG_F(LS_ERROR) << "Failed to initialize playout.";
  64. }
  65. }
  66. }
  67. void AudioState::RemoveReceivingStream(webrtc::AudioReceiveStream* stream) {
  68. RTC_DCHECK(thread_checker_.IsCurrent());
  69. auto count = receiving_streams_.erase(stream);
  70. RTC_DCHECK_EQ(1, count);
  71. config_.audio_mixer->RemoveSource(
  72. static_cast<internal::AudioReceiveStream*>(stream));
  73. UpdateNullAudioPollerState();
  74. if (receiving_streams_.empty()) {
  75. config_.audio_device_module->StopPlayout();
  76. }
  77. }
  78. void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
  79. int sample_rate_hz,
  80. size_t num_channels) {
  81. RTC_DCHECK(thread_checker_.IsCurrent());
  82. auto& properties = sending_streams_[stream];
  83. properties.sample_rate_hz = sample_rate_hz;
  84. properties.num_channels = num_channels;
  85. UpdateAudioTransportWithSendingStreams();
  86. // Make sure recording is initialized; start recording if enabled.
  87. auto* adm = config_.audio_device_module.get();
  88. if (!adm->Recording()) {
  89. if (adm->InitRecording() == 0) {
  90. if (recording_enabled_) {
  91. adm->StartRecording();
  92. }
  93. } else {
  94. RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
  95. }
  96. }
  97. }
  98. void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
  99. RTC_DCHECK(thread_checker_.IsCurrent());
  100. auto count = sending_streams_.erase(stream);
  101. RTC_DCHECK_EQ(1, count);
  102. UpdateAudioTransportWithSendingStreams();
  103. if (sending_streams_.empty()) {
  104. config_.audio_device_module->StopRecording();
  105. }
  106. }
  107. void AudioState::SetPlayout(bool enabled) {
  108. RTC_LOG(INFO) << "SetPlayout(" << enabled << ")";
  109. RTC_DCHECK(thread_checker_.IsCurrent());
  110. if (playout_enabled_ != enabled) {
  111. playout_enabled_ = enabled;
  112. if (enabled) {
  113. UpdateNullAudioPollerState();
  114. if (!receiving_streams_.empty()) {
  115. config_.audio_device_module->StartPlayout();
  116. }
  117. } else {
  118. config_.audio_device_module->StopPlayout();
  119. UpdateNullAudioPollerState();
  120. }
  121. }
  122. }
  123. void AudioState::SetRecording(bool enabled) {
  124. RTC_LOG(INFO) << "SetRecording(" << enabled << ")";
  125. RTC_DCHECK(thread_checker_.IsCurrent());
  126. if (recording_enabled_ != enabled) {
  127. recording_enabled_ = enabled;
  128. if (enabled) {
  129. if (!sending_streams_.empty()) {
  130. config_.audio_device_module->StartRecording();
  131. }
  132. } else {
  133. config_.audio_device_module->StopRecording();
  134. }
  135. }
  136. }
  137. void AudioState::SetStereoChannelSwapping(bool enable) {
  138. RTC_DCHECK(thread_checker_.IsCurrent());
  139. audio_transport_.SetStereoChannelSwapping(enable);
  140. }
  141. void AudioState::UpdateAudioTransportWithSendingStreams() {
  142. RTC_DCHECK(thread_checker_.IsCurrent());
  143. std::vector<webrtc::AudioSendStream*> sending_streams;
  144. int max_sample_rate_hz = 8000;
  145. size_t max_num_channels = 1;
  146. for (const auto& kv : sending_streams_) {
  147. sending_streams.push_back(kv.first);
  148. max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz);
  149. max_num_channels = std::max(max_num_channels, kv.second.num_channels);
  150. }
  151. audio_transport_.UpdateSendingStreams(std::move(sending_streams),
  152. max_sample_rate_hz, max_num_channels);
  153. }
  154. void AudioState::UpdateNullAudioPollerState() {
  155. // Run NullAudioPoller when there are receiving streams and playout is
  156. // disabled.
  157. if (!receiving_streams_.empty() && !playout_enabled_) {
  158. if (!null_audio_poller_)
  159. null_audio_poller_ = std::make_unique<NullAudioPoller>(&audio_transport_);
  160. } else {
  161. null_audio_poller_.reset();
  162. }
  163. }
  164. } // namespace internal
  165. rtc::scoped_refptr<AudioState> AudioState::Create(
  166. const AudioState::Config& config) {
  167. return new rtc::RefCountedObject<internal::AudioState>(config);
  168. }
  169. } // namespace webrtc