add .gitreview
[src/app-framework-binder.git] / plugins / audio / audio-pulse.c
index 50ddf40..3464360 100644 (file)
@@ -58,14 +58,15 @@ unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
     /* 1 second should be sufficient to retrieve sink info */
     gettimeofday (&tv_start, NULL);
     gettimeofday (&tv_now, NULL);
-    while (tv_now.tv_sec - tv_start.tv_sec <= 1) {
+    while (tv_now.tv_sec - tv_start.tv_sec <= 2) {
         pa_mainloop_iterate (pa_loop, 0, &ret);
 
-        if (ret == -1) {
-                       fprintf (stderr, "Stopping PulseAudio backend...\n");
+        if (ret == -1) { /* generic error */
+            fprintf (stderr, "Stopping PulseAudio backend...\n");
             return 0;
         }
-        if (ret > 0) {
+
+        if ((ret > 0)&&(ret < 100)) { /* 0 and >100 are PulseAudio codes */
             /* found a matching sink from callback */
             fprintf (stderr, "Success : using sink n.%d\n", ret-1);
             ctx->audio_dev = (void*)dev_ctx_p[ret-1];
@@ -166,7 +167,7 @@ unsigned int _pulse_get_volume (audioCtxHandleT *ctx, unsigned int channel) {
 
     _pulse_refresh_sink (dev_ctx_p_c);
 
-    return dev_ctx_p_c->volume.values[channel];
+    return (dev_ctx_p_c->volume.values[channel]*100)/PA_VOLUME_NORM;
 }
 
 void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) {
@@ -178,7 +179,7 @@ void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int
         return;
 
     volume = dev_ctx_p_c->volume;
-    volume.values[channel] = vol;
+    volume.values[channel] = (vol*PA_VOLUME_NORM)/100;
 
     pa_context_set_sink_volume_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name,
                                         &volume, NULL, NULL);
@@ -229,10 +230,23 @@ void _pulse_set_mute (audioCtxHandleT *ctx, unsigned char mute) {
 
 void _pulse_refresh_sink (dev_ctx_pulse_T* dev_ctx_p_c) {
 
+    pa_mainloop_api *pa_api;
+
+    dev_ctx_p_c->pa_loop = pa_mainloop_new ();
+    pa_api = pa_mainloop_get_api (dev_ctx_p_c->pa_loop);
+    dev_ctx_p_c->pa_context = pa_context_new (pa_api, "afb-audio-plugin");
+
     dev_ctx_p_c->refresh = 1;
 
-    pa_context_get_sink_info_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name,
-                                      _pulse_sink_info_cb, (void*)dev_ctx_p_c);
+    switch (pa_context_get_state (dev_ctx_p_c->pa_context)) {
+      case PA_CONTEXT_READY:
+        pa_context_get_sink_info_by_name (dev_ctx_p_c->pa_context,
+                                          dev_ctx_p_c->sink_name,
+                                          _pulse_sink_info_cb, (void*)dev_ctx_p_c);
+        break;
+      default:
+        return;
+    }
 
     while (dev_ctx_p_c->refresh)
         pa_mainloop_iterate (dev_ctx_p_c->pa_loop, 0, NULL);
@@ -289,6 +303,7 @@ void _pulse_enumerate_cards () {
         /* it was not ; create it */
         if (new_info) {
             alsa_info = (alsa_info_T**) realloc (alsa_info, (num+1)*sizeof(alsa_info_T*));
+            alsa_info[num] = (alsa_info_T*) malloc (sizeof(alsa_info_T));
             alsa_info[num]->device = strdup (card_name);
             asprintf (&alsa_info[num]->synonyms, ":%s", alsa_name);
             num++;
@@ -346,6 +361,9 @@ void _pulse_context_cb (pa_context *context, void *data) {
     if (state == PA_CONTEXT_FAILED) {
         fprintf (stderr, "Could not connect to PulseAudio !\n");
         pa_mainloop_quit (dev_ctx_p_t->pa_loop, -1);
+        pa_context_disconnect (dev_ctx_p_t->pa_context);
+        pa_context_unref (dev_ctx_p_t->pa_context);
+        pa_mainloop_free (dev_ctx_p_t->pa_loop);
     }
 
     if (state == PA_CONTEXT_READY)
@@ -382,6 +400,9 @@ void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
                     fprintf (stderr, "Found matching sink : %s\n", info->name);
                     /* we return num+1 because '0' is already used */
                     pa_mainloop_quit (dev_ctx_p_t->pa_loop, num+1);
+                    pa_context_disconnect (dev_ctx_p_t->pa_context);
+                    pa_context_unref (dev_ctx_p_t->pa_context);
+                    pa_mainloop_free (dev_ctx_p_t->pa_loop);
                 }
             }
             /* it did not, ignore and return */
@@ -395,7 +416,6 @@ void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
     found = strstr (device, ":");
     if (found) device[found-device] = '\0';
 
-fprintf(stderr, "BEFORE PULSE_FIND_CARDS\n");
     /* new sink, find all the cards it manages, fail if none */
     cards = _pulse_find_cards (device);
     free (device);
@@ -403,7 +423,6 @@ fprintf(stderr, "BEFORE PULSE_FIND_CARDS\n");
         return;
 
     /* everything is well, register it in global array */
-fprintf(stderr, "NEW SINK IN GLOBAL ARRAY\n");
     dev_ctx_p_t->sink_name = strdup (info->name);
     dev_ctx_p_t->card_name = cards;
     dev_ctx_p_t->mute = info->mute;
@@ -419,6 +438,9 @@ fprintf(stderr, "NEW SINK IN GLOBAL ARRAY\n");
              fprintf (stderr, "Found matching sink : %s\n", info->name);
              /* we return num+1 because '0' is already used */
              pa_mainloop_quit (dev_ctx_p_t->pa_loop, num+1);
+             pa_context_disconnect (dev_ctx_p_t->pa_context);
+             pa_context_unref (dev_ctx_p_t->pa_context);
+             pa_mainloop_free (dev_ctx_p_t->pa_loop);
         }
     }
 }