tor-browser

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

audio_device_data_observer.cc (11807B)


      1 /*
      2 *  Copyright (c) 2017 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 "modules/audio_device/include/audio_device_data_observer.h"
     12 
     13 #include <cstddef>
     14 #include <cstdint>
     15 #include <memory>
     16 #include <optional>
     17 #include <utility>
     18 
     19 #include "api/audio/audio_device.h"
     20 #include "api/audio/audio_device_defines.h"
     21 #include "api/make_ref_counted.h"
     22 #include "api/scoped_refptr.h"
     23 #include "rtc_base/checks.h"
     24 
     25 namespace webrtc {
     26 
     27 namespace {
     28 
     29 // A wrapper over AudioDeviceModule that registers itself as AudioTransport
     30 // callback and redirects the PCM data to AudioDeviceDataObserver callback.
     31 class ADMWrapper : public AudioDeviceModule, public AudioTransport {
     32 public:
     33  ADMWrapper(scoped_refptr<AudioDeviceModule> impl,
     34             std::unique_ptr<AudioDeviceDataObserver> observer)
     35      : impl_(std::move(impl)), observer_(std::move(observer)) {}
     36 
     37  ~ADMWrapper() override = default;
     38 
     39  int32_t RecordedDataIsAvailable(const void* audioSamples,
     40                                  size_t nSamples,
     41                                  size_t nBytesPerSample,
     42                                  size_t nChannels,
     43                                  uint32_t samples_per_sec,
     44                                  uint32_t total_delay_ms,
     45                                  int32_t clockDrift,
     46                                  uint32_t currentMicLevel,
     47                                  bool keyPressed,
     48                                  uint32_t& newMicLevel) override {
     49    return RecordedDataIsAvailable(
     50        audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
     51        total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel,
     52        /*capture_timestamp_ns=*/std::nullopt);
     53  }
     54 
     55  // AudioTransport methods overrides.
     56  int32_t RecordedDataIsAvailable(
     57      const void* audioSamples,
     58      size_t nSamples,
     59      size_t nBytesPerSample,
     60      size_t nChannels,
     61      uint32_t samples_per_sec,
     62      uint32_t total_delay_ms,
     63      int32_t clockDrift,
     64      uint32_t currentMicLevel,
     65      bool keyPressed,
     66      uint32_t& newMicLevel,
     67      std::optional<int64_t> capture_timestamp_ns) override {
     68    int32_t res = 0;
     69    // Capture PCM data of locally captured audio.
     70    if (observer_) {
     71      observer_->OnCaptureData(audioSamples, nSamples, nBytesPerSample,
     72                               nChannels, samples_per_sec);
     73    }
     74 
     75    // Send to the actual audio transport.
     76    if (audio_transport_) {
     77      res = audio_transport_->RecordedDataIsAvailable(
     78          audioSamples, nSamples, nBytesPerSample, nChannels, samples_per_sec,
     79          total_delay_ms, clockDrift, currentMicLevel, keyPressed, newMicLevel,
     80          capture_timestamp_ns);
     81    }
     82 
     83    return res;
     84  }
     85 
     86  int32_t NeedMorePlayData(const size_t nSamples,
     87                           const size_t nBytesPerSample,
     88                           const size_t nChannels,
     89                           const uint32_t samples_per_sec,
     90                           void* audioSamples,
     91                           size_t& nSamplesOut,
     92                           int64_t* elapsed_time_ms,
     93                           int64_t* ntp_time_ms) override {
     94    int32_t res = 0;
     95    // Set out parameters to safe values to be sure not to return corrupted
     96    // data.
     97    nSamplesOut = 0;
     98    *elapsed_time_ms = -1;
     99    *ntp_time_ms = -1;
    100    // Request data from audio transport.
    101    if (audio_transport_) {
    102      res = audio_transport_->NeedMorePlayData(
    103          nSamples, nBytesPerSample, nChannels, samples_per_sec, audioSamples,
    104          nSamplesOut, elapsed_time_ms, ntp_time_ms);
    105    }
    106 
    107    // Capture rendered data.
    108    if (observer_) {
    109      observer_->OnRenderData(audioSamples, nSamples, nBytesPerSample,
    110                              nChannels, samples_per_sec);
    111    }
    112 
    113    return res;
    114  }
    115 
    116  void PullRenderData(int /* bits_per_sample */,
    117                      int /* sample_rate */,
    118                      size_t /* number_of_channels */,
    119                      size_t /* number_of_frames */,
    120                      void* /* audio_data */,
    121                      int64_t* /* elapsed_time_ms */,
    122                      int64_t* /* ntp_time_ms */) override {
    123    RTC_DCHECK_NOTREACHED();
    124  }
    125 
    126  // Override AudioDeviceModule's RegisterAudioCallback method to remember the
    127  // actual audio transport (e.g.: voice engine).
    128  int32_t RegisterAudioCallback(AudioTransport* audio_callback) override {
    129    // Remember the audio callback to forward PCM data
    130    audio_transport_ = audio_callback;
    131    return 0;
    132  }
    133 
    134  // AudioDeviceModule pass through method overrides.
    135  int32_t ActiveAudioLayer(AudioLayer* audio_layer) const override {
    136    return impl_->ActiveAudioLayer(audio_layer);
    137  }
    138  int32_t Init() override {
    139    int res = impl_->Init();
    140    if (res != 0) {
    141      return res;
    142    }
    143    // Register self as the audio transport callback for underlying ADM impl.
    144    impl_->RegisterAudioCallback(this);
    145    return res;
    146  }
    147  int32_t Terminate() override { return impl_->Terminate(); }
    148  bool Initialized() const override { return impl_->Initialized(); }
    149  int16_t PlayoutDevices() override { return impl_->PlayoutDevices(); }
    150  int16_t RecordingDevices() override { return impl_->RecordingDevices(); }
    151  int32_t PlayoutDeviceName(uint16_t index,
    152                            char name[kAdmMaxDeviceNameSize],
    153                            char guid[kAdmMaxGuidSize]) override {
    154    return impl_->PlayoutDeviceName(index, name, guid);
    155  }
    156  int32_t RecordingDeviceName(uint16_t index,
    157                              char name[kAdmMaxDeviceNameSize],
    158                              char guid[kAdmMaxGuidSize]) override {
    159    return impl_->RecordingDeviceName(index, name, guid);
    160  }
    161  int32_t SetPlayoutDevice(uint16_t index) override {
    162    return impl_->SetPlayoutDevice(index);
    163  }
    164  int32_t SetPlayoutDevice(WindowsDeviceType device) override {
    165    return impl_->SetPlayoutDevice(device);
    166  }
    167  int32_t SetRecordingDevice(uint16_t index) override {
    168    return impl_->SetRecordingDevice(index);
    169  }
    170  int32_t SetRecordingDevice(WindowsDeviceType device) override {
    171    return impl_->SetRecordingDevice(device);
    172  }
    173  int32_t PlayoutIsAvailable(bool* available) override {
    174    return impl_->PlayoutIsAvailable(available);
    175  }
    176  int32_t InitPlayout() override { return impl_->InitPlayout(); }
    177  bool PlayoutIsInitialized() const override {
    178    return impl_->PlayoutIsInitialized();
    179  }
    180  int32_t RecordingIsAvailable(bool* available) override {
    181    return impl_->RecordingIsAvailable(available);
    182  }
    183  int32_t InitRecording() override { return impl_->InitRecording(); }
    184  bool RecordingIsInitialized() const override {
    185    return impl_->RecordingIsInitialized();
    186  }
    187  int32_t StartPlayout() override { return impl_->StartPlayout(); }
    188  int32_t StopPlayout() override { return impl_->StopPlayout(); }
    189  bool Playing() const override { return impl_->Playing(); }
    190  int32_t StartRecording() override { return impl_->StartRecording(); }
    191  int32_t StopRecording() override { return impl_->StopRecording(); }
    192  bool Recording() const override { return impl_->Recording(); }
    193  int32_t InitSpeaker() override { return impl_->InitSpeaker(); }
    194  bool SpeakerIsInitialized() const override {
    195    return impl_->SpeakerIsInitialized();
    196  }
    197  int32_t InitMicrophone() override { return impl_->InitMicrophone(); }
    198  bool MicrophoneIsInitialized() const override {
    199    return impl_->MicrophoneIsInitialized();
    200  }
    201  int32_t SpeakerVolumeIsAvailable(bool* available) override {
    202    return impl_->SpeakerVolumeIsAvailable(available);
    203  }
    204  int32_t SetSpeakerVolume(uint32_t volume) override {
    205    return impl_->SetSpeakerVolume(volume);
    206  }
    207  int32_t SpeakerVolume(uint32_t* volume) const override {
    208    return impl_->SpeakerVolume(volume);
    209  }
    210  int32_t MaxSpeakerVolume(uint32_t* max_volume) const override {
    211    return impl_->MaxSpeakerVolume(max_volume);
    212  }
    213  int32_t MinSpeakerVolume(uint32_t* min_volume) const override {
    214    return impl_->MinSpeakerVolume(min_volume);
    215  }
    216  int32_t MicrophoneVolumeIsAvailable(bool* available) override {
    217    return impl_->MicrophoneVolumeIsAvailable(available);
    218  }
    219  int32_t SetMicrophoneVolume(uint32_t volume) override {
    220    return impl_->SetMicrophoneVolume(volume);
    221  }
    222  int32_t MicrophoneVolume(uint32_t* volume) const override {
    223    return impl_->MicrophoneVolume(volume);
    224  }
    225  int32_t MaxMicrophoneVolume(uint32_t* max_volume) const override {
    226    return impl_->MaxMicrophoneVolume(max_volume);
    227  }
    228  int32_t MinMicrophoneVolume(uint32_t* min_volume) const override {
    229    return impl_->MinMicrophoneVolume(min_volume);
    230  }
    231  int32_t SpeakerMuteIsAvailable(bool* available) override {
    232    return impl_->SpeakerMuteIsAvailable(available);
    233  }
    234  int32_t SetSpeakerMute(bool enable) override {
    235    return impl_->SetSpeakerMute(enable);
    236  }
    237  int32_t SpeakerMute(bool* enabled) const override {
    238    return impl_->SpeakerMute(enabled);
    239  }
    240  int32_t MicrophoneMuteIsAvailable(bool* available) override {
    241    return impl_->MicrophoneMuteIsAvailable(available);
    242  }
    243  int32_t SetMicrophoneMute(bool enable) override {
    244    return impl_->SetMicrophoneMute(enable);
    245  }
    246  int32_t MicrophoneMute(bool* enabled) const override {
    247    return impl_->MicrophoneMute(enabled);
    248  }
    249  int32_t StereoPlayoutIsAvailable(bool* available) const override {
    250    return impl_->StereoPlayoutIsAvailable(available);
    251  }
    252  int32_t SetStereoPlayout(bool enable) override {
    253    return impl_->SetStereoPlayout(enable);
    254  }
    255  int32_t StereoPlayout(bool* enabled) const override {
    256    return impl_->StereoPlayout(enabled);
    257  }
    258  int32_t StereoRecordingIsAvailable(bool* available) const override {
    259    return impl_->StereoRecordingIsAvailable(available);
    260  }
    261  int32_t SetStereoRecording(bool enable) override {
    262    return impl_->SetStereoRecording(enable);
    263  }
    264  int32_t StereoRecording(bool* enabled) const override {
    265    return impl_->StereoRecording(enabled);
    266  }
    267  int32_t PlayoutDelay(uint16_t* delay_ms) const override {
    268    return impl_->PlayoutDelay(delay_ms);
    269  }
    270  bool BuiltInAECIsAvailable() const override {
    271    return impl_->BuiltInAECIsAvailable();
    272  }
    273  bool BuiltInAGCIsAvailable() const override {
    274    return impl_->BuiltInAGCIsAvailable();
    275  }
    276  bool BuiltInNSIsAvailable() const override {
    277    return impl_->BuiltInNSIsAvailable();
    278  }
    279  int32_t EnableBuiltInAEC(bool enable) override {
    280    return impl_->EnableBuiltInAEC(enable);
    281  }
    282  int32_t EnableBuiltInAGC(bool enable) override {
    283    return impl_->EnableBuiltInAGC(enable);
    284  }
    285  int32_t EnableBuiltInNS(bool enable) override {
    286    return impl_->EnableBuiltInNS(enable);
    287  }
    288  int32_t GetPlayoutUnderrunCount() const override {
    289    return impl_->GetPlayoutUnderrunCount();
    290  }
    291 // Only supported on iOS.
    292 #if defined(WEBRTC_IOS)
    293  int GetPlayoutAudioParameters(AudioParameters* params) const override {
    294    return impl_->GetPlayoutAudioParameters(params);
    295  }
    296  int GetRecordAudioParameters(AudioParameters* params) const override {
    297    return impl_->GetRecordAudioParameters(params);
    298  }
    299 #endif  // WEBRTC_IOS
    300 
    301 protected:
    302  scoped_refptr<AudioDeviceModule> impl_;
    303  std::unique_ptr<AudioDeviceDataObserver> observer_;
    304  AudioTransport* audio_transport_ = nullptr;
    305 };
    306 
    307 }  // namespace
    308 
    309 scoped_refptr<AudioDeviceModule> CreateAudioDeviceWithDataObserver(
    310    scoped_refptr<AudioDeviceModule> impl,
    311    std::unique_ptr<AudioDeviceDataObserver> observer) {
    312  if (impl == nullptr) {
    313    return nullptr;
    314  }
    315  return make_ref_counted<ADMWrapper>(std::move(impl), std::move(observer));
    316 }
    317 
    318 
    319 }  // namespace webrtc