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..be570d63c7e54f4875251e41e7369e8a8c81fc8e |
| --- /dev/null |
| +++ b/media/mojo/services/mojo_audio_output.cc |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2017 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/callback_helpers.h" |
| +#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, |
| + StreamCreatedCallback stream_created_callback, |
| + base::OnceClosure deleter_callback) |
| + : stream_created_callback_(std::move(stream_created_callback)), |
| + deleter_callback_(std::move(deleter_callback)), |
| + binding_(this, std::move(request)), |
| + delegate_(std::move(create_delegate_callback).Run(this)) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(deleter_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::Play() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + delegate_->OnPlayStream(); |
| +} |
| + |
| +void MojoAudioOutput::Pause() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + delegate_->OnPauseStream(); |
| +} |
| + |
| +void MojoAudioOutput::SetVolume(double volume) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (volume < 0 || volume > 1) { |
|
DaleCurtis
2017/03/06 17:56:12
Hmm, should this be a BadMessage instead which kil
Max Morin
2017/03/07 11:23:16
I don't know if we get any bad volumes, I recall b
|
| + LOG(ERROR) << "MojoAudioOutput::SetVolume(" << volume << ") out of range."; |
| + OnError(); |
| + return; |
| + } |
| + delegate_->OnSetVolume(volume); |
| +} |
| + |
| +void MojoAudioOutput::OnStreamCreated( |
| + int stream_id, |
|
DaleCurtis
2017/03/06 17:56:12
Unused? Still necessary?
Max Morin
2017/03/07 11:23:16
It is part of the AudioOutputDelegate::EventHandle
|
| + base::SharedMemory* shared_memory, |
| + base::CancelableSyncSocket* foreign_socket) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(stream_created_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()); |
| + |
| + base::ResetAndReturn(&stream_created_callback_) |
| + .Run(std::move(buffer_handle), std::move(socket_handle)); |
| +} |
| + |
| +void MojoAudioOutput::OnStreamError(int stream_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + OnError(); |
| +} |
| + |
| +void MojoAudioOutput::OnError() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + DCHECK(deleter_callback_); |
| + // Destroys |this|. |
| + std::move(deleter_callback_).Run(); |
| +} |
| + |
| +} // namespace media |