Fix Audio plugin loading, mute on Porter/Koelsch
authorManuel Bachmann <manuel.bachmann@iot.bzh>
Mon, 28 Dec 2015 15:16:12 +0000 (16:16 +0100)
committerManuel Bachmann <manuel.bachmann@iot.bzh>
Mon, 28 Dec 2015 15:16:12 +0000 (16:16 +0100)
Audio plugin was crashing due to new client context model
misuse.

Muting/demuting sound did fail on Porter/Koelsch boards,
because their audio cards do not obey standards found on
desktop systems.

Signed-off-by: Manuel Bachmann <manuel.bachmann@iot.bzh>
plugins/audio/audio-alsa.c
plugins/audio/audio-alsa.h
plugins/audio/audio-api.c

index 1f54f24..8a4caf2 100644 (file)
@@ -37,6 +37,7 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
     snd_mixer_t *mixer;
     snd_mixer_selem_id_t *mixer_sid;
     snd_mixer_elem_t *mixer_elm;
+    snd_mixer_elem_t *mixer_elm_m;
     unsigned int rate = 22050;
     long vol, vol_min, vol_max;
     int num, i;
@@ -69,6 +70,8 @@ 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);
+    mixer_elm_m = NULL;
+
     if (!mixer_elm) {
         /* no "Master" mixer ; we are probably on a board... search ! */
         for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL;
@@ -76,9 +79,15 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
             if (snd_mixer_elem_info (mixer_elm) < 0)
                 continue;
             snd_mixer_selem_get_id (mixer_elm, mixer_sid);
-            if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "Master") ||
-                strstr (snd_mixer_selem_id_get_name (mixer_sid), "Playback"))
+            if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) {
+
+                /* this is Porter... let us found the specific mute switch */
+                snd_mixer_selem_id_set_index (mixer_sid, 0);
+                snd_mixer_selem_id_set_name (mixer_sid, "DVC Out Mute");
+                mixer_elm_m = snd_mixer_find_selem (mixer, mixer_sid);
+
                 break;
+            }
         }
     }
 
@@ -110,6 +119,7 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
     dev_ctx[num]->dev = dev;
     dev_ctx[num]->params = params;
     dev_ctx[num]->mixer_elm = mixer_elm;
+    dev_ctx[num]->mixer_elm_m = mixer_elm_m;
     dev_ctx[num]->vol_max = vol_max;
     dev_ctx[num]->vol = vol;
     dev_ctx[num]->thr_should_run = 0;
@@ -198,14 +208,17 @@ PUBLIC void _alsa_set_volume_all (unsigned int num, int vol) {
 PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
 
     int mute = 0;
+    snd_mixer_elem_t *elm_m;
 
     if (!dev_ctx || !dev_ctx[num] || !dev_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); 
+    dev_ctx[num]->mixer_elm_m ? (elm_m = dev_ctx[num]->mixer_elm_m) :
+                                (elm_m = dev_ctx[num]->mixer_elm);
 
+    if (snd_mixer_selem_has_playback_switch (elm_m)) {
+        snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute);
+        snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute);
     }
 
     return (unsigned char)!mute;
@@ -213,11 +226,16 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
 
 PUBLIC void _alsa_set_mute (unsigned int num, unsigned char mute) {
 
+    snd_mixer_elem_t *elm_m;
+
     if (!dev_ctx || !dev_ctx[num] || !dev_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);
+    dev_ctx[num]->mixer_elm_m ? (elm_m = dev_ctx[num]->mixer_elm_m) :
+                                (elm_m = dev_ctx[num]->mixer_elm);
+
+    if (snd_mixer_selem_has_playback_switch (elm_m))
+        snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
 }
 
 PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
index 85545d5..25292ad 100644 (file)
@@ -31,6 +31,7 @@ struct dev_ctx {
   snd_pcm_t *dev;
   snd_pcm_hw_params_t *params;
   snd_mixer_elem_t *mixer_elm;
+  snd_mixer_elem_t *mixer_elm_m;
   long vol_max;
   long vol;
   pthread_t thr;
index 1311b65..4c7df8b 100644 (file)
@@ -60,14 +60,13 @@ STATIC void freeAudio (void *context) {
 
 STATIC json_object* init (AFB_request *request) {       /* AFB_SESSION_CREATE */
 
-    audioCtxHandleT *ctx;
     json_object *jresp;
     int idx;
 
     /* create a private client context */
     request->context = initAudioCtx();
     
-    _alsa_init("default", ctx);
+    _alsa_init("default", request->context);
     
     jresp = json_object_new_object();
     json_object_object_add (jresp, "info", json_object_new_string ("Audio initialised"));