Merge pull request #4074 from lioncash/soundstream

AudioCommon: Make the SoundStream global a unique_ptr
This commit is contained in:
Pierre Bourdon 2016-08-01 00:37:48 +02:00 committed by GitHub
commit 007df77fba
2 changed files with 28 additions and 35 deletions

View File

@ -21,7 +21,7 @@
#include "Core/Movie.h" #include "Core/Movie.h"
// This shouldn't be a global, at least not here. // This shouldn't be a global, at least not here.
SoundStream* g_sound_stream = nullptr; std::unique_ptr<SoundStream> g_sound_stream;
static bool s_audio_dump_start = false; static bool s_audio_dump_start = false;
@ -30,61 +30,51 @@ namespace AudioCommon
static const int AUDIO_VOLUME_MIN = 0; static const int AUDIO_VOLUME_MIN = 0;
static const int AUDIO_VOLUME_MAX = 100; static const int AUDIO_VOLUME_MAX = 100;
SoundStream* InitSoundStream() void InitSoundStream()
{ {
std::string backend = SConfig::GetInstance().sBackend; std::string backend = SConfig::GetInstance().sBackend;
if (backend == BACKEND_OPENAL && OpenALStream::isValid()) if (backend == BACKEND_OPENAL && OpenALStream::isValid())
g_sound_stream = new OpenALStream(); g_sound_stream = std::make_unique<OpenALStream>();
else if (backend == BACKEND_NULLSOUND && NullSound::isValid()) else if (backend == BACKEND_NULLSOUND && NullSound::isValid())
g_sound_stream = new NullSound(); g_sound_stream = std::make_unique<NullSound>();
else if (backend == BACKEND_XAUDIO2) else if (backend == BACKEND_XAUDIO2)
{ {
if (XAudio2::isValid()) if (XAudio2::isValid())
g_sound_stream = new XAudio2(); g_sound_stream = std::make_unique<XAudio2>();
else if (XAudio2_7::isValid()) else if (XAudio2_7::isValid())
g_sound_stream = new XAudio2_7(); g_sound_stream = std::make_unique<XAudio2_7>();
} }
else if (backend == BACKEND_AOSOUND && AOSound::isValid()) else if (backend == BACKEND_AOSOUND && AOSound::isValid())
g_sound_stream = new AOSound(); g_sound_stream = std::make_unique<AOSound>();
else if (backend == BACKEND_ALSA && AlsaSound::isValid()) else if (backend == BACKEND_ALSA && AlsaSound::isValid())
g_sound_stream = new AlsaSound(); g_sound_stream = std::make_unique<AlsaSound>();
else if (backend == BACKEND_COREAUDIO && CoreAudioSound::isValid()) else if (backend == BACKEND_COREAUDIO && CoreAudioSound::isValid())
g_sound_stream = new CoreAudioSound(); g_sound_stream = std::make_unique<CoreAudioSound>();
else if (backend == BACKEND_PULSEAUDIO && PulseAudio::isValid()) else if (backend == BACKEND_PULSEAUDIO && PulseAudio::isValid())
g_sound_stream = new PulseAudio(); g_sound_stream = std::make_unique<PulseAudio>();
else if (backend == BACKEND_OPENSLES && OpenSLESStream::isValid()) else if (backend == BACKEND_OPENSLES && OpenSLESStream::isValid())
g_sound_stream = new OpenSLESStream(); g_sound_stream = std::make_unique<OpenSLESStream>();
if (!g_sound_stream && NullSound::isValid()) if (!g_sound_stream && NullSound::isValid())
{ {
WARN_LOG(AUDIO, "Could not initialize backend %s, using %s instead.", backend.c_str(), WARN_LOG(AUDIO, "Could not initialize backend %s, using %s instead.", backend.c_str(),
BACKEND_NULLSOUND); BACKEND_NULLSOUND);
g_sound_stream = new NullSound(); g_sound_stream = std::make_unique<NullSound>();
} }
if (g_sound_stream) UpdateSoundStream();
if (!g_sound_stream->Start())
{ {
UpdateSoundStream(); ERROR_LOG(AUDIO, "Could not start backend %s, using %s instead", backend.c_str(),
if (!g_sound_stream->Start()) BACKEND_NULLSOUND);
{
ERROR_LOG(AUDIO, "Could not start backend %s, using %s instead", backend.c_str(),
BACKEND_NULLSOUND);
delete g_sound_stream;
g_sound_stream = new NullSound();
g_sound_stream->Start();
}
if (SConfig::GetInstance().m_DumpAudio && !s_audio_dump_start) g_sound_stream = std::make_unique<NullSound>();
StartAudioDump(); g_sound_stream->Start();
return g_sound_stream;
} }
PanicAlertT("Sound backend %s is not valid.", backend.c_str()); if (SConfig::GetInstance().m_DumpAudio && !s_audio_dump_start)
StartAudioDump();
delete g_sound_stream;
g_sound_stream = nullptr;
return nullptr;
} }
void ShutdownSoundStream() void ShutdownSoundStream()
@ -94,10 +84,11 @@ void ShutdownSoundStream()
if (g_sound_stream) if (g_sound_stream)
{ {
g_sound_stream->Stop(); g_sound_stream->Stop();
if (SConfig::GetInstance().m_DumpAudio && s_audio_dump_start) if (SConfig::GetInstance().m_DumpAudio && s_audio_dump_start)
StopAudioDump(); StopAudioDump();
delete g_sound_stream;
g_sound_stream = nullptr; g_sound_stream.reset();
} }
INFO_LOG(AUDIO, "Done shutting down sound stream"); INFO_LOG(AUDIO, "Done shutting down sound stream");

View File

@ -4,16 +4,18 @@
#pragma once #pragma once
#include <memory>
#include "AudioCommon/SoundStream.h" #include "AudioCommon/SoundStream.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
class CMixer; class CMixer;
extern SoundStream* g_sound_stream; extern std::unique_ptr<SoundStream> g_sound_stream;
namespace AudioCommon namespace AudioCommon
{ {
SoundStream* InitSoundStream(); void InitSoundStream();
void ShutdownSoundStream(); void ShutdownSoundStream();
std::vector<std::string> GetSoundBackends(); std::vector<std::string> GetSoundBackends();
void UpdateSoundStream(); void UpdateSoundStream();