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..28d61a6279359b9f426b9156939319ad6dae04cd |
--- /dev/null |
+++ b/content/renderer/media/audio_output_client.cc |
@@ -0,0 +1,181 @@ |
+// 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" |
+#include "mojo/public/cpp/system/platform_handle.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+// Mojo connection error handler. |
+void OnConnectionError() { |
+ LOG(ERROR) << "Mojo client diconnected"; |
+} |
+ |
+} // namespace |
+ |
+AudioOutputClient::AudioOutputClient( |
+ ServiceRegistry* service_registry, |
+ const scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner) |
+ : thread_task_runner_(thread_task_runner) { |
+ if (!service_registry || !AudioMessageFilter::Get()) |
+ return; |
+ service_ = new media::mojom::AudioOutputPtr(); |
+ service_registry->ConnectToRemoteService(mojo::GetProxy(service_)); |
+ service_->set_connection_error_handler(base::Bind(&OnConnectionError)); |
+} |
+ |
+AudioOutputClient::~AudioOutputClient() { |
+ if (thread_task_runner_->BelongsToCurrentThread()) |
+ Reset(service_); |
+ else |
+ thread_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&AudioOutputClient::Reset, service_)); |
+} |
+ |
+// static |
+void AudioOutputClient::Reset(media::mojom::AudioOutputPtr* service) { |
+ service->reset(); |
+ delete service; |
+} |
+ |
+void AudioOutputClient::OnStreamError(int stream_id) { |
+ if (!AudioMessageFilter::Get() || !service_->is_bound()) |
+ return; |
+ 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, |
+ const media::mojom::AudioOutput::CreateStreamCallback& callback) { |
+ if (!thread_task_runner_->BelongsToCurrentThread()) { |
+ thread_task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&AudioOutputClient::CreateStream, this, stream_id, |
+ params, callback)); |
+ return; |
+ } |
+ if (service_->is_bound()) { |
+ (*service_)->CreateStream(stream_id, params, callback); |
+ } |
+} |
+ |
+void AudioOutputClient::CreateStreamCallback( |
+ int stream_id, |
+ media::mojom::AudioOutputStreamPtr stream, |
+ mojo::ScopedSharedBufferHandle shared_buffer, |
+ mojo::ScopedHandle socket_descriptor) { |
+ DCHECK(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; |
+ bool read_only; |
+ MojoResult unwrap_shared_memory_result = mojo::UnwrapSharedMemoryHandle( |
+ std::move(shared_buffer), &shared_memory_handle, &length, &read_only); |
+ |
+ if (unwrap_shared_memory_result != MOJO_RESULT_OK) { |
+ DLOG(ERROR) << "Failed to pass shared memory. Closing: " |
+ << unwrap_shared_memory_result; |
+ AudioMessageFilter::Get()->io_task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
+ return; |
+ } |
+ |
+ base::SyncSocket::TransitDescriptor descriptor; |
+ MojoResult unwrap_platform_file_result = |
+ mojo::UnwrapPlatformFile(std::move(socket_descriptor), |
+#if defined(OS_WIN) |
+ &descriptor); |
+#else |
+ &descriptor.fd); |
+#endif |
+ if (unwrap_platform_file_result != MOJO_RESULT_OK) { |
+ DLOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
+ << unwrap_platform_file_result; |
+ AudioMessageFilter::Get()->io_task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
+ return; |
+ } |
+ |
+ 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()->StreamCreated(stream->first, handle, |
+ socket_descriptor, length); |
+} |
+ |
+void AudioOutputClient::CloseStream(int stream_id) { |
+ if (!thread_task_runner_->BelongsToCurrentThread()) { |
+ thread_task_runner_->PostTask( |
+ FROM_HERE, |
+ base::Bind(&AudioOutputClient::CloseStream, this, stream_id)); |
+ return; |
+ } |
+ |
+ DCHECK(thread_task_runner_->BelongsToCurrentThread()); |
+ if (service_->is_bound()) { |
+ if (streams_.find(stream_id) != streams_.end() && |
+ streams_[stream_id].is_bound()) { |
+ streams_[stream_id]->Close(); |
+ streams_.erase(stream_id); |
+ } else if (AudioMessageFilter::Get()) { |
+ AudioMessageFilter::Get()->io_task_runner()->PostTask( |
+ FROM_HERE, base::Bind(&AudioMessageFilter::CloseStream, |
+ AudioMessageFilter::Get(), 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 |