From 3bce4a6a8648c91b6ddea478116b3b0679ef4648 Mon Sep 17 00:00:00 2001 From: Manuel Bachmann Date: Thu, 17 Dec 2015 10:54:24 +0100 Subject: Final modifications for Radio-Audio API communication Signed-off-by: Manuel Bachmann --- plugins/audio/audio-alsa.c | 100 ++++++++++++++++++++++--------------------- plugins/audio/audio-alsa.h | 6 +-- plugins/audio/audio-api.c | 64 +++++++++++++-------------- plugins/audio/audio-api.h | 4 +- plugins/radio/radio-api.c | 2 +- plugins/radio/radio-api.h | 3 ++ plugins/radio/radio-rtlsdr.c | 17 +++++++- 7 files changed, 107 insertions(+), 89 deletions(-) (limited to 'plugins') diff --git a/plugins/audio/audio-alsa.c b/plugins/audio/audio-alsa.c index c13cdc59..813d8167 100644 --- a/plugins/audio/audio-alsa.c +++ b/plugins/audio/audio-alsa.c @@ -58,34 +58,36 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { snd_mixer_selem_id_set_name (mixer_sid, "Master"); mixer_elm = snd_mixer_find_selem (mixer, mixer_sid); - snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max); - snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol); + if (mixer_elm) { + snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max); + snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol); + } /* allocate the global array if it hasn't been done */ - if (!dev_ctx) { - dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T)); - dev_ctx[0] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T)); - dev_ctx[0]->name = NULL; - dev_ctx[0]->dev = NULL; + if (!adev_ctx) { + adev_ctx = (adev_ctx_T**) malloc (sizeof(adev_ctx_T)); + adev_ctx[0] = (adev_ctx_T*) malloc (sizeof(adev_ctx_T)); + adev_ctx[0]->name = NULL; + adev_ctx[0]->dev = NULL; } /* is a card with similar name already opened ? */ - for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) { - if (dev_ctx[num]->name && - !strcmp (dev_ctx[num]->name, name)) + for (num = 0; num < (sizeof(adev_ctx)/sizeof(adev_ctx_T)); num++) { + if (adev_ctx[num]->name && + !strcmp (adev_ctx[num]->name, name)) return 0; } num++; /* it's not... let us add it to the global array */ - dev_ctx = (dev_ctx_T**) realloc (dev_ctx, (num+1)*sizeof(dev_ctx_T)); - dev_ctx[num] = (dev_ctx_T*) malloc (sizeof(dev_ctx_T)); - dev_ctx[num]->name = strdup (name); - dev_ctx[num]->dev = dev; - dev_ctx[num]->params = params; - dev_ctx[num]->mixer_elm = mixer_elm; - dev_ctx[num]->vol_max = vol_max; - dev_ctx[num]->vol = vol; + adev_ctx = (adev_ctx_T**) realloc (adev_ctx, (num+1)*sizeof(adev_ctx_T)); + adev_ctx[num] = (adev_ctx_T*) malloc (sizeof(adev_ctx_T)); + adev_ctx[num]->name = strdup (name); + adev_ctx[num]->dev = dev; + adev_ctx[num]->params = params; + adev_ctx[num]->mixer_elm = mixer_elm; + adev_ctx[num]->vol_max = vol_max; + adev_ctx[num]->vol = vol; /* make the client context aware of current card state */ ctx->volume = _alsa_get_volume (num); @@ -99,15 +101,15 @@ PUBLIC void _alsa_free (const char *name) { int num; - for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) { - if (dev_ctx[num]->name && - !strcmp (dev_ctx[num]->name, name)) { - snd_pcm_close (dev_ctx[num]->dev); - snd_pcm_hw_params_free (dev_ctx[num]->params); - free (dev_ctx[num]->name); - dev_ctx[num]->name = NULL; - dev_ctx[num]->dev = NULL; - free(dev_ctx[num]); + for (num = 0; num < (sizeof(adev_ctx)/sizeof(adev_ctx_T)); num++) { + if (adev_ctx[num]->name && + !strcmp (adev_ctx[num]->name, name)) { + snd_pcm_close (adev_ctx[num]->dev); + snd_pcm_hw_params_free (adev_ctx[num]->params); + free (adev_ctx[num]->name); + adev_ctx[num]->name = NULL; + adev_ctx[num]->dev = NULL; + free(adev_ctx[num]); return; } } @@ -115,48 +117,48 @@ PUBLIC void _alsa_free (const char *name) { PUBLIC void _alsa_play (unsigned int num, void *buf, int len) { - if (!dev_ctx || !dev_ctx[num]) + if (!adev_ctx || !adev_ctx[num]) { return; - + } int16_t *cbuf = (int16_t *)buf; int frames = len / 2; int res; - if ((res = snd_pcm_writei (dev_ctx[num]->dev, cbuf, frames)) != frames) { - snd_pcm_recover (dev_ctx[num]->dev, res, 0); - snd_pcm_prepare (dev_ctx[num]->dev); + if ((res = snd_pcm_writei (adev_ctx[num]->dev, cbuf, frames)) != frames) { + snd_pcm_recover (adev_ctx[num]->dev, res, 0); + snd_pcm_prepare (adev_ctx[num]->dev); } - /* snd_pcm_drain (dev_ctx[num]->dev); */ + /* snd_pcm_drain (adev_ctx[num]->dev); */ } PUBLIC unsigned int _alsa_get_volume (unsigned int num) { - if (!dev_ctx || !dev_ctx[num]) + if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm) return; - snd_mixer_selem_get_playback_volume (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &dev_ctx[num]->vol); + snd_mixer_selem_get_playback_volume (adev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &adev_ctx[num]->vol); - return (unsigned int)(dev_ctx[num]->vol*100)/dev_ctx[num]->vol_max; + return (unsigned int)(adev_ctx[num]->vol*100)/adev_ctx[num]->vol_max; } PUBLIC unsigned int _alsa_set_volume (unsigned int num, unsigned int vol) { - if (!dev_ctx || !dev_ctx[num] || vol > 100) + if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm || vol > 100) return; - snd_mixer_selem_set_playback_volume_all (dev_ctx[num]->mixer_elm, (vol*dev_ctx[num]->vol_max)/100); + snd_mixer_selem_set_playback_volume_all (adev_ctx[num]->mixer_elm, (vol*adev_ctx[num]->vol_max)/100); } PUBLIC unsigned char _alsa_get_mute (unsigned int num) { int mute = 0; - if (!dev_ctx || !dev_ctx[num]) + if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm) return; - if (snd_mixer_selem_has_playback_switch (dev_ctx[num]->mixer_elm)) { - snd_mixer_selem_get_playback_switch (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &mute); - snd_mixer_selem_get_playback_switch (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_RIGHT, &mute); + if (snd_mixer_selem_has_playback_switch (adev_ctx[num]->mixer_elm)) { + snd_mixer_selem_get_playback_switch (adev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &mute); + snd_mixer_selem_get_playback_switch (adev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_RIGHT, &mute); } @@ -165,25 +167,25 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) { PUBLIC void _alsa_set_mute (unsigned int num, unsigned char mute) { - if (!dev_ctx || !dev_ctx[num] || 1 < mute < 0) + if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm || 1 < mute < 0) return; - if (snd_mixer_selem_has_playback_switch (dev_ctx[num]->mixer_elm)) - snd_mixer_selem_set_playback_switch_all (dev_ctx[num]->mixer_elm, !mute); + if (snd_mixer_selem_has_playback_switch (adev_ctx[num]->mixer_elm)) + snd_mixer_selem_set_playback_switch_all (adev_ctx[num]->mixer_elm, !mute); } PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) { - if (!dev_ctx || !dev_ctx[num]) + if (!adev_ctx || !adev_ctx[num]) return; - snd_pcm_hw_params_set_rate_near (dev_ctx[num]->dev, dev_ctx[num]->params, &rate, 0); + snd_pcm_hw_params_set_rate_near (adev_ctx[num]->dev, adev_ctx[num]->params, &rate, 0); } PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) { - if (!dev_ctx || !dev_ctx[num]) + if (!adev_ctx || !adev_ctx[num]) return; - snd_pcm_hw_params_set_channels (dev_ctx[num]->dev, dev_ctx[num]->params, channels); + snd_pcm_hw_params_set_channels (adev_ctx[num]->dev, adev_ctx[num]->params, channels); } diff --git a/plugins/audio/audio-alsa.h b/plugins/audio/audio-alsa.h index 9a4b0b58..776ac819 100644 --- a/plugins/audio/audio-alsa.h +++ b/plugins/audio/audio-alsa.h @@ -23,9 +23,9 @@ #include "local-def.h" -typedef struct dev_ctx dev_ctx_T; +typedef struct adev_ctx adev_ctx_T; -struct dev_ctx { +struct adev_ctx { char *name; snd_pcm_t *dev; snd_pcm_hw_params_t *params; @@ -37,6 +37,6 @@ struct dev_ctx { PUBLIC unsigned int _alsa_get_volume (unsigned int); PUBLIC unsigned char _alsa_get_mute (unsigned int); -static struct dev_ctx **dev_ctx = NULL; +static struct adev_ctx **adev_ctx = NULL; #endif /* AUDIO_ALSA_H */ diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c index fd5f620f..f7897682 100644 --- a/plugins/audio/audio-api.c +++ b/plugins/audio/audio-api.c @@ -25,24 +25,24 @@ /* private client context creation ; default values */ STATIC audioCtxHandleT* initAudioCtx () { - audioCtxHandleT *ctx; + audioCtxHandleT *actx; - ctx = malloc (sizeof(audioCtxHandleT)); - ctx->idx = -1; - ctx->volume = 25; - ctx->channels = 2; - ctx->mute = 0; + actx = malloc (sizeof(audioCtxHandleT)); + actx->idx = -1; + actx->volume = 25; + actx->channels = 2; + actx->mute = 0; - return ctx; + return actx; } -STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) { +STATIC AFB_error releaseAudio (audioCtxHandleT *actx) { /* power it off */ - _alsa_free (ctx->idx); + _alsa_free (actx->idx); /* clean client context */ - ctx->idx = -1; + actx->idx = -1; return AFB_SUCCESS; } @@ -61,15 +61,15 @@ STATIC json_object* freeAudio (AFB_clientCtx *client) { STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */ - audioCtxHandleT *ctx; + audioCtxHandleT *actx; json_object *jresp; int idx; /* create a private client context */ - ctx = initAudioCtx(); - request->client->ctx = (audioCtxHandleT*)ctx; + actx = initAudioCtx(); + request->client->ctx = (audioCtxHandleT*)actx; - _alsa_init("default", ctx); + _alsa_init("default", actx); jresp = json_object_new_object(); json_object_object_add (jresp, "token", json_object_new_string (request->client->token)); @@ -78,7 +78,7 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */ - audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx; + audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx; const char *value = getQueryValue (request, "value"); json_object *jresp; int volume; @@ -86,8 +86,8 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */ /* no "?value=" parameter : return current state */ if (!value) { - ctx->volume = _alsa_get_volume (ctx->idx); - snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume); + actx->volume = _alsa_get_volume (actx->idx); + snprintf (volume_str, sizeof(volume_str), "%d", actx->volume); jresp = json_object_new_object(); json_object_object_add (jresp, "volume", json_object_new_string(volume_str)); } @@ -99,10 +99,10 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */ request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE; return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100")); } - ctx->volume = volume; - _alsa_set_volume (ctx->idx, ctx->volume); + actx->volume = volume; + _alsa_set_volume (actx->idx, actx->volume); - snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume); + snprintf (volume_str, sizeof(volume_str), "%d", actx->volume); jresp = json_object_new_object(); json_object_object_add (jresp, "volume", json_object_new_string(volume_str)); } @@ -112,23 +112,23 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */ STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */ - audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx; + audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx; const char *value = getQueryValue (request, "value"); json_object *jresp = json_object_new_object(); char channels_str[256]; /* no "?value=" parameter : return current state */ if (!value) { - snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels); + snprintf (channels_str, sizeof(channels_str), "%d", actx->channels); json_object_object_add (jresp, "channels", json_object_new_string (channels_str)); } /* "?value=" parameter, set channels */ else { - ctx->channels = atoi (value); - _alsa_set_channels (ctx->idx, ctx->channels); + actx->channels = atoi (value); + _alsa_set_channels (actx->idx, actx->channels); - snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels); + snprintf (channels_str, sizeof(channels_str), "%d", actx->channels); json_object_object_add (jresp, "channels", json_object_new_string (channels_str)); } @@ -137,30 +137,30 @@ STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */ STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */ - audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx; + audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx; const char *value = getQueryValue (request, "value"); json_object *jresp = json_object_new_object(); /* no "?value=" parameter : return current state */ if (!value) { - ctx->mute = _alsa_get_mute (ctx->idx); - ctx->mute ? + actx->mute = _alsa_get_mute (actx->idx); + actx->mute ? json_object_object_add (jresp, "mute", json_object_new_string ("on")) : json_object_object_add (jresp, "mute", json_object_new_string ("off")); } /* "?value=" parameter is "1" or "on" */ else if ( atoi(value) == 1 || !strcasecmp(value, "on") ) { - ctx->mute = 1; - _alsa_set_mute (ctx->idx, ctx->mute); + actx->mute = 1; + _alsa_set_mute (actx->idx, actx->mute); json_object_object_add (jresp, "mute", json_object_new_string ("on")); } /* "?value=" parameter is "0" or "off" */ else if ( atoi(value) == 0 || !strcasecmp(value, "off") ) { - ctx->mute = 0; - _alsa_set_mute (ctx->idx, ctx->mute); + actx->mute = 0; + _alsa_set_mute (actx->idx, actx->mute); json_object_object_add (jresp, "mute", json_object_new_string ("off")); } diff --git a/plugins/audio/audio-api.h b/plugins/audio/audio-api.h index 8acb1bd8..1cf54fc0 100644 --- a/plugins/audio/audio-api.h +++ b/plugins/audio/audio-api.h @@ -22,9 +22,9 @@ #include "audio-alsa.h" /* global plugin handle, should store everything we may need */ -typedef struct { +/*typedef struct { int devCount; -} pluginHandleT; +} pluginHandleT;*/ /* structure holding one audio card with current usage status */ typedef struct { diff --git a/plugins/radio/radio-api.c b/plugins/radio/radio-api.c index f065a8ad..8754b63c 100644 --- a/plugins/radio/radio-api.c +++ b/plugins/radio/radio-api.c @@ -298,7 +298,7 @@ STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */ /* radio stop */ ctx->is_playing = 0; _radio_stop (ctx->idx); - json_object_object_add (jresp, "play-on", json_object_new_string ("off")); + json_object_object_add (jresp, "play", json_object_new_string ("off")); } return jresp; diff --git a/plugins/radio/radio-api.h b/plugins/radio/radio-api.h index 4ea41b1c..0dbd3422 100644 --- a/plugins/radio/radio-api.h +++ b/plugins/radio/radio-api.h @@ -21,6 +21,9 @@ #include "radio-rtlsdr.h" +#include "../audio/audio-api.h" +#include "../audio/audio-alsa.h" + /* -------------- PLUGIN DEFINITIONS ----------------- */ #define MAX_RADIO 10 diff --git a/plugins/radio/radio-rtlsdr.c b/plugins/radio/radio-rtlsdr.c index e90bda08..e5a609e3 100644 --- a/plugins/radio/radio-rtlsdr.c +++ b/plugins/radio/radio-rtlsdr.c @@ -19,6 +19,8 @@ #include "radio-api.h" #include "radio-rtlsdr.h" +static audioCtxHandleT *actx = NULL; + /* ------------- RADIO RTLSDR IMPLEMENTATION ---------------- */ /* --- PUBLIC FUNCTIONS --- */ @@ -46,6 +48,13 @@ PUBLIC unsigned char _radio_on (unsigned int num, radioCtxHandleT *ctx) { dev_ctx[num]->output = NULL; _radio_dev_init(dev_ctx[num], num); + actx = malloc (sizeof(audioCtxHandleT)); + actx->idx = -1; + actx->volume = 25; + actx->channels = 2; + actx->mute = 0; + _alsa_init ("default", actx); + return 1; } @@ -58,7 +67,11 @@ PUBLIC void _radio_off (unsigned int num) { _radio_dev_free(dev_ctx[num]); free(dev_ctx[num]); } + /* free(dev_ctx); */ + + _alsa_free ("default"); + free (actx); } PUBLIC void _radio_set_mode (unsigned int num, Mode mode) { @@ -398,8 +411,8 @@ STATIC void* _output_thread_fn (void *ctx) { while (dev_ctx->should_run) { pthread_wait(&output->ok, &output->ok_m); pthread_rwlock_rdlock(&output->lck); - //if (!dev_ctx->mute) - // mRadio->PlayAlsa((void*)&output->buf, output->buf_len); + if (!dev_ctx->mute) + _alsa_play(actx->idx, (void*)&output->buf, output->buf_len); pthread_rwlock_unlock(&output->lck); } -- cgit 1.2.3-korg