OLD | NEW |
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 // The audio processsor that applies audio post-processing on the source |
| 120 // audio. This is null if there is no audio processing taking place |
| 121 // upstream. This must be set before calls to GetAudioProcessor() are made. |
| 122 scoped_refptr<MediaStreamAudioProcessor> audio_processor_; |
| 123 |
| 124 // Thread-safe accessor to current audio signal level. This may be null, if |
| 125 // not applicable to the current use case. This must be set before calls to |
| 126 // GetSignalLevel() are made. |
| 127 scoped_refptr<MediaStreamAudioLevelCalculator::Level> level_; |
| 128 |
| 129 // Lock that protects concurrent access to the |sinks_| list. |
| 130 base::Lock lock_; |
| 131 |
| 132 // A vector of pointers to unowned WebRTC-internal objects which each |
| 133 // receive the audio data. |
| 134 std::vector<webrtc::AudioTrackSinkInterface*> sinks_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(Adapter); |
| 137 }; |
| 138 |
| 139 // MediaStreamAudioSink implementation. |
| 140 void OnData(const media::AudioBus& audio_bus, |
| 141 base::TimeTicks estimated_capture_time) override; |
| 142 void OnSetFormat(const media::AudioParameters& params) override; |
| 143 |
| 144 // Called by AudioPushFifo zero or more times during the call to OnData(). |
| 145 // Delivers audio data with the required 10ms buffer size to |adapter_|. |
| 146 void DeliverRebufferedAudio(const media::AudioBus& audio_bus, |
| 147 int frame_delay); |
| 148 |
| 149 // Owner of the WebRTC sinks. May outlive this WebRtcAudioSink (if references |
| 150 // are held by libjingle). |
| 151 const scoped_refptr<Adapter> adapter_; |
| 152 |
| 153 // The current format of the audio passing through this sink. |
| 154 media::AudioParameters params_; |
| 155 |
| 156 // Light-weight fifo used for re-chunking audio into the 10ms chunks required |
| 157 // by the WebRTC sinks. |
| 158 media::AudioPushFifo fifo_; |
| 159 |
| 160 // Buffer used for converting into the required signed 16-bit integer |
| 161 // interleaved samples. |
| 162 std::unique_ptr<int16_t[]> interleaved_data_; |
| 163 |
| 164 // In debug builds, check that WebRtcAudioSink's public methods are all being |
| 165 // called on the main render thread. |
| 166 base::ThreadChecker thread_checker_; |
| 167 |
| 168 // Used to DCHECK that OnSetFormat() and OnData() are called on the same |
| 169 // thread. |
| 170 base::ThreadChecker audio_thread_checker_; |
| 171 |
| 172 DISALLOW_COPY_AND_ASSIGN(WebRtcAudioSink); |
103 }; | 173 }; |
104 | 174 |
105 } // namespace content | 175 } // namespace content |
106 | 176 |
107 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_LOCAL_AUDIO_TRACK_ADAPTER_H_ | 177 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_ |
OLD | NEW |