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/audio/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 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 |
| 47 service_registry->ConnectToRemoteService(mojo::GetProxy(&service_)); |
| 48 service_.set_connection_error_handler(base::Bind( |
| 49 &AudioOutputClient::OnConnectionError, base::Unretained(this))); |
| 50 } |
| 51 |
| 52 AudioOutputClient::~AudioOutputClient() { |
| 53 } |
| 54 |
| 55 void AudioOutputClient::OnConnectionError() { |
| 56 LOG(ERROR) << "Mojo client connection error"; |
| 57 } |
| 58 |
| 59 void AudioOutputClient::OnStreamError(int stream_id) { |
| 60 audio_message_filter_->io_task_runner()->PostTask( |
| 61 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, |
| 62 base::Unretained(this), stream_id)); |
| 63 } |
| 64 |
| 65 void AudioOutputClient::CreateStream(int stream_id, |
| 66 int render_frame_id, |
| 67 const media::AudioParameters& params) { |
| 68 main_thread_task_runner_->PostTask( |
| 69 FROM_HERE, |
| 70 base::Bind(&AudioOutputClient::CreateStreamOnMainThread, |
| 71 base::Unretained(this), stream_id, render_frame_id, params)); |
| 72 } |
| 73 |
| 74 void AudioOutputClient::CreateStreamOnMainThread( |
| 75 int stream_id, |
| 76 int render_frame_id, |
| 77 const media::AudioParameters& params) { |
| 78 media::AudioParameters param; |
| 79 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 80 service_->CreateStream( |
| 81 stream_id, render_frame_id, |
| 82 mojo::ConvertTo<media::interfaces::AudioOutputStreamParametersPtr>( |
| 83 params), |
| 84 base::Bind(&AudioOutputClient::CreateStreamCallback, |
| 85 base::Unretained(this))); |
| 86 } |
| 87 |
| 88 void AudioOutputClient::CreateStreamCallback( |
| 89 int stream_id, |
| 90 mojom::AudioOutputStreamPtr stream, |
| 91 mojo::ScopedSharedBufferHandle shared_buffer, |
| 92 mojo::ScopedHandle socket_descriptor) { |
| 93 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 94 |
| 95 if (!stream.is_bound()) { |
| 96 audio_message_filter_->io_task_runner()->PostTask( |
| 97 FROM_HERE, base::Bind(&AudioOutputClient::ReportErrorOnIOThread, |
| 98 base::Unretained(this), stream_id)); |
| 99 return; |
| 100 } |
| 101 |
| 102 stream.set_connection_error_handler(base::Bind( |
| 103 &AudioOutputClient::OnStreamError, base::Unretained(this), stream_id)); |
| 104 |
| 105 base::SharedMemoryHandle shared_memory_handle; |
| 106 size_t length; |
| 107 |
| 108 MojoResult pass_shared_memory_result = mojo::edk::PassSharedMemoryHandle( |
| 109 shared_buffer.release().value(), &shared_memory_handle, &length, nullptr); |
| 110 |
| 111 if (pass_shared_memory_result != MOJO_RESULT_OK) { |
| 112 LOG(ERROR) << "Failed to pass shared memory. Closing: " |
| 113 << pass_shared_memory_result; |
| 114 return; |
| 115 } |
| 116 |
| 117 mojo::edk::ScopedPlatformHandle platform_handle; |
| 118 |
| 119 MojoResult pass_platform_handle_result = mojo::edk::PassWrappedPlatformHandle( |
| 120 socket_descriptor.release().value(), &platform_handle); |
| 121 |
| 122 if (pass_platform_handle_result != MOJO_RESULT_OK) { |
| 123 LOG(ERROR) << "Failed to pass transit descriptor. Closing: " |
| 124 << pass_platform_handle_result; |
| 125 return; |
| 126 } |
| 127 |
| 128 base::SyncSocket::TransitDescriptor descriptor; |
| 129 |
| 130 #if defined(OS_WIN) |
| 131 descriptor = platform_handle.release().handle; |
| 132 #else |
| 133 descriptor.fd = platform_handle.release().handle; |
| 134 #endif |
| 135 streams_[stream_id] = make_scoped_ptr( |
| 136 new mojom::AudioOutputStreamPtr(std::move(stream))); |
| 137 |
| 138 audio_message_filter_->io_task_runner()->PostTask( |
| 139 FROM_HERE, base::Bind(&AudioOutputClient::CreateStreamOnIOThread, |
| 140 base::Unretained(this), streams_[stream_id].get(), |
| 141 shared_memory_handle, descriptor, length)); |
| 142 |
| 143 } |
| 144 |
| 145 void AudioOutputClient::CreateStreamOnIOThread( |
| 146 mojom::AudioOutputStreamPtr* const stream, |
| 147 base::SharedMemoryHandle handle, |
| 148 base::SyncSocket::TransitDescriptor socket_descriptor, |
| 149 uint32_t length) { |
| 150 DCHECK(audio_message_filter_->io_task_runner()->BelongsToCurrentThread()); |
| 151 audio_message_filter_->OnStreamCreated( |
| 152 FindAudioOutputStreamPtrID(streams_, stream), |
| 153 handle, socket_descriptor, length); |
| 154 } |
| 155 |
| 156 void AudioOutputClient::CloseStream(int stream_id) { |
| 157 main_thread_task_runner_->PostTask( |
| 158 FROM_HERE, base::Bind(&AudioOutputClient::CloseStreamOnMainThread, |
| 159 base::Unretained(this), stream_id)); |
| 160 } |
| 161 |
| 162 void AudioOutputClient::CloseStreamOnMainThread(int stream_id) { |
| 163 DCHECK(main_thread_task_runner_->BelongsToCurrentThread()); |
| 164 streams_[stream_id]->get()->Close(); |
| 165 } |
| 166 |
| 167 void AudioOutputClient::ReportErrorOnIOThread(int stream_id) { |
| 168 DCHECK(audio_message_filter_->io_task_runner()->BelongsToCurrentThread()); |
| 169 audio_message_filter_->OnStreamStateChanged( |
| 170 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR); |
| 171 } |
| 172 |
| 173 } // namespace content |
OLD | NEW |