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 "base/time/time.h" | 13 #include "base/time/time.h" |
15 #include "media/audio/audio_parameters.h" | 14 #include "media/audio/audio_parameters.h" |
16 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
17 #include "media/base/audio_capturer_source.h" | 16 #include "media/base/audio_capturer_source.h" |
18 #include "media/base/audio_push_fifo.h" | 17 #include "media/base/audio_push_fifo.h" |
19 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" | 18 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h" |
20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 19 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
21 #include "third_party/WebKit/public/platform/WebVector.h" | 20 #include "third_party/WebKit/public/platform/WebVector.h" |
22 | 21 |
23 namespace content { | 22 namespace content { |
24 | 23 |
25 class WebRtcLocalAudioTrack; | 24 class WebRtcLocalAudioTrack; |
26 | 25 |
27 // WebAudioCapturerSource is the missing link between | 26 // WebAudioCapturerSource is the missing link between |
28 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack. | 27 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack. |
29 // | 28 // |
30 // 1. WebKit calls the setFormat() method setting up the basic stream format | 29 // 1. WebKit calls the setFormat() method setting up the basic stream format |
31 // (channels, and sample-rate). | 30 // (channels, and sample-rate). |
32 // 2. consumeAudio() is called periodically by WebKit which dispatches the | 31 // 2. consumeAudio() is called periodically by WebKit which dispatches the |
33 // audio stream to the WebRtcLocalAudioTrack::Capture() method. | 32 // audio stream to the WebRtcLocalAudioTrack::Capture() method. |
34 class WebAudioCapturerSource | 33 class WebAudioCapturerSource : public blink::WebAudioDestinationConsumer { |
35 : public base::RefCountedThreadSafe<WebAudioCapturerSource>, | |
36 public blink::WebAudioDestinationConsumer { | |
37 public: | 34 public: |
38 explicit WebAudioCapturerSource( | 35 explicit WebAudioCapturerSource(blink::WebMediaStreamSource* blink_source); |
39 const blink::WebMediaStreamSource& blink_source); | 36 |
| 37 ~WebAudioCapturerSource() override; |
40 | 38 |
41 // WebAudioDestinationConsumer implementation. | 39 // WebAudioDestinationConsumer implementation. |
42 // setFormat() is called early on, so that we can configure the audio track. | 40 // setFormat() is called early on, so that we can configure the audio track. |
43 void setFormat(size_t number_of_channels, float sample_rate) override; | 41 void setFormat(size_t number_of_channels, float sample_rate) override; |
44 // MediaStreamAudioDestinationNode periodically calls consumeAudio(). | 42 // MediaStreamAudioDestinationNode periodically calls consumeAudio(). |
45 // Called on the WebAudio audio thread. | 43 // Called on the WebAudio audio thread. |
46 void consumeAudio(const blink::WebVector<const float*>& audio_data, | 44 void consumeAudio(const blink::WebVector<const float*>& audio_data, |
47 size_t number_of_frames) override; | 45 size_t number_of_frames) override; |
48 | 46 |
49 // Called when the WebAudioCapturerSource is hooking to a media audio track. | 47 // Called when the WebAudioCapturerSource is hooking to a media audio track. |
50 // |track| is the sink of the data flow. |source_provider| is the source of | 48 // |track| is the sink of the data flow and must remain alive until Stop() is |
51 // the data flow where stream information like delay, volume, key_pressed, | 49 // called. |
52 // is stored. | |
53 void Start(WebRtcLocalAudioTrack* track); | 50 void Start(WebRtcLocalAudioTrack* track); |
54 | 51 |
55 // Called when the media audio track is stopping. | 52 // Called when the media audio track is stopping. |
56 void Stop(); | 53 void Stop(); |
57 | 54 |
58 protected: | |
59 friend class base::RefCountedThreadSafe<WebAudioCapturerSource>; | |
60 ~WebAudioCapturerSource() override; | |
61 | |
62 private: | 55 private: |
63 // Called by AudioPushFifo zero or more times during the call to | 56 // Called by AudioPushFifo zero or more times during the call to |
64 // consumeAudio(). Delivers audio data with the required buffer size to the | 57 // consumeAudio(). Delivers audio data with the required buffer size to the |
65 // track. | 58 // track. |
66 void DeliverRebufferedAudio(const media::AudioBus& audio_bus, | 59 void DeliverRebufferedAudio(const media::AudioBus& audio_bus, |
67 int frame_delay); | 60 int frame_delay); |
68 | 61 |
69 // Removes this object from a blink::WebMediaStreamSource with which it | 62 // Deregisters this object from its blink::WebMediaStreamSource. |
70 // might be registered. The goal is to avoid dangling pointers. | 63 void DeregisterFromBlinkSource(); |
71 void removeFromBlinkSource(); | |
72 | 64 |
73 // Used to DCHECK that some methods are called on the correct thread. | 65 // Used to DCHECK that some methods are called on the correct thread. |
74 base::ThreadChecker thread_checker_; | 66 base::ThreadChecker thread_checker_; |
75 | 67 |
76 // The audio track this WebAudioCapturerSource is feeding data to. | 68 // The audio track this WebAudioCapturerSource is feeding data to. |
77 // WebRtcLocalAudioTrack is reference counted, and owning this object. | |
78 // To avoid circular reference, a raw pointer is kept here. | |
79 WebRtcLocalAudioTrack* track_; | 69 WebRtcLocalAudioTrack* track_; |
80 | 70 |
81 media::AudioParameters params_; | 71 media::AudioParameters params_; |
82 | 72 |
83 // Flag to help notify the |track_| when the audio format has changed. | 73 // Flag to help notify the |track_| when the audio format has changed. |
84 bool audio_format_changed_; | 74 bool audio_format_changed_; |
85 | 75 |
86 // A wrapper used for providing audio to |fifo_|. | 76 // A wrapper used for providing audio to |fifo_|. |
87 scoped_ptr<media::AudioBus> wrapper_bus_; | 77 scoped_ptr<media::AudioBus> wrapper_bus_; |
88 | 78 |
(...skipping 14 matching lines...) Expand all Loading... |
103 // This object registers with a blink::WebMediaStreamSource. We keep track of | 93 // This object registers with a blink::WebMediaStreamSource. We keep track of |
104 // that in order to be able to deregister before stopping the audio track. | 94 // that in order to be able to deregister before stopping the audio track. |
105 blink::WebMediaStreamSource blink_source_; | 95 blink::WebMediaStreamSource blink_source_; |
106 | 96 |
107 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); | 97 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource); |
108 }; | 98 }; |
109 | 99 |
110 } // namespace content | 100 } // namespace content |
111 | 101 |
112 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ | 102 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_ |
OLD | NEW |