| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 5 #ifndef MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
| 6 #define MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 6 #define MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // share the same thread instead of spinning one per instance. | 31 // share the same thread instead of spinning one per instance. |
| 32 class MEDIA_EXPORT AudioDeviceThread { | 32 class MEDIA_EXPORT AudioDeviceThread { |
| 33 public: | 33 public: |
| 34 // This is the callback interface/base class that Audio[Output|Input]Device | 34 // This is the callback interface/base class that Audio[Output|Input]Device |
| 35 // implements to render input/output data. The callbacks run on the | 35 // implements to render input/output data. The callbacks run on the |
| 36 // thread owned by AudioDeviceThread. | 36 // thread owned by AudioDeviceThread. |
| 37 class Callback { | 37 class Callback { |
| 38 public: | 38 public: |
| 39 Callback(const AudioParameters& audio_parameters, | 39 Callback(const AudioParameters& audio_parameters, |
| 40 base::SharedMemoryHandle memory, | 40 base::SharedMemoryHandle memory, |
| 41 int memory_length); | 41 int memory_length, |
| 42 int total_segments); |
| 42 virtual ~Callback(); | 43 virtual ~Callback(); |
| 43 | 44 |
| 44 // One time initialization for the callback object on the audio thread. | 45 // One time initialization for the callback object on the audio thread. |
| 45 void InitializeOnAudioThread(); | 46 void InitializeOnAudioThread(); |
| 46 | 47 |
| 47 // Derived implementations must call shared_memory_.Map appropriately | 48 // Derived implementations must call shared_memory_.Map appropriately |
| 48 // before Process can be called. | 49 // before Process can be called. |
| 49 virtual void MapSharedMemory() = 0; | 50 virtual void MapSharedMemory() = 0; |
| 50 | 51 |
| 51 // Called whenever we receive notifications about pending data. | 52 // Called whenever we receive notifications about pending data. |
| 52 virtual void Process(int pending_data) = 0; | 53 // The |index| states which shared memory buffer segment is used. |
| 54 virtual void Process(int pending_data, int index) = 0; |
| 53 | 55 |
| 54 protected: | 56 protected: |
| 55 // Protected so that derived classes can access directly. | 57 // Protected so that derived classes can access directly. |
| 56 // The variables are 'const' since values are calculated/set in the | 58 // The variables are 'const' since values are calculated/set in the |
| 57 // constructor and must never change. | 59 // constructor and must never change. |
| 58 const AudioParameters audio_parameters_; | 60 const AudioParameters audio_parameters_; |
| 59 const int samples_per_ms_; | 61 const int samples_per_ms_; |
| 60 const int bytes_per_ms_; | 62 const int bytes_per_ms_; |
| 61 | 63 |
| 62 base::SharedMemory shared_memory_; | 64 base::SharedMemory shared_memory_; |
| 63 const int memory_length_; | 65 const int memory_length_; |
| 66 const int total_segments_; |
| 67 int segment_length_; |
| 64 | 68 |
| 65 private: | 69 private: |
| 66 DISALLOW_COPY_AND_ASSIGN(Callback); | 70 DISALLOW_COPY_AND_ASSIGN(Callback); |
| 67 }; | 71 }; |
| 68 | 72 |
| 69 AudioDeviceThread(); | 73 AudioDeviceThread(bool need_read_index); |
| 70 ~AudioDeviceThread(); | 74 ~AudioDeviceThread(); |
| 71 | 75 |
| 72 // Starts the audio thread. The thread must not already be running. | 76 // Starts the audio thread. The thread must not already be running. |
| 73 void Start(AudioDeviceThread::Callback* callback, | 77 void Start(AudioDeviceThread::Callback* callback, |
| 74 base::SyncSocket::Handle socket, | 78 base::SyncSocket::Handle socket, |
| 75 const char* thread_name); | 79 const char* thread_name); |
| 76 | 80 |
| 77 // This tells the audio thread to stop and clean up the data. | 81 // This tells the audio thread to stop and clean up the data. |
| 78 // The method can stop the thread synchronously or asynchronously. | 82 // The method can stop the thread synchronously or asynchronously. |
| 79 // In the latter case, the thread will still be running after Stop() | 83 // In the latter case, the thread will still be running after Stop() |
| (...skipping 11 matching lines...) Expand all Loading... |
| 91 private: | 95 private: |
| 92 // Our own private SimpleThread override. We implement this in a | 96 // Our own private SimpleThread override. We implement this in a |
| 93 // private class so that we get the following benefits: | 97 // private class so that we get the following benefits: |
| 94 // 1) AudioDeviceThread doesn't expose SimpleThread methods. | 98 // 1) AudioDeviceThread doesn't expose SimpleThread methods. |
| 95 // I.e. the caller can't call Start()/Stop() - which would be bad. | 99 // I.e. the caller can't call Start()/Stop() - which would be bad. |
| 96 // 2) We override ThreadMain to add additional on-thread initialization | 100 // 2) We override ThreadMain to add additional on-thread initialization |
| 97 // while still synchronized with SimpleThread::Start() to provide | 101 // while still synchronized with SimpleThread::Start() to provide |
| 98 // reliable initialization. | 102 // reliable initialization. |
| 99 class Thread; | 103 class Thread; |
| 100 | 104 |
| 105 // Flag indicating whether need to read index from sync socket. |
| 106 bool need_read_index_; |
| 107 |
| 101 base::Lock thread_lock_; | 108 base::Lock thread_lock_; |
| 102 scoped_refptr<AudioDeviceThread::Thread> thread_; | 109 scoped_refptr<AudioDeviceThread::Thread> thread_; |
| 103 | 110 |
| 104 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); | 111 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); |
| 105 }; | 112 }; |
| 106 | 113 |
| 107 } // namespace media. | 114 } // namespace media. |
| 108 | 115 |
| 109 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 116 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
| OLD | NEW |