Chromium Code Reviews| 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> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <limits> | 11 #include <limits> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/aligned_memory.h" | 16 #include "base/memory/aligned_memory.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/synchronization/waitable_event.h" | |
| 19 #include "base/threading/platform_thread.h" | 20 #include "base/threading/platform_thread.h" |
| 20 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
| 21 #include "media/base/audio_bus.h" | 22 #include "media/base/audio_bus.h" |
| 22 | 23 |
| 23 using base::PlatformThread; | 24 using base::PlatformThread; |
| 24 | 25 |
| 25 namespace media { | 26 namespace media { |
| 26 | 27 |
| 27 // The actual worker thread implementation. It's very bare bones and much | 28 // The actual worker thread implementation. It's very bare bones and much |
| 28 // simpler than SimpleThread (no synchronization in Start, etc) and supports | 29 // simpler than SimpleThread (no synchronization in Start, etc) and supports |
| 29 // joining the thread handle asynchronously via a provided message loop even | 30 // joining the thread handle asynchronously via a provided message loop even |
| 30 // after the Thread object itself has been deleted. | 31 // after the Thread object itself has been deleted. |
| 31 class AudioDeviceThread::Thread | 32 class AudioDeviceThread::Thread |
| 32 : public PlatformThread::Delegate, | 33 : public PlatformThread::Delegate, |
| 33 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> { | 34 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> { |
| 34 public: | 35 public: |
| 35 Thread(AudioDeviceThread::Callback* callback, | 36 Thread(const char* thread_name, bool synchronized_buffers); |
| 36 base::SyncSocket::Handle socket, | |
| 37 const char* thread_name, | |
| 38 bool synchronized_buffers); | |
| 39 | 37 |
| 38 // Starts the thread which will wait in ThreadMain(). StartRun() must then be | |
| 39 // called to actually start running it. | |
| 40 void Start(); | 40 void Start(); |
| 41 | 41 |
| 42 // Stops the thread. If |loop_for_join| is non-NULL, the function posts | 42 // Shuts down the thread. If |loop_for_join| is non-NULL, the function posts |
| 43 // a task to join (close) the thread handle later instead of waiting for | 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 | 44 // the thread. If loop_for_join is NULL, then the function waits |
| 45 // synchronously for the thread to terminate. | 45 // synchronously for the thread to terminate. |
| 46 void Stop(base::MessageLoop* loop_for_join); | 46 void Stop(base::MessageLoop* loop_for_join); |
| 47 | 47 |
| 48 // Releases the thread from waiting and starts running it with the given | |
| 49 // |callback| and |socket|. | |
| 50 void StartRun(AudioDeviceThread::Callback* callback, | |
| 51 base::SyncSocket::Handle socket); | |
| 52 | |
| 53 // Stops running the thread, it will wait in ThreadMain(). Shuts down the | |
| 54 // socket. StartRun() can then be called to restart it. | |
| 55 void StopRun(); | |
| 56 | |
| 48 private: | 57 private: |
| 49 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; | 58 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; |
| 59 | |
| 50 ~Thread() override; | 60 ~Thread() override; |
| 51 | 61 |
| 52 // Overrides from PlatformThread::Delegate. | 62 // Overrides from PlatformThread::Delegate. |
| 53 void ThreadMain() override; | 63 void ThreadMain() override; |
| 54 | 64 |
| 55 // Runs the loop that reads from the socket. | 65 // Runs the loop that reads from the socket. |
| 56 void Run(); | 66 void Run(); |
| 57 | 67 |
| 58 private: | 68 // Waits for |event_| to be signaled if in IDLE state. Returns true if it did |
| 69 // wait, otherwise false. | |
| 70 // Called on the audio thread. | |
| 71 bool MaybeWait(); | |
| 72 | |
| 73 // Initializes |socket_| and tells |callback_| to initialize itself. | |
| 74 // Called on the audio thread. | |
| 75 void Initialize(); | |
| 76 | |
| 77 enum State { | |
| 78 IDLE, | |
| 79 RUNNING, | |
| 80 STOPPING | |
| 81 }; | |
| 82 | |
| 59 base::PlatformThreadHandle thread_; | 83 base::PlatformThreadHandle thread_; |
| 60 AudioDeviceThread::Callback* callback_; | 84 AudioDeviceThread::Callback* callback_; |
| 61 base::CancelableSyncSocket socket_; | 85 scoped_ptr<base::CancelableSyncSocket> socket_; |
| 62 base::Lock callback_lock_; | 86 base::SyncSocket::Handle new_socket_handle_; |
| 87 State state_; | |
| 88 base::WaitableEvent event_; | |
|
o1ka
2016/02/17 09:46:29
Can we name it more descriptive, like 'wait_for_co
| |
| 63 const char* thread_name_; | 89 const char* thread_name_; |
| 64 const bool synchronized_buffers_; | 90 const bool synchronized_buffers_; |
| 91 // TODO: Rename. | |
| 92 base::Lock callback_lock_; // Protects |callback_| and |new_socket_|. | |
| 93 base::Lock state_lock_; // Protects |state_|. | |
| 65 | 94 |
| 66 DISALLOW_COPY_AND_ASSIGN(Thread); | 95 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 67 }; | 96 }; |
| 68 | 97 |
| 69 // AudioDeviceThread implementation | 98 // AudioDeviceThread implementation |
| 70 | 99 |
| 71 AudioDeviceThread::AudioDeviceThread() { | 100 AudioDeviceThread::AudioDeviceThread() { |
| 72 } | 101 } |
| 73 | 102 |
| 74 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); } | 103 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); } |
| 75 | 104 |
| 76 void AudioDeviceThread::Start(AudioDeviceThread::Callback* callback, | 105 void AudioDeviceThread::Start(const char* thread_name, |
| 77 base::SyncSocket::Handle socket, | |
| 78 const char* thread_name, | |
| 79 bool synchronized_buffers) { | 106 bool synchronized_buffers) { |
| 107 printf("*********** Starting audio device thread\n"); | |
| 80 base::AutoLock auto_lock(thread_lock_); | 108 base::AutoLock auto_lock(thread_lock_); |
| 81 CHECK(!thread_.get()); | 109 CHECK(!thread_.get()); |
| 82 thread_ = new AudioDeviceThread::Thread( | 110 thread_ = new AudioDeviceThread::Thread( |
| 83 callback, socket, thread_name, synchronized_buffers); | 111 thread_name, synchronized_buffers); |
| 84 thread_->Start(); | 112 thread_->Start(); |
| 85 } | 113 } |
| 86 | 114 |
| 87 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) { | 115 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) { |
| 88 base::AutoLock auto_lock(thread_lock_); | 116 base::AutoLock auto_lock(thread_lock_); |
| 89 if (thread_.get()) { | 117 if (thread_.get()) { |
| 90 thread_->Stop(loop_for_join); | 118 thread_->Stop(loop_for_join); |
| 91 thread_ = NULL; | 119 thread_ = NULL; |
| 92 } | 120 } |
| 93 } | 121 } |
| 94 | 122 |
| 123 void AudioDeviceThread::StartRun(AudioDeviceThread::Callback* callback, | |
| 124 base::SyncSocket::Handle socket) { | |
| 125 base::AutoLock auto_lock(thread_lock_); | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
This might be off topic but...
can we make it so
Henrik Grunell
2016/02/16 12:47:19
:) Yeah, I can add a thread checker instead, let's
| |
| 126 CHECK(thread_.get()); | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
nit: no need for these sort of checks since we der
Henrik Grunell
2016/02/16 12:47:19
Ineed. Done.
| |
| 127 thread_->StartRun(callback, socket); | |
| 128 } | |
| 129 | |
| 130 void AudioDeviceThread::StopRun() { | |
| 131 base::AutoLock auto_lock(thread_lock_); | |
| 132 // TODO: Allow calling if not started? | |
| 133 // CHECK(thread_.get()); | |
| 134 if (thread_.get()) | |
| 135 thread_->StopRun(); | |
| 136 } | |
| 137 | |
| 95 bool AudioDeviceThread::IsStopped() { | 138 bool AudioDeviceThread::IsStopped() { |
| 96 base::AutoLock auto_lock(thread_lock_); | 139 base::AutoLock auto_lock(thread_lock_); |
| 97 return !thread_.get(); | 140 return !thread_.get(); |
| 98 } | 141 } |
| 99 | 142 |
| 100 // AudioDeviceThread::Thread implementation | 143 // AudioDeviceThread::Thread implementation |
| 101 AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback, | 144 AudioDeviceThread::Thread::Thread(const char* thread_name, |
| 102 base::SyncSocket::Handle socket, | |
| 103 const char* thread_name, | |
| 104 bool synchronized_buffers) | 145 bool synchronized_buffers) |
| 105 : thread_(), | 146 : thread_(), |
| 106 callback_(callback), | 147 callback_(nullptr), |
| 107 socket_(socket), | 148 new_socket_handle_(base::CancelableSyncSocket::kInvalidHandle), |
| 149 state_(IDLE), | |
| 150 event_(false, false), | |
| 108 thread_name_(thread_name), | 151 thread_name_(thread_name), |
| 109 synchronized_buffers_(synchronized_buffers) { | 152 synchronized_buffers_(synchronized_buffers) { |
| 110 } | 153 } |
| 111 | 154 |
| 112 AudioDeviceThread::Thread::~Thread() { | 155 AudioDeviceThread::Thread::~Thread() { |
| 113 DCHECK(thread_.is_null()); | 156 DCHECK(thread_.is_null()); |
| 114 } | 157 } |
| 115 | 158 |
| 116 void AudioDeviceThread::Thread::Start() { | 159 void AudioDeviceThread::Thread::Start() { |
| 160 printf("____________ Thread Start. thread = %0lx\n", | |
| 161 reinterpret_cast<size_t*>(&thread_)[0]); | |
| 117 base::AutoLock auto_lock(callback_lock_); | 162 base::AutoLock auto_lock(callback_lock_); |
| 118 DCHECK(thread_.is_null()); | 163 DCHECK(thread_.is_null()); |
| 164 DCHECK_EQ(state_, IDLE); | |
| 119 // This reference will be released when the thread exists. | 165 // This reference will be released when the thread exists. |
| 120 AddRef(); | 166 AddRef(); |
| 121 | 167 |
| 122 PlatformThread::CreateWithPriority(0, this, &thread_, | 168 PlatformThread::CreateWithPriority(0, this, &thread_, |
| 123 base::ThreadPriority::REALTIME_AUDIO); | 169 base::ThreadPriority::REALTIME_AUDIO); |
| 124 CHECK(!thread_.is_null()); | 170 CHECK(!thread_.is_null()); |
| 125 } | 171 } |
| 126 | 172 |
| 127 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) { | 173 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) { |
| 128 socket_.Shutdown(); | 174 printf("____________ Thread Stop. thread = %0lx\n", |
| 175 reinterpret_cast<size_t*>(&thread_)[0]); | |
| 176 { | |
| 177 base::AutoLock auto_lock(state_lock_); | |
| 178 DCHECK(state_ == IDLE || state_ == RUNNING); | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
Can/do we update the state_ always on the same thr
Henrik Grunell
2016/02/16 12:47:19
Yes, if we add a thread checker and are sure it's
| |
| 179 state_ = STOPPING; | |
| 180 } | |
| 181 | |
| 182 socket_->Shutdown(); | |
|
o1ka
2016/02/17 09:46:29
Is it safe to shutdown a socket twice?
| |
| 183 event_.Signal(); | |
| 129 | 184 |
| 130 base::PlatformThreadHandle thread = base::PlatformThreadHandle(); | 185 base::PlatformThreadHandle thread = base::PlatformThreadHandle(); |
| 131 | 186 |
| 132 { // NOLINT | 187 { // NOLINT |
| 133 base::AutoLock auto_lock(callback_lock_); | 188 base::AutoLock auto_lock(callback_lock_); |
| 134 callback_ = NULL; | 189 callback_ = NULL; |
| 135 std::swap(thread, thread_); | 190 std::swap(thread, thread_); |
| 136 } | 191 } |
| 137 | 192 |
| 138 if (!thread.is_null()) { | 193 if (!thread.is_null()) { |
| 139 if (loop_for_join) { | 194 if (loop_for_join) { |
| 140 loop_for_join->PostTask(FROM_HERE, | 195 loop_for_join->PostTask(FROM_HERE, |
| 141 base::Bind(&base::PlatformThread::Join, thread)); | 196 base::Bind(&base::PlatformThread::Join, thread)); |
| 142 } else { | 197 } else { |
| 143 base::PlatformThread::Join(thread); | 198 base::PlatformThread::Join(thread); |
| 144 } | 199 } |
| 145 } | 200 } |
| 146 } | 201 } |
| 147 | 202 |
| 203 void AudioDeviceThread::Thread::StartRun( | |
| 204 AudioDeviceThread::Callback* callback, | |
| 205 base::SyncSocket::Handle socket) { | |
| 206 printf("____________ Thread StartRun. thread = %0lx\n", | |
| 207 reinterpret_cast<size_t*>(&thread_)[0]); | |
| 208 // TODO: Could maybe use one lock for all three variables. | |
| 209 { | |
| 210 base::AutoLock auto_lock(callback_lock_); | |
| 211 callback_ = callback; | |
| 212 new_socket_handle_ = socket; | |
| 213 } | |
| 214 { | |
| 215 base::AutoLock auto_lock(state_lock_); | |
| 216 DCHECK_EQ(state_, IDLE); | |
| 217 state_ = RUNNING; | |
| 218 } | |
| 219 event_.Signal(); | |
| 220 } | |
| 221 | |
| 222 void AudioDeviceThread::Thread::StopRun() { | |
| 223 printf("____________ Thread StopRun. thread = %0lx\n", | |
| 224 reinterpret_cast<size_t*>(&thread_)[0]); | |
| 225 { | |
| 226 base::AutoLock auto_lock(state_lock_); | |
| 227 // TODO: Remove if we don't allow StopRun when not running. Should likely | |
| 228 // require a IsRunning() function, since AOD is pretty messed up. | |
| 229 if (state_ == IDLE) | |
| 230 return; | |
| 231 DCHECK_EQ(state_, RUNNING); | |
| 232 state_ = IDLE; | |
| 233 } | |
| 234 { | |
| 235 base::AutoLock auto_lock(callback_lock_); | |
| 236 callback_ = nullptr; | |
| 237 } | |
| 238 socket_->Shutdown(); | |
| 239 } | |
| 240 | |
| 148 void AudioDeviceThread::Thread::ThreadMain() { | 241 void AudioDeviceThread::Thread::ThreadMain() { |
| 149 PlatformThread::SetName(thread_name_); | 242 PlatformThread::SetName(thread_name_); |
| 150 | 243 |
| 151 // Singleton access is safe from this thread as long as callback is non-NULL. | 244 // 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 | 245 // 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 | 246 // that might touch singletons and the lifetime of the callback is controlled |
| 154 // by another thread on which singleton access is OK as well. | 247 // by another thread on which singleton access is OK as well. |
| 155 base::ThreadRestrictions::SetSingletonAllowed(true); | 248 base::ThreadRestrictions::SetSingletonAllowed(true); |
| 156 | 249 |
| 157 { // NOLINT | 250 MaybeWait(); |
| 158 base::AutoLock auto_lock(callback_lock_); | 251 // TODO: Handle case when Stop() is called before we get here. |
|
o1ka
2016/02/17 09:46:29
For this you can probably just place if(MaybeWait(
| |
| 159 if (callback_) | 252 Initialize(); |
| 160 callback_->InitializeOnAudioThread(); | 253 |
| 254 static_assert(sizeof(size_t) == sizeof(pthread_t), "This won't work"); | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
fyi - pthread_t doesn't exist on some platforms an
Henrik Grunell
2016/02/16 12:47:19
Yeah, this was just for verifying my assumption in
| |
| 255 printf("____________ Thread Run start. thread = %0lx\n", | |
| 256 reinterpret_cast<size_t*>(&thread_)[0]); | |
| 257 | |
| 258 while (true) { | |
| 259 Run(); | |
| 260 | |
| 261 // If StopRun() was called, we'll wait until signalled by either StartRun() | |
| 262 // or Stop(). | |
| 263 if (MaybeWait()) { | |
| 264 { | |
| 265 // We must check if we are stopping, it may have been called while | |
| 266 // waiting. | |
| 267 base::AutoLock auto_lock(state_lock_); | |
| 268 if (state_ == STOPPING) | |
| 269 break; | |
| 270 } | |
| 271 Initialize(); | |
| 272 } else { | |
| 273 // This happens if Stop() was called while running or if the socket | |
| 274 // operation failed. | |
| 275 break; | |
| 276 } | |
| 161 } | 277 } |
| 162 | 278 |
| 163 Run(); | 279 printf("____________ Thread Run end. thread = %0lx\n", |
| 280 reinterpret_cast<size_t*>(&thread_)[0]); | |
| 164 | 281 |
| 165 // Release the reference for the thread. Note that after this, the Thread | 282 // Release the reference for the thread. Note that after this, the Thread |
| 166 // instance will most likely be deleted. | 283 // instance will most likely be deleted. |
| 167 Release(); | 284 Release(); |
| 168 } | 285 } |
| 169 | 286 |
| 170 void AudioDeviceThread::Thread::Run() { | 287 void AudioDeviceThread::Thread::Run() { |
| 171 uint32_t buffer_index = 0; | 288 uint32_t buffer_index = 0; |
| 289 | |
| 172 while (true) { | 290 while (true) { |
| 291 // printf("____________ Thread Run. thread = %0lx, socket handle = %d\n", | |
| 292 // reinterpret_cast<size_t*>(&thread_)[0], socket_->handle()); | |
| 173 uint32_t pending_data = 0; | 293 uint32_t pending_data = 0; |
| 174 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data)); | 294 size_t bytes_read = socket_->Receive(&pending_data, sizeof(pending_data)); |
| 175 if (bytes_read != sizeof(pending_data)) | 295 |
| 296 if (bytes_read != sizeof(pending_data)) { | |
| 297 printf("____________ Thread Run receive failed. thread = %0lx, socket hand le = %d\n", | |
| 298 reinterpret_cast<size_t*>(&thread_)[0], socket_->handle()); | |
| 176 break; | 299 break; |
| 300 } | |
| 177 | 301 |
| 178 // std::numeric_limits<uint32_t>::max() is a special signal which is | 302 // 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 | 303 // returned after the browser stops the output device in response to a |
| 180 // renderer side request. | 304 // renderer side request. |
| 181 // | 305 // |
| 182 // Avoid running Process() for the paused signal, we still need to update | 306 // Avoid running Process() for the paused signal, we still need to update |
| 183 // the buffer index if |synchronized_buffers_| is true though. | 307 // the buffer index if |synchronized_buffers_| is true though. |
| 184 // | 308 // |
| 185 // See comments in AudioOutputController::DoPause() for details on why. | 309 // See comments in AudioOutputController::DoPause() for details on why. |
| 186 if (pending_data != std::numeric_limits<uint32_t>::max()) { | 310 if (pending_data != std::numeric_limits<uint32_t>::max()) { |
| 187 base::AutoLock auto_lock(callback_lock_); | 311 base::AutoLock auto_lock(callback_lock_); |
| 188 if (callback_) | 312 if (callback_) |
| 313 // printf("____________ Thread Run callback. thread = %0lx, socket handle = %d\n", | |
| 314 // reinterpret_cast<size_t*>(&thread_)[0], socket_->handle()); | |
| 189 callback_->Process(pending_data); | 315 callback_->Process(pending_data); |
| 190 } | 316 } |
| 191 | 317 |
| 192 // The usage of |synchronized_buffers_| differs between input and output | 318 // The usage of |synchronized_buffers_| differs between input and output |
| 193 // cases. | 319 // cases. |
| 194 // Input: | 320 // Input: |
| 195 // Let the other end know that we have read data, so that it can verify | 321 // Let the other end know that we have read data, so that it can verify |
| 196 // it doesn't overwrite any data before read. The |buffer_index| value is | 322 // it doesn't overwrite any data before read. The |buffer_index| value is |
| 197 // not used. For more details, see AudioInputSyncWriter::Write(). | 323 // not used. For more details, see AudioInputSyncWriter::Write(). |
| 198 // Output: | 324 // Output: |
| 199 // Let the other end know which buffer we just filled. The |buffer_index| is | 325 // Let the other end know which buffer we just filled. The |buffer_index| is |
| 200 // used to ensure the other end is getting the buffer it expects. For more | 326 // used to ensure the other end is getting the buffer it expects. For more |
| 201 // details on how this works see AudioSyncReader::WaitUntilDataIsReady(). | 327 // details on how this works see AudioSyncReader::WaitUntilDataIsReady(). |
| 202 if (synchronized_buffers_) { | 328 if (synchronized_buffers_) { |
| 203 ++buffer_index; | 329 ++buffer_index; |
| 204 size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index)); | 330 size_t bytes_sent = socket_->Send(&buffer_index, sizeof(buffer_index)); |
| 205 if (bytes_sent != sizeof(buffer_index)) | 331 |
| 332 if (bytes_sent != sizeof(buffer_index)) { | |
| 333 printf("____________ Thread Run send failed. thread = %0lx, socket handl e = %d\n", | |
| 334 reinterpret_cast<size_t*>(&thread_)[0], socket_->handle()); | |
| 206 break; | 335 break; |
| 336 } | |
| 207 } | 337 } |
| 208 } | 338 } |
| 209 } | 339 } |
| 210 | 340 |
| 341 bool AudioDeviceThread::Thread::MaybeWait() { | |
|
o1ka
2016/02/17 09:46:29
I would call it "Continue" or "Resume" or "SyncAnd
| |
| 342 int socket_handle = socket_ ? socket_->handle() | |
| 343 : base::CancelableSyncSocket::kInvalidHandle; | |
| 344 printf("____________ Thread Maybe wait. thread = %0lx, socket handle = %d\n", | |
| 345 reinterpret_cast<size_t*>(&thread_)[0], socket_handle); | |
| 346 | |
| 347 State state; | |
| 348 { | |
| 349 base::AutoLock auto_lock(state_lock_); | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
this can be a wait too
Henrik Grunell
2016/02/16 12:47:19
Wait for the lock you mean?
o1ka
2016/02/17 09:46:29
If state modifications are done by this thread onl
| |
| 350 state = state_; | |
| 351 } | |
| 352 | |
| 353 if (state == IDLE) { | |
| 354 printf("____________ Thread waiting. thread = %0lx, socket handle = %d\n", | |
| 355 reinterpret_cast<size_t*>(&thread_)[0], socket_handle); | |
| 356 event_.Wait(); | |
| 357 printf("____________ Thread waiting done. thread = %0lx, socket handle = %d\ n", | |
| 358 reinterpret_cast<size_t*>(&thread_)[0], socket_handle); | |
| 359 return true; | |
| 360 } | |
| 361 | |
| 362 // Make sure the event is reset when returning. This is necessary when for | |
| 363 // example StartRun is called before ThreadMain() has started, in which case | |
| 364 // |state_| == RUNNING and the event is signaled. | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
This reads like a bug to me actually. The event s
Henrik Grunell
2016/02/16 12:47:19
Yes, that's what I want to do, but the case when t
| |
| 365 // TODO: What if context switch right before the below line and then event | |
| 366 // is signaled on the other thread. That needs to be fixed. Can maybe the | |
| 367 // state lock be held when waiting? | |
| 368 // Ideally remove |state_| and only use the event. The problem then is that | |
| 369 // we'll wait on the event if the socket receive or sen fails. At least don't | |
| 370 // reset on this thread. | |
| 371 event_.Reset(); | |
| 372 return false; | |
| 373 } | |
| 374 | |
| 375 void AudioDeviceThread::Thread::Initialize() { | |
| 376 int socket_handle = socket_ ? socket_->handle() | |
| 377 : base::CancelableSyncSocket::kInvalidHandle; | |
| 378 printf("____________ Thread init. thread = %0lx, socket handle = %d\n", | |
| 379 reinterpret_cast<size_t*>(&thread_)[0], socket_handle); | |
| 380 base::AutoLock auto_lock(callback_lock_); | |
|
tommi (sloooow) - chröme
2016/02/16 11:25:29
any chance we can get away with not using this loc
Henrik Grunell
2016/02/16 12:47:19
|callback_| needs protection. (And |new_socket_han
| |
| 381 DCHECK_NE(new_socket_handle_, base::CancelableSyncSocket::kInvalidHandle); | |
| 382 socket_.reset( | |
| 383 new base::CancelableSyncSocket(new_socket_handle_)); | |
| 384 new_socket_handle_ = base::CancelableSyncSocket::kInvalidHandle; | |
| 385 // TODO: We should bail out earlier (i.e. not initialize |socket_|) if no | |
| 386 // |callback_| | |
| 387 if (callback_) | |
| 388 callback_->InitializeOnAudioThread(); | |
| 389 } | |
| 390 | |
| 211 // AudioDeviceThread::Callback implementation | 391 // AudioDeviceThread::Callback implementation |
| 212 | 392 |
| 213 AudioDeviceThread::Callback::Callback( | 393 AudioDeviceThread::Callback::Callback( |
| 214 const AudioParameters& audio_parameters, | 394 const AudioParameters& audio_parameters, |
| 215 base::SharedMemoryHandle memory, | 395 base::SharedMemoryHandle memory, |
| 216 int memory_length, | 396 int memory_length, |
| 217 int total_segments) | 397 int total_segments) |
| 218 : audio_parameters_(audio_parameters), | 398 : audio_parameters_(audio_parameters), |
| 219 samples_per_ms_(audio_parameters.sample_rate() / 1000), | 399 samples_per_ms_(audio_parameters.sample_rate() / 1000), |
| 220 bytes_per_ms_(audio_parameters.channels() * | 400 bytes_per_ms_(audio_parameters.channels() * |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 231 } | 411 } |
| 232 | 412 |
| 233 AudioDeviceThread::Callback::~Callback() {} | 413 AudioDeviceThread::Callback::~Callback() {} |
| 234 | 414 |
| 235 void AudioDeviceThread::Callback::InitializeOnAudioThread() { | 415 void AudioDeviceThread::Callback::InitializeOnAudioThread() { |
| 236 MapSharedMemory(); | 416 MapSharedMemory(); |
| 237 CHECK(shared_memory_.memory()); | 417 CHECK(shared_memory_.memory()); |
| 238 } | 418 } |
| 239 | 419 |
| 240 } // namespace media. | 420 } // namespace media. |
| OLD | NEW |