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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/shared_memory.h" | 11 #include "base/memory/shared_memory.h" |
13 #include "base/sync_socket.h" | 12 #include "base/sync_socket.h" |
14 #include "base/synchronization/lock.h" | 13 #include "base/threading/platform_thread.h" |
15 #include "media/base/audio_parameters.h" | 14 #include "media/base/audio_parameters.h" |
16 #include "media/base/media_export.h" | 15 #include "media/base/media_export.h" |
17 | 16 |
18 namespace base { | |
19 class MessageLoop; | |
20 } | |
21 | |
22 namespace media { | 17 namespace media { |
23 class AudioBus; | |
24 | 18 |
25 // Data transfer between browser and render process uses a combination | 19 // Data transfer between browser and render process uses a combination |
26 // of sync sockets and shared memory. To read from the socket and render | 20 // of sync sockets and shared memory. To read from the socket and render |
27 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads | 21 // data, we use a worker thread, a.k.a. the AudioDeviceThread, which reads |
28 // data from the browser via the socket and fills the shared memory from the | 22 // data from the browser via the socket and fills the shared memory from the |
29 // audio thread via the AudioDeviceThread::Callback interface/class. | 23 // audio thread via the AudioDeviceThread::Callback interface/class. |
30 // For more details see the documentation in audio_device.h. | 24 class MEDIA_EXPORT AudioDeviceThread : public base::PlatformThread::Delegate { |
31 // | |
32 // TODO(tommi): Multiple audio input/output device instances should be able to | |
33 // share the same thread instead of spinning one per instance. | |
34 class MEDIA_EXPORT AudioDeviceThread { | |
35 public: | 25 public: |
36 // This is the callback interface/base class that Audio[Output|Input]Device | 26 // This is the callback interface/base class that Audio[Output|Input]Device |
37 // implements to render input/output data. The callbacks run on the | 27 // implements to render input/output data. The callbacks run on the |
38 // thread owned by AudioDeviceThread. | 28 // thread owned by AudioDeviceThread. |
39 class Callback { | 29 class Callback { |
40 public: | 30 public: |
41 Callback(const AudioParameters& audio_parameters, | 31 Callback(const AudioParameters& audio_parameters, |
42 base::SharedMemoryHandle memory, | 32 base::SharedMemoryHandle memory, |
43 int memory_length, | 33 int memory_length, |
44 int total_segments); | 34 int total_segments); |
45 virtual ~Callback(); | |
46 | 35 |
47 // One time initialization for the callback object on the audio thread. | 36 // One time initialization for the callback object on the audio thread. |
48 void InitializeOnAudioThread(); | 37 void InitializeOnAudioThread(); |
49 | 38 |
50 // Derived implementations must call shared_memory_.Map appropriately | 39 // Derived implementations must call shared_memory_.Map appropriately |
51 // before Process can be called. | 40 // before Process can be called. |
52 virtual void MapSharedMemory() = 0; | 41 virtual void MapSharedMemory() = 0; |
53 | 42 |
54 // Called whenever we receive notifications about pending input data. | 43 // Called whenever we receive notifications about pending input data. |
55 virtual void Process(uint32_t pending_data) = 0; | 44 virtual void Process(uint32_t pending_data) = 0; |
56 | 45 |
57 protected: | 46 protected: |
| 47 virtual ~Callback(); |
| 48 |
58 // Protected so that derived classes can access directly. | 49 // Protected so that derived classes can access directly. |
59 // The variables are 'const' since values are calculated/set in the | 50 // The variables are 'const' since values are calculated/set in the |
60 // constructor and must never change. | 51 // constructor and must never change. |
61 const AudioParameters audio_parameters_; | 52 const AudioParameters audio_parameters_; |
62 const double samples_per_ms_; | 53 const double samples_per_ms_; |
63 const double bytes_per_ms_; | 54 const double bytes_per_ms_; |
64 const uint32_t bytes_per_frame_; | 55 const uint32_t bytes_per_frame_; |
65 | 56 |
66 base::SharedMemory shared_memory_; | 57 base::SharedMemory shared_memory_; |
67 const int memory_length_; | 58 const int memory_length_; |
68 const int total_segments_; | 59 const int total_segments_; |
69 int segment_length_; | 60 int segment_length_; |
70 | 61 |
71 private: | 62 private: |
72 DISALLOW_COPY_AND_ASSIGN(Callback); | 63 DISALLOW_COPY_AND_ASSIGN(Callback); |
73 }; | 64 }; |
74 | 65 |
75 AudioDeviceThread(); | 66 // Creates and automatically starts the audio thread. |
76 ~AudioDeviceThread(); | 67 AudioDeviceThread(Callback* callback, |
| 68 base::SyncSocket::Handle socket, |
| 69 const char* thread_name); |
77 | 70 |
78 // Starts the audio thread. The thread must not already be running. If | 71 // This tells the audio thread to stop and clean up the data; this is a |
79 // |sychronized_buffers| is set, the browser expects to be notified via the | 72 // synchronous process and the thread will stop before the method returns. |
80 // |socket| every time AudioDeviceThread::Process() completes. | 73 ~AudioDeviceThread() override; |
81 void Start(AudioDeviceThread::Callback* callback, | |
82 base::SyncSocket::Handle socket, | |
83 const char* thread_name, | |
84 bool synchronized_buffers); | |
85 | |
86 // This tells the audio thread to stop and clean up the data. | |
87 // The method can stop the thread synchronously or asynchronously. | |
88 // In the latter case, the thread will still be running after Stop() | |
89 // returns, but the callback pointer is cleared so no further callbacks will | |
90 // be made (IOW after Stop() returns, it is safe to delete the callback). | |
91 // The |loop_for_join| parameter is required for asynchronous operation | |
92 // in order to join the worker thread and close the thread handle later via a | |
93 // posted task. | |
94 // If set to NULL, function will wait for the thread to exit before returning. | |
95 void Stop(base::MessageLoop* loop_for_join); | |
96 | |
97 // Returns true if the thread is stopped or stopping. | |
98 bool IsStopped(); | |
99 | 74 |
100 private: | 75 private: |
101 // Our own private SimpleThread override. We implement this in a | 76 void ThreadMain() final; |
102 // private class so that we get the following benefits: | |
103 // 1) AudioDeviceThread doesn't expose SimpleThread methods. | |
104 // I.e. the caller can't call Start()/Stop() - which would be bad. | |
105 // 2) We override ThreadMain to add additional on-thread initialization | |
106 // while still synchronized with SimpleThread::Start() to provide | |
107 // reliable initialization. | |
108 class Thread; | |
109 | 77 |
110 base::Lock thread_lock_; | 78 Callback* const callback_; |
111 scoped_refptr<AudioDeviceThread::Thread> thread_; | 79 const char* thread_name_; |
| 80 base::CancelableSyncSocket socket_; |
| 81 base::PlatformThreadHandle thread_handle_; |
112 | 82 |
113 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); | 83 DISALLOW_COPY_AND_ASSIGN(AudioDeviceThread); |
114 }; | 84 }; |
115 | 85 |
116 } // namespace media. | 86 } // namespace media. |
117 | 87 |
118 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ | 88 #endif // MEDIA_AUDIO_AUDIO_DEVICE_THREAD_H_ |
OLD | NEW |