| 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..abe0e945f95aa56d6ff4c28357f7bd3324a4db6b
|
| --- /dev/null
|
| +++ b/content/renderer/media/audio_output_client.cc
|
| @@ -0,0 +1,179 @@
|
| +// 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 "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,
|
| + media::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()) {
|
| + if (service_registry) {
|
| + service_registry->ConnectToRemoteService(mojo::GetProxy(&service_));
|
| + service_.set_connection_error_handler(base::Bind(
|
| + &AudioOutputClient::OnConnectionError, base::Unretained(this)));
|
| + } else {
|
| + service_ = 0;
|
| + }
|
| +}
|
| +
|
| +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());
|
| + if (service_) {
|
| + service_->CreateStream(
|
| + stream_id, render_frame_id,
|
| + mojo::ConvertTo<media::interfaces::AudioParametersPtr>(
|
| + params),
|
| + base::Bind(&AudioOutputClient::CreateStreamCallback,
|
| + base::Unretained(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 (!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_.insert(
|
| + std::pair<int, std::unique_ptr<media::mojom::AudioOutputStreamPtr>>
|
| + (stream_id,
|
| + std::unique_ptr<media::mojom::AudioOutputStreamPtr>
|
| + (new media::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(
|
| + media::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
|
|
|