1 /*
2 * common.h
3 * Copyright (C) 2018 Meltytech, LLC
4 * Author: Brian Matherly <code@brianmatherly.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "common.h"
22
23 #include <framework/mlt_log.h>
24
sdl2_open_audio(const SDL_AudioSpec * desired,SDL_AudioSpec * obtained)25 SDL_AudioDeviceID sdl2_open_audio( const SDL_AudioSpec* desired, SDL_AudioSpec* obtained )
26 {
27 SDL_AudioDeviceID dev = 0;
28
29 // First try to open using default/user requested driver.
30 dev = SDL_OpenAudioDevice( NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_CHANNELS_CHANGE );
31
32 if( dev == 0 )
33 {
34 mlt_log_info( NULL, "Failed to open audio device: %s\n", SDL_GetError() );
35
36 // Try alternative drivers.
37 int i = 0;
38 int driver_count = SDL_GetNumAudioDrivers();
39
40 for( i = 0; i < driver_count; i++ )
41 {
42 const char* driver = SDL_GetAudioDriver( i );
43 if( strcmp( driver, "disk" ) == 0 || strcmp( driver, "dummy" ) == 0 )
44 {
45 continue;
46 }
47 if( SDL_AudioInit( driver ) != 0 )
48 {
49 continue;
50 }
51 mlt_log_info( NULL, "[sdl2] Try alternative driver: %s\n", driver );
52 dev = SDL_OpenAudioDevice( NULL, 0, desired, obtained, SDL_AUDIO_ALLOW_CHANNELS_CHANGE );
53 if( dev != 0 )
54 {
55 break;
56 }
57 else
58 {
59 mlt_log_info( NULL, "[sdl2] Open failed: %s\n", SDL_GetError() );
60 }
61 }
62 }
63
64 if( dev == 0 && desired->channels > 2 )
65 {
66 // All drivers have failed to open with the provided spec.
67 // Try stereo channels since all drivers support that.
68 mlt_log_info( NULL, "Failed to open surround device. Try stereo instead\n" );
69 SDL_AudioSpec desired_copy = *desired;
70 desired_copy.channels = 2;
71 SDL_AudioInit( NULL );
72 dev = sdl2_open_audio( &desired_copy, obtained );
73 }
74
75 return dev;
76 }