Skip to content

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.

fix(milesaudio): finish a sound on the main thread, not the audio thread

Olcay Seygan26bc0c45001c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8780f2dbe..d6ced39d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -936,6 +936,8 @@ The whole HUD is on the hardware. Four colours carry it, and they mean the same
- Blowing up a full transport is survivable now, in every remaining variant.
- A unit can no longer load into a vehicle that no longer exists.
- Selling or losing an airfield under a jet on its takeoff roll no longer takes the game down. The jet takes off.
+- A sound ending at the same moment the game throws it away no longer takes the game down. The sound system tells the game a sound has finished from a thread of its own, and the game used to deal with it right there, picking the next loop or the tail of the sound, while the main game could be deleting that very sound. Two players' matches ended on "Technical Difficulties" that way on v2.0.0 and v2.0.1. The sound system now only notes which sound finished, and the game deals with it on its own next pass.
+- The First Decade finds its own copy of Generals. The collection registers its top folder as the base game's location and keeps Generals one folder down, beside Zero Hour, so the game said the original Generals files were missing on an install that has them.
- A looping sound coming round for its next pass no longer takes the game down. v2.0.0 lifted unit replies over the battle, and the check for whether a sound is a reply read a part of the sound that a looping one can have lost by then. A player's skirmish ended on "Technical Difficulties" twelve hundred frames in. A sound with nothing to say about itself now plays at the effects volume.
- The game starts on a machine that will not let it write to Documents. Windows' ransomware protection refuses a program it does not know yet, and a new 64-bit generals.exe is exactly that, so for some players v2.0.0 showed "Technical Difficulties" five seconds after every start: the first file the game wrote there was a leftover debugging file EA's code saved at the main menu, and when that failed nothing else could be written either, the crash report included. The debugging file is gone, and when Documents refuses the game, it keeps its settings, replays and saves in the same folder under your local application data instead.
- A network game with a computer player no longer falls apart because two players' processors do the maths slightly differently. v2.0.0 is the first 64-bit build, and the 64-bit Windows runtime picks between two versions of a logarithm depending on the processor, one bit apart. The computer player's unit choice used one, so two machines could see it buy different units. Every machine uses the same one now.
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h b/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h
index 2144e7e77..1d0cf7513 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/MilesAudioDevice/MilesAudioManager.h
@@ -24,6 +24,8 @@
#include "Common/GameAudio.h"
#include "MSS/MSS.h"
#include "mutex.h"
+#include <list>
+#include <map>
class AudioEventRTS;
@@ -343,6 +345,30 @@ class MilesAudioManager : public AudioManager
CriticalSectionClass m_playingStreamsCS;
CriticalSectionClass m_fadingAudioCS;
+ public:
+ /// Called by the audio thread when a sample or stream runs out; handled on the next update.
+ void queueAudioCompletion( UnsignedIntPtr audioCompleted, UnsignedInt flags );
+
+ protected:
+ /* The finished-sound callback used to run the whole completion - the next loop, the decay,
+ the stop - on the XAudio2 service thread, holding a PlayingAudio and its AudioEventRTS that
+ the main thread was free to delete at the same moment. v2.0.0 and v2.0.1 players crashed
+ in it reading freed memory. The thread now only writes the handle down, and the main
+ thread, the one that frees those objects, does the rest at the top of update(). A handle
+ is remembered with how many times it had been started when it finished, so a completion
+ that arrives after the sample was handed to another sound is dropped, not applied to it. */
+ struct CompletedAudio
+ {
+ UnsignedIntPtr handle;
+ UnsignedInt flags;
+ UnsignedInt startsAtCompletion;
+ };
+ void noteAudioStarted( UnsignedIntPtr handle );
+ void processCompletedAudio( void );
+ std::list<CompletedAudio> m_completedAudio;
+ std::map<UnsignedIntPtr, UnsignedInt> m_audioStarts;
+ CriticalSectionClass m_completedAudioCS;
+
AudioFileCache *m_audioCache;
PlayingAudio *m_binkHandle;
UnsignedInt m_num2DSamples;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
index c1d710d78..a6a89a289 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/MilesAudioDevice/MilesAudioManager.cpp
@@ -572,6 +572,7 @@ static void updateSoundCapture( void )
void MilesAudioManager::update()
{
AudioManager::update();
+ processCompletedAudio();
updateSoundCapture();
setDeviceListenerPosition();
processRequestList();
@@ -1713,6 +1714,45 @@ Bool MilesAudioManager::isCurrentlyPlaying( AudioHandle handle )
return false;
}
+//-------------------------------------------------------------------------------------------------
+void MilesAudioManager::queueAudioCompletion( UnsignedIntPtr audioCompleted, UnsignedInt flags )
+{
+ CriticalSectionClass::LockClass lock(m_completedAudioCS);
+ CompletedAudio completed;
+ completed.handle = audioCompleted;
+ completed.flags = flags;
+ completed.startsAtCompletion = m_audioStarts[audioCompleted];
+ m_completedAudio.push_back(completed);
+}
+
+//-------------------------------------------------------------------------------------------------
+void MilesAudioManager::noteAudioStarted( UnsignedIntPtr handle )
+{
+ CriticalSectionClass::LockClass lock(m_completedAudioCS);
+ ++m_audioStarts[handle];
+}
+
+//-------------------------------------------------------------------------------------------------
+void MilesAudioManager::processCompletedAudio( void )
+{
+ std::list<CompletedAudio> completedAudio;
+ {
+ CriticalSectionClass::LockClass lock(m_completedAudioCS);
+ completedAudio.swap(m_completedAudio);
+ }
+
+ for (std::list<CompletedAudio>::const_iterator it = completedAudio.begin(); it != completedAudio.end(); ++it) {
+ Bool restartedSince;
+ {
+ CriticalSectionClass::LockClass lock(m_completedAudioCS);
+ restartedSince = m_audioStarts[it->handle] != it->startsAtCompletion;
+ }
+ if (!restartedSince) {
+ notifyOfAudioCompletion(it->handle, it->flags);
+ }
+ }
+}
+
//-------------------------------------------------------------------------------------------------
void MilesAudioManager::notifyOfAudioCompletion( UnsignedIntPtr audioCompleted, UnsignedInt flags )
{
@@ -2991,6 +3031,7 @@ void MilesAudioManager::playStream( AudioEventRTS *event, HSTREAM stream )
}
AIL_register_stream_callback(stream, setStreamCompleted);
+ noteAudioStarted((UnsignedIntPtr) stream); // before the start: a stream that ends at once still counts as this one
AIL_start_stream(stream);
if (event->getAudioEventInfo()->m_soundType == AT_Music) {
// Need to stop/fade out the old music here.
@@ -3013,6 +3054,7 @@ void *MilesAudioManager::playSample( AudioEventRTS *event, HSAMPLE sample )
AIL_set_sample_file(sample, fileBuffer, 0);
// Start playback
+ noteAudioStarted((UnsignedIntPtr) sample);
AIL_start_sample(sample);
}
@@ -3047,6 +3089,7 @@ void *MilesAudioManager::playSample3D( AudioEventRTS *event, H3DSAMPLE sample3D
initFilters3D(sample3D, event, pos);
// Start playback
+ noteAudioStarted((UnsignedIntPtr) sample3D);
AIL_start_3D_sample(sample3D);
}
return fileBuffer;
@@ -3254,19 +3297,19 @@ void MilesAudioManager::friend_forcePlayAudioEventRTS(const AudioEventRTS* event
//-------------------------------------------------------------------------------------------------
void AILCALLBACK setSampleCompleted( HSAMPLE sampleCompleted )
{
- TheAudio->notifyOfAudioCompletion((UnsignedIntPtr) sampleCompleted, PAT_Sample);
+ static_cast<MilesAudioManager *>(TheAudio)->queueAudioCompletion((UnsignedIntPtr) sampleCompleted, PAT_Sample);
}
//-------------------------------------------------------------------------------------------------
void AILCALLBACK set3DSampleCompleted( H3DSAMPLE sample3DCompleted )
{
- TheAudio->notifyOfAudioCompletion((UnsignedIntPtr) sample3DCompleted, PAT_3DSample);
+ static_cast<MilesAudioManager *>(TheAudio)->queueAudioCompletion((UnsignedIntPtr) sample3DCompleted, PAT_3DSample);
}
//-------------------------------------------------------------------------------------------------
void AILCALLBACK setStreamCompleted( HSTREAM streamCompleted )
{
- TheAudio->notifyOfAudioCompletion((UnsignedIntPtr) streamCompleted, PAT_Stream);
+ static_cast<MilesAudioManager *>(TheAudio)->queueAudioCompletion((UnsignedIntPtr) streamCompleted, PAT_Stream);
}
//-------------------------------------------------------------------------------------------------