Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: media/audio/audio_device_thread.cc

Issue 1703473002: Make AudioOutputDevice restartable and reinitializable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@new_mixing
Patch Set: Added missing include. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/condition_variable.h"
19 #include "base/threading/platform_thread.h" 20 #include "base/threading/platform_thread.h"
21 #include "base/threading/thread_checker.h"
20 #include "base/threading/thread_restrictions.h" 22 #include "base/threading/thread_restrictions.h"
21 #include "media/base/audio_bus.h" 23 #include "media/base/audio_bus.h"
22 24
23 using base::PlatformThread; 25 using base::PlatformThread;
24 26
25 namespace media { 27 namespace media {
26 28
27 // The actual worker thread implementation. It's very bare bones and much 29 // The actual worker thread implementation. It's very bare bones and much
28 // simpler than SimpleThread (no synchronization in Start, etc) and supports 30 // simpler than SimpleThread (no synchronization in Start, etc) and supports
29 // joining the thread handle asynchronously via a provided message loop even 31 // joining the thread handle asynchronously via a provided message loop even
30 // after the Thread object itself has been deleted. 32 // after the Thread object itself has been deleted.
31 class AudioDeviceThread::Thread 33 class AudioDeviceThread::Thread
32 : public PlatformThread::Delegate, 34 : public PlatformThread::Delegate,
33 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> { 35 public base::RefCountedThreadSafe<AudioDeviceThread::Thread> {
34 public: 36 public:
35 Thread(AudioDeviceThread::Callback* callback, 37 Thread(const char* thread_name, bool synchronized_buffers);
36 base::SyncSocket::Handle socket,
37 const char* thread_name,
38 bool synchronized_buffers);
39 38
39 // Starts the thread which will wait in ThreadMain(). Resume() must then be
40 // called to actually start running it.
tommi (sloooow) - chröme 2016/05/14 11:06:28 Can we rename the method to StartSuspended?
Henrik Grunell 2016/05/16 14:37:35 Good idea. Done.
40 void Start(); 41 void Start();
41 42
42 // Stops the thread. If |loop_for_join| is non-NULL, the function posts 43 // 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 44 // 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 loop_for_join is NULL, then the function waits
45 // synchronously for the thread to terminate. 46 // synchronously for the thread to terminate.
47 // Must be called before destruction.
46 void Stop(base::MessageLoop* loop_for_join); 48 void Stop(base::MessageLoop* loop_for_join);
47 49
50 // Releases the thread from waiting and starts running it with the given
51 // |callback| and |socket|.
52 void Resume(AudioDeviceThread::Callback* callback,
53 base::SyncSocket::Handle socket);
54
55 // Pauses the thread, it will wait in ThreadMain(). Shuts down the
56 // socket. Resume() can then be called to restart it.
57 void Pause();
tommi (sloooow) - chröme 2016/05/14 11:06:28 Suspend() (to match with Resume())
Henrik Grunell 2016/05/16 14:37:35 Also good. Done.
58
48 private: 59 private:
60 enum State { STATE_PAUSED, STATE_PLAYING, STATE_STOPPED };
61
49 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>; 62 friend class base::RefCountedThreadSafe<AudioDeviceThread::Thread>;
63
50 ~Thread() override; 64 ~Thread() override;
51 65
52 // Overrides from PlatformThread::Delegate. 66 // Overrides from PlatformThread::Delegate.
53 void ThreadMain() override; 67 void ThreadMain() override;
54 68
55 // Runs the loop that reads from the socket. 69 // Runs the loop that reads from the socket, called in ThreadMain().
56 void Run(); 70 void Run();
57 71
58 private: 72 // Ensures that Start(), Resume() and Pause() are called on the same thread.
59 base::PlatformThreadHandle thread_; 73 // Stop() can be called on any thread.
60 AudioDeviceThread::Callback* callback_; 74 base::ThreadChecker thread_checker_;
61 base::CancelableSyncSocket socket_; 75
62 base::Lock callback_lock_;
63 const char* thread_name_; 76 const char* thread_name_;
64 const bool synchronized_buffers_; 77 const bool synchronized_buffers_;
65 78
79 base::Lock lock_; // Protects everything below.
80 base::ConditionVariable exit_paused_state_;
81 State state_;
82 AudioDeviceThread::Callback* callback_;
83
84 base::PlatformThreadHandle thread_handle_;
85
86 // Reset in ThreadMain(). Shutdown on client thread.
tommi (sloooow) - chröme 2016/05/14 11:06:27 what does 'client thread' mean? is it the thread_
Henrik Grunell 2016/05/16 14:37:36 It's actually any other thread since Stop() may be
87 std::unique_ptr<base::CancelableSyncSocket> socket_;
88
89 // Set on client thead.
tommi (sloooow) - chröme 2016/05/14 11:06:27 thread
Henrik Grunell 2016/05/16 14:37:35 I removed this comment, I don't think it adds anyt
90 base::SyncSocket::Handle new_socket_handle_;
91
66 DISALLOW_COPY_AND_ASSIGN(Thread); 92 DISALLOW_COPY_AND_ASSIGN(Thread);
67 }; 93 };
68 94
69 // AudioDeviceThread implementation 95 // AudioDeviceThread implementation
96 AudioDeviceThread::AudioDeviceThread() {}
70 97
71 AudioDeviceThread::AudioDeviceThread() { 98 AudioDeviceThread::~AudioDeviceThread() {
99 DCHECK(!thread_.get());
72 } 100 }
73 101
74 AudioDeviceThread::~AudioDeviceThread() { DCHECK(!thread_.get()); } 102 void AudioDeviceThread::Start(const char* thread_name,
75
76 void AudioDeviceThread::Start(AudioDeviceThread::Callback* callback,
77 base::SyncSocket::Handle socket,
78 const char* thread_name,
79 bool synchronized_buffers) { 103 bool synchronized_buffers) {
80 base::AutoLock auto_lock(thread_lock_); 104 base::AutoLock auto_lock(thread_lock_);
tommi (sloooow) - chröme 2016/05/14 11:06:27 are Start()/Stop()/Resume()/Pause() not always cal
Henrik Grunell 2016/05/16 14:37:35 Unfortunately no. AudioInputDevice still calls Sto
81 CHECK(!thread_.get()); 105 CHECK(!thread_.get());
82 thread_ = new AudioDeviceThread::Thread( 106 thread_ = new AudioDeviceThread::Thread(thread_name, synchronized_buffers);
tommi (sloooow) - chröme 2016/05/14 11:06:27 if thread_name and synchronize_buffers will always
Henrik Grunell 2016/05/16 14:37:35 It can be in the ctor. I'd like to not change that
83 callback, socket, thread_name, synchronized_buffers);
84 thread_->Start(); 107 thread_->Start();
85 } 108 }
86 109
87 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) { 110 void AudioDeviceThread::Stop(base::MessageLoop* loop_for_join) {
88 base::AutoLock auto_lock(thread_lock_); 111 base::AutoLock auto_lock(thread_lock_);
89 if (thread_.get()) { 112 if (thread_.get()) {
90 thread_->Stop(loop_for_join); 113 thread_->Stop(loop_for_join);
91 thread_ = NULL; 114 thread_ = nullptr;
92 } 115 }
93 } 116 }
94 117
118 void AudioDeviceThread::Resume(AudioDeviceThread::Callback* callback,
119 base::SyncSocket::Handle socket) {
120 base::AutoLock auto_lock(thread_lock_);
121 if (thread_.get())
122 thread_->Resume(callback, socket);
123 }
124
125 void AudioDeviceThread::Pause() {
126 base::AutoLock auto_lock(thread_lock_);
127 if (thread_.get())
128 thread_->Pause();
129 }
130
95 bool AudioDeviceThread::IsStopped() { 131 bool AudioDeviceThread::IsStopped() {
96 base::AutoLock auto_lock(thread_lock_); 132 base::AutoLock auto_lock(thread_lock_);
97 return !thread_.get(); 133 return !thread_.get();
98 } 134 }
99 135
100 // AudioDeviceThread::Thread implementation 136 // AudioDeviceThread::Thread implementation
101 AudioDeviceThread::Thread::Thread(AudioDeviceThread::Callback* callback, 137 AudioDeviceThread::Thread::Thread(const char* thread_name,
102 base::SyncSocket::Handle socket,
103 const char* thread_name,
104 bool synchronized_buffers) 138 bool synchronized_buffers)
105 : thread_(), 139 : thread_name_(thread_name),
106 callback_(callback), 140 synchronized_buffers_(synchronized_buffers),
107 socket_(socket), 141 exit_paused_state_(&lock_),
108 thread_name_(thread_name), 142 state_(STATE_PAUSED),
109 synchronized_buffers_(synchronized_buffers) { 143 callback_(nullptr),
144 new_socket_handle_(base::CancelableSyncSocket::kInvalidHandle) {
145 thread_checker_.DetachFromThread();
tommi (sloooow) - chröme 2016/05/14 11:06:27 why this detach? From what I can tell, Start() is
Henrik Grunell 2016/05/16 14:37:35 Indeed. removed.
110 } 146 }
111 147
112 AudioDeviceThread::Thread::~Thread() { 148 AudioDeviceThread::Thread::~Thread() {
113 DCHECK(thread_.is_null()); 149 DCHECK(thread_handle_.is_null());
150 DCHECK_EQ(state_, STATE_STOPPED)
151 << "Thread must be stopped before destruction";
114 } 152 }
115 153
116 void AudioDeviceThread::Thread::Start() { 154 void AudioDeviceThread::Thread::Start() {
117 base::AutoLock auto_lock(callback_lock_); 155 DCHECK(thread_checker_.CalledOnValidThread());
118 DCHECK(thread_.is_null()); 156
119 // This reference will be released when the thread exists. 157 base::AutoLock auto_lock(lock_);
158 DCHECK(thread_handle_.is_null()) << "Calling Start() on a started thread?";
159 if (state_ == STATE_STOPPED)
160 return; // Stop has already been executed on another thread.
161
162 // This reference will be released when the thread exits.
120 AddRef(); 163 AddRef();
121 164
122 PlatformThread::CreateWithPriority(0, this, &thread_, 165 PlatformThread::CreateWithPriority(0, this, &thread_handle_,
123 base::ThreadPriority::REALTIME_AUDIO); 166 base::ThreadPriority::REALTIME_AUDIO);
124 CHECK(!thread_.is_null()); 167 CHECK(!thread_handle_.is_null());
125 } 168 }
126 169
127 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) { 170 void AudioDeviceThread::Thread::Stop(base::MessageLoop* loop_for_join) {
128 socket_.Shutdown(); 171 // Note: Stop() can be called at any time, even before Start(), because it's
172 // allowed to be executed on a different thread.
tommi (sloooow) - chröme 2016/05/14 11:06:28 Is that something we can fix/avoid?
Henrik Grunell 2016/05/16 14:37:35 AudioOutputDevice doesn't do this, but AudioInputD
129 173
130 base::PlatformThreadHandle thread = base::PlatformThreadHandle(); 174 base::PlatformThreadHandle thread_handle_to_join =
175 base::PlatformThreadHandle();
176 {
177 base::AutoLock auto_lock(lock_);
178 DCHECK(state_ != STATE_STOPPED) << "Calling Stop() on a stopped thread?";
tommi (sloooow) - chröme 2016/05/14 11:06:28 nit: DCHECK_NE
Henrik Grunell 2016/05/16 14:37:35 Done.
179 callback_ = nullptr;
131 180
132 { // NOLINT 181 // |socket_| is only set/reset under the |lock_|, so it's safe to
133 base::AutoLock auto_lock(callback_lock_); 182 // access under that lock here.
134 callback_ = NULL; 183 if (socket_)
135 std::swap(thread, thread_); 184 socket_->Shutdown();
136 }
137 185
138 if (!thread.is_null()) { 186 // Must be done under lock to avoid race with Start().
187 std::swap(thread_handle_to_join, thread_handle_);
188
189 state_ = STATE_STOPPED;
190 exit_paused_state_.Signal();
191 } // auto_lock(lock_)
192
193 if (!thread_handle_to_join.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, base::Bind(&base::PlatformThread::Join,
141 base::Bind(&base::PlatformThread::Join, thread)); 196 thread_handle_to_join));
142 } else { 197 } else {
143 base::PlatformThread::Join(thread); 198 base::PlatformThread::Join(thread_handle_to_join);
144 } 199 }
145 } 200 }
146 } 201 }
147 202
203 void AudioDeviceThread::Thread::Resume(AudioDeviceThread::Callback* callback,
204 base::SyncSocket::Handle socket_handle) {
205 DCHECK(thread_checker_.CalledOnValidThread());
206
207 base::AutoLock auto_lock(lock_);
208 if (state_ == STATE_STOPPED)
209 return; // Stop is the last operation to be executed.
210
211 DCHECK(state_ == STATE_PAUSED) << "Resume a non-paused thread?";
tommi (sloooow) - chröme 2016/05/14 11:06:28 nit: DCHECK_EQ
Henrik Grunell 2016/05/16 14:37:35 Done.
212
213 DCHECK_EQ(new_socket_handle_, base::CancelableSyncSocket::kInvalidHandle);
214
215 callback_ = callback;
216 new_socket_handle_ = socket_handle;
217
218 state_ = STATE_PLAYING;
219 exit_paused_state_.Signal();
tommi (sloooow) - chröme 2016/05/14 11:06:27 any chance that this may already be signaled and w
Henrik Grunell 2016/05/16 14:37:35 You tell me. :D When we enter wait we have the loc
220 }
221
222 void AudioDeviceThread::Thread::Pause() {
223 DCHECK(thread_checker_.CalledOnValidThread());
224
225 base::AutoLock auto_lock(lock_);
226 if (state_ == STATE_STOPPED)
227 return; // Stop is the last operation to be executed.
228
229 DCHECK(state_ == STATE_PLAYING) << "Pause a paused thread?";
230 callback_ = nullptr;
231
232 // |socket_| is only set/reset under the |lock_|, so it's safe to
233 // access under that lock here.
234 if (socket_)
235 socket_->Shutdown();
tommi (sloooow) - chröme 2016/05/14 11:06:27 nit: would it be worth it to swap the socket to a
Henrik Grunell 2016/05/16 14:37:35 Gut feeling says no. Apart from that, I haven't lo
236 state_ = STATE_PAUSED;
237 }
238
148 void AudioDeviceThread::Thread::ThreadMain() { 239 void AudioDeviceThread::Thread::ThreadMain() {
149 PlatformThread::SetName(thread_name_); 240 PlatformThread::SetName(thread_name_);
150 241
151 // Singleton access is safe from this thread as long as callback is non-NULL. 242 // 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 243 // 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 244 // that might touch singletons and the lifetime of the callback is controlled
154 // by another thread on which singleton access is OK as well. 245 // by another thread on which singleton access is OK as well.
155 base::ThreadRestrictions::SetSingletonAllowed(true); 246 base::ThreadRestrictions::SetSingletonAllowed(true);
156 247
157 { // NOLINT 248 while (true) {
158 base::AutoLock auto_lock(callback_lock_); 249 {
159 if (callback_) 250 base::AutoLock auto_lock(lock_);
160 callback_->InitializeOnAudioThread(); 251 while (state_ == STATE_PAUSED) {
161 } 252 // This releases |lock_| while waiting.
253 exit_paused_state_.Wait();
254 }
162 255
163 Run(); 256 if (state_ == STATE_STOPPED)
257 break;
258
259 // If we are playing but don't have a valid new socket handle, we are here
260 // because the socket receive or send failed for another reason than
261 // Shutdown() being called in Stop() or Pause(). In that case, quit the
262 // thread.
263 if (new_socket_handle_ == base::CancelableSyncSocket::kInvalidHandle) {
264 LOG(WARNING) << "Audio socket failed, exiting audio thread.";
265 break;
266 }
267
268 // Else reinitialize and resume.
269
270 socket_.reset(new base::CancelableSyncSocket(new_socket_handle_));
271
272 if (callback_)
273 callback_->InitializeOnAudioThread();
274
275 new_socket_handle_ = base::CancelableSyncSocket::kInvalidHandle;
276 } // auto_lock(lock_)
277
278 Run();
279 } // while
164 280
165 // Release the reference for the thread. Note that after this, the Thread 281 // Release the reference for the thread. Note that after this, the Thread
166 // instance will most likely be deleted. 282 // instance will most likely be deleted.
167 Release(); 283 Release();
168 } 284 }
169 285
170 void AudioDeviceThread::Thread::Run() { 286 void AudioDeviceThread::Thread::Run() {
171 uint32_t buffer_index = 0; 287 uint32_t buffer_index = 0;
288
172 while (true) { 289 while (true) {
173 uint32_t pending_data = 0; 290 uint32_t pending_data = 0;
174 size_t bytes_read = socket_.Receive(&pending_data, sizeof(pending_data)); 291 // |socket_| is only modified on this thread, so we don't need a lock to
292 // access it here.
293 size_t bytes_read = socket_->Receive(&pending_data, sizeof(pending_data));
294
175 if (bytes_read != sizeof(pending_data)) 295 if (bytes_read != sizeof(pending_data))
176 break; 296 break;
177 297
178 // std::numeric_limits<uint32_t>::max() is a special signal which is 298 // 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 299 // returned after the browser stops the output device in response to a
180 // renderer side request. 300 // renderer side request.
181 // 301 //
182 // Avoid running Process() for the paused signal, we still need to update 302 // Avoid running Process() for the paused signal, we still need to update
183 // the buffer index if |synchronized_buffers_| is true though. 303 // the buffer index if |synchronized_buffers_| is true though.
184 // 304 //
185 // See comments in AudioOutputController::DoPause() for details on why. 305 // See comments in AudioOutputController::DoPause() for details on why.
186 if (pending_data != std::numeric_limits<uint32_t>::max()) { 306 if (pending_data != std::numeric_limits<uint32_t>::max()) {
187 base::AutoLock auto_lock(callback_lock_); 307 // We can't use an atomic pointer for |callback_|, but need to call on it
308 // under the state lock. This is because after clearing it in Pause() or
309 // Stop(), the caller may delete the callback object.
310 base::AutoLock auto_lock(lock_);
188 if (callback_) 311 if (callback_)
189 callback_->Process(pending_data); 312 callback_->Process(pending_data);
190 } 313 }
191 314
192 // The usage of |synchronized_buffers_| differs between input and output 315 // The usage of |synchronized_buffers_| differs between input and output
193 // cases. 316 // cases.
194 // Input: 317 // Input:
195 // Let the other end know that we have read data, so that it can verify 318 // 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 319 // it doesn't overwrite any data before read. The |buffer_index| value is
197 // not used. For more details, see AudioInputSyncWriter::Write(). 320 // not used. For more details, see AudioInputSyncWriter::Write().
198 // Output: 321 // Output:
199 // Let the other end know which buffer we just filled. The |buffer_index| is 322 // 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 323 // used to ensure the other end is getting the buffer it expects. For more
201 // details on how this works see AudioSyncReader::WaitUntilDataIsReady(). 324 // details on how this works see AudioSyncReader::WaitUntilDataIsReady().
202 if (synchronized_buffers_) { 325 if (synchronized_buffers_) {
203 ++buffer_index; 326 ++buffer_index;
204 size_t bytes_sent = socket_.Send(&buffer_index, sizeof(buffer_index)); 327 size_t bytes_sent = socket_->Send(&buffer_index, sizeof(buffer_index));
328
205 if (bytes_sent != sizeof(buffer_index)) 329 if (bytes_sent != sizeof(buffer_index))
206 break; 330 break;
207 } 331 }
208 } 332 } // while
209 } 333 }
210 334
211 // AudioDeviceThread::Callback implementation 335 // AudioDeviceThread::Callback implementation
212 336
213 AudioDeviceThread::Callback::Callback(const AudioParameters& audio_parameters, 337 AudioDeviceThread::Callback::Callback(const AudioParameters& audio_parameters,
214 base::SharedMemoryHandle memory, 338 base::SharedMemoryHandle memory,
215 int memory_length, 339 int memory_length,
216 int total_segments) 340 int total_segments)
217 : audio_parameters_(audio_parameters), 341 : audio_parameters_(audio_parameters),
218 samples_per_ms_(static_cast<double>(audio_parameters.sample_rate()) / 342 samples_per_ms_(static_cast<double>(audio_parameters.sample_rate()) /
(...skipping 14 matching lines...) Expand all
233 } 357 }
234 358
235 AudioDeviceThread::Callback::~Callback() {} 359 AudioDeviceThread::Callback::~Callback() {}
236 360
237 void AudioDeviceThread::Callback::InitializeOnAudioThread() { 361 void AudioDeviceThread::Callback::InitializeOnAudioThread() {
238 MapSharedMemory(); 362 MapSharedMemory();
239 CHECK(shared_memory_.memory()); 363 CHECK(shared_memory_.memory());
240 } 364 }
241 365
242 } // namespace media. 366 } // namespace media.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698