| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/process.h" | 10 #include "base/process.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 scoped_ptr<media::AudioOutputController::SyncReader> reader); | 35 scoped_ptr<media::AudioOutputController::SyncReader> reader); |
| 36 virtual ~AudioEntry(); | 36 virtual ~AudioEntry(); |
| 37 | 37 |
| 38 int stream_id() const { | 38 int stream_id() const { |
| 39 return stream_id_; | 39 return stream_id_; |
| 40 } | 40 } |
| 41 | 41 |
| 42 int render_view_id() const { | 42 int render_view_id() const { |
| 43 return render_view_id_; | 43 return render_view_id_; |
| 44 } | 44 } |
| 45 // TODO(miu): Remove this setter once my IPC clean-up change (in code review) | |
| 46 // lands! | |
| 47 void set_render_view_id(int render_view_id) { | |
| 48 render_view_id_ = render_view_id; | |
| 49 } | |
| 50 | 45 |
| 51 media::AudioOutputController* controller() const { | 46 media::AudioOutputController* controller() const { |
| 52 return controller_; | 47 return controller_; |
| 53 } | 48 } |
| 54 | 49 |
| 55 base::SharedMemory* shared_memory() { | 50 base::SharedMemory* shared_memory() { |
| 56 return shared_memory_.get(); | 51 return shared_memory_.get(); |
| 57 } | 52 } |
| 58 | 53 |
| 59 media::AudioOutputController::SyncReader* reader() const { | 54 media::AudioOutputController::SyncReader* reader() const { |
| 60 return reader_.get(); | 55 return reader_.get(); |
| 61 } | 56 } |
| 62 | 57 |
| 63 private: | 58 private: |
| 64 // media::AudioOutputController::EventHandler implementation. | 59 // media::AudioOutputController::EventHandler implementation. |
| 65 virtual void OnCreated() OVERRIDE; | 60 virtual void OnCreated() OVERRIDE; |
| 66 virtual void OnPlaying() OVERRIDE; | 61 virtual void OnPlaying() OVERRIDE; |
| 67 virtual void OnAudible(bool is_audible) OVERRIDE; | 62 virtual void OnAudible(bool is_audible) OVERRIDE; |
| 68 virtual void OnPaused() OVERRIDE; | 63 virtual void OnPaused() OVERRIDE; |
| 69 virtual void OnError() OVERRIDE; | 64 virtual void OnError() OVERRIDE; |
| 70 virtual void OnDeviceChange(int new_buffer_size, int new_sample_rate) | 65 virtual void OnDeviceChange(int new_buffer_size, int new_sample_rate) |
| 71 OVERRIDE; | 66 OVERRIDE; |
| 72 | 67 |
| 73 AudioRendererHost* const host_; | 68 AudioRendererHost* const host_; |
| 74 const int stream_id_; | 69 const int stream_id_; |
| 75 | 70 |
| 76 // The routing ID of the source render view. | 71 // The routing ID of the source render view. |
| 77 int render_view_id_; | 72 const int render_view_id_; |
| 78 | 73 |
| 79 // The AudioOutputController that manages the audio stream. | 74 // The AudioOutputController that manages the audio stream. |
| 80 const scoped_refptr<media::AudioOutputController> controller_; | 75 const scoped_refptr<media::AudioOutputController> controller_; |
| 81 | 76 |
| 82 // Shared memory for transmission of the audio data. | 77 // Shared memory for transmission of the audio data. |
| 83 const scoped_ptr<base::SharedMemory> shared_memory_; | 78 const scoped_ptr<base::SharedMemory> shared_memory_; |
| 84 | 79 |
| 85 // The synchronous reader to be used by the controller. | 80 // The synchronous reader to be used by the controller. |
| 86 const scoped_ptr<media::AudioOutputController::SyncReader> reader_; | 81 const scoped_ptr<media::AudioOutputController::SyncReader> reader_; |
| 87 }; | 82 }; |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 250 } | 245 } |
| 251 } | 246 } |
| 252 | 247 |
| 253 /////////////////////////////////////////////////////////////////////////////// | 248 /////////////////////////////////////////////////////////////////////////////// |
| 254 // IPC Messages handler | 249 // IPC Messages handler |
| 255 bool AudioRendererHost::OnMessageReceived(const IPC::Message& message, | 250 bool AudioRendererHost::OnMessageReceived(const IPC::Message& message, |
| 256 bool* message_was_ok) { | 251 bool* message_was_ok) { |
| 257 bool handled = true; | 252 bool handled = true; |
| 258 IPC_BEGIN_MESSAGE_MAP_EX(AudioRendererHost, message, *message_was_ok) | 253 IPC_BEGIN_MESSAGE_MAP_EX(AudioRendererHost, message, *message_was_ok) |
| 259 IPC_MESSAGE_HANDLER(AudioHostMsg_CreateStream, OnCreateStream) | 254 IPC_MESSAGE_HANDLER(AudioHostMsg_CreateStream, OnCreateStream) |
| 260 IPC_MESSAGE_HANDLER(AudioHostMsg_AssociateStreamWithProducer, | |
| 261 OnAssociateStreamWithProducer) | |
| 262 IPC_MESSAGE_HANDLER(AudioHostMsg_PlayStream, OnPlayStream) | 255 IPC_MESSAGE_HANDLER(AudioHostMsg_PlayStream, OnPlayStream) |
| 263 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) | 256 IPC_MESSAGE_HANDLER(AudioHostMsg_PauseStream, OnPauseStream) |
| 264 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) | 257 IPC_MESSAGE_HANDLER(AudioHostMsg_CloseStream, OnCloseStream) |
| 265 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) | 258 IPC_MESSAGE_HANDLER(AudioHostMsg_SetVolume, OnSetVolume) |
| 266 IPC_MESSAGE_UNHANDLED(handled = false) | 259 IPC_MESSAGE_UNHANDLED(handled = false) |
| 267 IPC_END_MESSAGE_MAP_EX() | 260 IPC_END_MESSAGE_MAP_EX() |
| 268 | 261 |
| 269 return handled; | 262 return handled; |
| 270 } | 263 } |
| 271 | 264 |
| 272 void AudioRendererHost::OnCreateStream( | 265 void AudioRendererHost::OnCreateStream( |
| 273 int stream_id, const media::AudioParameters& params) { | 266 int stream_id, int render_view_id, const media::AudioParameters& params) { |
| 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 268 |
| 269 DVLOG(1) << "AudioRendererHost@" << this |
| 270 << "::OnCreateStream(stream_id=" << stream_id |
| 271 << ", render_view_id=" << render_view_id << ")"; |
| 272 DCHECK_GT(render_view_id, 0); |
| 273 |
| 275 // media::AudioParameters is validated in the deserializer. | 274 // media::AudioParameters is validated in the deserializer. |
| 276 int input_channels = params.input_channels(); | 275 int input_channels = params.input_channels(); |
| 277 if (input_channels < 0 || | 276 if (input_channels < 0 || |
| 278 input_channels > media::limits::kMaxChannels || | 277 input_channels > media::limits::kMaxChannels || |
| 279 LookupById(stream_id) != NULL) { | 278 LookupById(stream_id) != NULL) { |
| 280 SendErrorMessage(stream_id); | 279 SendErrorMessage(stream_id); |
| 281 return; | 280 return; |
| 282 } | 281 } |
| 283 | 282 |
| 284 // Calculate output and input memory size. | 283 // Calculate output and input memory size. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 299 return; | 298 return; |
| 300 } | 299 } |
| 301 | 300 |
| 302 scoped_ptr<AudioSyncReader> reader( | 301 scoped_ptr<AudioSyncReader> reader( |
| 303 new AudioSyncReader(shared_memory.get(), params, input_channels)); | 302 new AudioSyncReader(shared_memory.get(), params, input_channels)); |
| 304 if (!reader->Init()) { | 303 if (!reader->Init()) { |
| 305 SendErrorMessage(stream_id); | 304 SendErrorMessage(stream_id); |
| 306 return; | 305 return; |
| 307 } | 306 } |
| 308 | 307 |
| 309 audio_entries_.insert(std::make_pair(stream_id, new AudioEntry( | 308 scoped_ptr<AudioEntry> entry(new AudioEntry( |
| 310 this, stream_id, MSG_ROUTING_NONE, params, shared_memory.Pass(), | 309 this, stream_id, render_view_id, params, shared_memory.Pass(), |
| 311 reader.PassAs<media::AudioOutputController::SyncReader>()))); | 310 reader.PassAs<media::AudioOutputController::SyncReader>())); |
| 312 | 311 if (mirroring_manager_) { |
| 312 mirroring_manager_->AddDiverter( |
| 313 render_process_id_, entry->render_view_id(), entry->controller()); |
| 314 } |
| 315 audio_entries_.insert(std::make_pair(stream_id, entry.release())); |
| 313 if (media_internals_) | 316 if (media_internals_) |
| 314 media_internals_->OnSetAudioStreamStatus(this, stream_id, "created"); | 317 media_internals_->OnSetAudioStreamStatus(this, stream_id, "created"); |
| 315 } | 318 } |
| 316 | 319 |
| 317 void AudioRendererHost::OnAssociateStreamWithProducer(int stream_id, | |
| 318 int render_view_id) { | |
| 319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 320 | |
| 321 DVLOG(1) << "AudioRendererHost@" << this | |
| 322 << "::OnAssociateStreamWithProducer(stream_id=" << stream_id | |
| 323 << ", render_view_id=" << render_view_id << ")"; | |
| 324 | |
| 325 AudioEntry* const entry = LookupById(stream_id); | |
| 326 if (!entry) { | |
| 327 SendErrorMessage(stream_id); | |
| 328 return; | |
| 329 } | |
| 330 | |
| 331 if (entry->render_view_id() == render_view_id) | |
| 332 return; | |
| 333 | |
| 334 // TODO(miu): Merge "AssociateWithProducer" message into "CreateStream" | |
| 335 // message so AudioRendererHost can assume a simpler "render_view_id is set | |
| 336 // once" scheme. http://crbug.com/166779 | |
| 337 if (mirroring_manager_) { | |
| 338 mirroring_manager_->RemoveDiverter( | |
| 339 render_process_id_, entry->render_view_id(), entry->controller()); | |
| 340 } | |
| 341 entry->set_render_view_id(render_view_id); | |
| 342 if (mirroring_manager_) { | |
| 343 mirroring_manager_->AddDiverter( | |
| 344 render_process_id_, entry->render_view_id(), entry->controller()); | |
| 345 } | |
| 346 } | |
| 347 | |
| 348 void AudioRendererHost::OnPlayStream(int stream_id) { | 320 void AudioRendererHost::OnPlayStream(int stream_id) { |
| 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 350 | 322 |
| 351 AudioEntry* entry = LookupById(stream_id); | 323 AudioEntry* entry = LookupById(stream_id); |
| 352 if (!entry) { | 324 if (!entry) { |
| 353 SendErrorMessage(stream_id); | 325 SendErrorMessage(stream_id); |
| 354 return; | 326 return; |
| 355 } | 327 } |
| 356 | 328 |
| 357 entry->controller()->Play(); | 329 entry->controller()->Play(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 } | 426 } |
| 455 | 427 |
| 456 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { | 428 AudioRendererHost::AudioEntry* AudioRendererHost::LookupById(int stream_id) { |
| 457 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 458 | 430 |
| 459 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); | 431 AudioEntryMap::const_iterator i = audio_entries_.find(stream_id); |
| 460 return i != audio_entries_.end() ? i->second : NULL; | 432 return i != audio_entries_.end() ? i->second : NULL; |
| 461 } | 433 } |
| 462 | 434 |
| 463 } // namespace content | 435 } // namespace content |
| OLD | NEW |