OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_AUDIO_INPUT_DEVICE_H_ | |
6 #define CONTENT_RENDERER_AUDIO_INPUT_DEVICE_H_ | |
7 #pragma once | |
8 | |
9 #include <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/shared_memory.h" | |
14 #include "base/threading/simple_thread.h" | |
15 #include "content/renderer/audio_input_message_filter.h" | |
16 | |
17 struct AudioParameters; | |
18 | |
19 // Each instance of AudioInputDevice corresponds to one host stream. | |
20 // This class is not thread-safe, so its methods must be called from | |
21 // the same thread. | |
22 | |
23 // TODO(henrika): This class is based on the AudioDevice class and it has | |
24 // many components in common. Investigate potential for re-factoring. | |
25 class AudioInputDevice : public AudioInputMessageFilter::Delegate, | |
26 public base::DelegateSimpleThread::Delegate, | |
27 public base::RefCountedThreadSafe<AudioInputDevice> { | |
28 public: | |
29 class CaptureCallback { | |
30 public: | |
31 virtual void Capture(const std::vector<float*>& audio_data, | |
32 size_t number_of_frames, | |
33 size_t audio_delay_milliseconds) = 0; | |
34 protected: | |
35 virtual ~CaptureCallback() {} | |
36 }; | |
37 | |
38 // |buffer_size| is the number of sample-frames. | |
39 AudioInputDevice(size_t buffer_size, | |
40 int channels, | |
41 double sample_rate, | |
42 CaptureCallback* callback); | |
43 virtual ~AudioInputDevice(); | |
44 | |
45 // Starts audio capturing. Returns |true| on success. | |
46 bool Start(); | |
47 | |
48 // Stops audio capturing. Returns |true| on success. | |
49 bool Stop(); | |
50 | |
51 // Sets the capture volume scaling, with range [0.0, 1.0] inclusive. | |
52 // Returns |true| on success. | |
53 bool SetVolume(double volume); | |
54 | |
55 // Gets the capture volume scaling, with range [0.0, 1.0] inclusive. | |
56 // Returns |true| on success. | |
57 bool GetVolume(double* volume); | |
58 | |
59 double sample_rate() const { return sample_rate_; } | |
60 | |
61 size_t buffer_size() const { return buffer_size_; } | |
62 | |
63 private: | |
64 // I/O thread backends to above functions. | |
65 void InitializeOnIOThread(const AudioParameters& params); | |
66 void StartOnIOThread(); | |
67 void ShutDownOnIOThread(); | |
68 void SetVolumeOnIOThread(double volume); | |
69 | |
70 // AudioInputMessageFilter::Delegate implementation | |
71 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | |
72 base::SyncSocket::Handle socket_handle, | |
73 uint32 length); | |
74 virtual void OnVolume(double volume); | |
75 | |
76 // DelegateSimpleThread::Delegate implementation. | |
77 virtual void Run(); | |
78 | |
79 // Format | |
80 size_t buffer_size_; // in sample-frames | |
81 int channels_; | |
82 int bits_per_sample_; | |
83 double sample_rate_; | |
84 | |
85 // Calls the client's callback for capturing audio. | |
86 void FireCaptureCallback(); | |
87 CaptureCallback* callback_; | |
88 | |
89 // The client callback receives captured audio here. | |
90 std::vector<float*> audio_data_; | |
91 | |
92 // The client stores the last reported audio delay in this member. | |
93 // The delay shall reflect the amount of audio which still resides in | |
94 // the input buffer, i.e., the expected audio input delay. | |
95 int audio_delay_milliseconds_; | |
96 | |
97 // The current volume scaling [0.0, 1.0] of the audio stream. | |
98 double volume_; | |
99 | |
100 // Callbacks for capturing audio occur on this thread. | |
101 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | |
102 | |
103 // IPC message stuff. | |
104 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | |
105 base::SyncSocket* socket() { return socket_.get(); } | |
106 void* shared_memory_data() { return shared_memory()->memory(); } | |
107 | |
108 // MessageFilter used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE | |
109 // I/O thread except to send messages and get the message loop. | |
110 static scoped_refptr<AudioInputMessageFilter> filter_; | |
111 | |
112 // Our ID on the message filter. THIS MUST ONLY BE ACCESSED ON THE I/O THREAD | |
113 // or else you could race with the initialize function which sets it. | |
114 int32 stream_id_; | |
115 | |
116 scoped_ptr<base::SharedMemory> shared_memory_; | |
117 scoped_ptr<base::SyncSocket> socket_; | |
118 | |
119 DISALLOW_IMPLICIT_CONSTRUCTORS(AudioInputDevice); | |
120 }; | |
121 | |
122 #endif // CONTENT_RENDERER_AUDIO_INPUT_DEVICE_H_ | |
OLD | NEW |