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..251ee33415cc50e97c64eb6be5c50f4140193fe8 |
--- /dev/null |
+++ b/content/renderer/media/audio_output_client.cc |
@@ -0,0 +1,188 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
Henrik Grunell
2016/04/19 15:36:09
2016
Same elsewhere.
rchtara
2016/04/21 09:10:18
Done.
|
+// 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 "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 { |
+// TODO(rchtara): Check that the enum in mojo and in the rendrer are the some |
Henrik Grunell
2016/04/19 15:36:09
This must be done in this CL.
rchtara
2016/04/21 09:10:18
Done.
|
+// for Format and ChannelLayout. |
+content::mojom::AudioOutputStreamParametersPtr convert( |
+ const media::AudioParameters& input) { |
+ content::mojom::AudioOutputStreamParametersPtr output( |
+ content::mojom::AudioOutputStreamParameters::New()); |
+ |
+ output->format_ = |
+ static_cast<content::mojom::AudioOutputStreamParameters::Format>( |
+ input.format()); |
+ output->channel_layout_ = |
+ static_cast<content::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; |
+} |
+ |
+int findKeys( |
Henrik Grunell
2016/04/19 15:36:09
Capital first letter.
Henrik Grunell
2016/04/19 15:36:09
Change name to something that describes a bit more
rchtara
2016/04/21 09:10:18
Done.
rchtara
2016/04/21 09:10:18
Done.
|
+ const std::map<int, scoped_ptr<content::mojom::AudioOutputStreamPtr>>& map, |
Henrik Grunell
2016/04/19 15:36:09
typedef the map.
Henrik Grunell
2016/04/19 15:36:09
Paramater name "map" doesn't say anything. Improve
rchtara
2016/04/21 09:10:18
Done.
rchtara
2016/04/21 09:10:18
Done.
|
+ content::mojom::AudioOutputStreamPtr* const key) { |
+ for (auto it = map.begin(); it != map.end(); ++it) { |
Henrik Grunell
2016/04/19 15:36:09
You can do
for (auto it: map) {
...
}
rchtara
2016/04/21 09:10:18
Done.
|
+ if (it->second.get() == key) { |
+ return it->first; |
+ } |
+ } |
+ return -1; |
+} |
+ |
+} // namespace |
+ |
+namespace content { |
Henrik Grunell
2016/04/19 15:36:09
Put on top. (Anonymous namespace nested in this.)
rchtara
2016/04/21 09:10:18
Done.
|
+ |
Henrik Grunell
2016/04/19 15:36:09
Remove two lines breaks.
rchtara
2016/04/21 09:10:18
Done.
|
+ |
+ |
+AudioOutputClient::AudioOutputClient(ServiceRegistry* service_registry, |
+ AudioMessageFilter* audio_message_filter) |
+ : main_thread_task_runner_(base::MessageLoop::current()->task_runner()), |
+ audio_message_filter_(audio_message_filter) { |
+ |
+ 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) { |
+ DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
+ service_->CreateStream(stream_id, render_frame_id, convert(params), |
+ base::Bind(&AudioOutputClient::CreateStreamCallback, |
+ base::Unretained(this))); |
+} |
+ |
+void AudioOutputClient::CreateStreamCallback( |
+ mojom::AudioOutputStreamPtr stream, |
+ int stream_id, |
+ mojo::ScopedSharedBufferHandle shared_buffer, |
+ mojo::ScopedHandle socket_descriptor) { |
+ |
+ 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) { |
+ |
+ audio_message_filter_->OnStreamCreated(findKeys(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) { |
+ audio_message_filter_->OnStreamStateChanged( |
+ stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); |
+} |
+ |
+} // namespace content |