Fix retrieved update size from pulseaudio
[openal-soft.git] / Alc / solaris.c
blob6cfaaa11461c319436a8fb34aaf8ca39a2649755
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <memory.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <math.h>
33 #include "alMain.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
37 #include <sys/audioio.h>
40 static const ALCchar solaris_device[] = "Solaris Software";
42 typedef struct {
43 int fd;
44 volatile int killNow;
45 ALvoid *thread;
47 ALubyte *mix_data;
48 int data_size;
49 } solaris_data;
52 static ALuint SolarisProc(ALvoid *ptr)
54 ALCdevice *pDevice = (ALCdevice*)ptr;
55 solaris_data *data = (solaris_data*)pDevice->ExtraData;
56 int remaining = 0;
57 ALint frameSize;
58 int wrote;
60 frameSize = aluChannelsFromFormat(pDevice->Format) *
61 aluBytesFromFormat(pDevice->Format);
63 while(!data->killNow && pDevice->Connected)
65 ALint len = data->data_size;
66 ALubyte *WritePtr = data->mix_data;
68 aluMixData(pDevice, WritePtr, len/frameSize);
69 while(len > 0 && !data->killNow)
71 wrote = write(data->fd, WritePtr, len);
72 if(wrote < 0)
74 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
76 AL_PRINT("write failed: %s\n", strerror(errno));
77 aluHandleDisconnect(pDevice);
78 len = 0;
80 else
81 Sleep(1);
82 continue;
85 len -= wrote;
86 WritePtr += wrote;
90 return 0;
94 static ALCboolean solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
96 char driver[64];
97 solaris_data *data;
99 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1);
100 driver[sizeof(driver)-1] = 0;
101 if(!deviceName)
102 deviceName = solaris_device;
103 else if(strcmp(deviceName, solaris_device) != 0)
104 return ALC_FALSE;
106 data = (solaris_data*)calloc(1, sizeof(solaris_data));
107 data->killNow = 0;
109 data->fd = open(driver, O_WRONLY);
110 if(data->fd == -1)
112 free(data);
113 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
114 return ALC_FALSE;
117 device->szDeviceName = strdup(deviceName);
118 device->ExtraData = data;
119 return ALC_TRUE;
122 static void solaris_close_playback(ALCdevice *device)
124 solaris_data *data = (solaris_data*)device->ExtraData;
126 close(data->fd);
127 free(data);
128 device->ExtraData = NULL;
131 static ALCboolean solaris_reset_playback(ALCdevice *device)
133 solaris_data *data = (solaris_data*)device->ExtraData;
134 audio_info_t info;
135 ALuint frameSize;
136 int numChannels;
138 AUDIO_INITINFO(&info);
140 switch(aluBytesFromFormat(device->Format))
142 case 1:
143 info.play.precision = 8;
144 info.play.encoding = AUDIO_ENCODING_LINEAR8;
145 break;
146 case 4:
147 switch(numChannels)
149 case 1: device->Format = AL_FORMAT_MONO16; break;
150 case 2: device->Format = AL_FORMAT_STEREO16; break;
151 case 4: device->Format = AL_FORMAT_QUAD16; break;
152 case 6: device->Format = AL_FORMAT_51CHN16; break;
153 case 7: device->Format = AL_FORMAT_61CHN16; break;
154 case 8: device->Format = AL_FORMAT_71CHN16; break;
156 /* fall-through */
157 case 2:
158 info.play.precision = 16;
159 info.play.encoding = AUDIO_ENCODING_LINEAR;
160 break;
161 default:
162 AL_PRINT("Unknown format: 0x%x\n", device->Format);
163 return ALC_FALSE;
166 numChannels = aluChannelsFromFormat(device->Format);
167 info.play.sample_rate = device->Frequency;
168 info.play.channels = numChannels;
170 frameSize = numChannels * aluBytesFromFormat(device->Format);
171 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
173 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
175 AL_PRINT("ioctl failed: %s\n", strerror(errno));
176 return ALC_FALSE;
179 if(aluChannelsFromFormat(device->Format) != info.play.channels)
181 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), info.play.channels);
182 return ALC_FALSE;
185 if(!((info.play.precision == 8 && aluBytesFromFormat(device->Format) == 1) ||
186 (info.play.precision == 16 && aluBytesFromFormat(device->Format) == 2)))
188 AL_PRINT("Could not set %d-bit output, got %d\n", aluBytesFromFormat(device->Format)*8, info.play.precision);
189 return ALC_FALSE;
192 device->Frequency = info.play.sample_rate;
193 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
195 data->data_size = device->UpdateSize * frameSize;
196 data->mix_data = calloc(1, data->data_size);
198 data->thread = StartThread(SolarisProc, device);
199 if(data->thread == NULL)
201 free(data->mix_data);
202 data->mix_data = NULL;
203 return ALC_FALSE;
206 return ALC_TRUE;
209 static void solaris_stop_playback(ALCdevice *device)
211 solaris_data *data = (solaris_data*)device->ExtraData;
213 if(!data->thread)
214 return;
216 data->killNow = 1;
217 StopThread(data->thread);
218 data->thread = NULL;
220 data->killNow = 0;
222 free(data->mix_data);
223 data->mix_data = NULL;
227 static ALCboolean solaris_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
229 (void)device;
230 (void)deviceName;
231 (void)frequency;
232 (void)format;
233 (void)SampleSize;
234 return ALC_FALSE;
237 static void solaris_close_capture(ALCdevice *device)
239 (void)device;
242 static void solaris_start_capture(ALCdevice *pDevice)
244 (void)pDevice;
247 static void solaris_stop_capture(ALCdevice *pDevice)
249 (void)pDevice;
252 static void solaris_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
254 (void)pDevice;
255 (void)pBuffer;
256 (void)lSamples;
259 static ALCuint solaris_available_samples(ALCdevice *pDevice)
261 (void)pDevice;
262 return 0;
266 BackendFuncs solaris_funcs = {
267 solaris_open_playback,
268 solaris_close_playback,
269 solaris_reset_playback,
270 solaris_stop_playback,
271 solaris_open_capture,
272 solaris_close_capture,
273 solaris_start_capture,
274 solaris_stop_capture,
275 solaris_capture_samples,
276 solaris_available_samples
279 void alc_solaris_init(BackendFuncs *func_list)
281 *func_list = solaris_funcs;
284 void alc_solaris_deinit(void)
288 void alc_solaris_probe(ALCboolean capture)
290 if(type == DEVICE_PROBE)
291 AppendDeviceList(solaris_device);
292 else if(type == ALL_DEVICE_PROBE)
293 AppendAllDeviceList(solaris_device);