OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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_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 | 9 #include "base/time/time.h" |
10 #include "base/macros.h" | 10 #include "content/renderer/media/media_stream_audio_source.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "media/base/audio_bus.h" |
12 #include "base/synchronization/lock.h" | 12 #include "media/base/audio_rechunker.h" |
13 #include "base/threading/thread_checker.h" | |
14 #include "media/audio/audio_parameters.h" | |
15 #include "media/base/audio_capturer_source.h" | |
16 #include "media/base/audio_fifo.h" | |
17 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" | 13 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" |
18 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
19 #include "third_party/WebKit/public/platform/WebVector.h" | 15 #include "third_party/WebKit/public/platform/WebVector.h" |
20 | 16 |
21 namespace content { | 17 namespace content { |
22 | 18 |
23 class WebRtcLocalAudioTrack; | 19 // Implements the WebAudioDestinationConsumer interface to provide a source of |
24 | 20 // audio data (i.e., the output from a graph of WebAudio nodes) to one or more |
25 // WebAudioCapturerSource is the missing link between | 21 // MediaStreamAudioTracks. Audio data is transported directly to the tracks in |
26 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack. | 22 // 10 ms chunks. |
27 // | 23 class WebAudioMediaStreamSource |
28 // 1. WebKit calls the setFormat() method setting up the basic stream format | 24 : NON_EXPORTED_BASE(public MediaStreamAudioSource), |
29 // (channels, and sample-rate). | 25 public blink::WebAudioDestinationConsumer { |
30 // 2. consumeAudio() is called periodically by WebKit which dispatches the | |
31 // audio stream to the WebRtcLocalAudioTrack::Capture() method. | |
32 class WebAudioCapturerSource | |
33 : public base::RefCountedThreadSafe<WebAudioCapturerSource>, | |
34 public blink::WebAudioDestinationConsumer { | |
35 public: | 26 public: |
36 explicit WebAudioCapturerSource( | 27 explicit WebAudioMediaStreamSource( |
37 const blink::WebMediaStreamSource& blink_source); | 28 const blink::WebMediaStreamSource& blink_source); |
38 | 29 |
| 30 ~WebAudioMediaStreamSource() final; |
| 31 |
| 32 private: |
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. | 42 // Called by AudioRechunker zero or more times during the call to |
48 // |track| is the sink of the data flow. |source_provider| is the source of | 43 // consumeAudio(). Delivers re-chunked audio data to the tracks. |
49 // the data flow where stream information like delay, volume, key_pressed, | 44 void DeliverRechunkedAudio(const media::AudioBus& audio_bus, |
50 // is stored. | 45 base::TimeDelta reference_timestamp); |
51 void Start(WebRtcLocalAudioTrack* track); | |
52 | 46 |
53 // Called when the media audio track is stopping. | 47 // MediaStreamAudioSource implementation. |
54 void Stop(); | 48 void DoStopSource() final; |
| 49 bool EnsureSourceIsStarted() final; |
55 | 50 |
56 protected: | |
57 friend class base::RefCountedThreadSafe<WebAudioCapturerSource>; | |
58 ~WebAudioCapturerSource() override; | |
59 | |
60 private: | |
61 // Removes this object from a blink::WebMediaStreamSource with which it | 51 // Removes this object from a blink::WebMediaStreamSource with which it |
62 // might be registered. The goal is to avoid dangling pointers. | 52 // might be registered. The goal is to avoid dangling pointers. |
63 void removeFromBlinkSource(); | 53 void removeFromBlinkSource(); |
64 | 54 |
65 // Used to DCHECK that some methods are called on the correct thread. | 55 // This object registers and de-registers as an audio consumer of a |
66 base::ThreadChecker thread_checker_; | 56 // blink::WebMediaStreamSource. |
| 57 blink::WebMediaStreamSource blink_source_; |
67 | 58 |
68 // The audio track this WebAudioCapturerSource is feeding data to. | 59 // True while this WebAudioMediaStreamSource is registered with |
69 // WebRtcLocalAudioTrack is reference counted, and owning this object. | 60 // |blink_source_| and is consuming audio. |
70 // To avoid circular reference, a raw pointer is kept here. | 61 bool is_started_; |
71 WebRtcLocalAudioTrack* track_; | |
72 | 62 |
73 media::AudioParameters params_; | 63 // An adapter used for providing audio to |rechunker_|. |
74 | |
75 // Flag to help notify the |track_| when the audio format has changed. | |
76 bool audio_format_changed_; | |
77 | |
78 // Wraps data coming from HandleCapture(). | |
79 scoped_ptr<media::AudioBus> wrapper_bus_; | 64 scoped_ptr<media::AudioBus> wrapper_bus_; |
80 | 65 |
81 // Bus for reading from FIFO and calling the CaptureCallback. | 66 // Takes in the audio data passed to consumeAudio() and re-chunks it into 10 |
82 scoped_ptr<media::AudioBus> capture_bus_; | 67 // ms chunks for the tracks. This ensures each chunk of audio delivered to |
| 68 // the tracks has the same buffer size, even if audio is provided in |
| 69 // varying-sized chunks. |
| 70 media::AudioRechunker rechunker_; |
83 | 71 |
84 // Handles mismatch between WebAudio buffer size and WebRTC. | 72 DISALLOW_COPY_AND_ASSIGN(WebAudioMediaStreamSource); |
85 scoped_ptr<media::AudioFifo> fifo_; | |
86 | |
87 // Synchronizes HandleCapture() with AudioCapturerSource calls. | |
88 base::Lock lock_; | |
89 bool started_; | |
90 | |
91 // 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. | |
93 blink::WebMediaStreamSource blink_source_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); | |
96 }; | 73 }; |
97 | 74 |
98 } // namespace content | 75 } // namespace content |
99 | 76 |
100 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ | 77 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_MEDIA_STREAM_SOURCE_H_ |
OLD | NEW |