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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/aligned_memory.h" | 11 #include "base/memory/aligned_memory.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/numerics/safe_conversions.h" |
13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
14 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
15 #include "media/base/audio_bus.h" | 16 #include "media/base/audio_bus.h" |
16 | 17 |
17 using base::PlatformThread; | 18 using base::PlatformThread; |
18 | 19 |
19 namespace media { | 20 namespace media { |
20 | 21 |
21 // The actual worker thread implementation. It's very bare bones and much | 22 // The actual worker thread implementation. It's very bare bones and much |
22 // simpler than SimpleThread (no synchronization in Start, etc) and supports | 23 // simpler than SimpleThread (no synchronization in Start, etc) and supports |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 Run(); | 158 Run(); |
158 | 159 |
159 // Release the reference for the thread. Note that after this, the Thread | 160 // Release the reference for the thread. Note that after this, the Thread |
160 // instance will most likely be deleted. | 161 // instance will most likely be deleted. |
161 Release(); | 162 Release(); |
162 } | 163 } |
163 | 164 |
164 void AudioDeviceThread::Thread::Run() { | 165 void AudioDeviceThread::Thread::Run() { |
165 uint32 buffer_index = 0; | 166 uint32 buffer_index = 0; |
166 while (true) { | 167 while (true) { |
167 int pending_data = 0; | 168 uint32 pending_data = 0; |
168 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data)); | 169 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data)); |
169 if (bytes_read != sizeof(pending_data)) | 170 if (bytes_read != sizeof(pending_data)) |
170 break; | 171 break; |
171 | 172 |
172 { | 173 { |
173 base::AutoLock auto_lock(callback_lock_); | 174 base::AutoLock auto_lock(callback_lock_); |
174 if (callback_) | 175 if (callback_) { |
175 callback_->Process(pending_data); | 176 // TODO(acolwell): Update downstream code to use a uint32. |
| 177 // Under normal operation saturation should never occur here |
| 178 // and even if it does, it would only cause a temporary loss |
| 179 // of A/V sync which is much better than crashing or halting |
| 180 // playback. |
| 181 callback_->Process(base::saturated_cast<uint32>(pending_data)); |
| 182 } |
176 } | 183 } |
177 | 184 |
178 // Let the other end know which buffer we just filled. The buffer index is | 185 // Let the other end know which buffer we just filled. The buffer index is |
179 // used to ensure the other end is getting the buffer it expects. For more | 186 // used to ensure the other end is getting the buffer it expects. For more |
180 // details on how this works see AudioSyncReader::WaitUntilDataIsReady(). | 187 // details on how this works see AudioSyncReader::WaitUntilDataIsReady(). |
181 if (synchronized_buffers_) { | 188 if (synchronized_buffers_) { |
182 ++buffer_index; | 189 ++buffer_index; |
183 size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index)); | 190 size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index)); |
184 if (bytes_sent != sizeof(buffer_index)) | 191 if (bytes_sent != sizeof(buffer_index)) |
185 break; | 192 break; |
(...skipping 24 matching lines...) Expand all Loading... |
210 } | 217 } |
211 | 218 |
212 AudioDeviceThread::Callback::~Callback() {} | 219 AudioDeviceThread::Callback::~Callback() {} |
213 | 220 |
214 void AudioDeviceThread::Callback::InitializeOnAudioThread() { | 221 void AudioDeviceThread::Callback::InitializeOnAudioThread() { |
215 MapSharedMemory(); | 222 MapSharedMemory(); |
216 CHECK(shared_memory_.memory()); | 223 CHECK(shared_memory_.memory()); |
217 } | 224 } |
218 | 225 |
219 } // namespace media. | 226 } // namespace media. |
OLD | NEW |