aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Bachmann <mbc@iot.bzh>2015-12-17 08:29:48 +0100
committerManuel Bachmann <manuel.bachmann@iot.bzh>2015-12-17 08:55:10 +0100
commit915d25a0c2fcff8a8bb189963f490e0894d05733 (patch)
tree918c6fcd7ac039bf7717f4f129d588d19d9a45ec
parent3f13bc8ca416e6318bcbd199113a2cd69b11b2b6 (diff)
Finalize Audio plugin
Signed-off-by: Manuel Bachmann <mbc@iot.bzh>
-rw-r--r--plugins/audio/audio-alsa.c113
-rw-r--r--plugins/audio/audio-alsa.h26
-rw-r--r--plugins/audio/audio-api.c126
-rw-r--r--plugins/audio/audio-api.h23
-rw-r--r--plugins/radio/radio-api.c1
-rw-r--r--plugins/radio/radio-api.h3
-rw-r--r--plugins/radio/radio-rtlsdr.c1
-rw-r--r--plugins/radio/radio-rtlsdr.h3
8 files changed, 281 insertions, 15 deletions
diff --git a/plugins/audio/audio-alsa.c b/plugins/audio/audio-alsa.c
index 8c08c8db..4d97c2d2 100644
--- a/plugins/audio/audio-alsa.c
+++ b/plugins/audio/audio-alsa.c
@@ -1,3 +1,21 @@
+/*
+ * Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
#include "audio-api.h"
#include "audio-alsa.h"
@@ -5,6 +23,11 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
snd_pcm_t *dev;
snd_pcm_hw_params_t *params;
+ snd_mixer_t *mixer;
+ snd_mixer_selem_id_t *mixer_sid;
+ snd_mixer_elem_t *mixer_elm;
+ unsigned int rate = 22050;
+ long vol, vol_min, vol_max;
int num;
if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
@@ -14,14 +37,30 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
snd_pcm_hw_params_any (dev, params);
snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE);
- snd_pcm_hw_params_set_rate_near (dev, params, &ctx->rate, 0);
+ snd_pcm_hw_params_set_rate_near (dev, params, &rate, 0);
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 0;
}
snd_pcm_prepare (dev);
-
+
+ snd_mixer_open (&mixer, 0);
+ if (snd_mixer_attach (mixer, name) < 0) {
+ snd_pcm_hw_params_free (params);
+ return 0;
+ }
+ snd_mixer_selem_register (mixer, NULL, NULL);
+ snd_mixer_load (mixer);
+
+ snd_mixer_selem_id_alloca (&mixer_sid);
+ snd_mixer_selem_id_set_index (mixer_sid, 0);
+ 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);
+
/* allocate the global array if it hasn't been done */
if (!dev_ctx) {
dev_ctx = (dev_ctx_T**) malloc (sizeof(dev_ctx_T));
@@ -39,10 +78,19 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
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;
+
+ /* make the client context aware of current card state */
+ ctx->volume = _alsa_get_volume (num);
+ ctx->mute = _alsa_get_mute (num);
+ ctx->idx = num;
return 1;
}
@@ -79,4 +127,63 @@ PUBLIC void _alsa_play (unsigned int num, void *buf, int len) {
snd_pcm_prepare (dev_ctx[num]->dev);
}
/* snd_pcm_drain (dev_ctx[num]->dev); */
-} \ No newline at end of file
+}
+
+PUBLIC unsigned int _alsa_get_volume (unsigned int num) {
+
+ if (!dev_ctx || !dev_ctx[num])
+ return;
+
+ snd_mixer_selem_get_playback_volume (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &dev_ctx[num]->vol);
+
+ 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 (!dev_ctx || !dev_ctx[num] || vol > 100)
+ return;
+
+ 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 (!dev_ctx || !dev_ctx[num])
+ 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);
+
+ }
+
+ return (unsigned char)mute;
+}
+
+PUBLIC void _alsa_set_mute (unsigned int num, unsigned char mute) {
+
+ if (!dev_ctx || !dev_ctx[num] || 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);
+}
+
+PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
+
+ if (!dev_ctx || !dev_ctx[num])
+ return;
+
+ 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 (!dev_ctx || !dev_ctx[num])
+ return;
+
+ snd_pcm_hw_params_set_channels (dev_ctx[num]->dev, dev_ctx[num]->params, channels);
+}
diff --git a/plugins/audio/audio-alsa.h b/plugins/audio/audio-alsa.h
index a85f419d..9a4b0b58 100644
--- a/plugins/audio/audio-alsa.h
+++ b/plugins/audio/audio-alsa.h
@@ -1,3 +1,21 @@
+/*
+ * Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
#ifndef AUDIO_ALSA_H
#define AUDIO_ALSA_H
@@ -11,8 +29,14 @@ 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;
};
+PUBLIC unsigned int _alsa_get_volume (unsigned int);
+PUBLIC unsigned char _alsa_get_mute (unsigned int);
+
static struct dev_ctx **dev_ctx = NULL;
-#endif /* AUDIO_ALSA_H */ \ No newline at end of file
+#endif /* AUDIO_ALSA_H */
diff --git a/plugins/audio/audio-api.c b/plugins/audio/audio-api.c
index 547f97ed..0fcc73d1 100644
--- a/plugins/audio/audio-api.c
+++ b/plugins/audio/audio-api.c
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2015 "IoT.bzh"
- * Author "Fulup Ar Foll"
+ * Author "Manuel Bachmann"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -28,17 +28,29 @@ STATIC audioCtxHandleT* initAudioCtx () {
audioCtxHandleT *ctx;
ctx = malloc (sizeof(audioCtxHandleT));
+ ctx->idx = -1;
ctx->volume = 25;
- ctx->rate = 22050;
ctx->channels = 2;
+ ctx->mute = 0;
return ctx;
}
+STATIC AFB_error releaseAudio (audioCtxHandleT *ctx) {
+
+ /* power it off */
+ _alsa_free (ctx->idx);
+
+ /* clean client context */
+ ctx->idx = -1;
+
+ return AFB_SUCCESS;
+}
+
/* called when client session dies [e.g. client quits for more than 15mns] */
STATIC json_object* freeAudio (AFB_clientCtx *client) {
- //releaseAudio (client->plugin->handle, client->ctx);
+ releaseAudio (client->ctx);
free (client->ctx);
return jsonNewMessage (AFB_SUCCESS, "Released radio and client context");
@@ -51,6 +63,7 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
audioCtxHandleT *ctx;
json_object *jresp;
+ int idx;
/* create a private client context */
ctx = initAudioCtx();
@@ -60,13 +73,112 @@ STATIC json_object* init (AFB_request *request) { /* AFB_SESSION_CREATE */
jresp = json_object_new_object();
json_object_object_add (jresp, "token", json_object_new_string (request->client->token));
+ return jresp;
}
+STATIC json_object* volume (AFB_request *request) { /* AFB_SESSION_CHECK */
-STATIC AFB_restapi pluginApis[]= {
- {"init" , AFB_SESSION_CREATE, (AFB_apiCB)init , "Audio API - init"},
-// {"error" , AFB_SESSION_CHECK, (AFB_apiCB)wrongApi , "Ping Application Framework"},
+ audioCtxHandleT *ctx = (audioCtxHandleT*)request->client->ctx;
+ const char *value = getQueryValue (request, "value");
+ json_object *jresp;
+ int volume;
+ char volume_str[256];
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ 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));
+ }
+
+ /* "?value=" parameter, set volume */
+ else {
+ volume = atoi (value);
+ if (100 < volume < 0) {
+ 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);
+
+ 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));
+ }
+
+ return jresp;
+}
+
+STATIC json_object* channels (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();
+ char channels_str[256];
+
+ /* no "?value=" parameter : return current state */
+ if (!value) {
+ 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 {
+ ctx->channels = atoi (value);
+ _alsa_set_channels (ctx->idx, 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;
+}
+STATIC json_object* mute (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->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") ) {
+ 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" */
+ if ( atoi(value) == 0 || !strcasecmp(value, "off") ) {
+ ctx->mute = 0;
+ _alsa_set_mute (ctx->idx, ctx->mute);
+
+ json_object_object_add (jresp, "mute", json_object_new_string ("off"));
+ }
+
+ return jresp;
+}
+
+STATIC json_object* status (AFB_request *request) { /* AFB_SESSION_RENEW */
+ return NULL;
+}
+
+
+STATIC AFB_restapi pluginApis[]= {
+ {"init" , AFB_SESSION_CREATE, (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"},
+ {"status" , AFB_SESSION_RENEW, (AFB_apiCB)status , "Audio API - status"},
{NULL}
};
@@ -80,4 +192,4 @@ PUBLIC AFB_plugin *audioRegister () {
plugin->freeCtxCB = freeAudio;
return (plugin);
-}; \ No newline at end of file
+};
diff --git a/plugins/audio/audio-api.h b/plugins/audio/audio-api.h
index 0928f0fa..8acb1bd8 100644
--- a/plugins/audio/audio-api.h
+++ b/plugins/audio/audio-api.h
@@ -1,3 +1,21 @@
+/*
+ * Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
+ *
+ * This program 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.
+ *
+ * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
#ifndef AUDIO_API_H
#define AUDIO_API_H
@@ -17,10 +35,11 @@ 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 rate; /* audio rate (Hz) */
unsigned int channels; /* audio channels : 1(mono)/2(stereo)... */
+ unsigned char mute; /* audio muted : 0(false)/1(true) */
} audioCtxHandleT;
-#endif /* AUDIO_API_H */ \ No newline at end of file
+#endif /* AUDIO_API_H */
diff --git a/plugins/radio/radio-api.c b/plugins/radio/radio-api.c
index efd309c3..f065a8ad 100644
--- a/plugins/radio/radio-api.c
+++ b/plugins/radio/radio-api.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/plugins/radio/radio-api.h b/plugins/radio/radio-api.h
index 3645fff8..4ea41b1c 100644
--- a/plugins/radio/radio-api.h
+++ b/plugins/radio/radio-api.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -47,4 +48,4 @@ typedef struct {
unsigned char is_playing; /* radio is playing: 0(false)/1(true) */
} radioCtxHandleT;
-#endif /* RADIO_API_H */ \ No newline at end of file
+#endif /* RADIO_API_H */
diff --git a/plugins/radio/radio-rtlsdr.c b/plugins/radio/radio-rtlsdr.c
index bf4fcea5..e90bda08 100644
--- a/plugins/radio/radio-rtlsdr.c
+++ b/plugins/radio/radio-rtlsdr.c
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
diff --git a/plugins/radio/radio-rtlsdr.h b/plugins/radio/radio-rtlsdr.h
index f3e2f454..74049a9b 100644
--- a/plugins/radio/radio-rtlsdr.h
+++ b/plugins/radio/radio-rtlsdr.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 "IoT.bzh"
+ * Author "Manuel Bachmann"
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -95,4 +96,4 @@ STATIC void _radio_stop_threads (struct dev_ctx *);
static unsigned int init_dev_count = 0;
static struct dev_ctx **dev_ctx = NULL;
-#endif /* RADIO_RTLSDR_H */ \ No newline at end of file
+#endif /* RADIO_RTLSDR_H */