| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_ | |
| 6 #define CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | |
| 15 #include "base/single_thread_task_runner.h" | |
| 16 #include "base/synchronization/lock.h" | |
| 17 #include "content/common/content_export.h" | |
| 18 #include "content/public/renderer/media_stream_audio_sink.h" | |
| 19 #include "content/renderer/media/media_stream_audio_level_calculator.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" | |
| 23 #include "third_party/webrtc/api/mediastreamtrack.h" | |
| 24 #include "third_party/webrtc/media/base/audiorenderer.h" | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 // Provides an implementation of the MediaStreamAudioSink which re-chunks audio | |
| 29 // data into the 10ms chunks required by WebRTC and then delivers the audio to | |
| 30 // one or more objects implementing the webrtc::AudioTrackSinkInterface. | |
| 31 // | |
| 32 // The inner class, Adapter, implements the webrtc::AudioTrackInterface and | |
| 33 // manages one or more "WebRTC sinks" (i.e., instances of | |
| 34 // webrtc::AudioTrackSinkInterface) which are added/removed on the WebRTC | |
| 35 // signaling thread. | |
| 36 class CONTENT_EXPORT WebRtcAudioSink : public MediaStreamAudioSink { | |
| 37 public: | |
| 38 WebRtcAudioSink( | |
| 39 const std::string& label, | |
| 40 scoped_refptr<webrtc::AudioSourceInterface> track_source, | |
| 41 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner); | |
| 42 | |
| 43 ~WebRtcAudioSink() override; | |
| 44 | |
| 45 webrtc::AudioTrackInterface* webrtc_audio_track() const { | |
| 46 return adapter_.get(); | |
| 47 } | |
| 48 | |
| 49 // Set the object that provides shared access to the current audio signal | |
| 50 // level. This is passed via the Adapter to libjingle. This method may only | |
| 51 // be called once, before the audio data flow starts, and before any calls to | |
| 52 // Adapter::GetSignalLevel() might be made. | |
| 53 void SetLevel(scoped_refptr<MediaStreamAudioLevelCalculator::Level> level); | |
| 54 | |
| 55 // Set the processor that applies signal processing on the data from the | |
| 56 // source. This is passed via the Adapter to libjingle. This method may only | |
| 57 // be called once, before the audio data flow starts, and before any calls to | |
| 58 // GetAudioProcessor() might be made. | |
| 59 void SetAudioProcessor(scoped_refptr<MediaStreamAudioProcessor> processor); | |
| 60 | |
| 61 // MediaStreamSink override. | |
| 62 void OnEnabledChanged(bool enabled) override; | |
| 63 | |
| 64 private: | |
| 65 // Private implementation of the webrtc::AudioTrackInterface whose control | |
| 66 // methods are all called on the WebRTC signaling thread. This class is | |
| 67 // ref-counted, per the requirements of webrtc::AudioTrackInterface. | |
| 68 class Adapter | |
| 69 : NON_EXPORTED_BASE( | |
| 70 public webrtc::MediaStreamTrack<webrtc::AudioTrackInterface>) { | |
| 71 public: | |
| 72 Adapter(const std::string& label, | |
| 73 scoped_refptr<webrtc::AudioSourceInterface> source, | |
| 74 scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner); | |
| 75 | |
| 76 base::SingleThreadTaskRunner* signaling_task_runner() const { | |
| 77 return signaling_task_runner_.get(); | |
| 78 } | |
| 79 | |
| 80 // These setters are called before the audio data flow starts, and before | |
| 81 // any methods called on the signaling thread reference these objects. | |
| 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 } | |
| 89 | |
| 90 // Delivers a 10ms chunk of audio to all WebRTC sinks managed by this | |
| 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); | |
| 96 | |
| 97 // webrtc::MediaStreamTrack implementation. | |
| 98 std::string kind() const override; | |
| 99 bool set_enabled(bool enable) override; | |
| 100 | |
| 101 // webrtc::AudioTrackInterface implementation. | |
| 102 void AddSink(webrtc::AudioTrackSinkInterface* sink) override; | |
| 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; | |
| 108 | |
| 109 protected: | |
| 110 ~Adapter() override; | |
| 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); | |
| 179 }; | |
| 180 | |
| 181 } // namespace content | |
| 182 | |
| 183 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_WEBRTC_AUDIO_SINK_H_ | |
| OLD | NEW |