Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / webaudio_capturer_source.h
blob1e682576a0c633bbbd93571adaead7510b59183b
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/synchronization/lock.h"
10 #include "base/threading/thread_checker.h"
11 #include "media/audio/audio_parameters.h"
12 #include "media/base/audio_capturer_source.h"
13 #include "media/base/audio_fifo.h"
14 #include "third_party/WebKit/public/platform/WebAudioDestinationConsumer.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
16 #include "third_party/WebKit/public/platform/WebVector.h"
18 namespace content {
20 class WebRtcLocalAudioTrack;
22 // WebAudioCapturerSource is the missing link between
23 // WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack.
25 // 1. WebKit calls the setFormat() method setting up the basic stream format
26 // (channels, and sample-rate).
27 // 2. consumeAudio() is called periodically by WebKit which dispatches the
28 // audio stream to the WebRtcLocalAudioTrack::Capture() method.
29 class WebAudioCapturerSource
30 : public base::RefCountedThreadSafe<WebAudioCapturerSource>,
31 public blink::WebAudioDestinationConsumer {
32 public:
33 explicit WebAudioCapturerSource(
34 const blink::WebMediaStreamSource& blink_source);
36 // WebAudioDestinationConsumer implementation.
37 // setFormat() is called early on, so that we can configure the audio track.
38 void setFormat(size_t number_of_channels, float sample_rate) override;
39 // MediaStreamAudioDestinationNode periodically calls consumeAudio().
40 // Called on the WebAudio audio thread.
41 void consumeAudio(const blink::WebVector<const float*>& audio_data,
42 size_t number_of_frames) override;
44 // Called when the WebAudioCapturerSource is hooking to a media audio track.
45 // |track| is the sink of the data flow. |source_provider| is the source of
46 // the data flow where stream information like delay, volume, key_pressed,
47 // is stored.
48 void Start(WebRtcLocalAudioTrack* track);
50 // Called when the media audio track is stopping.
51 void Stop();
53 protected:
54 friend class base::RefCountedThreadSafe<WebAudioCapturerSource>;
55 ~WebAudioCapturerSource() override;
57 private:
58 // Removes this object from a blink::WebMediaStreamSource with which it
59 // might be registered. The goal is to avoid dangling pointers.
60 void removeFromBlinkSource();
62 // Used to DCHECK that some methods are called on the correct thread.
63 base::ThreadChecker thread_checker_;
65 // The audio track this WebAudioCapturerSource is feeding data to.
66 // WebRtcLocalAudioTrack is reference counted, and owning this object.
67 // To avoid circular reference, a raw pointer is kept here.
68 WebRtcLocalAudioTrack* track_;
70 media::AudioParameters params_;
72 // Flag to help notify the |track_| when the audio format has changed.
73 bool audio_format_changed_;
75 // Wraps data coming from HandleCapture().
76 scoped_ptr<media::AudioBus> wrapper_bus_;
78 // Bus for reading from FIFO and calling the CaptureCallback.
79 scoped_ptr<media::AudioBus> capture_bus_;
81 // Handles mismatch between WebAudio buffer size and WebRTC.
82 scoped_ptr<media::AudioFifo> fifo_;
84 // Synchronizes HandleCapture() with AudioCapturerSource calls.
85 base::Lock lock_;
86 bool started_;
88 // This object registers with a blink::WebMediaStreamSource. We keep track of
89 // that in order to be able to deregister before stopping the audio track.
90 blink::WebMediaStreamSource blink_source_;
92 DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource);
95 } // namespace content
97 #endif // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_