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
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
40 #include "backends/base.h"
42 #include <sys/audioio.h>
45 typedef struct ALCsolarisBackend
{
46 DERIVE_FROM_TYPE(ALCbackend
);
53 ATOMIC(ALenum
) killNow
;
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
);
87 ATOMIC_INIT(&self
->killNow
, AL_FALSE
);
90 static void ALCsolarisBackend_Destruct(ALCsolarisBackend
*self
)
97 self
->mix_data
= NULL
;
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
;
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
)
125 FD_SET(self
->fd
, &wfds
);
129 ALCsolarisBackend_unlock(self
);
130 sret
= select(self
->fd
+1, NULL
, &wfds
, NULL
, &timeout
);
131 ALCsolarisBackend_lock(self
);
136 ERR("select failed: %s\n", strerror(errno
));
137 aluHandleDisconnect(device
);
142 WARN("select timeout\n");
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
);
154 if(errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
156 ERR("write failed: %s\n", strerror(errno
));
157 aluHandleDisconnect(device
);
165 ALCsolarisBackend_unlock(self
);
171 static ALCenum
ALCsolarisBackend_open(ALCsolarisBackend
*self
, const ALCchar
*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
);
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
);
193 static void ALCsolarisBackend_close(ALCsolarisBackend
*self
)
199 static ALCboolean
ALCsolarisBackend_reset(ALCsolarisBackend
*self
)
201 ALCdevice
*device
= STATIC_CAST(ALCbackend
,self
)->mDevice
;
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
)
218 info
.play
.precision
= 8;
219 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
222 info
.play
.precision
= 8;
223 info
.play
.encoding
= AUDIO_ENCODING_LINEAR8
;
229 device
->FmtType
= DevFmtShort
;
232 info
.play
.precision
= 16;
233 info
.play
.encoding
= AUDIO_ENCODING_LINEAR
;
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
));
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
);
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
);
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
);
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
)
284 static void ALCsolarisBackend_stop(ALCsolarisBackend
*self
)
288 if(ATOMIC_EXCHANGE_SEQ(&self
->killNow
, AL_TRUE
))
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
);
326 static ALCboolean
ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory
* UNUSED(self
), ALCbackend_Type type
)
328 if(type
== ALCbackend_Playback
)
333 static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory
* UNUSED(self
), enum DevProbe type
)
337 case ALL_DEVICE_PROBE
:
341 if(stat(solaris_driver
, &buf
) == 0)
343 AppendAllDevicesList(solaris_device
);
347 case CAPTURE_DEVICE_PROBE
:
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
);