blob: 115b85217bb91350853ad1918a4bf3a585051415 [file] [log] [blame]
// Copyright 2017 The Cobalt Authors. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <bitset>
#include <memory>
#include "base/threading/thread_checker.h"
#include "cobalt/extension/media_session.h"
#include "cobalt/media/web_media_player_factory.h"
#include "cobalt/media_session/media_session.h"
#include "cobalt/media_session/media_session_action_details.h"
#include "cobalt/media_session/media_session_state.h"
#include "starboard/time.h"
namespace cobalt {
namespace media_session {
// Base class for a platform-level implementation of MediaSession.
// Platforms should subclass this to connect MediaSession to their platform.
class MediaSessionClient {
friend class MediaSession;
MediaSessionClient(): MediaSessionClient(nullptr) {}
// Injectable MediaSession for tests.
explicit MediaSessionClient(MediaSession* media_session);
virtual ~MediaSessionClient();
// Retrieves the singleton MediaSession associated with this client.
MediaSession* GetMediaSession() { return media_session_; }
// The web app should set the MediaPositionState of the MediaSession object.
// However, if that is not done, then query the web media player factory to
// guess which player is associated with the media session to get the media
// position state. The player factory must outlive the media session client.
void SetMediaPlayerFactory(const media::WebMediaPlayerFactory* factory);
// Sets the platform's current playback state. This is used to compute
// the "guessed playback state"
// Can be invoked from any thread.
void UpdatePlatformPlaybackState(
CobaltExtensionMediaSessionPlaybackState state);
// Invokes a given media session action
// Can be invoked from any thread.
void InvokeAction(CobaltExtensionMediaSessionAction action) {
std::unique_ptr<CobaltExtensionMediaSessionActionDetails> details(
new CobaltExtensionMediaSessionActionDetails());
CobaltExtensionMediaSessionActionDetailsInit(details.get(), action);
// Invokes a given media session action that takes additional details.
void InvokeAction(CobaltExtensionMediaSessionActionDetails details) {
std::unique_ptr<CobaltExtensionMediaSessionActionDetails> details_ptr(
new CobaltExtensionMediaSessionActionDetails(details));
// Invoked on the browser thread when any metadata, position state, playback
// state, or supported session actions change.
virtual void OnMediaSessionStateChanged(
const MediaSessionState& session_state);
// Indicate the media session client is active or not depending on the
// media session playback state.
bool is_active() {
return session_state_.actual_playback_state() !=
// Set maybe freeze callback.
void SetMaybeFreezeCallback(const base::Closure& maybe_freeze_callback) {
maybe_freeze_callback_ = maybe_freeze_callback;
void set_media_session(MediaSession* media_session) {
media_session_ = media_session;
// If the media session is not active, then run MaybeFreezeCallback to
// suspend the App.
void RunMaybeFreezeCallback(int sequence_number);
MediaSession* media_session_;
MediaSessionState session_state_;
MediaSessionPlaybackState platform_playback_state_;
const media::WebMediaPlayerFactory* media_player_factory_ = nullptr;
const CobaltExtensionMediaSessionApi* extension_;
void UpdateMediaSessionState();
MediaSessionPlaybackState ComputeActualPlaybackState() const;
MediaSessionState::AvailableActionsSet ComputeAvailableActions() const;
void InvokeActionInternal(
std::unique_ptr<CobaltExtensionMediaSessionActionDetails> details);
void ConvertMediaSessionActions(
const MediaSessionState::AvailableActionsSet& actions,
bool result[kCobaltExtensionMediaSessionActionNumActions]);
std::unique_ptr<MediaSessionActionDetails> ConvertActionDetails(
const CobaltExtensionMediaSessionActionDetails& ext_details);
// Static callback wrappers for MediaSessionAPI extension.
static void UpdatePlatformPlaybackStateCallback(
CobaltExtensionMediaSessionPlaybackState state, void* callback_context);
static void InvokeActionCallback(
CobaltExtensionMediaSessionActionDetails details, void* callback_context);
// MediaSessionAPI extension type conversion helpers.
CobaltExtensionMediaSessionPlaybackState ConvertPlaybackState(
MediaSessionPlaybackState state);
MediaSessionPlaybackState ConvertPlaybackState(
CobaltExtensionMediaSessionPlaybackState state);
CobaltExtensionMediaSessionAction ConvertMediaSessionAction(
MediaSessionAction action);
MediaSessionAction ConvertMediaSessionAction(
CobaltExtensionMediaSessionAction action);
base::Closure maybe_freeze_callback_;
// This is for checking the sequence number of PostDelayedTask. It should be
// aligned with a single thread.
int sequence_number_;
} // namespace media_session
} // namespace cobalt