| 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/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/shared_memory.h" | 14 #include "base/memory/shared_memory.h" |
| 15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 16 #include "base/process/process.h" | 16 #include "base/process/process.h" |
| 17 #include "content/browser/bad_message.h" | 17 #include "content/browser/bad_message.h" |
| 18 #include "content/browser/browser_main_loop.h" | 18 #include "content/browser/browser_main_loop.h" |
| 19 #include "content/browser/child_process_security_policy_impl.h" | 19 #include "content/browser/child_process_security_policy_impl.h" |
| 20 #include "content/browser/media/audio_stream_monitor.h" | 20 #include "content/browser/media/audio_stream_monitor.h" |
| 21 #include "content/browser/media/capture/audio_mirroring_manager.h" | 21 #include "content/browser/media/capture/audio_mirroring_manager.h" |
| 22 #include "content/browser/media/media_internals.h" | 22 #include "content/browser/media/media_internals.h" |
| 23 #include "content/browser/media/media_web_contents_observer.h" |
| 23 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 24 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 24 #include "content/browser/renderer_host/media/audio_sync_reader.h" | 25 #include "content/browser/renderer_host/media/audio_sync_reader.h" |
| 25 #include "content/browser/renderer_host/media/media_stream_manager.h" | 26 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 26 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 27 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 27 #include "content/browser/renderer_host/render_widget_host_impl.h" | 28 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 28 #include "content/common/media/audio_messages.h" | 29 #include "content/common/media/audio_messages.h" |
| 29 #include "content/public/browser/content_browser_client.h" | 30 #include "content/public/browser/content_browser_client.h" |
| 30 #include "content/public/browser/media_device_id.h" | 31 #include "content/public/browser/media_device_id.h" |
| 31 #include "content/public/browser/media_observer.h" | 32 #include "content/public/browser/media_observer.h" |
| 32 #include "content/public/browser/render_frame_host.h" | 33 #include "content/public/browser/render_frame_host.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 base::SharedMemory* shared_memory() { | 153 base::SharedMemory* shared_memory() { |
| 153 return shared_memory_.get(); | 154 return shared_memory_.get(); |
| 154 } | 155 } |
| 155 | 156 |
| 156 media::AudioOutputController::SyncReader* reader() const { | 157 media::AudioOutputController::SyncReader* reader() const { |
| 157 return reader_.get(); | 158 return reader_.get(); |
| 158 } | 159 } |
| 159 | 160 |
| 160 bool playing() const { return playing_; } | 161 bool playing() const { return playing_; } |
| 161 void set_playing(bool playing) { playing_ = playing; } | 162 void set_playing(bool playing) { playing_ = playing; } |
| 163 void update_last_pause_time() { last_pause_time_ = base::TimeTicks::Now(); } |
| 164 base::TimeTicks last_pause_time() { return last_pause_time_; } |
| 162 | 165 |
| 163 private: | 166 private: |
| 164 // media::AudioOutputController::EventHandler implementation. | 167 // media::AudioOutputController::EventHandler implementation. |
| 165 void OnCreated() override; | 168 void OnCreated() override; |
| 166 void OnPlaying() override; | 169 void OnPlaying() override; |
| 167 void OnPaused() override; | 170 void OnPaused() override; |
| 168 void OnError() override; | 171 void OnError() override; |
| 169 | 172 |
| 170 AudioRendererHost* const host_; | 173 AudioRendererHost* const host_; |
| 171 const int stream_id_; | 174 const int stream_id_; |
| 172 | 175 |
| 173 // The routing ID of the source RenderFrame. | 176 // The routing ID of the source RenderFrame. |
| 174 const int render_frame_id_; | 177 const int render_frame_id_; |
| 175 | 178 |
| 176 // Shared memory for transmission of the audio data. Used by |reader_|. | 179 // Shared memory for transmission of the audio data. Used by |reader_|. |
| 177 const scoped_ptr<base::SharedMemory> shared_memory_; | 180 const scoped_ptr<base::SharedMemory> shared_memory_; |
| 178 | 181 |
| 179 // The synchronous reader to be used by |controller_|. | 182 // The synchronous reader to be used by |controller_|. |
| 180 const scoped_ptr<media::AudioOutputController::SyncReader> reader_; | 183 const scoped_ptr<media::AudioOutputController::SyncReader> reader_; |
| 181 | 184 |
| 182 // The AudioOutputController that manages the audio stream. | 185 // The AudioOutputController that manages the audio stream. |
| 183 const scoped_refptr<media::AudioOutputController> controller_; | 186 const scoped_refptr<media::AudioOutputController> controller_; |
| 184 | 187 |
| 185 bool playing_; | 188 bool playing_; |
| 189 |
| 190 base::TimeTicks last_pause_time_; |
| 186 }; | 191 }; |
| 187 | 192 |
| 188 AudioRendererHost::AudioEntry::AudioEntry( | 193 AudioRendererHost::AudioEntry::AudioEntry( |
| 189 AudioRendererHost* host, | 194 AudioRendererHost* host, |
| 190 int stream_id, | 195 int stream_id, |
| 191 int render_frame_id, | 196 int render_frame_id, |
| 192 const media::AudioParameters& params, | 197 const media::AudioParameters& params, |
| 193 const std::string& output_device_id, | 198 const std::string& output_device_id, |
| 194 scoped_ptr<base::SharedMemory> shared_memory, | 199 scoped_ptr<base::SharedMemory> shared_memory, |
| 195 scoped_ptr<media::AudioOutputController::SyncReader> reader) | 200 scoped_ptr<media::AudioOutputController::SyncReader> reader) |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 | 612 |
| 608 void AudioRendererHost::OnPlayStream(int stream_id) { | 613 void AudioRendererHost::OnPlayStream(int stream_id) { |
| 609 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 614 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 610 | 615 |
| 611 AudioEntry* entry = LookupById(stream_id); | 616 AudioEntry* entry = LookupById(stream_id); |
| 612 if (!entry) { | 617 if (!entry) { |
| 613 SendErrorMessage(stream_id); | 618 SendErrorMessage(stream_id); |
| 614 return; | 619 return; |
| 615 } | 620 } |
| 616 | 621 |
| 622 if (entry->controller()->GetAudioParameters().effects() & |
| 623 media::AudioParameters::FOCUSABLE) { |
| 624 MediaWebContentsObserver::WaitForAudioFocusAsync( |
| 625 render_process_id_, entry->render_frame_id(), |
| 626 base::Bind(&AudioRendererHost::OnFocusAvailable, this, stream_id, |
| 627 base::TimeTicks::Now())); |
| 628 } else { |
| 629 entry->controller()->Play(); |
| 630 audio_log_->OnStarted(stream_id); |
| 631 } |
| 632 } |
| 633 |
| 634 void AudioRendererHost::OnFocusAvailable(int stream_id, |
| 635 base::TimeTicks play_time) { |
| 636 // Abort the playback if: |
| 637 // - The entry has been destroyed while focus was requested. |
| 638 // - A subsequent pause has come through. |
| 639 // |
| 640 // A focus request never fails via this path. If it's denied a pause request |
| 641 // will be issues directly to the player. |
| 642 AudioEntry* entry = LookupById(stream_id); |
| 643 if (!entry || play_time < entry->last_pause_time()) |
| 644 return; |
| 645 |
| 617 entry->controller()->Play(); | 646 entry->controller()->Play(); |
| 618 audio_log_->OnStarted(stream_id); | 647 audio_log_->OnStarted(stream_id); |
| 619 } | 648 } |
| 620 | 649 |
| 621 void AudioRendererHost::OnPauseStream(int stream_id) { | 650 void AudioRendererHost::OnPauseStream(int stream_id) { |
| 622 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 651 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 623 | 652 |
| 624 AudioEntry* entry = LookupById(stream_id); | 653 AudioEntry* entry = LookupById(stream_id); |
| 625 if (!entry) { | 654 if (!entry) { |
| 626 SendErrorMessage(stream_id); | 655 SendErrorMessage(stream_id); |
| 627 return; | 656 return; |
| 628 } | 657 } |
| 629 | 658 |
| 659 entry->update_last_pause_time(); |
| 630 entry->controller()->Pause(); | 660 entry->controller()->Pause(); |
| 631 audio_log_->OnStopped(stream_id); | 661 audio_log_->OnStopped(stream_id); |
| 632 } | 662 } |
| 633 | 663 |
| 634 void AudioRendererHost::OnSetVolume(int stream_id, double volume) { | 664 void AudioRendererHost::OnSetVolume(int stream_id, double volume) { |
| 635 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 665 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 636 | 666 |
| 637 AudioEntry* entry = LookupById(stream_id); | 667 AudioEntry* entry = LookupById(stream_id); |
| 638 if (!entry) { | 668 if (!entry) { |
| 639 SendErrorMessage(stream_id); | 669 SendErrorMessage(stream_id); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 823 callback.Run(false, device_info); | 853 callback.Run(false, device_info); |
| 824 } | 854 } |
| 825 | 855 |
| 826 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { | 856 bool AudioRendererHost::IsAuthorizationStarted(int stream_id) { |
| 827 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 857 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 828 const auto& i = authorizations_.find(stream_id); | 858 const auto& i = authorizations_.find(stream_id); |
| 829 return i != authorizations_.end(); | 859 return i != authorizations_.end(); |
| 830 } | 860 } |
| 831 | 861 |
| 832 } // namespace content | 862 } // namespace content |
| OLD | NEW |