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