OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/base/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 #include "mojo/public/cpp/system/platform_handle.h" |
| 23 |
| 24 namespace content { |
| 25 |
| 26 namespace { |
| 27 |
| 28 // Mojo connection error handler. |
| 29 void OnConnectionError() { |
| 30 LOG(ERROR) << "Mojo client diconnected"; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 AudioOutputClient::AudioOutputClient( |
| 36 ServiceRegistry* service_registry, |
| 37 const scoped_refptr<base::SingleThreadTaskRunner> thread_task_runner) |
| 38 : thread_task_runner_(thread_task_runner) { |
| 39 if (!service_registry || !AudioMessageFilter::Get()) |
| 40 return; |
| 41 service_ = new media::mojom::AudioOutputPtr(); |
| 42 service_registry->ConnectToRemoteService(mojo::GetProxy(service_)); |
| 43 service_->set_connection_error_handler(base::Bind(&OnConnectionError)); |
| 44 } |
| 45 |
| 46 AudioOutputClient::~AudioOutputClient() { |
| 47 if (thread_task_runner_->BelongsToCurrentThread()) |
| 48 Reset(service_); |
| 49 else |
| 50 thread_task_runner_->PostTask( |
| 51 FROM_HERE, base::Bind(&AudioOutputClient::Reset, service_)); |
| 52 } |
| 53 |
| 54 // static |
| 55 void AudioOutputClient::Reset(media::mojom::AudioOutputPtr* service) { |
| 56 service->reset(); |
| 57 delete service; |
| 58 } |
| 59 |
| 60 void AudioOutputClient::OnStreamError(int stream_id) { |
| 61 if (!AudioMessageFilter::Get() || !service_->is_bound()) |
| 62 return; |
| 63 AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| 64 FROM_HERE, |
| 65 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
| 66 } |
| 67 |
| 68 void AudioOutputClient::CreateStream( |
| 69 int stream_id, |
| 70 const media::AudioParameters& params, |
| 71 const media::mojom::AudioOutput::CreateStreamCallback& callback) { |
| 72 if (!thread_task_runner_->BelongsToCurrentThread()) { |
| 73 thread_task_runner_->PostTask( |
| 74 FROM_HERE, base::Bind(&AudioOutputClient::CreateStream, this, stream_id, |
| 75 params, callback)); |
| 76 return; |
| 77 } |
| 78 if (service_->is_bound()) { |
| 79 (*service_)->CreateStream(stream_id, params, callback); |
| 80 } |
| 81 } |
| 82 |
| 83 void AudioOutputClient::CreateStreamCallback( |
| 84 int stream_id, |
| 85 media::mojom::AudioOutputStreamPtr stream, |
| 86 mojo::ScopedSharedBufferHandle shared_buffer, |
| 87 mojo::ScopedHandle socket_descriptor) { |
| 88 DCHECK(thread_task_runner_->BelongsToCurrentThread()); |
| 89 if (!AudioMessageFilter::Get()) |
| 90 return; |
| 91 if (!stream.is_bound()) { |
| 92 AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| 93 FROM_HERE, |
| 94 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
| 95 return; |
| 96 } |
| 97 |
| 98 stream.set_connection_error_handler( |
| 99 base::Bind(&AudioOutputClient::OnStreamError, this, stream_id)); |
| 100 base::SharedMemoryHandle shared_memory_handle; |
| 101 size_t length; |
| 102 bool read_only; |
| 103 MojoResult unwrap_shared_memory_result = mojo::UnwrapSharedMemoryHandle( |
| 104 std::move(shared_buffer), &shared_memory_handle, &length, &read_only); |
| 105 |
| 106 if (unwrap_shared_memory_result != MOJO_RESULT_OK) { |
| 107 DLOG(ERROR) << "Failed to pass shared memory. Closing: " |
| 108 << unwrap_shared_memory_result; |
| 109 AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| 110 FROM_HERE, |
| 111 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
| 112 return; |
| 113 } |
| 114 |
| 115 base::SyncSocket::TransitDescriptor descriptor; |
| 116 MojoResult unwrap_platform_file_result = |
| 117 mojo::UnwrapPlatformFile(std::move(socket_descriptor), |
| 118 #if defined(OS_WIN) |
| 119 &descriptor); |
| 120 #else |
| 121 &descriptor.fd); |
| 122 #endif |
| 123 if (unwrap_platform_file_result != MOJO_RESULT_OK) { |
| 124 DLOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
| 125 << unwrap_platform_file_result; |
| 126 AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| 127 FROM_HERE, |
| 128 base::Bind(&AudioOutputClient::ReportErrorOnIOThread, this, stream_id)); |
| 129 return; |
| 130 } |
| 131 |
| 132 auto result = streams_.insert(std::make_pair(stream_id, std::move(stream))); |
| 133 AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| 134 FROM_HERE, |
| 135 base::Bind(&AudioOutputClient::CreateStreamOnIOThread, this, result.first, |
| 136 shared_memory_handle, descriptor, length)); |
| 137 } |
| 138 |
| 139 void AudioOutputClient::CreateStreamOnIOThread( |
| 140 AudioOutputClient::AudioOutputStreamPtrMap::iterator stream, |
| 141 base::SharedMemoryHandle handle, |
| 142 base::SyncSocket::TransitDescriptor socket_descriptor, |
| 143 uint32_t length) { |
| 144 if (!AudioMessageFilter::Get()) |
| 145 return; |
| 146 DCHECK(AudioMessageFilter::Get()->io_task_runner()->BelongsToCurrentThread()); |
| 147 AudioMessageFilter::Get()->StreamCreated(stream->first, handle, |
| 148 socket_descriptor, length); |
| 149 } |
| 150 |
| 151 void AudioOutputClient::CloseStream(int stream_id) { |
| 152 if (!thread_task_runner_->BelongsToCurrentThread()) { |
| 153 thread_task_runner_->PostTask( |
| 154 FROM_HERE, |
| 155 base::Bind(&AudioOutputClient::CloseStream, this, stream_id)); |
| 156 return; |
| 157 } |
| 158 |
| 159 DCHECK(thread_task_runner_->BelongsToCurrentThread()); |
| 160 if (service_->is_bound()) { |
| 161 if (streams_.find(stream_id) != streams_.end() && |
| 162 streams_[stream_id].is_bound()) { |
| 163 streams_[stream_id]->Close(); |
| 164 streams_.erase(stream_id); |
| 165 } else if (AudioMessageFilter::Get()) { |
| 166 AudioMessageFilter::Get()->io_task_runner()->PostTask( |
| 167 FROM_HERE, base::Bind(&AudioMessageFilter::CloseStream, |
| 168 AudioMessageFilter::Get(), stream_id)); |
| 169 } |
| 170 } |
| 171 } |
| 172 |
| 173 void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { |
| 174 DCHECK(AudioMessageFilter::Get()->io_task_runner()->BelongsToCurrentThread()); |
| 175 if (!AudioMessageFilter::Get()) |
| 176 return; |
| 177 AudioMessageFilter::Get()->OnStreamStateChanged( |
| 178 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); |
| 179 } |
| 180 |
| 181 } // namespace content |
OLD | NEW |