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" |
11 #include "base/memory/scoped_vector.h" | |
11 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
12 #include "base/sync_socket.h" | 13 #include "base/sync_socket.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
15 #include "media/audio/audio_parameters.h" | 16 #include "media/audio/audio_parameters.h" |
16 #include "media/audio/shared_memory_util.h" | 17 #include "media/audio/shared_memory_util.h" |
17 | 18 |
18 class MessageLoop; | 19 class MessageLoop; |
19 | 20 |
20 namespace media { | 21 namespace media { |
21 class AudioBus; | 22 class AudioBus; |
22 | 23 |
23 // Data transfer between browser and render process uses a combination | 24 // Data transfer between browser and render process uses a combination |
24 // of sync sockets and shared memory. To read from the socket and render | 25 // of sync sockets and shared memory. To read from the socket and render |
25 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads | 26 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads |
26 // data from the browser via the socket and fills the shared memory from the | 27 // data from the browser via the socket and fills the shared memory from the |
27 // audio thread via the AudioDeviceThread::Callback interface/class. | 28 // audio thread via the AudioDeviceThread::Callback interface/class. |
28 // For more details see the documentation in audio_device.h. | 29 // For more details see the documentation in audio_device.h. |
29 // | 30 // |
30 // TODO(tommi): Multiple audio input/output device instances should be able to | 31 // TODO(tommi): Multiple audio input/output device instances should be able to |
31 // share the same thread instead of spinning one per instance. | 32 // share the same thread instead of spinning one per instance. |
32 class MEDIA_EXPORT AudioDeviceThread { | 33 class MEDIA_EXPORT AudioDeviceThread { |
33 public: | 34 public: |
34 // This is the callback interface/base class that Audio[Output|Input]Device | 35 // This is the callback interface/base class that Audio[Output|Input]Device |
35 // implements to render input/output data. The callbacks run on the | 36 // implements to render input/output data. The callbacks run on the |
36 // thread owned by AudioDeviceThread. | 37 // thread owned by AudioDeviceThread. |
37 class Callback { | 38 class Callback { |
38 public: | 39 public: |
40 typedef std::vector<base::SharedMemoryHandle> SharedMemoryHandleVector; | |
41 | |
39 Callback(const AudioParameters& audio_parameters, | 42 Callback(const AudioParameters& audio_parameters, |
40 base::SharedMemoryHandle memory, | 43 SharedMemoryHandleVector& memory, |
41 int memory_length); | 44 int memory_length); |
42 virtual ~Callback(); | 45 virtual ~Callback(); |
43 | 46 |
44 // One time initialization for the callback object on the audio thread. | 47 // One time initialization for the callback object on the audio thread. |
45 void InitializeOnAudioThread(); | 48 void InitializeOnAudioThread(); |
46 | 49 |
47 // Derived implementations must call shared_memory_.Map appropriately | 50 // Derived implementations must call shared_memory_.Map appropriately |
48 // before Process can be called. | 51 // before Process can be called. |
49 virtual void MapSharedMemory() = 0; | 52 virtual void MapSharedMemory() = 0; |
50 | 53 |
51 // Called whenever we receive notifications about pending data. | 54 // Called whenever we receive notifications about pending data. |
henrika (OOO until Aug 14)
2013/03/04 13:12:51
No comment about |index|.
wjia(left Chromium)
2013/03/05 02:40:13
Done.
| |
52 virtual void Process(int pending_data) = 0; | 55 virtual void Process(int pending_data, int index) = 0; |
53 | 56 |
54 protected: | 57 protected: |
55 // Protected so that derived classes can access directly. | 58 // Protected so that derived classes can access directly. |
56 // The variables are 'const' since values are calculated/set in the | 59 // The variables are 'const' since values are calculated/set in the |
57 // constructor and must never change. | 60 // constructor and must never change. |
58 const AudioParameters audio_parameters_; | 61 const AudioParameters audio_parameters_; |
59 const int samples_per_ms_; | 62 const int samples_per_ms_; |
60 const int bytes_per_ms_; | 63 const int bytes_per_ms_; |
61 | 64 |
62 base::SharedMemory shared_memory_; | 65 ScopedVector<base::SharedMemory> shared_memory_; |
63 const int memory_length_; | 66 const int memory_length_; |
64 | 67 |
65 private: | 68 private: |
66 DISALLOW_COPY_AND_ASSIGN(Callback); | 69 DISALLOW_COPY_AND_ASSIGN(Callback); |
67 }; | 70 }; |
68 | 71 |
69 AudioDeviceThread(); | 72 AudioDeviceThread(bool read_index); |
70 ~AudioDeviceThread(); | 73 ~AudioDeviceThread(); |
71 | 74 |
72 // Starts the audio thread. The thread must not already be running. | 75 // Starts the audio thread. The thread must not already be running. |
73 void Start(AudioDeviceThread::Callback* callback, | 76 void Start(AudioDeviceThread::Callback* callback, |
74 base::SyncSocket::Handle socket, | 77 base::SyncSocket::Handle socket, |
75 const char* thread_name); | 78 const char* thread_name); |
76 | 79 |
77 // This tells the audio thread to stop and clean up the data. | 80 // This tells the audio thread to stop and clean up the data. |
78 // The method can stop the thread synchronously or asynchronously. | 81 // The method can stop the thread synchronously or asynchronously. |
79 // In the latter case, the thread will still be running after Stop() | 82 // In the latter case, the thread will still be running after Stop() |
(...skipping 11 matching lines...) Expand all Loading... | |
91 private: | 94 private: |
92 // Our own private SimpleThread override. We implement this in a | 95 // Our own private SimpleThread override. We implement this in a |
93 // private class so that we get the following benefits: | 96 // private class so that we get the following benefits: |
94 // 1) AudioDeviceThread doesn't expose SimpleThread methods. | 97 // 1) AudioDeviceThread doesn't expose SimpleThread methods. |
95 // I.e. the caller can't call Start()/Stop() - which would be bad. | 98 // I.e. the caller can't call Start()/Stop() - which would be bad. |
96 // 2) We override ThreadMain to add additional on-thread initialization | 99 // 2) We override ThreadMain to add additional on-thread initialization |
97 // while still synchronized with SimpleThread::Start() to provide | 100 // while still synchronized with SimpleThread::Start() to provide |
98 // reliable initialization. | 101 // reliable initialization. |
99 class Thread; | 102 class Thread; |
100 | 103 |
104 // Flag indicating whether need to read index from sync socket. | |
105 bool read_index_; | |
henrika (OOO until Aug 14)
2013/03/04 13:12:51
It feels odd to use index as name in a flag. When
wjia(left Chromium)
2013/03/05 02:40:13
Changed name to need_read_index_ to be self explai
| |
106 | |
101 base::Lock thread_lock_; | 107 base::Lock thread_lock_; |
102 scoped_refptr<AudioDeviceThread::Thread> thread_; | 108 scoped_refptr<AudioDeviceThread::Thread> thread_; |
103 | 109 |
104 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); | 110 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); |
105 }; | 111 }; |
106 | 112 |
107 } // namespace media. | 113 } // namespace media. |
108 | 114 |
109 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 115 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
OLD | NEW |