Browse Source

code review + pause handling

Laserlicht 11 months ago
parent
commit
bb73a35412

+ 12 - 0
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;

+ 2 - 0
client/media/CSoundHandler.h

@@ -67,6 +67,8 @@ public:
 	int playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats = 0, bool cache = false) final;
 	int playSoundFromSet(std::vector<soundBase::soundID> & sound_vec) final;
 	void stopSound(int handler) final;
+	void pauseSound(int handler) final;
+	void resumeSound(int handler) final;
 
 	void setCallback(int channel, std::function<void()> function) final;
 	void resetCallback(int channel) final;

+ 19 - 3
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<std::chrono::milliseconds>(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;

+ 4 - 1
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

+ 2 - 0
client/media/ISoundPlayer.h

@@ -22,6 +22,8 @@ public:
 	virtual int playSound(std::pair<std::unique_ptr<ui8[]>, si64> & data, int repeats = 0, bool cache = false) = 0;
 	virtual int playSoundFromSet(std::vector<soundBase::soundID> & 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;

+ 4 - 0
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;
 };
 

+ 11 - 3
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<CMultiLineLabel>(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<CMultiLineLabel>(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)