Chromium Code Reviews| 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 #if defined(OS_WIN) | |
| 6 #include <windows.h> | |
| 7 #endif | |
| 8 | |
| 9 #include "base/memory/shared_memory.h" | |
| 10 #include "content/browser/frame_host/render_frame_host_impl.h" | |
| 11 #include "content/browser/media/audio_output_impl.h" | |
| 12 #include "content/browser/media/audio_output_stream_impl.h" | |
| 13 #include "content/browser/renderer_host/render_process_host_impl.h" | |
| 14 #include "content/public/browser/render_frame_host.h" | |
| 15 #include "mojo/edk/embedder/embedder.h" | |
| 16 #include "mojo/public/cpp/system/handle.h" | |
| 17 #include "mojo/public/cpp/system/platform_handle.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 base::SyncSocket::TransitDescriptor DuplicateSocket( | |
| 24 base::SyncSocket::TransitDescriptor socket_descriptor) { | |
| 25 base::SyncSocket::TransitDescriptor socket_descriptor_dup; | |
| 26 | |
| 27 #if defined(OS_WIN) | |
| 28 socket_descriptor_dup = 0; | |
| 29 if (!::DuplicateHandle(GetCurrentProcess(), // hSourceProcessHandle | |
| 30 socket_descriptor, | |
| 31 GetCurrentProcess(), // hTargetProcessHandle | |
| 32 &socket_descriptor_dup, | |
| 33 0, // dwDesiredAccess ignored due to SAME_ACCESS | |
| 34 FALSE, // !bInheritHandle | |
| 35 DUPLICATE_SAME_ACCESS)) { | |
| 36 LOG(ERROR) << "Unable to duplicate socket handle."; | |
| 37 } | |
| 38 | |
| 39 #else | |
| 40 socket_descriptor_dup.fd = dup(socket_descriptor.fd); | |
| 41 #endif | |
| 42 return socket_descriptor_dup; | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 AudioOutputImpl::AudioOutputImpl(RenderFrameHostImpl* frame, | |
| 48 media::mojom::AudioOutputRequest request) | |
| 49 : frame_(frame), binding_(this) { | |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 51 binding_.Bind(std::move(request)); | |
| 52 } | |
| 53 | |
| 54 AudioOutputImpl::~AudioOutputImpl() { | |
| 55 for (auto it = create_stream_callbacks_.begin(); | |
| 56 it != create_stream_callbacks_.end(); ++it) { | |
| 57 it->second.reset(); | |
| 58 } | |
| 59 create_stream_callbacks_.clear(); | |
| 60 } | |
| 61 | |
| 62 // static | |
| 63 void AudioOutputImpl::CreateService(RenderFrameHostImpl* frame, | |
| 64 media::mojom::AudioOutputRequest request) { | |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 66 | |
| 67 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 68 base::Bind(&AudioOutputImpl::CreateServiceOnIOThread, | |
| 69 frame, base::Passed(&request))); | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 void AudioOutputImpl::CreateServiceOnIOThread( | |
| 74 RenderFrameHostImpl* frame, | |
| 75 media::mojom::AudioOutputRequest request) { | |
| 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 77 | |
| 78 new AudioOutputImpl(frame, std::move(request)); | |
| 79 } | |
| 80 | |
| 81 void AudioOutputImpl::CreateStream(int stream_id, | |
| 82 const media::AudioParameters& params, | |
| 83 const CreateStreamCallback& callback) { | |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 85 | |
| 86 create_stream_callbacks_.insert(std::make_pair(stream_id, callback)); | |
| 87 InitAudioRendererHost(); | |
| 88 if (audio_renderer_host_) | |
| 89 audio_renderer_host_->CreateStream(stream_id, frame_->GetRoutingID(), | |
| 90 params, this); | |
| 91 } | |
| 92 | |
| 93 void AudioOutputImpl::CreateStreamFactory( | |
| 94 int stream_id, | |
| 95 base::SharedMemory* shared_memory, | |
| 96 base::SyncSocket::TransitDescriptor socket_descriptor) { | |
| 97 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 98 | |
| 99 auto callback = create_stream_callbacks_.find(stream_id); | |
| 100 if (callback == create_stream_callbacks_.end()) | |
| 101 return; | |
| 102 | |
| 103 media::mojom::AudioOutputStreamPtr stream_ptr = | |
| 104 media::mojom::AudioOutputStreamPtr(); | |
| 105 std::unique_ptr<AudioOutputStreamImpl> stream( | |
| 106 new AudioOutputStreamImpl(stream_id, this, mojo::GetProxy(&stream_ptr))); | |
| 107 stream_impls_.insert(std::make_pair(stream_id, std::move(stream))); | |
| 108 | |
| 109 base::SharedMemoryHandle shared_memory_handle = | |
| 110 base::SharedMemory::DuplicateHandle(shared_memory->handle()); | |
| 111 if (!base::SharedMemory::IsHandleValid(shared_memory_handle)) | |
| 112 return; | |
| 113 mojo::ScopedSharedBufferHandle shared_buffer_handle = | |
| 114 mojo::WrapSharedMemoryHandle(shared_memory_handle, | |
| 115 shared_memory->requested_size(), false); | |
| 116 // The socket that we will send from the browser to the renderer is a | |
| 117 // |foreign_socket_| which is a part AudioSyncReader that is owned by | |
| 118 // AudioEntry. The socket handle will be closed when the AudioEntry it | |
| 119 // belongs to will be closed. However, with mojo and unlike IPC, the | |
| 120 // ownership of the handle is transferred to the target process. It's no | |
| 121 // longer a valid handle in the sending process and it is an error to try | |
| 122 // closing it there. So, if the socket handle is closed when the AudioEntry | |
| 123 // is deleted, we will have an error. To fix this error we could just | |
| 124 // duplicate the socket and send the duplicate to the renderer. Thus, we | |
| 125 // will avoid having an problem when closing the socket. | |
| 126 // See https://goo.gl/ACwSfa. | |
| 127 base::SyncSocket::TransitDescriptor socket_descriptor_dup = | |
| 128 DuplicateSocket(socket_descriptor); | |
| 129 #if defined(OS_WIN) | |
| 130 mojo::ScopedHandle socket_handle = | |
| 131 mojo::WrapPlatformFile(socket_descriptor_dup); | |
| 132 #else | |
| 133 mojo::ScopedHandle socket_handle = | |
| 134 mojo::WrapPlatformFile(socket_descriptor_dup.fd); | |
| 135 #endif | |
| 136 | |
| 137 callback->second.Run(stream_id, std::move(stream_ptr), | |
| 138 std::move(shared_buffer_handle), | |
| 139 std::move(socket_handle)); | |
| 140 callback->second.reset(); | |
| 141 create_stream_callbacks_.erase(stream_id); | |
| 142 } | |
| 143 | |
| 144 bool AudioOutputImpl::CloseStream(int stream_id) { | |
| 145 if (audio_renderer_host_.get()) | |
| 146 audio_renderer_host_->CloseStream(stream_id); | |
| 147 | |
| 148 if (stream_impls_.erase(stream_id) == 0) | |
| 149 return false; | |
| 150 | |
| 151 auto callback = create_stream_callbacks_.find(stream_id); | |
| 152 | |
| 153 if (callback != create_stream_callbacks_.end()) { | |
| 154 callback->second.reset(); | |
| 155 create_stream_callbacks_.erase(stream_id); | |
| 156 } | |
| 157 return true; | |
| 158 } | |
| 159 | |
| 160 void AudioOutputImpl::ReportErrorAndCloseStream(int stream_id) { | |
| 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 162 | |
| 163 // Make sure this isn't a stray callback executing after the stream has been | |
| 164 // closed, so error notifications aren't sent after clients believe the stream | |
| 165 // is closed. | |
| 166 auto callback = create_stream_callbacks_.find(stream_id); | |
| 167 | |
| 168 mojo::ScopedSharedBufferHandle shared_buffer_handle = | |
| 169 mojo::ScopedSharedBufferHandle(mojo::SharedBufferHandle()); | |
| 170 | |
| 171 mojo::ScopedHandle socket_handle = mojo::ScopedHandle(mojo::Handle()); | |
| 172 | |
| 173 callback->second.Run(stream_id, media::mojom::AudioOutputStreamPtr(), | |
| 174 std::move(shared_buffer_handle), | |
| 175 std::move(socket_handle)); | |
| 176 | |
| 177 if (audio_renderer_host_) | |
| 178 audio_renderer_host_->get_audio_log()->OnError(stream_id); | |
| 179 CloseStream(stream_id); | |
| 180 } | |
| 181 | |
| 182 void AudioOutputImpl::InitAudioRendererHost() { | |
| 183 RenderProcessHost* process = frame_->GetProcess(); | |
| 184 // It's safe to use static casting of the |process| to RenderProcessHostImpl | |
| 185 // outside the tests: there is just RenderProcessHostImpl and | |
| 186 // MockRenderProcessHost that inherit from RenderProcessHost and | |
| 187 // MockRenderProcessHost is used in just some tests that doesn't involve | |
| 188 // AudioOutputImpl. It's also a common practice used in many places to access | |
| 189 // RenderProcessHostImpl hidden methods from RenderProcessHost. | |
| 190 // See https://goo.gl/tkKvkd. | |
| 191 | |
| 192 auto host = | |
| 193 static_cast<RenderProcessHostImpl*>(process)->audio_renderer_host(); | |
| 194 if (host.get()) | |
| 195 audio_renderer_host_ = host; | |
|
Henrik Grunell
2016/05/30 12:44:56
If we should surely be able to initialize the ARH
| |
| 196 } | |
| 197 | |
| 198 } // namespace content | |
| OLD | NEW |