OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 CHROME_RENDERER_AUDIO_DEVICE_H_ |
| 6 #define CHROME_RENDERER_AUDIO_DEVICE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <vector> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/lock.h" |
| 13 #include "base/scoped_ptr.h" |
| 14 #include "base/shared_memory.h" |
| 15 #include "base/threading/simple_thread.h" |
| 16 #include "chrome/common/render_messages.h" |
| 17 #include "chrome/renderer/audio_message_filter.h" |
| 18 |
| 19 // Each instance of AudioDevice corresponds to one host stream. |
| 20 // This class is not thread-safe, so its methods must be called from |
| 21 // the same thread. |
| 22 class AudioDevice : public AudioMessageFilter::Delegate, |
| 23 public base::DelegateSimpleThread::Delegate { |
| 24 public: |
| 25 class RenderCallback { |
| 26 public: |
| 27 virtual void Render(const std::vector<float*>& audio_data, |
| 28 size_t number_of_frames) = 0; |
| 29 protected: |
| 30 virtual ~RenderCallback() { } |
| 31 }; |
| 32 |
| 33 // |buffer_size| is the number of sample-frames. |
| 34 AudioDevice(size_t buffer_size, |
| 35 int channels, |
| 36 double sample_rate, |
| 37 RenderCallback* callback); |
| 38 virtual ~AudioDevice(); |
| 39 |
| 40 // Returns |true| on success. |
| 41 bool Start(); |
| 42 bool Stop(); |
| 43 |
| 44 private: |
| 45 // AudioMessageFilter::Delegate implementation. |
| 46 virtual void OnRequestPacket(AudioBuffersState buffers_state); |
| 47 virtual void OnStateChanged(const ViewMsg_AudioStreamState_Params& state); |
| 48 virtual void OnCreated(base::SharedMemoryHandle handle, uint32 length); |
| 49 virtual void OnLowLatencyCreated(base::SharedMemoryHandle handle, |
| 50 base::SyncSocket::Handle socket_handle, |
| 51 uint32 length); |
| 52 virtual void OnVolume(double volume); |
| 53 virtual void OnDestroy(); |
| 54 |
| 55 // DelegateSimpleThread::Delegate implementation. |
| 56 virtual void Run(); |
| 57 |
| 58 // Format |
| 59 size_t buffer_size_; // in sample-frames |
| 60 int channels_; |
| 61 double sample_rate_; |
| 62 |
| 63 // Calls the client's callback for rendering audio. |
| 64 void FireRenderCallback(); |
| 65 RenderCallback* callback_; |
| 66 |
| 67 // The client callback renders audio into here. |
| 68 std::vector<float*> audio_data_; |
| 69 |
| 70 // Callbacks for rendering audio occur on this thread. |
| 71 scoped_ptr<base::DelegateSimpleThread> audio_thread_; |
| 72 |
| 73 // IPC message stuff. |
| 74 base::SharedMemory* shared_memory() { return shared_memory_.get(); } |
| 75 base::SyncSocket* socket() { return socket_.get(); } |
| 76 void* shared_memory_data() { return shared_memory()->memory(); } |
| 77 |
| 78 static scoped_refptr<AudioMessageFilter> filter_; |
| 79 static Lock message_filter_lock_; |
| 80 int32 stream_id_; |
| 81 scoped_ptr<base::SharedMemory> shared_memory_; |
| 82 scoped_ptr<base::SyncSocket> socket_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(AudioDevice); |
| 85 }; |
| 86 |
| 87 #endif // CHROME_RENDERER_AUDIO_DEVICE_H_ |
OLD | NEW |