From 573784b37f2b95c5fc42b45d2da7e0d26a3aef83 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 6 Oct 2022 11:08:47 -0700 Subject: [PATCH] Handle the SoundStream in MvePlayer instead of InterplayDecoder --- src/common/cutscenes/movieplayer.cpp | 25 +++++++++++++ src/common/cutscenes/playmve.cpp | 25 ++++++------- src/common/cutscenes/playmve.h | 55 ++++++++++++++-------------- 3 files changed, 65 insertions(+), 40 deletions(-) diff --git a/src/common/cutscenes/movieplayer.cpp b/src/common/cutscenes/movieplayer.cpp index e3f497e2f..b86df6a2f 100644 --- a/src/common/cutscenes/movieplayer.cpp +++ b/src/common/cutscenes/movieplayer.cpp @@ -260,8 +260,16 @@ public: class MvePlayer : public MoviePlayer { InterplayDecoder decoder; + SoundStream* stream = nullptr; bool failed = false; + bool StreamCallback(SoundStream *stream, void *buff, int len) + { + return decoder.FillSamples(buff, len); + } + static bool StreamCallbackC(SoundStream *stream, void *buff, int len, void *userdata) + { return static_cast(userdata)->StreamCallback(stream, buff, len); } + public: bool isvalid() { return !failed; } @@ -279,12 +287,29 @@ public: bool Frame(uint64_t clock) override { if (failed) return false; + bool playon = decoder.RunFrame(clock); + if (playon) + { + if (!stream && decoder.HasAudio()) + { + S_StopMusic(true); + // start audio playback + stream = S_CreateCustomStream(6000, decoder.GetSampleRate(), decoder.NumChannels(), MusicSamples16bit, StreamCallbackC, this); + if (!stream) + decoder.DisableAudio(); + } + } + return playon; } ~MvePlayer() { + if (stream) + S_StopCustomStream(stream); + stream = nullptr; + decoder.Close(); } diff --git a/src/common/cutscenes/playmve.cpp b/src/common/cutscenes/playmve.cpp index a3ce67bfc..403daa531 100644 --- a/src/common/cutscenes/playmve.cpp +++ b/src/common/cutscenes/playmve.cpp @@ -95,7 +95,7 @@ static const int16_t delta_table[] = { #define LE_64(x) (LE_32(x) | ((uint64_t)LE_32(x+4) << 32)) -bool InterplayDecoder::StreamCallback(SoundStream *stream, void *buff, int len) +bool InterplayDecoder::FillSamples(void *buff, int len) { for (int i = 0; i < len;) { @@ -174,10 +174,20 @@ bool InterplayDecoder::StreamCallback(SoundStream *stream, void *buff, int len) return true; } +void InterplayDecoder::DisableAudio() +{ + if (bAudioEnabled) + { + std::unique_lock plock(PacketMutex); + bAudioEnabled = false; + audio.Packets.clear(); + } +} + + InterplayDecoder::InterplayDecoder(bool soundenabled) { bIsPlaying = false; - bAudioStarted = false; bAudioEnabled = soundenabled; nWidth = 0; @@ -485,9 +495,6 @@ int InterplayDecoder::ProcessNextChunk() void InterplayDecoder::Close() { bIsPlaying = false; - if (stream) - S_StopCustomStream(stream); - stream = nullptr; fr.Close(); if (decodeMap.pData) { @@ -562,14 +569,6 @@ bool InterplayDecoder::RunFrame(uint64_t clock) } nNextFrameTime += nFrameDuration; - if (!bAudioStarted && bAudioEnabled) - { - S_StopMusic(true); - // start audio playback - stream = S_CreateCustomStream(6000, audio.nSampleRate, audio.nChannels, MusicSamples16bit, StreamCallbackC, this); - bAudioStarted = true; - } - bool doFrame = false; do { diff --git a/src/common/cutscenes/playmve.h b/src/common/cutscenes/playmve.h index 974dc34ba..9cb299038 100644 --- a/src/common/cutscenes/playmve.h +++ b/src/common/cutscenes/playmve.h @@ -58,21 +58,6 @@ class InterplayDecoder { - struct AudioPacket - { - size_t nSize = 0; - std::unique_ptr pData; - }; - - struct VideoPacket - { - uint16_t nPalStart=0, nPalCount=0; - uint32_t nDecodeMapSize = 0; - uint32_t nVideoDataSize = 0; - bool bSendFlag = false; - std::unique_ptr pData; - }; - public: enum { @@ -123,8 +108,34 @@ public: bool Open(FileReader &fr); void Close(); + bool RunFrame(uint64_t clock); + bool FillSamples(void *buff, int len); + + bool HasAudio() const noexcept { return bAudioEnabled; } + int NumChannels() const noexcept { return audio.nChannels; } + int GetSampleRate() const noexcept { return audio.nSampleRate; } + void DisableAudio(); + + AnimTextures& animTex() { return animtex; } + +private: + struct AudioPacket + { + size_t nSize = 0; + std::unique_ptr pData; + }; + + struct VideoPacket + { + uint16_t nPalStart=0, nPalCount=0; + uint32_t nDecodeMapSize = 0; + uint32_t nVideoDataSize = 0; + bool bSendFlag = false; + std::unique_ptr pData; + }; + struct AudioData { int nChannels = 0; @@ -138,16 +149,7 @@ public: std::deque Packets; }; - AudioData audio; - AnimTextures animtex; - - AnimTextures& animTex() { return animtex; } - -private: - bool StreamCallback(SoundStream *stream, void *buff, int len); - static bool StreamCallbackC(SoundStream *stream, void *buff, int len, void *userdata) - { return static_cast(userdata)->StreamCallback(stream, buff, len); } struct DecodeMap { @@ -185,8 +187,7 @@ private: std::mutex PacketMutex; FileReader fr; - bool bIsPlaying, bAudioStarted; - bool bAudioEnabled; + bool bIsPlaying, bAudioEnabled; uint32_t nTimerRate, nTimerDiv; uint32_t nWidth, nHeight, nFrame; @@ -204,9 +205,9 @@ private: const uint8_t *ChunkPtr = nullptr; DecodeMap decodeMap; + AnimTextures animtex; Palette palette[256]; uint64_t nNextFrameTime = 0; - SoundStream* stream = nullptr; }; #endif