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..5ae81796a451422291f76d5c0e02f8cd82d651c6 |
--- /dev/null |
+++ b/media/mojo/services/mojo_audio_output.cc |
@@ -0,0 +1,120 @@ |
+// 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/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, |
+ 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::Initialize(const AudioParameters& params, |
+ const InitializeCallback& callback) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (delegate_) { |
+ // Already initialized. |
+ OnError(); |
+ return; |
+ } |
+ delegate_ = std::move(create_delegate_callback_).Run(this, params); |
+ DCHECK(delegate_); |
+ initialize_callback_ = callback; |
o1ka
2017/02/22 13:26:31
Add a comment that it will be called when OnStream
Max Morin
2017/03/02 23:11:32
Done.
|
+} |
+ |
+void MojoAudioOutput::Play() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (!delegate_) { |
+ // Not initialized yet. |
+ OnError(); |
+ return; |
+ } |
+ delegate_->OnPlayStream(); |
+} |
+ |
+void MojoAudioOutput::Pause() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (!delegate_) { |
+ // Not initialized yet. |
+ OnError(); |
+ return; |
+ } |
+ delegate_->OnPauseStream(); |
+} |
+ |
+void MojoAudioOutput::SetVolume(double volume) { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ if (!delegate_ || volume < 0 || volume > 1) { |
+ // Not initialized yet. |
+ 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(initialize_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(&initialize_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(finished_callback_); |
+ // Destroys |this|. |
+ std::move(finished_callback_).Run(this); |
+} |
+ |
+} // namespace media |