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