| 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 #include "mojo/public/cpp/system/handle.h" |
| 20 #include "content/common/media/audio_messages.h" |
| 21 #include "base/time/time.h" |
| 22 #include "mojo/public/c/system/buffer.h" |
| 23 |
| 24 namespace content { |
| 25 // TODO(rchtara): Check that the enum in mojo and in the rendrer are the some |
| 26 // for Format and ChannelLayout. |
| 27 mojom::AudioOutputStreamParametersPtr convert( |
| 28 const media::AudioParameters& input) { |
| 29 mojom::AudioOutputStreamParametersPtr output( |
| 30 mojom::AudioOutputStreamParameters::New()); |
| 31 |
| 32 output->format_ = |
| 33 static_cast<mojom::AudioOutputStreamParameters::Format>(input.format()); |
| 34 output->channel_layout_ = |
| 35 static_cast<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 AudioOutputClient* AudioOutputClient::g_filter = nullptr; |
| 47 |
| 48 AudioOutputClient::AudioOutputClient(ServiceRegistry* service_registry) |
| 49 : main_thread_task_runner_(base::MessageLoop::current()->task_runner()) { |
| 50 DCHECK(!g_filter); |
| 51 g_filter = this; |
| 52 |
| 53 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| 54 service_.set_connection_error_handler(base::Bind( |
| 55 &AudioOutputClient::OnConnectionError, base::Unretained(this))); |
| 56 DLOG(WARNING) << "AudioOutputClient::AudioOutputClient"; |
| 57 } |
| 58 |
| 59 AudioOutputClient::~AudioOutputClient() { |
| 60 DLOG(WARNING) << "AudioOutputClient::~AudioOutputClient"; |
| 61 } |
| 62 |
| 63 // static |
| 64 AudioOutputClient* AudioOutputClient::Get() { |
| 65 return g_filter; |
| 66 } |
| 67 |
| 68 void AudioOutputClient::OnConnectionError() { |
| 69 LOG(ERROR) << "Failed to establish MOJO"; |
| 70 } |
| 71 |
| 72 void AudioOutputClient::CreateStream(int stream_id_, |
| 73 int render_frame_id_, |
| 74 const media::AudioParameters& params) { |
| 75 LOG(ERROR) << "AudioOutputClient::CreateStream" << base::TimeTicks::Now(); |
| 76 main_thread_task_runner_->PostTask( |
| 77 FROM_HERE, |
| 78 base::Bind(&AudioOutputClient::CreateStreamOnMainThread, |
| 79 base::Unretained(this), stream_id_, render_frame_id_, params)); |
| 80 } |
| 81 |
| 82 void AudioOutputClient::CreateStreamCallback( |
| 83 mojom::AudioOutputStreamPtr stream, |
| 84 int stream_id, |
| 85 mojo::ScopedSharedBufferHandle shared_buffer, |
| 86 mojo::ScopedHandle socket_descriptor) { |
| 87 AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get(); |
| 88 |
| 89 if (!stream.is_bound()) { |
| 90 audio_message_filter->io_task_runner()->PostTask( |
| 91 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, |
| 92 base::Unretained(this), stream_id)); |
| 93 return; |
| 94 } |
| 95 base::SharedMemoryHandle shared_memory_handle; |
| 96 size_t length; |
| 97 |
| 98 MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( |
| 99 shared_buffer.release().value(), &shared_memory_handle, &length, nullptr); |
| 100 |
| 101 if (pass_shared_memory_result != MOJO_RESULT_OK) { |
| 102 LOG(ERROR) << "Failed to pass shared memory. Closing: " |
| 103 << pass_shared_memory_result; |
| 104 return; |
| 105 } |
| 106 |
| 107 mojo::edk::ScopedPlatformHandle platform_handle; |
| 108 |
| 109 MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( |
| 110 socket_descriptor.release().value(), &platform_handle); |
| 111 |
| 112 if (pass_platform_handle_result != MOJO_RESULT_OK) { |
| 113 LOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
| 114 << pass_platform_handle_result; |
| 115 return; |
| 116 } |
| 117 |
| 118 base::SyncSocket::TransitDescriptor descriptor; |
| 119 |
| 120 #if defined(OS_WIN) |
| 121 descriptor = platform_handle.release().handle; |
| 122 #else |
| 123 descriptor.fd = platform_handle.release().handle; |
| 124 #endif |
| 125 streams_[stream_id] = new mojom::AudioOutputStreamPtr(std::move(stream)); |
| 126 |
| 127 PlayStream(streams_[stream_id]); |
| 128 |
| 129 audio_message_filter->io_task_runner()->PostTask( |
| 130 FROM_HERE, |
| 131 base::Bind(&AudioOutputClient::CreateStreamOnIOThread, |
| 132 base::Unretained(this), streams_[stream_id], stream_id, |
| 133 shared_memory_handle, descriptor, length)); |
| 134 } |
| 135 |
| 136 void AudioOutputClient::CreateStreamOnIOThread( |
| 137 mojom::AudioOutputStreamPtr* stream, |
| 138 int stream_id, |
| 139 base::SharedMemoryHandle handle, |
| 140 base::SyncSocket::TransitDescriptor socket_descriptor, |
| 141 uint32_t length) { |
| 142 LOG(ERROR) << "AudioOutputClient::CreateStreamOnIOThread" |
| 143 << base::TimeTicks::Now(); |
| 144 AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get(); |
| 145 audio_message_filter->OnStreamCreated(stream, stream_id, handle, |
| 146 socket_descriptor, length); |
| 147 } |
| 148 |
| 149 void AudioOutputClient::CreateStreamOnMainThread( |
| 150 int stream_id, |
| 151 int render_frame_id, |
| 152 const media::AudioParameters& params) { |
| 153 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 154 service_->CreateStream(stream_id, render_frame_id, convert(params), |
| 155 base::Bind(&AudioOutputClient::CreateStreamCallback, |
| 156 base::Unretained(this))); |
| 157 } |
| 158 |
| 159 void AudioOutputClient::PlayStreamCallback(bool success) { |
| 160 // AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get(); |
| 161 LOG(ERROR) << "AudioOutputClient::PlayStreamCallback"; |
| 162 } |
| 163 |
| 164 void AudioOutputClient::PlayStream(mojom::AudioOutputStreamPtr* stream) { |
| 165 LOG(ERROR) << "AudioOutputClient::PlayStream"; |
| 166 main_thread_task_runner_->PostTask( |
| 167 FROM_HERE, base::Bind(&AudioOutputClient::PlayStreamOnMainThread, |
| 168 base::Unretained(this), stream)); |
| 169 } |
| 170 |
| 171 void AudioOutputClient::PlayStreamOnMainThread( |
| 172 mojom::AudioOutputStreamPtr* stream) { |
| 173 LOG(ERROR) << "AudioOutputClient::PlayStreamOnMainThread"; |
| 174 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 175 stream->get()->Play(base::Bind(&AudioOutputClient::PlayStreamCallback, |
| 176 base::Unretained(this))); |
| 177 } |
| 178 |
| 179 void AudioOutputClient::CloseStream(int id) { |
| 180 main_thread_task_runner_->PostTask( |
| 181 FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread, |
| 182 base::Unretained(this), id)); |
| 183 } |
| 184 |
| 185 void AudioOutputClient::CloseStreamOnMainThread(int id) { |
| 186 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 187 service_->CloseStream(id); |
| 188 } |
| 189 |
| 190 void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { |
| 191 AudioMessageFilter* audio_message_filter = AudioMessageFilter::Get(); |
| 192 audio_message_filter->OnStreamStateChanged( |
| 193 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); |
| 194 } |
| 195 |
| 196 } // namespace content |
| OLD | NEW |