Mercurial > audio-send
comparison Alc/backends/solaris.c @ 0:f9476ff7637e
initial forking of open-al to create multiple listeners
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 25 Oct 2011 13:02:31 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f9476ff7637e |
---|---|
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. | |
8 * | |
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. | |
13 * | |
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 | |
19 */ | |
20 | |
21 #include "config.h" | |
22 | |
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" | |
36 | |
37 #include <sys/audioio.h> | |
38 | |
39 | |
40 static const ALCchar solaris_device[] = "Solaris Default"; | |
41 | |
42 typedef struct { | |
43 int fd; | |
44 volatile int killNow; | |
45 ALvoid *thread; | |
46 | |
47 ALubyte *mix_data; | |
48 int data_size; | |
49 } solaris_data; | |
50 | |
51 | |
52 static ALuint SolarisProc(ALvoid *ptr) | |
53 { | |
54 ALCdevice *pDevice = (ALCdevice*)ptr; | |
55 solaris_data *data = (solaris_data*)pDevice->ExtraData; | |
56 ALint frameSize; | |
57 int wrote; | |
58 | |
59 SetRTPriority(); | |
60 | |
61 frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType); | |
62 | |
63 while(!data->killNow && pDevice->Connected) | |
64 { | |
65 ALint len = data->data_size; | |
66 ALubyte *WritePtr = data->mix_data; | |
67 | |
68 aluMixData(pDevice, WritePtr, len/frameSize); | |
69 while(len > 0 && !data->killNow) | |
70 { | |
71 wrote = write(data->fd, WritePtr, len); | |
72 if(wrote < 0) | |
73 { | |
74 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) | |
75 { | |
76 ERR("write failed: %s\n", strerror(errno)); | |
77 aluHandleDisconnect(pDevice); | |
78 break; | |
79 } | |
80 | |
81 Sleep(1); | |
82 continue; | |
83 } | |
84 | |
85 len -= wrote; | |
86 WritePtr += wrote; | |
87 } | |
88 } | |
89 | |
90 return 0; | |
91 } | |
92 | |
93 | |
94 static ALCboolean solaris_open_playback(ALCdevice *device, const ALCchar *deviceName) | |
95 { | |
96 char driver[64]; | |
97 solaris_data *data; | |
98 | |
99 strncpy(driver, GetConfigValue("solaris", "device", "/dev/audio"), sizeof(driver)-1); | |
100 driver[sizeof(driver)-1] = 0; | |
101 | |
102 if(!deviceName) | |
103 deviceName = solaris_device; | |
104 else if(strcmp(deviceName, solaris_device) != 0) | |
105 return ALC_FALSE; | |
106 | |
107 data = (solaris_data*)calloc(1, sizeof(solaris_data)); | |
108 data->killNow = 0; | |
109 | |
110 data->fd = open(driver, O_WRONLY); | |
111 if(data->fd == -1) | |
112 { | |
113 free(data); | |
114 ERR("Could not open %s: %s\n", driver, strerror(errno)); | |
115 return ALC_FALSE; | |
116 } | |
117 | |
118 device->szDeviceName = strdup(deviceName); | |
119 device->ExtraData = data; | |
120 return ALC_TRUE; | |
121 } | |
122 | |
123 static void solaris_close_playback(ALCdevice *device) | |
124 { | |
125 solaris_data *data = (solaris_data*)device->ExtraData; | |
126 | |
127 close(data->fd); | |
128 free(data); | |
129 device->ExtraData = NULL; | |
130 } | |
131 | |
132 static ALCboolean solaris_reset_playback(ALCdevice *device) | |
133 { | |
134 solaris_data *data = (solaris_data*)device->ExtraData; | |
135 audio_info_t info; | |
136 ALuint frameSize; | |
137 int numChannels; | |
138 | |
139 AUDIO_INITINFO(&info); | |
140 | |
141 info.play.sample_rate = device->Frequency; | |
142 | |
143 if(device->FmtChans != DevFmtMono) | |
144 device->FmtChans = DevFmtStereo; | |
145 numChannels = ChannelsFromDevFmt(device->FmtChans); | |
146 info.play.channels = numChannels; | |
147 | |
148 switch(device->FmtType) | |
149 { | |
150 case DevFmtByte: | |
151 info.play.precision = 8; | |
152 info.play.encoding = AUDIO_ENCODING_LINEAR; | |
153 break; | |
154 case DevFmtUByte: | |
155 info.play.precision = 8; | |
156 info.play.encoding = AUDIO_ENCODING_LINEAR8; | |
157 break; | |
158 case DevFmtUShort: | |
159 case DevFmtFloat: | |
160 device->FmtType = DevFmtShort; | |
161 /* fall-through */ | |
162 case DevFmtShort: | |
163 info.play.precision = 16; | |
164 info.play.encoding = AUDIO_ENCODING_LINEAR; | |
165 break; | |
166 } | |
167 | |
168 frameSize = numChannels * BytesFromDevFmt(device->FmtType); | |
169 info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize; | |
170 | |
171 if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0) | |
172 { | |
173 ERR("ioctl failed: %s\n", strerror(errno)); | |
174 return ALC_FALSE; | |
175 } | |
176 | |
177 if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels) | |
178 { | |
179 ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels); | |
180 return ALC_FALSE; | |
181 } | |
182 | |
183 if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && | |
184 device->FmtType == DevFmtByte) || | |
185 (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && | |
186 device->FmtType == DevFmtUByte) || | |
187 (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && | |
188 device->FmtType == DevFmtShort))) | |
189 { | |
190 ERR("Could not set %#x sample type, got %d (%#x)\n", | |
191 device->FmtType, info.play.precision, info.play.encoding); | |
192 return ALC_FALSE; | |
193 } | |
194 | |
195 if(device->Frequency != info.play.sample_rate) | |
196 { | |
197 if((device->Flags&DEVICE_FREQUENCY_REQUEST)) | |
198 ERR("Failed to set requested frequency %dhz, got %dhz instead\n", device->Frequency, info.play.sample_rate); | |
199 device->Flags &= ~DEVICE_FREQUENCY_REQUEST; | |
200 device->Frequency = info.play.sample_rate; | |
201 } | |
202 device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1; | |
203 | |
204 data->data_size = device->UpdateSize * frameSize; | |
205 data->mix_data = calloc(1, data->data_size); | |
206 | |
207 SetDefaultChannelOrder(device); | |
208 | |
209 data->thread = StartThread(SolarisProc, device); | |
210 if(data->thread == NULL) | |
211 { | |
212 free(data->mix_data); | |
213 data->mix_data = NULL; | |
214 return ALC_FALSE; | |
215 } | |
216 | |
217 return ALC_TRUE; | |
218 } | |
219 | |
220 static void solaris_stop_playback(ALCdevice *device) | |
221 { | |
222 solaris_data *data = (solaris_data*)device->ExtraData; | |
223 | |
224 if(!data->thread) | |
225 return; | |
226 | |
227 data->killNow = 1; | |
228 StopThread(data->thread); | |
229 data->thread = NULL; | |
230 | |
231 data->killNow = 0; | |
232 if(ioctl(data->fd, AUDIO_DRAIN) < 0) | |
233 ERR("Error draining device: %s\n", strerror(errno)); | |
234 | |
235 free(data->mix_data); | |
236 data->mix_data = NULL; | |
237 } | |
238 | |
239 | |
240 static const BackendFuncs solaris_funcs = { | |
241 solaris_open_playback, | |
242 solaris_close_playback, | |
243 solaris_reset_playback, | |
244 solaris_stop_playback, | |
245 NULL, | |
246 NULL, | |
247 NULL, | |
248 NULL, | |
249 NULL, | |
250 NULL | |
251 }; | |
252 | |
253 ALCboolean alc_solaris_init(BackendFuncs *func_list) | |
254 { | |
255 *func_list = solaris_funcs; | |
256 return ALC_TRUE; | |
257 } | |
258 | |
259 void alc_solaris_deinit(void) | |
260 { | |
261 } | |
262 | |
263 void alc_solaris_probe(enum DevProbe type) | |
264 { | |
265 #ifdef HAVE_STAT | |
266 struct stat buf; | |
267 if(stat(GetConfigValue("solaris", "device", "/dev/audio"), &buf) != 0) | |
268 return; | |
269 #endif | |
270 | |
271 switch(type) | |
272 { | |
273 case DEVICE_PROBE: | |
274 AppendDeviceList(solaris_device); | |
275 break; | |
276 case ALL_DEVICE_PROBE: | |
277 AppendAllDeviceList(solaris_device); | |
278 break; | |
279 case CAPTURE_DEVICE_PROBE: | |
280 break; | |
281 } | |
282 } |