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