2020-09-15 20:57:54 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-28 16:39:18 +00:00
|
|
|
//
|
2020-09-15 20:57:54 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-28 16:39:18 +00:00
|
|
|
// VideoComponent.cpp
|
|
|
|
//
|
|
|
|
// Base class for playing videos.
|
|
|
|
//
|
|
|
|
|
2016-12-04 23:47:34 +00:00
|
|
|
#include "components/VideoComponent.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2016-12-04 23:47:34 +00:00
|
|
|
#include "ThemeData.h"
|
2017-03-25 17:02:28 +00:00
|
|
|
#include "Window.h"
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "resources/ResourceManager.h"
|
|
|
|
#include "utils/FileSystemUtil.h"
|
2020-06-26 16:03:55 +00:00
|
|
|
|
|
|
|
#include <SDL2/SDL_timer.h>
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
#define SCREENSAVER_FADE_IN_TIME 1100
|
|
|
|
#define MEDIA_VIEWER_FADE_IN_TIME 600
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
VideoComponent::VideoComponent(Window* window)
|
|
|
|
: GuiComponent(window)
|
|
|
|
, mWindow(window)
|
|
|
|
, mStaticImage(window)
|
|
|
|
, mVideoHeight(0)
|
|
|
|
, mVideoWidth(0)
|
|
|
|
, mStartDelayed(false)
|
|
|
|
, mIsPlaying(false)
|
|
|
|
, mIsActuallyPlaying(false)
|
|
|
|
, mPause(false)
|
|
|
|
, mShowing(false)
|
|
|
|
, mDisable(false)
|
|
|
|
, mMediaViewerMode(false)
|
|
|
|
, mScreensaverActive(false)
|
|
|
|
, mScreensaverMode(false)
|
|
|
|
, mGameLaunched(false)
|
|
|
|
, mBlockPlayer(false)
|
|
|
|
, mTargetIsMax(false)
|
|
|
|
, mFadeIn(1.0)
|
|
|
|
, mTargetSize(0, 0)
|
|
|
|
, mVideoAreaPos(0, 0)
|
|
|
|
, mVideoAreaSize(0, 0)
|
2016-12-04 23:47:34 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
// Setup the default configuration.
|
|
|
|
mConfig.showSnapshotDelay = false;
|
|
|
|
mConfig.showSnapshotNoVideo = false;
|
|
|
|
mConfig.startDelay = 0;
|
|
|
|
|
|
|
|
if (mWindow->getGuiStackSize() > 1)
|
|
|
|
topWindow(false);
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VideoComponent::~VideoComponent()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
// Stop any currently running video.
|
|
|
|
stopVideo();
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VideoComponent::setVideo(std::string path)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
// Convert the path into a generic format.
|
|
|
|
std::string fullPath = Utils::FileSystem::getCanonicalPath(path);
|
|
|
|
|
|
|
|
// Check that it's changed.
|
|
|
|
if (fullPath == mVideoPath)
|
|
|
|
return !path.empty();
|
|
|
|
|
|
|
|
// Store the path.
|
|
|
|
mVideoPath = fullPath;
|
|
|
|
|
|
|
|
// If the file exists then set the new video.
|
|
|
|
if (!fullPath.empty() && ResourceManager::getInstance()->fileExists(fullPath)) {
|
|
|
|
// Return true to show that we are going to attempt to play a video.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return false to show that no video will be displayed.
|
|
|
|
return false;
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::setImage(std::string path)
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
// Check that the image has changed.
|
|
|
|
if (path == mStaticImagePath)
|
|
|
|
return;
|
2017-01-25 15:00:56 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
mStaticImage.setImage(path);
|
|
|
|
mStaticImagePath = path;
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 21:13:33 +00:00
|
|
|
void VideoComponent::onShow()
|
|
|
|
{
|
|
|
|
mBlockPlayer = false;
|
|
|
|
mPause = false;
|
|
|
|
mShowing = true;
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::onHide()
|
|
|
|
{
|
|
|
|
mShowing = false;
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
2021-01-31 19:51:24 +00:00
|
|
|
void VideoComponent::onStopVideo()
|
|
|
|
{
|
|
|
|
stopVideo();
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
2020-11-17 21:13:33 +00:00
|
|
|
void VideoComponent::onPauseVideo()
|
|
|
|
{
|
|
|
|
mBlockPlayer = true;
|
|
|
|
mPause = true;
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::onUnpauseVideo()
|
|
|
|
{
|
|
|
|
mBlockPlayer = false;
|
|
|
|
mPause = false;
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::onScreensaverActivate()
|
|
|
|
{
|
|
|
|
mBlockPlayer = true;
|
|
|
|
mPause = true;
|
|
|
|
if (Settings::getInstance()->getString("ScreensaverType") == "dim")
|
|
|
|
stopVideo();
|
2020-12-16 17:03:23 +00:00
|
|
|
else
|
|
|
|
pauseVideo();
|
2020-11-17 21:13:33 +00:00
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::onScreensaverDeactivate()
|
|
|
|
{
|
|
|
|
mBlockPlayer = false;
|
|
|
|
// Stop video when deactivating the screensaver to force a reload of the
|
|
|
|
// static image (if the theme is configured as such).
|
|
|
|
stopVideo();
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::onGameLaunchedActivate()
|
|
|
|
{
|
|
|
|
mGameLaunched = true;
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::onGameLaunchedDeactivate()
|
|
|
|
{
|
|
|
|
mGameLaunched = false;
|
|
|
|
stopVideo();
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::topWindow(bool isTop)
|
|
|
|
{
|
|
|
|
if (isTop) {
|
|
|
|
mBlockPlayer = false;
|
|
|
|
mPause = false;
|
|
|
|
// Stop video when closing the menu to force a reload of the
|
|
|
|
// static image (if the theme is configured as such).
|
|
|
|
stopVideo();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mBlockPlayer = true;
|
|
|
|
mPause = true;
|
|
|
|
}
|
|
|
|
manageState();
|
|
|
|
}
|
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void VideoComponent::render(const glm::mat4& parentTrans)
|
2016-12-04 23:47:34 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!isVisible())
|
|
|
|
return;
|
2019-07-22 03:13:48 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::mat4 trans{parentTrans * getTransform()};
|
2020-06-28 16:39:18 +00:00
|
|
|
GuiComponent::renderChildren(trans);
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
Renderer::setMatrix(trans);
|
2017-01-25 15:00:56 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// Handle the case where the video is delayed.
|
|
|
|
handleStartDelay();
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// Handle looping of the video.
|
|
|
|
handleLooping();
|
2020-07-18 11:21:44 +00:00
|
|
|
|
|
|
|
// Pause video in case a game has been launched.
|
|
|
|
pauseVideo();
|
2017-06-13 22:57:18 +00:00
|
|
|
}
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2021-08-15 17:30:31 +00:00
|
|
|
void VideoComponent::renderSnapshot(const glm::mat4& parentTrans)
|
2017-06-13 22:57:18 +00:00
|
|
|
{
|
2021-04-09 20:54:00 +00:00
|
|
|
// This function is called when the video is not currently being played. We need to
|
|
|
|
// work out if we should display a static image. If the menu is open, then always render
|
|
|
|
// the static image as the metadata may have been changed. In that case the gamelist
|
|
|
|
// was reloaded and there would just be a blank space unless we render the image here.
|
|
|
|
// The side effect of this is that a static image is displayed even for themes that are
|
|
|
|
// set to start playing the video immediately. Although this may seem a bit inconsistent it
|
|
|
|
// simply looks better than leaving an empty space where the video would have been located.
|
|
|
|
if (mWindow->getGuiStackSize() > 1 || (mConfig.showSnapshotNoVideo && mVideoPath.empty()) ||
|
2021-07-07 18:31:46 +00:00
|
|
|
(mStartDelayed && mConfig.showSnapshotDelay)) {
|
2020-09-18 18:43:46 +00:00
|
|
|
mStaticImage.setOpacity(mOpacity);
|
2020-06-28 16:39:18 +00:00
|
|
|
mStaticImage.render(parentTrans);
|
|
|
|
}
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
void VideoComponent::applyTheme(const std::shared_ptr<ThemeData>& theme,
|
|
|
|
const std::string& view,
|
|
|
|
const std::string& element,
|
|
|
|
unsigned int properties)
|
2016-12-04 23:47:34 +00:00
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
using namespace ThemeFlags;
|
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
GuiComponent::applyTheme(theme, view, element,
|
|
|
|
(properties ^ ThemeFlags::SIZE) |
|
|
|
|
((properties & (ThemeFlags::SIZE | POSITION)) ? ORIGIN : 0));
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "video");
|
2019-08-31 01:57:32 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (!elem)
|
|
|
|
return;
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::vec2 scale{getParent() ? getParent()->getSize() :
|
|
|
|
glm::vec2{static_cast<float>(Renderer::getScreenWidth()),
|
|
|
|
static_cast<float>(Renderer::getScreenHeight())}};
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (properties & ThemeFlags::SIZE) {
|
2020-11-16 22:34:08 +00:00
|
|
|
if (elem->has("size")) {
|
2021-08-16 16:25:01 +00:00
|
|
|
setResize(elem->get<glm::vec2>("size") * scale);
|
|
|
|
mVideoAreaSize = elem->get<glm::vec2>("size") * scale;
|
2020-11-16 22:34:08 +00:00
|
|
|
}
|
|
|
|
else if (elem->has("maxSize")) {
|
2021-08-16 16:25:01 +00:00
|
|
|
setMaxSize(elem->get<glm::vec2>("maxSize") * scale);
|
|
|
|
mVideoAreaSize = elem->get<glm::vec2>("maxSize") * scale;
|
2020-11-16 22:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties & ThemeFlags::POSITION) {
|
|
|
|
if (elem->has("pos"))
|
2021-08-16 16:25:01 +00:00
|
|
|
mVideoAreaPos = elem->get<glm::vec2>("pos") * scale;
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (elem->has("default"))
|
|
|
|
mConfig.defaultVideoPath = elem->get<std::string>("default");
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if ((properties & ThemeFlags::DELAY) && elem->has("delay"))
|
2020-09-13 12:28:06 +00:00
|
|
|
mConfig.startDelay = static_cast<unsigned>((elem->get<float>("delay") * 1000.0f));
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (elem->has("showSnapshotNoVideo"))
|
|
|
|
mConfig.showSnapshotNoVideo = elem->get<bool>("showSnapshotNoVideo");
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
if (elem->has("showSnapshotDelay"))
|
|
|
|
mConfig.showSnapshotDelay = elem->get<bool>("showSnapshotDelay");
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> VideoComponent::getHelpPrompts()
|
|
|
|
{
|
2020-06-28 16:39:18 +00:00
|
|
|
std::vector<HelpPrompt> ret;
|
|
|
|
ret.push_back(HelpPrompt("a", "select"));
|
|
|
|
return ret;
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 21:13:33 +00:00
|
|
|
void VideoComponent::update(int deltaTime)
|
2016-12-04 23:47:34 +00:00
|
|
|
{
|
2020-11-17 21:13:33 +00:00
|
|
|
if (mBlockPlayer) {
|
|
|
|
setImage(mStaticImagePath);
|
2020-09-15 20:57:54 +00:00
|
|
|
return;
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
2016-12-04 23:47:34 +00:00
|
|
|
|
2020-11-17 21:13:33 +00:00
|
|
|
manageState();
|
|
|
|
|
2021-05-29 08:58:51 +00:00
|
|
|
// Fade in videos, the time period is a bit different between the screensaver,
|
|
|
|
// media viewer and gamelist view.
|
|
|
|
if (mScreensaverMode && mFadeIn < 1.0f) {
|
2021-07-07 18:31:46 +00:00
|
|
|
mFadeIn = Math::clamp(mFadeIn + (deltaTime / static_cast<float>(SCREENSAVER_FADE_IN_TIME)),
|
|
|
|
0.0f, 1.0f);
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
|
|
|
else if (mMediaViewerMode && mFadeIn < 1.0f) {
|
2021-07-07 18:31:46 +00:00
|
|
|
mFadeIn = Math::clamp(mFadeIn + (deltaTime / static_cast<float>(MEDIA_VIEWER_FADE_IN_TIME)),
|
|
|
|
0.0f, 1.0f);
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
|
|
|
else if (mFadeIn < 1.0f) {
|
2020-12-29 10:06:01 +00:00
|
|
|
mFadeIn = Math::clamp(mFadeIn + 0.01f, 0.0f, 1.0f);
|
2021-05-29 08:58:51 +00:00
|
|
|
}
|
2020-11-17 21:13:33 +00:00
|
|
|
|
|
|
|
GuiComponent::update(deltaTime);
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::startVideoWithDelay()
|
|
|
|
{
|
2020-07-18 11:21:44 +00:00
|
|
|
mPause = false;
|
|
|
|
|
2020-06-28 16:39:18 +00:00
|
|
|
// If not playing then either start the video or initiate the delay.
|
|
|
|
if (!mIsPlaying) {
|
|
|
|
// Set the video that we are going to be playing so we don't attempt to restart it.
|
|
|
|
mPlayingVideoPath = mVideoPath;
|
|
|
|
|
2020-12-16 16:57:10 +00:00
|
|
|
if (mConfig.startDelay == 0) {
|
2020-06-28 16:39:18 +00:00
|
|
|
// No delay. Just start the video.
|
|
|
|
mStartDelayed = false;
|
|
|
|
startVideo();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Configure the start delay.
|
|
|
|
mStartDelayed = true;
|
|
|
|
mStartTime = SDL_GetTicks() + mConfig.startDelay;
|
|
|
|
}
|
|
|
|
mIsPlaying = true;
|
|
|
|
}
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 21:13:33 +00:00
|
|
|
void VideoComponent::handleStartDelay()
|
2016-12-04 23:47:34 +00:00
|
|
|
{
|
2021-03-24 19:15:17 +00:00
|
|
|
if (mBlockPlayer || mGameLaunched)
|
2020-09-15 20:57:54 +00:00
|
|
|
return;
|
2020-09-13 12:28:06 +00:00
|
|
|
|
2020-11-17 21:13:33 +00:00
|
|
|
// Only play if any delay has timed out.
|
|
|
|
if (mStartDelayed) {
|
|
|
|
// If the setting to override the theme-supplied video delay setting has been enabled,
|
|
|
|
// then play the video immediately.
|
|
|
|
if (!Settings::getInstance()->getBool("PlayVideosImmediately")) {
|
|
|
|
// If there is a video file available but no static image, then start playing the
|
|
|
|
// video immediately regardless of theme configuration or settings.
|
|
|
|
if (mStaticImagePath != "") {
|
|
|
|
if (mStartTime > SDL_GetTicks()) {
|
|
|
|
// Timeout not yet completed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Completed.
|
|
|
|
mStartDelayed = false;
|
|
|
|
// Clear the playing flag so startVideo works.
|
|
|
|
mIsPlaying = false;
|
|
|
|
startVideo();
|
|
|
|
}
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VideoComponent::manageState()
|
|
|
|
{
|
2020-07-18 11:21:44 +00:00
|
|
|
// We will only show the video if the component is on display and the screensaver
|
2020-06-28 16:39:18 +00:00
|
|
|
// is not active.
|
|
|
|
bool show = mShowing && !mScreensaverActive && !mDisable;
|
|
|
|
|
|
|
|
// See if we're already playing.
|
|
|
|
if (mIsPlaying) {
|
|
|
|
// If we are not on display then stop the video from playing.
|
|
|
|
if (!show) {
|
|
|
|
stopVideo();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (mVideoPath != mPlayingVideoPath) {
|
|
|
|
// Path changed. Stop the video. We will start it again below because
|
|
|
|
// mIsPlaying will be modified by stopVideo to be false.
|
|
|
|
stopVideo();
|
|
|
|
}
|
|
|
|
}
|
2021-05-29 08:58:51 +00:00
|
|
|
updatePlayer();
|
2020-06-28 16:39:18 +00:00
|
|
|
}
|
|
|
|
// Need to recheck variable rather than 'else' because it may be modified above.
|
|
|
|
if (!mIsPlaying) {
|
|
|
|
// If we are on display then see if we should start the video.
|
|
|
|
if (show && !mVideoPath.empty())
|
|
|
|
startVideoWithDelay();
|
|
|
|
}
|
2020-07-18 11:21:44 +00:00
|
|
|
|
|
|
|
// If a game has just been launched and a video is actually shown, then request a
|
|
|
|
// pause of the video so it doesn't continue to play in the background while the
|
|
|
|
// game is running.
|
|
|
|
if (mGameLaunched && show && !mPause)
|
|
|
|
mPause = true;
|
2016-12-04 23:47:34 +00:00
|
|
|
}
|