Chromium Code Reviews| 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..67c2c74ae94bc014352c1ee70844c945610cf146 |
| --- /dev/null |
| +++ b/content/renderer/media/audio_output_client.cc |
| @@ -0,0 +1,171 @@ |
| +// 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" |
| + |
| +namespace content { |
| + |
| +namespace { |
| +// Mojo connection error handler. |
| +void OnConnectionError() { |
| + DLOG(ERROR) << "Mojo client connection error"; |
| +} |
| + |
| +} // namespace |
| + |
| +AudioOutputClient::AudioOutputClient( |
| + ServiceRegistry* service_registry, |
| + const scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner) |
| + : thread_task_runner_(thread_task_runner) { |
| + if (!service_registry) |
| + return; |
| + service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| + service_.set_connection_error_handler(base::Bind(&OnConnectionError)); |
| +} |
| + |
| +AudioOutputClient::~AudioOutputClient() {} |
| + |
| +void AudioOutputClient::OnStreamError(int stream_id) { |
| + if (!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; |
| + } |
| + DCHECK(thread_task_runner_->BelongsToCurrentThread()); |
|
Henrik Grunell
2016/05/20 13:35:41
No need since there is a conditional post above.
rchtara
2016/05/27 15:24:38
Done.
|
| + 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( |
|
Henrik Grunell
2016/05/20 13:35:41
Pass the io task runner in the constructor instead
rchtara
2016/05/27 15:24:38
Done.
|
| + 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; |
| + |
| + 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) { |
| + DLOG(ERROR) << "Failed to pass shared memory. Closing: " |
| + << pass_shared_memory_result; |
| + AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
| + 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) { |
| + DLOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
| + << pass_platform_handle_result; |
| + AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
| + return; |
| + } |
| + |
| + base::SyncSocket::TransitDescriptor descriptor; |
| + |
| +#if defined(OS_WIN) |
| + descriptor = platform_handle.release().handle; |
| +#else |
| + descriptor.fd = platform_handle.release().handle; |
| +#endif |
| + |
| + 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()->OnStreamCreated(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()->CloseStream(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 |