/* build with: * $ gcc -fPIC -DPIC -c -Wall -Wno-unused-variable -Wno-unused-function alsa-example.c * $ gcc -Wl,--no-undefined -shared -fPIC -lasound -o libasound_module_pcm_example.so alsa-example.o -lc * # cp libasound_module_pcm_example.so /usr/lib/alsa-lib/ * create an 'example' PCM in asoundrc of type 'example' * test with attached alsa-test.c */ #include #include #include #include #include #include #include #include #include struct example_pcm { snd_pcm_ioplug_t io; }; static int example_start(snd_pcm_ioplug_t *io) { struct example_pcm *sp_pcm = io->private_data; fprintf(stderr, "ALSA-example: start\n"); return 0; } static int example_stop(snd_pcm_ioplug_t *io) { struct example_pcm *sp_pcm = io->private_data; fprintf(stderr, "ALSA-example: stop\n"); return 0; } static snd_pcm_sframes_t example_pointer(snd_pcm_ioplug_t *io) { struct example_pcm *sp_pcm = io->private_data; fprintf(stderr, "ALSA-example: pointer\n"); return 0; } static snd_pcm_sframes_t example_transfer(snd_pcm_ioplug_t *io, const snd_pcm_channel_area_t *areas, snd_pcm_uframes_t offset, snd_pcm_uframes_t size) { struct example_pcm *sp_pcm = io->private_data; fprintf(stderr, "ALSA-example: transfer\n"); return size; } static int example_close(snd_pcm_ioplug_t *io) { struct example_pcm *sp_pcm = io->private_data; fprintf(stderr, "ALSA-example: close\n"); return 0; } static int example_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params) { struct example_pcm *sp_pcm = io->private_data; fprintf(stderr, "ALSA-example: hw_params\n"); if(io->buffer_size != 8812) fprintf(stderr, "TEST FAILURE: buffer_size != 8812: %lu\n", io->buffer_size); else fprintf(stderr, "TEST SUCCESS: buffer_size == 8812: %lu\n", io->buffer_size); return 0; } static const snd_pcm_ioplug_callback_t example_playback_callback = { example_start, /* start, required */ example_stop, /* stop, required */ example_pointer, /* pointer, required */ example_transfer, /* transfer, optional */ example_close, /* close, optional */ example_hw_params, /* hw_params, optional */ NULL, /* hw_free, optional */ NULL, /* sw_params, optional */ NULL, /* prepare, optional */ NULL, /* drain, optional */ NULL, /* pause, optional */ NULL, /* resume, optional */ NULL, /* poll_descriptors_count, optional */ NULL, /* poll_descriptors, optional */ NULL, /* poll_revents, optional */ NULL, /* dump, optional */ NULL/* delay, optional */ }; static int example_set_hw_params(snd_pcm_ioplug_t *io) { static const snd_pcm_access_t access_list[] = { SND_PCM_ACCESS_RW_INTERLEAVED }; static const unsigned int format_list[] = { SND_PCM_FORMAT_S16 }; int err; if((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS, sizeof(access_list) / sizeof(*access_list), access_list)) < 0) return err; if((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT, sizeof(format_list) / sizeof(*format_list), format_list)) < 0) return err; if((err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE, 44100, 44100)) < 0) return err; if((err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_CHANNELS, 2, 2)) < 0) return err; /* 8 * 256 = 2048 */ if((err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIOD_BYTES, 256,256)) < 0) return err; if((err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_PERIODS, 8, 8)) < 0) return err; if((err = snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_BUFFER_BYTES, 2048, 2048)) < 0) return err; return 0; } int _snd_pcm_example_open(snd_pcm_t **pcmp, const char *name, snd_config_t *root, snd_config_t *conf, snd_pcm_stream_t stream, int mode) { struct example_pcm *sp_pcm; int err; fprintf(stderr, "ALSA-example: Opening\n"); sp_pcm = malloc(sizeof(*sp_pcm)); if(!sp_pcm) return -ENOMEM; memset(sp_pcm, 0, sizeof(*sp_pcm)); sp_pcm->io.version = SND_PCM_IOPLUG_VERSION; sp_pcm->io.name = "example Protocol Plugin"; sp_pcm->io.callback = &example_playback_callback; sp_pcm->io.private_data = sp_pcm; err = snd_pcm_ioplug_create(&sp_pcm->io, name, stream, mode); if(err){ free(sp_pcm); return err > 0 ? -err : err; } err = example_set_hw_params(&sp_pcm->io); if(err){ snd_pcm_ioplug_delete(&sp_pcm->io); free(sp_pcm); return err; } *pcmp = sp_pcm->io.pcm; return 0; } SND_PCM_PLUGIN_SYMBOL(example);