340 lines
10 KiB
C
Raw Normal View History

2008-09-07 14:34:14 -07:00
/**
* OpenAL cross platform audio library
* Copyright (C) 1999-2007 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2008-09-07 14:34:14 -07:00
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/time.h>
2008-09-07 14:34:14 -07:00
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
2012-09-14 02:14:29 -07:00
2008-09-07 14:34:14 -07:00
#include "alMain.h"
2012-09-14 02:14:29 -07:00
#include "alu.h"
2013-10-27 08:14:13 -07:00
#include "threads.h"
2013-10-28 12:05:33 -07:00
#include "compat.h"
2008-09-07 14:34:14 -07:00
#include "backends/base.h"
2008-09-07 14:34:14 -07:00
#include <sys/audioio.h>
2008-09-07 14:34:14 -07:00
typedef struct ALCsolarisBackend {
DERIVE_FROM_TYPE(ALCbackend);
2012-02-29 21:45:44 -08:00
2008-09-07 14:34:14 -07:00
int fd;
ALubyte *mix_data;
int data_size;
2013-10-27 08:14:13 -07:00
volatile int killNow;
althrd_t thread;
} ALCsolarisBackend;
static int ALCsolarisBackend_mixerProc(void *ptr);
static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device);
static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self);
static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name);
static void ALCsolarisBackend_close(ALCsolarisBackend *self);
static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self);
static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self);
static void ALCsolarisBackend_stop(ALCsolarisBackend *self);
static DECLARE_FORWARD2(ALCsolarisBackend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, ALCuint, availableSamples)
static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, ALint64, getLatency)
static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, void, lock)
static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, void, unlock)
DECLARE_DEFAULT_ALLOCATORS(ALCsolarisBackend)
DEFINE_ALCBACKEND_VTABLE(ALCsolarisBackend);
static const ALCchar solaris_device[] = "Solaris Default";
static const char *solaris_driver = "/dev/audio";
2008-09-07 14:34:14 -07:00
static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device)
2008-09-07 14:34:14 -07:00
{
ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
SET_VTABLE2(ALCsolarisBackend, ALCbackend, self);
self->fd = -1;
}
static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self)
{
if(self->fd != -1)
close(self->fd);
self->fd = -1;
free(self->mix_data);
self->mix_data = NULL;
self->data_size = 0;
ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
}
static int ALCsolarisBackend_mixerProc(void *ptr)
{
ALCsolarisBackend *self = ptr;
ALCdevice *Device = STATIC_CAST(ALCbackend,self)->mDevice;
2009-09-15 18:19:00 -07:00
ALint frameSize;
2008-09-07 14:34:14 -07:00
int wrote;
SetRTPriority();
2014-04-17 20:41:32 -07:00
althrd_setname(althrd_current(), MIXER_THREAD_NAME);
2012-04-19 23:00:58 -07:00
frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
2009-09-15 18:19:00 -07:00
while(!self->killNow && Device->Connected)
2008-09-07 14:34:14 -07:00
{
ALint len = self->data_size;
ALubyte *WritePtr = self->mix_data;
2008-09-07 14:34:14 -07:00
2012-04-19 23:00:58 -07:00
aluMixData(Device, WritePtr, len/frameSize);
while(len > 0 && !self->killNow)
2008-09-07 14:34:14 -07:00
{
wrote = write(self->fd, WritePtr, len);
2009-08-26 21:49:38 -07:00
if(wrote < 0)
{
2009-10-19 00:33:00 -07:00
if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
2009-08-26 21:49:38 -07:00
{
2011-07-13 01:43:00 -07:00
ERR("write failed: %s\n", strerror(errno));
ALCsolarisBackend_lock(self);
2012-04-19 23:00:58 -07:00
aluHandleDisconnect(Device);
ALCsolarisBackend_unlock(self);
2010-01-10 12:40:59 -08:00
break;
2009-08-26 21:49:38 -07:00
}
2010-01-10 12:40:59 -08:00
2014-09-08 04:37:52 -07:00
al_nssleep(1000000);
2009-08-26 21:49:38 -07:00
continue;
}
len -= wrote;
WritePtr += wrote;
2008-09-07 14:34:14 -07:00
}
}
return 0;
}
static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name)
2008-09-07 14:34:14 -07:00
{
ALCdevice *device;
2008-09-07 14:34:14 -07:00
if(!name)
name = solaris_device;
else if(strcmp(name, solaris_device) != 0)
return ALC_INVALID_VALUE;
2008-09-07 14:34:14 -07:00
self->fd = open(solaris_driver, O_WRONLY);
if(self->fd == -1)
2008-09-07 14:34:14 -07:00
{
2012-02-29 21:45:44 -08:00
ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
return ALC_INVALID_VALUE;
2008-09-07 14:34:14 -07:00
}
device = STATIC_CAST(ALCbackend,self)->mDevice;
al_string_copy_cstr(&device->DeviceName, name);
return ALC_NO_ERROR;
}
static void ALCsolarisBackend_close(ALCsolarisBackend *self)
{
close(self->fd);
self->fd = -1;
}
static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self)
{
ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
audio_info_t info;
ALuint frameSize;
int numChannels;
2008-09-07 14:34:14 -07:00
AUDIO_INITINFO(&info);
info.play.sample_rate = device->Frequency;
if(device->FmtChans != DevFmtMono)
device->FmtChans = DevFmtStereo;
numChannels = ChannelsFromDevFmt(device->FmtChans);
info.play.channels = numChannels;
switch(device->FmtType)
2008-09-07 14:34:14 -07:00
{
case DevFmtByte:
info.play.precision = 8;
info.play.encoding = AUDIO_ENCODING_LINEAR;
break;
case DevFmtUByte:
2008-09-07 14:34:14 -07:00
info.play.precision = 8;
info.play.encoding = AUDIO_ENCODING_LINEAR8;
break;
case DevFmtUShort:
case DevFmtInt:
case DevFmtUInt:
case DevFmtFloat:
device->FmtType = DevFmtShort;
2009-08-16 13:16:41 -07:00
/* fall-through */
case DevFmtShort:
2008-09-07 14:34:14 -07:00
info.play.precision = 16;
info.play.encoding = AUDIO_ENCODING_LINEAR;
break;
}
frameSize = numChannels * BytesFromDevFmt(device->FmtType);
info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
2008-09-07 14:34:14 -07:00
if(ioctl(self->fd, AUDIO_SETINFO, &info) < 0)
2008-09-07 14:34:14 -07:00
{
2011-07-13 01:43:00 -07:00
ERR("ioctl failed: %s\n", strerror(errno));
2008-09-07 14:34:14 -07:00
return ALC_FALSE;
}
if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels)
2008-09-07 14:34:14 -07:00
{
2011-07-13 01:43:00 -07:00
ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels);
2008-09-07 14:34:14 -07:00
return ALC_FALSE;
}
2012-02-29 21:45:44 -08:00
if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
(info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
(info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
2008-09-07 14:34:14 -07:00
{
2012-02-29 21:45:44 -08:00
ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
info.play.precision, info.play.encoding);
2008-09-07 14:34:14 -07:00
return ALC_FALSE;
}
device->Frequency = info.play.sample_rate;
device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
2008-09-07 14:34:14 -07:00
SetDefaultChannelOrder(device);
free(self->mix_data);
self->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
self->mix_data = calloc(1, self->data_size);
return ALC_TRUE;
}
static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self)
{
self->killNow = 0;
if(althrd_create(&self->thread, ALCsolarisBackend_mixerProc, self) != althrd_success)
2008-09-07 14:34:14 -07:00
return ALC_FALSE;
return ALC_TRUE;
}
static void ALCsolarisBackend_stop(ALCsolarisBackend *self)
2008-09-07 14:34:14 -07:00
{
int res;
if(self->killNow)
return;
self->killNow = 1;
althrd_join(self->thread, &res);
2008-09-07 14:34:14 -07:00
if(ioctl(self->fd, AUDIO_DRAIN) < 0)
2011-07-13 01:43:00 -07:00
ERR("Error draining device: %s\n", strerror(errno));
}
typedef struct ALCsolarisBackendFactory {
DERIVE_FROM_TYPE(ALCbackendFactory);
} ALCsolarisBackendFactory;
#define ALCSOLARISBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCsolarisBackendFactory, ALCbackendFactory) } }
ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void);
static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory *self);
static DECLARE_FORWARD(ALCsolarisBackendFactory, ALCbackendFactory, void, deinit)
static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory *self, ALCbackend_Type type);
static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory *self, enum DevProbe type);
static ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
DEFINE_ALCBACKENDFACTORY_VTABLE(ALCsolarisBackendFactory);
ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void)
{
static ALCsolarisBackendFactory factory = ALCSOLARISBACKENDFACTORY_INITIALIZER;
return STATIC_CAST(ALCbackendFactory, &factory);
2009-08-16 13:16:41 -07:00
}
2008-09-07 14:34:14 -07:00
static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory* UNUSED(self))
2008-09-07 14:34:14 -07:00
{
ConfigValueStr(NULL, "solaris", "device", &solaris_driver);
return ALC_TRUE;
2008-09-07 14:34:14 -07:00
}
2009-08-26 23:45:00 -07:00
static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory* UNUSED(self), ALCbackend_Type type)
2009-08-26 23:45:00 -07:00
{
if(type == ALCbackend_Playback)
return ALC_TRUE;
return ALC_FALSE;
2009-08-26 23:45:00 -07:00
}
static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory* UNUSED(self), enum DevProbe type)
{
2011-06-14 04:02:58 -07:00
switch(type)
{
case ALL_DEVICE_PROBE:
2012-02-29 21:45:44 -08:00
{
#ifdef HAVE_STAT
struct stat buf;
if(stat(solaris_driver, &buf) == 0)
#endif
2012-05-09 16:28:16 -07:00
AppendAllDevicesList(solaris_device);
2012-02-29 21:45:44 -08:00
}
break;
2011-06-14 04:02:58 -07:00
case CAPTURE_DEVICE_PROBE:
break;
}
}
ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
{
if(type == ALCbackend_Playback)
{
ALCsolarisBackend *backend;
NEW_OBJ(backend, ALCsolarisBackend)(device);
if(!backend) return NULL;
return STATIC_CAST(ALCbackend, backend);
}
return NULL;
}