Skip to content

Commit

Permalink
Merge pull request godotengine#11252 from marcelofg55/fix_noaudio_crash
Browse files Browse the repository at this point in the history
Fix crash when no audio driver is available
  • Loading branch information
akien-mga authored Sep 17, 2017
2 parents a00f19a + 7a4c0ff commit 53bbc04
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 83 deletions.
2 changes: 1 addition & 1 deletion drivers/rtaudio/audio_driver_rtaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Error AudioDriverRtAudio::init() {
}
}

return OK;
return active ? OK : ERR_UNAVAILABLE;
}

int AudioDriverRtAudio::get_mix_rate() const {
Expand Down
7 changes: 1 addition & 6 deletions platform/android/os_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ void OS_Android::initialize(const VideoMode &p_desired, int p_video_driver, int
visual_server->init();
// visual_server->cursor_set_visible(false, 0);

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {

ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

physics_server = memnew(PhysicsServerSW);
physics_server->init();
Expand Down
6 changes: 1 addition & 5 deletions platform/haiku/os_haiku.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,7 @@ void OS_Haiku::initialize(const VideoMode &p_desired, int p_video_driver, int p_
//physics_2d_server = Physics2DServerWrapMT::init_server<Physics2DServerSW>();
physics_2d_server->init();

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {
ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

power_manager = memnew(PowerHaiku);
}
Expand Down
6 changes: 1 addition & 5 deletions platform/javascript/os_javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,7 @@ void OS_JavaScript::initialize(const VideoMode &p_desired, int p_video_driver, i
print_line("Init Audio");

AudioDriverManager::add_driver(&audio_driver_javascript);
audio_driver_javascript.set_singleton();
if (audio_driver_javascript.init() != OK) {

ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

RasterizerGLES3::register_config();
RasterizerGLES3::make_current();
Expand Down
7 changes: 1 addition & 6 deletions platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1088,12 +1088,7 @@ static void keyboardLayoutChanged(CFNotificationCenterRef center, void *observer
visual_server->init();
// visual_server->cursor_set_visible(false, 0);

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {

ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

//
physics_server = memnew(PhysicsServerSW);
Expand Down
8 changes: 1 addition & 7 deletions platform/server/os_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,7 @@ void OS_Server::initialize(const VideoMode &p_desired, int p_video_driver, int p

//visual_server = memnew( VisualServerRaster(rasterizer) );

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {

ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

sample_manager = memnew(SampleManagerMallocSW);
audio_server = memnew(AudioServerSW(sample_manager));
Expand Down Expand Up @@ -232,7 +227,6 @@ void OS_Server::run() {

OS_Server::OS_Server() {

AudioDriverManager::add_driver(&driver_dummy);
//adriver here
grab = false;
};
2 changes: 0 additions & 2 deletions platform/server/os_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "drivers/rtaudio/audio_driver_rtaudio.h"
#include "drivers/unix/os_unix.h"
#include "main/input_default.h"
#include "servers/audio/audio_driver_dummy.h"
#include "servers/audio_server.h"
#include "servers/physics_2d/physics_2d_server_sw.h"
#include "servers/physics_server.h"
Expand All @@ -55,7 +54,6 @@ class OS_Server : public OS_Unix {
List<String> args;
MainLoop *main_loop;

AudioDriverDummy driver_dummy;
bool grab;

PhysicsServer *physics_server;
Expand Down
7 changes: 1 addition & 6 deletions platform/uwp/os_uwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,7 @@ void OSUWP::initialize(const VideoMode &p_desired, int p_video_driver, int p_aud
joypad = ref new JoypadUWP(input);
joypad->register_events();

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {

ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

power_manager = memnew(PowerUWP);

Expand Down
7 changes: 1 addition & 6 deletions platform/windows/os_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,12 +1081,7 @@ void OS_Windows::initialize(const VideoMode &p_desired, int p_video_driver, int

power_manager = memnew(PowerWindows);

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {

ERR_PRINT("Initializing audio failed.");
}
AudioDriverManager::initialize(p_audio_driver);

TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(TRACKMOUSEEVENT);
Expand Down
33 changes: 1 addition & 32 deletions platform/x11/os_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,29 +313,7 @@ void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_au
XFree(xsh);
}

AudioDriverManager::get_driver(p_audio_driver)->set_singleton();

audio_driver_index = p_audio_driver;
if (AudioDriverManager::get_driver(p_audio_driver)->init() != OK) {

bool success = false;
audio_driver_index = -1;
for (int i = 0; i < AudioDriverManager::get_driver_count(); i++) {
if (i == p_audio_driver)
continue;
AudioDriverManager::get_driver(i)->set_singleton();
if (AudioDriverManager::get_driver(i)->init() == OK) {
success = true;
print_line("Audio Driver Failed: " + String(AudioDriverManager::get_driver(p_audio_driver)->get_name()));
print_line("Using alternate audio driver: " + String(AudioDriverManager::get_driver(i)->get_name()));
audio_driver_index = i;
break;
}
}
if (!success) {
ERR_PRINT("Initializing audio failed.");
}
}
AudioDriverManager::initialize(p_audio_driver);

ERR_FAIL_COND(!visual_server);
ERR_FAIL_COND(x11_window == 0);
Expand Down Expand Up @@ -2189,10 +2167,6 @@ bool OS_X11::is_disable_crash_handler() const {

OS_X11::OS_X11() {

#ifdef RTAUDIO_ENABLED
AudioDriverManager::add_driver(&driver_rtaudio);
#endif

#ifdef PULSEAUDIO_ENABLED
AudioDriverManager::add_driver(&driver_pulseaudio);
#endif
Expand All @@ -2201,11 +2175,6 @@ OS_X11::OS_X11() {
AudioDriverManager::add_driver(&driver_alsa);
#endif

if (AudioDriverManager::get_driver_count() == 0) {
WARN_PRINT("No sound driver found... Defaulting to dummy driver");
AudioDriverManager::add_driver(&driver_dummy);
}

minimized = false;
xim_style = 0L;
mouse_mode = MOUSE_MODE_VISIBLE;
Expand Down
7 changes: 0 additions & 7 deletions platform/x11/os_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@
//#include "servers/visual/visual_server_wrap_mt.h"
#include "drivers/alsa/audio_driver_alsa.h"
#include "drivers/pulseaudio/audio_driver_pulseaudio.h"
#include "drivers/rtaudio/audio_driver_rtaudio.h"
#include "joypad_linux.h"
#include "main/input_default.h"
#include "power_x11.h"
#include "servers/audio/audio_driver_dummy.h"
#include "servers/audio_server.h"
#include "servers/physics_2d/physics_2d_server_sw.h"
#include "servers/physics_2d/physics_2d_server_wrap_mt.h"
Expand Down Expand Up @@ -154,18 +152,13 @@ class OS_X11 : public OS_Unix {
JoypadLinux *joypad;
#endif

#ifdef RTAUDIO_ENABLED
AudioDriverRtAudio driver_rtaudio;
#endif

#ifdef ALSA_ENABLED
AudioDriverALSA driver_alsa;
#endif

#ifdef PULSEAUDIO_ENABLED
AudioDriverPulseAudio driver_pulseaudio;
#endif
AudioDriverDummy driver_dummy;

Atom net_wm_icon;

Expand Down
39 changes: 39 additions & 0 deletions servers/audio_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "os/file_access.h"
#include "os/os.h"
#include "project_settings.h"
#include "servers/audio/audio_driver_dummy.h"
#include "servers/audio/effects/audio_effect_compressor.h"
#ifdef TOOLS_ENABLED

Expand Down Expand Up @@ -107,6 +108,7 @@ AudioDriver::AudioDriver() {

AudioDriver *AudioDriverManager::drivers[MAX_DRIVERS];
int AudioDriverManager::driver_count = 0;
AudioDriverDummy AudioDriverManager::dummy_driver;

void AudioDriverManager::add_driver(AudioDriver *p_driver) {

Expand All @@ -118,6 +120,43 @@ int AudioDriverManager::get_driver_count() {

return driver_count;
}

void AudioDriverManager::initialize(int p_driver) {
AudioDriver *driver;
int failed_driver = -1;

// Check if there is a selected driver
if (p_driver >= 0 && p_driver < driver_count) {
if (drivers[p_driver]->init() == OK) {
drivers[p_driver]->set_singleton();
return;
} else {
failed_driver = p_driver;
}
}

// No selected driver, try them all in order
for (int i = 0; i < driver_count; i++) {
// Don't re-init the driver if it failed above
if (i == failed_driver) {
continue;
}

if (drivers[i]->init() == OK) {
drivers[i]->set_singleton();
return;
}
}

// Fallback to our dummy driver
if (dummy_driver.init() == OK) {
ERR_PRINT("AudioDriverManager: all drivers failed, falling back to dummy driver");
dummy_driver.set_singleton();
} else {
ERR_PRINT("AudioDriverManager: dummy driver faild to init()");
}
}

AudioDriver *AudioDriverManager::get_driver(int p_driver) {

ERR_FAIL_INDEX_V(p_driver, driver_count, NULL);
Expand Down
5 changes: 5 additions & 0 deletions servers/audio_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "servers/audio/audio_effect.h"
#include "variant.h"

class AudioDriverDummy;

class AudioDriver {

static AudioDriver *singleton;
Expand Down Expand Up @@ -90,8 +92,11 @@ class AudioDriverManager {
static AudioDriver *drivers[MAX_DRIVERS];
static int driver_count;

static AudioDriverDummy dummy_driver;

public:
static void add_driver(AudioDriver *p_driver);
static void initialize(int p_driver);
static int get_driver_count();
static AudioDriver *get_driver(int p_driver);
};
Expand Down

0 comments on commit 53bbc04

Please sign in to comment.