cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / media / audio / audio_device_thread.cc
blobd5c1bbcebdc652b8d0cf1fd7ac222a1458af2ba7
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
3 // found in the LICENSE file.
5 #include "media/audio/audio_device_thread.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/aligned_memory.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/threading/platform_thread.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "media/audio/audio_util.h"
16 #include "media/base/audio_bus.h"
18 using base::PlatformThread;
20 namespace media {
22 // The actual worker thread implementation. It's very bare bones and much
23 // simpler than SimpleThread (no synchronization in Start, etc) and supports
24 // joining the thread handle asynchronously via a provided message loop even
25 // after the Thread object itself has been deleted.
26 class AudioDeviceThread::Thread
27 : public PlatformThread::Delegate,
28 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> {
29 public:
30 Thread(AudioDeviceThread::Callback* callback,
31 base::SyncSocket::Handle socket,
32 const char* thread_name);
34 void Start();
36 // Stops the thread. If |loop_for_join| is non-NULL, the function posts
37 // a task to join (close) the thread handle later instead of waiting for
38 // the thread. If loop_for_join is NULL, then the function waits
39 // synchronously for the thread to terminate.
40 void Stop(base::MessageLoop* loop_for_join);
42 private:
43 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>;
44 virtual ~Thread();
46 // Overrides from PlatformThread::Delegate.
47 virtual void ThreadMain() OVERRIDE;
49 // Runs the loop that reads from the socket.
50 void Run();
52 private:
53 base::PlatformThreadHandle thread_;
54 AudioDeviceThread::Callback* callback_;
55 base::CancelableSyncSocket socket_;
56 base::Lock callback_lock_;
57 const char* thread_name_;
59 DISALLOW_COPY_AND_ASSIGN(Thread);
62 // AudioDeviceThread implementation
64 AudioDeviceThread::AudioDeviceThread() {
67 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); }
69 void AudioDeviceThread::Start(AudioDeviceThread::Callback* callback,
70 base::SyncSocket::Handle socket,
71 const char* thread_name) {
72 base::AutoLock auto_lock(thread_lock_);
73 CHECK(thread_.get() == NULL);
74 thread_ = new AudioDeviceThread::Thread(callback, socket, thread_name);
75 thread_->Start();
78 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) {
79 base::AutoLock auto_lock(thread_lock_);
80 if (thread_.get()) {
81 thread_->Stop(loop_for_join);
82 thread_ = NULL;
86 bool AudioDeviceThread::IsStopped() {
87 base::AutoLock auto_lock(thread_lock_);
88 return thread_.get() == NULL;
91 // AudioDeviceThread::Thread implementation
92 AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback,
93 base::SyncSocket::Handle socket,
94 const char* thread_name)
95 : thread_(),
96 callback_(callback),
97 socket_(socket),
98 thread_name_(thread_name) {
101 AudioDeviceThread::Thread::~Thread() {
102 DCHECK(thread_.is_null());
105 void AudioDeviceThread::Thread::Start() {
106 base::AutoLock auto_lock(callback_lock_);
107 DCHECK(thread_.is_null());
108 // This reference will be released when the thread exists.
109 AddRef();
111 PlatformThread::CreateWithPriority(0, this, &thread_,
112 base::kThreadPriority_RealtimeAudio);
113 CHECK(!thread_.is_null());
116 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) {
117 socket_.Shutdown();
119 base::PlatformThreadHandle thread = base::PlatformThreadHandle();
121 { // NOLINT
122 base::AutoLock auto_lock(callback_lock_);
123 callback_ = NULL;
124 std::swap(thread, thread_);
127 if (!thread.is_null()) {
128 if (loop_for_join) {
129 loop_for_join->PostTask(FROM_HERE,
130 base::Bind(&base::PlatformThread::Join, thread));
131 } else {
132 base::PlatformThread::Join(thread);
137 void AudioDeviceThread::Thread::ThreadMain() {
138 PlatformThread::SetName(thread_name_);
140 // Singleton access is safe from this thread as long as callback is non-NULL.
141 // The callback is the only point where the thread calls out to 'unknown' code
142 // that might touch singletons and the lifetime of the callback is controlled
143 // by another thread on which singleton access is OK as well.
144 base::ThreadRestrictions::SetSingletonAllowed(true);
146 { // NOLINT
147 base::AutoLock auto_lock(callback_lock_);
148 if (callback_)
149 callback_->InitializeOnAudioThread();
152 Run();
154 // Release the reference for the thread. Note that after this, the Thread
155 // instance will most likely be deleted.
156 Release();
159 void AudioDeviceThread::Thread::Run() {
160 while (true) {
161 int pending_data = 0;
162 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data));
163 if (bytes_read != sizeof(pending_data)) {
164 DCHECK_EQ(bytes_read, 0U);
165 break;
168 base::AutoLock auto_lock(callback_lock_);
169 if (callback_)
170 callback_->Process(pending_data);
174 // AudioDeviceThread::Callback implementation
176 AudioDeviceThread::Callback::Callback(
177 const AudioParameters& audio_parameters,
178 base::SharedMemoryHandle memory,
179 int memory_length,
180 int total_segments)
181 : audio_parameters_(audio_parameters),
182 samples_per_ms_(audio_parameters.sample_rate() / 1000),
183 bytes_per_ms_(audio_parameters.channels() *
184 (audio_parameters_.bits_per_sample() / 8) *
185 samples_per_ms_),
186 shared_memory_(memory, false),
187 memory_length_(memory_length),
188 total_segments_(total_segments) {
189 CHECK_NE(bytes_per_ms_, 0); // Catch division by zero early.
190 CHECK_NE(samples_per_ms_, 0);
191 CHECK_GT(total_segments_, 0);
192 CHECK_EQ(memory_length_ % total_segments_, 0);
193 segment_length_ = memory_length_ / total_segments_;
196 AudioDeviceThread::Callback::~Callback() {}
198 void AudioDeviceThread::Callback::InitializeOnAudioThread() {
199 MapSharedMemory();
200 CHECK(shared_memory_.memory());
203 } // namespace media.