From bb73a35412b2cc06ee2ec04d997eede85c9fe12f Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Wed, 30 Oct 2024 00:35:50 +0100 Subject: [PATCH] code review + pause handling --- client/media/CSoundHandler.cpp | 12 ++++++++++++ client/media/CSoundHandler.h | 2 ++ client/media/CVideoHandler.cpp | 22 +++++++++++++++++++--- client/media/CVideoHandler.h | 5 ++++- client/media/ISoundPlayer.h | 2 ++ client/media/IVideoPlayer.h | 4 ++++ client/widgets/VideoWidget.cpp | 14 +++++++++++--- 7 files changed, 54 insertions(+), 7 deletions(-) diff --git a/client/media/CSoundHandler.cpp b/client/media/CSoundHandler.cpp index adfb1a2f7..bd099e728 100644 --- a/client/media/CSoundHandler.cpp +++ b/client/media/CSoundHandler.cpp @@ -240,6 +240,18 @@ void CSoundHandler::stopSound(int handler) Mix_HaltChannel(handler); } +void CSoundHandler::pauseSound(int handler) +{ + if(isInitialized() && handler != -1) + Mix_Pause(handler); +} + +void CSoundHandler::resumeSound(int handler) +{ + if(isInitialized() && handler != -1) + Mix_Resume(handler); +} + ui32 CSoundHandler::getVolume() const { return volume; diff --git a/client/media/CSoundHandler.h b/client/media/CSoundHandler.h index 5a10a5493..3450cbffb 100644 --- a/client/media/CSoundHandler.h +++ b/client/media/CSoundHandler.h @@ -67,6 +67,8 @@ public: int playSound(std::pair, si64> & data, int repeats = 0, bool cache = false) final; int playSoundFromSet(std::vector & sound_vec) final; void stopSound(int handler) final; + void pauseSound(int handler) final; + void resumeSound(int handler) final; void setCallback(int channel, std::function function) final; void resetCallback(int channel) final; diff --git a/client/media/CVideoHandler.cpp b/client/media/CVideoHandler.cpp index 809af5b26..a2a0ef939 100644 --- a/client/media/CVideoHandler.cpp +++ b/client/media/CVideoHandler.cpp @@ -391,10 +391,10 @@ void CVideoInstance::tick(uint32_t msPassed) if(videoEnded()) throw std::runtime_error("Video already ended!"); - if(startTime == std::chrono::high_resolution_clock::time_point()) - startTime = std::chrono::high_resolution_clock::now(); + if(startTime == std::chrono::steady_clock::time_point()) + startTime = std::chrono::steady_clock::now(); - auto nowTime = std::chrono::high_resolution_clock::now(); + auto nowTime = std::chrono::steady_clock::now(); double difference = std::chrono::duration_cast(nowTime - startTime).count() / 1000.0; int frameskipCounter = 0; @@ -407,6 +407,22 @@ void CVideoInstance::tick(uint32_t msPassed) loadNextFrame(); } + +void CVideoInstance::activate() +{ + if(deactivationStartTime != std::chrono::steady_clock::time_point()) + { + auto pauseDuration = std::chrono::steady_clock::now() - deactivationStartTime; + startTime += pauseDuration; + deactivationStartTime = std::chrono::steady_clock::time_point(); + } +} + +void CVideoInstance::deactivate() +{ + deactivationStartTime = std::chrono::steady_clock::now(); +} + struct FFMpegFormatDescription { uint8_t sampleSizeBytes; diff --git a/client/media/CVideoHandler.h b/client/media/CVideoHandler.h index b4a3c7c81..63e4a6176 100644 --- a/client/media/CVideoHandler.h +++ b/client/media/CVideoHandler.h @@ -78,7 +78,8 @@ class CVideoInstance final : public IVideoInstance, public FFMpegStream Point dimensions; /// video playback start time point - std::chrono::high_resolution_clock::time_point startTime; + std::chrono::steady_clock::time_point startTime; + std::chrono::steady_clock::time_point deactivationStartTime; void prepareOutput(float scaleFactor, bool useTextureOutput); @@ -96,6 +97,8 @@ public: void show(const Point & position, Canvas & canvas) final; void tick(uint32_t msPassed) final; + void activate() final; + void deactivate() final; }; class CVideoPlayer final : public IVideoPlayer diff --git a/client/media/ISoundPlayer.h b/client/media/ISoundPlayer.h index 9b3d9d5e9..ffcb90dce 100644 --- a/client/media/ISoundPlayer.h +++ b/client/media/ISoundPlayer.h @@ -22,6 +22,8 @@ public: virtual int playSound(std::pair, si64> & data, int repeats = 0, bool cache = false) = 0; virtual int playSoundFromSet(std::vector & sound_vec) = 0; virtual void stopSound(int handler) = 0; + virtual void pauseSound(int handler) = 0; + virtual void resumeSound(int handler) = 0; virtual ui32 getVolume() const = 0; virtual void setVolume(ui32 percent) = 0; diff --git a/client/media/IVideoPlayer.h b/client/media/IVideoPlayer.h index 6f7380166..3f2784c16 100644 --- a/client/media/IVideoPlayer.h +++ b/client/media/IVideoPlayer.h @@ -35,6 +35,10 @@ public: /// Advances video playback by specified duration virtual void tick(uint32_t msPassed) = 0; + /// activate or deactivate video + virtual void activate() = 0; + virtual void deactivate() = 0; + virtual ~IVideoInstance() = default; }; diff --git a/client/widgets/VideoWidget.cpp b/client/widgets/VideoWidget.cpp index 348548935..35fe4adcb 100644 --- a/client/widgets/VideoWidget.cpp +++ b/client/widgets/VideoWidget.cpp @@ -50,7 +50,8 @@ void VideoWidgetBase::playVideo(const VideoPath & fileToPlay) { pos.w = videoInstance->size().x; pos.h = videoInstance->size().y; - subTitle = std::make_unique(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE); + if(!subTitleData.isNull()) + subTitle = std::make_unique(Rect(0, (pos.h / 5) * 4, pos.w, pos.h / 5), EFonts::FONT_HIGH_SCORE, ETextAlignment::CENTER, Colors::WHITE); } if (playAudio) @@ -121,13 +122,20 @@ std::string VideoWidgetBase::getSubTitleLine(double timestamp) void VideoWidgetBase::activate() { CIntObject::activate(); - startAudio(); + if(audioHandle != -1) + CCS->soundh->resumeSound(audioHandle); + else + startAudio(); + if(videoInstance) + videoInstance->activate(); } void VideoWidgetBase::deactivate() { CIntObject::deactivate(); - stopAudio(); + CCS->soundh->pauseSound(audioHandle); + if(videoInstance) + videoInstance->deactivate(); } void VideoWidgetBase::showAll(Canvas & to)