Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/media/session/pepper_playback_observer.h" | 5 #include "content/browser/media/session/pepper_playback_observer.h" |
| 6 | 6 |
| 7 #include "base/feature_list.h" | 7 #include "base/command_line.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "content/browser/media/session/media_session_impl.h" | 9 #include "content/browser/media/session/media_session_impl.h" |
| 10 #include "content/browser/media/session/pepper_player_delegate.h" | 10 #include "content/browser/media/session/pepper_player_delegate.h" |
| 11 #include "content/common/frame_messages.h" | 11 #include "content/common/frame_messages.h" |
| 12 #include "ipc/ipc_message_macros.h" | 12 #include "ipc/ipc_message_macros.h" |
| 13 #include "media/base/media_content_type.h" | 13 #include "media/base/media_content_type.h" |
| 14 #include "media/base/media_switches.h" | 14 #include "media/base/media_switches.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 bool ShouldDuckFlash() { | 20 bool ShouldDuckFlash() { |
| 21 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 21 return base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 22 switches::kEnableDefaultMediaSession) == | 22 switches::kEnableDefaultMediaSession) == |
| 23 switches::kEnableDefaultMediaSessionDuckFlash; | 23 switches::kEnableDefaultMediaSessionDuckFlash; |
| 24 } | 24 } |
| 25 | 25 |
| 26 } // anonymous namespace | 26 } // anonymous namespace |
| 27 | 27 |
| 28 PepperPlaybackObserver::PepperPlaybackObserver(WebContentsImpl *contents) | 28 PepperPlaybackObserver::PepperPlaybackObserver(WebContents* contents) |
| 29 : contents_(contents) {} | 29 : contents_(contents) {} |
| 30 | 30 |
| 31 PepperPlaybackObserver::~PepperPlaybackObserver() { | 31 PepperPlaybackObserver::~PepperPlaybackObserver() { |
| 32 // At this point WebContents is being destructed, so it's safe to | 32 // At this point WebContents is being destructed, so it's safe to |
| 33 // call this. MediaSession may decide to send further IPC messages | 33 // call this. MediaSession may decide to send further IPC messages |
| 34 // through PepperPlayerDelegates, which might be declined if the | 34 // through PepperPlayerDelegates, which might be declined if the |
| 35 // RenderViewHost has been destroyed. | 35 // RenderViewHost has been destroyed. |
| 36 for (PlayersMap::iterator iter = players_map_.begin(); | 36 std::vector<PlayerId> players_to_remove; |
| 37 iter != players_map_.end();) { | 37 for (const auto& player_played_sound : players_played_sound_map_) |
| 38 MediaSessionImpl::Get(contents_)->RemovePlayer( | 38 players_to_remove.push_back(player_played_sound.first); |
| 39 iter->second.get(), PepperPlayerDelegate::kPlayerId); | 39 |
| 40 iter = players_map_.erase(iter); | 40 for (const auto& player_id : players_to_remove) |
| 41 } | 41 PepperInstanceDeleted(player_id.first, player_id.second); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void PepperPlaybackObserver::PepperInstanceCreated(int32_t pp_instance) { | 44 void PepperPlaybackObserver::RenderFrameDeleted( |
| 45 players_played_sound_map_[pp_instance] = false; | 45 RenderFrameHost* render_frame_host) { |
| 46 std::vector<PlayerId> players_to_remove; | |
| 47 for (const auto& player_played_sound : players_played_sound_map_) { | |
| 48 if (player_played_sound.first.first == render_frame_host) | |
| 49 players_to_remove.push_back(player_played_sound.first); | |
|
whywhat
2016/12/07 18:09:25
nit: I think you could use the erase(it++) idiom h
Zhiqiang Zhang (Slow)
2016/12/07 19:40:23
Thanks! Done.
| |
| 50 } | |
| 51 | |
| 52 for (const auto& player_id : players_to_remove) | |
| 53 PepperInstanceDeleted(player_id.first, player_id.second); | |
| 46 } | 54 } |
| 47 | 55 |
| 48 void PepperPlaybackObserver::PepperInstanceDeleted(int32_t pp_instance) { | 56 void PepperPlaybackObserver::PepperInstanceCreated( |
| 49 UMA_HISTOGRAM_BOOLEAN("Media.Pepper.PlayedSound", | 57 RenderFrameHost* render_frame_host, int32_t pp_instance) { |
| 50 players_played_sound_map_[pp_instance]); | 58 PlayerId id(render_frame_host, pp_instance); |
| 51 players_played_sound_map_.erase(pp_instance); | 59 players_played_sound_map_[id] = false; |
| 52 | |
| 53 PepperStopsPlayback(pp_instance); | |
| 54 } | 60 } |
| 55 | 61 |
| 56 void PepperPlaybackObserver::PepperStartsPlayback(int32_t pp_instance) { | 62 void PepperPlaybackObserver::PepperInstanceDeleted( |
| 57 players_played_sound_map_[pp_instance] = true; | 63 RenderFrameHost* render_frame_host, int32_t pp_instance) { |
| 64 PlayerId id(render_frame_host, pp_instance); | |
| 58 | 65 |
| 59 if (players_map_.count(pp_instance)) | 66 auto iter = players_played_sound_map_.find(id); |
| 67 if (iter == players_played_sound_map_.end()) | |
| 60 return; | 68 return; |
| 61 | 69 |
| 62 players_map_[pp_instance].reset(new PepperPlayerDelegate( | 70 UMA_HISTOGRAM_BOOLEAN("Media.Pepper.PlayedSound", iter->second); |
| 63 contents_, pp_instance)); | 71 players_played_sound_map_.erase(iter); |
| 72 | |
| 73 PepperStopsPlayback(render_frame_host, pp_instance); | |
| 74 } | |
| 75 | |
| 76 void PepperPlaybackObserver::PepperStartsPlayback( | |
| 77 RenderFrameHost* render_frame_host, int32_t pp_instance) { | |
| 78 PlayerId id(render_frame_host, pp_instance); | |
| 79 | |
| 80 players_played_sound_map_[id] = true; | |
| 81 | |
| 82 if (players_map_.count(id)) | |
| 83 return; | |
| 84 | |
| 85 players_map_[id].reset(new PepperPlayerDelegate( | |
| 86 render_frame_host, pp_instance)); | |
| 64 | 87 |
| 65 MediaSessionImpl::Get(contents_)->AddPlayer( | 88 MediaSessionImpl::Get(contents_)->AddPlayer( |
| 66 players_map_[pp_instance].get(), PepperPlayerDelegate::kPlayerId, | 89 players_map_[id].get(), PepperPlayerDelegate::kPlayerId, |
| 67 ShouldDuckFlash() ? media::MediaContentType::Pepper | 90 ShouldDuckFlash() ? media::MediaContentType::Pepper |
| 68 : media::MediaContentType::OneShot); | 91 : media::MediaContentType::OneShot); |
| 69 } | 92 } |
| 70 | 93 |
| 71 void PepperPlaybackObserver::PepperStopsPlayback(int32_t pp_instance) { | 94 void PepperPlaybackObserver::PepperStopsPlayback( |
| 72 if (!players_map_.count(pp_instance)) | 95 RenderFrameHost* render_frame_host, int32_t pp_instance) { |
| 96 PlayerId id(render_frame_host, pp_instance); | |
| 97 | |
| 98 if (!players_map_.count(id)) | |
| 73 return; | 99 return; |
| 74 | 100 |
| 75 MediaSessionImpl::Get(contents_)->RemovePlayer( | 101 MediaSessionImpl::Get(contents_)->RemovePlayer( |
| 76 players_map_[pp_instance].get(), PepperPlayerDelegate::kPlayerId); | 102 players_map_[id].get(), PepperPlayerDelegate::kPlayerId); |
| 77 | 103 |
| 78 players_map_.erase(pp_instance); | 104 players_map_.erase(id); |
| 79 } | 105 } |
| 80 | 106 |
| 81 } // namespace content | 107 } // namespace content |
| OLD | NEW |