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 #include "media/audio/audio_device_thread.h" | 5 #include "media/audio/audio_device_thread.h" |
6 | 6 |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <algorithm> | |
11 #include <limits> | 7 #include <limits> |
12 | 8 |
13 #include "base/bind.h" | |
14 #include "base/logging.h" | 9 #include "base/logging.h" |
15 #include "base/macros.h" | |
16 #include "base/memory/aligned_memory.h" | |
17 #include "base/message_loop/message_loop.h" | |
18 #include "base/numerics/safe_conversions.h" | |
19 #include "base/threading/platform_thread.h" | |
20 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
21 #include "media/base/audio_bus.h" | |
22 | |
23 using base::PlatformThread; | |
24 | 11 |
25 namespace media { | 12 namespace media { |
26 | 13 |
27 // The actual worker thread implementation. It's very bare bones and much | 14 // AudioDeviceThread::Callback implementation |
28 // simpler than SimpleThread (no synchronization in Start, etc) and supports | |
29 // joining the thread handle asynchronously via a provided message loop even | |
30 // after the Thread object itself has been deleted. | |
31 class AudioDeviceThread::Thread | |
32 : public PlatformThread::Delegate, | |
33 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> { | |
34 public: | |
35 Thread(AudioDeviceThread::Callback* callback, | |
36 base::SyncSocket::Handle socket, | |
37 const char* thread_name, | |
38 bool synchronized_buffers); | |
39 | 15 |
40 void Start(); | 16 AudioDeviceThread::Callback::Callback(const AudioParameters& audio_parameters, |
| 17 base::SharedMemoryHandle memory, |
| 18 int memory_length, |
| 19 int total_segments) |
| 20 : audio_parameters_(audio_parameters), |
| 21 shared_memory_(memory, false), |
| 22 memory_length_(memory_length), |
| 23 total_segments_(total_segments), |
| 24 // Avoid division by zero during construction. |
| 25 segment_length_(memory_length_ / |
| 26 (total_segments_ ? total_segments_ : 1)) { |
| 27 CHECK_GT(total_segments_, 0); |
| 28 CHECK_EQ(memory_length_ % total_segments_, 0); |
| 29 thread_checker_.DetachFromThread(); |
| 30 } |
41 | 31 |
42 // Stops the thread. If |loop_for_join| is non-NULL, the function posts | 32 AudioDeviceThread::Callback::~Callback() {} |
43 // a task to join (close) the thread handle later instead of waiting for | |
44 // the thread. If loop_for_join is NULL, then the function waits | |
45 // synchronously for the thread to terminate. | |
46 void Stop(base::MessageLoop* loop_for_join); | |
47 | 33 |
48 private: | 34 void AudioDeviceThread::Callback::InitializeOnAudioThread() { |
49 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; | 35 // Normally this function is called before the thread checker is used |
50 ~Thread() override; | 36 // elsewhere, but it's not guaranteed. DCHECK to ensure it was not used on |
51 | 37 // another thread before we get here. |
52 // Overrides from PlatformThread::Delegate. | 38 DCHECK(thread_checker_.CalledOnValidThread()) |
53 void ThreadMain() override; | 39 << "Thread checker was attached on the wrong thread"; |
54 | 40 DCHECK(!shared_memory_.memory()); |
55 // Runs the loop that reads from the socket. | 41 MapSharedMemory(); |
56 void Run(); | 42 CHECK(shared_memory_.memory()); |
57 | 43 } |
58 private: | |
59 base::PlatformThreadHandle thread_; | |
60 AudioDeviceThread::Callback* callback_; | |
61 base::CancelableSyncSocket socket_; | |
62 base::Lock callback_lock_; | |
63 const char* thread_name_; | |
64 const bool synchronized_buffers_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(Thread); | |
67 }; | |
68 | 44 |
69 // AudioDeviceThread implementation | 45 // AudioDeviceThread implementation |
70 | 46 |
71 AudioDeviceThread::AudioDeviceThread() { | 47 AudioDeviceThread::AudioDeviceThread(Callback* callback, |
| 48 base::SyncSocket::Handle socket, |
| 49 const char* thread_name) |
| 50 : callback_(callback), thread_name_(thread_name), socket_(socket) { |
| 51 CHECK(base::PlatformThread::CreateWithPriority( |
| 52 0, this, &thread_handle_, base::ThreadPriority::REALTIME_AUDIO)); |
| 53 DCHECK(!thread_handle_.is_null()); |
72 } | 54 } |
73 | 55 |
74 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); } | 56 AudioDeviceThread::~AudioDeviceThread() { |
75 | 57 socket_.Shutdown(); |
76 void AudioDeviceThread::Start(AudioDeviceThread::Callback* callback, | 58 if (thread_handle_.is_null()) |
77 base::SyncSocket::Handle socket, | 59 return; |
78 const char* thread_name, | 60 base::PlatformThread::Join(thread_handle_); |
79 bool synchronized_buffers) { | |
80 base::AutoLock auto_lock(thread_lock_); | |
81 CHECK(!thread_.get()); | |
82 thread_ = new AudioDeviceThread::Thread( | |
83 callback, socket, thread_name, synchronized_buffers); | |
84 thread_->Start(); | |
85 } | 61 } |
86 | 62 |
87 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) { | 63 void AudioDeviceThread::ThreadMain() { |
88 base::AutoLock auto_lock(thread_lock_); | 64 base::PlatformThread::SetName(thread_name_); |
89 if (thread_.get()) { | 65 base::ThreadRestrictions::SetSingletonAllowed(true); |
90 thread_->Stop(loop_for_join); | 66 callback_->InitializeOnAudioThread(); |
91 thread_ = NULL; | |
92 } | |
93 } | |
94 | 67 |
95 bool AudioDeviceThread::IsStopped() { | |
96 base::AutoLock auto_lock(thread_lock_); | |
97 return !thread_.get(); | |
98 } | |
99 | |
100 // AudioDeviceThread::Thread implementation | |
101 AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback, | |
102 base::SyncSocket::Handle socket, | |
103 const char* thread_name, | |
104 bool synchronized_buffers) | |
105 : thread_(), | |
106 callback_(callback), | |
107 socket_(socket), | |
108 thread_name_(thread_name), | |
109 synchronized_buffers_(synchronized_buffers) { | |
110 } | |
111 | |
112 AudioDeviceThread::Thread::~Thread() { | |
113 DCHECK(thread_.is_null()); | |
114 } | |
115 | |
116 void AudioDeviceThread::Thread::Start() { | |
117 base::AutoLock auto_lock(callback_lock_); | |
118 DCHECK(thread_.is_null()); | |
119 // This reference will be released when the thread exists. | |
120 AddRef(); | |
121 | |
122 PlatformThread::CreateWithPriority(0, this, &thread_, | |
123 base::ThreadPriority::REALTIME_AUDIO); | |
124 CHECK(!thread_.is_null()); | |
125 } | |
126 | |
127 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) { | |
128 socket_.Shutdown(); | |
129 | |
130 base::PlatformThreadHandle thread = base::PlatformThreadHandle(); | |
131 | |
132 { // NOLINT | |
133 base::AutoLock auto_lock(callback_lock_); | |
134 callback_ = NULL; | |
135 std::swap(thread, thread_); | |
136 } | |
137 | |
138 if (!thread.is_null()) { | |
139 if (loop_for_join) { | |
140 loop_for_join->PostTask(FROM_HERE, | |
141 base::Bind(&base::PlatformThread::Join, thread)); | |
142 } else { | |
143 base::PlatformThread::Join(thread); | |
144 } | |
145 } | |
146 } | |
147 | |
148 void AudioDeviceThread::Thread::ThreadMain() { | |
149 PlatformThread::SetName(thread_name_); | |
150 | |
151 // Singleton access is safe from this thread as long as callback is non-NULL. | |
152 // The callback is the only point where the thread calls out to 'unknown' code | |
153 // that might touch singletons and the lifetime of the callback is controlled | |
154 // by another thread on which singleton access is OK as well. | |
155 base::ThreadRestrictions::SetSingletonAllowed(true); | |
156 | |
157 { // NOLINT | |
158 base::AutoLock auto_lock(callback_lock_); | |
159 if (callback_) | |
160 callback_->InitializeOnAudioThread(); | |
161 } | |
162 | |
163 Run(); | |
164 | |
165 // Release the reference for the thread. Note that after this, the Thread | |
166 // instance will most likely be deleted. | |
167 Release(); | |
168 } | |
169 | |
170 void AudioDeviceThread::Thread::Run() { | |
171 uint32_t buffer_index = 0; | 68 uint32_t buffer_index = 0; |
172 while (true) { | 69 while (true) { |
173 uint32_t pending_data = 0; | 70 uint32_t pending_data = 0; |
174 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data)); | 71 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data)); |
175 if (bytes_read != sizeof(pending_data)) | 72 if (bytes_read != sizeof(pending_data)) |
176 break; | 73 break; |
177 | 74 |
178 // std::numeric_limits<uint32_t>::max() is a special signal which is | 75 // std::numeric_limits<uint32_t>::max() is a special signal which is |
179 // returned after the browser stops the output device in response to a | 76 // returned after the browser stops the output device in response to a |
180 // renderer side request. | 77 // renderer side request. |
181 // | 78 // |
182 // Avoid running Process() for the paused signal, we still need to update | 79 // Avoid running Process() for the paused signal, we still need to update |
183 // the buffer index if |synchronized_buffers_| is true though. | 80 // the buffer index for synchronized buffers though. |
184 // | 81 // |
185 // See comments in AudioOutputController::DoPause() for details on why. | 82 // See comments in AudioOutputController::DoPause() for details on why. |
186 if (pending_data != std::numeric_limits<uint32_t>::max()) { | 83 if (pending_data != std::numeric_limits<uint32_t>::max()) |
187 base::AutoLock auto_lock(callback_lock_); | 84 callback_->Process(pending_data); |
188 if (callback_) | |
189 callback_->Process(pending_data); | |
190 } | |
191 | 85 |
192 // The usage of |synchronized_buffers_| differs between input and output | 86 // The usage of synchronized buffers differs between input and output cases. |
193 // cases. | 87 // |
194 // Input: | 88 // Input: Let the other end know that we have read data, so that it can |
195 // Let the other end know that we have read data, so that it can verify | 89 // verify it doesn't overwrite any data before read. The |buffer_index| |
196 // it doesn't overwrite any data before read. The |buffer_index| value is | 90 // value is not used. For more details, see AudioInputSyncWriter::Write(). |
197 // not used. For more details, see AudioInputSyncWriter::Write(). | 91 // |
198 // Output: | 92 // Output: Let the other end know which buffer we just filled. The |
199 // Let the other end know which buffer we just filled. The |buffer_index| is | 93 // |buffer_index| is used to ensure the other end is getting the buffer it |
200 // used to ensure the other end is getting the buffer it expects. For more | 94 // expects. For more details on how this works see |
201 // details on how this works see AudioSyncReader::WaitUntilDataIsReady(). | 95 // AudioSyncReader::WaitUntilDataIsReady(). |
202 if (synchronized_buffers_) { | 96 ++buffer_index; |
203 ++buffer_index; | 97 size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index)); |
204 size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index)); | 98 if (bytes_sent != sizeof(buffer_index)) |
205 if (bytes_sent != sizeof(buffer_index)) | 99 break; |
206 break; | |
207 } | |
208 } | 100 } |
209 } | 101 } |
210 | 102 |
211 // AudioDeviceThread::Callback implementation | |
212 | |
213 AudioDeviceThread::Callback::Callback(const AudioParameters& audio_parameters, | |
214 base::SharedMemoryHandle memory, | |
215 int memory_length, | |
216 int total_segments) | |
217 : audio_parameters_(audio_parameters), | |
218 samples_per_ms_(static_cast<double>(audio_parameters.sample_rate()) / | |
219 base::Time::kMillisecondsPerSecond), | |
220 bytes_per_ms_(audio_parameters.channels() * | |
221 (audio_parameters_.bits_per_sample() / 8) * | |
222 samples_per_ms_), | |
223 bytes_per_frame_(audio_parameters.GetBytesPerFrame()), | |
224 shared_memory_(memory, false), | |
225 memory_length_(memory_length), | |
226 total_segments_(total_segments) { | |
227 CHECK_NE(bytes_per_ms_, 0.0); // Catch division by zero early. | |
228 CHECK_NE(samples_per_ms_, 0.0); | |
229 CHECK_NE(bytes_per_frame_, 0u); | |
230 CHECK_GT(total_segments_, 0); | |
231 CHECK_EQ(memory_length_ % total_segments_, 0); | |
232 segment_length_ = memory_length_ / total_segments_; | |
233 thread_checker_.DetachFromThread(); | |
234 } | |
235 | |
236 AudioDeviceThread::Callback::~Callback() {} | |
237 | |
238 void AudioDeviceThread::Callback::InitializeOnAudioThread() { | |
239 // Normally this function is called before the thread checker is used | |
240 // elsewhere, but it's not guaranteed. DCHECK to ensure it was not used on | |
241 // another thread before we get here. | |
242 DCHECK(thread_checker_.CalledOnValidThread()) | |
243 << "Thread checker was attached on the wrong thread"; | |
244 MapSharedMemory(); | |
245 CHECK(shared_memory_.memory()); | |
246 } | |
247 | |
248 } // namespace media. | 103 } // namespace media. |
OLD | NEW |