Improve the Audio ALSA backend
[src/app-framework-binder.git] / plugins / audio / audio-pulse.c
index 4caf3db..89e5c21 100644 (file)
@@ -2,24 +2,31 @@
  * Copyright (C) 2016 "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.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * 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.
+ *   http://www.apache.org/licenses/LICENSE-2.0
  *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
+#define _GNU_SOURCE
+#include <stdio.h>
+
 #include "audio-api.h"
 #include "audio-pulse.h"
 
-PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
+static struct alsa_info **alsa_info = NULL;
+static struct dev_ctx_pulse **dev_ctx_p = NULL;
+static unsigned int client_count = 0;
+
+
+unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
 
     pa_mainloop *pa_loop;
     pa_mainloop_api *pa_api;
@@ -55,12 +62,12 @@ PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
         pa_mainloop_iterate (pa_loop, 0, &ret);
 
         if (ret == -1) {
-            if (verbose) fprintf (stderr, "Stopping PulseAudio backend...\n");
-            return -1;
+                       fprintf (stderr, "Stopping PulseAudio backend...\n");
+            return 0;
         }
         if (ret >= 0) {
             /* found a matching sink from callback */
-            if (verbose) fprintf (stderr, "Success : using sink n.%d\n", error);
+            fprintf (stderr, "Success : using sink n.%d\n", error);
             ctx->audio_dev = (void*)dev_ctx_p[ret];
             break;
         }
@@ -68,36 +75,38 @@ 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;
     ctx->channels = (unsigned int)dev_ctx_p[ret]->volume.channels;
     for (i = 0; i < ctx->channels; i++)
-        ctx->volume[i] = (int)dev_ctx_p[ret]->volume.values[i];
+        ctx->volume[i] = dev_ctx_p[ret]->volume.values[i];
     ctx->idx = ret;
 
     /* open matching sink for playback */
     pa_spec = (pa_sample_spec*) malloc (sizeof(pa_sample_spec));
     pa_spec->format = PA_SAMPLE_S16LE;
     pa_spec->rate = 22050;
-    pa_spec->channels = ctx->channels;
+    pa_spec->channels = (uint8_t)ctx->channels;
 
     if (!(pa = pa_simple_new (NULL, "afb-audio-plugin", PA_STREAM_PLAYBACK, dev_ctx_p[ret]->sink_name,
                               "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);
 
     client_count++;
 
-    return 0;
+    fprintf (stderr, "Successfully initialized PulseAudio backend.\n");
+
+    return 1;
 }
 
-PUBLIC void _pulse_free (audioCtxHandleT *ctx) {
+void _pulse_free (audioCtxHandleT *ctx) {
 
     int num, i;
 
@@ -123,7 +132,7 @@ PUBLIC void _pulse_free (audioCtxHandleT *ctx) {
     }
 }
 
-PUBLIC void _pulse_play (audioCtxHandleT *ctx) {
+void _pulse_play (audioCtxHandleT *ctx) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
 
@@ -135,7 +144,7 @@ PUBLIC void _pulse_play (audioCtxHandleT *ctx) {
     pthread_create (&dev_ctx_p_c->thr, NULL, _pulse_play_thread_fn, (void*)dev_ctx_p_c);
 }
 
-PUBLIC void _pulse_stop (audioCtxHandleT *ctx) {
+void _pulse_stop (audioCtxHandleT *ctx) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
 
@@ -148,19 +157,19 @@ PUBLIC void _pulse_stop (audioCtxHandleT *ctx) {
     pthread_join (dev_ctx_p_c->thr, NULL);
 }
 
-PUBLIC int _pulse_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
+unsigned int _pulse_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
 
     if (!dev_ctx_p_c)
-        return;
+        return 0;
 
     _pulse_refresh_sink (dev_ctx_p_c);
 
-    return (int)dev_ctx_p_c->volume.values[channel];
+    return dev_ctx_p_c->volume.values[channel];
 }
 
-PUBLIC void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, int vol) {
+void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
     struct pa_cvolume volume;
@@ -176,7 +185,7 @@ PUBLIC void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, int v
     _pulse_refresh_sink (dev_ctx_p_c);
 }
 
-PUBLIC void _pulse_set_volume_all (audioCtxHandleT *ctx, int vol) {
+void _pulse_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
     struct pa_cvolume volume;
@@ -192,19 +201,19 @@ PUBLIC void _pulse_set_volume_all (audioCtxHandleT *ctx, int vol) {
     _pulse_refresh_sink (dev_ctx_p_c);
 }
 
-PUBLIC unsigned char _pulse_get_mute (audioCtxHandleT *ctx) {
+unsigned char _pulse_get_mute (audioCtxHandleT *ctx) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
 
     if (!dev_ctx_p_c)
-        return;
+        return 0;
 
     _pulse_refresh_sink (dev_ctx_p_c);
 
     return (unsigned char)dev_ctx_p_c->mute;
 }
 
-PUBLIC void _pulse_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
+void _pulse_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
 
     dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
 
@@ -328,7 +337,7 @@ char** _pulse_find_cards (const char *name) {
 
  /* ---- LOCAL CALLBACK FUNCTIONS ---- */
 
-STATIC void _pulse_context_cb (pa_context *context, void *data) {
+void _pulse_context_cb (pa_context *context, void *data) {
 
     pa_context_state_t state = pa_context_get_state (context);
     dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T *)data;
@@ -342,7 +351,7 @@ STATIC void _pulse_context_cb (pa_context *context, void *data) {
         pa_context_get_sink_info_list (context, _pulse_sink_list_cb, (void*)dev_ctx_p_t);
 }
 
-STATIC void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
+void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
                                 int eol, void *data) {
 
     dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T *)data;
@@ -369,7 +378,7 @@ STATIC void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
             for (i = 0; i < (sizeof(cards)/sizeof(char*)); i++) {
                 if (!strcmp (cards[i], dev_ctx_p_t->card_name[0])) {
                     /* it did : stop there and succeed */
-                    if (verbose) fprintf (stderr, "Found matching sink : %s\n", info->name);
+                    fprintf (stderr, "Found matching sink : %s\n", info->name);
                     pa_mainloop_quit (dev_ctx_p_t->pa_loop, num);
                 }
             }
@@ -396,13 +405,13 @@ STATIC void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
     for (i = 0; i < (sizeof(cards)/sizeof(char*)); i++) {
         if (!strcmp (cards[i], dev_ctx_p_t->card_name[0])) {
              /* it did : stop there and succeed */
-             if (verbose) fprintf (stderr, "Found matching sink : %s\n", info->name);
+             fprintf (stderr, "Found matching sink : %s\n", info->name);
              pa_mainloop_quit (dev_ctx_p_t->pa_loop, num);
         }
     }
 }
 
-STATIC void _pulse_sink_info_cb (pa_context *context, const pa_sink_info *info,
+void _pulse_sink_info_cb (pa_context *context, const pa_sink_info *info,
                                 int eol, void *data) {
 
     dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)data;
@@ -417,7 +426,7 @@ STATIC void _pulse_sink_info_cb (pa_context *context, const pa_sink_info *info,
 
  /* ---- LOCAL THREADED FUNCTIONS ---- */
 
-STATIC void* _pulse_play_thread_fn (void *ctx) {
+void* _pulse_play_thread_fn (void *ctx) {
 
     dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)ctx;
     FILE *file = NULL;