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 #include <utility> | |
| 12 | 13 |
| 13 #include "base/bind.h" | 14 #include "base/bind.h" |
| 14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/aligned_memory.h" | 17 #include "base/memory/aligned_memory.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/single_thread_task_runner.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 task runner 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(AudioDeviceThread::Callback* callback, |
| 36 base::SyncSocket::Handle socket, | 37 base::SyncSocket::Handle socket, |
| 37 const char* thread_name, | 38 const char* thread_name, |
| 38 bool synchronized_buffers); | 39 bool synchronized_buffers); |
| 39 | 40 |
| 40 void Start(); | 41 void Start(); |
| 41 | 42 |
| 42 // Stops the thread. If |loop_for_join| is non-NULL, the function posts | 43 // Stops the thread. If |task_runner_for_join| is non-NULL, the function |
| 43 // a task to join (close) the thread handle later instead of waiting for | 44 // posts 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 // the thread. If |task_runner_for_join| is NULL, then the function waits |
| 45 // synchronously for the thread to terminate. | 46 // synchronously for the thread to terminate. |
| 46 void Stop(base::MessageLoop* loop_for_join); | 47 void Stop(scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_join); |
|
chcunningham
2016/06/21 01:34:36
This file just underwent some significant cleanup
fdoray
2016/06/29 15:44:31
Done.
| |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; | 50 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; |
| 50 ~Thread() override; | 51 ~Thread() override; |
| 51 | 52 |
| 52 // Overrides from PlatformThread::Delegate. | 53 // Overrides from PlatformThread::Delegate. |
| 53 void ThreadMain() override; | 54 void ThreadMain() override; |
| 54 | 55 |
| 55 // Runs the loop that reads from the socket. | 56 // Runs the loop that reads from the socket. |
| 56 void Run(); | 57 void Run(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 77 base::SyncSocket::Handle socket, | 78 base::SyncSocket::Handle socket, |
| 78 const char* thread_name, | 79 const char* thread_name, |
| 79 bool synchronized_buffers) { | 80 bool synchronized_buffers) { |
| 80 base::AutoLock auto_lock(thread_lock_); | 81 base::AutoLock auto_lock(thread_lock_); |
| 81 CHECK(!thread_.get()); | 82 CHECK(!thread_.get()); |
| 82 thread_ = new AudioDeviceThread::Thread( | 83 thread_ = new AudioDeviceThread::Thread( |
| 83 callback, socket, thread_name, synchronized_buffers); | 84 callback, socket, thread_name, synchronized_buffers); |
| 84 thread_->Start(); | 85 thread_->Start(); |
| 85 } | 86 } |
| 86 | 87 |
| 87 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) { | 88 void AudioDeviceThread::Stop( |
| 89 scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_join) { | |
| 88 base::AutoLock auto_lock(thread_lock_); | 90 base::AutoLock auto_lock(thread_lock_); |
| 89 if (thread_.get()) { | 91 if (thread_.get()) { |
| 90 thread_->Stop(loop_for_join); | 92 thread_->Stop(std::move(task_runner_for_join)); |
| 91 thread_ = NULL; | 93 thread_ = NULL; |
| 92 } | 94 } |
| 93 } | 95 } |
| 94 | 96 |
| 95 bool AudioDeviceThread::IsStopped() { | 97 bool AudioDeviceThread::IsStopped() { |
| 96 base::AutoLock auto_lock(thread_lock_); | 98 base::AutoLock auto_lock(thread_lock_); |
| 97 return !thread_.get(); | 99 return !thread_.get(); |
| 98 } | 100 } |
| 99 | 101 |
| 100 // AudioDeviceThread::Thread implementation | 102 // AudioDeviceThread::Thread implementation |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 117 base::AutoLock auto_lock(callback_lock_); | 119 base::AutoLock auto_lock(callback_lock_); |
| 118 DCHECK(thread_.is_null()); | 120 DCHECK(thread_.is_null()); |
| 119 // This reference will be released when the thread exists. | 121 // This reference will be released when the thread exists. |
| 120 AddRef(); | 122 AddRef(); |
| 121 | 123 |
| 122 PlatformThread::CreateWithPriority(0, this, &thread_, | 124 PlatformThread::CreateWithPriority(0, this, &thread_, |
| 123 base::ThreadPriority::REALTIME_AUDIO); | 125 base::ThreadPriority::REALTIME_AUDIO); |
| 124 CHECK(!thread_.is_null()); | 126 CHECK(!thread_.is_null()); |
| 125 } | 127 } |
| 126 | 128 |
| 127 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) { | 129 void AudioDeviceThread::Thread::Stop( |
| 130 scoped_refptr<base::SingleThreadTaskRunner> task_runner_for_join) { | |
| 128 socket_.Shutdown(); | 131 socket_.Shutdown(); |
| 129 | 132 |
| 130 base::PlatformThreadHandle thread = base::PlatformThreadHandle(); | 133 base::PlatformThreadHandle thread = base::PlatformThreadHandle(); |
| 131 | 134 |
| 132 { // NOLINT | 135 { // NOLINT |
| 133 base::AutoLock auto_lock(callback_lock_); | 136 base::AutoLock auto_lock(callback_lock_); |
| 134 callback_ = NULL; | 137 callback_ = NULL; |
| 135 std::swap(thread, thread_); | 138 std::swap(thread, thread_); |
| 136 } | 139 } |
| 137 | 140 |
| 138 if (!thread.is_null()) { | 141 if (!thread.is_null()) { |
| 139 if (loop_for_join) { | 142 if (task_runner_for_join) { |
| 140 loop_for_join->PostTask(FROM_HERE, | 143 task_runner_for_join->PostTask( |
| 141 base::Bind(&base::PlatformThread::Join, thread)); | 144 FROM_HERE, base::Bind(&base::PlatformThread::Join, thread)); |
| 142 } else { | 145 } else { |
| 143 base::PlatformThread::Join(thread); | 146 base::PlatformThread::Join(thread); |
| 144 } | 147 } |
| 145 } | 148 } |
| 146 } | 149 } |
| 147 | 150 |
| 148 void AudioDeviceThread::Thread::ThreadMain() { | 151 void AudioDeviceThread::Thread::ThreadMain() { |
| 149 PlatformThread::SetName(thread_name_); | 152 PlatformThread::SetName(thread_name_); |
| 150 | 153 |
| 151 // Singleton access is safe from this thread as long as callback is non-NULL. | 154 // Singleton access is safe from this thread as long as callback is non-NULL. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 // Normally this function is called before the thread checker is used | 242 // 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 | 243 // elsewhere, but it's not guaranteed. DCHECK to ensure it was not used on |
| 241 // another thread before we get here. | 244 // another thread before we get here. |
| 242 DCHECK(thread_checker_.CalledOnValidThread()) | 245 DCHECK(thread_checker_.CalledOnValidThread()) |
| 243 << "Thread checker was attached on the wrong thread"; | 246 << "Thread checker was attached on the wrong thread"; |
| 244 MapSharedMemory(); | 247 MapSharedMemory(); |
| 245 CHECK(shared_memory_.memory()); | 248 CHECK(shared_memory_.memory()); |
| 246 } | 249 } |
| 247 | 250 |
| 248 } // namespace media. | 251 } // namespace media. |
| OLD | NEW |