File: webaudio_media_stream_audio_sink.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (137 lines) | stat: -rw-r--r-- 5,285 bytes parent folder | download | duplicates (6)
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
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_WEBAUDIO_MEDIA_STREAM_AUDIO_SINK_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_WEBAUDIO_MEDIA_STREAM_AUDIO_SINK_H_

#include <stddef.h>

#include <memory>
#include <vector>

#include "base/synchronization/lock.h"
#include "base/time/time.h"
#include "media/base/audio_converter.h"
#include "media/base/reentrancy_checker.h"
#include "third_party/blink/public/platform/modules/mediastream/web_media_stream_audio_sink.h"
#include "third_party/blink/public/platform/web_audio_source_provider.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/mediastream/media_stream_component.h"

namespace media {
class AudioBus;
class AudioConverter;
class AudioFifo;
class AudioParameters;
}  // namespace media

namespace blink {

class WebAudioSourceProviderClient;

// WebAudioMediaStreamAudioSink provides a bridge between classes:
//     MediaStreamAudioTrack ---> WebAudioSourceProvider
//
// WebAudioMediaStreamAudioSink works as a sink to the MediaStreamAudioTrack
// and stores the capture data to a FIFO. When the media stream is connected to
// WebAudio MediaStreamAudioSourceNode as a source provider,
// MediaStreamAudioSourceNode will periodically call provideInput() to get the
// data from the FIFO.
//
// Most calls are protected by a lock.
class MODULES_EXPORT WebAudioMediaStreamAudioSink
    : public WebAudioSourceProvider,
      public media::AudioConverter::InputCallback,
      public WebMediaStreamAudioSink {
 public:
  static const int kWebAudioRenderBufferSize;

  WebAudioMediaStreamAudioSink(MediaStreamComponent* component,
                               int context_sample_rate,
                               base::TimeDelta platform_buffer_duration);

  WebAudioMediaStreamAudioSink(const WebAudioMediaStreamAudioSink&) = delete;
  WebAudioMediaStreamAudioSink& operator=(const WebAudioMediaStreamAudioSink&) =
      delete;

  ~WebAudioMediaStreamAudioSink() override;

  // WebMediaStreamAudioSink implementation.
  void OnData(const media::AudioBus& audio_bus,
              base::TimeTicks estimated_capture_time) override;
  void OnSetFormat(const media::AudioParameters& params) override;
  void OnReadyStateChanged(WebMediaStreamSource::ReadyState state) override;

  // WebAudioSourceProvider implementation.
  void SetClient(WebAudioSourceProviderClient* client) override;
  void ProvideInput(const std::vector<float*>& audio_data,
                    int number_of_frames) override;

 private:
  FRIEND_TEST_ALL_PREFIXES(WebAudioMediaStreamAudioSinkFifoTest, VerifyFifo);

  struct FifoStats {
    int overruns = 0;
    int underruns = 0;
  };

  void ResetFifoStatsForTesting();
  const FifoStats& GetFifoStatsForTesting();

  // media::AudioConverter::InputCallback implementation.
  // This function is triggered by the above ProvideInput() on the WebAudio
  // audio thread, so it has be called under the protection of |lock_|.
  double ProvideInput(media::AudioBus* audio_bus,
                      uint32_t frames_delayed,
                      const media::AudioGlitchInfo& glitch_info) override;

  std::unique_ptr<media::AudioConverter> audio_converter_ GUARDED_BY(lock_);
  std::unique_ptr<media::AudioFifo> fifo_ GUARDED_BY(lock_);
  bool is_enabled_ GUARDED_BY(lock_);
  media::AudioParameters source_params_ GUARDED_BY(lock_);

  // Protects the above variables.
  base::Lock lock_;

  // No lock protection needed since only accessed in std::vector version of
  // ProvideInput().
  std::unique_ptr<media::AudioBus> output_wrapper_;

  // The audio track that this source provider is connected to.
  // No lock protection needed since only accessed in constructor and
  // destructor.
  Persistent<MediaStreamComponent> component_;

  // Flag to tell if the track has been stopped or not.
  // No lock protection needed since only accessed in constructor, destructor
  // and OnReadyStateChanged().
  bool track_stopped_;

  // Buffer duration of the output backing up the audio context the sink
  // delivers audio to. Affects how many public ProvideInput() calls can be
  // received in one batch, and thus needs to be taken into account when
  // configuring `fifo_`.
  const base::TimeDelta platform_buffer_duration_;

  // Parameters at which audio is delivered to the WebAudio graph, i.e. of the
  // public ProvideInput() call.
  const media::AudioParameters sink_params_;

  // For testing. Instantiated only if ResetFifoStatsForTesting() is called.
  std::unique_ptr<FifoStats> fifo_stats_;

  // Used to assert that OnData() is only accessed by one thread at a time. We
  // can't use a thread checker since thread may change.
  REENTRANCY_CHECKER(capture_reentrancy_checker_);

  // Used to assert that ProvideInput() is not accessed concurrently.
  REENTRANCY_CHECKER(provide_input_reentrancy_checker_);

  // Used to assert that OnReadyStateChanged() is not accessed concurrently.
  REENTRANCY_CHECKER(ready_state_reentrancy_checker_);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_WEBAUDIO_MEDIA_STREAM_AUDIO_SINK_H_