tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

audio_state.cc (7180B)


      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 
     11 #include "audio/audio_state.h"
     12 
     13 #include <algorithm>
     14 #include <cstddef>
     15 #include <cstdint>
     16 #include <utility>
     17 #include <vector>
     18 
     19 #include "api/audio/audio_device.h"
     20 #include "api/audio/audio_device_defines.h"
     21 #include "api/audio/audio_processing.h"
     22 #include "api/make_ref_counted.h"
     23 #include "api/scoped_refptr.h"
     24 #include "api/sequence_checker.h"
     25 #include "api/task_queue/task_queue_base.h"
     26 #include "api/units/time_delta.h"
     27 #include "audio/audio_send_stream.h"
     28 #include "call/audio_receive_stream.h"
     29 #include "call/audio_sender.h"
     30 #include "call/audio_state.h"
     31 #include "rtc_base/checks.h"
     32 #include "rtc_base/logging.h"
     33 #include "rtc_base/task_utils/repeating_task.h"
     34 
     35 namespace webrtc {
     36 namespace internal {
     37 
     38 AudioState::AudioState(const AudioState::Config& config)
     39    : config_(config),
     40      audio_transport_(config_.audio_mixer.get(),
     41                       config_.audio_processing.get(),
     42                       config_.async_audio_processing_factory.get()) {
     43  RTC_DCHECK(config_.audio_mixer);
     44  RTC_DCHECK(config_.audio_device_module);
     45 }
     46 
     47 AudioState::~AudioState() {
     48  RTC_DCHECK_RUN_ON(&thread_checker_);
     49  RTC_DCHECK(receiving_streams_.empty());
     50  RTC_DCHECK(sending_streams_.empty());
     51  RTC_DCHECK(!null_audio_poller_.Running());
     52 }
     53 
     54 AudioProcessing* AudioState::audio_processing() {
     55  return config_.audio_processing.get();
     56 }
     57 
     58 AudioTransport* AudioState::audio_transport() {
     59  return &audio_transport_;
     60 }
     61 
     62 void AudioState::SetPlayout(bool enabled) {
     63  RTC_LOG(LS_INFO) << "SetPlayout(" << enabled << ")";
     64  RTC_DCHECK_RUN_ON(&thread_checker_);
     65  auto* adm = config_.audio_device_module.get();
     66  if (enabled) {
     67    if (!receiving_streams_.empty()) {
     68      if (!adm->Playing()) {
     69        if (adm->InitPlayout() == 0) {
     70          adm->StartPlayout();
     71        }
     72      }
     73    }
     74  } else {
     75    // Disable playout.
     76    config_.audio_device_module->StopPlayout();
     77  }
     78  playout_enabled_ = enabled;
     79  UpdateNullAudioPollerState();
     80 }
     81 
     82 void AudioState::AddReceivingStream(
     83    webrtc::AudioReceiveStreamInterface* stream) {
     84  RTC_DCHECK_RUN_ON(&thread_checker_);
     85  RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
     86  receiving_streams_.insert(stream);
     87  if (!config_.audio_mixer->AddSource(stream->source())) {
     88    RTC_DLOG(LS_ERROR) << "Failed to add source to mixer.";
     89  }
     90 
     91  // Make sure playback is initialized; start playing if enabled.
     92  if (playout_enabled_) {
     93    auto* adm = config_.audio_device_module.get();
     94    if (!adm->Playing()) {
     95      if (adm->InitPlayout() == 0) {
     96        adm->StartPlayout();
     97      } else {
     98        RTC_DLOG_F(LS_ERROR) << "Failed to initialize playout.";
     99      }
    100    }
    101  }
    102  UpdateNullAudioPollerState();
    103 }
    104 
    105 void AudioState::RemoveReceivingStream(
    106    webrtc::AudioReceiveStreamInterface* stream) {
    107  RTC_DCHECK_RUN_ON(&thread_checker_);
    108  auto count = receiving_streams_.erase(stream);
    109  RTC_DCHECK_EQ(1, count);
    110  config_.audio_mixer->RemoveSource(stream->source());
    111  if (receiving_streams_.empty()) {
    112    config_.audio_device_module->StopPlayout();
    113  }
    114  UpdateNullAudioPollerState();
    115 }
    116 
    117 void AudioState::SetRecording(bool enabled) {
    118  RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")";
    119  RTC_DCHECK_RUN_ON(&thread_checker_);
    120  auto* adm = config_.audio_device_module.get();
    121  if (enabled) {
    122    if (!sending_streams_.empty()) {
    123      if (!adm->Recording()) {
    124        if (adm->InitRecording() == 0) {
    125          adm->StartRecording();
    126        }
    127      }
    128    }
    129  } else {
    130    // Disable recording.
    131    adm->StopRecording();
    132  }
    133  recording_enabled_ = enabled;
    134 }
    135 
    136 void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
    137                                  int sample_rate_hz,
    138                                  size_t num_channels) {
    139  RTC_DCHECK_RUN_ON(&thread_checker_);
    140  auto& properties = sending_streams_[stream];
    141  properties.sample_rate_hz = sample_rate_hz;
    142  properties.num_channels = num_channels;
    143  UpdateAudioTransportWithSendingStreams();
    144 
    145  // Make sure recording is initialized; start recording if enabled.
    146  auto* adm = config_.audio_device_module.get();
    147  if (recording_enabled_) {
    148    if (!adm->Recording()) {
    149      if (adm->InitRecording() == 0) {
    150        adm->StartRecording();
    151      } else {
    152        RTC_DLOG_F(LS_ERROR) << "Failed to initialize recording.";
    153      }
    154    }
    155  }
    156 }
    157 
    158 void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
    159  RTC_DCHECK_RUN_ON(&thread_checker_);
    160  auto count = sending_streams_.erase(stream);
    161  RTC_DCHECK_EQ(1, count);
    162  UpdateAudioTransportWithSendingStreams();
    163  if (sending_streams_.empty()) {
    164    config_.audio_device_module->StopRecording();
    165  }
    166 }
    167 
    168 void AudioState::SetStereoChannelSwapping(bool enable) {
    169  RTC_DCHECK(thread_checker_.IsCurrent());
    170  audio_transport_.SetStereoChannelSwapping(enable);
    171 }
    172 
    173 void AudioState::UpdateAudioTransportWithSendingStreams() {
    174  RTC_DCHECK(thread_checker_.IsCurrent());
    175  std::vector<AudioSender*> audio_senders;
    176  int max_sample_rate_hz = 8000;
    177  size_t max_num_channels = 1;
    178  for (const auto& kv : sending_streams_) {
    179    audio_senders.push_back(kv.first);
    180    max_sample_rate_hz = std::max(max_sample_rate_hz, kv.second.sample_rate_hz);
    181    max_num_channels = std::max(max_num_channels, kv.second.num_channels);
    182  }
    183  audio_transport_.UpdateAudioSenders(std::move(audio_senders),
    184                                      max_sample_rate_hz, max_num_channels);
    185 }
    186 
    187 void AudioState::UpdateNullAudioPollerState() {
    188  // Run NullAudioPoller when there are receiving streams and playout is
    189  // disabled.
    190  if (!receiving_streams_.empty() && !playout_enabled_) {
    191    if (!null_audio_poller_.Running()) {
    192      AudioTransport* audio_transport = &audio_transport_;
    193      null_audio_poller_ = RepeatingTaskHandle::Start(
    194          TaskQueueBase::Current(), [audio_transport] {
    195            static constexpr size_t kNumChannels = 1;
    196            static constexpr uint32_t kSamplesPerSecond = 48'000;
    197            // 10ms of samples
    198            static constexpr size_t kNumSamples = kSamplesPerSecond / 100;
    199 
    200            // Buffer to hold the audio samples.
    201            int16_t buffer[kNumSamples * kNumChannels];
    202 
    203            // Output variables from `NeedMorePlayData`.
    204            size_t n_samples;
    205            int64_t elapsed_time_ms;
    206            int64_t ntp_time_ms;
    207            audio_transport->NeedMorePlayData(
    208                kNumSamples, sizeof(int16_t), kNumChannels, kSamplesPerSecond,
    209                buffer, n_samples, &elapsed_time_ms, &ntp_time_ms);
    210 
    211            // Reschedule the next poll iteration.
    212            return TimeDelta::Millis(10);
    213          });
    214    }
    215  } else {
    216    null_audio_poller_.Stop();
    217  }
    218 }
    219 }  // namespace internal
    220 
    221 scoped_refptr<AudioState> AudioState::Create(const AudioState::Config& config) {
    222  return make_ref_counted<internal::AudioState>(config);
    223 }
    224 }  // namespace webrtc