Code Review
/
src
/
app-framework-binder.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
Merge branch 'master' of https://github.com/iotbzh/afb-daemon
[src/app-framework-binder.git]
/
plugins
/
audio
/
audio-pulse.c
diff --git
a/plugins/audio/audio-pulse.c
b/plugins/audio/audio-pulse.c
index
d7988d4
..
739b377
100644
(file)
--- a/
plugins/audio/audio-pulse.c
+++ b/
plugins/audio/audio-pulse.c
@@
-21,7
+21,12
@@
#include "audio-api.h"
#include "audio-pulse.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;
pa_mainloop *pa_loop;
pa_mainloop_api *pa_api;
@@
-37,7
+42,7
@@
PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
/* allocate the global array if it hasn't been done */
if (!dev_ctx_p)
/* allocate the global array if it hasn't been done */
if (!dev_ctx_p)
- dev_ctx_p = (dev_ctx_pulse_T**) malloc (sizeof(dev_ctx_pulse_T));
+ dev_ctx_p = (dev_ctx_pulse_T**) malloc (sizeof(dev_ctx_pulse_T
*
));
/* create a temporary device, to be held until sink gets discovered */
dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T*) malloc (sizeof(dev_ctx_pulse_T));
/* create a temporary device, to be held until sink gets discovered */
dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T*) malloc (sizeof(dev_ctx_pulse_T));
@@
-53,17
+58,19
@@
PUBLIC 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);
/* 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) {
pa_mainloop_iterate (pa_loop, 0, &ret);
if (ret == -1) {
-
if (verbose)
fprintf (stderr, "Stopping PulseAudio backend...\n");
+
fprintf (stderr, "Stopping PulseAudio backend...\n");
return 0;
}
return 0;
}
- if (ret >= 0) {
+
+ /* 0 and >100 are returned by PulseAudio itself */
+ if ((ret > 0)&&(ret < 100)) {
/* found a matching sink from callback */
/* found a matching sink from callback */
-
if (verbose) fprintf (stderr, "Success : using sink n.%d\n", error
);
- ctx->audio_dev = (void*)dev_ctx_p[ret];
+
fprintf (stderr, "Success : using sink n.%d\n", ret-1
);
+ ctx->audio_dev = (void*)dev_ctx_p[ret
-1
];
break;
}
gettimeofday (&tv_now, NULL);
break;
}
gettimeofday (&tv_now, NULL);
@@
-73,11
+80,11
@@
PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
return 0;
/* make the client context aware of current card state */
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;
+ ctx->mute = (unsigned char)dev_ctx_p[ret
-1
]->mute;
+ ctx->channels = (unsigned int)dev_ctx_p[ret
-1
]->volume.channels;
for (i = 0; i < ctx->channels; i++)
for (i = 0; i < ctx->channels; i++)
- ctx->volume[i] = dev_ctx_p[ret]->volume.values[i];
- ctx->idx = ret;
+ ctx->volume[i] = dev_ctx_p[ret
-1
]->volume.values[i];
+ ctx->idx = ret
-1
;
/* open matching sink for playback */
pa_spec = (pa_sample_spec*) malloc (sizeof(pa_sample_spec));
/* open matching sink for playback */
pa_spec = (pa_sample_spec*) malloc (sizeof(pa_sample_spec));
@@
-85,30
+92,30
@@
PUBLIC unsigned char _pulse_init (const char *name, audioCtxHandleT *ctx) {
pa_spec->rate = 22050;
pa_spec->channels = (uint8_t)ctx->channels;
pa_spec->rate = 22050;
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,
+ if (!(pa = pa_simple_new (NULL, "afb-audio-plugin", PA_STREAM_PLAYBACK, dev_ctx_p[ret
-1
]->sink_name,
"afb-audio-output", pa_spec, NULL, NULL, &error))) {
fprintf (stderr, "Error opening PulseAudio sink %s : %s\n",
"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));
+ dev_ctx_p[ret
-1
]->sink_name, pa_strerror(error));
return 0;
}
return 0;
}
- dev_ctx_p[ret]->pa = pa;
+ dev_ctx_p[ret
-1
]->pa = pa;
free (pa_spec);
client_count++;
free (pa_spec);
client_count++;
-
if (verbose)
fprintf (stderr, "Successfully initialized PulseAudio backend.\n");
+ fprintf (stderr, "Successfully initialized PulseAudio backend.\n");
return 1;
}
return 1;
}
-
PUBLIC
void _pulse_free (audioCtxHandleT *ctx) {
+void _pulse_free (audioCtxHandleT *ctx) {
int num, i;
client_count--;
if (client_count > 0) return;
int num, i;
client_count--;
if (client_count > 0) return;
- for (num = 0; num < (sizeof(dev_ctx_p)/sizeof(dev_ctx_pulse_T)); num++) {
+ for (num = 0; num < (sizeof(dev_ctx_p)/sizeof(dev_ctx_pulse_T
*
)); num++) {
for (i = 0; num < (sizeof(dev_ctx_p[num]->card_name)/sizeof(char*)); i++) {
free (dev_ctx_p[num]->card_name[i]);
for (i = 0; num < (sizeof(dev_ctx_p[num]->card_name)/sizeof(char*)); i++) {
free (dev_ctx_p[num]->card_name[i]);
@@
-127,7
+134,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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
@@
-139,7
+146,7
@@
PUBLIC void _pulse_play (audioCtxHandleT *ctx) {
pthread_create (&dev_ctx_p_c->thr, NULL, _pulse_play_thread_fn, (void*)dev_ctx_p_c);
}
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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
@@
-152,7
+159,7
@@
PUBLIC void _pulse_stop (audioCtxHandleT *ctx) {
pthread_join (dev_ctx_p_c->thr, NULL);
}
pthread_join (dev_ctx_p_c->thr, NULL);
}
-
PUBLIC
unsigned 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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
@@
-164,7
+171,7
@@
PUBLIC unsigned int _pulse_get_volume (audioCtxHandleT *ctx, unsigned int channe
return 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, unsigned 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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
struct pa_cvolume volume;
@@
-180,7
+187,7
@@
PUBLIC void _pulse_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsig
_pulse_refresh_sink (dev_ctx_p_c);
}
_pulse_refresh_sink (dev_ctx_p_c);
}
-
PUBLIC
void _pulse_set_volume_all (audioCtxHandleT *ctx, unsigned 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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
struct pa_cvolume volume;
@@
-196,7
+203,7
@@
PUBLIC void _pulse_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) {
_pulse_refresh_sink (dev_ctx_p_c);
}
_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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
@@
-208,7
+215,7
@@
PUBLIC unsigned char _pulse_get_mute (audioCtxHandleT *ctx) {
return (unsigned char)dev_ctx_p_c->mute;
}
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;
dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev;
@@
-240,7
+247,7
@@
void _pulse_enumerate_cards () {
int new_info, i, num = 0;
/* allocate the global alsa array */
int new_info, i, num = 0;
/* allocate the global alsa array */
- alsa_info = (alsa_info_T**) malloc (sizeof(alsa_info_T));
+ alsa_info = (alsa_info_T**) malloc (sizeof(alsa_info_T
*
));
alsa_info[0] = (alsa_info_T*) malloc (sizeof(alsa_info_T));
alsa_info[0]->device = NULL;
alsa_info[0]->synonyms = NULL;
alsa_info[0] = (alsa_info_T*) malloc (sizeof(alsa_info_T));
alsa_info[0]->device = NULL;
alsa_info[0]->synonyms = NULL;
@@
-271,7
+278,7
@@
void _pulse_enumerate_cards () {
if (found) card_name[found-card_name] = '\0';
/* was the card name already listed in the global alsa array ? */
if (found) card_name[found-card_name] = '\0';
/* was the card name already listed in the global alsa array ? */
- for (i = 0; i < (sizeof(alsa_info)/sizeof(alsa_info_T)); i++) {
+ for (i = 0; i < (sizeof(alsa_info)/sizeof(alsa_info_T
*
)); i++) {
if (alsa_info[i]->device &&
!strcmp (alsa_info[i]->device, card_name)) {
if (alsa_info[i]->device &&
!strcmp (alsa_info[i]->device, card_name)) {
@@
-283,7
+290,7
@@
void _pulse_enumerate_cards () {
}
/* it was not ; create it */
if (new_info) {
}
/* it was not ; create it */
if (new_info) {
- alsa_info = (alsa_info_T**) realloc (alsa_info, (num+1)*sizeof(alsa_info_T));
+ alsa_info = (alsa_info_T**) realloc (alsa_info, (num+1)*sizeof(alsa_info_T
*
));
alsa_info[num]->device = strdup (card_name);
asprintf (&alsa_info[num]->synonyms, ":%s", alsa_name);
num++;
alsa_info[num]->device = strdup (card_name);
asprintf (&alsa_info[num]->synonyms, ":%s", alsa_name);
num++;
@@
-304,12
+311,12
@@
char** _pulse_find_cards (const char *name) {
asprintf (&needle, ":%s", name);
asprintf (&needle, ":%s", name);
- for (num = 0; num < (sizeof(alsa_info)/sizeof(alsa_info_T)); num++) {
+ for (num = 0; num < (sizeof(alsa_info)/sizeof(alsa_info_T
*
)); num++) {
found = strstr (alsa_info[num]->synonyms, needle);
while (found) {
/* if next character is not ':' or '\0', we are wrong */
found = strstr (alsa_info[num]->synonyms, needle);
while (found) {
/* if next character is not ':' or '\0', we are wrong */
- if ((found[strlen(name)
] != ':') && (found[strlen(name)
] != '\0')) {
+ if ((found[strlen(name)
+1] != ':') && (found[strlen(name)+1
] != '\0')) {
found = strstr (found+1, needle);
continue;
}
found = strstr (found+1, needle);
continue;
}
@@
-317,11
+324,12
@@
char** _pulse_find_cards (const char *name) {
found = strstr (alsa_info[num]->synonyms, ":");
while (found) {
next = strstr (found+1, ":");
found = strstr (alsa_info[num]->synonyms, ":");
while (found) {
next = strstr (found+1, ":");
+ if (!next) break;
cards = (char**) realloc (cards, (i+1)*sizeof(char*));
cards[i] = (char*) malloc (next-found+1);
strncpy (cards[i], found+1, next-found);
cards = (char**) realloc (cards, (i+1)*sizeof(char*));
cards[i] = (char*) malloc (next-found+1);
strncpy (cards[i], found+1, next-found);
- cards[i][next-found] = '\0';
- i++;
+ cards[i][next-found
-1
] = '\0';
+
found = next;
i++;
}
}
}
}
}
}
@@
-332,7
+340,7
@@
char** _pulse_find_cards (const char *name) {
/* ---- LOCAL CALLBACK FUNCTIONS ---- */
/* ---- 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;
pa_context_state_t state = pa_context_get_state (context);
dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T *)data;
@@
-346,25
+354,25
@@
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);
}
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;
const char *device_string;
int eol, void *data) {
dev_ctx_pulse_T *dev_ctx_p_t = (dev_ctx_pulse_T *)data;
const char *device_string;
+ char *device, *found;
char **cards;
int num, i;
if (eol != 0)
return;
char **cards;
int num, i;
if (eol != 0)
return;
+ device_string = pa_proplist_gets (info->proplist, "device.string");
/* ignore sinks with no cards */
/* ignore sinks with no cards */
- if (!
pa_proplist_contains (info->proplist, "device.string")
)
+ if (!
device_string
)
return;
return;
- device_string = pa_proplist_gets (info->proplist, "device.string");
-
/* was a sink with similar name already found ? */
/* was a sink with similar name already found ? */
- for (num = 0; num < (sizeof(dev_ctx_p)/sizeof(dev_ctx_pulse_T)); num++) {
+ for (num = 0; num < (sizeof(dev_ctx_p)/sizeof(dev_ctx_pulse_T
*
)); num++) {
if (dev_ctx_p[num]->sink_name &&
!strcmp (dev_ctx_p[num]->sink_name, info->name)) {
if (dev_ctx_p[num]->sink_name &&
!strcmp (dev_ctx_p[num]->sink_name, info->name)) {
@@
-373,8
+381,9
@@
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 */
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);
- pa_mainloop_quit (dev_ctx_p_t->pa_loop, num);
+ 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);
}
}
/* it did not, ignore and return */
}
}
/* it did not, ignore and return */
@@
-383,9
+392,16
@@
STATIC void _pulse_sink_list_cb (pa_context *context, const pa_sink_info *info,
}
num++;
}
num++;
+ /* remove ending ":0",":1"... in device name */
+ device = strdup (device_string);
+ found = strstr (device, ":");
+ if (found) device[found-device] = '\0';
+
/* new sink, find all the cards it manages, fail if none */
/* new sink, find all the cards it manages, fail if none */
- cards = _pulse_find_cards (device_string);
- if (!cards) return;
+ cards = _pulse_find_cards (device);
+ free (device);
+ if (!cards)
+ return;
/* everything is well, register it in global array */
dev_ctx_p_t->sink_name = strdup (info->name);
/* everything is well, register it in global array */
dev_ctx_p_t->sink_name = strdup (info->name);
@@
-400,13
+416,14
@@
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 */
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);
- pa_mainloop_quit (dev_ctx_p_t->pa_loop, num);
+ 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);
}
}
}
}
}
}
-
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;
int eol, void *data) {
dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)data;
@@
-421,7
+438,7
@@
STATIC void _pulse_sink_info_cb (pa_context *context, const pa_sink_info *info,
/* ---- LOCAL THREADED FUNCTIONS ---- */
/* ---- 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;
dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)ctx;
FILE *file = NULL;