summaryrefslogtreecommitdiffstats
path: root/plugins/audio
diff options
context:
space:
mode:
authorManuel Bachmann <manuel.bachmann@iot.bzh>2015-12-21 04:21:51 +0100
committerManuel Bachmann <manuel.bachmann@iot.bzh>2015-12-21 04:25:23 +0100
commit03bebc12f0fd5006a72e430084146a36d2db7c8d (patch)
treec9dc5d4e03099c802bf3cc4bf08a4658665b5408 /plugins/audio
parent6f22e88cd3e6d502efa7100ad2b129641305fb53 (diff)
Untie Radio and Audio APIs
Radio and Audio API functions do not cross-call themselves directly anymore ; this is necessary to have independent plugin binaries (.so) in the near future. (PS : audio buffer securization is WIP) Signed-off-by: Manuel Bachmann <manuel.bachmann@iot.bzh>
Diffstat (limited to 'plugins/audio')
-rw-r--r--plugins/audio/audio-alsa.c150
-rw-r--r--plugins/audio/audio-alsa.h11
-rw-r--r--plugins/audio/audio-api.c110
-rw-r--r--plugins/audio/audio-api.h15
4 files changed, 186 insertions, 100 deletions
diff --git a/plugins/audio/audio-alsa.c b/plugins/audio/audio-alsa.c
index 813d8167..4ee48045 100644
--- a/plugins/audio/audio-alsa.c
+++ b/plugins/audio/audio-alsa.c
@@ -64,30 +64,32 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
}
/* allocate the global array if it hasn't been done */
- 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;
+ 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;
}
/* is a card with similar name already opened ? */
- for (num = 0; num < (sizeof(adev_ctx)/sizeof(adev_ctx_T)); num++) {
- if (adev_ctx[num]->name &&
- !strcmp (adev_ctx[num]->name, name))
+ for (num = 0; num < (sizeof(dev_ctx)/sizeof(dev_ctx_T)); num++) {
+ if (dev_ctx[num]->name &&
+ !strcmp (dev_ctx[num]->name, name))
return 0;
}
num++;
/* it's not... let us add it to the global array */
- 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;
+ 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;
+ dev_ctx[num]->thr_should_run = 0;
+ dev_ctx[num]->thr_finished = 0;
/* make the client context aware of current card state */
ctx->volume = _alsa_get_volume (num);
@@ -101,64 +103,73 @@ PUBLIC void _alsa_free (const char *name) {
int 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]);
+ 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]);
return;
}
}
}
-PUBLIC void _alsa_play (unsigned int num, void *buf, int len) {
+PUBLIC void _alsa_play (unsigned int num) {
- if (!adev_ctx || !adev_ctx[num]) {
+ if (!dev_ctx || !dev_ctx[num] || dev_ctx[num]->thr_should_run ||
+ access (AUDIO_BUFFER, F_OK) == -1)
return;
- }
- int16_t *cbuf = (int16_t *)buf;
- int frames = len / 2;
- int res;
- 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 (adev_ctx[num]->dev); */
+ dev_ctx[num]->thr_should_run = 1;
+ dev_ctx[num]->thr_finished = 0;
+ pthread_create(&dev_ctx[num]->thr, NULL, _play_thread_fn, (void*)dev_ctx[num]);
+}
+
+PUBLIC void _alsa_stop (unsigned int num) {
+
+ if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->thr_should_run)
+ return;
+
+ /* stop the "while" loop in thread */
+ dev_ctx[num]->thr_should_run = 0;
+
+ while (!dev_ctx[num]->thr_finished)
+ usleep(100000);
+
+ pthread_join(dev_ctx[num]->thr, NULL);
}
PUBLIC unsigned int _alsa_get_volume (unsigned int num) {
- if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm)
+ if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
return;
- snd_mixer_selem_get_playback_volume (adev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &adev_ctx[num]->vol);
+ snd_mixer_selem_get_playback_volume (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &dev_ctx[num]->vol);
- return (unsigned int)(adev_ctx[num]->vol*100)/adev_ctx[num]->vol_max;
+ return (unsigned int)(dev_ctx[num]->vol*100)/dev_ctx[num]->vol_max;
}
PUBLIC unsigned int _alsa_set_volume (unsigned int num, unsigned int vol) {
- if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm || vol > 100)
+ if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm || vol > 100)
return;
- snd_mixer_selem_set_playback_volume_all (adev_ctx[num]->mixer_elm, (vol*adev_ctx[num]->vol_max)/100);
+ snd_mixer_selem_set_playback_volume_all (dev_ctx[num]->mixer_elm, (vol*dev_ctx[num]->vol_max)/100);
}
PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
int mute = 0;
- if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm)
+ if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
return;
- 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);
+ 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);
}
@@ -167,25 +178,60 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
PUBLIC void _alsa_set_mute (unsigned int num, unsigned char mute) {
- if (!adev_ctx || !adev_ctx[num] || !adev_ctx[num]->mixer_elm || 1 < mute < 0)
+ if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm || 1 < mute < 0)
return;
- 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);
+ 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);
}
PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
- if (!adev_ctx || !adev_ctx[num])
+ if (!dev_ctx || !dev_ctx[num])
return;
- snd_pcm_hw_params_set_rate_near (adev_ctx[num]->dev, adev_ctx[num]->params, &rate, 0);
+ snd_pcm_hw_params_set_rate_near (dev_ctx[num]->dev, dev_ctx[num]->params, &rate, 0);
}
PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) {
- if (!adev_ctx || !adev_ctx[num])
+ if (!dev_ctx || !dev_ctx[num])
return;
- snd_pcm_hw_params_set_channels (adev_ctx[num]->dev, adev_ctx[num]->params, channels);
+ snd_pcm_hw_params_set_channels (dev_ctx[num]->dev, dev_ctx[num]->params, channels);
+}
+
+ /* ---- LOCAL THREADED FUNCTIONS ---- */
+
+STATIC void* _play_thread_fn (void *ctx) {
+
+ dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
+ FILE *file = NULL;
+ char *buf = NULL;
+ long size;
+ int frames, res;
+
+ file = fopen (AUDIO_BUFFER, "rb");
+
+ while (dev_ctx->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
+ fseek (file, 0, SEEK_END);
+ size = ftell (file);
+ buf = (char*) realloc (buf, size * sizeof(char));
+ frames = (size * sizeof(char)) / 4;
+
+ fseek (file, 0, SEEK_SET);
+ fread (buf, 1, size, file);
+ fflush (file);
+
+ if ((res = snd_pcm_writei (dev_ctx->dev, buf, frames)) != frames) {
+ snd_pcm_recover (dev_ctx->dev, res, 0);
+ snd_pcm_prepare (dev_ctx->dev);
+ }
+ /* snd_pcm_drain (dev_ctx->dev); */
+ }
+ if (buf) free(buf);
+ if (file) fclose(file);
+
+ dev_ctx->thr_finished = 1;
+ return 0;
}
diff --git a/plugins/audio/audio-alsa.h b/plugins/audio/audio-alsa.h
index 776ac819..56b4f55f 100644
--- a/plugins/audio/audio-alsa.h
+++ b/plugins/audio/audio-alsa.h
@@ -19,24 +19,29 @@
#ifndef AUDIO_ALSA_H
#define AUDIO_ALSA_H
+#include <pthread.h>
#include <alsa/asoundlib.h>
#include "local-def.h"
-typedef struct adev_ctx adev_ctx_T;
+typedef struct dev_ctx dev_ctx_T;
-struct adev_ctx {
+struct dev_ctx {
char *name;
snd_pcm_t *dev;
snd_pcm_hw_params_t *params;
snd_mixer_elem_t *mixer_elm;
long vol_max;
long vol;
+ pthread_t thr;
+ unsigned char thr_should_run;
+ unsigned char thr_finished;
};
+STATIC void* _play_thread_fn (void *);
PUBLIC unsigned int _alsa_get_volume (unsigned int);
PUBLIC unsigned char _alsa_get_mute (unsigned int);
-static struct adev_ctx **adev_ctx = NULL;
+static struct dev_ctx **dev_ctx = NULL;
#endif /* AUDIO_ALSA_H */
diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c
index 74a1be95..53a4beab 100644
--- a/plugins/audio/audio-api.c
+++ b/plugins/audio/audio-api.c
@@ -25,24 +25,25 @@
/* private client context creation ; default values */
STATIC audioCtxHandleT* initAudioCtx () {
- audioCtxHandleT *actx;
+ audioCtxHandleT *ctx;
- actx = malloc (sizeof(audioCtxHandleT));
- actx->idx = -1;
- actx->volume = 25;
- actx->channels = 2;
- actx->mute = 0;
+ ctx = malloc (sizeof(audioCtxHandleT));
+ ctx->idx = -1;
+ ctx->volume = 25;
+ ctx->channels = 2;
+ ctx->mute = 0;
+ ctx->is_playing = 0;
- return actx;
+ return ctx;
}
-STATIC AFB_error releaseAudio (audioCtxHandleT *actx) {
+STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
/* power it off */
- _alsa_free (actx->idx);
+ _alsa_free (ctx->idx);
/* clean client context */
- actx->idx = -1;
+ ctx->idx = -1;
return AFB_SUCCESS;
}
@@ -61,15 +62,15 @@ STATIC json_object* freeAudio (AFB_clientCtx *client) {
STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
- audioCtxHandleT *actx;
+ audioCtxHandleT *ctx;
json_object *jresp;
int idx;
/* create a private client context */
- actx = initAudioCtx();
- request->client->ctx = (audioCtxHandleT*)actx;
+ ctx = initAudioCtx();
+ request->client->ctx = (audioCtxHandleT*)ctx;
- _alsa_init("default", actx);
+ _alsa_init("default", ctx);
jresp = json_object_new_object();
json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
@@ -78,7 +79,7 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx;
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
const char *value = getQueryValue (request, "value");
json_object *jresp;
int volume;
@@ -86,8 +87,8 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
/* no "?value=" parameter : return current state */
if (!value) {
- actx->volume = _alsa_get_volume (actx->idx);
- snprintf (volume_str, sizeof(volume_str), "%d", actx->volume);
+ ctx->volume = _alsa_get_volume (ctx->idx);
+ snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume);
jresp = json_object_new_object();
json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
}
@@ -99,10 +100,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"));
}
- actx->volume = volume;
- _alsa_set_volume (actx->idx, actx->volume);
+ ctx->volume = volume;
+ _alsa_set_volume (ctx->idx, ctx->volume);
- snprintf (volume_str, sizeof(volume_str), "%d", actx->volume);
+ snprintf (volume_str, sizeof(volume_str), "%d", ctx->volume);
jresp = json_object_new_object();
json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
}
@@ -112,23 +113,23 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx;
+ audioCtxHandleT *ctx = (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", actx->channels);
+ snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
}
/* "?value=" parameter, set channels */
else {
- actx->channels = atoi (value);
- _alsa_set_channels (actx->idx, actx->channels);
+ ctx->channels = atoi (value);
+ _alsa_set_channels (ctx->idx, ctx->channels);
- snprintf (channels_str, sizeof(channels_str), "%d", actx->channels);
+ snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
}
@@ -137,30 +138,30 @@ STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *actx = (audioCtxHandleT*)request->client->ctx;
+ audioCtxHandleT *ctx = (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) {
- actx->mute = _alsa_get_mute (actx->idx);
- actx->mute ?
+ ctx->mute = _alsa_get_mute (ctx->idx);
+ ctx->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") ) {
- actx->mute = 1;
- _alsa_set_mute (actx->idx, actx->mute);
+ /* "?value=" parameter is "1" or "true" */
+ else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
+ ctx->mute = 1;
+ _alsa_set_mute (ctx->idx, ctx->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") ) {
- actx->mute = 0;
- _alsa_set_mute (actx->idx, actx->mute);
+ /* "?value=" parameter is "0" or "false" */
+ else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
+ ctx->mute = 0;
+ _alsa_set_mute (ctx->idx, ctx->mute);
json_object_object_add (jresp, "mute", json_object_new_string ("off"));
}
@@ -168,6 +169,38 @@ STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
return jresp;
}
+STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
+
+ audioCtxHandleT *ctx = (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->is_playing ?
+ json_object_object_add (jresp, "play", json_object_new_string ("on"))
+ : json_object_object_add (jresp, "play", json_object_new_string ("off"));
+ }
+
+ /* "?value=" parameter is "1" or "true" */
+ else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) {
+ ctx->is_playing = 1;
+ _alsa_play (ctx->idx);
+
+ json_object_object_add (jresp, "play", json_object_new_string ("on"));
+ }
+
+ /* "?value=" parameter is "0" or "false" */
+ else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) {
+ ctx->is_playing = 0;
+ _alsa_stop (ctx->idx);
+
+ json_object_object_add (jresp, "play", json_object_new_string ("off"));
+ }
+
+ return jresp;
+}
+
STATIC json_object* refresh (AFB_request *request) { /* AFB_SESSION_RENEW */
json_object *jresp = json_object_new_object();
json_object_object_add(jresp, "token", json_object_new_string (request->client->token));
@@ -184,8 +217,9 @@ STATIC AFB_restapi pluginApis[]= {
{"volume" , AFB_SESSION_CHECK, (AFB_apiCB)volume , "Audio API - volume"},
{"channels", AFB_SESSION_CHECK, (AFB_apiCB)channels , "Audio API - channels"},
{"mute" , AFB_SESSION_CHECK, (AFB_apiCB)mute , "Audio API - mute"},
- {"refresh", AFB_SESSION_RENEW, (AFB_apiCB)refresh , "Audio API - refresh"},
- {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
+ {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Audio API - play"},
+ {"refresh" , AFB_SESSION_RENEW, (AFB_apiCB)refresh , "Audio API - refresh"},
+ {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
{NULL}
};
diff --git a/plugins/audio/audio-api.h b/plugins/audio/audio-api.h
index 1cf54fc0..12f70a8e 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 {
@@ -34,11 +34,12 @@ typedef struct {
/* private client context [will be destroyed when client leaves] */
typedef struct {
- audioDevT *radio; /* pointer to client audio card */
- int idx; /* audio card index within global array */
- unsigned int volume; /* audio volume : 0-100 */
- unsigned int channels; /* audio channels : 1(mono)/2(stereo)... */
- unsigned char mute; /* audio muted : 0(false)/1(true) */
+ audioDevT *radio; /* pointer to client audio card */
+ int idx; /* audio card index within global array */
+ unsigned int volume; /* audio volume : 0-100 */
+ unsigned int channels; /* audio channels : 1(mono)/2(stereo)... */
+ unsigned char mute; /* audio muted : 0(false)/1(true) */
+ unsigned char is_playing; /* audio is playing: 0(false)/1(true) */
} audioCtxHandleT;