Don't lock the device state when preparing a new effect state
[openal-soft.git] / alc / backends / sdl2.cpp
blobec3be6b0e689989e3bfd815b72474d497f5287e0
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2018 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include "sdl2.h"
25 #include <cassert>
26 #include <cstdlib>
27 #include <cstring>
28 #include <string>
29 #include <string_view>
31 #include "almalloc.h"
32 #include "alnumeric.h"
33 #include "core/device.h"
34 #include "core/logging.h"
36 _Pragma("GCC diagnostic push")
37 _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"")
38 #include "SDL.h"
39 _Pragma("GCC diagnostic pop")
42 namespace {
44 #ifdef _WIN32
45 #define DEVNAME_PREFIX "OpenAL Soft on "
46 #else
47 #define DEVNAME_PREFIX ""
48 #endif
50 constexpr auto getDevicePrefix() noexcept -> std::string_view { return DEVNAME_PREFIX; }
51 constexpr auto getDefaultDeviceName() noexcept -> std::string_view
52 { return DEVNAME_PREFIX "Default Device"; }
54 struct Sdl2Backend final : public BackendBase {
55 Sdl2Backend(DeviceBase *device) noexcept : BackendBase{device} { }
56 ~Sdl2Backend() override;
58 void audioCallback(Uint8 *stream, int len) noexcept;
60 void open(std::string_view name) override;
61 bool reset() override;
62 void start() override;
63 void stop() override;
65 SDL_AudioDeviceID mDeviceID{0u};
66 uint mFrameSize{0};
68 uint mFrequency{0u};
69 DevFmtChannels mFmtChans{};
70 DevFmtType mFmtType{};
71 uint mUpdateSize{0u};
74 Sdl2Backend::~Sdl2Backend()
76 if(mDeviceID)
77 SDL_CloseAudioDevice(mDeviceID);
78 mDeviceID = 0;
81 void Sdl2Backend::audioCallback(Uint8 *stream, int len) noexcept
83 const auto ulen = static_cast<unsigned int>(len);
84 assert((ulen % mFrameSize) == 0);
85 mDevice->renderSamples(stream, ulen / mFrameSize, mDevice->channelsFromFmt());
88 void Sdl2Backend::open(std::string_view name)
90 SDL_AudioSpec want{}, have{};
92 want.freq = static_cast<int>(mDevice->Frequency);
93 switch(mDevice->FmtType)
95 case DevFmtUByte: want.format = AUDIO_U8; break;
96 case DevFmtByte: want.format = AUDIO_S8; break;
97 case DevFmtUShort: want.format = AUDIO_U16SYS; break;
98 case DevFmtShort: want.format = AUDIO_S16SYS; break;
99 case DevFmtUInt: /* fall-through */
100 case DevFmtInt: want.format = AUDIO_S32SYS; break;
101 case DevFmtFloat: want.format = AUDIO_F32; break;
103 want.channels = (mDevice->FmtChans == DevFmtMono) ? 1 : 2;
104 want.samples = static_cast<Uint16>(std::min(mDevice->UpdateSize, 8192u));
105 want.callback = [](void *ptr, Uint8 *stream, int len) noexcept
106 { return static_cast<Sdl2Backend*>(ptr)->audioCallback(stream, len); };
107 want.userdata = this;
109 /* Passing nullptr to SDL_OpenAudioDevice opens a default, which isn't
110 * necessarily the first in the list.
112 const auto defaultDeviceName = getDefaultDeviceName();
113 SDL_AudioDeviceID devid;
114 if(name.empty() || name == defaultDeviceName)
116 name = defaultDeviceName;
117 devid = SDL_OpenAudioDevice(nullptr, SDL_FALSE, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
119 else
121 const auto namePrefix = getDevicePrefix();
122 if(name.size() >= namePrefix.size() && name.substr(0, namePrefix.size()) == namePrefix)
124 /* Copy the string_view to a string to ensure it's null terminated
125 * for this call.
127 const std::string devname{name.substr(namePrefix.size())};
128 devid = SDL_OpenAudioDevice(devname.c_str(), SDL_FALSE, &want, &have,
129 SDL_AUDIO_ALLOW_ANY_CHANGE);
131 else
133 const std::string devname{name};
134 devid = SDL_OpenAudioDevice(devname.c_str(), SDL_FALSE, &want, &have,
135 SDL_AUDIO_ALLOW_ANY_CHANGE);
138 if(!devid)
139 throw al::backend_exception{al::backend_error::NoDevice, "%s", SDL_GetError()};
141 DevFmtChannels devchans{};
142 if(have.channels >= 2)
143 devchans = DevFmtStereo;
144 else if(have.channels == 1)
145 devchans = DevFmtMono;
146 else
148 SDL_CloseAudioDevice(devid);
149 throw al::backend_exception{al::backend_error::DeviceError,
150 "Unhandled SDL channel count: %d", int{have.channels}};
153 DevFmtType devtype{};
154 switch(have.format)
156 case AUDIO_U8: devtype = DevFmtUByte; break;
157 case AUDIO_S8: devtype = DevFmtByte; break;
158 case AUDIO_U16SYS: devtype = DevFmtUShort; break;
159 case AUDIO_S16SYS: devtype = DevFmtShort; break;
160 case AUDIO_S32SYS: devtype = DevFmtInt; break;
161 case AUDIO_F32SYS: devtype = DevFmtFloat; break;
162 default:
163 SDL_CloseAudioDevice(devid);
164 throw al::backend_exception{al::backend_error::DeviceError, "Unhandled SDL format: 0x%04x",
165 have.format};
168 if(mDeviceID)
169 SDL_CloseAudioDevice(mDeviceID);
170 mDeviceID = devid;
172 mFrameSize = BytesFromDevFmt(devtype) * have.channels;
173 mFrequency = static_cast<uint>(have.freq);
174 mFmtChans = devchans;
175 mFmtType = devtype;
176 mUpdateSize = have.samples;
178 mDevice->DeviceName = name;
181 bool Sdl2Backend::reset()
183 mDevice->Frequency = mFrequency;
184 mDevice->FmtChans = mFmtChans;
185 mDevice->FmtType = mFmtType;
186 mDevice->UpdateSize = mUpdateSize;
187 mDevice->BufferSize = mUpdateSize * 2; /* SDL always (tries to) use two periods. */
188 setDefaultWFXChannelOrder();
189 return true;
192 void Sdl2Backend::start()
193 { SDL_PauseAudioDevice(mDeviceID, 0); }
195 void Sdl2Backend::stop()
196 { SDL_PauseAudioDevice(mDeviceID, 1); }
198 } // namespace
200 BackendFactory &SDL2BackendFactory::getFactory()
202 static SDL2BackendFactory factory{};
203 return factory;
206 bool SDL2BackendFactory::init()
207 { return (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0); }
209 bool SDL2BackendFactory::querySupport(BackendType type)
210 { return type == BackendType::Playback; }
212 auto SDL2BackendFactory::enumerate(BackendType type) -> std::vector<std::string>
214 std::vector<std::string> outnames;
216 if(type != BackendType::Playback)
217 return outnames;
219 int num_devices{SDL_GetNumAudioDevices(SDL_FALSE)};
220 if(num_devices <= 0)
221 return outnames;
223 outnames.reserve(static_cast<unsigned int>(num_devices));
224 outnames.emplace_back(getDefaultDeviceName());
225 for(int i{0};i < num_devices;++i)
227 std::string outname{getDevicePrefix()};
228 if(const char *name = SDL_GetAudioDeviceName(i, SDL_FALSE))
229 outname += name;
230 else
231 outname += "Unknown Device Name #"+std::to_string(i);
232 outnames.emplace_back(std::move(outname));
234 return outnames;
237 BackendPtr SDL2BackendFactory::createBackend(DeviceBase *device, BackendType type)
239 if(type == BackendType::Playback)
240 return BackendPtr{new Sdl2Backend{device}};
241 return nullptr;