Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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_MEDIA_AUDIO_DEVICE_H_ | 5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 6 #define CONTENT_RENDERER_MEDIA_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/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop.h" | |
| 13 #include "base/shared_memory.h" | 14 #include "base/shared_memory.h" |
| 14 #include "base/threading/simple_thread.h" | 15 #include "base/threading/simple_thread.h" |
| 15 #include "content/renderer/media/audio_message_filter.h" | 16 #include "content/renderer/media/audio_message_filter.h" |
| 16 | 17 |
| 17 struct AudioParameters; | 18 struct AudioParameters; |
| 18 | 19 |
| 19 // Each instance of AudioDevice corresponds to one host stream. | 20 // TODO(henrika): add comments here... |
|
scherkus (not reviewing)
2011/06/16 21:56:22
want to add comments right away? don't see a reas
henrika_dont_use
2011/06/17 11:52:02
Done.
| |
| 20 // This class is not thread-safe, so its methods must be called from | |
| 21 // the same thread. | |
| 22 class AudioDevice : public AudioMessageFilter::Delegate, | 21 class AudioDevice : public AudioMessageFilter::Delegate, |
| 23 public base::DelegateSimpleThread::Delegate, | 22 public base::DelegateSimpleThread::Delegate, |
| 24 public base::RefCountedThreadSafe<AudioDevice> { | 23 public base::RefCountedThreadSafe<AudioDevice> { |
| 25 public: | 24 public: |
| 26 class RenderCallback { | 25 class RenderCallback { |
| 27 public: | 26 public: |
| 28 virtual void Render(const std::vector<float*>& audio_data, | 27 virtual void Render(const std::vector<float*>& audio_data, |
| 29 size_t number_of_frames, | 28 size_t number_of_frames, |
| 30 size_t audio_delay_milliseconds) = 0; | 29 size_t audio_delay_milliseconds) = 0; |
| 31 protected: | 30 protected: |
| 32 virtual ~RenderCallback() {} | 31 virtual ~RenderCallback() {} |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 // |buffer_size| is the number of sample-frames. | 34 // Methods called on creating thread ---------------------------------------- |
| 36 AudioDevice(size_t buffer_size, | 35 AudioDevice(size_t buffer_size, |
| 37 int channels, | 36 int channels, |
| 38 double sample_rate, | 37 double sample_rate, |
| 39 RenderCallback* callback); | 38 RenderCallback* callback); |
| 40 virtual ~AudioDevice(); | 39 virtual ~AudioDevice(); |
| 41 | 40 |
| 42 // Starts audio playback. Returns |true| on success. | 41 // Starts audio playback. Returns |true| on success. |
| 43 bool Start(); | 42 bool Start(); |
| 44 | 43 |
| 45 // Stops audio playback. Returns |true| on success. | 44 // Stops audio playback. Returns |true| on success. |
| 46 bool Stop(); | 45 bool Stop(); |
| 47 | 46 |
| 48 // Sets the playback volume, with range [0.0, 1.0] inclusive. | 47 // Sets the playback volume, with range [0.0, 1.0] inclusive. |
| 49 // Returns |true| on success. | 48 // Returns |true| on success. |
| 50 bool SetVolume(double volume); | 49 bool SetVolume(double volume); |
| 51 | 50 |
| 52 // Gets the playback volume, with range [0.0, 1.0] inclusive. | 51 // Gets the playback volume, with range [0.0, 1.0] inclusive. |
| 53 // Returns |true| on success. | 52 // Returns |true| on success. |
| 54 bool GetVolume(double* volume); | 53 bool GetVolume(double* volume); |
| 55 | 54 |
| 56 double sample_rate() const { return sample_rate_; } | 55 double sample_rate() const { return sample_rate_; } |
| 57 size_t buffer_size() const { return buffer_size_; } | 56 size_t buffer_size() const { return buffer_size_; } |
| 58 | 57 |
| 59 private: | 58 // Methods called on IO thread ---------------------------------------------- |
| 60 // I/O thread backends to above functions. | 59 // AudioMessageFilter::Delegate methods, called by AudioMessageFilter. |
| 61 void InitializeOnIOThread(const AudioParameters& params); | |
| 62 void StartOnIOThread(); | |
| 63 void ShutDownOnIOThread(); | |
| 64 void SetVolumeOnIOThread(double volume); | |
| 65 | |
| 66 // AudioMessageFilter::Delegate implementation. | |
| 67 virtual void OnRequestPacket(AudioBuffersState buffers_state); | 60 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
| 68 virtual void OnStateChanged(AudioStreamState state); | 61 virtual void OnStateChanged(AudioStreamState state); |
| 69 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); | 62 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); |
| 70 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, | 63 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
| 71 base::SyncSocket::Handle socket_handle, | 64 base::SyncSocket::Handle socket_handle, |
| 72 uint32 length); | 65 uint32 length); |
| 73 virtual void OnVolume(double volume); | 66 virtual void OnVolume(double volume); |
| 74 | 67 |
| 68 private: | |
| 69 // Methods called on IO thread ---------------------------------------------- | |
| 70 // The following methods are tasks posted on the IO thread that needs to | |
| 71 // be executed on that thread. They interact with AudioMessageFilter and | |
| 72 // sends IPC messages on that thread. | |
| 73 void InitializeTask(const AudioParameters& params); | |
|
scherkus (not reviewing)
2011/06/16 21:56:22
instead of xxxTask suffix, I wonder if it'd be cle
henrika_dont_use
2011/06/17 11:52:02
Agree. Will change back. Used notation in existing
scherkus (not reviewing)
2011/06/17 17:50:10
We can get to that later :)
| |
| 74 void StartTask(); | |
| 75 void ShutDownTask(); | |
| 76 void SetVolumeTask(double volume); | |
| 77 | |
| 78 // Method called on the audio thread ---------------------------------------- | |
| 79 // Calls the client's callback for rendering audio. | |
| 80 void FireRenderCallback(); | |
| 81 | |
| 75 // DelegateSimpleThread::Delegate implementation. | 82 // DelegateSimpleThread::Delegate implementation. |
| 76 virtual void Run(); | 83 virtual void Run(); |
| 77 | 84 |
| 78 // Format | 85 // Format |
| 79 size_t buffer_size_; // in sample-frames | 86 size_t buffer_size_; // in sample-frames |
| 80 int channels_; | 87 int channels_; |
| 81 int bits_per_sample_; | 88 int bits_per_sample_; |
| 82 double sample_rate_; | 89 double sample_rate_; |
| 83 | 90 |
| 84 // Calls the client's callback for rendering audio. | |
| 85 void FireRenderCallback(); | |
| 86 RenderCallback* callback_; | 91 RenderCallback* callback_; |
| 87 | 92 |
| 88 // The client callback renders audio into here. | 93 // The client callback renders audio into here. |
| 89 std::vector<float*> audio_data_; | 94 std::vector<float*> audio_data_; |
| 90 | 95 |
| 91 // The client stores the last reported audio delay in this member. | 96 // The client stores the last reported audio delay in this member. |
| 92 // The delay shall reflect the amount of audio which still resides in | 97 // The delay shall reflect the amount of audio which still resides in |
| 93 // the output buffer, i.e., the expected audio output delay. | 98 // the output buffer, i.e., the expected audio output delay. |
| 94 int audio_delay_milliseconds_; | 99 int audio_delay_milliseconds_; |
| 95 | 100 |
| 96 // The current volume scaling [0.0, 1.0] of the audio stream. | 101 // The current volume scaling [0.0, 1.0] of the audio stream. |
| 97 double volume_; | 102 double volume_; |
| 98 | 103 |
| 99 // Callbacks for rendering audio occur on this thread. | 104 // Callbacks for rendering audio occur on this thread. |
| 100 scoped_ptr<base::DelegateSimpleThread> audio_thread_; | 105 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
| 101 | 106 |
| 102 // IPC message stuff. | 107 // IPC message stuff. |
| 103 base::SharedMemory* shared_memory() { return shared_memory_.get(); } | 108 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
| 104 base::SyncSocket* socket() { return socket_.get(); } | 109 base::SyncSocket* socket() { return socket_.get(); } |
| 105 void* shared_memory_data() { return shared_memory()->memory(); } | 110 void* shared_memory_data() { return shared_memory()->memory(); } |
| 106 | 111 |
| 107 // MessageFilter used to send/receive IPC. THIS MUST ONLY BE ACCESSED ON THE | 112 // MessageFilter used to send/receive IPC. Only accessed on the IO thread. |
| 108 // I/O thread except to send messages and get the message loop. | 113 scoped_refptr<AudioMessageFilter> filter_; |
| 109 static scoped_refptr<AudioMessageFilter> filter_; | |
| 110 | 114 |
| 111 // Our ID on the message filter. THIS MUST ONLY BE ACCESSED ON THE I/O THREAD | 115 // Message loop for the IO thread. |
| 112 // or else you could race with the initialize function which sets it. | 116 MessageLoop* io_loop_; |
| 117 | |
| 118 // Our ID on the message filter. Only modified on the IO thread. | |
| 113 int32 stream_id_; | 119 int32 stream_id_; |
| 114 | 120 |
| 115 scoped_ptr<base::SharedMemory> shared_memory_; | 121 scoped_ptr<base::SharedMemory> shared_memory_; |
| 116 scoped_ptr<base::SyncSocket> socket_; | 122 scoped_ptr<base::SyncSocket> socket_; |
| 117 | 123 |
| 118 DISALLOW_COPY_AND_ASSIGN(AudioDevice); | 124 DISALLOW_COPY_AND_ASSIGN(AudioDevice); |
| 119 }; | 125 }; |
| 120 | 126 |
| 121 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ | 127 #endif // CONTENT_RENDERER_MEDIA_AUDIO_DEVICE_H_ |
| OLD | NEW |