Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_WEBAUDIO_CAPTURER_SOURCE_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 13 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 14 #include "media/audio/audio_parameters.h" | 13 #include "media/audio/audio_parameters.h" |
| 15 #include "media/base/audio_capturer_source.h" | 14 #include "media/base/audio_capturer_source.h" |
| 16 #include "media/base/audio_fifo.h" | 15 #include "media/base/audio_fifo.h" |
| 17 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" | 16 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" |
| 18 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 17 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
| 19 #include "third_party/WebKit/public/platform/WebVector.h" | 18 #include "third_party/WebKit/public/platform/WebVector.h" |
| 20 | 19 |
| 21 namespace content { | 20 namespace content { |
| 22 | 21 |
| 23 class WebRtcLocalAudioTrack; | 22 class WebRtcLocalAudioTrack; |
| 24 | 23 |
| 25 // WebAudioCapturerSource is the missing link between | 24 // WebAudioCapturerSource is the missing link between |
| 26 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack. | 25 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack. |
| 27 // | 26 // |
| 28 // 1. WebKit calls the setFormat() method setting up the basic stream format | 27 // 1. WebKit calls the setFormat() method setting up the basic stream format |
| 29 // (channels, and sample-rate). | 28 // (channels, and sample-rate). |
| 30 // 2. consumeAudio() is called periodically by WebKit which dispatches the | 29 // 2. consumeAudio() is called periodically by WebKit which dispatches the |
| 31 // audio stream to the WebRtcLocalAudioTrack::Capture() method. | 30 // audio stream to the WebRtcLocalAudioTrack::Capture() method. |
| 32 class WebAudioCapturerSource | 31 class WebAudioCapturerSource : public blink::WebAudioDestinationConsumer { |
| 33 : public base::RefCountedThreadSafe<WebAudioCapturerSource>, | |
| 34 public blink::WebAudioDestinationConsumer { | |
| 35 public: | 32 public: |
| 36 explicit WebAudioCapturerSource( | 33 explicit WebAudioCapturerSource(blink::WebMediaStreamSource* blink_source); |
| 37 const blink::WebMediaStreamSource& blink_source); | 34 |
| 35 ~WebAudioCapturerSource() override; | |
| 38 | 36 |
| 39 // WebAudioDestinationConsumer implementation. | 37 // WebAudioDestinationConsumer implementation. |
| 40 // setFormat() is called early on, so that we can configure the audio track. | 38 // setFormat() is called early on, so that we can configure the audio track. |
| 41 void setFormat(size_t number_of_channels, float sample_rate) override; | 39 void setFormat(size_t number_of_channels, float sample_rate) override; |
| 42 // MediaStreamAudioDestinationNode periodically calls consumeAudio(). | 40 // MediaStreamAudioDestinationNode periodically calls consumeAudio(). |
| 43 // Called on the WebAudio audio thread. | 41 // Called on the WebAudio audio thread. |
| 44 void consumeAudio(const blink::WebVector<const float*>& audio_data, | 42 void consumeAudio(const blink::WebVector<const float*>& audio_data, |
| 45 size_t number_of_frames) override; | 43 size_t number_of_frames) override; |
| 46 | 44 |
| 47 // Called when the WebAudioCapturerSource is hooking to a media audio track. | 45 // Called when the WebAudioCapturerSource is hooking to a media audio track. |
| 48 // |track| is the sink of the data flow. |source_provider| is the source of | 46 // |track| is the sink of the data flow and must remain alive until Stop() is |
| 49 // the data flow where stream information like delay, volume, key_pressed, | 47 // called. |
| 50 // is stored. | |
| 51 void Start(WebRtcLocalAudioTrack* track); | 48 void Start(WebRtcLocalAudioTrack* track); |
| 52 | 49 |
| 53 // Called when the media audio track is stopping. | 50 // Called when the media audio track is stopping. |
| 54 void Stop(); | 51 void Stop(); |
| 55 | 52 |
| 56 protected: | |
| 57 friend class base::RefCountedThreadSafe<WebAudioCapturerSource>; | |
| 58 ~WebAudioCapturerSource() override; | |
| 59 | |
| 60 private: | 53 private: |
| 61 // Removes this object from a blink::WebMediaStreamSource with which it | 54 // Deregisters this object from its blink::WebMediaStreamSource. |
| 62 // might be registered. The goal is to avoid dangling pointers. | 55 void DeregisterFromBlinkSource(); |
| 63 void removeFromBlinkSource(); | |
| 64 | 56 |
| 65 // Used to DCHECK that some methods are called on the correct thread. | 57 // Used to DCHECK that some methods are called on the correct thread. |
| 66 base::ThreadChecker thread_checker_; | 58 base::ThreadChecker thread_checker_; |
| 67 | 59 |
| 68 // The audio track this WebAudioCapturerSource is feeding data to. | 60 // The audio track this WebAudioCapturerSource is feeding data to. |
| 69 // WebRtcLocalAudioTrack is reference counted, and owning this object. | |
| 70 // To avoid circular reference, a raw pointer is kept here. | |
| 71 WebRtcLocalAudioTrack* track_; | 61 WebRtcLocalAudioTrack* track_; |
|
mcasas
2016/02/26 01:28:19
Shouldn't this guy be a WeakPtr?
miu
2016/02/27 03:46:36
No. It must remain alive until Stop() is called (
| |
| 72 | 62 |
| 73 media::AudioParameters params_; | 63 media::AudioParameters params_; |
| 74 | 64 |
| 75 // Flag to help notify the |track_| when the audio format has changed. | 65 // Flag to help notify the |track_| when the audio format has changed. |
| 76 bool audio_format_changed_; | 66 bool audio_format_changed_; |
| 77 | 67 |
| 78 // Wraps data coming from HandleCapture(). | 68 // Wraps data coming from HandleCapture(). |
| 79 scoped_ptr<media::AudioBus> wrapper_bus_; | 69 scoped_ptr<media::AudioBus> wrapper_bus_; |
| 80 | 70 |
| 81 // Bus for reading from FIFO and calling the CaptureCallback. | 71 // Bus for reading from FIFO and calling the CaptureCallback. |
| 82 scoped_ptr<media::AudioBus> capture_bus_; | 72 scoped_ptr<media::AudioBus> capture_bus_; |
| 83 | 73 |
| 84 // Handles mismatch between WebAudio buffer size and WebRTC. | 74 // Handles mismatch between WebAudio buffer size and WebRTC. |
| 85 scoped_ptr<media::AudioFifo> fifo_; | 75 scoped_ptr<media::AudioFifo> fifo_; |
| 86 | 76 |
| 87 // Synchronizes HandleCapture() with AudioCapturerSource calls. | 77 // Synchronizes HandleCapture() with AudioCapturerSource calls. |
| 88 base::Lock lock_; | 78 base::Lock lock_; |
| 89 bool started_; | 79 bool started_; |
| 90 | 80 |
| 91 // This object registers with a blink::WebMediaStreamSource. We keep track of | 81 // This object registers with a blink::WebMediaStreamSource. We keep track of |
| 92 // that in order to be able to deregister before stopping the audio track. | 82 // that in order to be able to deregister before stopping the audio track. |
| 93 blink::WebMediaStreamSource blink_source_; | 83 blink::WebMediaStreamSource blink_source_; |
| 94 | 84 |
| 95 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); | 85 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); |
| 96 }; | 86 }; |
| 97 | 87 |
| 98 } // namespace content | 88 } // namespace content |
| 99 | 89 |
| 100 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ | 90 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ |
| OLD | NEW |