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_MEDIA_STREAM_SOURCE_H_ |
6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ | 6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_MEDIA_STREAM_SOURCE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include "base/memory/scoped_ptr.h" |
9 | |
10 #include "base/macros.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "base/threading/thread_checker.h" | |
13 #include "base/time/time.h" | 9 #include "base/time/time.h" |
14 #include "media/audio/audio_parameters.h" | 10 #include "content/renderer/media/media_stream_audio_source.h" |
15 #include "media/base/audio_bus.h" | 11 #include "media/base/audio_bus.h" |
16 #include "media/base/audio_capturer_source.h" | |
17 #include "media/base/audio_push_fifo.h" | 12 #include "media/base/audio_push_fifo.h" |
18 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" | 13 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" |
19 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
20 #include "third_party/WebKit/public/platform/WebVector.h" | 15 #include "third_party/WebKit/public/platform/WebVector.h" |
21 | 16 |
22 namespace content { | 17 namespace content { |
23 | 18 |
24 class WebRtcLocalAudioTrack; | 19 // Implements the WebAudioDestinationConsumer interface to provide a source of |
| 20 // audio data (i.e., the output from a graph of WebAudio nodes) to one or more |
| 21 // MediaStreamAudioTracks. Audio data is transported directly to the tracks in |
| 22 // 10 ms chunks. |
| 23 class WebAudioMediaStreamSource final |
| 24 : NON_EXPORTED_BASE(public MediaStreamAudioSource), |
| 25 public blink::WebAudioDestinationConsumer { |
| 26 public: |
| 27 explicit WebAudioMediaStreamSource(blink::WebMediaStreamSource* blink_source); |
25 | 28 |
26 // WebAudioCapturerSource is the missing link between | 29 ~WebAudioMediaStreamSource() override; |
27 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack. | |
28 // | |
29 // 1. WebKit calls the setFormat() method setting up the basic stream format | |
30 // (channels, and sample-rate). | |
31 // 2. consumeAudio() is called periodically by WebKit which dispatches the | |
32 // audio stream to the WebRtcLocalAudioTrack::Capture() method. | |
33 class WebAudioCapturerSource : public blink::WebAudioDestinationConsumer { | |
34 public: | |
35 explicit WebAudioCapturerSource(blink::WebMediaStreamSource* blink_source); | |
36 | 30 |
37 ~WebAudioCapturerSource() override; | 31 private: |
38 | |
39 // WebAudioDestinationConsumer implementation. | 32 // WebAudioDestinationConsumer implementation. |
40 // setFormat() is called early on, so that we can configure the audio track. | 33 // |
| 34 // Note: Blink ensures setFormat() and consumeAudio() are not called |
| 35 // concurrently across threads, but these methods could be called on any |
| 36 // thread. |
41 void setFormat(size_t number_of_channels, float sample_rate) override; | 37 void setFormat(size_t number_of_channels, float sample_rate) override; |
42 // MediaStreamAudioDestinationNode periodically calls consumeAudio(). | |
43 // Called on the WebAudio audio thread. | |
44 void consumeAudio(const blink::WebVector<const float*>& audio_data, | 38 void consumeAudio(const blink::WebVector<const float*>& audio_data, |
45 size_t number_of_frames) override; | 39 size_t number_of_frames) override; |
46 | 40 |
47 // Called when the WebAudioCapturerSource is hooking to a media audio track. | |
48 // |track| is the sink of the data flow and must remain alive until Stop() is | |
49 // called. | |
50 void Start(WebRtcLocalAudioTrack* track); | |
51 | |
52 // Called when the media audio track is stopping. | |
53 void Stop(); | |
54 | |
55 private: | |
56 // Called by AudioPushFifo zero or more times during the call to | 41 // Called by AudioPushFifo zero or more times during the call to |
57 // consumeAudio(). Delivers audio data with the required buffer size to the | 42 // consumeAudio(). Delivers audio data with the required buffer size to the |
58 // track. | 43 // tracks. |
59 void DeliverRebufferedAudio(const media::AudioBus& audio_bus, | 44 void DeliverRebufferedAudio(const media::AudioBus& audio_bus, |
60 int frame_delay); | 45 int frame_delay); |
61 | 46 |
62 // Deregisters this object from its blink::WebMediaStreamSource. | 47 // MediaStreamAudioSource implementation. |
63 void DeregisterFromBlinkSource(); | 48 void DoStopSource() final; |
| 49 bool EnsureSourceIsStarted() final; |
64 | 50 |
65 // Used to DCHECK that some methods are called on the correct thread. | 51 // True while this WebAudioMediaStreamSource is registered with |
66 base::ThreadChecker thread_checker_; | 52 // |blink_source_| and is consuming audio. |
67 | 53 bool is_registered_consumer_; |
68 // The audio track this WebAudioCapturerSource is feeding data to. | |
69 WebRtcLocalAudioTrack* track_; | |
70 | |
71 media::AudioParameters params_; | |
72 | |
73 // Flag to help notify the |track_| when the audio format has changed. | |
74 bool audio_format_changed_; | |
75 | 54 |
76 // A wrapper used for providing audio to |fifo_|. | 55 // A wrapper used for providing audio to |fifo_|. |
77 scoped_ptr<media::AudioBus> wrapper_bus_; | 56 scoped_ptr<media::AudioBus> wrapper_bus_; |
78 | 57 |
79 // Takes in the audio data passed to consumeAudio() and re-buffers it into 10 | 58 // Takes in the audio data passed to consumeAudio() and re-buffers it into 10 |
80 // ms chunks for the track. This ensures each chunk of audio delivered to the | 59 // ms chunks for the tracks. This ensures each chunk of audio delivered to |
81 // track has the required buffer size, regardless of the amount of audio | 60 // the tracks has the required buffer size, regardless of the amount of audio |
82 // provided via each consumeAudio() call. | 61 // provided via each consumeAudio() call. |
83 media::AudioPushFifo fifo_; | 62 media::AudioPushFifo fifo_; |
84 | 63 |
85 // Used to pass the reference timestamp between DeliverDecodedAudio() and | 64 // Used to pass the reference timestamp between DeliverDecodedAudio() and |
86 // DeliverRebufferedAudio(). | 65 // DeliverRebufferedAudio(). |
87 base::TimeTicks current_reference_time_; | 66 base::TimeTicks current_reference_time_; |
88 | 67 |
89 // Synchronizes HandleCapture() with AudioCapturerSource calls. | |
90 base::Lock lock_; | |
91 bool started_; | |
92 | |
93 // This object registers with a blink::WebMediaStreamSource. We keep track of | 68 // This object registers with a blink::WebMediaStreamSource. We keep track of |
94 // that in order to be able to deregister before stopping the audio track. | 69 // that in order to be able to deregister before stopping this source. |
95 blink::WebMediaStreamSource blink_source_; | 70 blink::WebMediaStreamSource blink_source_; |
96 | 71 |
97 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); | 72 DISALLOW_COPY_AND_ASSIGN(WebAudioMediaStreamSource); |
98 }; | 73 }; |
99 | 74 |
100 } // namespace content | 75 } // namespace content |
101 | 76 |
102 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ | 77 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_MEDIA_STREAM_SOURCE_H_ |
OLD | NEW |