Implement opening and closing devices in the router
[openal-soft.git] / Alc / backends / solaris.c
blob5dfb50845e6d3721f127165bd6466c40d58065e6
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 "threads.h"
38 #include "compat.h"
40 #include "backends/base.h"
42 #include <sys/audioio.h>
45 typedef struct ALCsolarisBackend {
46 DERIVE_FROM_TYPE(ALCbackend);
48 int fd;
50 ALubyte *mix_data;
51 int data_size;
53 ATOMIC(ALenum) killNow;
54 althrd_t thread;
55 } ALCsolarisBackend;
57 static int ALCsolarisBackend_mixerProc(void *ptr);
59 static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device);
60 static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self);
61 static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name);
62 static void ALCsolarisBackend_close(ALCsolarisBackend *self);
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 ATOMIC_INIT(&self->killNow, AL_FALSE);
90 static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self)
92 if(self->fd != -1)
93 close(self->fd);
94 self->fd = -1;
96 free(self->mix_data);
97 self->mix_data = NULL;
98 self->data_size = 0;
100 ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
104 static int ALCsolarisBackend_mixerProc(void *ptr)
106 ALCsolarisBackend *self = ptr;
107 ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
108 struct timeval timeout;
109 ALubyte *write_ptr;
110 ALint frame_size;
111 ALint to_write;
112 ssize_t wrote;
113 fd_set wfds;
114 int sret;
116 SetRTPriority();
117 althrd_setname(althrd_current(), MIXER_THREAD_NAME);
119 frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
121 ALCsolarisBackend_lock(self);
122 while(!ATOMIC_LOAD_SEQ(&self->killNow) && device->Connected)
124 FD_ZERO(&wfds);
125 FD_SET(self->fd, &wfds);
126 timeout.tv_sec = 1;
127 timeout.tv_usec = 0;
129 ALCsolarisBackend_unlock(self);
130 sret = select(self->fd+1, NULL, &wfds, NULL, &timeout);
131 ALCsolarisBackend_lock(self);
132 if(sret < 0)
134 if(errno == EINTR)
135 continue;
136 ERR("select failed: %s\n", strerror(errno));
137 aluHandleDisconnect(device);
138 break;
140 else if(sret == 0)
142 WARN("select timeout\n");
143 continue;
146 write_ptr = self->mix_data;
147 to_write = self->data_size;
148 aluMixData(device, write_ptr, to_write/frame_size);
149 while(to_write > 0 && !ATOMIC_LOAD_SEQ(&self->killNow))
151 wrote = write(self->fd, write_ptr, to_write);
152 if(wrote < 0)
154 if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
155 continue;
156 ERR("write failed: %s\n", strerror(errno));
157 aluHandleDisconnect(device);
158 break;
161 to_write -= wrote;
162 write_ptr += wrote;
165 ALCsolarisBackend_unlock(self);
167 return 0;
171 static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name)
173 ALCdevice *device;
175 if(!name)
176 name = solaris_device;
177 else if(strcmp(name, solaris_device) != 0)
178 return ALC_INVALID_VALUE;
180 self->fd = open(solaris_driver, O_WRONLY);
181 if(self->fd == -1)
183 ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
184 return ALC_INVALID_VALUE;
187 device = STATIC_CAST(ALCbackend,self)->mDevice;
188 alstr_copy_cstr(&device->DeviceName, name);
190 return ALC_NO_ERROR;
193 static void ALCsolarisBackend_close(ALCsolarisBackend *self)
195 close(self->fd);
196 self->fd = -1;
199 static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self)
201 ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
202 audio_info_t info;
203 ALsizei frameSize;
204 ALsizei numChannels;
206 AUDIO_INITINFO(&info);
208 info.play.sample_rate = device->Frequency;
210 if(device->FmtChans != DevFmtMono)
211 device->FmtChans = DevFmtStereo;
212 numChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
213 info.play.channels = numChannels;
215 switch(device->FmtType)
217 case DevFmtByte:
218 info.play.precision = 8;
219 info.play.encoding = AUDIO_ENCODING_LINEAR;
220 break;
221 case DevFmtUByte:
222 info.play.precision = 8;
223 info.play.encoding = AUDIO_ENCODING_LINEAR8;
224 break;
225 case DevFmtUShort:
226 case DevFmtInt:
227 case DevFmtUInt:
228 case DevFmtFloat:
229 device->FmtType = DevFmtShort;
230 /* fall-through */
231 case DevFmtShort:
232 info.play.precision = 16;
233 info.play.encoding = AUDIO_ENCODING_LINEAR;
234 break;
237 frameSize = numChannels * BytesFromDevFmt(device->FmtType);
238 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
240 if(ioctl(self->fd, AUDIO_SETINFO, &info) < 0)
242 ERR("ioctl failed: %s\n", strerror(errno));
243 return ALC_FALSE;
246 if(ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder) != (ALsizei)info.play.channels)
248 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), info.play.channels);
249 return ALC_FALSE;
252 if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
253 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
254 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
255 (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
257 ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
258 info.play.precision, info.play.encoding);
259 return ALC_FALSE;
262 device->Frequency = info.play.sample_rate;
263 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
265 SetDefaultChannelOrder(device);
267 free(self->mix_data);
268 self->data_size = device->UpdateSize * FrameSizeFromDevFmt(
269 device->FmtChans, device->FmtType, device->AmbiOrder
271 self->mix_data = calloc(1, self->data_size);
273 return ALC_TRUE;
276 static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self)
278 ATOMIC_STORE_SEQ(&self->killNow, AL_FALSE);
279 if(althrd_create(&self->thread, ALCsolarisBackend_mixerProc, self) != althrd_success)
280 return ALC_FALSE;
281 return ALC_TRUE;
284 static void ALCsolarisBackend_stop(ALCsolarisBackend *self)
286 int res;
288 if(ATOMIC_EXCHANGE_SEQ(&self->killNow, AL_TRUE))
289 return;
291 althrd_join(self->thread, &res);
293 if(ioctl(self->fd, AUDIO_DRAIN) < 0)
294 ERR("Error draining device: %s\n", strerror(errno));
298 typedef struct ALCsolarisBackendFactory {
299 DERIVE_FROM_TYPE(ALCbackendFactory);
300 } ALCsolarisBackendFactory;
301 #define ALCSOLARISBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCsolarisBackendFactory, ALCbackendFactory) } }
303 ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void);
305 static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory *self);
306 static DECLARE_FORWARD(ALCsolarisBackendFactory, ALCbackendFactory, void, deinit)
307 static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory *self, ALCbackend_Type type);
308 static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory *self, enum DevProbe type);
309 static ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
310 DEFINE_ALCBACKENDFACTORY_VTABLE(ALCsolarisBackendFactory);
313 ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void)
315 static ALCsolarisBackendFactory factory = ALCSOLARISBACKENDFACTORY_INITIALIZER;
316 return STATIC_CAST(ALCbackendFactory, &factory);
320 static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory* UNUSED(self))
322 ConfigValueStr(NULL, "solaris", "device", &solaris_driver);
323 return ALC_TRUE;
326 static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory* UNUSED(self), ALCbackend_Type type)
328 if(type == ALCbackend_Playback)
329 return ALC_TRUE;
330 return ALC_FALSE;
333 static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory* UNUSED(self), enum DevProbe type)
335 switch(type)
337 case ALL_DEVICE_PROBE:
339 #ifdef HAVE_STAT
340 struct stat buf;
341 if(stat(solaris_driver, &buf) == 0)
342 #endif
343 AppendAllDevicesList(solaris_device);
345 break;
347 case CAPTURE_DEVICE_PROBE:
348 break;
352 ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
354 if(type == ALCbackend_Playback)
356 ALCsolarisBackend *backend;
357 NEW_OBJ(backend, ALCsolarisBackend)(device);
358 if(!backend) return NULL;
359 return STATIC_CAST(ALCbackend, backend);
362 return NULL;