diff options
Diffstat (limited to 'tinyDAV/src/audio/alsa')
-rw-r--r-- | tinyDAV/src/audio/alsa/tdav_common_alsa.c | 275 | ||||
-rw-r--r-- | tinyDAV/src/audio/alsa/tdav_consumer_alsa.c | 288 | ||||
-rw-r--r-- | tinyDAV/src/audio/alsa/tdav_producer_alsa.c | 261 |
3 files changed, 824 insertions, 0 deletions
diff --git a/tinyDAV/src/audio/alsa/tdav_common_alsa.c b/tinyDAV/src/audio/alsa/tdav_common_alsa.c new file mode 100644 index 0000000..d1deec8 --- /dev/null +++ b/tinyDAV/src/audio/alsa/tdav_common_alsa.c @@ -0,0 +1,275 @@ +/* Copyright (C) 2014 Mamadou DIOP. +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +*/ +#include "tinydav/audio/alsa/tdav_common_alsa.h" + +#if HAVE_ALSA_ASOUNDLIB_H + +#define ALSA_DEBUG_INFO(FMT, ...) TSK_DEBUG_INFO("[ALSA Common] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_WARN(FMT, ...) TSK_DEBUG_WARN("[ALSA Common] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_ERROR(FMT, ...) TSK_DEBUG_ERROR("[ALSA Common] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_FATAL(FMT, ...) TSK_DEBUG_FATAL("[ALSA Common] " FMT, ##__VA_ARGS__) + +#define ALSA_PLAYBACK_PERIODS 6 + +int tdav_common_alsa_init(tdav_common_alsa_t* p_self) +{ + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + if (p_self->b_initialized) { + ALSA_DEBUG_WARN("Already initialized"); + return 0; + } + tsk_safeobj_init(p_self); + p_self->b_initialized = tsk_true; + return 0; +} + +int tdav_common_alsa_lock(tdav_common_alsa_t* p_self) +{ + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + return tsk_safeobj_lock(p_self); +} + +int tdav_common_alsa_unlock(tdav_common_alsa_t* p_self) +{ + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + return tsk_safeobj_unlock(p_self); +} + +int tdav_common_alsa_prepare(tdav_common_alsa_t* p_self, tsk_bool_t is_capture, int ptime, int channels, int sample_rate) +{ + int err = 0, val; + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(p_self); + + if (p_self->b_prepared) { + ALSA_DEBUG_WARN("Already prepared"); + goto bail; + } + if (!p_self->p_device_name) { + p_self->p_device_name = strdup("default"); + } + p_self->b_capture = is_capture; + + if ((err = snd_pcm_open(&p_self->p_handle, p_self->p_device_name, is_capture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, /*SND_PCM_NONBLOCK | SND_PCM_ASYNC*/0)) != 0) { + ALSA_DEBUG_ERROR("Failed to open audio device %s (%s)", p_self->p_device_name, snd_strerror(err)); + goto bail; + } + ALSA_DEBUG_INFO("device('%s') opened", p_self->p_device_name); + + if ((err = snd_pcm_hw_params_malloc(&p_self->p_params)) != 0) { + ALSA_DEBUG_ERROR("Failed to allocate hardware parameter structure(%s)", snd_strerror(err)); + goto bail; + } + + if ((err = snd_pcm_hw_params_any(p_self->p_handle, p_self->p_params)) < 0) { + ALSA_DEBUG_ERROR("Failed to initialize hardware parameter structure (device=%s, err=%s)", p_self->p_device_name, snd_strerror(err)); + goto bail; + } + + if ((err = snd_pcm_hw_params_set_access(p_self->p_handle, p_self->p_params, SND_PCM_ACCESS_RW_INTERLEAVED)) != 0) { + ALSA_DEBUG_ERROR("Failed to set access type (device=%s, err=%s)", p_self->p_device_name, snd_strerror(err)); + goto bail; + } + + if ((err = snd_pcm_hw_params_set_format(p_self->p_handle, p_self->p_params, SND_PCM_FORMAT_S16_LE)) != 0) { + ALSA_DEBUG_ERROR("Failed to set sample format (device=%s, err=%s)", p_self->p_device_name, snd_strerror(err)); + goto bail; + } + + val = sample_rate; + if ((err = snd_pcm_hw_params_set_rate_near(p_self->p_handle, p_self->p_params, &val, 0)) != 0) { + ALSA_DEBUG_ERROR("Failed to set sample rate (rate=%d, device=%s, err=%s)", p_self->sample_rate, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + ALSA_DEBUG_INFO("sample_rate: req=%d, resp=%d", sample_rate, val); + p_self->sample_rate = val; + + val = channels; + if ((err = snd_pcm_hw_params_set_channels_near(p_self->p_handle, p_self->p_params, &val)) != 0) { + ALSA_DEBUG_ERROR("Failed to set channels (channels=%d, device=%s, err=%s)", p_self->channels, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + ALSA_DEBUG_INFO("channels: req=%d, resp=%d", channels, val); + p_self->channels = val; + + if (!is_capture) { + unsigned int periods = ALSA_PLAYBACK_PERIODS; + snd_pcm_uframes_t periodSize = (ptime * p_self->sample_rate * p_self->channels) / 1000; + if ((err = snd_pcm_hw_params_set_periods_near(p_self->p_handle, p_self->p_params, &periods, 0)) != 0) { + ALSA_DEBUG_ERROR ("Failed to set periods (val=%u, device=%s, err=%s)", periods, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + + snd_pcm_uframes_t bufferSize = (periodSize * periods); + if ((err = snd_pcm_hw_params_set_buffer_size(p_self->p_handle, p_self->p_params, bufferSize)) != 0) { + ALSA_DEBUG_ERROR ("Failed to set buffer size (val=%lu, device=%s, err=%s)", bufferSize, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + ALSA_DEBUG_INFO("periods=%u, buffersize=%lu", periods, bufferSize); + } + + if ((err = snd_pcm_hw_params (p_self->p_handle, p_self->p_params)) != 0) { + ALSA_DEBUG_ERROR ("Failed to set parameters (channels=%d, rate=%d, device=%s, err=%s)", p_self->channels, p_self->sample_rate, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + if ((err = snd_pcm_prepare(p_self->p_handle)) != 0) { + ALSA_DEBUG_ERROR ("Failed to prepare device (channels=%d, rate=%d, device=%s, err=%s)", p_self->channels, p_self->sample_rate, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + + /*if (is_capture)*/ { + p_self->n_buff_size_in_bytes = (ptime * p_self->sample_rate * (2/*SND_PCM_FORMAT_S16_LE*/ * p_self->channels)) / 1000; + if (!(p_self->p_buff_ptr = tsk_realloc(p_self->p_buff_ptr, p_self->n_buff_size_in_bytes))) { + ALSA_DEBUG_ERROR("Failed to allocate buffer with size = %u", p_self->n_buff_size_in_bytes); + err = -4; + goto bail; + } + p_self->n_buff_size_in_samples = (p_self->n_buff_size_in_bytes >> 1/*SND_PCM_FORMAT_S16_LE*/); + ALSA_DEBUG_INFO("n_buff_size_in_bytes=%u", p_self->n_buff_size_in_bytes); + } + + ALSA_DEBUG_INFO("device('%s') prepared", p_self->p_device_name); + + // everything is OK + p_self->b_prepared = tsk_true; +bail: + if (err) { + tdav_common_alsa_unprepare(p_self); + } + tdav_common_alsa_unlock(p_self); + return err; + +} + +int tdav_common_alsa_unprepare(tdav_common_alsa_t* p_self) +{ + int err = 0; + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(p_self); + + if (p_self->b_started) { + ALSA_DEBUG_ERROR("Must stop the capture device before unpreparing"); + err = -2; + goto bail; + } + + if (p_self->p_params) { + snd_pcm_hw_params_free(p_self->p_params); + p_self->p_params = tsk_null; + } + if (p_self->p_handle) { + snd_pcm_close(p_self->p_handle); + p_self->p_handle = tsk_null; + } + p_self->b_prepared = tsk_false; + + ALSA_DEBUG_INFO("device('%s') unprepared", p_self->p_device_name); + +bail: + tdav_common_alsa_unlock(p_self); + return err; +} + +int tdav_common_alsa_start(tdav_common_alsa_t* p_self) +{ + int err = 0; + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(p_self); + + if (p_self->b_started) { + ALSA_DEBUG_WARN("Already started"); + err = - 3; + goto bail; + } + if (!p_self->b_prepared) { + ALSA_DEBUG_ERROR("Not prepared"); + err = -2; + goto bail; + } + + if ((err = snd_pcm_start(p_self->p_handle)) != 0) { + ALSA_DEBUG_ERROR ("Failed to start device (channels=%d, rate=%d, device=%s, err=%s)", p_self->channels, p_self->sample_rate, p_self->p_device_name, snd_strerror(err)); + goto bail; + } + + p_self->b_started = tsk_true; + ALSA_DEBUG_INFO("device('%s') started", p_self->p_device_name); +bail: + tdav_common_alsa_unlock(p_self); + return err; +} + +int tdav_common_alsa_stop(tdav_common_alsa_t* p_self) +{ + int err = 0; + if (!p_self) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(p_self); + + if (p_self->b_started) { + p_self->b_started = tsk_false; + //err = snd_pcm_drain(p_self->p_handle); + ALSA_DEBUG_INFO("device('%s') stopped", p_self->p_device_name); + } + if (p_self->b_prepared) { + tdav_common_alsa_unprepare(p_self); + } +bail: + tdav_common_alsa_unlock(p_self); + return err; +} + +int tdav_common_alsa_deinit(tdav_common_alsa_t* p_self) +{ + if (p_self && p_self->b_initialized) { + tdav_common_alsa_stop(p_self); + tdav_common_alsa_unprepare(p_self); + TSK_FREE(p_self->p_device_name); + TSK_FREE(p_self->p_buff_ptr); + tsk_safeobj_deinit(p_self); + p_self->b_initialized = tsk_false; + } + return 0; +} + +#endif /* HAVE_ALSA_ASOUNDLIB_H */ + diff --git a/tinyDAV/src/audio/alsa/tdav_consumer_alsa.c b/tinyDAV/src/audio/alsa/tdav_consumer_alsa.c new file mode 100644 index 0000000..65bfcd8 --- /dev/null +++ b/tinyDAV/src/audio/alsa/tdav_consumer_alsa.c @@ -0,0 +1,288 @@ +/* Copyright (C) 2014 Mamadou DIOP. +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +*/ +#include "tinydav/audio/alsa/tdav_consumer_alsa.h" + +#if HAVE_ALSA_ASOUNDLIB_H + +#include "tinydav/audio/alsa/tdav_common_alsa.h" + +#define ALSA_DEBUG_INFO(FMT, ...) TSK_DEBUG_INFO("[ALSA Consumer] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_WARN(FMT, ...) TSK_DEBUG_WARN("[ALSA Consumer] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_ERROR(FMT, ...) TSK_DEBUG_ERROR("[ALSA Consumer] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_FATAL(FMT, ...) TSK_DEBUG_FATAL("[ALSA Consumer] " FMT, ##__VA_ARGS__) + +typedef struct tdav_consumer_alsa_s +{ + TDAV_DECLARE_CONSUMER_AUDIO; + + tsk_bool_t b_muted; + tsk_bool_t b_started; + tsk_bool_t b_paused; + + tsk_thread_handle_t* tid[1]; + + struct tdav_common_alsa_s alsa_common; +} +tdav_consumer_alsa_t; + +static void* TSK_STDCALL _tdav_producer_alsa_playback_thread(void *param) +{ + tdav_consumer_alsa_t* p_alsa = (tdav_consumer_alsa_t*)param; + int err; + + ALSA_DEBUG_INFO("__playback_thread -- START"); + + tsk_thread_set_priority_2(TSK_THREAD_PRIORITY_TIME_CRITICAL); + + while (p_alsa->b_started) { + tdav_common_alsa_lock(&p_alsa->alsa_common); + //snd_pcm_wait(p_alsa->alsa_common.p_handle, 20); + //ALSA_DEBUG_INFO ("get (%d)", p_alsa->alsa_common.n_buff_size_in_bytes); + err = tdav_consumer_audio_get(TDAV_CONSUMER_AUDIO(p_alsa), p_alsa->alsa_common.p_buff_ptr, p_alsa->alsa_common.n_buff_size_in_bytes); // requires 16bits, thread-safe + //ALSA_DEBUG_INFO ("get returned %d", err); + if (err < p_alsa->alsa_common.n_buff_size_in_bytes) { + memset(((uint8_t*)p_alsa->alsa_common.p_buff_ptr) + err, 0, (p_alsa->alsa_common.n_buff_size_in_bytes - err)); + + } + if ((err = snd_pcm_writei(p_alsa->alsa_common.p_handle, p_alsa->alsa_common.p_buff_ptr, p_alsa->alsa_common.n_buff_size_in_samples)) != p_alsa->alsa_common.n_buff_size_in_samples) { + if (err == -EPIPE) { // pipe broken + err = snd_pcm_recover(p_alsa->alsa_common.p_handle, err, 0); + if (err == 0) { + ALSA_DEBUG_INFO ("recovered"); + goto next; + } + } + ALSA_DEBUG_ERROR ("Failed to read data from audio interface failed (%d->%s)", err, snd_strerror(err)); + tdav_common_alsa_unlock(&p_alsa->alsa_common); + goto bail; + } + tdav_consumer_audio_tick(TDAV_CONSUMER_AUDIO(p_alsa)); +next: + tdav_common_alsa_unlock(&p_alsa->alsa_common); + } +bail: + ALSA_DEBUG_INFO("__playback_thread -- STOP"); + return tsk_null; +} + + +/* ============ Media Consumer Interface ================= */ +static int tdav_consumer_alsa_set(tmedia_consumer_t* self, const tmedia_param_t* param) +{ + tdav_consumer_alsa_t* p_alsa = (tdav_consumer_alsa_t*)self; + int ret = 0; + + ret = tdav_consumer_audio_set(TDAV_CONSUMER_AUDIO(self), param); + + return ret; +} + +static int tdav_consumer_alsa_prepare(tmedia_consumer_t* self, const tmedia_codec_t* codec) +{ + tdav_consumer_alsa_t* p_alsa = (tdav_consumer_alsa_t*)self; + int err = 0; + ALSA_DEBUG_INFO("******* tdav_consumer_alsa_prepare ******"); + + if (! p_alsa || !codec && codec->plugin) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(&p_alsa->alsa_common); + + // Set using requested + TMEDIA_CONSUMER(p_alsa)->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_DECODING(codec); + TMEDIA_CONSUMER(p_alsa)->audio.in.channels = TMEDIA_CODEC_CHANNELS_AUDIO_DECODING(codec); + TMEDIA_CONSUMER(p_alsa)->audio.in.rate = TMEDIA_CODEC_RATE_DECODING(codec); + + // Prepare + err = tdav_common_alsa_prepare(&p_alsa->alsa_common, tsk_false/*is_record*/, TMEDIA_CONSUMER( p_alsa)->audio.ptime, TMEDIA_CONSUMER( p_alsa)->audio.in.channels, TMEDIA_CONSUMER( p_alsa)->audio.in.rate); + if (err) { + goto bail; + } + + ALSA_DEBUG_INFO("prepared: req_channels=%d; req_rate=%d, resp_channels=%d; resp_rate=%d", + TMEDIA_CONSUMER(p_alsa)->audio.in.channels, TMEDIA_CONSUMER(p_alsa)->audio.in.rate, + p_alsa->alsa_common.channels, p_alsa->alsa_common.sample_rate); + + // Set using supported (up to the resampler to convert to requested) + TMEDIA_CONSUMER(p_alsa)->audio.out.channels = p_alsa->alsa_common.channels; + TMEDIA_CONSUMER(p_alsa)->audio.out.rate = p_alsa->alsa_common.sample_rate; + +bail: + tdav_common_alsa_unlock(&p_alsa->alsa_common); + return err; +} + +static int tdav_consumer_alsa_start(tmedia_consumer_t* self) +{ + tdav_consumer_alsa_t* p_alsa = (tdav_consumer_alsa_t*)self; + int err = 0; + + ALSA_DEBUG_INFO("******* tdav_consumer_alsa_start ******"); + + if (!p_alsa) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(&p_alsa->alsa_common); + + if (p_alsa->b_started) { + ALSA_DEBUG_WARN("Already started"); + goto bail; + } + + /* start device */ + err = tdav_common_alsa_start(&p_alsa->alsa_common); + if (err) { + goto bail; + } + + /* start thread */ + p_alsa->b_started = tsk_true; + tsk_thread_create(&p_alsa->tid[0], _tdav_producer_alsa_playback_thread, p_alsa); + + ALSA_DEBUG_INFO("started"); + +bail: + tdav_common_alsa_unlock(&p_alsa->alsa_common); + return err; +} + +static int tdav_consumer_alsa_consume(tmedia_consumer_t* self, const void* buffer, tsk_size_t size, const tsk_object_t* proto_hdr) +{ + int err = 0; + tdav_consumer_alsa_t* p_alsa = (tdav_consumer_alsa_t*)self; + + if (!p_alsa || !buffer || !size) { + ALSA_DEBUG_ERROR("Invalid paramter"); + return -1; + } + + //tdav_common_alsa_lock(&p_alsa->alsa_common); + + if (!p_alsa->b_started) { + ALSA_DEBUG_WARN("Not started"); + err = -2; + goto bail; + } + + if ((err = tdav_consumer_audio_put(TDAV_CONSUMER_AUDIO(p_alsa), buffer, size, proto_hdr))) {//thread-safe + ALSA_DEBUG_WARN("Failed to put audio data to the jitter buffer"); + goto bail; + } + +bail: + //tdav_common_alsa_unlock(&p_alsa->alsa_common); + return err; +} + +static int tdav_consumer_alsa_pause(tmedia_consumer_t* self) +{ + return 0; +} + +static int tdav_consumer_alsa_stop(tmedia_consumer_t* self) +{ + tdav_consumer_alsa_t* p_alsa = (tdav_consumer_alsa_t*)self; + int err; + + if (!p_alsa) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + /* should be done here */ + p_alsa->b_started = tsk_false; + + err = tdav_common_alsa_stop(&p_alsa->alsa_common); + + /* stop thread */ + if (p_alsa->tid[0]) { + tsk_thread_join(&(p_alsa->tid[0])); + } + + ALSA_DEBUG_INFO("stopped"); + + return 0; +} + + +// +// ALSA consumer object definition +// +/* constructor */ +static tsk_object_t* tdav_consumer_alsa_ctor(tsk_object_t * self, va_list * app) +{ + tdav_consumer_alsa_t *p_alsa = self; + if (p_alsa) { + ALSA_DEBUG_INFO("create"); + /* init base */ + tdav_consumer_audio_init(TDAV_CONSUMER_AUDIO(p_alsa)); + /* init self */ + tdav_common_alsa_init(&p_alsa->alsa_common); + } + return self; +} +/* destructor */ +static tsk_object_t* tdav_consumer_alsa_dtor(tsk_object_t * self) +{ + tdav_consumer_alsa_t *p_alsa = self; + if (p_alsa) { + /* stop */ + if (p_alsa->b_started) { + tdav_consumer_alsa_stop((tmedia_consumer_t*)p_alsa); + } + + /* deinit base */ + tdav_consumer_audio_deinit(TDAV_CONSUMER_AUDIO(p_alsa)); + /* deinit self */ + tdav_common_alsa_deinit(&p_alsa->alsa_common); + + ALSA_DEBUG_INFO("*** destroyed ***"); + } + + return self; +} +/* object definition */ +static const tsk_object_def_t tdav_consumer_alsa_def_s = +{ + sizeof(tdav_consumer_alsa_t), + tdav_consumer_alsa_ctor, + tdav_consumer_alsa_dtor, + tdav_consumer_audio_cmp, +}; +/* plugin definition*/ +static const tmedia_consumer_plugin_def_t tdav_consumer_alsa_plugin_def_s = +{ + &tdav_consumer_alsa_def_s, + + tmedia_audio, + "Linux ALSA consumer", + + tdav_consumer_alsa_set, + tdav_consumer_alsa_prepare, + tdav_consumer_alsa_start, + tdav_consumer_alsa_consume, + tdav_consumer_alsa_pause, + tdav_consumer_alsa_stop +}; +const tmedia_consumer_plugin_def_t *tdav_consumer_alsa_plugin_def_t = &tdav_consumer_alsa_plugin_def_s; + +#endif /* #if HAVE_ALSA_ASOUNDLIB_H */ diff --git a/tinyDAV/src/audio/alsa/tdav_producer_alsa.c b/tinyDAV/src/audio/alsa/tdav_producer_alsa.c new file mode 100644 index 0000000..d5c4021 --- /dev/null +++ b/tinyDAV/src/audio/alsa/tdav_producer_alsa.c @@ -0,0 +1,261 @@ +/* Copyright (C) 2014 Mamadou DIOP. +* +* This file is part of Open Source Doubango Framework. +* +* DOUBANGO is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* DOUBANGO is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with DOUBANGO. +*/ +#include "tinydav/audio/alsa/tdav_producer_alsa.h" + +#if HAVE_ALSA_ASOUNDLIB_H + +#include "tinydav/audio/alsa/tdav_common_alsa.h" + +#define ALSA_DEBUG_INFO(FMT, ...) TSK_DEBUG_INFO("[ALSA Producer] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_WARN(FMT, ...) TSK_DEBUG_WARN("[ALSA Producer] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_ERROR(FMT, ...) TSK_DEBUG_ERROR("[ALSA Producer] " FMT, ##__VA_ARGS__) +#define ALSA_DEBUG_FATAL(FMT, ...) TSK_DEBUG_FATAL("[ALSA Producer] " FMT, ##__VA_ARGS__) + +typedef struct tdav_producer_alsa_s +{ + TDAV_DECLARE_PRODUCER_AUDIO; + + tsk_bool_t b_muted; + tsk_bool_t b_started; + tsk_bool_t b_paused; + + tsk_thread_handle_t* tid[1]; + + struct tdav_common_alsa_s alsa_common; +} +tdav_producer_alsa_t; + +static void* TSK_STDCALL _tdav_producer_alsa_record_thread(void *param) +{ + tdav_producer_alsa_t* p_alsa = (tdav_producer_alsa_t*)param; + int err; + + ALSA_DEBUG_INFO("__record_thread -- START"); + + tsk_thread_set_priority_2(TSK_THREAD_PRIORITY_TIME_CRITICAL); + + while (p_alsa->b_started) { + tdav_common_alsa_lock(&p_alsa->alsa_common); + if ((err = snd_pcm_readi(p_alsa->alsa_common.p_handle, p_alsa->alsa_common.p_buff_ptr, p_alsa->alsa_common.n_buff_size_in_samples)) != p_alsa->alsa_common.n_buff_size_in_samples) { + ALSA_DEBUG_ERROR ("Failed to read data from audio interface failed (%d->%s)", err, snd_strerror(err)); + tdav_common_alsa_unlock(&p_alsa->alsa_common); + goto bail; + } + if (!p_alsa->b_muted && TMEDIA_PRODUCER(p_alsa)->enc_cb.callback) { + TMEDIA_PRODUCER(p_alsa)->enc_cb.callback(TMEDIA_PRODUCER(p_alsa)->enc_cb.callback_data, p_alsa->alsa_common.p_buff_ptr, p_alsa->alsa_common.n_buff_size_in_bytes); + } + tdav_common_alsa_unlock(&p_alsa->alsa_common); + } +bail: + ALSA_DEBUG_INFO("__record_thread -- STOP"); + return tsk_null; +} + + +/* ============ Media Producer Interface ================= */ +static int tdav_producer_alsa_set(tmedia_producer_t* self, const tmedia_param_t* param) +{ + tdav_producer_alsa_t* p_alsa = (tdav_producer_alsa_t*)self; + if (param->plugin_type == tmedia_ppt_producer) { + if (param->value_type == tmedia_pvt_int32) { + if (tsk_striequals(param->key, "volume")) { + return 0; + } + else if(tsk_striequals(param->key, "mute")){ + p_alsa->b_muted = (TSK_TO_INT32((uint8_t*)param->value) != 0); + return 0; + } + } + } + return tdav_producer_audio_set(TDAV_PRODUCER_AUDIO(self), param); +} + +static int tdav_producer_alsa_prepare(tmedia_producer_t* self, const tmedia_codec_t* codec) +{ + tdav_producer_alsa_t* p_alsa = (tdav_producer_alsa_t*)self; + int err = 0; + ALSA_DEBUG_INFO("******* tdav_producer_alsa_prepare ******"); + + if (! p_alsa || !codec && codec->plugin) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(&p_alsa->alsa_common); + + // Set using requested + TMEDIA_PRODUCER( p_alsa)->audio.channels = TMEDIA_CODEC_CHANNELS_AUDIO_ENCODING(codec); + TMEDIA_PRODUCER( p_alsa)->audio.rate = TMEDIA_CODEC_RATE_ENCODING(codec); + TMEDIA_PRODUCER( p_alsa)->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_ENCODING(codec); + + // Prepare + err = tdav_common_alsa_prepare(&p_alsa->alsa_common, tsk_true/*is_capture*/, TMEDIA_PRODUCER( p_alsa)->audio.ptime, TMEDIA_PRODUCER( p_alsa)->audio.channels, TMEDIA_PRODUCER( p_alsa)->audio.rate); + if (err) { + goto bail; + } + + ALSA_DEBUG_INFO("prepared: req_channels=%d; req_rate=%d, resp_channels=%d; resp_rate=%d", + TMEDIA_PRODUCER(p_alsa)->audio.channels, TMEDIA_PRODUCER(p_alsa)->audio.rate, + p_alsa->alsa_common.channels, p_alsa->alsa_common.sample_rate); + + // Set using supported (up to the resampler to convert to requested) + TMEDIA_PRODUCER(p_alsa)->audio.channels = p_alsa->alsa_common.channels; + TMEDIA_PRODUCER(p_alsa)->audio.rate = p_alsa->alsa_common.sample_rate; + +bail: + tdav_common_alsa_unlock(&p_alsa->alsa_common); + return err; +} + +static int tdav_producer_alsa_start(tmedia_producer_t* self) +{ + tdav_producer_alsa_t* p_alsa = (tdav_producer_alsa_t*)self; + int err = 0; + + ALSA_DEBUG_INFO("******* tdav_producer_alsa_start ******"); + + if (!p_alsa) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + tdav_common_alsa_lock(&p_alsa->alsa_common); + + if (p_alsa->b_started) { + ALSA_DEBUG_WARN("Already started"); + goto bail; + } + + /* start device */ + err = tdav_common_alsa_start(&p_alsa->alsa_common); + if (err) { + goto bail; + } + + /* start thread */ + p_alsa->b_started = tsk_true; + tsk_thread_create(&p_alsa->tid[0], _tdav_producer_alsa_record_thread, p_alsa); + + ALSA_DEBUG_INFO("started"); + +bail: + tdav_common_alsa_unlock(&p_alsa->alsa_common); + return err; +} + +static int tdav_producer_alsa_pause(tmedia_producer_t* self) +{ + tdav_producer_alsa_t* p_alsa = (tdav_producer_alsa_t*)self; + + if (!p_alsa) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + ALSA_DEBUG_INFO("paused"); + + return 0; +} + +static int tdav_producer_alsa_stop(tmedia_producer_t* self) +{ + tdav_producer_alsa_t* p_alsa = (tdav_producer_alsa_t*)self; + int err; + + if (!p_alsa) { + ALSA_DEBUG_ERROR("Invalid parameter"); + return -1; + } + + /* should be done here */ + p_alsa->b_started = tsk_false; + + err = tdav_common_alsa_stop(&p_alsa->alsa_common); + + /* stop thread */ + if (p_alsa->tid[0]) { + tsk_thread_join(&(p_alsa->tid[0])); + } + + ALSA_DEBUG_INFO("stopped"); + + return 0; +} + + +// +// ALSA producer object definition +// +/* constructor */ +static tsk_object_t* tdav_producer_alsa_ctor(tsk_object_t * self, va_list * app) +{ + tdav_producer_alsa_t *p_alsa = (tdav_producer_alsa_t*)self; + if (p_alsa) { + ALSA_DEBUG_INFO("create"); + /* init base */ + tdav_producer_audio_init(TDAV_PRODUCER_AUDIO(p_alsa)); + /* init self */ + tdav_common_alsa_init(&p_alsa->alsa_common); + } + return self; +} +/* destructor */ +static tsk_object_t* tdav_producer_alsa_dtor(tsk_object_t * self) +{ + tdav_producer_alsa_t *p_alsa = (tdav_producer_alsa_t *)self; + if (p_alsa) { + /* stop */ + if (p_alsa->b_started) { + tdav_producer_alsa_stop((tmedia_producer_t*)p_alsa); + } + + /* deinit base */ + tdav_producer_audio_deinit(TDAV_PRODUCER_AUDIO(p_alsa)); + /* deinit self */ + tdav_common_alsa_deinit(&p_alsa->alsa_common); + + ALSA_DEBUG_INFO("*** destroyed ***"); + } + + return self; +} +/* object definition */ +static const tsk_object_def_t tdav_producer_alsa_def_s = +{ + sizeof(tdav_producer_alsa_t), + tdav_producer_alsa_ctor, + tdav_producer_alsa_dtor, + tdav_producer_audio_cmp, +}; +/* plugin definition*/ +static const tmedia_producer_plugin_def_t tdav_producer_alsa_plugin_def_s = +{ + &tdav_producer_alsa_def_s, + + tmedia_audio, + "Linux ALSA producer", + + tdav_producer_alsa_set, + tdav_producer_alsa_prepare, + tdav_producer_alsa_start, + tdav_producer_alsa_pause, + tdav_producer_alsa_stop +}; +const tmedia_producer_plugin_def_t *tdav_producer_alsa_plugin_def_t = &tdav_producer_alsa_plugin_def_s; + +#endif /* #if HAVE_ALSA_ASOUNDLIB_H */ |