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