Chromium Code Reviews| Index: media/mojo/services/mojo_audio_output.cc |
| diff --git a/media/mojo/services/mojo_audio_output.cc b/media/mojo/services/mojo_audio_output.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..243c5a0b3bdf208526b26dfc5bace1408a0c0bc7 |
| --- /dev/null |
| +++ b/media/mojo/services/mojo_audio_output.cc |
| @@ -0,0 +1,124 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/mojo/services/mojo_audio_output.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/memory/shared_memory.h" |
| +#include "base/sync_socket.h" |
| +#include "mojo/public/cpp/system/platform_handle.h" |
| + |
| +namespace media { |
| + |
| +MojoAudioOutput::MojoAudioOutput( |
| + mojom::AudioOutputRequest request, |
| + CreateDelegateCallback create_delegate_callback, |
| + FinishedCallback finished_callback) |
| + : binding_(this, std::move(request)), |
| + create_delegate_callback_(std::move(create_delegate_callback)), |
| + finished_callback_(std::move(finished_callback)) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(finished_callback_); |
| + // |this| owns |binding_|, so unretained is safe. |
| + binding_.set_connection_error_handler( |
| + base::Bind(&MojoAudioOutput::OnError, base::Unretained(this))); |
| +} |
| + |
| +MojoAudioOutput::~MojoAudioOutput() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| +} |
| + |
| +void MojoAudioOutput::Start(const AudioParameters& params, |
| + const StartCallback& callback) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (delegate_) { |
| + // Already started. |
|
o1ka
2017/02/22 13:26:30
Add logging on error in all places like this? Will
Max Morin
2017/03/02 23:11:32
Done.
|
| + OnError(); |
| + return; |
| + } |
| + delegate_ = std::move(create_delegate_callback_).Run(this, params); |
|
DaleCurtis
2017/02/21 18:18:32
Hmm, post std::move the contents of create_delegat
Max Morin
2017/02/22 10:08:43
The C++ standard requires that it is in a "valid b
o1ka
2017/02/22 13:26:30
Here is what I found it unit tests: https://cs.chr
Max Morin
2017/02/22 16:07:44
The comment actually contradicts the test, since t
|
| + DCHECK(delegate_); |
| + start_callback_ = callback; |
| +} |
| + |
| +void MojoAudioOutput::Play() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!delegate_) { |
| + // Not started yet. |
| + OnError(); |
| + return; |
| + } |
| + delegate_->OnPlayStream(); |
| +} |
| + |
| +void MojoAudioOutput::Pause() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!delegate_) { |
| + // Not started yet. |
| + OnError(); |
| + return; |
| + } |
| + delegate_->OnPauseStream(); |
| +} |
| + |
| +void MojoAudioOutput::SetVolume(double volume) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!delegate_ || volume < 0 || volume > 1) { |
| + // Not started yet. |
|
o1ka
2017/02/22 13:26:30
...or volume is out of range
|
| + OnError(); |
| + return; |
| + } |
| + delegate_->OnSetVolume(volume); |
| +} |
| + |
| +void MojoAudioOutput::OnStreamCreated( |
| + int stream_id, |
| + base::SharedMemory* shared_memory, |
| + base::CancelableSyncSocket* foreign_socket) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!binding_.is_bound()) { |
| + // Already closed, make sure we are properly cleaned up. |
| + OnError(); |
| + return; |
| + } |
| + DCHECK(start_callback_); |
| + DCHECK(shared_memory); |
| + DCHECK(foreign_socket); |
| + |
| + base::SharedMemoryHandle foreign_memory_handle = |
| + base::SharedMemory::DuplicateHandle(shared_memory->handle()); |
| + DCHECK(base::SharedMemory::IsHandleValid(foreign_memory_handle)); |
| + |
| + mojo::ScopedSharedBufferHandle buffer_handle = mojo::WrapSharedMemoryHandle( |
| + foreign_memory_handle, shared_memory->requested_size(), false); |
| + mojo::ScopedHandle socket_handle = |
| + mojo::WrapPlatformFile(foreign_socket->handle()); |
| + |
| + DCHECK(buffer_handle.is_valid()); |
| + DCHECK(socket_handle.is_valid()); |
| + |
| + start_callback_.Run(std::move(buffer_handle), std::move(socket_handle)); |
|
DaleCurtis
2017/02/21 18:18:32
base::ResetAndReturn?
Max Morin
2017/02/22 10:08:43
Done.
|
| + start_callback_ = StartCallback(); |
| +} |
| + |
| +void MojoAudioOutput::OnStreamError(int stream_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + OnError(); |
| +} |
| + |
| +void MojoAudioOutput::OnError() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!finished_callback_) { |
|
DaleCurtis
2017/02/21 18:18:32
Hmm, this is relying on finished_callback_ being n
Max Morin
2017/02/22 10:08:43
I'm pretty sure it's null after being moved from,
o1ka
2017/02/22 13:26:30
Hmm... |finished_callback_| acts as a deleter for
Max Morin
2017/03/02 23:11:32
Renamed to deleter_.
|
| + // There is already a destruction in progress. |
| + return; |
| + } |
| + // Notify the renderer. |
| + binding_.Close(); |
| + // The rest of the cleanup is done when |this| is destructed. |
| + std::move(finished_callback_).Run(this); |
| +} |
| + |
| +} // namespace media |