Merge pull request #204 from jhasse/android-byte-order
[openal-soft.git] / Alc / backends / solaris.c
blobf1c4aeaa2758221fcb28e7c43157b0544563e4c9
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 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 <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/time.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <memory.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <math.h>
35 #include "alMain.h"
36 #include "alu.h"
37 #include "alconfig.h"
38 #include "threads.h"
39 #include "compat.h"
41 #include "backends/base.h"
43 #include <sys/audioio.h>
46 typedef struct ALCsolarisBackend {
47 DERIVE_FROM_TYPE(ALCbackend);
49 int fd;
51 ALubyte *mix_data;
52 int data_size;
54 ATOMIC(ALenum) killNow;
55 althrd_t thread;
56 } ALCsolarisBackend;
58 static int ALCsolarisBackend_mixerProc(void *ptr);
60 static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device);
61 static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self);
62 static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name);
63 static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self);
64 static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self);
65 static void ALCsolarisBackend_stop(ALCsolarisBackend *self);
66 static DECLARE_FORWARD2(ALCsolarisBackend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
67 static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, ALCuint, availableSamples)
68 static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, ClockLatency, getClockLatency)
69 static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, void, lock)
70 static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, void, unlock)
71 DECLARE_DEFAULT_ALLOCATORS(ALCsolarisBackend)
73 DEFINE_ALCBACKEND_VTABLE(ALCsolarisBackend);
76 static const ALCchar solaris_device[] = "Solaris Default";
78 static const char *solaris_driver = "/dev/audio";
81 static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device)
83 ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
84 SET_VTABLE2(ALCsolarisBackend, ALCbackend, self);
86 self->fd = -1;
87 self->mix_data = NULL;
88 ATOMIC_INIT(&self->killNow, AL_FALSE);
91 static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self)
93 if(self->fd != -1)
94 close(self->fd);
95 self->fd = -1;
97 free(self->mix_data);
98 self->mix_data = NULL;
99 self->data_size = 0;
101 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
105 static int ALCsolarisBackend_mixerProc(void *ptr)
107 ALCsolarisBackend *self = ptr;
108 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
109 struct timeval timeout;
110 ALubyte *write_ptr;
111 ALint frame_size;
112 ALint to_write;
113 ssize_t wrote;
114 fd_set wfds;
115 int sret;
117 SetRTPriority();
118 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
120 frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
122 ALCsolarisBackend_lock(self);
123 while(!ATOMIC_LOAD(&self->killNow, almemory_order_acquire) &&
124 ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
126 FD_ZERO(&wfds);
127 FD_SET(self->fd, &wfds);
128 timeout.tv_sec = 1;
129 timeout.tv_usec = 0;
131 ALCsolarisBackend_unlock(self);
132 sret = select(self->fd+1, NULL, &wfds, NULL, &timeout);
133 ALCsolarisBackend_lock(self);
134 if(sret < 0)
136 if(errno == EINTR)
137 continue;
138 ERR("select failed: %s\n", strerror(errno));
139 aluHandleDisconnect(device, "Failed to wait for playback buffer: %s", strerror(errno));
140 break;
142 else if(sret == 0)
144 WARN("select timeout\n");
145 continue;
148 write_ptr = self->mix_data;
149 to_write = self->data_size;
150 aluMixData(device, write_ptr, to_write/frame_size);
151 while(to_write > 0 && !ATOMIC_LOAD_SEQ(&self->killNow))
153 wrote = write(self->fd, write_ptr, to_write);
154 if(wrote < 0)
156 if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
157 continue;
158 ERR("write failed: %s\n", strerror(errno));
159 aluHandleDisconnect(device, "Failed to write playback samples: %s",
160 strerror(errno));
161 break;
164 to_write -= wrote;
165 write_ptr += wrote;
168 ALCsolarisBackend_unlock(self);
170 return 0;
174 static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name)
176 ALCdevice *device;
178 if(!name)
179 name = solaris_device;
180 else if(strcmp(name, solaris_device) != 0)
181 return ALC_INVALID_VALUE;
183 self->fd = open(solaris_driver, O_WRONLY);
184 if(self->fd == -1)
186 ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
187 return ALC_INVALID_VALUE;
190 device = STATIC_CAST(ALCbackend,self)->mDevice;
191 alstr_copy_cstr(&device->DeviceName, name);
193 return ALC_NO_ERROR;
196 static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self)
198 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
199 audio_info_t info;
200 ALsizei frameSize;
201 ALsizei numChannels;
203 AUDIO_INITINFO(&info);
205 info.play.sample_rate = device->Frequency;
207 if(device->FmtChans != DevFmtMono)
208 device->FmtChans = DevFmtStereo;
209 numChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
210 info.play.channels = numChannels;
212 switch(device->FmtType)
214 case DevFmtByte:
215 info.play.precision = 8;
216 info.play.encoding = AUDIO_ENCODING_LINEAR;
217 break;
218 case DevFmtUByte:
219 info.play.precision = 8;
220 info.play.encoding = AUDIO_ENCODING_LINEAR8;
221 break;
222 case DevFmtUShort:
223 case DevFmtInt:
224 case DevFmtUInt:
225 case DevFmtFloat:
226 device->FmtType = DevFmtShort;
227 /* fall-through */
228 case DevFmtShort:
229 info.play.precision = 16;
230 info.play.encoding = AUDIO_ENCODING_LINEAR;
231 break;
234 frameSize = numChannels * BytesFromDevFmt(device->FmtType);
235 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
237 if(ioctl(self->fd, AUDIO_SETINFO, &info) < 0)
239 ERR("ioctl failed: %s\n", strerror(errno));
240 return ALC_FALSE;
243 if(ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder) != (ALsizei)info.play.channels)
245 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), info.play.channels);
246 return ALC_FALSE;
249 if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
250 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
251 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
252 (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
254 ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
255 info.play.precision, info.play.encoding);
256 return ALC_FALSE;
259 device->Frequency = info.play.sample_rate;
260 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
262 SetDefaultChannelOrder(device);
264 free(self->mix_data);
265 self->data_size = device->UpdateSize * FrameSizeFromDevFmt(
266 device->FmtChans, device->FmtType, device->AmbiOrder
268 self->mix_data = calloc(1, self->data_size);
270 return ALC_TRUE;
273 static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self)
275 ATOMIC_STORE_SEQ(&self->killNow, AL_FALSE);
276 if(althrd_create(&self->thread, ALCsolarisBackend_mixerProc, self) != althrd_success)
277 return ALC_FALSE;
278 return ALC_TRUE;
281 static void ALCsolarisBackend_stop(ALCsolarisBackend *self)
283 int res;
285 if(ATOMIC_EXCHANGE_SEQ(&self->killNow, AL_TRUE))
286 return;
288 althrd_join(self->thread, &res);
290 if(ioctl(self->fd, AUDIO_DRAIN) < 0)
291 ERR("Error draining device: %s\n", strerror(errno));
295 typedef struct ALCsolarisBackendFactory {
296 DERIVE_FROM_TYPE(ALCbackendFactory);
297 } ALCsolarisBackendFactory;
298 #define ALCSOLARISBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCsolarisBackendFactory, ALCbackendFactory) } }
300 ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void);
302 static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory *self);
303 static DECLARE_FORWARD(ALCsolarisBackendFactory, ALCbackendFactory, void, deinit)
304 static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory *self, ALCbackend_Type type);
305 static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory *self, enum DevProbe type);
306 static ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
307 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCsolarisBackendFactory);
310 ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void)
312 static ALCsolarisBackendFactory factory = ALCSOLARISBACKENDFACTORY_INITIALIZER;
313 return STATIC_CAST(ALCbackendFactory, &factory);
317 static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory* UNUSED(self))
319 ConfigValueStr(NULL, "solaris", "device", &solaris_driver);
320 return ALC_TRUE;
323 static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory* UNUSED(self), ALCbackend_Type type)
325 if(type == ALCbackend_Playback)
326 return ALC_TRUE;
327 return ALC_FALSE;
330 static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory* UNUSED(self), enum DevProbe type)
332 switch(type)
334 case ALL_DEVICE_PROBE:
336 #ifdef HAVE_STAT
337 struct stat buf;
338 if(stat(solaris_driver, &buf) == 0)
339 #endif
340 AppendAllDevicesList(solaris_device);
342 break;
344 case CAPTURE_DEVICE_PROBE:
345 break;
349 ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
351 if(type == ALCbackend_Playback)
353 ALCsolarisBackend *backend;
354 NEW_OBJ(backend, ALCsolarisBackend)(device);
355 if(!backend) return NULL;
356 return STATIC_CAST(ALCbackend, backend);
359 return NULL;