| 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..7a6f109fb124d8b16e33a16b8a338d76fcb6f204
|
| --- /dev/null
|
| +++ b/content/renderer/media/audio_output_client.cc
|
| @@ -0,0 +1,196 @@
|
| +// Copyright 2015 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 "content/common/media/audio_output.mojom.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 "mojo/edk/embedder/embedder.h"
|
| +#include "mojo/public/cpp/bindings/binding.h"
|
| +#include "mojo/public/cpp/system/handle.h"
|
| +#include "content/common/media/audio_messages.h"
|
| +#include "base/time/time.h"
|
| +#include "mojo/public/c/system/buffer.h"
|
| +
|
| +namespace content {
|
| +// TODO(rchtara): Check that the enum in mojo and in the rendrer are the some
|
| +// for Format and ChannelLayout.
|
| +mojom::AudioOutputStreamParametersPtr convert(
|
| + const media::AudioParameters& input) {
|
| + mojom::AudioOutputStreamParametersPtr output(
|
| + mojom::AudioOutputStreamParameters::New());
|
| +
|
| + output->format_ =
|
| + static_cast<mojom::AudioOutputStreamParameters::Format>(input.format());
|
| + output->channel_layout_ =
|
| + static_cast<mojom::AudioOutputStreamParameters::ChannelLayout>(
|
| + input.channel_layout());
|
| + output->channels_ = input.channels();
|
| +
|
| + output->sample_rate_ = input.sample_rate();
|
| + output->bits_per_sample_ = input.bits_per_sample();
|
| + output->frames_per_buffer_ = input.frames_per_buffer();
|
| + output->effects_ = input.effects();
|
| + return output;
|
| +}
|
| +
|
| +AudioOutputClient* AudioOutputClient::g_filter = nullptr;
|
| +
|
| +AudioOutputClient::AudioOutputClient(ServiceRegistry* service_registry)
|
| + : main_thread_task_runner_(base::MessageLoop::current()->task_runner()) {
|
| + DCHECK(!g_filter);
|
| + g_filter = this;
|
| +
|
| + service_registry->ConnectToRemoteService(mojo::GetProxy(&service_));
|
| + service_.set_connection_error_handler(base::Bind(
|
| + &AudioOutputClient::OnConnectionError, base::Unretained(this)));
|
| + DLOG(WARNING) << "AudioOutputClient::AudioOutputClient";
|
| +}
|
| +
|
| +AudioOutputClient::~AudioOutputClient() {
|
| + DLOG(WARNING) << "AudioOutputClient::~AudioOutputClient";
|
| +}
|
| +
|
| +// static
|
| +AudioOutputClient* AudioOutputClient::Get() {
|
| + return g_filter;
|
| +}
|
| +
|
| +void AudioOutputClient::OnConnectionError() {
|
| + LOG(ERROR) << "Failed to establish MOJO";
|
| +}
|
| +
|
| +void AudioOutputClient::CreateStream(int stream_id_,
|
| + int render_frame_id_,
|
| + const media::AudioParameters& params) {
|
| + LOG(ERROR) << "AudioOutputClient::CreateStream" << base::TimeTicks::Now();
|
| + main_thread_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&AudioOutputClient::CreateStreamOnMainThread,
|
| + base::Unretained(this), stream_id_, render_frame_id_, params));
|
| +}
|
| +
|
| +void AudioOutputClient::CreateStreamCallback(
|
| + mojom::AudioOutputStreamPtr stream,
|
| + int stream_id,
|
| + mojo::ScopedSharedBufferHandle shared_buffer,
|
| + mojo::ScopedHandle socket_descriptor) {
|
| + AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get();
|
| +
|
| + if (!stream.is_bound()) {
|
| + audio_message_filter->io_task_runner()->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread,
|
| + base::Unretained(this), stream_id));
|
| + return;
|
| + }
|
| + 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] = new mojom::AudioOutputStreamPtr(std::move(stream));
|
| +
|
| + PlayStream(streams_[stream_id]);
|
| +
|
| + audio_message_filter->io_task_runner()->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&AudioOutputClient::CreateStreamOnIOThread,
|
| + base::Unretained(this), streams_[stream_id], stream_id,
|
| + shared_memory_handle, descriptor, length));
|
| +}
|
| +
|
| +void AudioOutputClient::CreateStreamOnIOThread(
|
| + mojom::AudioOutputStreamPtr* stream,
|
| + int stream_id,
|
| + base::SharedMemoryHandle handle,
|
| + base::SyncSocket::TransitDescriptor socket_descriptor,
|
| + uint32_t length) {
|
| + LOG(ERROR) << "AudioOutputClient::CreateStreamOnIOThread"
|
| + << base::TimeTicks::Now();
|
| + AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get();
|
| + audio_message_filter->OnStreamCreated(stream, stream_id, handle,
|
| + socket_descriptor, length);
|
| +}
|
| +
|
| +void AudioOutputClient::CreateStreamOnMainThread(
|
| + int stream_id,
|
| + int render_frame_id,
|
| + const media::AudioParameters& params) {
|
| + DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
|
| + service_->CreateStream(stream_id, render_frame_id, convert(params),
|
| + base::Bind(&AudioOutputClient::CreateStreamCallback,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void AudioOutputClient::PlayStreamCallback(bool success) {
|
| + // AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get();
|
| + LOG(ERROR) << "AudioOutputClient::PlayStreamCallback";
|
| +}
|
| +
|
| +void AudioOutputClient::PlayStream(mojom::AudioOutputStreamPtr* stream) {
|
| + LOG(ERROR) << "AudioOutputClient::PlayStream";
|
| + main_thread_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputClient::PlayStreamOnMainThread,
|
| + base::Unretained(this), stream));
|
| +}
|
| +
|
| +void AudioOutputClient::PlayStreamOnMainThread(
|
| + mojom::AudioOutputStreamPtr* stream) {
|
| + LOG(ERROR) << "AudioOutputClient::PlayStreamOnMainThread";
|
| + DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
|
| + stream->get()->Play(base::Bind(&AudioOutputClient::PlayStreamCallback,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void AudioOutputClient::CloseStream(int id) {
|
| + main_thread_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread,
|
| + base::Unretained(this), id));
|
| +}
|
| +
|
| +void AudioOutputClient::CloseStreamOnMainThread(int id) {
|
| + DCHECK(main_thread_task_runner_->BelongsToCurrentThread());
|
| + service_->CloseStream(id);
|
| +}
|
| +
|
| +void AudioOutputClient::ReportErrorOnIOThread(int stream_id) {
|
| + AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get();
|
| + audio_message_filter->OnStreamStateChanged(
|
| + stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR);
|
| +}
|
| +
|
| +} // namespace content
|
|
|