tor-browser

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

async_audio_processing.cc (3806B)


      1 /*
      2 *  Copyright (c) 2020 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 "modules/async_audio_processing/async_audio_processing.h"
     11 
     12 #include <memory>
     13 #include <utility>
     14 
     15 #include "api/audio/audio_frame.h"
     16 #include "api/audio/audio_frame_processor.h"
     17 #include "api/task_queue/task_queue_factory.h"
     18 
     19 namespace webrtc {
     20 
     21 AsyncAudioProcessing::Factory::~Factory() = default;
     22 AsyncAudioProcessing::Factory::Factory(AudioFrameProcessor& frame_processor,
     23                                       TaskQueueFactory& task_queue_factory)
     24    : frame_processor_(frame_processor),
     25      task_queue_factory_(task_queue_factory) {}
     26 
     27 AsyncAudioProcessing::Factory::Factory(
     28    std::unique_ptr<AudioFrameProcessor> frame_processor,
     29    TaskQueueFactory& task_queue_factory)
     30    : frame_processor_(*frame_processor),
     31      owned_frame_processor_(std::move(frame_processor)),
     32      task_queue_factory_(task_queue_factory) {}
     33 
     34 std::unique_ptr<AsyncAudioProcessing>
     35 AsyncAudioProcessing::Factory::CreateAsyncAudioProcessing(
     36    AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback) {
     37  if (owned_frame_processor_) {
     38    return std::make_unique<AsyncAudioProcessing>(
     39        std::move(owned_frame_processor_), task_queue_factory_,
     40        std::move(on_frame_processed_callback));
     41  } else {
     42    return std::make_unique<AsyncAudioProcessing>(
     43        frame_processor_, task_queue_factory_,
     44        std::move(on_frame_processed_callback));
     45  }
     46 }
     47 
     48 AsyncAudioProcessing::~AsyncAudioProcessing() {
     49  if (owned_frame_processor_) {
     50    owned_frame_processor_->SetSink(nullptr);
     51  } else {
     52    frame_processor_.SetSink(nullptr);
     53  }
     54 }
     55 
     56 AsyncAudioProcessing::AsyncAudioProcessing(
     57    AudioFrameProcessor& frame_processor,
     58    TaskQueueFactory& task_queue_factory,
     59    AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback)
     60    : on_frame_processed_callback_(std::move(on_frame_processed_callback)),
     61      frame_processor_(frame_processor),
     62      task_queue_(task_queue_factory.CreateTaskQueue(
     63          "AsyncAudioProcessing",
     64          TaskQueueFactory::Priority::NORMAL)) {
     65  frame_processor_.SetSink([this](std::unique_ptr<AudioFrame> frame) {
     66    task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
     67      on_frame_processed_callback_(std::move(frame));
     68    });
     69  });
     70 }
     71 
     72 AsyncAudioProcessing::AsyncAudioProcessing(
     73    std::unique_ptr<AudioFrameProcessor> frame_processor,
     74    TaskQueueFactory& task_queue_factory,
     75    AudioFrameProcessor::OnAudioFrameCallback on_frame_processed_callback)
     76    : on_frame_processed_callback_(std::move(on_frame_processed_callback)),
     77      frame_processor_(*frame_processor),
     78      owned_frame_processor_(std::move(frame_processor)),
     79      task_queue_(task_queue_factory.CreateTaskQueue(
     80          "AsyncAudioProcessing",
     81          TaskQueueFactory::Priority::NORMAL)) {
     82  owned_frame_processor_->SetSink([this](std::unique_ptr<AudioFrame> frame) {
     83    task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
     84      on_frame_processed_callback_(std::move(frame));
     85    });
     86  });
     87 }
     88 
     89 void AsyncAudioProcessing::Process(std::unique_ptr<AudioFrame> frame) {
     90  if (owned_frame_processor_) {
     91    task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
     92      owned_frame_processor_->Process(std::move(frame));
     93    });
     94  } else {
     95    task_queue_->PostTask([this, frame = std::move(frame)]() mutable {
     96      frame_processor_.Process(std::move(frame));
     97    });
     98  }
     99 }
    100 
    101 }  // namespace webrtc