2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
27 #include "MediaSession.h"
30 #include "HTMLMediaElement.h"
32 #include "MediaPlayer.h"
33 #include "MediaSessionManager.h"
38 static const char* stateName(MediaSession::State state)
40 #define CASE(state) case MediaSession::state: return #state
53 std::unique_ptr<MediaSession> MediaSession::create(MediaSessionClient& client)
55 return std::make_unique<MediaSession>(client);
58 MediaSession::MediaSession(MediaSessionClient& client)
61 , m_stateToRestore(Idle)
62 , m_notifyingClient(false)
64 ASSERT(m_client.mediaType() >= None && m_client.mediaType() <= WebAudio);
65 MediaSessionManager::sharedManager().addSession(*this);
68 MediaSession::~MediaSession()
70 MediaSessionManager::sharedManager().removeSession(*this);
73 void MediaSession::setState(State state)
75 LOG(Media, "MediaSession::setState(%p) - %s", this, stateName(state));
79 void MediaSession::beginInterruption(InterruptionType type)
81 LOG(Media, "MediaSession::beginInterruption(%p), state = %s", this, stateName(m_state));
83 if (type == EnteringBackground && client().overrideBackgroundPlaybackRestriction())
86 m_stateToRestore = state();
87 m_notifyingClient = true;
88 client().pausePlayback();
89 setState(Interrupted);
90 m_notifyingClient = false;
93 void MediaSession::endInterruption(EndInterruptionFlags flags)
95 LOG(Media, "MediaSession::endInterruption(%p) - flags = %i, stateToRestore = %s", this, (int)flags, stateName(m_stateToRestore));
97 State stateToRestore = m_stateToRestore;
98 m_stateToRestore = Idle;
101 if (flags & MayResumePlaying && stateToRestore == Playing) {
102 LOG(Media, "MediaSession::endInterruption - resuming playback");
103 client().resumePlayback();
107 bool MediaSession::clientWillBeginPlayback()
110 MediaSessionManager::sharedManager().sessionWillBeginPlayback(*this);
111 updateClientDataBuffering();
115 bool MediaSession::clientWillPausePlayback()
117 LOG(Media, "MediaSession::clientWillPausePlayback(%p)- state = %s", this, stateName(m_state));
118 if (state() == Interrupted) {
119 if (!m_notifyingClient) {
120 m_stateToRestore = Paused;
121 LOG(Media, " setting stateToRestore to \"Paused\"");
127 MediaSessionManager::sharedManager().sessionWillEndPlayback(*this);
128 updateClientDataBuffering();
132 void MediaSession::pauseSession()
134 LOG(Media, "MediaSession::pauseSession(%p)", this);
135 m_client.pausePlayback();
138 MediaSession::MediaType MediaSession::mediaType() const
140 return m_client.mediaType();
143 String MediaSession::title() const
145 return m_client.mediaSessionTitle();
148 double MediaSession::duration() const
150 return m_client.mediaSessionDuration();
153 double MediaSession::currentTime() const
155 return m_client.mediaSessionCurrentTime();
158 bool MediaSession::canReceiveRemoteControlCommands() const
160 return m_client.canReceiveRemoteControlCommands();
163 void MediaSession::didReceiveRemoteControlCommand(RemoteControlCommandType command)
165 m_client.didReceiveRemoteControlCommand(command);
168 #if ENABLE(PAGE_VISIBILITY_API)
169 void MediaSession::visibilityChanged()
171 updateClientDataBuffering();
174 void MediaSession::updateClientDataBuffering()
176 bool shouldBuffer = m_state == Playing || !m_client.elementIsHidden();
177 m_client.setShouldBufferData(shouldBuffer);
181 String MediaSessionClient::mediaSessionTitle() const
186 double MediaSessionClient::mediaSessionDuration() const
188 return MediaPlayer::invalidTime();
191 double MediaSessionClient::mediaSessionCurrentTime() const
193 return MediaPlayer::invalidTime();