Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(miles6): give every sample voice its own pcm copy
Olcay Seygan1bb1e81b3c26
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d138f3be8..a95a0d73b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -1731,6 +1731,7 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same## It does not crash+- A sound cut off in the middle no longer takes the game down a moment later. The game stopped the sound and freed it, but Windows' audio mixer only stops reading on its next pass, a hundredth of a second on, and two players' games died with the mixer reading what had just been freed. Every sound now plays from a copy the mixer gives back itself when it is done. A load screen movie skipped to its last frame threw its sound away the same way, and now leaves it to the mixer too.- The game no longer crashes now and then as a match starts or a new unit comes on the field. Since 3.2.0 the battle runs beside the picture, and both sides ask the game's files whether a model or a texture is there. The answers go into one shared list, and two answers written at once tore it, which took the game down later in some unrelated place. Two reads from the same game archive at once could also hand one of them the other's bytes. Each now waits its turn.- Text on the screen, a message line or a unit's label, no longer crashes the game with the Direct3D 11 picture on. Three players met it on 3.2.0 in the same spot. The picture keeps a note of every texture it has copied, and a texture thrown away on the battle's side struck its note off while the picture was writing a new one in. The battle's side now leaves the striking off to the picture, which does it before its next look.- A tank or helicopter carrying an add-on on its back, an Overlord's Gattling Cannon or a Helix's bunker, no longer crashes the game now and then. Since 3.2.0 the battle runs beside the picture, and the picture, seating the add-on on its carrier, finished the carrier's waiting model changes while the battle was still adding to that same list. The picture now takes the carrier as the last step between pictures left it.diff --git a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cppindex 99f00ed37..3aec96877 100644--- a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp+++ b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp@@ -1909,8 +1909,13 @@ void MilesAudioManager::notifyOfAudioCompletion( UnsignedIntPtr audioCompleted,playing->m_audioEventRTS->advanceNextPlayPortion();if (playing->m_audioEventRTS->getNextPlayPortion() != PP_Done) {+ // Closed and forgotten before the next portion opens, as startNextLoop does: opening it can+ // make the cache evict a file, and closeAnySamplesUsingFile would find this sound still+ // naming the one it just closed, then stop and free it, its event and its handle in the+ // middle of this call, and close that file a second time.+ closeFile(playing->m_file);+ playing->m_file = NULL;if (playing->m_type == PAT_Sample) {- closeFile(playing->m_file); // close it so as not to leak it.playing->m_file = playSample(playing->m_audioEventRTS, playing->m_sample);// If we don't have a file now, then we should drop to the stopped status so that@@ -1919,7 +1924,6 @@ void MilesAudioManager::notifyOfAudioCompletion( UnsignedIntPtr audioCompleted,return;}} else if (playing->m_type == PAT_3DSample) {- closeFile(playing->m_file); // close it so as not to leak it.playing->m_file = playSample3D(playing->m_audioEventRTS, playing->m_3DSample);// If we don't have a file now, then we should drop to the stopped status so thatdiff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/ffmpeg/bink_ffmpeg.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/ffmpeg/bink_ffmpeg.cppindex 04b1be7f5..e0b76fcb5 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/ffmpeg/bink_ffmpeg.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Bink/ffmpeg/bink_ffmpeg.cpp@@ -497,8 +497,9 @@ int __stdcall BinkGoto(HBINK handle, unsigned int frame, int)}#if defined(_WIN32)if (movie->voice != NULL) {+ // The flush lands on the mixer's next pass, which may still read what is queued, so the+ // chunks stay in audioQueued: queueAudioFrame pops them once BuffersQueued says they are gone.movie->voice->FlushSourceBuffers();- movie->audioQueued.clear();}#elseAIL_ex_flush_pcm(movie->pcm);diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cppindex bd27b995d..435799fcf 100644--- a/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cpp+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/Miles6/xaudio2/miles_xaudio2.cpp@@ -6,7 +6,8 @@* with the semantics that file relies on; the rest of Miles is not.** What maps onto what:- * HSAMPLE / H3DSAMPLE -> one IXAudio2SourceVoice each, fed a whole PCM WAV image in one buffer.+ * HSAMPLE / H3DSAMPLE -> one IXAudio2SourceVoice each, fed its own copy of a whole PCM WAV+ * image in one buffer.* HSTREAM / HAUDIO -> a file decoded by FFmpeg through the game's own file callbacks, fed to* a source voice in ~200ms buffers by the service thread below.* 3D positioning -> distance attenuation and a stereo pan computed here, not by XAudio2's@@ -226,13 +227,59 @@ unsigned int decodeAdpcm(const WaveImage &wave, std::vector<short> *out)// Voices// ---------------------------------------------------------------------------------------------+// The PCM a sample's voice reads. XAudio2 reads a submitted buffer on its own mixer thread until+// that buffer's OnBufferEnd, and Stop and FlushSourceBuffers only take effect on the mixer's next+// pass. A voice fed a pointer into the game's file image went on reading it after+// AIL_stop_sample had returned and the file cache had freed the image, which was an access+// violation inside xaudio2_9.dll with no game frame on the stack. The voice reads this copy+// instead: the Sample holds one reference and every buffer still queued holds another, which+// OnBufferEnd gives back on the mixer thread.+struct SampleData+{+ volatile LONG references;+ unsigned int bytes;++ const BYTE *pcm() const { return (const BYTE *)(this + 1); }+};++SampleData *copySampleData(const unsigned char *pcm, unsigned int bytes)+{+ SampleData *data = (SampleData *)malloc(sizeof(SampleData) + bytes);+ data->references = 1;+ data->bytes = bytes;+ memcpy(data + 1, pcm, bytes);+ return data;+}++void releaseSampleData(SampleData *data)+{+ if (data != NULL && InterlockedDecrement(&data->references) == 0) {+ free(data);+ }+}++// Runs on the mixer thread, so it takes no lock: DestroyVoice is called holding g_engine.lock and+// waits for this thread.+class SampleVoiceCallback : public IXAudio2VoiceCallback+{+public:+ STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) {}+ STDMETHOD_(void, OnVoiceProcessingPassEnd)() {}+ STDMETHOD_(void, OnStreamEnd)() {}+ STDMETHOD_(void, OnBufferStart)(void *) {}+ STDMETHOD_(void, OnBufferEnd)(void *context) { releaseSampleData((SampleData *)context); }+ STDMETHOD_(void, OnLoopEnd)(void *) {}+ STDMETHOD_(void, OnVoiceError)(void *, HRESULT) {}+};++SampleVoiceCallback g_sampleCallback;+struct Sample{bool is3D;IXAudio2SourceVoice *voice;WAVEFORMATEX format;- const unsigned char *data;- unsigned int dataBytes;+ SampleData *data;float volume;float pan;unsigned int baseRate;@@ -300,6 +347,7 @@ struct EngineHANDLE serviceThread;volatile LONG serviceRunning;std::vector<Sample *> samples;+ std::vector<IXAudio2SourceVoice *> retiredVoices;std::vector<Stream *> streams;Listener listener;bool started;@@ -413,6 +461,20 @@ void destroyVoice(IXAudio2SourceVoice **voice)}}+// A sample voice is not destroyed on the spot: the buffers still queued on it hold SampleData+// references, and only OnBufferEnd gives them back, which a destroyed voice no longer calls. It+// is stopped and flushed here, and the service thread destroys it once the mixer has retired its+// last buffer. Under the lock.+void retireVoice(IXAudio2SourceVoice **voice)+{+ if (*voice != NULL) {+ (*voice)->Stop(0);+ (*voice)->FlushSourceBuffers();+ g_engine.retiredVoices.push_back(*voice);+ *voice = NULL;+ }+}+bool ensureVoice(Sample *sample, const WAVEFORMATEX &format){if (g_engine.xaudio == NULL) {@@ -428,9 +490,9 @@ bool ensureVoice(Sample *sample, const WAVEFORMATEX &format)return true;}- destroyVoice(&sample->voice);+ retireVoice(&sample->voice);sample->format = format;- return SUCCEEDED(g_engine.xaudio->CreateSourceVoice(&sample->voice, &format, 0, 4.0f));+ return SUCCEEDED(g_engine.xaudio->CreateSourceVoice(&sample->voice, &format, 0, 4.0f, &g_sampleCallback));}// ---------------------------------------------------------------------------------------------@@ -686,6 +748,17 @@ DWORD WINAPI serviceThreadMain(LPVOID)std::vector<HSTREAM> streamHandles;EnterCriticalSection(&g_engine.lock);+ for (size_t i = 0; i < g_engine.retiredVoices.size(); ) {+ XAUDIO2_VOICE_STATE state;+ g_engine.retiredVoices[i]->GetState(&state);+ if (state.BuffersQueued == 0) {+ g_engine.retiredVoices[i]->DestroyVoice();+ g_engine.retiredVoices.erase(g_engine.retiredVoices.begin() + i);+ } else {+ ++i;+ }+ }+for (size_t i = 0; i < g_engine.samples.size(); ++i) {Sample *sample = g_engine.samples[i];if (sample->voice == NULL) {@@ -1065,6 +1138,13 @@ void AILCALL AIL_shutdown(void)g_engine.serviceThread = NULL;}+ // whatever the service thread had not destroyed yet; the references their buffers hold go with+ // the process+ for (size_t i = 0; i < g_engine.retiredVoices.size(); ++i) {+ g_engine.retiredVoices[i]->DestroyVoice();+ }+ g_engine.retiredVoices.clear();+if (g_engine.master != NULL) {g_engine.master->DestroyVoice();g_engine.master = NULL;@@ -1203,7 +1283,8 @@ void AILCALL AIL_release_sample_handle(HSAMPLE handle)break;}}- destroyVoice(&sample->voice);+ retireVoice(&sample->voice);+ releaseSampleData(sample->data);LeaveCriticalSection(&g_engine.lock);delete sample;}@@ -1224,8 +1305,8 @@ void AILCALL AIL_init_sample(HSAMPLE handle)sample->volume = 1.0f;sample->pan = DEFAULT_PAN;sample->endOfSample = NULL;+ releaseSampleData(sample->data);sample->data = NULL;- sample->dataBytes = 0;LeaveCriticalSection(&g_engine.lock);}@@ -1241,8 +1322,8 @@ S32 AILCALL AIL_set_sample_file(HSAMPLE handle, const void *file_image, S32)sample->playing = false;const bool ready = ensureVoice(sample, wave.format);if (ready) {- sample->data = wave.data;- sample->dataBytes = wave.dataBytes;+ releaseSampleData(sample->data);+ sample->data = copySampleData(wave.data, wave.dataBytes);sample->baseRate = wave.format.nSamplesPerSec;sample->rate = wave.format.nSamplesPerSec;refreshSampleOutput(sample);@@ -1264,13 +1345,17 @@ void AILCALL AIL_start_sample(HSAMPLE handle)XAUDIO2_BUFFER buffer;memset(&buffer, 0, sizeof(buffer));- buffer.AudioBytes = sample->dataBytes;- buffer.pAudioData = sample->data;+ buffer.AudioBytes = sample->data->bytes;+ buffer.pAudioData = sample->data->pcm();buffer.Flags = XAUDIO2_END_OF_STREAM;+ buffer.pContext = sample->data;+ InterlockedIncrement(&sample->data->references); // the buffer's, given back by OnBufferEndif (SUCCEEDED(sample->voice->SubmitSourceBuffer(&buffer))) {refreshSampleOutput(sample);sample->voice->Start(0);sample->playing = true;+ } else {+ releaseSampleData(sample->data);}LeaveCriticalSection(&g_engine.lock);}