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/media_session_controller.h" | 5 #include "content/browser/media/session/media_session_controller.h" |
6 | 6 |
7 #include "content/browser/media/media_web_contents_observer.h" | 7 #include "content/browser/media/media_web_contents_observer.h" |
8 #include "content/browser/media/session/media_session.h" | 8 #include "content/browser/media/session/media_session.h" |
9 #include "content/common/media/media_player_delegate_messages.h" | 9 #include "content/common/media/media_player_delegate_messages.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/render_frame_host.h" | 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "media/base/media_content_type.h" |
12 | 13 |
13 namespace content { | 14 namespace content { |
14 | 15 |
15 MediaSessionController::MediaSessionController( | 16 MediaSessionController::MediaSessionController( |
16 const WebContentsObserver::MediaPlayerId& id, | 17 const WebContentsObserver::MediaPlayerId& id, |
17 MediaWebContentsObserver* media_web_contents_observer) | 18 MediaWebContentsObserver* media_web_contents_observer) |
18 : id_(id), | 19 : id_(id), |
19 media_web_contents_observer_(media_web_contents_observer), | 20 media_web_contents_observer_(media_web_contents_observer), |
20 media_session_( | 21 media_session_( |
21 MediaSession::Get(media_web_contents_observer_->web_contents())) {} | 22 MediaSession::Get(media_web_contents_observer_->web_contents())) {} |
22 | 23 |
23 MediaSessionController::~MediaSessionController() { | 24 MediaSessionController::~MediaSessionController() { |
24 if (!has_session_) | 25 if (!has_session_) |
25 return; | 26 return; |
26 media_session_->RemovePlayer(this, player_id_); | 27 media_session_->RemovePlayer(this, player_id_); |
27 } | 28 } |
28 | 29 |
29 bool MediaSessionController::Initialize(bool has_audio, | 30 bool MediaSessionController::Initialize( |
30 bool is_remote, | 31 bool has_audio, bool is_remote, |
31 base::TimeDelta duration) { | 32 media::MediaContentType media_content_type) { |
32 // Don't generate a new id if one has already been set. | 33 // Don't generate a new id if one has already been set. |
33 if (!has_session_) { | 34 if (!has_session_) { |
34 // These objects are only created on the UI thread, so this is safe. | 35 // These objects are only created on the UI thread, so this is safe. |
35 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 36 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
36 static uint32_t player_id = 0; | 37 static uint32_t player_id = 0; |
37 player_id_ = static_cast<int>(player_id++); | 38 player_id_ = static_cast<int>(player_id++); |
38 } else { | 39 } else { |
39 // WebMediaPlayerAndroid does not have an accurate sense of audio presence, | 40 // WebMediaPlayerAndroid does not have an accurate sense of audio presence, |
40 // only the MediaPlayerManager does, so WMPA never reports audio unless it's | 41 // only the MediaPlayerManager does, so WMPA never reports audio unless it's |
41 // sure (no video stream). This leads to issues when Initialize() is called | 42 // sure (no video stream). This leads to issues when Initialize() is called |
(...skipping 11 matching lines...) Expand all Loading... |
53 // Don't bother with a MediaSession for remote players or without audio. If | 54 // Don't bother with a MediaSession for remote players or without audio. If |
54 // we already have a session from a previous call, release it. | 55 // we already have a session from a previous call, release it. |
55 if (!has_audio || is_remote) { | 56 if (!has_audio || is_remote) { |
56 if (has_session_) { | 57 if (has_session_) { |
57 has_session_ = false; | 58 has_session_ = false; |
58 media_session_->RemovePlayer(this, player_id_); | 59 media_session_->RemovePlayer(this, player_id_); |
59 } | 60 } |
60 return true; | 61 return true; |
61 } | 62 } |
62 | 63 |
63 const MediaSession::Type media_session_type = | |
64 (duration.is_zero() || | |
65 duration > base::TimeDelta::FromSeconds(kMinimumDurationForContentSecs)) | |
66 ? MediaSession::Type::Content | |
67 : MediaSession::Type::Transient; | |
68 | |
69 // If a session can't be created, force a pause immediately. Attempt to add a | 64 // If a session can't be created, force a pause immediately. Attempt to add a |
70 // session even if we already have one. MediaSession expects AddPlayer() to | 65 // session even if we already have one. MediaSession expects AddPlayer() to |
71 // be called after OnPlaybackPaused() to reactivate the session. | 66 // be called after OnPlaybackPaused() to reactivate the session. |
72 if (!media_session_->AddPlayer(this, player_id_, media_session_type)) { | 67 if (!media_session_->AddPlayer(this, player_id_, media_content_type)) { |
73 OnSuspend(player_id_); | 68 OnSuspend(player_id_); |
74 return false; | 69 return false; |
75 } | 70 } |
76 | 71 |
77 has_session_ = true; | 72 has_session_ = true; |
78 return true; | 73 return true; |
79 } | 74 } |
80 | 75 |
81 void MediaSessionController::OnSuspend(int player_id) { | 76 void MediaSessionController::OnSuspend(int player_id) { |
82 DCHECK_EQ(player_id_, player_id); | 77 DCHECK_EQ(player_id_, player_id); |
(...skipping 16 matching lines...) Expand all Loading... |
99 } | 94 } |
100 | 95 |
101 void MediaSessionController::OnPlaybackPaused() { | 96 void MediaSessionController::OnPlaybackPaused() { |
102 // We check for suspension here since the renderer may issue its own pause | 97 // We check for suspension here since the renderer may issue its own pause |
103 // in response to or while a pause from the browser is in flight. | 98 // in response to or while a pause from the browser is in flight. |
104 if (!media_session_->IsSuspended()) | 99 if (!media_session_->IsSuspended()) |
105 media_session_->OnPlayerPaused(this, player_id_); | 100 media_session_->OnPlayerPaused(this, player_id_); |
106 } | 101 } |
107 | 102 |
108 } // namespace content | 103 } // namespace content |
OLD | NEW |