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