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