Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(384)

Unified Diff: content/renderer/media/audio_output_client.cc

Issue 1896883002: Mojo interfaces needed for switching audio rendering stream creation and closing from IPC to Mojo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: All grunell comments resolved Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8d1d15025cd5620d229426a416f8d10f83d85bed
--- /dev/null
+++ b/content/renderer/media/audio_output_client.cc
@@ -0,0 +1,173 @@
+// 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/audio/audio_parameters.h"
+#include "media/mojo/common/media_type_converters.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 {
+
+int FindAudioOutputStreamPtrID(
+ const AudioOutputClient::ScopedAudioOutputStreamPtrMap& stream_ids,
+ mojom::AudioOutputStreamPtr* const key) {
+ for (auto& it: stream_ids) {
+ if (it.second.get() == key) {
+ return it.first;
+ }
+ }
+ return -1;
+}
+
+} // namespace
+
+AudioOutputClient::AudioOutputClient(
+ ServiceRegistry* service_registry,
+ scoped_refptr<AudioMessageFilter> audio_message_filter)
+ : audio_message_filter_(audio_message_filter),
+ main_thread_task_runner_(base::MessageLoop::current()->task_runner()) {
+
+ service_registry->ConnectToRemoteService(mojo::GetProxy(&service_));
+ service_.set_connection_error_handler(base::Bind(
+ &AudioOutputClient::OnConnectionError, base::Unretained(this)));
+}
+
+AudioOutputClient::~AudioOutputClient() {
+}
+
+void AudioOutputClient::OnConnectionError() {
+ LOG(ERROR) << "Mojo client connection error";
+}
+
+void AudioOutputClient::OnStreamError(int stream_id) {
+ audio_message_filter_->io_task_runner()->PostTask(
+ FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread,
+ base::Unretained(this), stream_id));
+}
+
+void AudioOutputClient::CreateStream(int stream_id,
+ int render_frame_id,
+ const media::AudioParameters& params) {
+ main_thread_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&AudioOutputClient::CreateStreamOnMainThread,
+ base::Unretained(this), stream_id, render_frame_id, params));
+}
+
+void AudioOutputClient::CreateStreamOnMainThread(
+ int stream_id,
+ int render_frame_id,
+ const media::AudioParameters& params) {
+ media::AudioParameters param;
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
+ service_->CreateStream(
+ stream_id, render_frame_id,
+ mojo::ConvertTo<media::interfaces::AudioOutputStreamParametersPtr>(
+ params),
+ base::Bind(&AudioOutputClient::CreateStreamCallback,
+ base::Unretained(this)));
+}
+
+void AudioOutputClient::CreateStreamCallback(
+ int stream_id,
+ mojom::AudioOutputStreamPtr stream,
+ mojo::ScopedSharedBufferHandle shared_buffer,
+ mojo::ScopedHandle socket_descriptor) {
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
+
+ if (!stream.is_bound()) {
+ audio_message_filter_->io_task_runner()->PostTask(
+ FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread,
+ base::Unretained(this), stream_id));
+ return;
+ }
+
+ stream.set_connection_error_handler(base::Bind(
+ &AudioOutputClient::OnStreamError, base::Unretained(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) {
+ LOG(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) {
+ LOG(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
+ streams_[stream_id] = make_scoped_ptr(
+ new mojom::AudioOutputStreamPtr(std::move(stream)));
+
+ audio_message_filter_->io_task_runner()->PostTask(
+ FROM_HERE, base::Bind(&AudioOutputClient::CreateStreamOnIOThread,
+ base::Unretained(this), streams_[stream_id].get(),
+ shared_memory_handle, descriptor, length));
+
+}
+
+void AudioOutputClient::CreateStreamOnIOThread(
+ mojom::AudioOutputStreamPtr* const stream,
+ base::SharedMemoryHandle handle,
+ base::SyncSocket::TransitDescriptor socket_descriptor,
+ uint32_t length) {
+ DCHECK(audio_message_filter_->io_task_runner()->BelongsToCurrentThread());
+ audio_message_filter_->OnStreamCreated(
+ FindAudioOutputStreamPtrID(streams_, stream),
+ handle, socket_descriptor, length);
+}
+
+void AudioOutputClient::CloseStream(int stream_id) {
+ main_thread_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread,
+ base::Unretained(this), stream_id));
+}
+
+void AudioOutputClient::CloseStreamOnMainThread(int stream_id) {
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
+ streams_[stream_id]->get()->Close();
+}
+
+void AudioOutputClient::ReportErrorOnIOThread(int stream_id) {
+ DCHECK(audio_message_filter_->io_task_runner()->BelongsToCurrentThread());
+ audio_message_filter_->OnStreamStateChanged(
+ stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR);
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698