Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Side by Side Diff: content/renderer/media/webrtc/webrtc_audio_sink.h

Issue 1834323002: MediaStream audio: Refactor 3 separate "glue" implementations into one. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: REBASE + Workaround to ensure MediaStreamAudioProcessor is destroyed on the main thread. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_
7 7
8 #include <stdint.h>
9
10 #include <memory>
8 #include <vector> 11 #include <vector>
9 12
13 #include "base/macros.h"
10 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
13 #include "base/synchronization/lock.h" 16 #include "base/synchronization/lock.h"
14 #include "content/common/content_export.h" 17 #include "content/common/content_export.h"
18 #include "content/public/renderer/media_stream_audio_sink.h"
15 #include "content/renderer/media/media_stream_audio_level_calculator.h" 19 #include "content/renderer/media/media_stream_audio_level_calculator.h"
16 #include "content/renderer/media/media_stream_audio_processor.h" 20 #include "content/renderer/media/media_stream_audio_processor.h"
21 #include "media/base/audio_parameters.h"
22 #include "media/base/audio_push_fifo.h"
17 #include "third_party/webrtc/api/mediastreamtrack.h" 23 #include "third_party/webrtc/api/mediastreamtrack.h"
18 #include "third_party/webrtc/media/base/audiorenderer.h" 24 #include "third_party/webrtc/media/base/audiorenderer.h"
19 25
20 namespace cricket {
21 class AudioRenderer;
22 }
23
24 namespace webrtc {
25 class AudioSourceInterface;
26 class AudioProcessorInterface;
27 }
28
29 namespace content { 26 namespace content {
30 27
31 class MediaStreamAudioProcessor; 28 // Provides an implementation of the MediaStreamAudioSink which re-chunks audio
32 class WebRtcAudioSinkAdapter; 29 // data into the 10ms chunks required by WebRTC and then delivers the audio to
33 class WebRtcLocalAudioTrack; 30 // one or more objects implementing the webrtc::AudioTrackSinkInterface.
34 31 //
35 // Provides an implementation of the webrtc::AudioTrackInterface that can be 32 // The inner class, Adapter, implements the webrtc::AudioTrackInterface and
36 // bound/unbound to/from a MediaStreamAudioTrack. In other words, this is an 33 // manages one or more "WebRTC sinks" (i.e., instances of
37 // adapter that sits between the media stream object graph and WebRtc's object 34 // webrtc::AudioTrackSinkInterface) which are added/removed on the WebRTC
38 // graph and proxies between the two. 35 // signaling thread.
39 class CONTENT_EXPORT WebRtcLocalAudioTrackAdapter 36 class CONTENT_EXPORT WebRtcAudioSink : public MediaStreamAudioSink {
40 : NON_EXPORTED_BASE(
41 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
42 public: 37 public:
43 static scoped_refptr<WebRtcLocalAudioTrackAdapter> Create( 38 WebRtcAudioSink(
44 const std::string& label, 39 const std::string& label,
45 webrtc::AudioSourceInterface* track_source); 40 scoped_refptr<webrtc::AudioSourceInterface> track_source,
46
47 WebRtcLocalAudioTrackAdapter(
48 const std::string& label,
49 webrtc::AudioSourceInterface* track_source,
50 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner); 41 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner);
51 42
52 ~WebRtcLocalAudioTrackAdapter() override; 43 ~WebRtcAudioSink() override;
53 44
54 void Initialize(WebRtcLocalAudioTrack* owner); 45 webrtc::AudioTrackInterface* webrtc_audio_track() const {
46 return adapter_.get();
47 }
55 48
56 // Set the object that provides shared access to the current audio signal 49 // Set the object that provides shared access to the current audio signal
57 // level. This method may only be called once, before the audio data flow 50 // level. This is passed via the Adapter to libjingle. This method may only
58 // starts, and before any calls to GetSignalLevel() might be made. 51 // be called once, before the audio data flow starts, and before any calls to
52 // Adapter::GetSignalLevel() might be made.
59 void SetLevel(scoped_refptr<MediaStreamAudioLevelCalculator::Level> level); 53 void SetLevel(scoped_refptr<MediaStreamAudioLevelCalculator::Level> level);
60 54
61 // Method called by the WebRtcLocalAudioTrack to set the processor that 55 // Set the processor that applies signal processing on the data from the
62 // applies signal processing on the data of the track. 56 // source. This is passed via the Adapter to libjingle. This method may only
63 // This class will keep a reference of the |processor|. 57 // be called once, before the audio data flow starts, and before any calls to
64 // Called on the main render thread. 58 // GetAudioProcessor() might be made.
65 // This method may only be called once, before the audio data flow starts, and
66 // before any calls to GetAudioProcessor() might be made.
67 void SetAudioProcessor(scoped_refptr<MediaStreamAudioProcessor> processor); 59 void SetAudioProcessor(scoped_refptr<MediaStreamAudioProcessor> processor);
68 60
69 // webrtc::MediaStreamTrack implementation. 61 // MediaStreamSink override.
70 std::string kind() const override; 62 void OnEnabledChanged(bool enabled) override;
71 bool set_enabled(bool enable) override;
72 63
73 private: 64 private:
74 // webrtc::AudioTrackInterface implementation. 65 // Private implementation of the webrtc::AudioTrackInterface whose control
75 void AddSink(webrtc::AudioTrackSinkInterface* sink) override; 66 // methods are all called on the WebRTC signaling thread. This class is
76 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override; 67 // ref-counted, per the requirements of webrtc::AudioTrackInterface.
77 bool GetSignalLevel(int* level) override; 68 class Adapter
78 rtc::scoped_refptr<webrtc::AudioProcessorInterface> GetAudioProcessor() 69 : NON_EXPORTED_BASE(
79 override; 70 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) {
80 webrtc::AudioSourceInterface* GetSource() const override; 71 public:
72 Adapter(const std::string& label,
73 scoped_refptr<webrtc::AudioSourceInterface> source,
74 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner);
81 75
82 // Weak reference. 76 base::SingleThreadTaskRunner* signaling_task_runner() const {
83 WebRtcLocalAudioTrack* owner_; 77 return signaling_task_runner_.get();
78 }
84 79
85 // The source of the audio track which handles the audio constraints. 80 // These setters are called before the audio data flow starts, and before
86 // TODO(xians): merge |track_source_| to |capturer_| in WebRtcLocalAudioTrack. 81 // any methods called on the signaling thread reference these objects.
87 rtc::scoped_refptr<webrtc::AudioSourceInterface> track_source_; 82 void set_processor(scoped_refptr<MediaStreamAudioProcessor> processor) {
83 audio_processor_ = std::move(processor);
84 }
85 void set_level(
86 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level) {
87 level_ = std::move(level);
88 }
88 89
89 // Libjingle's signaling thread. 90 // Delivers a 10ms chunk of audio to all WebRTC sinks managed by this
90 const scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner_; 91 // Adapter. This is called on the audio thread.
92 void DeliverPCMToWebRtcSinks(const int16_t* audio_data,
93 int sample_rate,
94 size_t number_of_channels,
95 size_t number_of_frames);
91 96
92 // The audio processsor that applies audio processing on the data of audio 97 // webrtc::MediaStreamTrack implementation.
93 // track. This must be set before calls to GetAudioProcessor() are made. 98 std::string kind() const override;
94 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; 99 bool set_enabled(bool enable) override;
95 100
96 // A vector of the peer connection sink adapters which receive the audio data 101 // webrtc::AudioTrackInterface implementation.
97 // from the audio track. 102 void AddSink(webrtc::AudioTrackSinkInterface* sink) override;
98 ScopedVector<WebRtcAudioSinkAdapter> sink_adapters_; 103 void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override;
104 bool GetSignalLevel(int* level) override;
105 rtc::scoped_refptr<webrtc::AudioProcessorInterface> GetAudioProcessor()
106 override;
107 webrtc::AudioSourceInterface* GetSource() const override;
99 108
100 // Thread-safe accessor to current audio signal level. This must be set 109 protected:
101 // before calls to GetSignalLevel() are made. 110 ~Adapter() override;
102 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level_; 111
112 private:
113 const scoped_refptr<webrtc::AudioSourceInterface> source_;
114
115 // Task runner for operations that must be done on libjingle's signaling
116 // thread.
117 const scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner_;
118
119 // Task runner used for the final de-referencing of |audio_processor_| at
120 // destruction time.
121 //
122 // TODO(miu): Remove this once MediaStreamAudioProcessor is fixed.
123 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
124
125 // The audio processsor that applies audio post-processing on the source
126 // audio. This is null if there is no audio processing taking place
127 // upstream. This must be set before calls to GetAudioProcessor() are made.
128 scoped_refptr<MediaStreamAudioProcessor> audio_processor_;
129
130 // Thread-safe accessor to current audio signal level. This may be null, if
131 // not applicable to the current use case. This must be set before calls to
132 // GetSignalLevel() are made.
133 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level_;
134
135 // Lock that protects concurrent access to the |sinks_| list.
136 base::Lock lock_;
137
138 // A vector of pointers to unowned WebRTC-internal objects which each
139 // receive the audio data.
140 std::vector<webrtc::AudioTrackSinkInterface*> sinks_;
141
142 DISALLOW_COPY_AND_ASSIGN(Adapter);
143 };
144
145 // MediaStreamAudioSink implementation.
146 void OnData(const media::AudioBus& audio_bus,
147 base::TimeTicks estimated_capture_time) override;
148 void OnSetFormat(const media::AudioParameters& params) override;
149
150 // Called by AudioPushFifo zero or more times during the call to OnData().
151 // Delivers audio data with the required 10ms buffer size to |adapter_|.
152 void DeliverRebufferedAudio(const media::AudioBus& audio_bus,
153 int frame_delay);
154
155 // Owner of the WebRTC sinks. May outlive this WebRtcAudioSink (if references
156 // are held by libjingle).
157 const scoped_refptr<Adapter> adapter_;
158
159 // The current format of the audio passing through this sink.
160 media::AudioParameters params_;
161
162 // Light-weight fifo used for re-chunking audio into the 10ms chunks required
163 // by the WebRTC sinks.
164 media::AudioPushFifo fifo_;
165
166 // Buffer used for converting into the required signed 16-bit integer
167 // interleaved samples.
168 std::unique_ptr<int16_t[]> interleaved_data_;
169
170 // In debug builds, check that WebRtcAudioSink's public methods are all being
171 // called on the main render thread.
172 base::ThreadChecker thread_checker_;
173
174 // Used to DCHECK that OnSetFormat() and OnData() are called on the same
175 // thread.
176 base::ThreadChecker audio_thread_checker_;
177
178 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioSink);
103 }; 179 };
104 180
105 } // namespace content 181 } // namespace content
106 182
107 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ 183 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698