1 | /* $Id: solaudio.c 53624 2014-12-31 14:59:44Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Audio Driver - Solaris host.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #include <unistd.h>
|
---|
22 | #include <errno.h>
|
---|
23 | #include <stropts.h>
|
---|
24 | #include <fcntl.h>
|
---|
25 | #include <sys/audio.h>
|
---|
26 | #include <sys/stat.h>
|
---|
27 | #include <sys/time.h>
|
---|
28 | #include <sys/mixer.h>
|
---|
29 |
|
---|
30 | #define LOG_GROUP LOG_GROUP_DEV_AUDIO
|
---|
31 | #include <VBox/log.h>
|
---|
32 | #include <iprt/env.h>
|
---|
33 |
|
---|
34 | #include "VBoxDD.h"
|
---|
35 | #include "vl_vbox.h"
|
---|
36 | #include "audio.h"
|
---|
37 | #include <iprt/alloc.h>
|
---|
38 |
|
---|
39 | #define AUDIO_CAP "solaudio"
|
---|
40 | #include "audio_int.h"
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *******************************************************************************/
|
---|
45 | typedef struct solaudioVoiceOut
|
---|
46 | {
|
---|
47 | HWVoiceOut Hw;
|
---|
48 | audio_info_t AudioInfo;
|
---|
49 | uint_t cBuffersPlayed;
|
---|
50 | void *pPCMBuf;
|
---|
51 | } solaudioVoiceOut;
|
---|
52 |
|
---|
53 | typedef struct solaudioVoiceIn
|
---|
54 | {
|
---|
55 | HWVoiceIn Hw;
|
---|
56 | audio_info_t AudioInfo;
|
---|
57 | void *pPCMBuf;
|
---|
58 | } solaudioVoiceIn;
|
---|
59 |
|
---|
60 |
|
---|
61 | /*******************************************************************************
|
---|
62 | * Global Variables *
|
---|
63 | *******************************************************************************/
|
---|
64 | static struct
|
---|
65 | {
|
---|
66 | int cbPlayBuffer;
|
---|
67 | int cbRecordBuffer;
|
---|
68 | } conf =
|
---|
69 | {
|
---|
70 | INIT_FIELD (cbPlayBuffer =) 4352,
|
---|
71 | INIT_FIELD (cbRecordBuffer = ) 8192
|
---|
72 | };
|
---|
73 |
|
---|
74 | static int g_AudioDev = -1;
|
---|
75 | static int g_RecordDev = -1;
|
---|
76 | static int g_AudioCtl = -1;
|
---|
77 | static char *g_pszAudioDev = NULL;
|
---|
78 | static char *g_pszAudioCtl = NULL;
|
---|
79 |
|
---|
80 | typedef enum
|
---|
81 | {
|
---|
82 | enmPlay = 6,
|
---|
83 | enmRecord = 9,
|
---|
84 | enmRecordPassive = 15
|
---|
85 | } audio_dest_t;
|
---|
86 |
|
---|
87 |
|
---|
88 | static int aud_to_solfmt (audfmt_e fmt)
|
---|
89 | {
|
---|
90 | switch (fmt)
|
---|
91 | {
|
---|
92 | case AUD_FMT_S8:
|
---|
93 | case AUD_FMT_U8:
|
---|
94 | return AUDIO_PRECISION_8;
|
---|
95 |
|
---|
96 | case AUD_FMT_S16:
|
---|
97 | case AUD_FMT_U16:
|
---|
98 | return AUDIO_PRECISION_16;
|
---|
99 |
|
---|
100 | default:
|
---|
101 | LogRel(("solaudio: aud_to_solfmt: Bad audio format %d\n", fmt));
|
---|
102 | return AUDIO_PRECISION_8;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | static int sol_to_audfmt (int fmt, int encoding)
|
---|
108 | {
|
---|
109 | switch (fmt)
|
---|
110 | {
|
---|
111 | case AUDIO_PRECISION_8:
|
---|
112 | {
|
---|
113 | if (encoding == AUDIO_ENCODING_LINEAR8)
|
---|
114 | return AUD_FMT_U8;
|
---|
115 | else
|
---|
116 | return AUD_FMT_S8;
|
---|
117 | break;
|
---|
118 | }
|
---|
119 |
|
---|
120 | case AUDIO_PRECISION_16:
|
---|
121 | {
|
---|
122 | if (encoding == AUDIO_ENCODING_LINEAR)
|
---|
123 | return AUD_FMT_S16;
|
---|
124 | else
|
---|
125 | return AUD_FMT_U16;
|
---|
126 | break;
|
---|
127 | }
|
---|
128 |
|
---|
129 | default:
|
---|
130 | LogRel(("solaudio: sol_to_audfmt: Bad audio format %d\n", fmt));
|
---|
131 | return AUD_FMT_S8;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | static char *solaudio_getdevice (void)
|
---|
137 | {
|
---|
138 | /*
|
---|
139 | * This is for multiple audio devices where env. var determines current one,
|
---|
140 | * otherwise else we fallback to default.
|
---|
141 | */
|
---|
142 | char *pszAudioDev = RTEnvDupEx(RTENV_DEFAULT, "AUDIODEV");
|
---|
143 | if (!pszAudioDev)
|
---|
144 | pszAudioDev = RTStrDup("/dev/audio");
|
---|
145 | return pszAudioDev;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | static void solaudio_close_device (audio_dest_t dst)
|
---|
150 | {
|
---|
151 | LogFlow(("solaudio: solaudio_close_device\n"));
|
---|
152 | switch (dst)
|
---|
153 | {
|
---|
154 | case enmPlay:
|
---|
155 | {
|
---|
156 | close(g_AudioDev);
|
---|
157 | g_AudioDev = -1;
|
---|
158 | break;
|
---|
159 | }
|
---|
160 |
|
---|
161 | case enmRecord:
|
---|
162 | case enmRecordPassive:
|
---|
163 | {
|
---|
164 | close(g_RecordDev);
|
---|
165 | g_RecordDev = -1;
|
---|
166 | break;
|
---|
167 | }
|
---|
168 |
|
---|
169 | default:
|
---|
170 | LogRel(("solaudio: cannot close. invalid audio destination %d.\n", dst));
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | static int solaudio_open_device (audio_dest_t dst)
|
---|
176 | {
|
---|
177 | int rc = 0;
|
---|
178 |
|
---|
179 | LogFlow(("solaudio: solaudio_open_device dest=%d\n", dst));
|
---|
180 |
|
---|
181 | switch (dst)
|
---|
182 | {
|
---|
183 | case enmPlay:
|
---|
184 | {
|
---|
185 | LogFlow(("solaudio: open_device for enmPlay\n"));
|
---|
186 | g_AudioDev = open(g_pszAudioDev, O_WRONLY | O_NONBLOCK);
|
---|
187 | if (g_AudioDev < 0)
|
---|
188 | {
|
---|
189 | LogRel(("solaudio: failed to open device %s dst=%d\n", g_pszAudioDev, dst));
|
---|
190 | rc = -1;
|
---|
191 | }
|
---|
192 | break;
|
---|
193 | }
|
---|
194 |
|
---|
195 | case enmRecord:
|
---|
196 | case enmRecordPassive:
|
---|
197 | {
|
---|
198 | LogFlow(("solaudio: open_device for enmRecord\n"));
|
---|
199 | g_RecordDev = open(g_pszAudioDev, (dst == enmRecord ? O_RDONLY : O_WRONLY) | O_NONBLOCK);
|
---|
200 | if (g_RecordDev < 0)
|
---|
201 | {
|
---|
202 | LogRel(("solaudio: failed to open device %s dst=%d\n", g_pszAudioDev, dst));
|
---|
203 | rc = -1;
|
---|
204 | }
|
---|
205 | break;
|
---|
206 | }
|
---|
207 |
|
---|
208 | default:
|
---|
209 | LogRel(("solaudio: Invalid audio destination.\n"));
|
---|
210 | break;
|
---|
211 | }
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | static int solaudio_setattrs(audio_dest_t dst, audio_info_t *info)
|
---|
217 | {
|
---|
218 | audio_info_t AudioInfo;
|
---|
219 | audio_prinfo_t *pDstInfo;
|
---|
220 | audio_prinfo_t *pSrcInfo;
|
---|
221 |
|
---|
222 | LogFlow(("solaudio: solaudio_setattrs dst=%d info=%p\n", dst, info));
|
---|
223 |
|
---|
224 | if (!info)
|
---|
225 | return -1;
|
---|
226 |
|
---|
227 | AUDIO_INITINFO(&AudioInfo);
|
---|
228 | if (ioctl(dst == enmPlay ? g_AudioDev : g_RecordDev, AUDIO_GETINFO, &AudioInfo) < 0)
|
---|
229 | {
|
---|
230 | LogRel(("solaudio: AUDIO_GETINFO failed\n"));
|
---|
231 | return -1;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (dst == enmPlay)
|
---|
235 | {
|
---|
236 | pDstInfo = &AudioInfo.play;
|
---|
237 | pSrcInfo = &info->play;
|
---|
238 | }
|
---|
239 | else
|
---|
240 | {
|
---|
241 | pDstInfo = &AudioInfo.record;
|
---|
242 | pSrcInfo = &info->record;
|
---|
243 | }
|
---|
244 |
|
---|
245 | pDstInfo->sample_rate = pSrcInfo->sample_rate;
|
---|
246 | pDstInfo->channels = pSrcInfo->channels;
|
---|
247 | pDstInfo->precision = pSrcInfo->precision;
|
---|
248 | pDstInfo->encoding = pSrcInfo->encoding;
|
---|
249 | pDstInfo->buffer_size = pSrcInfo->buffer_size;
|
---|
250 | pDstInfo->gain = AUDIO_MAX_GAIN;
|
---|
251 | pDstInfo->open = 0;
|
---|
252 |
|
---|
253 | if (ioctl(dst == enmPlay ? g_AudioDev : g_RecordDev, AUDIO_SETINFO, &AudioInfo) < 0)
|
---|
254 | {
|
---|
255 | LogRel(("solaudio: AUDIO_SETINFO failed\n"));
|
---|
256 | return -1;
|
---|
257 | }
|
---|
258 | return 0;
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | static int solaudio_init_out (HWVoiceOut *hw, audsettings_t *as)
|
---|
263 | {
|
---|
264 | solaudioVoiceOut *pSol = (solaudioVoiceOut *)hw;
|
---|
265 | audsettings_t ObtAudioInfo;
|
---|
266 |
|
---|
267 | AUDIO_INITINFO(&pSol->AudioInfo);
|
---|
268 | pSol->AudioInfo.play.sample_rate = as->freq;
|
---|
269 | pSol->AudioInfo.play.channels = as->nchannels;
|
---|
270 | pSol->AudioInfo.play.precision = aud_to_solfmt(as->fmt);
|
---|
271 | pSol->AudioInfo.play.buffer_size = conf.cbPlayBuffer;
|
---|
272 |
|
---|
273 | if (as->fmt == AUD_FMT_U8)
|
---|
274 | pSol->AudioInfo.play.encoding = AUDIO_ENCODING_LINEAR8;
|
---|
275 | else
|
---|
276 | pSol->AudioInfo.play.encoding = AUDIO_ENCODING_LINEAR;
|
---|
277 |
|
---|
278 | /* Open device for playback. */
|
---|
279 | if (solaudio_open_device(enmPlay))
|
---|
280 | {
|
---|
281 | LogRel(("solaudio: solaudio_open failed\n"));
|
---|
282 | return -1;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* Specify playback attributes to device. */
|
---|
286 | if (solaudio_setattrs(enmPlay, &pSol->AudioInfo))
|
---|
287 | {
|
---|
288 | LogRel(("solaudio: failed to set playback attributes.\n"));
|
---|
289 | return -1;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /* Copy obtained playback attributes. */
|
---|
293 | ObtAudioInfo.freq = pSol->AudioInfo.play.sample_rate;
|
---|
294 | ObtAudioInfo.nchannels = pSol->AudioInfo.play.channels;
|
---|
295 | ObtAudioInfo.fmt = sol_to_audfmt(pSol->AudioInfo.play.precision, pSol->AudioInfo.play.encoding);
|
---|
296 | ObtAudioInfo.endianness = as->endianness;
|
---|
297 |
|
---|
298 | audio_pcm_init_info(&hw->info, &ObtAudioInfo);
|
---|
299 | pSol->cBuffersPlayed = 0;
|
---|
300 |
|
---|
301 | hw->samples = pSol->AudioInfo.play.buffer_size >> hw->info.shift;
|
---|
302 | pSol->pPCMBuf = RTMemAllocZ(pSol->AudioInfo.play.buffer_size);
|
---|
303 | if (!pSol->pPCMBuf)
|
---|
304 | {
|
---|
305 | LogRel(("solaudio: failed to alloc %d %d bytes to pPCMBuf\n", hw->samples << hw->info.shift, hw->samples));
|
---|
306 | return -1;
|
---|
307 | }
|
---|
308 | LogFlow(("solaudio: init_out hw->samples=%d play.buffer_size=%d\n", hw->samples, pSol->AudioInfo.play.buffer_size));
|
---|
309 | return 0;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | static void solaudio_fini_out (HWVoiceOut *hw)
|
---|
314 | {
|
---|
315 | solaudioVoiceOut *sol = (solaudioVoiceOut *) hw;
|
---|
316 | LogFlow(("solaudio: fini_out\n"));
|
---|
317 |
|
---|
318 | solaudio_close_device(enmPlay);
|
---|
319 | if (sol->pPCMBuf)
|
---|
320 | {
|
---|
321 | RTMemFree(sol->pPCMBuf);
|
---|
322 | sol->pPCMBuf = NULL;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | static void solaudio_start_out (HWVoiceOut *hw)
|
---|
328 | {
|
---|
329 | audio_info_t AudioInfo;
|
---|
330 | solaudioVoiceOut *pSol = (solaudioVoiceOut *)hw;
|
---|
331 | LogFlow(("solaudio: voice_enable\n"));
|
---|
332 |
|
---|
333 | audio_pcm_info_clear_buf(&hw->info, pSol->pPCMBuf, hw->samples);
|
---|
334 |
|
---|
335 | AUDIO_INITINFO(&AudioInfo);
|
---|
336 | ioctl(g_AudioDev, AUDIO_GETINFO, &AudioInfo);
|
---|
337 | AudioInfo.play.pause = 0;
|
---|
338 | #if 0
|
---|
339 | AudioInfo.play.eof = 0;
|
---|
340 | AudioInfo.play.samples = 0;
|
---|
341 | pSol->cBuffersPlayed = 0;
|
---|
342 | #endif
|
---|
343 | ioctl(g_AudioDev, AUDIO_SETINFO, &AudioInfo);
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | static void solaudio_stop_out (solaudioVoiceOut *sol)
|
---|
348 | {
|
---|
349 | audio_info_t AudioInfo;
|
---|
350 | LogFlow(("solaudio: stop_out\n"));
|
---|
351 |
|
---|
352 | if (ioctl(g_AudioCtl, I_SETSIG, 0) < 0)
|
---|
353 | {
|
---|
354 | Log(("solaudio: failed to stop signalling\n"));
|
---|
355 | return;
|
---|
356 | }
|
---|
357 |
|
---|
358 | if (ioctl(g_AudioDev, I_FLUSH, FLUSHW) < 0)
|
---|
359 | {
|
---|
360 | LogRel(("solaudio: failed to drop unplayed buffers\n"));
|
---|
361 | return;
|
---|
362 | }
|
---|
363 |
|
---|
364 | AUDIO_INITINFO(&AudioInfo);
|
---|
365 | AudioInfo.play.pause = 1;
|
---|
366 | #if 0
|
---|
367 | AudioInfo.play.samples = 0;
|
---|
368 | AudioInfo.play.eof = 0;
|
---|
369 | AudioInfo.play.error = 0;
|
---|
370 | sol->cBuffersPlayed = 0;
|
---|
371 | #endif
|
---|
372 | if (ioctl(g_AudioDev, AUDIO_SETINFO, &AudioInfo) < 0)
|
---|
373 | {
|
---|
374 | LogRel(("solaudio: AUDIO_SETINFO failed during stop_out.\n"));
|
---|
375 | return;
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 |
|
---|
380 | static int solaudio_availbuf (solaudioVoiceOut *sol)
|
---|
381 | {
|
---|
382 | int cbPlayBuffer = 0;
|
---|
383 | if (ioctl(g_AudioDev, AUDIO_GETINFO, &sol->AudioInfo) < 0)
|
---|
384 | {
|
---|
385 | LogRel(("solaudio: AUDIO_GETINFO ioctl failed\n"));
|
---|
386 | return -1;
|
---|
387 | }
|
---|
388 |
|
---|
389 | if (sol->cBuffersPlayed - sol->AudioInfo.play.eof <= 2)
|
---|
390 | cbPlayBuffer = conf.cbPlayBuffer;
|
---|
391 |
|
---|
392 | /* Check for overflow */
|
---|
393 | if (sol->cBuffersPlayed > UINT_MAX - 4)
|
---|
394 | {
|
---|
395 | sol->cBuffersPlayed -= UINT_MAX - 4;
|
---|
396 | sol->AudioInfo.play.eof -= UINT_MAX - 4;
|
---|
397 | ioctl(g_AudioDev, AUDIO_SETINFO, &sol->AudioInfo);
|
---|
398 | }
|
---|
399 |
|
---|
400 | LogFlow(("avail: eof=%d samples=%d bufplayed=%d avail=%d\n", sol->AudioInfo.play.eof, sol->AudioInfo.play.samples,
|
---|
401 | sol->cBuffersPlayed, cbPlayBuffer));
|
---|
402 | return cbPlayBuffer;
|
---|
403 | }
|
---|
404 |
|
---|
405 |
|
---|
406 | static int solaudio_run_out (HWVoiceOut *hw)
|
---|
407 | {
|
---|
408 | solaudioVoiceOut *pSol = (solaudioVoiceOut *) hw;
|
---|
409 | int csLive, csDecr, csSamples, csToWrite, csAvail;
|
---|
410 | size_t cbAvail, cbToWrite;
|
---|
411 | ssize_t cbWritten;
|
---|
412 | uint8_t *pu8Dst;
|
---|
413 | st_sample_t *psSrc;
|
---|
414 |
|
---|
415 | csLive = audio_pcm_hw_get_live_out(hw);
|
---|
416 | if (!csLive)
|
---|
417 | return 0;
|
---|
418 |
|
---|
419 | cbAvail = solaudio_availbuf(pSol);
|
---|
420 | if (cbAvail <= 0)
|
---|
421 | return 0;
|
---|
422 |
|
---|
423 | csAvail = cbAvail >> hw->info.shift; /* bytes => samples */
|
---|
424 | csDecr = audio_MIN(csLive, csAvail);
|
---|
425 | csSamples = csDecr;
|
---|
426 |
|
---|
427 | while (csSamples)
|
---|
428 | {
|
---|
429 | /* split request at the end of our samples buffer */
|
---|
430 | csToWrite = audio_MIN(csSamples, hw->samples - hw->rpos);
|
---|
431 | cbToWrite = csToWrite << hw->info.shift;
|
---|
432 | psSrc = hw->mix_buf + hw->rpos;
|
---|
433 | pu8Dst = advance(pSol->pPCMBuf, hw->rpos << hw->info.shift);
|
---|
434 |
|
---|
435 | hw->clip(pu8Dst, psSrc, csToWrite);
|
---|
436 |
|
---|
437 | cbWritten = write(g_AudioDev, pu8Dst, cbToWrite);
|
---|
438 | if (cbWritten < 0)
|
---|
439 | break;
|
---|
440 |
|
---|
441 | hw->rpos = (hw->rpos + csToWrite) % hw->samples;
|
---|
442 | csSamples -= csToWrite;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /* Increment eof marker for synchronous buffer processed */
|
---|
446 | write (g_AudioDev, NULL, 0);
|
---|
447 | pSol->cBuffersPlayed++;
|
---|
448 | return csDecr;
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | static int solaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
---|
453 | {
|
---|
454 | solaudioVoiceOut *pSol = (solaudioVoiceOut *) hw;
|
---|
455 | switch (cmd)
|
---|
456 | {
|
---|
457 | case VOICE_ENABLE:
|
---|
458 | {
|
---|
459 | LogFlow(("solaudio: voice_enable\n"));
|
---|
460 | solaudio_start_out(hw);
|
---|
461 | break;
|
---|
462 | }
|
---|
463 |
|
---|
464 | case VOICE_DISABLE:
|
---|
465 | {
|
---|
466 | LogFlow(("solaudio: voice_disable\n"));
|
---|
467 | solaudio_stop_out(pSol);
|
---|
468 | break;
|
---|
469 | }
|
---|
470 | }
|
---|
471 | return 0;
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | static int solaudio_write (SWVoiceOut *sw, void *buf, int len)
|
---|
476 | {
|
---|
477 | return audio_pcm_sw_write (sw, buf, len);
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | static void *solaudio_audio_init (void)
|
---|
482 | {
|
---|
483 | struct stat FileStat;
|
---|
484 |
|
---|
485 | LogFlow(("solaudio_audio_init\n"));
|
---|
486 | if (!g_pszAudioDev)
|
---|
487 | {
|
---|
488 | g_pszAudioDev = solaudio_getdevice();
|
---|
489 | if (!g_pszAudioDev)
|
---|
490 | {
|
---|
491 | LogRel(("solaudio: solaudio_getdevice() failed to return a valid device.\n"));
|
---|
492 | return NULL;
|
---|
493 | }
|
---|
494 | }
|
---|
495 |
|
---|
496 | if (stat(g_pszAudioDev, &FileStat) < 0)
|
---|
497 | {
|
---|
498 | LogRel(("solaudio: failed to stat %s\n", g_pszAudioDev));
|
---|
499 | return NULL;
|
---|
500 | }
|
---|
501 |
|
---|
502 | if (!S_ISCHR(FileStat.st_mode))
|
---|
503 | {
|
---|
504 | LogRel(("solaudio: invalid mode for %s\n", g_pszAudioDev));
|
---|
505 | return NULL;
|
---|
506 | }
|
---|
507 |
|
---|
508 | if (!g_pszAudioCtl)
|
---|
509 | RTStrAPrintf(&g_pszAudioCtl, "%sctl", g_pszAudioDev);
|
---|
510 |
|
---|
511 | if (g_AudioCtl < 0)
|
---|
512 | {
|
---|
513 | g_AudioCtl = open(g_pszAudioCtl, O_RDWR | O_NONBLOCK);
|
---|
514 | if (g_AudioCtl < 0)
|
---|
515 | {
|
---|
516 | LogRel(("solaudio: failed to open device %s\n", g_pszAudioCtl));
|
---|
517 | return NULL;
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | return &conf;
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | static void solaudio_audio_fini (void *opaque)
|
---|
526 | {
|
---|
527 | LogFlow(("solaudio_audio_fini\n"));
|
---|
528 | if (g_pszAudioDev)
|
---|
529 | {
|
---|
530 | RTStrFree(g_pszAudioDev);
|
---|
531 | g_pszAudioDev = NULL;
|
---|
532 | }
|
---|
533 | if (g_pszAudioCtl)
|
---|
534 | {
|
---|
535 | RTStrFree(g_pszAudioCtl);
|
---|
536 | g_pszAudioCtl = NULL;
|
---|
537 | }
|
---|
538 | close(g_AudioCtl);
|
---|
539 | g_AudioCtl = -1;
|
---|
540 |
|
---|
541 | NOREF(opaque);
|
---|
542 | }
|
---|
543 |
|
---|
544 |
|
---|
545 | /* -=-=-=-=- Audio Input -=-=-=-=- */
|
---|
546 |
|
---|
547 | static void solaudio_pause_record(void)
|
---|
548 | {
|
---|
549 | audio_info_t AudioInfo;
|
---|
550 | AUDIO_INITINFO(&AudioInfo);
|
---|
551 | if (ioctl(g_RecordDev, AUDIO_GETINFO, &AudioInfo) < 0)
|
---|
552 | {
|
---|
553 | LogRel(("solaudio: failed to get info. to pause recording.\n"));
|
---|
554 | return;
|
---|
555 | }
|
---|
556 |
|
---|
557 | AudioInfo.record.pause = 1;
|
---|
558 | if (ioctl(g_RecordDev, AUDIO_SETINFO, &AudioInfo))
|
---|
559 | LogRel(("solaudio: failed to pause recording.\n"));
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | static void solaudio_resume_record(void)
|
---|
564 | {
|
---|
565 | audio_info_t AudioInfo;
|
---|
566 | AUDIO_INITINFO(&AudioInfo);
|
---|
567 | if (ioctl(g_RecordDev, AUDIO_GETINFO, &AudioInfo) < 0)
|
---|
568 | {
|
---|
569 | LogRel(("solaudio: failed to get info. to resume recording.\n"));
|
---|
570 | return;
|
---|
571 | }
|
---|
572 |
|
---|
573 | AudioInfo.record.pause = 0;
|
---|
574 | if (ioctl(g_RecordDev, AUDIO_SETINFO, &AudioInfo))
|
---|
575 | LogRel(("solaudio: failed to resume recording.\n"));
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 |
|
---|
580 | static void solaudio_stop_in (solaudioVoiceIn *sol)
|
---|
581 | {
|
---|
582 | audio_info_t AudioInfo;
|
---|
583 | LogFlow(("solaudio: stop_in\n"));
|
---|
584 |
|
---|
585 | if (ioctl(g_AudioCtl, I_SETSIG, 0) < 0)
|
---|
586 | {
|
---|
587 | Log(("solaudio: failed to stop signalling\n"));
|
---|
588 | return;
|
---|
589 | }
|
---|
590 |
|
---|
591 | if (ioctl(g_RecordDev, I_FLUSH, FLUSHR) < 0)
|
---|
592 | {
|
---|
593 | LogRel(("solaudio: failed to drop record buffers\n"));
|
---|
594 | return;
|
---|
595 | }
|
---|
596 |
|
---|
597 | AUDIO_INITINFO(&AudioInfo);
|
---|
598 | AudioInfo.record.samples = 0;
|
---|
599 | AudioInfo.record.pause = 1;
|
---|
600 | AudioInfo.record.eof = 0;
|
---|
601 | AudioInfo.record.error = 0;
|
---|
602 | if (ioctl(g_RecordDev, AUDIO_SETINFO, &AudioInfo) < 0)
|
---|
603 | {
|
---|
604 | LogRel(("solaudio: AUDIO_SETINFO failed during stop_in.\n"));
|
---|
605 | return;
|
---|
606 | }
|
---|
607 |
|
---|
608 | solaudio_close_device(enmRecord);
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | static void solaudio_start_in (solaudioVoiceIn *sol)
|
---|
613 | {
|
---|
614 | LogFlow(("solaudio: start_in\n"));
|
---|
615 | if (solaudio_open_device(enmRecord))
|
---|
616 | {
|
---|
617 | LogRel(("solaudio: failed to open for recording.\n"));
|
---|
618 | }
|
---|
619 |
|
---|
620 | if (solaudio_setattrs(enmRecord, &sol->AudioInfo))
|
---|
621 | {
|
---|
622 | LogRel(("solaudio: solaudio_setattrs for recording failed.\n"));
|
---|
623 | return;
|
---|
624 | }
|
---|
625 | solaudio_resume_record();
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | static int solaudio_init_in (HWVoiceIn *hw, audsettings_t *as)
|
---|
630 | {
|
---|
631 | solaudioVoiceIn *pSol = (solaudioVoiceIn *)hw;
|
---|
632 | audsettings_t ObtAudioInfo;
|
---|
633 |
|
---|
634 | AUDIO_INITINFO(&pSol->AudioInfo);
|
---|
635 | pSol->AudioInfo.record.sample_rate = as->freq;
|
---|
636 | pSol->AudioInfo.record.channels = as->nchannels;
|
---|
637 | pSol->AudioInfo.record.precision = aud_to_solfmt(as->fmt);
|
---|
638 | pSol->AudioInfo.record.buffer_size = conf.cbRecordBuffer;
|
---|
639 |
|
---|
640 | if (as->fmt == AUD_FMT_U8)
|
---|
641 | pSol->AudioInfo.record.encoding = AUDIO_ENCODING_LINEAR8;
|
---|
642 | else
|
---|
643 | pSol->AudioInfo.record.encoding = AUDIO_ENCODING_LINEAR;
|
---|
644 |
|
---|
645 | /*
|
---|
646 | * Open device for recording in passive mode (O_WRONLY) as we do not
|
---|
647 | * want to start buffering audio immediately. This is what is recommended.
|
---|
648 | */
|
---|
649 | if (solaudio_open_device(enmRecordPassive))
|
---|
650 | {
|
---|
651 | LogRel(("solaudio: solaudio_open failed.\n"));
|
---|
652 | return -1;
|
---|
653 | }
|
---|
654 |
|
---|
655 | /* Specify playback attributes to device. */
|
---|
656 | if (solaudio_setattrs(enmRecord, &pSol->AudioInfo))
|
---|
657 | {
|
---|
658 | LogRel(("solaudio: failed to set playback attributes.\n"));
|
---|
659 | return -1;
|
---|
660 | }
|
---|
661 |
|
---|
662 | /* Copy obtained record attributes. */
|
---|
663 | ObtAudioInfo.freq = pSol->AudioInfo.record.sample_rate;
|
---|
664 | ObtAudioInfo.nchannels = pSol->AudioInfo.record.channels;
|
---|
665 | ObtAudioInfo.fmt = sol_to_audfmt(pSol->AudioInfo.record.precision, pSol->AudioInfo.record.encoding);
|
---|
666 | ObtAudioInfo.endianness = as->endianness;
|
---|
667 |
|
---|
668 | audio_pcm_init_info(&hw->info, &ObtAudioInfo);
|
---|
669 |
|
---|
670 | hw->samples = pSol->AudioInfo.record.buffer_size >> hw->info.shift;
|
---|
671 | pSol->pPCMBuf = RTMemAllocZ(pSol->AudioInfo.record.buffer_size);
|
---|
672 | if (!pSol->pPCMBuf)
|
---|
673 | {
|
---|
674 | LogRel(("solaudio: init_in: failed to alloc %d %d bytes to pPCMBuf\n", hw->samples << hw->info.shift, hw->samples));
|
---|
675 | return -1;
|
---|
676 | }
|
---|
677 | solaudio_close_device(enmRecordPassive);
|
---|
678 | LogFlow(("solaudio: init_in: hw->samples=%d record.buffer_size=%d rate=%d\n", hw->samples, pSol->AudioInfo.record.buffer_size,
|
---|
679 | pSol->AudioInfo.record.sample_rate));
|
---|
680 | return 0;
|
---|
681 | }
|
---|
682 |
|
---|
683 |
|
---|
684 | static void solaudio_fini_in (HWVoiceIn *hw)
|
---|
685 | {
|
---|
686 | solaudioVoiceIn *sol = (solaudioVoiceIn *) hw;
|
---|
687 | LogFlow(("solaudio: fini_in done\n"));
|
---|
688 |
|
---|
689 | if (sol->pPCMBuf)
|
---|
690 | {
|
---|
691 | RTMemFree(sol->pPCMBuf);
|
---|
692 | sol->pPCMBuf = NULL;
|
---|
693 | }
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 | static int solaudio_run_in (HWVoiceIn *hw)
|
---|
698 | {
|
---|
699 | #if 0
|
---|
700 | solaudioVoiceIn *pSol = (solaudioVoiceIn *) hw;
|
---|
701 | int csDead, csDecr = 0, csSamples, csRead, csAvail;
|
---|
702 | size_t cbAvail, cbRead;
|
---|
703 | void *pu8Src;
|
---|
704 | st_sample_t *psDst;
|
---|
705 |
|
---|
706 | csDead = hw->samples - audio_pcm_hw_get_live_in (hw);
|
---|
707 |
|
---|
708 | if (!csDead)
|
---|
709 | return 0;
|
---|
710 |
|
---|
711 | if (ioctl(g_AudioDev, I_NREAD, &cbAvail) < 0)
|
---|
712 | {
|
---|
713 | LogRel(("solaudio: I_NREAD failed\n"));
|
---|
714 | return 0;
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (!cbAvail)
|
---|
718 | return 0;
|
---|
719 |
|
---|
720 | cbAvail = audio_MIN(cbAvail, conf.cbRecordBuffer);
|
---|
721 | pu8Src = pSol->pPCMBuf;
|
---|
722 | cbRead = read(g_AudioDev, pu8Src, cbAvail);
|
---|
723 | if (cbRead <= 0)
|
---|
724 | return 0;
|
---|
725 |
|
---|
726 | csAvail = cbAvail >> hw->info.shift;
|
---|
727 | csDecr = audio_MIN (csDead, csAvail);
|
---|
728 | csSamples = csDecr;
|
---|
729 |
|
---|
730 | while (csSamples)
|
---|
731 | {
|
---|
732 | /* split request at the end of our samples buffer */
|
---|
733 | psDst = hw->conv_buf + hw->wpos;
|
---|
734 | csRead = audio_MIN (csSamples, hw->samples - hw->wpos);
|
---|
735 | hw->conv (psDst, pu8Src, csRead, &pcm_in_volume);
|
---|
736 | hw->wpos = (hw->wpos + csRead) % hw->samples;
|
---|
737 | csSamples -= csRead;
|
---|
738 | pu8Src = (void *)((uint8_t*)pu8Src + (csRead << hw->info.shift));
|
---|
739 | }
|
---|
740 | return csDecr;
|
---|
741 | #else
|
---|
742 | solaudioVoiceIn *sol = (solaudioVoiceIn *) hw;
|
---|
743 | int hwshift = hw->info.shift;
|
---|
744 | int i;
|
---|
745 | int live = audio_pcm_hw_get_live_in (hw);
|
---|
746 | int dead = hw->samples - live;
|
---|
747 | size_t read_samples = 0;
|
---|
748 | struct
|
---|
749 | {
|
---|
750 | int add;
|
---|
751 | int len;
|
---|
752 | } bufs[2];
|
---|
753 |
|
---|
754 | bufs[0].add = hw->wpos;
|
---|
755 | bufs[0].len = 0;
|
---|
756 | bufs[1].add = 0;
|
---|
757 | bufs[1].len = 0;
|
---|
758 |
|
---|
759 | if (!dead) {
|
---|
760 | return 0;
|
---|
761 | }
|
---|
762 |
|
---|
763 | if (hw->wpos + dead > hw->samples)
|
---|
764 | {
|
---|
765 | bufs[0].len = (hw->samples - hw->wpos) << hwshift;
|
---|
766 | bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
|
---|
767 | }
|
---|
768 | else
|
---|
769 | bufs[0].len = dead << hwshift;
|
---|
770 |
|
---|
771 |
|
---|
772 | for (i = 0; i < 2; ++i)
|
---|
773 | {
|
---|
774 | ssize_t nread;
|
---|
775 |
|
---|
776 | if (bufs[i].len)
|
---|
777 | {
|
---|
778 | void *p = advance (sol->pPCMBuf, bufs[i].add << hwshift);
|
---|
779 | nread = read (g_RecordDev, p, bufs[i].len);
|
---|
780 |
|
---|
781 | if (nread > 0)
|
---|
782 | {
|
---|
783 | read_samples += nread >> hwshift;
|
---|
784 | hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
|
---|
785 | &pcm_in_volume);
|
---|
786 | }
|
---|
787 |
|
---|
788 | if (bufs[i].len - nread)
|
---|
789 | if (nread == -1)
|
---|
790 | break;
|
---|
791 | }
|
---|
792 | }
|
---|
793 |
|
---|
794 | hw->wpos = (hw->wpos + read_samples) % hw->samples;
|
---|
795 | return read_samples;
|
---|
796 | #endif
|
---|
797 | }
|
---|
798 |
|
---|
799 |
|
---|
800 | static int solaudio_read (SWVoiceIn *sw, void *buf, int size)
|
---|
801 | {
|
---|
802 | return audio_pcm_sw_read (sw, buf, size);
|
---|
803 | }
|
---|
804 |
|
---|
805 |
|
---|
806 | static int solaudio_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
---|
807 | {
|
---|
808 | solaudioVoiceIn *pSol = (solaudioVoiceIn *) hw;
|
---|
809 | switch (cmd)
|
---|
810 | {
|
---|
811 | case VOICE_ENABLE:
|
---|
812 | {
|
---|
813 | LogRel(("solaudio: solaudio_ctl_in voice_enable\n"));
|
---|
814 | solaudio_start_in(pSol);
|
---|
815 | break;
|
---|
816 | }
|
---|
817 |
|
---|
818 | case VOICE_DISABLE:
|
---|
819 | {
|
---|
820 | LogRel(("solaudio: solaudio_ctl_in voice_disable\n"));
|
---|
821 | solaudio_stop_in(pSol);
|
---|
822 | break;
|
---|
823 | }
|
---|
824 | }
|
---|
825 | return 0;
|
---|
826 | }
|
---|
827 |
|
---|
828 |
|
---|
829 | static struct audio_pcm_ops solaudio_pcm_ops =
|
---|
830 | {
|
---|
831 | solaudio_init_out,
|
---|
832 | solaudio_fini_out,
|
---|
833 | solaudio_run_out,
|
---|
834 | solaudio_write,
|
---|
835 | solaudio_ctl_out,
|
---|
836 |
|
---|
837 | NULL,
|
---|
838 | NULL,
|
---|
839 | NULL,
|
---|
840 | NULL,
|
---|
841 | NULL
|
---|
842 | };
|
---|
843 |
|
---|
844 |
|
---|
845 | static struct audio_option solaudio_options[] =
|
---|
846 | {
|
---|
847 | {"PlayBufferSize", AUD_OPT_INT, &conf.cbPlayBuffer,
|
---|
848 | "Size of the buffer in bytes", NULL, 0},
|
---|
849 | #if 0
|
---|
850 | {"RECORD_BUFFER_SIZE", AUD_OPT_INT, &conf.cbRecordBuffer,
|
---|
851 | "Size of the record bufffer in bytes", NULL, 0},
|
---|
852 | #endif
|
---|
853 | {NULL, 0, NULL, NULL, NULL, 0}
|
---|
854 | };
|
---|
855 |
|
---|
856 |
|
---|
857 | struct audio_driver solaudio_audio_driver =
|
---|
858 | {
|
---|
859 | INIT_FIELD (name = ) "solaudio",
|
---|
860 | INIT_FIELD (descr = ) "SolarisAudio http://sun.com",
|
---|
861 | INIT_FIELD (options = ) solaudio_options,
|
---|
862 | INIT_FIELD (init = ) solaudio_audio_init,
|
---|
863 | INIT_FIELD (fini = ) solaudio_audio_fini,
|
---|
864 | INIT_FIELD (pcm_ops = ) &solaudio_pcm_ops,
|
---|
865 | INIT_FIELD (can_be_default = ) 1,
|
---|
866 | INIT_FIELD (max_voices_out = ) INT_MAX,
|
---|
867 | INIT_FIELD (max_voices_in = ) 0, /* Input not really supported. */
|
---|
868 | INIT_FIELD (voice_size_out = ) sizeof (solaudioVoiceOut),
|
---|
869 | INIT_FIELD (voice_size_in = ) 0
|
---|
870 | };
|
---|
871 |
|
---|