summaryrefslogtreecommitdiff
path: root/src/VBox/Devices/Audio/solaudio.c
blob: 6998eead28a4927cfd7dc53b39e8dd36ec52eacd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
/* $Id: solaudio.c $ */
/** @file
 * VirtualBox Audio Driver - Solaris host.
 */

/*
 * Copyright (C) 2006-2012 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#include <unistd.h>
#include <errno.h>
#include <stropts.h>
#include <fcntl.h>
#include <sys/audio.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mixer.h>

#define LOG_GROUP LOG_GROUP_DEV_AUDIO
#include <VBox/log.h>
#include <iprt/env.h>

#include "VBoxDD.h"
#include "vl_vbox.h"
#include "audio.h"
#include <iprt/alloc.h>

#define AUDIO_CAP "solaudio"
#include "audio_int.h"

/*******************************************************************************
*   Structures and Typedefs                                                    *
*******************************************************************************/
typedef struct solaudioVoiceOut
{
    HWVoiceOut    Hw;
    audio_info_t  AudioInfo;
    uint_t        cBuffersPlayed;
    void         *pPCMBuf;
} solaudioVoiceOut;

typedef struct solaudioVoiceIn
{
    HWVoiceIn     Hw;
    audio_info_t  AudioInfo;
    void         *pPCMBuf;
} solaudioVoiceIn;


/*******************************************************************************
*   Global Variables                                                           *
*******************************************************************************/
static struct
{
    int cbPlayBuffer;
    int cbRecordBuffer;
} conf =
{
    INIT_FIELD (cbPlayBuffer =) 4352,
    INIT_FIELD (cbRecordBuffer = ) 8192
};

static int   g_AudioDev = -1;
static int   g_RecordDev = -1;
static int   g_AudioCtl = -1;
static char *g_pszAudioDev = NULL;
static char *g_pszAudioCtl = NULL;

typedef enum
{
    enmPlay          = 6,
    enmRecord        = 9,
    enmRecordPassive = 15
} audio_dest_t;


static int aud_to_solfmt (audfmt_e fmt)
{
    switch (fmt)
    {
        case AUD_FMT_S8:
        case AUD_FMT_U8:
            return AUDIO_PRECISION_8;

        case AUD_FMT_S16:
        case AUD_FMT_U16:
            return AUDIO_PRECISION_16;

        default:
            LogRel(("solaudio: aud_to_solfmt: Bad audio format %d\n", fmt));
            return AUDIO_PRECISION_8;
    }
}


static int sol_to_audfmt (int fmt, int encoding)
{
    switch (fmt)
    {
        case AUDIO_PRECISION_8:
        {
            if (encoding == AUDIO_ENCODING_LINEAR8)
                return AUD_FMT_U8;
            else
                return AUD_FMT_S8;
            break;
        }

        case AUDIO_PRECISION_16:
        {
            if (encoding == AUDIO_ENCODING_LINEAR)
                return AUD_FMT_S16;
            else
                return AUD_FMT_U16;
            break;
        }

        default:
            LogRel(("solaudio: sol_to_audfmt: Bad audio format %d\n", fmt));
            return AUD_FMT_S8;
    }
}


static char *solaudio_getdevice (void)
{
    /*
     * This is for multiple audio devices where env. var determines current one,
     * otherwise else we fallback to default.
     */
    char *pszAudioDev = RTEnvDupEx(RTENV_DEFAULT, "AUDIODEV");
    if (!pszAudioDev)
        pszAudioDev = RTStrDup("/dev/audio");
    return pszAudioDev;
}


static void solaudio_close_device (audio_dest_t dst)
{
    LogFlow(("solaudio: solaudio_close_device\n"));
    switch (dst)
    {
        case enmPlay:
        {
            close(g_AudioDev);
            g_AudioDev = -1;
            break;
        }

        case enmRecord:
        case enmRecordPassive:
        {
            close(g_RecordDev);
            g_RecordDev = -1;
            break;
        }

        default:
            LogRel(("solaudio: cannot close. invalid audio destination %d.\n", dst));
    }
}


static int solaudio_open_device (audio_dest_t dst)
{
    int rc = 0;

    LogFlow(("solaudio: solaudio_open_device dest=%d\n", dst));

    switch (dst)
    {
        case enmPlay:
        {
            LogFlow(("solaudio: open_device for enmPlay\n"));
            g_AudioDev = open(g_pszAudioDev, O_WRONLY | O_NONBLOCK);
            if (g_AudioDev < 0)
            {
                LogRel(("solaudio: failed to open device %s dst=%d\n", g_pszAudioDev, dst));
                rc = -1;
            }
            break;
        }

        case enmRecord:
        case enmRecordPassive:
        {
            LogFlow(("solaudio: open_device for enmRecord\n"));
            g_RecordDev = open(g_pszAudioDev, (dst == enmRecord ? O_RDONLY : O_WRONLY) | O_NONBLOCK);
            if (g_RecordDev < 0)
            {
                LogRel(("solaudio: failed to open device %s dst=%d\n", g_pszAudioDev, dst));
                rc = -1;
            }
            break;
        }

        default:
            LogRel(("solaudio: Invalid audio destination.\n"));
            break;
    }
    return rc;
}


static int solaudio_setattrs(audio_dest_t dst, audio_info_t *info)
{
    audio_info_t AudioInfo;
    audio_prinfo_t *pDstInfo;
    audio_prinfo_t *pSrcInfo;

    LogFlow(("solaudio: solaudio_setattrs dst=%d info=%p\n", dst, info));

    if (!info)
        return -1;

    AUDIO_INITINFO(&AudioInfo);
    if (ioctl(dst == enmPlay ? g_AudioDev : g_RecordDev, AUDIO_GETINFO, &AudioInfo) < 0)
    {
        LogRel(("solaudio: AUDIO_GETINFO failed\n"));
        return -1;
    }

    if (dst == enmPlay)
    {
        pDstInfo = &AudioInfo.play;
        pSrcInfo = &info->play;
    }
    else
    {
        pDstInfo = &AudioInfo.record;
        pSrcInfo = &info->record;
    }

    pDstInfo->sample_rate = pSrcInfo->sample_rate;
    pDstInfo->channels = pSrcInfo->channels;
    pDstInfo->precision = pSrcInfo->precision;
    pDstInfo->encoding = pSrcInfo->encoding;
    pDstInfo->buffer_size = pSrcInfo->buffer_size;
    pDstInfo->gain = AUDIO_MAX_GAIN;
    pDstInfo->open = 0;

    if (ioctl(dst == enmPlay ? g_AudioDev : g_RecordDev, AUDIO_SETINFO, &AudioInfo) < 0)
    {
        LogRel(("solaudio: AUDIO_SETINFO failed\n"));
        return -1;
    }
    return 0;
}


static int solaudio_init_out (HWVoiceOut *hw, audsettings_t *as)
{
    solaudioVoiceOut *pSol = (solaudioVoiceOut *)hw;
    audsettings_t ObtAudioInfo;

    AUDIO_INITINFO(&pSol->AudioInfo);
    pSol->AudioInfo.play.sample_rate = as->freq;
    pSol->AudioInfo.play.channels = as->nchannels;
    pSol->AudioInfo.play.precision = aud_to_solfmt(as->fmt);
    pSol->AudioInfo.play.buffer_size = conf.cbPlayBuffer;

    if (as->fmt == AUD_FMT_U8)
        pSol->AudioInfo.play.encoding = AUDIO_ENCODING_LINEAR8;
    else
        pSol->AudioInfo.play.encoding = AUDIO_ENCODING_LINEAR;

    /* Open device for playback. */
    if (solaudio_open_device(enmPlay))
    {
        LogRel(("solaudio: solaudio_open failed\n"));
        return -1;
    }

    /* Specify playback attributes to device. */
    if (solaudio_setattrs(enmPlay, &pSol->AudioInfo))
    {
        LogRel(("solaudio: failed to set playback attributes.\n"));
        return -1;
    }

    /* Copy obtained playback attributes. */
    ObtAudioInfo.freq = pSol->AudioInfo.play.sample_rate;
    ObtAudioInfo.nchannels = pSol->AudioInfo.play.channels;
    ObtAudioInfo.fmt = sol_to_audfmt(pSol->AudioInfo.play.precision, pSol->AudioInfo.play.encoding);
    ObtAudioInfo.endianness = as->endianness;

    audio_pcm_init_info(&hw->info, &ObtAudioInfo);
    pSol->cBuffersPlayed = 0;

    hw->samples = pSol->AudioInfo.play.buffer_size >> hw->info.shift;
    pSol->pPCMBuf = RTMemAllocZ(pSol->AudioInfo.play.buffer_size);
    if (!pSol->pPCMBuf)
    {
        LogRel(("solaudio: failed to alloc %d %d bytes to pPCMBuf\n", hw->samples << hw->info.shift, hw->samples));
        return -1;
    }
    LogFlow(("solaudio: init_out hw->samples=%d play.buffer_size=%d\n", hw->samples, pSol->AudioInfo.play.buffer_size));
    return 0;
}


static void solaudio_fini_out (HWVoiceOut *hw)
{
    solaudioVoiceOut *sol = (solaudioVoiceOut *) hw;
    LogFlow(("solaudio: fini_out\n"));

    solaudio_close_device(enmPlay);
    if (sol->pPCMBuf)
    {
        RTMemFree(sol->pPCMBuf);
        sol->pPCMBuf = NULL;
    }
}


static void solaudio_start_out (HWVoiceOut *hw)
{
    audio_info_t AudioInfo;
    solaudioVoiceOut *pSol = (solaudioVoiceOut *)hw;
    LogFlow(("solaudio: voice_enable\n"));

    audio_pcm_info_clear_buf(&hw->info, pSol->pPCMBuf, hw->samples);

    AUDIO_INITINFO(&AudioInfo);
    ioctl(g_AudioDev, AUDIO_GETINFO, &AudioInfo);
    AudioInfo.play.pause = 0;
#if 0
    AudioInfo.play.eof = 0;
    AudioInfo.play.samples = 0;
    pSol->cBuffersPlayed = 0;
#endif
    ioctl(g_AudioDev, AUDIO_SETINFO, &AudioInfo);
}


static void solaudio_stop_out (solaudioVoiceOut *sol)
{
    audio_info_t AudioInfo;
    LogFlow(("solaudio: stop_out\n"));

    if (ioctl(g_AudioCtl, I_SETSIG, 0) < 0)
    {
        Log(("solaudio: failed to stop signalling\n"));
        return;
    }

    if (ioctl(g_AudioDev, I_FLUSH, FLUSHW) < 0)
    {
        LogRel(("solaudio: failed to drop unplayed buffers\n"));
        return;
    }

    AUDIO_INITINFO(&AudioInfo);
    AudioInfo.play.pause = 1;
#if 0
    AudioInfo.play.samples = 0;
    AudioInfo.play.eof = 0;
    AudioInfo.play.error = 0;
    sol->cBuffersPlayed = 0;
#endif
    if (ioctl(g_AudioDev, AUDIO_SETINFO, &AudioInfo) < 0)
    {
        LogRel(("solaudio: AUDIO_SETINFO failed during stop_out.\n"));
        return;
    }
}


static int solaudio_availbuf (solaudioVoiceOut *sol)
{
    int cbPlayBuffer = 0;
    if (ioctl(g_AudioDev, AUDIO_GETINFO, &sol->AudioInfo) < 0)
    {
        LogRel(("solaudio: AUDIO_GETINFO ioctl failed\n"));
        return -1;
    }

    if (sol->cBuffersPlayed - sol->AudioInfo.play.eof <= 2)
        cbPlayBuffer = conf.cbPlayBuffer;

    /* Check for overflow */
    if (sol->cBuffersPlayed > UINT_MAX - 4)
    {
        sol->cBuffersPlayed -= UINT_MAX - 4;
        sol->AudioInfo.play.eof -= UINT_MAX - 4;
        ioctl(g_AudioDev, AUDIO_SETINFO, &sol->AudioInfo);
    }

    LogFlow(("avail: eof=%d samples=%d bufplayed=%d avail=%d\n", sol->AudioInfo.play.eof, sol->AudioInfo.play.samples,
            sol->cBuffersPlayed, cbPlayBuffer));
    return cbPlayBuffer;
}


static int solaudio_run_out (HWVoiceOut *hw)
{
    solaudioVoiceOut *pSol = (solaudioVoiceOut *) hw;
    int          csLive, csDecr, csSamples, csToWrite, csAvail;
    size_t       cbAvail, cbToWrite;
    ssize_t      cbWritten;
    uint8_t     *pu8Dst;
    st_sample_t *psSrc;

    csLive = audio_pcm_hw_get_live_out(hw);
    if (!csLive)
        return 0;

    cbAvail = solaudio_availbuf(pSol);
    if (cbAvail <= 0)
        return 0;

    csAvail   = cbAvail >> hw->info.shift; /* bytes => samples */
    csDecr    = audio_MIN(csLive, csAvail);
    csSamples = csDecr;

    while (csSamples)
    {
        /* split request at the end of our samples buffer */
        csToWrite = audio_MIN(csSamples, hw->samples - hw->rpos);
        cbToWrite = csToWrite << hw->info.shift;
        psSrc     = hw->mix_buf + hw->rpos;
        pu8Dst    = advance(pSol->pPCMBuf, hw->rpos << hw->info.shift);

        hw->clip(pu8Dst, psSrc, csToWrite);

        cbWritten = write(g_AudioDev, pu8Dst, cbToWrite);
        if (cbWritten < 0)
            break;

        hw->rpos   = (hw->rpos + csToWrite) % hw->samples;
        csSamples -= csToWrite;
    }

    /* Increment eof marker for synchronous buffer processed */
    write (g_AudioDev, NULL, 0);
    pSol->cBuffersPlayed++;
    return csDecr;
}


static int solaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
{
    solaudioVoiceOut *pSol = (solaudioVoiceOut *) hw;
    switch (cmd)
    {
        case VOICE_ENABLE:
        {
            LogFlow(("solaudio: voice_enable\n"));
            solaudio_start_out(hw);
            break;
        }

        case VOICE_DISABLE:
        {
            LogFlow(("solaudio: voice_disable\n"));
            solaudio_stop_out(pSol);
            break;
        }
    }
    return 0;
}


static int solaudio_write (SWVoiceOut *sw, void *buf, int len)
{
    return audio_pcm_sw_write (sw, buf, len);
}


static void *solaudio_audio_init (void)
{
    struct stat FileStat;

    LogFlow(("solaudio_audio_init\n"));
    if (!g_pszAudioDev)
    {
        g_pszAudioDev = solaudio_getdevice();
        if (!g_pszAudioDev)
        {
            LogRel(("solaudio: solaudio_getdevice() failed to return a valid device.\n"));
            return NULL;
        }
    }

    if (stat(g_pszAudioDev, &FileStat) < 0)
    {
        LogRel(("solaudio: failed to stat %s\n", g_pszAudioDev));
        return NULL;
    }

    if (!S_ISCHR(FileStat.st_mode))
    {
        LogRel(("solaudio: invalid mode for %s\n", g_pszAudioDev));
        return NULL;
    }

    if (!g_pszAudioCtl)
        RTStrAPrintf(&g_pszAudioCtl, "%sctl", g_pszAudioDev);

    if (g_AudioCtl < 0)
    {
        g_AudioCtl = open(g_pszAudioCtl, O_RDWR | O_NONBLOCK);
        if (g_AudioCtl < 0)
        {
            LogRel(("solaudio: failed to open device %s\n", g_pszAudioCtl));
            return NULL;
        }
    }

    return &conf;
}


static void solaudio_audio_fini (void *opaque)
{
    LogFlow(("solaudio_audio_fini\n"));
    if (g_pszAudioDev)
    {
        RTStrFree(g_pszAudioDev);
        g_pszAudioDev = NULL;
    }
    if (g_pszAudioCtl)
    {
        RTStrFree(g_pszAudioCtl);
        g_pszAudioCtl = NULL;
    }
    close(g_AudioCtl);
    g_AudioCtl = -1;

    NOREF(opaque);
}


/* -=-=-=-=- Audio Input -=-=-=-=- */

static void solaudio_pause_record(void)
{
    audio_info_t AudioInfo;
    AUDIO_INITINFO(&AudioInfo);
    if (ioctl(g_RecordDev, AUDIO_GETINFO, &AudioInfo) < 0)
    {
        LogRel(("solaudio: failed to get info. to pause recording.\n"));
        return;
    }

    AudioInfo.record.pause = 1;
    if (ioctl(g_RecordDev, AUDIO_SETINFO, &AudioInfo))
        LogRel(("solaudio: failed to pause recording.\n"));
}


static void solaudio_resume_record(void)
{
    audio_info_t AudioInfo;
    AUDIO_INITINFO(&AudioInfo);
    if (ioctl(g_RecordDev, AUDIO_GETINFO, &AudioInfo) < 0)
    {
        LogRel(("solaudio: failed to get info. to resume recording.\n"));
        return;
    }

    AudioInfo.record.pause = 0;
    if (ioctl(g_RecordDev, AUDIO_SETINFO, &AudioInfo))
        LogRel(("solaudio: failed to resume recording.\n"));
}



static void solaudio_stop_in (solaudioVoiceIn *sol)
{
    audio_info_t AudioInfo;
    LogFlow(("solaudio: stop_in\n"));

    if (ioctl(g_AudioCtl, I_SETSIG, 0) < 0)
    {
        Log(("solaudio: failed to stop signalling\n"));
        return;
    }

    if (ioctl(g_RecordDev, I_FLUSH, FLUSHR) < 0)
    {
        LogRel(("solaudio: failed to drop record buffers\n"));
        return;
    }

    AUDIO_INITINFO(&AudioInfo);
    AudioInfo.record.samples = 0;
    AudioInfo.record.pause = 1;
    AudioInfo.record.eof = 0;
    AudioInfo.record.error = 0;
    if (ioctl(g_RecordDev, AUDIO_SETINFO, &AudioInfo) < 0)
    {
        LogRel(("solaudio: AUDIO_SETINFO failed during stop_in.\n"));
        return;
    }

    solaudio_close_device(enmRecord);
}


static void solaudio_start_in (solaudioVoiceIn *sol)
{
    LogFlow(("solaudio: start_in\n"));
    if (solaudio_open_device(enmRecord))
    {
        LogRel(("solaudio: failed to open for recording.\n"));
    }

    if (solaudio_setattrs(enmRecord, &sol->AudioInfo))
    {
        LogRel(("solaudio: solaudio_setattrs for recording failed.\n"));
        return;
    }
    solaudio_resume_record();
}


static int solaudio_init_in (HWVoiceIn *hw, audsettings_t *as)
{
    solaudioVoiceIn *pSol = (solaudioVoiceIn *)hw;
    audsettings_t ObtAudioInfo;

    AUDIO_INITINFO(&pSol->AudioInfo);
    pSol->AudioInfo.record.sample_rate = as->freq;
    pSol->AudioInfo.record.channels = as->nchannels;
    pSol->AudioInfo.record.precision = aud_to_solfmt(as->fmt);
    pSol->AudioInfo.record.buffer_size = conf.cbRecordBuffer;

    if (as->fmt == AUD_FMT_U8)
        pSol->AudioInfo.record.encoding = AUDIO_ENCODING_LINEAR8;
    else
        pSol->AudioInfo.record.encoding = AUDIO_ENCODING_LINEAR;

    /*
     * Open device for recording in passive mode (O_WRONLY) as we do not
     * want to start buffering audio immediately. This is what is recommended.
     */
    if (solaudio_open_device(enmRecordPassive))
    {
        LogRel(("solaudio: solaudio_open failed.\n"));
        return -1;
    }

    /* Specify playback attributes to device. */
    if (solaudio_setattrs(enmRecord, &pSol->AudioInfo))
    {
        LogRel(("solaudio: failed to set playback attributes.\n"));
        return -1;
    }

    /* Copy obtained record attributes. */
    ObtAudioInfo.freq = pSol->AudioInfo.record.sample_rate;
    ObtAudioInfo.nchannels = pSol->AudioInfo.record.channels;
    ObtAudioInfo.fmt = sol_to_audfmt(pSol->AudioInfo.record.precision, pSol->AudioInfo.record.encoding);
    ObtAudioInfo.endianness = as->endianness;

    audio_pcm_init_info(&hw->info, &ObtAudioInfo);

    hw->samples = pSol->AudioInfo.record.buffer_size >> hw->info.shift;
    pSol->pPCMBuf = RTMemAllocZ(pSol->AudioInfo.record.buffer_size);
    if (!pSol->pPCMBuf)
    {
        LogRel(("solaudio: init_in: failed to alloc %d %d bytes to pPCMBuf\n", hw->samples << hw->info.shift, hw->samples));
        return -1;
    }
    solaudio_close_device(enmRecordPassive);
    LogFlow(("solaudio: init_in: hw->samples=%d record.buffer_size=%d rate=%d\n", hw->samples, pSol->AudioInfo.record.buffer_size,
            pSol->AudioInfo.record.sample_rate));
    return 0;
}


static void solaudio_fini_in (HWVoiceIn *hw)
{
    solaudioVoiceIn *sol = (solaudioVoiceIn *) hw;
    LogFlow(("solaudio: fini_in done\n"));

    if (sol->pPCMBuf)
    {
        RTMemFree(sol->pPCMBuf);
        sol->pPCMBuf = NULL;
    }
}


static int solaudio_run_in (HWVoiceIn *hw)
{
#if 0
    solaudioVoiceIn *pSol = (solaudioVoiceIn *) hw;
    int          csDead, csDecr = 0, csSamples, csRead, csAvail;
    size_t       cbAvail, cbRead;
    void        *pu8Src;
    st_sample_t *psDst;

    csDead = hw->samples - audio_pcm_hw_get_live_in (hw);

    if (!csDead)
        return 0;

    if (ioctl(g_AudioDev, I_NREAD, &cbAvail) < 0)
    {
        LogRel(("solaudio: I_NREAD failed\n"));
        return 0;
    }

    if (!cbAvail)
        return 0;

    cbAvail = audio_MIN(cbAvail, conf.cbRecordBuffer);
    pu8Src = pSol->pPCMBuf;
    cbRead = read(g_AudioDev, pu8Src, cbAvail);
    if (cbRead <= 0)
        return 0;

    csAvail = cbAvail >> hw->info.shift;
    csDecr  = audio_MIN (csDead, csAvail);
    csSamples = csDecr;

    while (csSamples)
    {
        /* split request at the end of our samples buffer */
        psDst      = hw->conv_buf + hw->wpos;
        csRead     = audio_MIN (csSamples, hw->samples - hw->wpos);
        hw->conv (psDst, pu8Src, csRead, &pcm_in_volume);
        hw->wpos   = (hw->wpos + csRead) % hw->samples;
        csSamples -= csRead;
        pu8Src     = (void *)((uint8_t*)pu8Src + (csRead << hw->info.shift));
    }
    return csDecr;
#else
    solaudioVoiceIn *sol = (solaudioVoiceIn *) hw;
    int hwshift = hw->info.shift;
    int i;
    int live = audio_pcm_hw_get_live_in (hw);
    int dead = hw->samples - live;
    size_t read_samples = 0;
    struct
    {
        int add;
        int len;
    } bufs[2];

    bufs[0].add = hw->wpos;
    bufs[0].len = 0;
    bufs[1].add = 0;
    bufs[1].len = 0;

    if (!dead) {
        return 0;
    }

    if (hw->wpos + dead > hw->samples)
    {
        bufs[0].len = (hw->samples - hw->wpos) << hwshift;
        bufs[1].len = (dead - (hw->samples - hw->wpos)) << hwshift;
    }
    else
        bufs[0].len = dead << hwshift;


    for (i = 0; i < 2; ++i)
    {
        ssize_t nread;

        if (bufs[i].len)
        {
            void *p = advance (sol->pPCMBuf, bufs[i].add << hwshift);
            nread = read (g_RecordDev, p, bufs[i].len);

            if (nread > 0)
            {
                read_samples += nread >> hwshift;
                hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift,
                          &pcm_in_volume);
            }

            if (bufs[i].len - nread)
                if (nread == -1)
                    break;
        }
    }

    hw->wpos = (hw->wpos + read_samples) % hw->samples;
    return read_samples;
#endif
}


static int solaudio_read (SWVoiceIn *sw, void *buf, int size)
{
    return audio_pcm_sw_read (sw, buf, size);
}


static int solaudio_ctl_in (HWVoiceIn *hw, int cmd, ...)
{
    solaudioVoiceIn *pSol = (solaudioVoiceIn *) hw;
    switch (cmd)
    {
        case VOICE_ENABLE:
        {
            LogRel(("solaudio: solaudio_ctl_in voice_enable\n"));
            solaudio_start_in(pSol);
            break;
        }

        case VOICE_DISABLE:
        {
            LogRel(("solaudio: solaudio_ctl_in voice_disable\n"));
            solaudio_stop_in(pSol);
            break;
        }
    }
    return 0;
}


static struct audio_pcm_ops solaudio_pcm_ops =
{
    solaudio_init_out,
    solaudio_fini_out,
    solaudio_run_out,
    solaudio_write,
    solaudio_ctl_out,

    NULL,
    NULL,
    NULL,
    NULL,
    NULL
};


static struct audio_option solaudio_options[] =
{
    {"PlayBufferSize", AUD_OPT_INT, &conf.cbPlayBuffer,
     "Size of the buffer in bytes", NULL, 0},
#if 0
    {"RECORD_BUFFER_SIZE", AUD_OPT_INT, &conf.cbRecordBuffer,
     "Size of the record bufffer in bytes", NULL, 0},
#endif
    {NULL, 0, NULL, NULL, NULL, 0}
};


struct audio_driver solaudio_audio_driver =
{
    INIT_FIELD (name           = ) "solaudio",
    INIT_FIELD (descr          = ) "SolarisAudio http://sun.com",
    INIT_FIELD (options        = ) solaudio_options,
    INIT_FIELD (init           = ) solaudio_audio_init,
    INIT_FIELD (fini           = ) solaudio_audio_fini,
    INIT_FIELD (pcm_ops        = ) &solaudio_pcm_ops,
    INIT_FIELD (can_be_default = ) 1,
    INIT_FIELD (max_voices_out = ) INT_MAX,
    INIT_FIELD (max_voices_in  = ) 0,           /* Input not really supported. */
    INIT_FIELD (voice_size_out = ) sizeof (solaudioVoiceOut),
    INIT_FIELD (voice_size_in  = ) 0
};