| 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..ed4c36cf9e6c9ce89bdc401796c53a183b09e360
|
| --- /dev/null
|
| +++ b/content/renderer/media/audio_output_client.cc
|
| @@ -0,0 +1,139 @@
|
| +// 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"
|
| +
|
| +namespace content {
|
| +// TODO(rchtara): Check that the enum in mojo and in the rendrer are the some
|
| +// for Format and ChannelLayout.
|
| +AudioOutputStreamParametersPtr convert(const media::AudioParameters& input) {
|
| + AudioOutputStreamParametersPtr output(AudioOutputStreamParameters::New());
|
| +
|
| + output->format_ =
|
| + static_cast<AudioOutputStreamParameters::Format>(input.format());
|
| + output->channel_layout_ =
|
| + static_cast<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)));
|
| +}
|
| +
|
| +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) {
|
| + main_thread_task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&AudioOutputClient::CreateStreamOnMainThread,
|
| + base::Unretained(this), stream_id_, render_frame_id_, params));
|
| +}
|
| +
|
| +void AudioOutputClient::CreateStreamCallback(
|
| + mojo::ScopedSharedBufferHandle shared_buffer,
|
| + mojo::ScopedHandle socket_descriptor,
|
| + int stream_id,
|
| + content::AudioOutputStreamPtr stream) {
|
| + LOG(ERROR) << "CreateStreamCallback";
|
| + AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get();
|
| + audio_message_filter = AudioMessageFilter::Get();
|
| + // int stream_id = 0;
|
| + base::SharedMemoryHandle shared_memory_handle;
|
| + size_t length;
|
| +
|
| + MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle(
|
| + shared_buffer.get().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.get().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.get().handle;
|
| +#else
|
| + descriptor.fd = platform_handle.get().handle;
|
| +#endif
|
| +
|
| + audio_message_filter->OnStreamCreated(stream_id, shared_memory_handle,
|
| + descriptor, length);
|
| +
|
| + stream->Play();
|
| +}
|
| +
|
| +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::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);
|
| +}
|
| +
|
| +} // namespace content
|
|
|