webaudio_capturer_source.h revision f2477e01787aa58f445919b809d89e252beef54f
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.
4
5#ifndef CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_
6#define CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_
7
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/WebVector.h"
16
17namespace content {
18
19class WebRtcAudioCapturer;
20class WebRtcLocalAudioTrack;
21
22// WebAudioCapturerSource is the missing link between
23// WebAudio's MediaStreamAudioDestinationNode and WebRtcLocalAudioTrack.
24//
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.
29class WebAudioCapturerSource
30    :  public base::RefCountedThreadSafe<WebAudioCapturerSource>,
31       public blink::WebAudioDestinationConsumer {
32 public:
33  WebAudioCapturerSource();
34
35  // WebAudioDestinationConsumer implementation.
36  // setFormat() is called early on, so that we can configure the audio track.
37  virtual void setFormat(size_t number_of_channels, float sample_rate) OVERRIDE;
38  // MediaStreamAudioDestinationNode periodically calls consumeAudio().
39  // Called on the WebAudio audio thread.
40  virtual void consumeAudio(const blink::WebVector<const float*>& audio_data,
41      size_t number_of_frames) OVERRIDE;
42
43  // Called when the WebAudioCapturerSource is hooking to a media audio track.
44  // |track| is the sink of the data flow. |source_provider| is the source of
45  // the data flow where stream information like delay, volume, key_pressed,
46  // is stored.
47  void Start(WebRtcLocalAudioTrack* track, WebRtcAudioCapturer* capturer);
48
49  // Called when the media audio track is stopping.
50  void Stop();
51
52 protected:
53  friend class base::RefCountedThreadSafe<WebAudioCapturerSource>;
54  virtual ~WebAudioCapturerSource();
55
56 private:
57  // Used to DCHECK that some methods are called on the correct thread.
58  base::ThreadChecker thread_checker_;
59
60  // The audio track this WebAudioCapturerSource is feeding data to.
61  // WebRtcLocalAudioTrack is reference counted, and owning this object.
62  // To avoid circular reference, a raw pointer is kept here.
63  WebRtcLocalAudioTrack* track_;
64
65  // A raw pointer to the capturer to get audio processing params like
66  // delay, volume, key_pressed information.
67  // This |capturer_| is guaranteed to outlive this object.
68  WebRtcAudioCapturer* capturer_;
69
70  media::AudioParameters params_;
71
72  // Flag to help notify the |track_| when the audio format has changed.
73  bool audio_format_changed_;
74
75  // Wraps data coming from HandleCapture().
76  scoped_ptr<media::AudioBus> wrapper_bus_;
77
78  // Bus for reading from FIFO and calling the CaptureCallback.
79  scoped_ptr<media::AudioBus> capture_bus_;
80
81  // Handles mismatch between WebAudio buffer size and WebRTC.
82  scoped_ptr<media::AudioFifo> fifo_;
83
84  // Synchronizes HandleCapture() with AudioCapturerSource calls.
85  base::Lock lock_;
86  bool started_;
87
88  DISALLOW_COPY_AND_ASSIGN(WebAudioCapturerSource);
89};
90
91}  // namespace content
92
93#endif  // CONTENT_RENDERER_MEDIA_WEBAUDIO_CAPTURER_SOURCE_H_
94