1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
/*
* Copyright (C) 2015 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "MediaSessionManager.h"
#if ENABLE(MEDIA_SESSION)
#include "MediaSession.h"
#include "MediaSessionInterruptionProviderMac.h"
namespace WebCore {
static const char* contentSessionKind = "content";
MediaSessionManager& MediaSessionManager::singleton()
{
static NeverDestroyed<MediaSessionManager> manager;
return manager;
}
MediaSessionManager::MediaSessionManager()
{
#if PLATFORM(MAC)
m_interruptionProvider = adoptRef(new MediaSessionInterruptionProviderMac(*this));
m_interruptionProvider->beginListeningForInterruptions();
#endif
}
bool MediaSessionManager::hasActiveMediaElements() const
{
for (auto* session : m_sessions) {
if (session->hasActiveMediaElements())
return true;
}
return false;
}
void MediaSessionManager::addMediaSession(MediaSession& session)
{
m_sessions.add(&session);
}
void MediaSessionManager::removeMediaSession(MediaSession& session)
{
m_sessions.remove(&session);
}
void MediaSessionManager::togglePlayback()
{
for (auto* session : m_sessions) {
String sessionKind = session->kind();
if (session->currentState() == MediaSession::State::Active && (sessionKind == contentSessionKind || sessionKind == ""))
session->togglePlayback();
}
}
void MediaSessionManager::skipToNextTrack()
{
// 5.2.2 When the user presses the MediaTrackNext media key, then for each Content-based media session that is
// currently ACTIVE and has a media remote controller with its nextTrackEnabled attribute set to true, queue a task
// to fire a simple event named nexttrack at its media remote controller.
for (auto* session : m_sessions) {
if (session->currentState() == MediaSession::State::Active && session->kind() == contentSessionKind)
session->skipToNextTrack();
}
}
void MediaSessionManager::skipToPreviousTrack()
{
// 5.2.2 When the user presses the MediaTrackPrevious media key, then for each Content-based media session that is
// currently ACTIVE and has a media remote controller with its previousTrackEnabled attribute set to true, queue a task
// to fire a simple event named previoustrack at its media remote controller.
for (auto* session : m_sessions) {
if (session->currentState() == MediaSession::State::Active && session->kind() == contentSessionKind)
session->skipToPreviousTrack();
}
}
void MediaSessionManager::didReceiveStartOfInterruptionNotification(MediaSessionInterruptingCategory interruptingCategory)
{
// 4.5.2 Interrupting a media session
// When a start-of-interruption notification event is received from the platform, then the user agent must run the
// media session interruption algorithm against all known media sessions, passing in each media session as media session.
for (auto* session : m_sessions) {
// 1. If media session's current state is not active, then terminate these steps.
if (session->currentState() != MediaSession::State::Active)
continue;
// 2. Let interrupting media session category be the media session category that triggered this interruption.
// If this interruption has no known media session category, let interrupting media session category be Default.
// 3. Run these substeps:
if (interruptingCategory == MediaSessionInterruptingCategory::Content) {
// - If interrupting media session category is Content:
// If media session's current media session type is Default or Content then indefinitely pause all of media
// session's active audio-producing participants and set media session's current state to idle.
if (session->kindEnum() == MediaSession::Kind::Default || session->kindEnum() == MediaSession::Kind::Content)
session->handleIndefinitePauseInterruption();
} else if (interruptingCategory == MediaSessionInterruptingCategory::Transient) {
// - If interrupting media session category is Transient:
// If media session's current media session type is Default or Content then duck all of media session's active
// audio-producing participants and set media session's current state to interrupted.
if (session->kindEnum() == MediaSession::Kind::Default || session->kindEnum() == MediaSession::Kind::Content)
session->handleDuckInterruption();
} else if (interruptingCategory == MediaSessionInterruptingCategory::TransientSolo) {
// - If interrupting media session category is Transient Solo:
// If media session's current media session type is Default, Content, Transient or Transient Solo then pause
// all of media session's active audio-producing participants and set current media session's current state to interrupted.
if (session->kindEnum() != MediaSession::Kind::Ambient)
session->handlePauseInterruption();
}
}
}
void MediaSessionManager::didReceiveEndOfInterruptionNotification(MediaSessionInterruptingCategory interruptingCategory)
{
// 4.5.2 Interrupting a media session
// When an end-of-interruption notification event is received from the platform, then the user agent must run the
// media session continuation algorithm against all known media sessions, passing in each media session as media session.
for (auto* session : m_sessions) {
// 1. If media session's current state is not interrupted, then terminate these steps.
if (session->currentState() != MediaSession::State::Interrupted)
continue;
// 2. Let interrupting media session category be the media session category that initially triggered this interruption.
// If this interruption has no known media session category, let interrupting media session category be Default.
// 3. Run these substeps:
if (interruptingCategory == MediaSessionInterruptingCategory::Transient) {
// - If interrupting media session category is Transient:
// If media session's current media session type is Default or Content, then unduck all of media session's
// active audio-producing participants and set media session's current state to active.
if (session->kindEnum() == MediaSession::Kind::Default || session->kindEnum() == MediaSession::Kind::Content)
session->handleUnduckInterruption();
} else if (interruptingCategory == MediaSessionInterruptingCategory::TransientSolo) {
// - If interrupting media session category is Transient Solo:
// If media session's current media session type is Default, Content, Transient, or Transient Solo, then
// unpause the media session's active audio-producing participants and set media session's current state to active.
if (session->kindEnum() != MediaSession::Kind::Ambient)
session->handleUnpauseInterruption();
}
}
}
} // namespace WebCore
#endif /* ENABLE(MEDIA_SESSION) */
|