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