| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/media/audio_output_client.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/files/file.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/single_thread_task_runner.h" |
| 12 #include "content/common/media/audio_output.mojom.h" |
| 13 #include "content/public/common/service_registry.h" |
| 14 #include "content/renderer/media/audio_message_filter.h" |
| 15 #include "content/renderer/media/webrtc_logging.h" |
| 16 #include "media/audio/audio_parameters.h" |
| 17 #include "mojo/edk/embedder/embedder.h" |
| 18 #include "mojo/public/cpp/bindings/binding.h" |
| 19 |
| 20 namespace content { |
| 21 // TODO(rchtara): Check that the enum in mojo and in the rendrer are the some |
| 22 // for Format and ChannelLayout. |
| 23 AudioOutputStreamParametersPtr convert(const media::AudioParameters& input) { |
| 24 AudioOutputStreamParametersPtr output(AudioOutputStreamParameters::New()); |
| 25 |
| 26 output->format_ = |
| 27 static_cast<AudioOutputStreamParameters::Format>(input.format()); |
| 28 output->channel_layout_ = |
| 29 static_cast<AudioOutputStreamParameters::ChannelLayout>( |
| 30 input.channel_layout()); |
| 31 output->channels_ = input.channels(); |
| 32 |
| 33 output->sample_rate_ = input.sample_rate(); |
| 34 output->bits_per_sample_ = input.bits_per_sample(); |
| 35 output->frames_per_buffer_ = input.frames_per_buffer(); |
| 36 output->effects_ = input.effects(); |
| 37 return output; |
| 38 } |
| 39 |
| 40 AudioOutputClient* AudioOutputClient::g_filter = nullptr; |
| 41 |
| 42 AudioOutputClient::AudioOutputClient(ServiceRegistry* service_registry) |
| 43 : main_thread_task_runner_(base::MessageLoop::current()->task_runner()) { |
| 44 DCHECK(!g_filter); |
| 45 g_filter = this; |
| 46 |
| 47 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| 48 service_.set_connection_error_handler(base::Bind( |
| 49 &AudioOutputClient::OnConnectionError, base::Unretained(this))); |
| 50 } |
| 51 |
| 52 AudioOutputClient::~AudioOutputClient() {} |
| 53 |
| 54 // static |
| 55 AudioOutputClient* AudioOutputClient::Get() { |
| 56 return g_filter; |
| 57 } |
| 58 |
| 59 void AudioOutputClient::OnConnectionError() { |
| 60 LOG(ERROR) << "Failed to establish MOJO"; |
| 61 } |
| 62 |
| 63 void AudioOutputClient::CreateStream(int stream_id_, |
| 64 int render_frame_id_, |
| 65 const media::AudioParameters& params) { |
| 66 main_thread_task_runner_->PostTask( |
| 67 FROM_HERE, |
| 68 base::Bind(&AudioOutputClient::CreateStreamOnMainThread, |
| 69 base::Unretained(this), stream_id_, render_frame_id_, params)); |
| 70 } |
| 71 |
| 72 void AudioOutputClient::CreateStreamCallback( |
| 73 mojo::ScopedSharedBufferHandle shared_buffer, |
| 74 mojo::ScopedHandle socket_descriptor, |
| 75 int stream_id, |
| 76 content::AudioOutputStreamPtr stream) { |
| 77 LOG(ERROR) << "CreateStreamCallback"; |
| 78 AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get(); |
| 79 audio_message_filter = AudioMessageFilter::Get(); |
| 80 // int stream_id = 0; |
| 81 base::SharedMemoryHandle shared_memory_handle; |
| 82 size_t length; |
| 83 |
| 84 MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( |
| 85 shared_buffer.get().value(), &shared_memory_handle, &length, nullptr); |
| 86 |
| 87 if (pass_shared_memory_result != MOJO_RESULT_OK) { |
| 88 LOG(ERROR) << "Failed to pass shared memory. Closing: " |
| 89 << pass_shared_memory_result; |
| 90 return; |
| 91 } |
| 92 |
| 93 mojo::edk::ScopedPlatformHandle platform_handle; |
| 94 |
| 95 MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( |
| 96 socket_descriptor.get().value(), &platform_handle); |
| 97 |
| 98 if (pass_platform_handle_result != MOJO_RESULT_OK) { |
| 99 LOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
| 100 << pass_platform_handle_result; |
| 101 return; |
| 102 } |
| 103 |
| 104 base::SyncSocket::TransitDescriptor descriptor; |
| 105 |
| 106 #if defined(OS_WIN) |
| 107 descriptor = platform_handle.get().handle; |
| 108 #else |
| 109 descriptor.fd = platform_handle.get().handle; |
| 110 #endif |
| 111 |
| 112 audio_message_filter->OnStreamCreated(stream_id, shared_memory_handle, |
| 113 descriptor, length); |
| 114 |
| 115 stream->Play(); |
| 116 } |
| 117 |
| 118 void AudioOutputClient::CreateStreamOnMainThread( |
| 119 int stream_id_, |
| 120 int render_frame_id_, |
| 121 const media::AudioParameters& params) { |
| 122 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 123 service_->CreateStream(stream_id_, render_frame_id_, convert(params), |
| 124 base::Bind(&AudioOutputClient::CreateStreamCallback, |
| 125 base::Unretained(this))); |
| 126 } |
| 127 |
| 128 void AudioOutputClient::CloseStream(int id) { |
| 129 main_thread_task_runner_->PostTask( |
| 130 FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread, |
| 131 base::Unretained(this), id)); |
| 132 } |
| 133 |
| 134 void AudioOutputClient::CloseStreamOnMainThread(int id) { |
| 135 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 136 service_->CloseStream(id); |
| 137 } |
| 138 |
| 139 } // namespace content |
| OLD | NEW |