File: media_stream_audio_processor.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (141 lines) | stat: -rw-r--r-- 5,076 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/mediastream/media_stream_audio_processor.h"

#include <memory>
#include <optional>
#include <string_view>

#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "media/base/audio_parameters.h"
#include "third_party/blink/public/platform/modules/webrtc/webrtc_logging.h"
#include "third_party/blink/renderer/modules/webrtc/webrtc_audio_device_impl.h"
#include "third_party/blink/renderer/platform/mediastream/aec_dump_agent_impl.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {
namespace {
void WebRtcLogStringPiece(std::string_view message) {
  WebRtcLogMessage(std::string{message});
}
}  // namespace

// Subscribes a sink to the playout data source for the duration of the
// PlayoutListener lifetime.
class MediaStreamAudioProcessor::PlayoutListener {
 public:
  PlayoutListener(scoped_refptr<WebRtcAudioDeviceImpl> playout_data_source,
                  WebRtcPlayoutDataSource::Sink* sink)
      : playout_data_source_(std::move(playout_data_source)), sink_(sink) {
    DCHECK(playout_data_source_);
    DCHECK(sink_);
    playout_data_source_->AddPlayoutSink(sink_);
  }

  ~PlayoutListener() { playout_data_source_->RemovePlayoutSink(sink_); }

 private:
  // TODO(crbug.com/704136): Replace with Member at some point.
  scoped_refptr<WebRtcAudioDeviceImpl> const playout_data_source_;
  const raw_ptr<WebRtcPlayoutDataSource::Sink> sink_;
};

MediaStreamAudioProcessor::MediaStreamAudioProcessor(
    DeliverProcessedAudioCallback deliver_processed_audio_callback,
    const media::AudioProcessingSettings& settings,
    const media::AudioParameters& capture_data_source_params,
    scoped_refptr<WebRtcAudioDeviceImpl> playout_data_source)
    : audio_processor_(media::AudioProcessor::Create(
          std::move(deliver_processed_audio_callback),
          /*log_callback=*/
          WTF::BindRepeating(&WebRtcLogStringPiece),
          settings,
          capture_data_source_params,
          media::AudioProcessor::GetDefaultOutputFormat(
              capture_data_source_params,
              settings))),
      main_thread_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()),
      aec_dump_agent_impl_(AecDumpAgentImpl::Create(this)),
      stopped_(false) {
  DCHECK(main_thread_runner_);
  // Register as a listener for the playout reference signal. Used for e.g. echo
  // cancellation.
  if (audio_processor_->needs_playout_reference() && playout_data_source) {
    playout_listener_ =
        std::make_unique<PlayoutListener>(std::move(playout_data_source), this);
  }
  DETACH_FROM_THREAD(capture_thread_checker_);
  DETACH_FROM_THREAD(render_thread_checker_);
}

MediaStreamAudioProcessor::~MediaStreamAudioProcessor() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  Stop();
}

void MediaStreamAudioProcessor::ProcessCapturedAudio(
    const media::AudioBus& audio_source,
    base::TimeTicks audio_capture_time,
    int num_preferred_channels,
    double volume) {
  DCHECK_CALLED_ON_VALID_THREAD(capture_thread_checker_);
  audio_processor_->ProcessCapturedAudio(audio_source, audio_capture_time,
                                         num_preferred_channels, volume);
}

void MediaStreamAudioProcessor::Stop() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  if (stopped_)
    return;
  stopped_ = true;

  aec_dump_agent_impl_.reset();
  audio_processor_->OnStopDump();
  playout_listener_.reset();
}

const media::AudioParameters&
MediaStreamAudioProcessor::GetInputFormatForTesting() const {
  return audio_processor_->input_format();
}

void MediaStreamAudioProcessor::OnStartDump(base::File dump_file) {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  audio_processor_->OnStartDump(std::move(dump_file));
}

void MediaStreamAudioProcessor::OnStopDump() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  audio_processor_->OnStopDump();
}

void MediaStreamAudioProcessor::OnPlayoutData(media::AudioBus* audio_bus,
                                              int sample_rate,
                                              base::TimeDelta audio_delay) {
  DCHECK_CALLED_ON_VALID_THREAD(render_thread_checker_);
  DCHECK(audio_bus);
  audio_processor_->OnPlayoutData(*audio_bus, sample_rate, audio_delay);
}

void MediaStreamAudioProcessor::OnPlayoutDataSourceChanged() {
  DCHECK(main_thread_runner_->BelongsToCurrentThread());
  DETACH_FROM_THREAD(render_thread_checker_);
}

void MediaStreamAudioProcessor::OnRenderThreadChanged() {
  DETACH_FROM_THREAD(render_thread_checker_);
  DCHECK_CALLED_ON_VALID_THREAD(render_thread_checker_);
}

webrtc::AudioProcessorInterface::AudioProcessorStatistics
MediaStreamAudioProcessor::GetStats(bool has_remote_tracks) {
  AudioProcessorStatistics stats;
  stats.apm_statistics = audio_processor_->GetStats();
  return stats;
}

}  // namespace blink