summaryrefslogtreecommitdiffstats
path: root/plugins/audio
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/audio')
-rw-r--r--plugins/audio/audio-alsa.c40
-rw-r--r--plugins/audio/audio-alsa.h13
-rw-r--r--plugins/audio/audio-api.c121
-rw-r--r--plugins/audio/audio-api.h4
-rw-r--r--plugins/audio/audio-pulse.c8
5 files changed, 100 insertions, 86 deletions
diff --git a/plugins/audio/audio-alsa.c b/plugins/audio/audio-alsa.c
index bc777fce..6240d39d 100644
--- a/plugins/audio/audio-alsa.c
+++ b/plugins/audio/audio-alsa.c
@@ -17,6 +17,7 @@
*/
#include "audio-api.h"
+#include "audio-alsa.h"
snd_mixer_selem_channel_id_t SCHANNELS[8] = {
SND_MIXER_SCHN_FRONT_LEFT,
@@ -42,7 +43,7 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
int num, i;
if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
- return -1;
+ return 0;
snd_pcm_hw_params_malloc (&params);
snd_pcm_hw_params_any (dev, params);
@@ -52,14 +53,14 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
snd_pcm_hw_params_set_channels (dev, params, ctx->channels);
if (snd_pcm_hw_params (dev, params) < 0) {
snd_pcm_hw_params_free (params);
- return -1;
+ return 0;
}
snd_pcm_prepare (dev);
snd_mixer_open (&mixer, 0);
if (snd_mixer_attach (mixer, name) < 0) {
snd_pcm_hw_params_free (params);
- return -1;
+ return 0;
}
snd_mixer_selem_register (mixer, NULL, NULL);
snd_mixer_load (mixer);
@@ -107,7 +108,7 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T)); num++) {
if (dev_ctx_a[num]->name &&
!strcmp (dev_ctx_a[num]->name, name))
- return -1;
+ return 0;
}
num++;
@@ -129,10 +130,11 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
ctx->volume[i] = _alsa_get_volume (num, i);
ctx->mute = _alsa_get_mute (num);
ctx->idx = num;
+ ctx->name = strdup (name);
if (verbose) fprintf (stderr, "Successfully initialized ALSA backend.\n");
- return 0;
+ return 1;
}
PUBLIC void _alsa_free (const char *name) {
@@ -153,7 +155,7 @@ PUBLIC void _alsa_free (const char *name) {
}
}
-PUBLIC void _alsa_play (unsigned int num) {
+PUBLIC void _alsa_play (int num) {
if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run ||
access (AUDIO_BUFFER, F_OK) == -1)
@@ -164,7 +166,7 @@ PUBLIC void _alsa_play (unsigned int num) {
pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]);
}
-PUBLIC void _alsa_stop (unsigned int num) {
+PUBLIC void _alsa_stop (int num) {
if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run)
return;
@@ -178,41 +180,41 @@ PUBLIC void _alsa_stop (unsigned int num) {
pthread_join (dev_ctx_a[num]->thr, NULL);
}
-PUBLIC int _alsa_get_volume (unsigned int num, unsigned int channel) {
+PUBLIC unsigned int _alsa_get_volume (int num, unsigned int channel) {
if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
- return;
+ return 0;
snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol);
return (int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max;
}
-PUBLIC void _alsa_set_volume (unsigned int num, unsigned int channel, int vol) {
+PUBLIC void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) {
if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
- 0 > vol > 100)
+ vol > 100)
return;
snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100);
}
-PUBLIC void _alsa_set_volume_all (unsigned int num, int vol) {
+PUBLIC void _alsa_set_volume_all (int num, unsigned int vol) {
if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
- 0 > vol > 100)
+ vol > 100)
snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100);
}
-PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
+PUBLIC unsigned char _alsa_get_mute (int num) {
int mute = 0;
snd_mixer_elem_t *elm_m;
if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
- return;
+ return 0;
dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) :
(elm_m = dev_ctx_a[num]->mixer_elm);
@@ -228,12 +230,12 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
return (unsigned char)!mute;
}
-PUBLIC void _alsa_set_mute (unsigned int num, unsigned char tomute) {
+PUBLIC void _alsa_set_mute (int num, unsigned char tomute) {
snd_mixer_elem_t *elm_m;
int mute;
- if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute < 0)
+ if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute)
return;
if (dev_ctx_a[num]->mixer_elm_m) {
@@ -248,7 +250,7 @@ PUBLIC void _alsa_set_mute (unsigned int num, unsigned char tomute) {
snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
}
-PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
+PUBLIC void _alsa_set_rate (int num, unsigned int rate) {
if (!dev_ctx_a || !dev_ctx_a[num])
return;
@@ -256,7 +258,7 @@ PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0);
}
-PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) {
+PUBLIC void _alsa_set_channels (int num, unsigned int channels) {
if (!dev_ctx_a || !dev_ctx_a[num])
return;
diff --git a/plugins/audio/audio-alsa.h b/plugins/audio/audio-alsa.h
index 9b9ee21c..5ac8ea49 100644
--- a/plugins/audio/audio-alsa.h
+++ b/plugins/audio/audio-alsa.h
@@ -22,6 +22,7 @@
#include <pthread.h>
#include <alsa/asoundlib.h>
+#include "audio-api.h"
#include "local-def.h"
typedef struct dev_ctx_alsa dev_ctx_alsa_T;
@@ -39,9 +40,17 @@ struct dev_ctx_alsa {
unsigned char thr_finished;
};
+PUBLIC unsigned char _alsa_init (const char *, audioCtxHandleT *);
+PUBLIC void _alsa_free (const char *);
+PUBLIC void _alsa_play (int);
+PUBLIC void _alsa_stop (int);
+PUBLIC unsigned int _alsa_get_volume (int, unsigned int);
+PUBLIC void _alsa_set_volume (int, unsigned int, unsigned int);
+PUBLIC void _alsa_set_volume_all (int, unsigned int);
+PUBLIC unsigned char _alsa_get_mute (int);
+PUBLIC void _alsa_set_mute (int, unsigned char);
+PUBLIC void _alsa_set_channels (int, unsigned int);
STATIC void* _alsa_play_thread_fn (void *);
-PUBLIC int _alsa_get_volume (unsigned int, unsigned int);
-PUBLIC unsigned char _alsa_get_mute (unsigned int);
static struct dev_ctx_alsa **dev_ctx_a = NULL;
diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c
index 9c9da251..ebf78e18 100644
--- a/plugins/audio/audio-api.c
+++ b/plugins/audio/audio-api.c
@@ -16,23 +16,30 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#define _GNU_SOURCE
+#include <stdlib.h>
+
#include "audio-api.h"
+#include "audio-alsa.h"
+
+#include "afb-plugin.h"
+#include "afb-req-itf.h"
/* ------ BACKEND FUNCTIONS ------- */
void _backend_init (const char *name, audioCtxHandleT *ctx) {
char *backend_env = getenv ("AFB_AUDIO_OUTPUT");
- unsigned char res = -1;
+ unsigned char res = 0;
# ifdef HAVE_PULSE
- if (!backend_env || (strcasecmp (backend_env, "Alsa") != 0))
+ if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0))
res = _pulse_init (name, ctx);
- if (res < 0)
+ if (!res)
#endif
res = _alsa_init (name, ctx);
- if (res < 0 && verbose)
+ if (!res && verbose)
fprintf (stderr, "Could not initialize Audio backend\n");
}
@@ -41,7 +48,7 @@ void _backend_free (audioCtxHandleT *ctx) {
# ifdef HAVE_PULSE
if (ctx->audio_dev) _pulse_free (ctx); else
# endif
- _alsa_free (ctx->idx);
+ _alsa_free (ctx->name);
}
void _backend_play (audioCtxHandleT *ctx) {
@@ -60,7 +67,7 @@ void _backend_stop (audioCtxHandleT *ctx) {
_alsa_stop (ctx->idx);
}
-int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
+unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
# ifdef HAVE_PULSE
if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else
@@ -68,7 +75,7 @@ int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
return _alsa_get_volume (ctx->idx, channel);
}
-void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, int vol) {
+void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
# ifdef HAVE_PULSE
if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else
@@ -76,7 +83,7 @@ void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, int vol) {
_alsa_set_volume (ctx->idx, channel, vol);
}
-void _backend_set_volume_all (audioCtxHandleT *ctx, int vol) {
+void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
# ifdef HAVE_PULSE
if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else
@@ -147,31 +154,31 @@ STATIC void freeAudio (void *context) {
/* ------ PUBLIC PLUGIN FUNCTIONS --------- */
-STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CHECK */
+STATIC void init (struct afb_req request) { /* AFB_SESSION_CHECK */
json_object *jresp;
- int idx;
/* create a private client context */
- if (!request->context)
- request->context = initAudioCtx();
+ if (!request.context)
+ request.context = initAudioCtx();
- _backend_init("default", request->context);
+ _backend_init("default", request.context);
jresp = json_object_new_object();
- json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));
- return (jresp);
+ json_object_object_add (jresp, "info", json_object_new_string ("Audio initialized"));
+
+ afb_req_success (request, jresp, "Audio initiliazed");
}
-STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
+STATIC void volume (struct afb_req request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
- const char *value = getQueryValue (request, "value");
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
+ const char *value = afb_req_argument (request, "value");
json_object *jresp;
- int volume[8], i;
+ unsigned int volume[8], i;
char *volume_i;
char volume_str[256];
- int len_str = 0;
+ size_t len_str = 0;
/* no "?value=" parameter : return current state */
if (!value) {
@@ -188,12 +195,13 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
else {
volume_i = strdup (value);
volume_i = strtok (volume_i, ",");
- volume[0] = atoi (volume_i);
+ volume[0] = (unsigned int) atoi (volume_i);
- if ((100 < volume[0])||(volume[0] < 0)) {
+ if (100 < volume[0]) {
free (volume_i);
- request->errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
- return (jsonNewMessage (AFB_FAIL, "Volume must be between 0 and 100"));
+ //request.errcode = MHD_HTTP_SERVICE_UNAVAILABLE;
+ afb_req_fail (request, "Failed", "Volume must be between 0 and 100");
+ return;
}
ctx->volume[0] = volume[0];
_backend_set_volume (ctx, 0, ctx->volume[0]);
@@ -204,10 +212,10 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
/* if there is only one value, set all channels to this one */
if (!volume_i && i == 1)
_backend_set_volume_all (ctx, ctx->volume[0]);
- if (!volume_i || 100 < atoi(volume_i) < 0) {
+ if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0) {
ctx->volume[i] = _backend_get_volume (ctx, i);
} else {
- ctx->volume[i] = atoi(volume_i);
+ ctx->volume[i] = (unsigned int) atoi(volume_i);
_backend_set_volume (ctx, i, ctx->volume[i]);
}
len_str = strlen(volume_str);
@@ -217,13 +225,13 @@ STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
json_object_object_add (jresp, "volume", json_object_new_string(volume_str));
}
- return jresp;
+ afb_req_success (request, jresp, "Audio - Volume changed");
}
-STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
+STATIC void channels (struct afb_req request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
- const char *value = getQueryValue (request, "value");
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
+ const char *value = afb_req_argument (request, "value");
json_object *jresp = json_object_new_object();
char channels_str[256];
@@ -235,20 +243,20 @@ STATIC json_object* channels (AFB_request *request) { /* AFB_SESSION_CHECK */
/* "?value=" parameter, set channels */
else {
- ctx->channels = atoi (value);
+ ctx->channels = (unsigned int) atoi (value);
_backend_set_channels (ctx, ctx->channels);
snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels);
json_object_object_add (jresp, "channels", json_object_new_string (channels_str));
}
- return jresp;
+ afb_req_success (request, jresp, "Audio - Channels set");
}
-STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
+STATIC void mute (struct afb_req request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
- const char *value = getQueryValue (request, "value");
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
+ const char *value = afb_req_argument (request, "value");
json_object *jresp = json_object_new_object();
/* no "?value=" parameter : return current state */
@@ -275,13 +283,13 @@ STATIC json_object* mute (AFB_request *request) { /* AFB_SESSION_CHECK */
json_object_object_add (jresp, "mute", json_object_new_string ("off"));
}
- return jresp;
+ afb_req_success (request, jresp, "Audio - Mute set");
}
-STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
+STATIC void play (struct afb_req request) { /* AFB_SESSION_CHECK */
- audioCtxHandleT *ctx = (audioCtxHandleT*)request->context;
- const char *value = getQueryValue (request, "value");
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request.context;
+ const char *value = afb_req_argument (request, "value");
json_object *jresp = json_object_new_object();
/* no "?value=" parameter : return current state */
@@ -307,31 +315,26 @@ STATIC json_object* play (AFB_request *request) { /* AFB_SESSION_CHECK */
json_object_object_add (jresp, "play", json_object_new_string ("off"));
}
- return jresp;
+ afb_req_success (request, jresp, "Audio - Play");
}
-STATIC json_object* ping (AFB_request *request) { /* AFB_SESSION_NONE */
- return jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon - Radio API");
+STATIC void ping (struct afb_req request) { /* AFB_SESSION_NONE */
+ afb_req_success (request, NULL, "Audio - Ping success");
}
-STATIC AFB_restapi pluginApis[]= {
- {"init" , AFB_SESSION_CHECK, (AFB_apiCB)init , "Audio API - init"},
- {"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"},
- {"play" , AFB_SESSION_CHECK, (AFB_apiCB)play , "Audio API - play"},
- {"ping" , AFB_SESSION_NONE, (AFB_apiCB)ping , "Audio API - ping"},
+STATIC const struct AFB_restapi pluginApis[]= {
+ {"init" , AFB_SESSION_CHECK, init , "Audio API - init"},
+ {"volume" , AFB_SESSION_CHECK, volume , "Audio API - volume"},
+ {"channels", AFB_SESSION_CHECK, channels , "Audio API - channels"},
+ {"mute" , AFB_SESSION_CHECK, mute , "Audio API - mute"},
+ {"play" , AFB_SESSION_CHECK, play , "Audio API - play"},
+ {"ping" , AFB_SESSION_NONE, ping , "Audio API - ping"},
{NULL}
};
-PUBLIC AFB_plugin *pluginRegister () {
- AFB_plugin *plugin = malloc (sizeof(AFB_plugin));
- plugin->type = AFB_PLUGIN_JSON;
- plugin->info = "Application Framework Binder - Audio plugin";
- plugin->prefix = "audio";
- plugin->apis = pluginApis;
-
- plugin->freeCtxCB = (AFB_freeCtxCB)freeAudio;
-
- return (plugin);
+STATIC const struct AFB_plugin plug_desc = {
+ .type = AFB_PLUGIN_JSON,
+ .info = "Application Framework Binder - Audio plugin",
+ .prefix = "audio",
+ .apis = pluginApis
};
diff --git a/plugins/audio/audio-api.h b/plugins/audio/audio-api.h
index 77d6e152..bd21b6ab 100644
--- a/plugins/audio/audio-api.h
+++ b/plugins/audio/audio-api.h
@@ -19,7 +19,6 @@
#ifndef AUDIO_API_H
#define AUDIO_API_H
-#include "audio-alsa.h"
#ifdef HAVE_PULSE
#include "audio-pulse.h"
#endif
@@ -32,8 +31,9 @@ typedef struct {
/* private client context [will be destroyed when client leaves] */
typedef struct {
void *audio_dev; /* handle to implementation (ALSA, PulseAudio...) */
+ char *name; /* name of the audio card */
int idx; /* audio card index within global array */
- int volume[8]; /* audio volume (8 channels) : 0-100 */
+ unsigned int volume[8]; /* audio volume (8 channels) : 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) */
diff --git a/plugins/audio/audio-pulse.c b/plugins/audio/audio-pulse.c
index ceffcf7d..35de7f9a 100644
--- a/plugins/audio/audio-pulse.c
+++ b/plugins/audio/audio-pulse.c
@@ -56,7 +56,7 @@ PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
if (ret == -1) {
if (verbose) fprintf (stderr, "Stopping PulseAudio backend...\n");
- return -1;
+ return 0;
}
if (ret >= 0) {
/* found a matching sink from callback */
@@ -68,7 +68,7 @@ PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
}
/* fail if we found no matching sink */
if (!ctx->audio_dev)
- return -1;
+ return 0;
/* make the client context aware of current card state */
ctx->mute = (unsigned char)dev_ctx_p[ret]->mute;
@@ -87,7 +87,7 @@ PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
"afb-audio-output", pa_spec, NULL, NULL, &error))) {
fprintf (stderr, "Error opening PulseAudio sink %s : %s\n",
dev_ctx_p[ret]->sink_name, pa_strerror(error));
- return -1;
+ return 0;
}
dev_ctx_p[ret]->pa = pa;
free (pa_spec);
@@ -96,7 +96,7 @@ PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
if (verbose) fprintf (stderr, "Successfully initialized PulseAudio backend.\n");
- return 0;
+ return 1;
}
PUBLIC void _pulse_free (audioCtxHandleT *ctx) {