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 "base/time/time.h" |
| 13 #include "content/common/media/audio_messages.h" |
| 14 #include "content/public/common/service_registry.h" |
| 15 #include "content/renderer/media/audio_message_filter.h" |
| 16 #include "content/renderer/media/webrtc_logging.h" |
| 17 #include "media/audio/audio_parameters.h" |
| 18 #include "mojo/edk/embedder/embedder.h" |
| 19 #include "mojo/public/c/system/buffer.h" |
| 20 #include "mojo/public/cpp/bindings/binding.h" |
| 21 #include "mojo/public/cpp/system/handle.h" |
| 22 |
| 23 namespace { |
| 24 // TODO(rchtara): Check that the enum in mojo and in the rendrer are the some |
| 25 // for Format and ChannelLayout. |
| 26 content::mojom::AudioOutputStreamParametersPtr convert( |
| 27 const media::AudioParameters& input) { |
| 28 content::mojom::AudioOutputStreamParametersPtr output( |
| 29 content::mojom::AudioOutputStreamParameters::New()); |
| 30 |
| 31 output->format_ = |
| 32 static_cast<content::mojom::AudioOutputStreamParameters::Format>( |
| 33 input.format()); |
| 34 output->channel_layout_ = |
| 35 static_cast<content::mojom::AudioOutputStreamParameters::ChannelLayout>( |
| 36 input.channel_layout()); |
| 37 output->channels_ = input.channels(); |
| 38 |
| 39 output->sample_rate_ = input.sample_rate(); |
| 40 output->bits_per_sample_ = input.bits_per_sample(); |
| 41 output->frames_per_buffer_ = input.frames_per_buffer(); |
| 42 output->effects_ = input.effects(); |
| 43 return output; |
| 44 } |
| 45 |
| 46 int findKeys( |
| 47 const std::map<int, scoped_ptr<content::mojom::AudioOutputStreamPtr>>& map, |
| 48 content::mojom::AudioOutputStreamPtr* const key) { |
| 49 for (auto it = map.begin(); it != map.end(); ++it) { |
| 50 if (it->second.get() == key) { |
| 51 return it->first; |
| 52 } |
| 53 } |
| 54 return -1; |
| 55 } |
| 56 |
| 57 } // namespace |
| 58 |
| 59 namespace content { |
| 60 |
| 61 |
| 62 |
| 63 AudioOutputClient::AudioOutputClient(ServiceRegistry* service_registry, |
| 64 AudioMessageFilter* audio_message_filter) |
| 65 : main_thread_task_runner_(base::MessageLoop::current()->task_runner()), |
| 66 audio_message_filter_(audio_message_filter) { |
| 67 |
| 68 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| 69 service_.set_connection_error_handler(base::Bind( |
| 70 &AudioOutputClient::OnConnectionError, base::Unretained(this))); |
| 71 } |
| 72 |
| 73 AudioOutputClient::~AudioOutputClient() { |
| 74 |
| 75 } |
| 76 |
| 77 void AudioOutputClient::OnConnectionError() { |
| 78 LOG(ERROR) << "Mojo client connection error"; |
| 79 } |
| 80 |
| 81 void AudioOutputClient::OnStreamError(int stream_id) { |
| 82 audio_message_filter_->io_task_runner()->PostTask( |
| 83 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, |
| 84 base::Unretained(this), stream_id)); |
| 85 } |
| 86 |
| 87 void AudioOutputClient::CreateStream(int stream_id, |
| 88 int render_frame_id, |
| 89 const media::AudioParameters& params) { |
| 90 main_thread_task_runner_->PostTask( |
| 91 FROM_HERE, |
| 92 base::Bind(&AudioOutputClient::CreateStreamOnMainThread, |
| 93 base::Unretained(this), stream_id, render_frame_id, params)); |
| 94 } |
| 95 |
| 96 void AudioOutputClient::CreateStreamOnMainThread( |
| 97 int stream_id, |
| 98 int render_frame_id, |
| 99 const media::AudioParameters& params) { |
| 100 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 101 service_->CreateStream(stream_id, render_frame_id, convert(params), |
| 102 base::Bind(&AudioOutputClient::CreateStreamCallback, |
| 103 base::Unretained(this))); |
| 104 } |
| 105 |
| 106 void AudioOutputClient::CreateStreamCallback( |
| 107 mojom::AudioOutputStreamPtr stream, |
| 108 int stream_id, |
| 109 mojo::ScopedSharedBufferHandle shared_buffer, |
| 110 mojo::ScopedHandle socket_descriptor) { |
| 111 |
| 112 if (!stream.is_bound()) { |
| 113 audio_message_filter_->io_task_runner()->PostTask( |
| 114 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, |
| 115 base::Unretained(this), stream_id)); |
| 116 return; |
| 117 } |
| 118 |
| 119 stream.set_connection_error_handler(base::Bind( |
| 120 &AudioOutputClient::OnStreamError, base::Unretained(this), stream_id)); |
| 121 |
| 122 base::SharedMemoryHandle shared_memory_handle; |
| 123 size_t length; |
| 124 |
| 125 MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( |
| 126 shared_buffer.release().value(), &shared_memory_handle, &length, nullptr); |
| 127 |
| 128 if (pass_shared_memory_result != MOJO_RESULT_OK) { |
| 129 LOG(ERROR) << "Failed to pass shared memory. Closing: " |
| 130 << pass_shared_memory_result; |
| 131 return; |
| 132 } |
| 133 |
| 134 mojo::edk::ScopedPlatformHandle platform_handle; |
| 135 |
| 136 MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( |
| 137 socket_descriptor.release().value(), &platform_handle); |
| 138 |
| 139 if (pass_platform_handle_result != MOJO_RESULT_OK) { |
| 140 LOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
| 141 << pass_platform_handle_result; |
| 142 return; |
| 143 } |
| 144 |
| 145 base::SyncSocket::TransitDescriptor descriptor; |
| 146 |
| 147 #if defined(OS_WIN) |
| 148 descriptor = platform_handle.release().handle; |
| 149 #else |
| 150 descriptor.fd = platform_handle.release().handle; |
| 151 #endif |
| 152 streams_[stream_id] = make_scoped_ptr( |
| 153 new mojom::AudioOutputStreamPtr(std::move(stream))); |
| 154 |
| 155 audio_message_filter_->io_task_runner()->PostTask( |
| 156 FROM_HERE, base::Bind(&AudioOutputClient::CreateStreamOnIOThread, |
| 157 base::Unretained(this), streams_[stream_id].get(), |
| 158 shared_memory_handle, descriptor, length)); |
| 159 |
| 160 } |
| 161 |
| 162 void AudioOutputClient::CreateStreamOnIOThread( |
| 163 mojom::AudioOutputStreamPtr* const stream, |
| 164 base::SharedMemoryHandle handle, |
| 165 base::SyncSocket::TransitDescriptor socket_descriptor, |
| 166 uint32_t length) { |
| 167 |
| 168 audio_message_filter_->OnStreamCreated(findKeys(streams_, stream), |
| 169 handle, socket_descriptor, length); |
| 170 } |
| 171 |
| 172 void AudioOutputClient::CloseStream(int stream_id) { |
| 173 main_thread_task_runner_->PostTask( |
| 174 FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread, |
| 175 base::Unretained(this), stream_id)); |
| 176 } |
| 177 |
| 178 void AudioOutputClient::CloseStreamOnMainThread(int stream_id) { |
| 179 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 180 streams_[stream_id]->get()->Close(); |
| 181 } |
| 182 |
| 183 void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { |
| 184 audio_message_filter_->OnStreamStateChanged( |
| 185 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); |
| 186 } |
| 187 |
| 188 } // namespace content |
OLD | NEW |