Index: content/renderer/media/audio_output_client.cc |
diff --git a/content/renderer/media/audio_output_client.cc b/content/renderer/media/audio_output_client.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b81ab484efa1e336d86dafdaab0c3cf14eb7690f |
--- /dev/null |
+++ b/content/renderer/media/audio_output_client.cc |
@@ -0,0 +1,160 @@ |
+// 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 "content/renderer/media/audio_output_client.h" |
+ |
+#include <utility> |
+ |
+#include "base/files/file.h" |
+#include "base/message_loop/message_loop.h" |
+#include "base/single_thread_task_runner.h" |
+#include "base/time/time.h" |
+#include "content/common/media/audio_messages.h" |
+#include "content/public/common/service_registry.h" |
+#include "content/renderer/media/audio_message_filter.h" |
+#include "content/renderer/media/webrtc_logging.h" |
+#include "media/base/audio_parameters.h" |
+#include "mojo/edk/embedder/embedder.h" |
+#include "mojo/public/c/system/buffer.h" |
+#include "mojo/public/cpp/bindings/binding.h" |
+#include "mojo/public/cpp/system/handle.h" |
+ |
+namespace content { |
+ |
+namespace { |
+// Mojo connection error handler. |
+void OnConnectionError() { |
+ DLOG(ERROR) << "Mojo client connection error"; |
+} |
+ |
+} // namespace |
+ |
+AudioOutputClient::AudioOutputClient(ServiceRegistry* service_registry) |
+ : main_thread_task_runner_(base::MessageLoop::current()->task_runner()) { |
+ if (service_registry) { |
+ service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
+ service_.set_connection_error_handler(base::Bind(&OnConnectionError)); |
+ } |
+} |
+ |
+AudioOutputClient::~AudioOutputClient() {} |
+ |
+void AudioOutputClient::OnStreamError(int stream_id) { |
+ AudioMessageFilter::Get()->io_task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
+} |
+ |
+void AudioOutputClient::CreateStream(int stream_id, |
+ const media::AudioParameters& params) { |
+ if (!main_thread_task_runner_->BelongsToCurrentThread()) { |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::CreateStream, this, stream_id, params)); |
+ return; |
+ } |
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
+ if (service_.is_bound()) { |
+ service_->CreateStream( |
+ stream_id, params, |
+ base::Bind(&AudioOutputClient::CreateStreamCallback, this)); |
+ } |
+} |
+ |
+void AudioOutputClient::CreateStreamCallback( |
+ int stream_id, |
+ media::mojom::AudioOutputStreamPtr stream, |
+ mojo::ScopedSharedBufferHandle shared_buffer, |
+ mojo::ScopedHandle socket_descriptor) { |
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
+ if (!AudioMessageFilter::Get()) |
+ return; |
+ if (!stream.is_bound()) { |
+ AudioMessageFilter::Get()->io_task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
+ return; |
+ } |
+ |
+ stream.set_connection_error_handler( |
+ base::Bind(&AudioOutputClient::OnStreamError, this, stream_id)); |
+ |
+ base::SharedMemoryHandle shared_memory_handle; |
+ size_t length; |
+ |
+ MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( |
+ shared_buffer.release().value(), &shared_memory_handle, &length, nullptr); |
+ |
+ if (pass_shared_memory_result != MOJO_RESULT_OK) { |
+ DLOG(ERROR) << "Failed to pass shared memory. Closing: " |
+ << pass_shared_memory_result; |
+ return; |
+ } |
+ |
+ mojo::edk::ScopedPlatformHandle platform_handle; |
+ |
+ MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( |
+ socket_descriptor.release().value(), &platform_handle); |
+ |
+ if (pass_platform_handle_result != MOJO_RESULT_OK) { |
+ DLOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
+ << pass_platform_handle_result; |
+ return; |
+ } |
+ |
+ base::SyncSocket::TransitDescriptor descriptor; |
+ |
+#if defined(OS_WIN) |
+ descriptor = platform_handle.release().handle; |
+#else |
+ descriptor.fd = platform_handle.release().handle; |
+#endif |
+ |
+ auto result = streams_.insert(std::make_pair(stream_id, std::move(stream))); |
+ AudioMessageFilter::Get()->io_task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::CreateStreamOnIOThread, this, result.first, |
+ shared_memory_handle, descriptor, length)); |
+} |
+ |
+void AudioOutputClient::CreateStreamOnIOThread( |
+ AudioOutputClient::AudioOutputStreamPtrMap::iterator stream, |
+ base::SharedMemoryHandle handle, |
+ base::SyncSocket::TransitDescriptor socket_descriptor, |
+ uint32_t length) { |
+ if (!AudioMessageFilter::Get()) |
+ return; |
+ DCHECK(AudioMessageFilter::Get()->io_task_runner()->BelongsToCurrentThread()); |
+ AudioMessageFilter::Get()->OnStreamCreated(stream->first, handle, |
+ socket_descriptor, length); |
+} |
+ |
+void AudioOutputClient::CloseStream(int stream_id) { |
+ if (!main_thread_task_runner_->BelongsToCurrentThread()) { |
+ main_thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::CloseStream, this, stream_id)); |
+ return; |
+ } |
+ |
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
+ if (service_.is_bound()) { |
+ if (streams_.find(stream_id) != streams_.end() && |
+ streams_[stream_id].is_bound()) |
+ streams_[stream_id]->Close(); |
+ else if (AudioMessageFilter::Get()) { |
+ AudioMessageFilter::Get()->CloseStream(stream_id); |
+ } |
+ } |
+} |
+ |
+void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { |
+ DCHECK(AudioMessageFilter::Get()->io_task_runner()->BelongsToCurrentThread()); |
+ if (!AudioMessageFilter::Get()) |
+ return; |
+ AudioMessageFilter::Get()->OnStreamStateChanged( |
+ stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); |
+} |
+ |
+} // namespace content |