Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/media/audio_renderer_host.h" | 5 #include "content/browser/renderer_host/media/audio_renderer_host.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/process/process.h" | 15 #include "base/process/process.h" |
| 16 #include "content/browser/bad_message.h" | 16 #include "content/browser/bad_message.h" |
| 17 #include "content/browser/browser_main_loop.h" | 17 #include "content/browser/browser_main_loop.h" |
| 18 #include "content/browser/media/audio_stream_monitor.h" | 18 #include "content/browser/media/audio_stream_monitor.h" |
| 19 #include "content/browser/media/capture/audio_mirroring_manager.h" | 19 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 20 #include "content/browser/media/media_internals.h" | 20 #include "content/browser/media/media_internals.h" |
| 21 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 21 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 22 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 22 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 23 #include "content/browser/renderer_host/media/media_stream_manager.h" | 23 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 24 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 24 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 25 #include "content/browser/renderer_host/render_widget_host_impl.h" | 25 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 26 #include "content/common/media/audio_messages.h" | |
| 27 #include "content/public/browser/content_browser_client.h" | 26 #include "content/public/browser/content_browser_client.h" |
| 28 #include "content/public/browser/media_device_id.h" | 27 #include "content/public/browser/media_device_id.h" |
| 29 #include "content/public/browser/media_observer.h" | 28 #include "content/public/browser/media_observer.h" |
| 30 #include "content/public/browser/render_frame_host.h" | 29 #include "content/public/browser/render_frame_host.h" |
| 31 #include "content/public/common/content_switches.h" | 30 #include "content/public/common/content_switches.h" |
| 32 #include "media/audio/audio_device_description.h" | 31 #include "media/audio/audio_device_description.h" |
| 33 #include "media/audio/audio_streams_tracker.h" | 32 #include "media/audio/audio_streams_tracker.h" |
| 34 #include "media/base/audio_bus.h" | 33 #include "media/base/audio_bus.h" |
| 35 #include "media/base/limits.h" | 34 #include "media/base/limits.h" |
| 35 #include "media/mojo/interfaces/audio_output.mojom-shared.h" | |
| 36 #include "media/mojo/interfaces/audio_output.mojom.h" | |
| 37 #include "mojo/edk/embedder/embedder.h" | |
| 38 #include "mojo/public/cpp/system/handle.h" | |
| 39 #include "mojo/public/cpp/system/platform_handle.h" | |
| 36 | 40 |
| 37 using media::AudioBus; | 41 using media::AudioBus; |
| 38 using media::AudioManager; | 42 using media::AudioManager; |
| 39 | 43 |
| 40 namespace content { | 44 namespace content { |
| 41 | 45 |
| 42 namespace { | 46 namespace { |
| 43 | 47 |
| 44 // Tracks the maximum number of simultaneous output streams browser-wide. | 48 // Tracks the maximum number of simultaneous output streams browser-wide. |
| 45 // Accessed on IO thread. | 49 // Accessed on IO thread. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 128 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 125 const bool frame_exists = | 129 const bool frame_exists = |
| 126 !!RenderFrameHost::FromID(render_process_id, render_frame_id); | 130 !!RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 127 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 131 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 128 base::Bind(callback, frame_exists)); | 132 base::Bind(callback, frame_exists)); |
| 129 } | 133 } |
| 130 #endif // DCHECK_IS_ON() | 134 #endif // DCHECK_IS_ON() |
| 131 | 135 |
| 132 } // namespace | 136 } // namespace |
| 133 | 137 |
| 134 class AudioRendererHost::AudioEntry | 138 class AudioRendererHost::AudioEntry |
|
Henrik Grunell
2016/09/01 15:09:07
Maybe this should be renamed, but let's settle the
| |
| 135 : public media::AudioOutputController::EventHandler { | 139 : public media::AudioOutputController::EventHandler, |
| 140 private media::mojom::AudioOutputStream { | |
| 136 public: | 141 public: |
| 137 AudioEntry(AudioRendererHost* host, | 142 AudioEntry(AudioRendererHost* host, |
| 143 CreateStreamCallback callback, | |
| 138 int stream_id, | 144 int stream_id, |
| 139 int render_frame_id, | 145 int render_frame_id, |
| 140 const media::AudioParameters& params, | 146 const media::AudioParameters& params, |
| 141 const std::string& output_device_id, | 147 const std::string& output_device_id, |
| 142 std::unique_ptr<base::SharedMemory> shared_memory, | 148 std::unique_ptr<base::SharedMemory> shared_memory, |
| 143 std::unique_ptr<media::AudioOutputController::SyncReader> reader); | 149 std::unique_ptr<media::AudioOutputController::SyncReader> reader, |
| 150 media::mojom::AudioOutputStreamClientPtr output_stream_client); | |
| 144 ~AudioEntry() override; | 151 ~AudioEntry() override; |
| 145 | 152 |
| 146 int stream_id() const { | 153 int stream_id() const { |
| 147 return stream_id_; | 154 return stream_id_; |
| 148 } | 155 } |
| 149 | 156 |
| 150 int render_frame_id() const { return render_frame_id_; } | 157 int render_frame_id() const { return render_frame_id_; } |
| 151 | 158 |
| 152 media::AudioOutputController* controller() const { return controller_.get(); } | 159 media::AudioOutputController* controller() const { return controller_.get(); } |
| 153 | 160 |
| 154 base::SharedMemory* shared_memory() { | 161 base::SharedMemory* shared_memory() { |
| 155 return shared_memory_.get(); | 162 return shared_memory_.get(); |
| 156 } | 163 } |
| 157 | 164 |
| 158 media::AudioOutputController::SyncReader* reader() const { | 165 media::AudioOutputController::SyncReader* reader() const { |
| 159 return reader_.get(); | 166 return reader_.get(); |
| 160 } | 167 } |
| 161 | 168 |
| 162 bool playing() const { return playing_; } | 169 bool playing() const { return playing_; } |
| 163 void set_playing(bool playing) { playing_ = playing; } | 170 void set_playing(bool playing) { playing_ = playing; } |
| 164 | 171 |
| 172 // media::mojom::AudioOutputStream implementation | |
| 173 void Play() override; | |
| 174 void Pause() override; | |
| 175 void SetVolume(double volume) override; | |
| 176 | |
| 177 void OnConnectionError(); | |
| 178 bool SendStreamStateResponse(media::mojom::AudioOutputStreamState state); | |
| 179 void BindRequest(media::mojom::AudioOutputStreamRequest); | |
| 180 | |
| 165 private: | 181 private: |
| 166 // media::AudioOutputController::EventHandler implementation. | 182 // media::AudioOutputController::EventHandler implementation. |
| 167 void OnCreated() override; | 183 void OnCreated() override; |
| 168 void OnPlaying() override; | 184 void OnPlaying() override; |
| 169 void OnPaused() override; | 185 void OnPaused() override; |
| 170 void OnError() override; | 186 void OnError() override; |
| 171 | 187 |
| 172 AudioRendererHost* const host_; | 188 AudioRendererHost* const host_; |
| 173 const int stream_id_; | 189 const int stream_id_; |
| 174 | 190 |
| 175 // The routing ID of the source RenderFrame. | 191 // The routing ID of the source RenderFrame. |
| 176 const int render_frame_id_; | 192 const int render_frame_id_; |
| 177 | 193 |
| 178 // Shared memory for transmission of the audio data. Used by |reader_|. | 194 // Shared memory for transmission of the audio data. Used by |reader_|. |
| 179 const std::unique_ptr<base::SharedMemory> shared_memory_; | 195 const std::unique_ptr<base::SharedMemory> shared_memory_; |
| 180 | 196 |
| 181 // The synchronous reader to be used by |controller_|. | 197 // The synchronous reader to be used by |controller_|. |
| 182 const std::unique_ptr<media::AudioOutputController::SyncReader> reader_; | 198 const std::unique_ptr<media::AudioOutputController::SyncReader> reader_; |
| 183 | 199 |
| 184 // The AudioOutputController that manages the audio stream. | 200 // The AudioOutputController that manages the audio stream. |
| 185 const scoped_refptr<media::AudioOutputController> controller_; | 201 const scoped_refptr<media::AudioOutputController> controller_; |
| 186 | 202 |
| 203 // Used for communication with the bound AudioOutputStreamClient. | |
| 204 media::mojom::AudioOutputStreamClientPtr output_stream_client; | |
|
o1ka
2016/09/02 07:31:46
output_stream_client_
| |
| 205 | |
| 206 // Binds |this| to an AudioOutputStreamPtr that is passed to the | |
| 207 // AudioOutputStreamClient (AudioOutputIPC in the renderer process). | |
| 208 std::unique_ptr<mojo::Binding<media::mojom::AudioOutputStream>> binding_; | |
| 209 | |
| 210 CreateStreamCallback callback_; | |
| 211 | |
| 187 bool playing_; | 212 bool playing_; |
| 213 | |
| 214 DISALLOW_COPY_AND_ASSIGN(AudioEntry); | |
| 188 }; | 215 }; |
| 189 | 216 |
| 190 AudioRendererHost::AudioEntry::AudioEntry( | 217 AudioRendererHost::AudioEntry::AudioEntry( |
| 191 AudioRendererHost* host, | 218 AudioRendererHost* host, |
| 219 CreateStreamCallback callback, | |
|
o1ka
2016/09/02 07:31:46
const &
| |
| 192 int stream_id, | 220 int stream_id, |
| 193 int render_frame_id, | 221 int render_frame_id, |
| 194 const media::AudioParameters& params, | 222 const media::AudioParameters& params, |
| 195 const std::string& output_device_id, | 223 const std::string& output_device_id, |
| 196 std::unique_ptr<base::SharedMemory> shared_memory, | 224 std::unique_ptr<base::SharedMemory> shared_memory, |
| 197 std::unique_ptr<media::AudioOutputController::SyncReader> reader) | 225 std::unique_ptr<media::AudioOutputController::SyncReader> reader, |
| 226 media::mojom::AudioOutputStreamClientPtr output_stream_client) | |
| 198 : host_(host), | 227 : host_(host), |
| 199 stream_id_(stream_id), | 228 stream_id_(stream_id), |
| 200 render_frame_id_(render_frame_id), | 229 render_frame_id_(render_frame_id), |
| 201 shared_memory_(std::move(shared_memory)), | 230 shared_memory_(std::move(shared_memory)), |
| 202 reader_(std::move(reader)), | 231 reader_(std::move(reader)), |
| 203 controller_(media::AudioOutputController::Create(host->audio_manager_, | 232 controller_(media::AudioOutputController::Create(host->audio_manager_, |
| 204 this, | 233 this, |
| 205 params, | 234 params, |
| 206 output_device_id, | 235 output_device_id, |
| 207 reader_.get())), | 236 reader_.get())), |
| 237 output_stream_client(std::move(output_stream_client)), | |
| 238 callback_(callback), | |
| 208 playing_(false) { | 239 playing_(false) { |
| 209 DCHECK(controller_.get()); | 240 DCHECK(controller_.get()); |
| 241 | |
| 242 if (output_stream_client.is_bound()) | |
| 243 output_stream_client.set_connection_error_handler( | |
| 244 base::Bind(&AudioEntry::OnConnectionError, base::Unretained(this))); | |
| 245 else | |
| 246 BrowserThread::PostTask( | |
| 247 BrowserThread::IO, FROM_HERE, | |
| 248 base::Bind(&AudioRendererHost::OnCloseStream, host_, stream_id_)); | |
|
o1ka
2016/09/02 07:31:46
|host_| is a raw pointer which AudioEntry does not
| |
| 249 } | |
| 250 | |
| 251 void AudioRendererHost::AudioEntry::Play() { | |
| 252 controller_->Play(); | |
| 253 host_->audio_log_->OnStarted(stream_id_); | |
| 254 } | |
| 255 | |
| 256 void AudioRendererHost::AudioEntry::Pause() { | |
| 257 controller_->Pause(); | |
| 258 host_->audio_log_->OnStopped(stream_id_); | |
| 259 } | |
| 260 | |
| 261 void AudioRendererHost::AudioEntry::SetVolume(double volume) { | |
| 262 if (volume < 0 || volume > 1.0) | |
| 263 return; | |
| 264 | |
| 265 controller_->SetVolume(volume); | |
| 266 host_->audio_log_->OnSetVolume(stream_id_, volume); | |
| 267 } | |
| 268 | |
| 269 void AudioRendererHost::AudioEntry::OnConnectionError() { | |
| 270 binding_.reset(); | |
| 271 output_stream_client.reset(); | |
| 272 | |
| 273 BrowserThread::PostTask( | |
| 274 BrowserThread::IO, FROM_HERE, | |
| 275 base::Bind(&AudioRendererHost::CloseStream, host_, stream_id_)); | |
|
o1ka
2016/09/02 07:31:46
Same as above.
AudioRendererHost should probably h
| |
| 276 } | |
| 277 | |
| 278 bool AudioRendererHost::AudioEntry::SendStreamStateResponse( | |
| 279 media::mojom::AudioOutputStreamState state) { | |
| 280 if (!output_stream_client.is_bound()) | |
| 281 return false; | |
| 282 | |
| 283 output_stream_client->OnStreamStateChange(state); | |
| 284 return true; | |
| 285 } | |
| 286 | |
| 287 void AudioRendererHost::AudioEntry::BindRequest( | |
| 288 media::mojom::AudioOutputStreamRequest request) { | |
| 289 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 290 | |
| 291 binding_.reset(new mojo::Binding<media::mojom::AudioOutputStream>( | |
| 292 this, std::move(request))); | |
| 293 binding_->set_connection_error_handler( | |
| 294 base::Bind(&AudioEntry::OnConnectionError, base::Unretained(this))); | |
| 210 } | 295 } |
| 211 | 296 |
| 212 AudioRendererHost::AudioEntry::~AudioEntry() {} | 297 AudioRendererHost::AudioEntry::~AudioEntry() {} |
| 213 | 298 |
| 214 /////////////////////////////////////////////////////////////////////////////// | 299 /////////////////////////////////////////////////////////////////////////////// |
| 215 // AudioRendererHost implementations. | 300 // AudioRendererHost implementations. |
| 216 | 301 |
| 217 AudioRendererHost::AudioRendererHost(int render_process_id, | 302 AudioRendererHost::AudioRendererHost(int render_process_id, |
| 218 media::AudioManager* audio_manager, | 303 media::AudioManager* audio_manager, |
| 219 AudioMirroringManager* mirroring_manager, | 304 AudioMirroringManager* mirroring_manager, |
| 220 MediaInternals* media_internals, | 305 MediaInternals* media_internals, |
| 221 MediaStreamManager* media_stream_manager, | 306 MediaStreamManager* media_stream_manager, |
| 222 const std::string& salt) | 307 const std::string& salt) |
| 223 : BrowserMessageFilter(AudioMsgStart), | 308 : render_process_id_(render_process_id), |
| 224 render_process_id_(render_process_id), | |
| 225 audio_manager_(audio_manager), | 309 audio_manager_(audio_manager), |
| 226 mirroring_manager_(mirroring_manager), | 310 mirroring_manager_(mirroring_manager), |
| 227 audio_log_(media_internals->CreateAudioLog( | 311 audio_log_(media_internals->CreateAudioLog( |
| 228 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER)), | 312 media::AudioLogFactory::AUDIO_OUTPUT_CONTROLLER)), |
| 229 media_stream_manager_(media_stream_manager), | 313 media_stream_manager_(media_stream_manager), |
| 230 num_playing_streams_(0), | 314 num_playing_streams_(0), |
| 231 salt_(salt), | 315 salt_(salt), |
| 232 #if DCHECK_IS_ON() | 316 #if DCHECK_IS_ON() |
| 233 validate_render_frame_id_function_(&ValidateRenderFrameId), | 317 validate_render_frame_id_function_(&ValidateRenderFrameId), |
| 234 #endif // DCHECK_IS_ON() | 318 #endif // DCHECK_IS_ON() |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 256 } | 340 } |
| 257 | 341 |
| 258 void AudioRendererHost::GetOutputControllers( | 342 void AudioRendererHost::GetOutputControllers( |
| 259 const RenderProcessHost::GetAudioOutputControllersCallback& | 343 const RenderProcessHost::GetAudioOutputControllersCallback& |
| 260 callback) const { | 344 callback) const { |
| 261 BrowserThread::PostTaskAndReplyWithResult( | 345 BrowserThread::PostTaskAndReplyWithResult( |
| 262 BrowserThread::IO, FROM_HERE, | 346 BrowserThread::IO, FROM_HERE, |
| 263 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback); | 347 base::Bind(&AudioRendererHost::DoGetOutputControllers, this), callback); |
| 264 } | 348 } |
| 265 | 349 |
| 266 void AudioRendererHost::OnChannelClosing() { | |
| 267 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 268 // Since the IPC sender is gone, close all requested audio streams. | |
| 269 while (!audio_entries_.empty()) { | |
| 270 // Note: OnCloseStream() removes the entries from audio_entries_. | |
| 271 OnCloseStream(audio_entries_.begin()->first); | |
| 272 } | |
| 273 | |
| 274 // Remove any authorizations for streams that were not yet created | |
| 275 authorizations_.clear(); | |
| 276 } | |
| 277 | |
| 278 void AudioRendererHost::OnDestruct() const { | |
| 279 BrowserThread::DeleteOnIOThread::Destruct(this); | |
| 280 } | |
| 281 | |
| 282 void AudioRendererHost::AudioEntry::OnCreated() { | 350 void AudioRendererHost::AudioEntry::OnCreated() { |
| 283 BrowserThread::PostTask( | 351 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 284 BrowserThread::IO, | 352 base::Bind(&AudioRendererHost::DoCompleteCreation, |
| 285 FROM_HERE, | 353 host_, stream_id_, callback_)); |
|
o1ka
2016/09/02 07:31:46
same as above
| |
| 286 base::Bind(&AudioRendererHost::DoCompleteCreation, host_, stream_id_)); | |
| 287 } | 354 } |
| 288 | 355 |
| 289 void AudioRendererHost::AudioEntry::OnPlaying() { | 356 void AudioRendererHost::AudioEntry::OnPlaying() { |
| 290 BrowserThread::PostTask( | 357 output_stream_client->OnStreamStateChange( |
| 291 BrowserThread::IO, | 358 ::media::mojom::AudioOutputStreamState::PLAYING); |
| 292 FROM_HERE, | |
| 293 base::Bind(&AudioRendererHost::DoNotifyStreamStateChanged, | |
| 294 host_, | |
| 295 stream_id_, | |
| 296 true)); | |
| 297 } | 359 } |
| 298 | 360 |
| 299 void AudioRendererHost::AudioEntry::OnPaused() { | 361 void AudioRendererHost::AudioEntry::OnPaused() { |
| 300 BrowserThread::PostTask( | 362 output_stream_client->OnStreamStateChange( |
| 301 BrowserThread::IO, | 363 ::media::mojom::AudioOutputStreamState::PAUSED); |
| 302 FROM_HERE, | |
| 303 base::Bind(&AudioRendererHost::DoNotifyStreamStateChanged, | |
| 304 host_, | |
| 305 stream_id_, | |
| 306 false)); | |
| 307 } | 364 } |
| 308 | 365 |
| 309 void AudioRendererHost::AudioEntry::OnError() { | 366 void AudioRendererHost::AudioEntry::OnError() { |
| 367 output_stream_client->OnStreamStateChange( | |
| 368 ::media::mojom::AudioOutputStreamState::ERROR); | |
| 310 BrowserThread::PostTask( | 369 BrowserThread::PostTask( |
| 311 BrowserThread::IO, | 370 BrowserThread::IO, FROM_HERE, |
| 312 FROM_HERE, | 371 base::Bind(&AudioRendererHost::OnCloseStream, host_, stream_id_)); |
| 313 base::Bind(&AudioRendererHost::ReportErrorAndClose, host_, stream_id_)); | |
| 314 } | 372 } |
| 315 | 373 |
| 316 void AudioRendererHost::DoCompleteCreation(int stream_id) { | 374 void AudioRendererHost::DoCompleteCreation(int stream_id, |
| 375 CreateStreamCallback callback) { | |
|
o1ka
2016/09/02 07:31:46
const &
Max Morin
2016/09/02 10:27:07
mojo gets upset if I do this. This inefficiency is
| |
| 317 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 376 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 318 | 377 |
| 319 if (!PeerHandle()) { | |
| 320 DLOG(WARNING) << "Renderer process handle is invalid."; | |
| 321 ReportErrorAndClose(stream_id); | |
| 322 return; | |
| 323 } | |
| 324 | |
| 325 AudioEntry* const entry = LookupById(stream_id); | 378 AudioEntry* const entry = LookupById(stream_id); |
| 326 if (!entry) { | 379 if (!entry) { |
| 327 ReportErrorAndClose(stream_id); | 380 ReportErrorAndClose(stream_id); |
| 328 return; | 381 return; |
| 329 } | 382 } |
| 330 | 383 |
| 331 // Once the audio stream is created then complete the creation process by | 384 media::mojom::AudioOutputStreamPtr stream_ptr; |
| 332 // mapping shared memory and sharing with the renderer process. | 385 entry->BindRequest(mojo::GetProxy(&stream_ptr)); |
| 333 base::SharedMemoryHandle foreign_memory_handle; | |
| 334 if (!entry->shared_memory()->ShareToProcess(PeerHandle(), | |
| 335 &foreign_memory_handle)) { | |
| 336 // If we failed to map and share the shared memory then close the audio | |
| 337 // stream and send an error message. | |
| 338 ReportErrorAndClose(entry->stream_id()); | |
| 339 return; | |
| 340 } | |
| 341 | 386 |
| 342 AudioSyncReader* reader = static_cast<AudioSyncReader*>(entry->reader()); | 387 AudioSyncReader* reader = static_cast<AudioSyncReader*>(entry->reader()); |
| 388 mojo::ScopedHandle socket_handle = | |
| 389 mojo::WrapPlatformFile(reader->GetSyncSocket()); | |
| 343 | 390 |
| 344 base::SyncSocket::TransitDescriptor socket_descriptor; | 391 base::SharedMemoryHandle shared_memory_handle = |
| 392 base::SharedMemory::DuplicateHandle(entry->shared_memory()->handle()); | |
| 393 if (!base::SharedMemory::IsHandleValid(shared_memory_handle)) | |
| 394 return ReportErrorAndClose(stream_id); | |
| 345 | 395 |
| 346 // If we failed to prepare the sync socket for the renderer then we fail | 396 mojo::ScopedSharedBufferHandle shared_buffer_handle = |
| 347 // the construction of audio stream. | 397 mojo::WrapSharedMemoryHandle(shared_memory_handle, |
| 348 if (!reader->PrepareForeignSocket(PeerHandle(), &socket_descriptor)) { | 398 entry->shared_memory()->requested_size(), |
| 349 ReportErrorAndClose(entry->stream_id()); | 399 false); |
| 350 return; | |
| 351 } | |
| 352 | 400 |
| 353 Send(new AudioMsg_NotifyStreamCreated( | 401 /* TODO: investigate comment. |
| 354 entry->stream_id(), foreign_memory_handle, socket_descriptor, | 402 // The socket sent from the browser to the renderer is a ForeignSocket, which |
| 355 entry->shared_memory()->requested_size())); | 403 // is a part of AudioSyncReader that is owned by AudioEntry. The socket handle |
| 404 // is closed when the owning AudioEntry destructs. With mojo, the ownership of | |
| 405 // the handle is transferred to the target process. The handle is no longer | |
| 406 // valid in the sending process, and cannot be closed there. If the socket | |
| 407 // handle is closed when the AudioEntry is deleted, an error occurs. To WAR | |
| 408 // this error, duplicate the socket and send the duplicate to the renderer. | |
| 409 #if defined(OS_WIN) | |
| 410 mojo::ScopedHandle socket_handle = | |
| 411 mojo::WrapPlatformFile(socket_descriptor); | |
| 412 #else | |
| 413 mojo::ScopedHandle socket_handle = | |
| 414 mojo::WrapPlatformFile(socket_descriptor.fd); | |
| 415 #endif | |
| 416 */ | |
| 417 | |
| 418 callback.Run(std::move(stream_ptr), std::move(shared_buffer_handle), | |
| 419 std::move(socket_handle)); | |
| 356 } | 420 } |
| 357 | 421 |
| 358 void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id, | 422 void AudioRendererHost::DoNotifyStreamStateChanged(int stream_id, |
| 359 bool is_playing) { | 423 bool is_playing) { |
| 360 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 424 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 361 | 425 |
| 362 AudioEntry* const entry = LookupById(stream_id); | 426 AudioEntry* const entry = LookupById(stream_id); |
| 363 if (!entry) | 427 if (!entry) |
| 364 return; | 428 return; |
| 365 | 429 |
| 366 Send(new AudioMsg_NotifyStreamStateChanged( | 430 entry->SendStreamStateResponse( |
| 367 stream_id, | 431 is_playing ? media::mojom::AudioOutputStreamState::PLAYING |
| 368 is_playing ? media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_PLAYING | 432 : media::mojom::AudioOutputStreamState::PAUSED); |
| 369 : media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_PAUSED)); | |
| 370 | 433 |
| 371 if (is_playing) { | 434 if (is_playing) { |
| 372 AudioStreamMonitor::StartMonitoringStream( | 435 AudioStreamMonitor::StartMonitoringStream( |
| 373 render_process_id_, | 436 render_process_id_, |
| 374 entry->render_frame_id(), | 437 entry->render_frame_id(), |
| 375 entry->stream_id(), | 438 entry->stream_id(), |
| 376 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, | 439 base::Bind(&media::AudioOutputController::ReadCurrentPowerAndClip, |
| 377 entry->controller())); | 440 entry->controller())); |
| 378 } else { | 441 } else { |
| 379 AudioStreamMonitor::StopMonitoringStream( | 442 AudioStreamMonitor::StopMonitoringStream( |
| 380 render_process_id_, entry->render_frame_id(), entry->stream_id()); | 443 render_process_id_, entry->render_frame_id(), entry->stream_id()); |
| 381 } | 444 } |
| 382 UpdateNumPlayingStreams(entry, is_playing); | 445 UpdateNumPlayingStreams(entry, is_playing); |
| 383 } | 446 } |
| 384 | 447 |
| 385 RenderProcessHost::AudioOutputControllerList | 448 RenderProcessHost::AudioOutputControllerList |
| 386 AudioRendererHost::DoGetOutputControllers() const { | 449 AudioRendererHost::DoGetOutputControllers() const { |
| 387 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 450 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 388 | 451 |
| 389 RenderProcessHost::AudioOutputControllerList controllers; | 452 RenderProcessHost::AudioOutputControllerList controllers; |
| 390 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); | 453 for (AudioEntryMap::const_iterator it = audio_entries_.begin(); |
| 391 it != audio_entries_.end(); | 454 it != audio_entries_.end(); |
| 392 ++it) { | 455 ++it) { |
| 393 controllers.push_back(it->second->controller()); | 456 controllers.push_back(it->second->controller()); |
| 394 } | 457 } |
| 395 | 458 |
| 396 return controllers; | 459 return controllers; |
| 397 } | 460 } |
| 398 | 461 |
| 399 /////////////////////////////////////////////////////////////////////////////// | 462 void AudioRendererHost::OnDeviceAuthorized( |
| 400 // IPC Messages handler | |
| 401 bool AudioRendererHost::OnMessageReceived(const IPC::Message& message) { | |
| 402 bool handled = true; | |
| 403 IPC_BEGIN_MESSAGE_MAP(AudioRendererHost, message) | |
| 404 IPC_MESSAGE_HANDLER(AudioHostMsg_RequestDeviceAuthorization, | |
| 405 OnRequestDeviceAuthorization) | |
| 406 IPC_MESSAGE_HANDLER(AudioHostMsg_CreateStream, OnCreateStream) | |
| 407 IPC_MESSAGE_HANDLER(AudioHostMsg_PlayStream, OnPlayStream) | |
| 408 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) | |
| 409 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) | |
| 410 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) | |
| 411 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 412 IPC_END_MESSAGE_MAP() | |
| 413 | |
| 414 return handled; | |
| 415 } | |
| 416 | |
| 417 void AudioRendererHost::OnRequestDeviceAuthorization( | |
| 418 int stream_id, | 463 int stream_id, |
| 419 int render_frame_id, | 464 RequestDeviceAuthorizationCallback callback, |
| 420 int session_id, | |
| 421 const std::string& device_id, | 465 const std::string& device_id, |
| 422 const url::Origin& security_origin) { | 466 const url::Origin& security_origin, |
| 423 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 467 base::TimeTicks auth_start_time, |
| 424 const base::TimeTicks auth_start_time = base::TimeTicks::Now(); | 468 bool have_access) { |
| 425 | |
| 426 DVLOG(1) << "AudioRendererHost@" << this << "::OnRequestDeviceAuthorization" | |
| 427 << "(stream_id=" << stream_id | |
| 428 << ", render_frame_id=" << render_frame_id | |
| 429 << ", session_id=" << session_id << ", device_id=" << device_id | |
| 430 << ", security_origin=" << security_origin << ")"; | |
| 431 | |
| 432 if (LookupById(stream_id) || IsAuthorizationStarted(stream_id)) | |
| 433 return; | |
| 434 | |
| 435 if (!IsValidDeviceId(device_id)) { | |
| 436 UMALogDeviceAuthorizationTime(auth_start_time); | |
| 437 Send(new AudioMsg_NotifyDeviceAuthorized( | |
| 438 stream_id, media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, | |
| 439 media::AudioParameters::UnavailableDeviceParams(), std::string())); | |
| 440 return; | |
| 441 } | |
| 442 | |
| 443 // If |session_id should be used for output device selection and such output | |
| 444 // device is found, reuse the input device permissions. | |
| 445 if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, | |
| 446 device_id)) { | |
| 447 const StreamDeviceInfo* info = | |
| 448 media_stream_manager_->audio_input_device_manager() | |
| 449 ->GetOpenedDeviceInfoById(session_id); | |
| 450 if (info) { | |
| 451 media::AudioParameters output_params( | |
| 452 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 453 static_cast<media::ChannelLayout>( | |
| 454 info->device.matched_output.channel_layout), | |
| 455 info->device.matched_output.sample_rate, 16, | |
| 456 info->device.matched_output.frames_per_buffer); | |
| 457 output_params.set_effects(info->device.matched_output.effects); | |
| 458 authorizations_.insert(MakeAuthorizationData( | |
| 459 stream_id, true, info->device.matched_output_device_id)); | |
| 460 MaybeFixAudioParameters(&output_params); | |
| 461 UMALogDeviceAuthorizationTime(auth_start_time); | |
| 462 // Hash matched device id and pass it to the renderer | |
| 463 Send(new AudioMsg_NotifyDeviceAuthorized( | |
| 464 stream_id, media::OUTPUT_DEVICE_STATUS_OK, output_params, | |
| 465 GetHMACForMediaDeviceID(salt_, security_origin, | |
| 466 info->device.matched_output_device_id))); | |
| 467 return; | |
| 468 } | |
| 469 } | |
| 470 | |
| 471 authorizations_.insert( | |
| 472 MakeAuthorizationData(stream_id, false, std::string())); | |
| 473 CheckOutputDeviceAccess( | |
| 474 render_frame_id, device_id, security_origin, | |
| 475 base::Bind(&AudioRendererHost::OnDeviceAuthorized, this, stream_id, | |
| 476 device_id, security_origin, auth_start_time)); | |
| 477 } | |
| 478 | |
| 479 void AudioRendererHost::OnDeviceAuthorized(int stream_id, | |
| 480 const std::string& device_id, | |
| 481 const url::Origin& security_origin, | |
| 482 base::TimeTicks auth_start_time, | |
| 483 bool have_access) { | |
| 484 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 469 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 485 const auto& auth_data = authorizations_.find(stream_id); | 470 const auto& auth_data = authorizations_.find(stream_id); |
| 486 | 471 |
| 487 // A close request was received while access check was in progress. | 472 // A close request was received while access check was in progress. |
| 488 if (auth_data == authorizations_.end()) { | 473 if (auth_data == authorizations_.end()) { |
| 489 UMALogDeviceAuthorizationTime(auth_start_time); | 474 UMALogDeviceAuthorizationTime(auth_start_time); |
| 490 return; | 475 return; |
| 491 } | 476 } |
| 492 | 477 |
| 493 if (!have_access) { | 478 if (!have_access) { |
| 494 authorizations_.erase(auth_data); | 479 authorizations_.erase(auth_data); |
| 495 UMALogDeviceAuthorizationTime(auth_start_time); | 480 UMALogDeviceAuthorizationTime(auth_start_time); |
| 496 Send(new AudioMsg_NotifyDeviceAuthorized( | 481 SendAuthorizationMessage( |
| 497 stream_id, media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, | 482 callback, stream_id, media::OUTPUT_DEVICE_STATUS_ERROR_NOT_AUTHORIZED, |
| 498 media::AudioParameters::UnavailableDeviceParams(), std::string())); | 483 media::AudioParameters::UnavailableDeviceParams(), std::string()); |
| 499 return; | 484 return; |
| 500 } | 485 } |
| 501 | 486 |
| 502 // If enumerator caching is disabled, avoid the enumeration if the default | 487 // If enumerator caching is disabled, avoid the enumeration if the default |
| 503 // device is requested, since no device ID translation is needed. | 488 // device is requested, since no device ID translation is needed. |
| 504 // If enumerator caching is enabled, it is better to use its cache, even | 489 // If enumerator caching is enabled, it is better to use its cache, even |
| 505 // for the default device. | 490 // for the default device. |
| 506 if (media::AudioDeviceDescription::IsDefaultDevice(device_id) && | 491 if (media::AudioDeviceDescription::IsDefaultDevice(device_id) && |
| 507 !media_stream_manager_->audio_output_device_enumerator() | 492 !media_stream_manager_->audio_output_device_enumerator() |
| 508 ->IsCacheEnabled()) { | 493 ->IsCacheEnabled()) { |
| 509 base::PostTaskAndReplyWithResult( | 494 base::PostTaskAndReplyWithResult( |
| 510 audio_manager_->GetTaskRunner(), FROM_HERE, | 495 audio_manager_->GetTaskRunner(), FROM_HERE, |
| 511 base::Bind(&GetDefaultDeviceInfoOnDeviceThread, audio_manager_), | 496 base::Bind(&GetDefaultDeviceInfoOnDeviceThread, audio_manager_), |
| 512 base::Bind(&AudioRendererHost::OnDeviceIDTranslated, this, stream_id, | 497 base::Bind(&AudioRendererHost::OnDeviceIDTranslated, this, stream_id, |
| 513 auth_start_time, true)); | 498 auth_start_time, callback, true)); |
| 514 } else { | 499 } else { |
| 515 media_stream_manager_->audio_output_device_enumerator()->Enumerate( | 500 media_stream_manager_->audio_output_device_enumerator()->Enumerate( |
| 516 base::Bind(&AudioRendererHost::TranslateDeviceID, this, device_id, | 501 base::Bind(&AudioRendererHost::TranslateDeviceID, this, device_id, |
| 517 security_origin, | 502 security_origin, |
| 518 base::Bind(&AudioRendererHost::OnDeviceIDTranslated, this, | 503 base::Bind(&AudioRendererHost::OnDeviceIDTranslated, this, |
| 519 stream_id, auth_start_time))); | 504 stream_id, auth_start_time, callback))); |
| 520 } | 505 } |
| 521 } | 506 } |
| 522 | 507 |
| 523 void AudioRendererHost::OnDeviceIDTranslated( | 508 void AudioRendererHost::OnDeviceIDTranslated( |
| 524 int stream_id, | 509 int stream_id, |
| 525 base::TimeTicks auth_start_time, | 510 base::TimeTicks auth_start_time, |
| 511 RequestDeviceAuthorizationCallback callback, | |
| 526 bool device_found, | 512 bool device_found, |
| 527 const AudioOutputDeviceInfo& device_info) { | 513 const AudioOutputDeviceInfo& device_info) { |
| 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 514 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 529 const auto& auth_data = authorizations_.find(stream_id); | 515 const auto& auth_data = authorizations_.find(stream_id); |
| 530 | 516 |
| 531 // A close request was received while translation was in progress | 517 // A close request was received while translation was in progress |
| 532 if (auth_data == authorizations_.end()) { | 518 if (auth_data == authorizations_.end()) { |
| 533 UMALogDeviceAuthorizationTime(auth_start_time); | 519 UMALogDeviceAuthorizationTime(auth_start_time); |
| 534 return; | 520 return; |
| 535 } | 521 } |
| 536 | 522 |
| 537 if (!device_found) { | 523 if (!device_found) { |
| 538 authorizations_.erase(auth_data); | 524 authorizations_.erase(auth_data); |
| 539 UMALogDeviceAuthorizationTime(auth_start_time); | 525 UMALogDeviceAuthorizationTime(auth_start_time); |
| 540 Send(new AudioMsg_NotifyDeviceAuthorized( | 526 SendAuthorizationMessage( |
| 541 stream_id, media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, | 527 callback, stream_id, media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, |
| 542 media::AudioParameters::UnavailableDeviceParams(), std::string())); | 528 media::AudioParameters::UnavailableDeviceParams(), std::string()); |
| 543 return; | 529 return; |
| 544 } | 530 } |
| 545 | 531 |
| 546 auth_data->second.first = true; | 532 auth_data->second.first = true; |
| 547 auth_data->second.second = device_info.unique_id; | 533 auth_data->second.second = device_info.unique_id; |
| 548 | 534 |
| 549 media::AudioParameters output_params = device_info.output_params; | 535 media::AudioParameters output_params = device_info.output_params; |
| 550 MaybeFixAudioParameters(&output_params); | 536 MaybeFixAudioParameters(&output_params); |
| 551 UMALogDeviceAuthorizationTime(auth_start_time); | 537 UMALogDeviceAuthorizationTime(auth_start_time); |
| 552 Send(new AudioMsg_NotifyDeviceAuthorized( | 538 SendAuthorizationMessage(callback, stream_id, media::OUTPUT_DEVICE_STATUS_OK, |
| 553 stream_id, media::OUTPUT_DEVICE_STATUS_OK, output_params, std::string())); | 539 output_params, std::string()); |
| 554 } | 540 } |
| 555 | 541 |
| 556 void AudioRendererHost::OnCreateStream(int stream_id, | 542 void AudioRendererHost::DoCreateStream( |
| 557 int render_frame_id, | 543 int stream_id, |
| 558 const media::AudioParameters& params) { | 544 int render_frame_id, |
| 559 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 545 const media::AudioParameters& params, |
| 560 DVLOG(1) << "AudioRendererHost@" << this << "::OnCreateStream" | 546 const std::string& device_unique_id, |
| 561 << "(stream_id=" << stream_id << ")"; | 547 media::mojom::AudioOutputStreamClientPtr client, |
| 562 | 548 CreateStreamCallback callback, |
| 563 // Determine whether to use the device_unique_id from an authorization, or an | 549 bool render_frame_id_is_valid) { |
| 564 // empty string (i.e., when no previous authorization was requested, assume | |
| 565 // default device). | |
| 566 std::string device_unique_id; | |
| 567 const auto& auth_data = authorizations_.find(stream_id); | |
| 568 if (auth_data != authorizations_.end()) { | |
| 569 CHECK(auth_data->second.first); | |
| 570 device_unique_id.swap(auth_data->second.second); | |
| 571 authorizations_.erase(auth_data); | |
| 572 } | |
| 573 | |
| 574 #if DCHECK_IS_ON() | |
| 575 // When DCHECKs are turned on, hop over to the UI thread to validate the | |
| 576 // |render_frame_id|, then continue stream creation on the IO thread. See | |
| 577 // comment at top of DoCreateStream() for further details. | |
| 578 BrowserThread::PostTask( | |
| 579 BrowserThread::UI, FROM_HERE, | |
| 580 base::Bind(validate_render_frame_id_function_, render_process_id_, | |
| 581 render_frame_id, | |
| 582 base::Bind(&AudioRendererHost::DoCreateStream, this, stream_id, | |
| 583 render_frame_id, params, device_unique_id))); | |
| 584 #else | |
| 585 DoCreateStream(stream_id, render_frame_id, params, device_unique_id, | |
| 586 render_frame_id > 0); | |
| 587 #endif // DCHECK_IS_ON() | |
| 588 } | |
| 589 | |
| 590 void AudioRendererHost::DoCreateStream(int stream_id, | |
| 591 int render_frame_id, | |
| 592 const media::AudioParameters& params, | |
| 593 const std::string& device_unique_id, | |
| 594 bool render_frame_id_is_valid) { | |
| 595 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 550 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 596 | 551 |
| 597 // Fail early if either of two sanity-checks fail: | 552 // Fail early if either of two sanity-checks fail: |
| 598 // 1. There should not yet exist an AudioEntry for the given |stream_id| | 553 // 1. There should not yet exist an AudioEntry for the given |stream_id| |
| 599 // since the renderer may not create two streams with the same ID. | 554 // since the renderer may not create two streams with the same ID. |
| 600 // 2. The render frame ID was either invalid or the render frame host it | 555 // 2. The render frame ID was either invalid or the render frame host it |
| 601 // references has shutdown before the request could be fulfilled (race | 556 // references has shutdown before the request could be fulfilled (race |
| 602 // condition). Renderers must *always* specify a valid render frame ID | 557 // condition). Renderers must *always* specify a valid render frame ID |
| 603 // for each audio output they create, as several browser-level features | 558 // for each audio output they create, as several browser-level features |
| 604 // depend on this (e.g., OOM manager, UI audio indicator, muting, audio | 559 // depend on this (e.g., OOM manager, UI audio indicator, muting, audio |
| 605 // capture). | 560 // capture). |
| 606 // Note: media::AudioParameters is validated in the deserializer, so there is | 561 // Note: media::AudioParameters is validated in the deserializer, so there is |
| 607 // no need to check that here. | 562 // no need to check that here. |
| 608 if (LookupById(stream_id)) { | 563 if (LookupById(stream_id)) { |
| 609 SendErrorMessage(stream_id); | 564 SendErrorMessage(stream_id); |
| 610 return; | 565 return; |
| 611 } | 566 } |
| 612 if (!render_frame_id_is_valid) { | 567 if (!render_frame_id_is_valid) { |
| 613 SendErrorMessage(stream_id); | 568 SendErrorMessage(stream_id); |
| 614 return; | 569 return; |
| 615 } | 570 } |
| 616 | 571 |
| 617 // Create the shared memory and share with the renderer process. | 572 // Create the shared memory and share with the renderer process. |
| 618 uint32_t shared_memory_size = sizeof(media::AudioOutputBufferParameters) + | 573 uint32_t shared_memory_size = sizeof(media::AudioOutputBufferParameters) + |
| 619 AudioBus::CalculateMemorySize(params); | 574 AudioBus::CalculateMemorySize(params); |
| 620 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | 575 std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); |
| 621 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) { | 576 if (!shared_memory->CreateAndMapAnonymous(shared_memory_size)) { |
| 577 client->OnStreamStateChange(media::mojom::AudioOutputStreamState::ERROR); | |
| 622 SendErrorMessage(stream_id); | 578 SendErrorMessage(stream_id); |
| 623 return; | 579 return; |
| 624 } | 580 } |
| 625 | 581 |
| 626 std::unique_ptr<AudioSyncReader> reader( | 582 std::unique_ptr<AudioSyncReader> reader( |
| 627 new AudioSyncReader(shared_memory.get(), params)); | 583 new AudioSyncReader(shared_memory.get(), params)); |
| 628 if (!reader->Init()) { | 584 if (!reader->Init()) { |
| 629 SendErrorMessage(stream_id); | 585 SendErrorMessage(stream_id); |
| 630 return; | 586 return; |
| 631 } | 587 } |
| 632 | 588 |
| 633 MediaObserver* const media_observer = | 589 MediaObserver* const media_observer = |
| 634 GetContentClient()->browser()->GetMediaObserver(); | 590 GetContentClient()->browser()->GetMediaObserver(); |
| 635 if (media_observer) | 591 if (media_observer) |
| 636 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id); | 592 media_observer->OnCreatingAudioStream(render_process_id_, render_frame_id); |
| 637 | 593 |
| 638 std::unique_ptr<AudioEntry> entry( | 594 std::unique_ptr<AudioEntry> entry(new AudioEntry( |
| 639 new AudioEntry(this, stream_id, render_frame_id, params, device_unique_id, | 595 this, callback, stream_id, render_frame_id, params, device_unique_id, |
| 640 std::move(shared_memory), std::move(reader))); | 596 std::move(shared_memory), std::move(reader), std::move(client))); |
| 641 if (mirroring_manager_) { | 597 if (mirroring_manager_) { |
| 642 mirroring_manager_->AddDiverter( | 598 mirroring_manager_->AddDiverter( |
| 643 render_process_id_, entry->render_frame_id(), entry->controller()); | 599 render_process_id_, entry->render_frame_id(), entry->controller()); |
| 644 } | 600 } |
| 645 audio_entries_.insert(std::make_pair(stream_id, entry.release())); | 601 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
| 646 g_audio_streams_tracker.Get().IncreaseStreamCount(); | 602 g_audio_streams_tracker.Get().IncreaseStreamCount(); |
| 647 | 603 |
| 648 audio_log_->OnCreated(stream_id, params, device_unique_id); | 604 audio_log_->OnCreated(stream_id, params, device_unique_id); |
| 649 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( | 605 MediaInternals::GetInstance()->SetWebContentsTitleForAudioLogEntry( |
| 650 stream_id, render_process_id_, render_frame_id, audio_log_.get()); | 606 stream_id, render_process_id_, render_frame_id, audio_log_.get()); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 689 } | 645 } |
| 690 | 646 |
| 691 // Make sure the volume is valid. | 647 // Make sure the volume is valid. |
| 692 if (volume < 0 || volume > 1.0) | 648 if (volume < 0 || volume > 1.0) |
| 693 return; | 649 return; |
| 694 entry->controller()->SetVolume(volume); | 650 entry->controller()->SetVolume(volume); |
| 695 audio_log_->OnSetVolume(stream_id, volume); | 651 audio_log_->OnSetVolume(stream_id, volume); |
| 696 } | 652 } |
| 697 | 653 |
| 698 void AudioRendererHost::SendErrorMessage(int stream_id) { | 654 void AudioRendererHost::SendErrorMessage(int stream_id) { |
| 699 Send(new AudioMsg_NotifyStreamStateChanged( | 655 AudioEntry* entry = LookupById(stream_id); |
|
o1ka
2016/09/02 07:31:46
I suspect it's safe to do on IO thread only.
| |
| 700 stream_id, media::AUDIO_OUTPUT_IPC_DELEGATE_STATE_ERROR)); | 656 if (entry) |
| 657 entry->SendStreamStateResponse(media::mojom::AudioOutputStreamState::ERROR); | |
| 701 } | 658 } |
| 702 | 659 |
| 703 void AudioRendererHost::OnCloseStream(int stream_id) { | 660 void AudioRendererHost::OnCloseStream(int stream_id) { |
| 704 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 661 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 705 authorizations_.erase(stream_id); | 662 authorizations_.erase(stream_id); |
| 706 | 663 |
| 707 // Prevent oustanding callbacks from attempting to close/delete the same | 664 // Prevent oustanding callbacks from attempting to close/delete the same |
| 708 // AudioEntry twice. | 665 // AudioEntry twice. |
| 709 AudioEntryMap::iterator i = audio_entries_.find(stream_id); | 666 AudioEntryMap::iterator i = audio_entries_.find(stream_id); |
| 710 if (i == audio_entries_.end()) | 667 if (i == audio_entries_.end()) |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 784 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, | 741 base::Bind(&NotifyRenderProcessHostThatAudioStateChanged, |
| 785 render_process_id_)); | 742 render_process_id_)); |
| 786 } | 743 } |
| 787 } | 744 } |
| 788 } | 745 } |
| 789 | 746 |
| 790 bool AudioRendererHost::HasActiveAudio() { | 747 bool AudioRendererHost::HasActiveAudio() { |
| 791 return !base::AtomicRefCountIsZero(&num_playing_streams_); | 748 return !base::AtomicRefCountIsZero(&num_playing_streams_); |
| 792 } | 749 } |
| 793 | 750 |
| 751 void AudioRendererHost::BindRequest(media::mojom::AudioOutputRequest request) { | |
| 752 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 753 | |
| 754 binding_.reset( | |
| 755 new mojo::Binding<media::mojom::AudioOutput>(this, std::move(request))); | |
| 756 } | |
| 757 | |
| 758 // media::mojom::AudioOutput implementation | |
| 759 void AudioRendererHost::RequestDeviceAuthorization( | |
| 760 int stream_id, | |
| 761 int render_frame_id, | |
| 762 int session_id, | |
| 763 const mojo::String& device_id, | |
| 764 const url::Origin& origin, | |
| 765 const RequestDeviceAuthorizationCallback& callback) { | |
| 766 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 767 const base::TimeTicks auth_start_time = base::TimeTicks::Now(); | |
| 768 | |
| 769 DVLOG(1) << "AudioRendererHost@" << this << "::RequestDeviceAuthorization" | |
| 770 << "(stream_id=" << stream_id | |
| 771 << ", render_frame_id=" << render_frame_id | |
| 772 << ", session_id=" << session_id << ", device_id=" << device_id | |
| 773 << ", origin=" << origin << ")"; | |
| 774 | |
| 775 if (LookupById(stream_id) || IsAuthorizationStarted(stream_id)) | |
| 776 return; | |
| 777 | |
| 778 if (!IsValidDeviceId(device_id)) { | |
| 779 UMALogDeviceAuthorizationTime(auth_start_time); | |
| 780 SendAuthorizationMessage( | |
| 781 callback, stream_id, media::OUTPUT_DEVICE_STATUS_ERROR_NOT_FOUND, | |
| 782 media::AudioParameters::UnavailableDeviceParams(), std::string()); | |
| 783 return; | |
| 784 } | |
| 785 | |
| 786 // If |session_id should be used for output device selection and such output | |
| 787 // device is found, reuse the input device permissions. | |
| 788 if (media::AudioDeviceDescription::UseSessionIdToSelectDevice(session_id, | |
| 789 device_id)) { | |
| 790 const StreamDeviceInfo* info = | |
| 791 media_stream_manager_->audio_input_device_manager() | |
| 792 ->GetOpenedDeviceInfoById(session_id); | |
| 793 if (info) { | |
| 794 media::AudioParameters output_params( | |
| 795 media::AudioParameters::AUDIO_PCM_LOW_LATENCY, | |
| 796 static_cast<media::ChannelLayout>( | |
| 797 info->device.matched_output.channel_layout), | |
| 798 info->device.matched_output.sample_rate, 16, | |
| 799 info->device.matched_output.frames_per_buffer); | |
| 800 output_params.set_effects(info->device.matched_output.effects); | |
| 801 authorizations_.insert(MakeAuthorizationData( | |
| 802 stream_id, true, info->device.matched_output_device_id)); | |
| 803 MaybeFixAudioParameters(&output_params); | |
| 804 UMALogDeviceAuthorizationTime(auth_start_time); | |
| 805 // Hash matched device id and pass it to the renderer | |
| 806 SendAuthorizationMessage( | |
| 807 callback, stream_id, media::OUTPUT_DEVICE_STATUS_OK, output_params, | |
| 808 GetHMACForMediaDeviceID(salt_, origin, | |
| 809 info->device.matched_output_device_id)); | |
| 810 return; | |
| 811 } | |
| 812 } | |
| 813 | |
| 814 authorizations_.insert( | |
| 815 MakeAuthorizationData(stream_id, false, std::string())); | |
| 816 CheckOutputDeviceAccess( | |
| 817 render_frame_id, device_id, origin, | |
| 818 base::Bind(&AudioRendererHost::OnDeviceAuthorized, this, stream_id, | |
| 819 callback, device_id, origin, auth_start_time)); | |
| 820 } | |
| 821 | |
| 822 void AudioRendererHost::CreateStream( | |
| 823 int stream_id, | |
| 824 int render_frame_id, | |
| 825 media::mojom::AudioOutputStreamClientPtr client, | |
| 826 const media::AudioParameters& params, | |
| 827 const CreateStreamCallback& callback) { | |
| 828 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 829 DVLOG(1) << "AudioRendererHost@" << this << "::CreateStream" | |
| 830 << "(stream_id=" << stream_id << ")"; | |
| 831 | |
| 832 // Determine whether to use the device_unique_id from an authorization, or an | |
| 833 // empty string (i.e., when no previous authorization was requested, assume | |
| 834 // default device). | |
| 835 std::string device_unique_id; | |
| 836 const auto& auth_data = authorizations_.find(stream_id); | |
| 837 if (auth_data != authorizations_.end()) { | |
| 838 CHECK(auth_data->second.first); | |
| 839 device_unique_id.swap(auth_data->second.second); | |
| 840 authorizations_.erase(auth_data); | |
| 841 } | |
| 842 | |
| 843 #if DCHECK_IS_ON() | |
| 844 // When DCHECKs are turned on, hop over to the UI thread to validate the | |
| 845 // |render_frame_id|, then continue stream creation on the IO thread. See | |
| 846 // comment at top of DoCreateStream() for further details. | |
| 847 BrowserThread::PostTask( | |
|
o1ka
2016/09/02 11:30:55
Beware of this bug https://bugs.chromium.org/p/chr
Max Morin
2016/09/02 11:37:30
Thanks for the heads up. Maybe we can eliminate Lo
| |
| 848 BrowserThread::UI, FROM_HERE, | |
| 849 base::Bind(validate_render_frame_id_function_, render_process_id_, | |
| 850 render_frame_id, | |
| 851 base::Bind(&AudioRendererHost::DoCreateStream, this, stream_id, | |
| 852 render_frame_id, params, device_unique_id, | |
| 853 base::Passed(&client), callback))); | |
| 854 #else | |
| 855 DoCreateStream(stream_id, render_frame_id, params, device_unique_id, | |
| 856 std::move(client), callback, render_frame_id > 0); | |
| 857 #endif // DCHECK_IS_ON() | |
| 858 } | |
| 859 | |
| 860 void AudioRendererHost::CloseStream(int stream_id) { | |
| 861 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 862 | |
| 863 OnCloseStream(stream_id); | |
| 864 } | |
| 865 | |
| 866 void AudioRendererHost::SendAuthorizationMessage( | |
| 867 RequestDeviceAuthorizationCallback callback, | |
| 868 int stream_id, | |
| 869 media::OutputDeviceStatus status, | |
| 870 const media::AudioParameters& params, | |
| 871 const std::string& matched_device_id) { | |
| 872 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 873 | |
| 874 callback.Run(status, params, matched_device_id); | |
| 875 } | |
| 876 | |
| 794 void AudioRendererHost::CheckOutputDeviceAccess( | 877 void AudioRendererHost::CheckOutputDeviceAccess( |
| 795 int render_frame_id, | 878 int render_frame_id, |
| 796 const std::string& device_id, | 879 const std::string& device_id, |
| 797 const url::Origin& security_origin, | 880 const url::Origin& security_origin, |
| 798 const OutputDeviceAccessCB& callback) { | 881 const OutputDeviceAccessCB& callback) { |
| 799 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 882 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 800 | 883 |
| 801 // Check security origin if nondefault device is requested. | 884 // Check security origin if nondefault device is requested. |
| 802 // Ignore check for default device, which is always authorized. | 885 // Ignore check for default device, which is always authorized. |
| 803 if (!media::AudioDeviceDescription::IsDefaultDevice(device_id) && | 886 if (!media::AudioDeviceDescription::IsDefaultDevice(device_id) && |
| 804 !MediaStreamManager::IsOriginAllowed(render_process_id_, | 887 !MediaStreamManager::IsOriginAllowed(render_process_id_, |
| 805 security_origin)) { | 888 security_origin)) { |
| 806 content::bad_message::ReceivedBadMessage(this, | 889 // content::bad_message::ReceivedBadMessage(this, |
| 807 bad_message::ARH_UNAUTHORIZED_URL); | 890 // bad_message::ARH_UNAUTHORIZED_URL); |
| 808 return; | 891 return; |
| 809 } | 892 } |
| 810 | 893 |
| 811 if (device_id.empty()) { | 894 if (device_id.empty()) { |
| 812 callback.Run(true); | 895 callback.Run(true); |
| 813 } else { | 896 } else { |
| 814 // Check that MediaStream device permissions have been granted, | 897 // Check that MediaStream device permissions have been granted, |
| 815 // hence the use of a MediaStreamUIProxy. | 898 // hence the use of a MediaStreamUIProxy. |
| 816 std::unique_ptr<MediaStreamUIProxy> ui_proxy = MediaStreamUIProxy::Create(); | 899 std::unique_ptr<MediaStreamUIProxy> ui_proxy = MediaStreamUIProxy::Create(); |
| 817 | 900 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 861 callback.Run(false, device_info); | 944 callback.Run(false, device_info); |
| 862 } | 945 } |
| 863 | 946 |
| 864 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { | 947 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { |
| 865 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 948 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 866 const auto& i = authorizations_.find(stream_id); | 949 const auto& i = authorizations_.find(stream_id); |
| 867 return i != authorizations_.end(); | 950 return i != authorizations_.end(); |
| 868 } | 951 } |
| 869 | 952 |
| 870 } // namespace content | 953 } // namespace content |
| OLD | NEW |