summaryrefslogtreecommitdiff
path: root/qemu/audio/vd_interface_audio.c
blob: 1de84489e06054aa3907c3666e27a59a2ed7c9fa (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
#include "hw/hw.h"
#include "audio.h"
#define AUDIO_CAP "vd_interface_audio"
#include "audio_int.h"
#include "qemu-timer.h"
#include "interface.h"

//#define WAVE_CAPTURE
#ifdef WAVE_CAPTURE

#include <fcntl.h>

#define WAVE_BUF_SIZE (1024 * 1024 * 20)

typedef struct __attribute__ ((__packed__)) ChunkHeader {
    uint32_t id;
    uint32_t size;
} ChunkHeader;

typedef struct __attribute__ ((__packed__)) FormatInfo {
    uint16_t compression_code;
    uint16_t num_channels;
    uint32_t sample_rate;
    uint32_t average_bytes_per_second;
    uint16_t block_align;
    uint16_t bits_per_sample;
    //uint16_t extra_format_bytes;
    //uint8_t extra[0];
} FormatInfo;

static uint8_t* wave_buf = NULL;
static uint8_t* wave_now = NULL;
static uint8_t* wave_end = NULL;
static int wave_blocked = 0;

static void write_all(int fd, uint8_t* data, uint32_t size)
{
    while (size) {
        int n = write(fd, data, size);
        if (n == -1) {
            if (errno != EINTR) {
                printf("%s: failed\n", __FUNCTION__);
                exit(-1);
            }
        } else {
            data += n;
            size -= n;
        }
    }
}

static void write_wave(void)
{
    static uint32_t file_id = 0;
    char file_name[100];
    ChunkHeader header;
    FormatInfo format;

    if (wave_buf == wave_now) {
        return;
    }

    sprintf(file_name, "/tmp/vdi_%u.wav", ++file_id);
    int fd = open(file_name, O_CREAT| O_TRUNC | O_WRONLY, 0666);
    if (fd == -1) {
        printf("%s: open file %s failed\n", __FUNCTION__, file_name);
        return;
    }

    memcpy((char *)&header.id, "RIFF", 4);
    header.size = 4;
    write_all(fd, (uint8_t *)&header, sizeof(header));
    write_all(fd, (uint8_t *)"WAVE", 4);
    
    memcpy((char *)&header.id, "fmt ", 4);
    header.size = sizeof(format);
    write_all(fd, (uint8_t *)&header, sizeof(header));
    
    format.compression_code = 1;
    format.num_channels = 2;
    format.sample_rate = 44100;
    format.average_bytes_per_second = format.sample_rate * 4;
    format.block_align = 4;
    format.bits_per_sample = 16;
    write_all(fd, (uint8_t *)&format, sizeof(format));
    
    memcpy((char *)&header.id, "data", 4);
    header.size = wave_now - wave_buf;
    write_all(fd, (uint8_t *)&header, sizeof(header));
    write_all(fd, wave_buf, header.size);
    close(fd);
}

static void init_wave(void)
{
    if (!wave_buf) {
        wave_buf = malloc(WAVE_BUF_SIZE);
    }
    wave_now = wave_buf;
    wave_end = wave_buf + WAVE_BUF_SIZE;
}

static void start_wave(void)
{
    wave_blocked = 0;
    wave_now = wave_buf;
}

static void put_wave_data(uint8_t *data, uint32_t size)
{
    if (wave_blocked || size > wave_end - wave_now) {
        wave_blocked = 1;
        return;
    }
    memcpy((void *)wave_now, (void *)data, size);
    wave_now += size;
}

static void end_wave(void)
{
    write_wave();
}

#endif

#define dprintf(format, ...) \
    printf("%s: " format "\n", __FUNCTION__, ## __VA_ARGS__ )

#define ASSERT(x) if (!(x)) {                           \
    printf("%s: ASSERT %s failed\n", __FUNCTION__, #x); \
    abort();                                           \
}

#define LINE_IN_SAMPLES 1024
#define LINE_OUT_SAMPLES 1024

typedef struct InterfaceVoiceOut {
    HWVoiceOut base;
    uint64_t prev_ticks;
} InterfaceVoiceOut;

typedef struct InterfaceVoiceIn {
    HWVoiceOut base;
    uint64_t prev_ticks;
    uint32_t samples[LINE_IN_SAMPLES];
} InterfaceVoiceIn;

static struct audio_option options[] = {
    {NULL, 0, NULL, NULL, NULL, 0}
};

typedef struct Interface_audio {
    PlaybackInterface *play_interface;
    PlaybackPlug *play_plug;
    uint32_t play_avail;
    uint32_t *play_frame;
    uint32_t *play_now;

    RecordInterface *record_interface;
    RecordPlug *record_plug;
    uint32_t silence[LINE_IN_SAMPLES];

    InterfaceVoiceOut *voic_out;
    HWVoiceIn *voic_in;
} Interface_audio;

static Interface_audio driver = {
    .play_interface = NULL,
    .play_plug = NULL,
    .play_avail = 0,
    .play_frame = NULL,
    .play_now = NULL,

    .record_interface = NULL,
    .record_plug = NULL,

    .voic_out = NULL,
    .voic_in = NULL,
};

static VDObjectRef interface_play_plug(PlaybackInterface *playback, PlaybackPlug* plug,
                                int *enabled)
{
    if (driver.play_plug) {
        dprintf("plug failed");
        return INVALID_VD_OBJECT_REF;
    }
    ASSERT(plug && playback == driver.play_interface && enabled);
    driver.play_plug = plug;
    *enabled = driver.voic_out ? driver.voic_out->base.enabled : 0;
    return (VDObjectRef)plug;
}

static void interface_play_unplug(PlaybackInterface *playback, VDObjectRef obj)
{
    if (!driver.play_plug || obj != (VDObjectRef)driver.play_plug) {
        dprintf("unplug failed");
        return;
    }
    ASSERT(playback == driver.play_interface);
    driver.play_plug = NULL;
    driver.play_avail = 0;
    driver.play_frame = NULL;
    driver.play_now = NULL;
}

static void regitser_playback(void)
{
    PlaybackInterface *interface = (PlaybackInterface *)qemu_mallocz(sizeof(*interface));
    static int playback_interface_id = 0;

    if (!interface) {
        printf("%s: malloc failed\n", __FUNCTION__);
        exit(-1);
    }
    interface->base.base_version = VM_INTERFACE_VERSION;
    interface->base.type = VD_INTERFACE_PLAYBACK;
    interface->base.id = playback_interface_id++;
    interface->base.description = "playback";
    interface->base.major_version = VD_INTERFACE_PLAYBACK_MAJOR;
    interface->base.minor_version = VD_INTERFACE_PLAYBACK_MINOR;

    interface->plug = interface_play_plug;
    interface->unplug = interface_play_unplug;

    driver.play_interface = interface;
    add_interface(&interface->base);
}

static VDObjectRef interface_record_plug(RecordInterface *recorder, RecordPlug* plug,
                                  int *enabled)
{
    ASSERT(!driver.record_plug && plug && recorder == driver.record_interface
           && enabled);
    driver.record_plug = plug;
    *enabled = driver.voic_in ? driver.voic_in->enabled : 0;
    return (VDObjectRef)plug;
}

static void interface_record_unplug(RecordInterface *recorder, VDObjectRef obj)
{
    ASSERT(driver.record_plug && recorder == driver.record_interface);
    driver.record_plug = NULL;
}

static void regitser_record(void)
{
    RecordInterface *interface = (RecordInterface *)qemu_mallocz(sizeof(*interface));
    static int record_interface_id = 0;

    if (!interface) {
        printf("%s: malloc failed\n", __FUNCTION__);
        exit(-1);
    }
    interface->base.base_version = VM_INTERFACE_VERSION;
    interface->base.type = VD_INTERFACE_RECORD;
    interface->base.id = record_interface_id++;
    interface->base.description = "record";
    interface->base.major_version = VD_INTERFACE_RECORD_MAJOR;
    interface->base.minor_version = VD_INTERFACE_RECORD_MINOR;

    interface->plug = interface_record_plug;
    interface->unplug = interface_record_unplug;

    driver.record_interface = interface;
    add_interface(&interface->base);
}

static void *interface_audio_init(void)
{
    dprintf("");
    if (VD_INTERFACE_PLAYBACK_FMT != VD_INTERFACE_AUDIO_FMT_S16 ||
        VD_INTERFACE_PLAYBACK_CHAN != 2 ||
        VD_INTERFACE_RECORD_FMT != VD_INTERFACE_AUDIO_FMT_S16 ||
        VD_INTERFACE_RECORD_CHAN != 2) {
        dprintf("bad dormat");
        exit(-1);
    }
    
    ASSERT(driver.play_interface == NULL && driver.record_interface == NULL);
    memset(driver.silence, 0, sizeof(driver.silence));
    regitser_playback();
    regitser_record();
    return &driver;
}

static void interface_audio_fini(void *opaque)
{
    dprintf("");

    if (driver.play_interface) {
        remove_interface(&driver.play_interface->base);
        ASSERT(!driver.play_plug);
        free(driver.play_interface);
        driver.play_interface = NULL;
    }

    if (driver.record_interface) {
        remove_interface(&driver.record_interface->base);
        ASSERT(!driver.record_plug);
        free(driver.record_interface);
        driver.record_interface = NULL;
    }
}

static const char *fmt_to_str(audfmt_e fmt)
{
    switch(fmt) {
    case AUD_FMT_U8:
        return "AUD_FMT_U8";
    case AUD_FMT_S8:
        return "AUD_FMT_S8";
    case AUD_FMT_U16:
        return "AUD_FMT_U16";
    case AUD_FMT_S16:
        return "AUD_FMT_S16";
    case AUD_FMT_U32:
        return "AUD_FMT_U32";
    case AUD_FMT_S32:
        return "AUD_FMT_S32";
    default:
        return "???";
    }
}

static int line_out_init(HWVoiceOut *hw, struct audsettings *as)
{
    InterfaceVoiceOut *voice_out = (InterfaceVoiceOut *)hw;
    struct audsettings settings;

    dprintf("freq %d channels %d format %s%s",
            as->freq, 
            as->nchannels, 
            fmt_to_str(as->fmt),
            as->endianness == AUDIO_HOST_ENDIANNESS ? " HOST_ENDIANNESS" : "" );


    settings.freq = VD_INTERFACE_PLAYBACK_FREQ; 
    settings.nchannels = VD_INTERFACE_PLAYBACK_CHAN;
    settings.fmt = AUD_FMT_S16;
    settings.endianness = AUDIO_HOST_ENDIANNESS;

    audio_pcm_init_info(&hw->info, &settings);
    hw->samples = LINE_OUT_SAMPLES;
    driver.voic_out = voice_out;
    return 0;
}

static void line_out_fini(HWVoiceOut *hw)
{
    driver.voic_out = NULL;
}

static uint64_t get_monotonic_time(void)
{
    struct timespec time_space;
    clock_gettime(CLOCK_MONOTONIC, &time_space);
    return time_space.tv_sec * 1000 * 1000 * 1000 + time_space.tv_nsec;
}

static int line_out_run(HWVoiceOut *hw)
{
    InterfaceVoiceOut *voice_out = (InterfaceVoiceOut *)hw;
    int rpos, live, decr;
    int samples;
    uint64_t now;
    uint64_t ticks;
    uint64_t bytes;

    if (!(live = audio_pcm_hw_get_live_out(hw))) {
        return 0;
    }

    now = get_monotonic_time();
    ticks = now - voice_out->prev_ticks;
    bytes = (ticks * hw->info.bytes_per_second) / (1000 * 1000 * 1000);

    voice_out->prev_ticks = now;

    decr = (bytes > INT_MAX) ? INT_MAX >> hw->info.shift :
                                        (bytes + (1 << (hw->info.shift - 1))) >> hw->info.shift;
    decr = audio_MIN(live, decr);

    samples = decr;
    rpos = hw->rpos;
    while (samples) {
        int left_till_end_samples = hw->samples - rpos;
        int len = audio_MIN(samples, left_till_end_samples);

        if (driver.play_plug && !driver.play_frame) {
            driver.play_plug->get_frame(driver.play_plug,
                                        &driver.play_frame,
                                        &driver.play_avail);
            driver.play_now = driver.play_frame;
        }
        if (driver.play_avail) {
            len = audio_MIN(len, driver.play_avail);
            hw->clip(driver.play_now, hw->mix_buf + rpos, len);
            if (!(driver.play_avail -= len)) {
                ASSERT(driver.play_plug);
                driver.play_plug->put_frame(driver.play_plug,
                                            driver.play_frame);
                driver.play_frame = driver.play_now = NULL;
            } else {
                driver.play_now += len;
            }
        }
        rpos = (rpos + len) % hw->samples;
        samples -= len;
    }
    hw->rpos = rpos;
    return decr;
}

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

static int line_out_ctl(HWVoiceOut *hw, int cmd, ...)
{
    InterfaceVoiceOut *voice_out = (InterfaceVoiceOut *)hw;

    switch (cmd) {
    case VOICE_ENABLE:
        voice_out->prev_ticks = get_monotonic_time();
        if (driver.play_plug) {
            driver.play_plug->start(driver.play_plug);
        }
        break;
    case VOICE_DISABLE:
        if (driver.play_plug) {
            if (driver.play_frame) {
                uint32_t *frame = driver.play_frame;
                memset(driver.play_now, 0, driver.play_avail << 2);
                driver.play_avail = 0;
                driver.play_now = driver.play_frame = NULL;
                driver.play_plug->put_frame(driver.play_plug, frame);
            }

            if (driver.play_plug) {
                driver.play_plug->stop(driver.play_plug);
            }
        }
        break;
    }
    return 0;
}

static int line_in_init(HWVoiceIn *hw, struct audsettings *as)
{
    struct audsettings settings;

    dprintf("");
#ifdef WAVE_CAPTURE
    init_wave();
#endif
    settings.freq = VD_INTERFACE_RECORD_FREQ; 
    settings.nchannels = VD_INTERFACE_RECORD_CHAN;
    if (VD_INTERFACE_PLAYBACK_FMT != VD_INTERFACE_AUDIO_FMT_S16) {
        dprintf("bad vd interface format");
        return -1;
    }
    settings.fmt = AUD_FMT_S16;
    settings.endianness = AUDIO_HOST_ENDIANNESS;

    audio_pcm_init_info(&hw->info, &settings);
    hw->samples = LINE_IN_SAMPLES;
    driver.voic_in = hw;
    return 0;
}

static void line_in_fini(HWVoiceIn *hw)
{
    driver.voic_in = NULL;
}

static int line_in_run(HWVoiceIn *hw)
{
    InterfaceVoiceIn *voice_in = (InterfaceVoiceIn *)hw;
    int num_samples;
    int ready;
    int len[2];
    uint64_t now;
    uint64_t ticks;
    uint64_t delta_samp;
    uint32_t *samples;

    if (!(num_samples = hw->samples - audio_pcm_hw_get_live_in(hw))) {
        return 0;
    }

    now = get_monotonic_time();
    ticks = now - voice_in->prev_ticks;
    voice_in->prev_ticks = now;
    delta_samp = (ticks * hw->info.bytes_per_second) / (1000 * 1000 * 1000);
    delta_samp = (delta_samp + (1 << (hw->info.shift - 1))) >> hw->info.shift;

    num_samples = audio_MIN(num_samples, delta_samp);

    if (driver.record_plug) {
        ready = driver.record_plug->read(driver.record_plug, num_samples, voice_in->samples);
        samples = voice_in->samples;
    } else {
        ready = num_samples;
        samples = driver.silence;
    }

    num_samples = audio_MIN(ready, num_samples);

    if (hw->wpos + num_samples > hw->samples) {
        len[0] = hw->samples - hw->wpos;
        len[1] = num_samples - len[0];
    } else {
        len[0] = num_samples;
        len[1] = 0;
    }

#ifdef WAVE_CAPTURE
    put_wave_data((uint8_t *)samples, len[0] * 4);
#endif
    hw->conv(hw->conv_buf + hw->wpos, samples, len[0], &nominal_volume);

    if (len[1]) {
#ifdef WAVE_CAPTURE
        put_wave_data((uint8_t *)(samples + len[0]), len[1] * 4);
#endif
        hw->conv(hw->conv_buf, samples + len[0], len[1],
                 &nominal_volume);
    }

    hw->wpos = (hw->wpos + num_samples) % hw->samples;

    return num_samples;
}

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

static int line_in_ctl(HWVoiceIn *hw, int cmd, ...)
{
    InterfaceVoiceIn *voice_in = (InterfaceVoiceIn *)hw;

    switch (cmd) {
    case VOICE_ENABLE:
#ifdef WAVE_CAPTURE
        start_wave();
#endif
        voice_in->prev_ticks = get_monotonic_time();
        if (driver.record_plug) {
            driver.record_plug->start(driver.record_plug);
        }
        break;
    case VOICE_DISABLE:
#ifdef WAVE_CAPTURE
        end_wave();
#endif
        if (driver.record_plug) {
            driver.record_plug->stop(driver.record_plug);
        }
        break;
    }
    return 0;
}


static struct audio_pcm_ops audio_callbacks = {
    line_out_init,
    line_out_fini,
    line_out_run,
    line_out_write,
    line_out_ctl,

    line_in_init,
    line_in_fini,
    line_in_run,
    line_in_read,
    line_in_ctl,
};

struct audio_driver interface_audio_driver = {
    INIT_FIELD (name = ) "vd_interface",
    INIT_FIELD (descr = ) "vd_interface audio driver",
    INIT_FIELD (options = ) options,
    INIT_FIELD (init = ) interface_audio_init,
    INIT_FIELD (fini = ) interface_audio_fini,
    INIT_FIELD (pcm_ops = ) &audio_callbacks,
    INIT_FIELD (can_be_default = ) 1,
    INIT_FIELD (max_voices_out = ) 1,
    INIT_FIELD (max_voices_in  = ) 1,
    INIT_FIELD (voice_size_out = ) sizeof (InterfaceVoiceOut),
    INIT_FIELD (voice_size_in  = ) sizeof (InterfaceVoiceIn),
};