From 7059e59cddc1c81321639875636e88895bc14309 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Thu, 23 Jun 2016 20:34:57 +0200 Subject: vocabulary: moving from 'plugin' to 'binding' MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ic9e118df2bede1fefbb591f8ae7887266b7324ca Signed-off-by: José Bollo --- bindings/audio/CMakeLists.txt | 27 +++ bindings/audio/audio-alsa.c | 311 ++++++++++++++++++++++++++ bindings/audio/audio-alsa.h | 56 +++++ bindings/audio/audio-api.c | 383 ++++++++++++++++++++++++++++++++ bindings/audio/audio-api.h | 38 ++++ bindings/audio/audio-pulse.c | 491 ++++++++++++++++++++++++++++++++++++++++++ bindings/audio/audio-pulse.h | 66 ++++++ bindings/audio/export.map | 1 + 8 files changed, 1373 insertions(+) create mode 100644 bindings/audio/CMakeLists.txt create mode 100644 bindings/audio/audio-alsa.c create mode 100644 bindings/audio/audio-alsa.h create mode 100644 bindings/audio/audio-api.c create mode 100644 bindings/audio/audio-api.h create mode 100644 bindings/audio/audio-pulse.c create mode 100644 bindings/audio/audio-pulse.h create mode 100644 bindings/audio/export.map (limited to 'bindings/audio') diff --git a/bindings/audio/CMakeLists.txt b/bindings/audio/CMakeLists.txt new file mode 100644 index 00000000..11da28e1 --- /dev/null +++ b/bindings/audio/CMakeLists.txt @@ -0,0 +1,27 @@ +INCLUDE(FindPkgConfig) +PKG_CHECK_MODULES(alsa alsa) +PKG_CHECK_MODULES(pulseaudio libpulse libpulse-simple) +INCLUDE(FindThreads) +FIND_PACKAGE(Threads) + +IF(alsa_FOUND) + + MESSAGE(STATUS "ALSA found ; will compile Audio binding... (BINDING)") + + IF(pulseaudio_FOUND) + MESSAGE(STATUS "PulseAudio found ; Audio binding will have PulseAudio support") + ADD_DEFINITIONS(-DHAVE_PULSE=1) + SET(pulse_sources audio-pulse.c) + ENDIF(pulseaudio_FOUND) + + INCLUDE_DIRECTORIES(${include_dirs} ${alsa_INCLUDE_DIRS} ${pulseaudio_INCLUDE_DIRS}) + ADD_LIBRARY(audio-api MODULE audio-api.c audio-alsa.c ${pulse_sources}) + SET_TARGET_PROPERTIES(audio-api PROPERTIES + PREFIX "" + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/export.map" + ) + TARGET_LINK_LIBRARIES(audio-api ${link_libraries} ${alsa_LIBRARIES} ${pulseaudio_LIBRARIES}) + INSTALL(TARGETS audio-api + LIBRARY DESTINATION ${binding_install_dir}) + +ENDIF(alsa_FOUND) diff --git a/bindings/audio/audio-alsa.c b/bindings/audio/audio-alsa.c new file mode 100644 index 00000000..45ff2edb --- /dev/null +++ b/bindings/audio/audio-alsa.c @@ -0,0 +1,311 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#include "audio-api.h" +#include "audio-alsa.h" + +snd_mixer_selem_channel_id_t SCHANNELS[8] = { + SND_MIXER_SCHN_FRONT_LEFT, + SND_MIXER_SCHN_FRONT_RIGHT, + SND_MIXER_SCHN_FRONT_CENTER, + SND_MIXER_SCHN_REAR_LEFT, + SND_MIXER_SCHN_REAR_RIGHT, + SND_MIXER_SCHN_REAR_CENTER, + SND_MIXER_SCHN_SIDE_LEFT, + SND_MIXER_SCHN_SIDE_RIGHT +}; + +static struct dev_ctx_alsa **dev_ctx_a = NULL; + + +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; + snd_mixer_elem_t *mixer_elm_m; + unsigned int rate = 22050; + long vol, vol_min, vol_max; + int num, i; + + if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0) { + fprintf (stderr, "ALSA backend could not open card '%s'\n", name); + return 0; + } + + snd_pcm_hw_params_malloc (¶ms); + 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, &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); + fprintf (stderr, "ALSA backend could set channels on card '%s'\n", name); + return 0; + } + snd_pcm_prepare (dev); + + snd_mixer_open (&mixer, 0); + if (snd_mixer_attach (mixer, name) < 0) { + snd_pcm_hw_params_free (params); + fprintf (stderr, "ALSA backend could not open mixer for card '%s'\n", name); + 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); + 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; + mixer_elm = snd_mixer_elem_next (mixer_elm)) { + 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), "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; + } + } + } + + if (mixer_elm) { + 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_a) { + dev_ctx_a = (dev_ctx_alsa_T**) malloc (sizeof(dev_ctx_alsa_T*)); + dev_ctx_a[0] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T)); + dev_ctx_a[0]->name = NULL; + dev_ctx_a[0]->dev = NULL; + } + + /* is a card with similar name already opened ? */ + for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) { + if (dev_ctx_a[num]->name && + !strcmp (dev_ctx_a[num]->name, name)) { + fprintf (stderr, "Card '%s' already locked by other ALSA backend session\n", name); + return 0; + } + } + num--; + + /* it's not... let us add it to the global array */ + dev_ctx_a = (dev_ctx_alsa_T**) realloc (dev_ctx_a, (num+1)*sizeof(dev_ctx_alsa_T*)); + if (!dev_ctx_a[num]) + dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T)); + dev_ctx_a[num]->name = strdup (name); + dev_ctx_a[num]->dev = dev; + dev_ctx_a[num]->params = params; + dev_ctx_a[num]->mixer_elm = mixer_elm; + dev_ctx_a[num]->mixer_elm_m = mixer_elm_m; + dev_ctx_a[num]->vol_max = vol_max; + dev_ctx_a[num]->vol = vol; + dev_ctx_a[num]->thr_should_run = 0; + dev_ctx_a[num]->thr_finished = 0; + + /* make the client context aware of current card state */ + for (i = 0; i < 8; i++) + ctx->volume[i] = _alsa_get_volume (num, i); + ctx->mute = _alsa_get_mute (num); + ctx->idx = num; + ctx->name = strdup (name); + + fprintf (stderr, "Successfully initialized ALSA backend.\n"); + + return 1; +} + +void _alsa_free (const char *name) { + + int num; + + for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) { + if (dev_ctx_a[num]->name && + !strcmp (dev_ctx_a[num]->name, name)) { + snd_pcm_close (dev_ctx_a[num]->dev); + snd_pcm_hw_params_free (dev_ctx_a[num]->params); + free (dev_ctx_a[num]->name); + dev_ctx_a[num]->dev = NULL; + dev_ctx_a[num]->name = NULL; + free (dev_ctx_a[num]); + return; + } + } +} + +void _alsa_play (int num) { + + if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run || + access (AUDIO_BUFFER, F_OK) == -1) + return; + + dev_ctx_a[num]->thr_should_run = 1; + dev_ctx_a[num]->thr_finished = 0; + pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]); +} + +void _alsa_stop (int num) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run) + return; + + /* stop the "while" loop in thread */ + dev_ctx_a[num]->thr_should_run = 0; + + while (!dev_ctx_a[num]->thr_finished) + usleep(100000); + + pthread_join (dev_ctx_a[num]->thr, NULL); +} + +unsigned int _alsa_get_volume (int num, unsigned int channel) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm) + return 0; + + snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol); + + return (unsigned int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max; +} + +void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || + vol > 100) + return; + + snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100); +} + +void _alsa_set_volume_all (int num, unsigned int vol) { + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || + vol > 100) + + fflush (stdout); /* seems to force this logic to apply quickly */ + snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100); +} + +unsigned char _alsa_get_mute (int num) { + + int mute = 0; + snd_mixer_elem_t *elm_m; + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm) + return 0; + + dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) : + (elm_m = dev_ctx_a[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); + } + + if (dev_ctx_a[num]->mixer_elm_m) + return (unsigned char)mute; + else + return (unsigned char)!mute; +} + +void _alsa_set_mute (int num, unsigned char tomute) { + + snd_mixer_elem_t *elm_m; + int mute; + + if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute) + return; + + if (dev_ctx_a[num]->mixer_elm_m) { + elm_m = dev_ctx_a[num]->mixer_elm_m; + mute = (int)!tomute; + } else { + elm_m = dev_ctx_a[num]->mixer_elm; + mute = (int)tomute; + } + + if (snd_mixer_selem_has_playback_switch (elm_m)) + snd_mixer_selem_set_playback_switch_all (elm_m, !mute); +} + +void _alsa_set_rate (int num, unsigned int rate) { + + if (!dev_ctx_a || !dev_ctx_a[num]) + return; + + snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0); +} + +void _alsa_set_channels (int num, unsigned int channels) { + + if (!dev_ctx_a || !dev_ctx_a[num]) + return; + + snd_pcm_hw_params_set_channels (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, channels); +} + + /* ---- LOCAL THREADED FUNCTIONS ---- */ + +void* _alsa_play_thread_fn (void *ctx) { + + dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx; + FILE *file = NULL; + char *buf = NULL; + long size; + int frames, res; + + file = fopen (AUDIO_BUFFER, "rb"); + + while (dev_ctx_a->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) { + fseek (file, 0, SEEK_END); + size = ftell (file); + buf = (char*) realloc (buf, size * sizeof(char)); + frames = (size * sizeof(char)) / 4; + + fseek (file, 0, SEEK_SET); + fread (buf, 1, size, file); + fflush (file); + + if ((res = snd_pcm_writei (dev_ctx_a->dev, buf, frames)) != frames) { + snd_pcm_recover (dev_ctx_a->dev, res, 0); + snd_pcm_prepare (dev_ctx_a->dev); + } + /* snd_pcm_drain (dev_ctx->dev); */ + } + if (buf) free(buf); + if (file) fclose(file); + + dev_ctx_a->thr_finished = 1; + return 0; +} diff --git a/bindings/audio/audio-alsa.h b/bindings/audio/audio-alsa.h new file mode 100644 index 00000000..679d4eee --- /dev/null +++ b/bindings/audio/audio-alsa.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef AUDIO_ALSA_H +#define AUDIO_ALSA_H + +#include +#include + +#include "audio-api.h" + +#define AUDIO_BUFFER "/tmp/audio_buf" + +typedef struct dev_ctx_alsa dev_ctx_alsa_T; + +struct dev_ctx_alsa { + char *name; + 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; + unsigned char thr_should_run; + unsigned char thr_finished; +}; + +unsigned char _alsa_init (const char *, audioCtxHandleT *); +void _alsa_free (const char *); +void _alsa_play (int); +void _alsa_stop (int); +unsigned int _alsa_get_volume (int, unsigned int); +void _alsa_set_volume (int, unsigned int, unsigned int); +void _alsa_set_volume_all (int, unsigned int); +unsigned char _alsa_get_mute (int); +void _alsa_set_mute (int, unsigned char); +void _alsa_set_channels (int, unsigned int); + +void* _alsa_play_thread_fn (void *); + +#endif /* AUDIO_ALSA_H */ diff --git a/bindings/audio/audio-api.c b/bindings/audio/audio-api.c new file mode 100644 index 00000000..1ba9126c --- /dev/null +++ b/bindings/audio/audio-api.c @@ -0,0 +1,383 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 +#include + +#include "audio-api.h" +#include "audio-alsa.h" +#ifdef HAVE_PULSE +#include "audio-pulse.h" +#endif + +#include +#include + +/* ------ BACKEND FUNCTIONS ------- */ + +unsigned char _backend_init (const char *name, audioCtxHandleT *ctx) { + + char *backend_env = getenv ("AFB_AUDIO_OUTPUT"); + unsigned char res = 0; + +# ifdef HAVE_PULSE + if (!backend_env || (strcasecmp (backend_env, "Pulse") == 0)) + res = _pulse_init (name, ctx); + if (!res) +#endif + res = _alsa_init (name, ctx); + + if (!res) + fprintf (stderr, "Could not initialize Audio backend\n"); + + return res; +} + +void _backend_free (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_free (ctx); else +# endif + _alsa_free (ctx->name); +} + +void _backend_play (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_play (ctx); else +# endif + _alsa_play (ctx->idx); +} + +void _backend_stop (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_stop (ctx); else +# endif + _alsa_stop (ctx->idx); +} + +unsigned int _backend_get_volume (audioCtxHandleT *ctx, unsigned int channel) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) return _pulse_get_volume (ctx, channel); else +# endif + return _alsa_get_volume (ctx->idx, channel); +} + +void _backend_set_volume (audioCtxHandleT *ctx, unsigned int channel, unsigned int vol) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_set_volume (ctx, channel, vol); else +# endif + _alsa_set_volume (ctx->idx, channel, vol); +} + +void _backend_set_volume_all (audioCtxHandleT *ctx, unsigned int vol) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_set_volume_all (ctx, vol); else +# endif + _alsa_set_volume_all (ctx->idx, vol); +} + +unsigned char _backend_get_mute (audioCtxHandleT *ctx) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) return _pulse_get_mute (ctx); else +# endif + return _alsa_get_mute (ctx->idx); +} + +void _backend_set_mute (audioCtxHandleT *ctx, unsigned char mute) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) _pulse_set_mute (ctx, mute); else +# endif + _alsa_set_mute (ctx->idx, mute); +} + +void _backend_set_channels (audioCtxHandleT *ctx, unsigned int channels) { + +# ifdef HAVE_PULSE + if (ctx->audio_dev) return; else +# endif + _alsa_set_channels (ctx->idx, channels); +} + +/* ------ LOCAL HELPER FUNCTIONS --------- */ + +/* private client context constructor ; default values */ +static audioCtxHandleT* initAudioCtx () { + + audioCtxHandleT *ctx; + int i; + + ctx = malloc (sizeof(audioCtxHandleT)); + ctx->audio_dev = NULL; + ctx->name = NULL; + ctx->idx = -1; + for (i = 0; i < 8; i++) + ctx->volume[i] = 25; + ctx->channels = 2; + ctx->mute = 0; + ctx->is_playing = 0; + + return ctx; +} + +static void releaseAudioCtx (void *context) { + + audioCtxHandleT *ctx = (audioCtxHandleT*) context; + + /* power it off */ + _backend_free (ctx); + + /* clean client context */ + ctx->audio_dev = NULL; + if (ctx->name) + free (ctx->name); + ctx->idx = -1; + free (ctx); +} + + +/* ------ PUBLIC PLUGIN FUNCTIONS --------- */ + +static void init (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + json_object *jresp; + + /* create a private client context */ + if (!ctx) { + ctx = initAudioCtx(); + afb_req_context_set (request, ctx, releaseAudioCtx); + } + + if (!_backend_init ("default", ctx)) + afb_req_fail (request, "failed", "backend initialization failed"); + + jresp = json_object_new_object(); + json_object_object_add (jresp, "init", json_object_new_string ("success")); + afb_req_success (request, jresp, "Audio initialized"); +} + +static void volume (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + unsigned int volume[8], i; + char *volume_i; + char volume_str[256]; + size_t len_str = 0; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + for (i = 0; i < 8; i++) { + ctx->volume[i] = _backend_get_volume (ctx, i); + snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]); + len_str = strlen (volume_str); + } + json_object_object_add (jresp, "volume", json_object_new_string(volume_str)); + afb_req_success (request, jresp, "Audio - Volume obtained"); + return; + } + + /* "?value=" parameter, set volume */ + else { + volume_i = strdup (value); + volume_i = strtok (volume_i, ","); + volume[0] = (unsigned int) atoi (volume_i); + + if (100 < volume[0]) { + free (volume_i); + afb_req_fail (request, "failed", "volume must be between 0 and 100"); + return; + } + ctx->volume[0] = volume[0]; + _backend_set_volume (ctx, 0, ctx->volume[0]); + snprintf (volume_str, sizeof(volume_str), "%d,", ctx->volume[0]); + + for (i = 1; i < 8; i++) { + volume_i = strtok (NULL, ","); + /* if there is only one value, set all channels to this one */ + if (!volume_i && i == 1) + _backend_set_volume_all (ctx, ctx->volume[0]); + if (!volume_i || 100 < atoi(volume_i) || atoi(volume_i) < 0) + ctx->volume[i] = _backend_get_volume (ctx, i); + else { + ctx->volume[i] = (unsigned int) atoi(volume_i); + _backend_set_volume (ctx, i, ctx->volume[i]); + } + len_str = strlen(volume_str); + snprintf (volume_str+len_str, sizeof(volume_str)-len_str, "%d,", ctx->volume[i]); + } + free (volume_i); + json_object_object_add (jresp, "volume", json_object_new_string(volume_str)); + } + + afb_req_success (request, jresp, "Audio - Volume changed"); +} + +static void channels (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + char channels_str[256]; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* 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)); + afb_req_success (request, jresp, "Audio - Channels obtained"); + return; + } + + /* "?value=" parameter, set channels */ + else { + ctx->channels = (unsigned int) atoi (value); + _backend_set_channels (ctx, ctx->channels); + snprintf (channels_str, sizeof(channels_str), "%d", ctx->channels); + + jresp = json_object_new_object(); + json_object_object_add (jresp, "channels", json_object_new_string (channels_str)); + } + + afb_req_success (request, jresp, "Audio - Channels set"); +} + +static void mute (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + ctx->mute = _backend_get_mute (ctx); + ctx->mute ? + json_object_object_add (jresp, "mute", json_object_new_string ("on")) + : json_object_object_add (jresp, "mute", json_object_new_string ("off")); + afb_req_success (request, jresp, "Audio - Mute status obtained"); + return; + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + ctx->mute = 1; + _backend_set_mute (ctx, ctx->mute); + json_object_object_add (jresp, "mute", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) { + ctx->mute = 0; + _backend_set_mute (ctx, ctx->mute); + json_object_object_add (jresp, "mute", json_object_new_string ("off")); + } + + afb_req_success (request, jresp, "Audio - Mute set"); +} + +static void play (struct afb_req request) { /* AFB_SESSION_CHECK */ + + audioCtxHandleT *ctx = afb_req_context_get (request); + const char *value = afb_req_value (request, "value"); + json_object *jresp; + + if (!ctx) { + afb_req_fail (request, "failed", "you must call 'init' first"); + return; + } + jresp = json_object_new_object(); + + /* no "?value=" parameter : return current state */ + if (!value) { + ctx->is_playing ? + json_object_object_add (jresp, "play", json_object_new_string ("on")) + : json_object_object_add (jresp, "play", json_object_new_string ("off")); + afb_req_success (request, jresp, "Audio - Playing status obtained"); + return; + } + + /* "?value=" parameter is "1" or "true" */ + else if ( atoi(value) == 1 || !strcasecmp(value, "true") ) { + ctx->is_playing = 1; + _backend_play (ctx); + json_object_object_add (jresp, "play", json_object_new_string ("on")); + } + + /* "?value=" parameter is "0" or "false" */ + else if ( atoi(value) == 0 || !strcasecmp(value, "false") ) { + ctx->is_playing = 0; + _backend_stop (ctx); + json_object_object_add (jresp, "play", json_object_new_string ("off")); + } + + afb_req_success (request, jresp, "Audio - Play"); +} + +static void ping (struct afb_req request) { /* AFB_SESSION_NONE */ + afb_req_success (request, NULL, "Audio - Ping success"); +} + +static const struct AFB_verb_desc_v1 verbs[] = { + {"init" , AFB_SESSION_CHECK, init , "Audio API - init"}, + {"volume" , AFB_SESSION_CHECK, volume , "Audio API - volume"}, + {"channels", AFB_SESSION_CHECK, channels , "Audio API - channels"}, + {"mute" , AFB_SESSION_CHECK, mute , "Audio API - mute"}, + {"play" , AFB_SESSION_CHECK, play , "Audio API - play"}, + {"ping" , AFB_SESSION_NONE, ping , "Audio API - ping"}, + {NULL} +}; + +static const struct AFB_plugin pluginDesc = { + .type = AFB_PLUGIN_VERSION_1, + .v1 = { + .info = "Application Framework Binder - Audio plugin", + .prefix = "audio", + .verbs = verbs + } +}; + +const struct AFB_plugin *pluginAfbV1Register (const struct AFB_interface *itf) +{ + return &pluginDesc; +} diff --git a/bindings/audio/audio-api.h b/bindings/audio/audio-api.h new file mode 100644 index 00000000..f5c77c2e --- /dev/null +++ b/bindings/audio/audio-api.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015, 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef AUDIO_API_H +#define AUDIO_API_H + +/* global plugin handle, should store everything we may need */ +typedef struct { + int devCount; +} pluginHandleT; + +/* private client context [will be destroyed when client leaves] */ +typedef struct { + void *audio_dev; /* handle to implementation (ALSA, PulseAudio...) */ + char *name; /* name of the audio card */ + int idx; /* audio card index within global array */ + unsigned int volume[8]; /* audio volume (8 channels) : 0-100 */ + unsigned int channels; /* audio channels : 1(mono)/2(stereo)... */ + unsigned char mute; /* audio muted : 0(false)/1(true) */ + unsigned char is_playing; /* audio is playing: 0(false)/1(true) */ +} audioCtxHandleT; + + +#endif /* AUDIO_API_H */ diff --git a/bindings/audio/audio-pulse.c b/bindings/audio/audio-pulse.c new file mode 100644 index 00000000..34643603 --- /dev/null +++ b/bindings/audio/audio-pulse.c @@ -0,0 +1,491 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 + +#include "audio-api.h" +#include "audio-pulse.h" + +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_context *pa_context; + pa_simple *pa; + pa_sample_spec *pa_spec; + struct timeval tv_start, tv_now; + int ret, error, i; + + pa_loop = pa_mainloop_new (); + pa_api = pa_mainloop_get_api (pa_loop); + pa_context = pa_context_new (pa_api, "afb-audio-plugin"); + + /* 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*)); + + /* 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)); + dev_ctx_p_t->sink_name = NULL; + dev_ctx_p_t->card_name = (char**) malloc (sizeof(char*)); + dev_ctx_p_t->card_name[0] = strdup (name); + dev_ctx_p_t->pa_loop = pa_loop; + dev_ctx_p_t->pa_context = pa_context; + + pa_context_set_state_callback (pa_context, _pulse_context_cb, (void*)dev_ctx_p_t); + pa_context_connect (pa_context, NULL, 0, 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 <= 2) { + pa_mainloop_iterate (pa_loop, 0, &ret); + + if (ret == -1) { /* generic error */ + fprintf (stderr, "Stopping PulseAudio backend...\n"); + return 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]; + break; + } + gettimeofday (&tv_now, NULL); + } + /* fail if we found no matching sink */ + if (!ctx->audio_dev) + return 0; + + /* make the client context aware of current card state */ + 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++) + 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)); + pa_spec->format = PA_SAMPLE_S16LE; + 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-1]->sink_name, + "afb-audio-output", pa_spec, NULL, NULL, &error))) { + fprintf (stderr, "Error opening PulseAudio sink %s : %s\n", + dev_ctx_p[ret-1]->sink_name, pa_strerror(error)); + return 0; + } + dev_ctx_p[ret-1]->pa = pa; + free (pa_spec); + + client_count++; + + fprintf (stderr, "Successfully initialized PulseAudio backend.\n"); + + return 1; +} + +void _pulse_free (audioCtxHandleT *ctx) { + + 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 (i = 0; num < (sizeof(dev_ctx_p[num]->card_name)/sizeof(char*)); i++) { + free (dev_ctx_p[num]->card_name[i]); + dev_ctx_p[num]->card_name[i] = NULL; + } + pa_context_disconnect (dev_ctx_p[num]->pa_context); + pa_context_unref (dev_ctx_p[num]->pa_context); + pa_mainloop_free (dev_ctx_p[num]->pa_loop); + pa_simple_free (dev_ctx_p[num]->pa); + free (dev_ctx_p[num]->sink_name); + dev_ctx_p[num]->pa_context = NULL; + dev_ctx_p[num]->pa_loop = NULL; + dev_ctx_p[num]->pa = NULL; + dev_ctx_p[num]->sink_name = NULL; + free (dev_ctx_p[num]); + } +} + +void _pulse_play (audioCtxHandleT *ctx) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c || dev_ctx_p_c->thr_should_run || access (AUDIO_BUFFER, F_OK) == -1) + return; + + dev_ctx_p_c->thr_should_run = 1; + dev_ctx_p_c->thr_finished = 0; + pthread_create (&dev_ctx_p_c->thr, NULL, _pulse_play_thread_fn, (void*)dev_ctx_p_c); +} + +void _pulse_stop (audioCtxHandleT *ctx) { + + dev_ctx_pulse_T* dev_ctx_p_c = (dev_ctx_pulse_T*)ctx->audio_dev; + + if (!dev_ctx_p_c || !dev_ctx_p_c->thr_should_run) + return; + + dev_ctx_p_c->thr_should_run = 0; + while (!dev_ctx_p_c->thr_finished) + usleep(100000); + pthread_join (dev_ctx_p_c->thr, NULL); +} + +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 0; + + _pulse_refresh_sink (dev_ctx_p_c); + + return (dev_ctx_p_c->volume.values[channel]*100)/PA_VOLUME_NORM; +} + +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; + + if (!dev_ctx_p_c) + return; + + volume = dev_ctx_p_c->volume; + 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); + _pulse_refresh_sink (dev_ctx_p_c); +} + +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; + + if (!dev_ctx_p_c) + return; + + pa_cvolume_init (&volume); + pa_cvolume_set (&volume, dev_ctx_p_c->volume.channels, vol); + + pa_context_set_sink_volume_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name, + &volume, NULL, NULL); + _pulse_refresh_sink (dev_ctx_p_c); +} + +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 0; + + _pulse_refresh_sink (dev_ctx_p_c); + + return (unsigned char)dev_ctx_p_c->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; + + if (!dev_ctx_p_c) + return; + + pa_context_set_sink_mute_by_name (dev_ctx_p_c->pa_context, dev_ctx_p_c->sink_name, + (int)mute, NULL, NULL); + _pulse_refresh_sink (dev_ctx_p_c); +} + + /* ---- LOCAL HELPER FUNCTIONS ---- */ + +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; + + 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); +} + +void _pulse_enumerate_cards () { + + void **cards, **card; + char *name, *found, *alsa_name, *card_name; + int new_info, i, num = 0; + + /* allocate the global alsa array */ + 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; + + /* we use ALSA to enumerate cards */ + snd_device_name_hint (-1, "pcm", &cards); + card = cards; + + for (; *card != NULL; card++) { + name = snd_device_name_get_hint (*card, "NAME"); + new_info = 1; + + /* alsa name is before ':' (if ':' misses, then it has no card) */ + found = strstr (name, ":"); + if (!found) continue; + /* */ + alsa_name = (char*) malloc (found-name+1); + strncpy (alsa_name, name, found-name); + alsa_name[found-name] = '\0'; + + /* card name is the invariant between "CARD=" and ',' */ + found = strstr (name, "CARD="); + if (!found) continue; + /* */ + found += 5; + card_name = strdup (found); + found = strstr (card_name, ","); + 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++) { + + if (alsa_info[i]->device && + !strcmp (alsa_info[i]->device, card_name)) { + /* it was ; add the alsa name as a new synonym */ + asprintf (&alsa_info[i]->synonyms, "%s:%s", alsa_info[i]->synonyms, alsa_name); + new_info = 0; + break; + } + } + /* 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++; + } + free (alsa_name); + free (card_name); + } +} + +char** _pulse_find_cards (const char *name) { + + char **cards = NULL; + char *needle, *found, *next; + int num, i = 0; + + if (!alsa_info) + _pulse_enumerate_cards (); + + asprintf (&needle, ":%s", name); + + 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 */ + if ((found[strlen(name)+1] != ':') && (found[strlen(name)+1] != '\0')) { + found = strstr (found+1, needle); + continue; + } + /* found it ; now return all the "synonym" cards */ + 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[i][next-found-1] = '\0'; + found = next; i++; + } + } + } + free (needle); + + return cards; +} + + /* ---- LOCAL CALLBACK FUNCTIONS ---- */ + +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; + + 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) + pa_context_get_sink_info_list (context, _pulse_sink_list_cb, (void*)dev_ctx_p_t); +} + +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; + char *device, *found; + char **cards; + int num, i; + + if (eol != 0) + return; + + device_string = pa_proplist_gets (info->proplist, "device.string"); + /* ignore sinks with no cards */ + if (!device_string) + return; + + /* was a sink with similar name already found ? */ + 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)) { + + /* yet it was, did it have the required card ? */ + cards = dev_ctx_p[num]->card_name; + 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 */ + 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 */ + return; + } + } + 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 */ + 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); + dev_ctx_p_t->card_name = cards; + dev_ctx_p_t->mute = info->mute; + dev_ctx_p_t->volume = info->volume; + dev_ctx_p_t->thr_should_run = 0; + dev_ctx_p_t->thr_finished = 0; + dev_ctx_p[num] = dev_ctx_p_t; + + /* does this new sink have the card we are looking for ? */ /* TODO : factorize this */ + 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 */ + 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); + } + } +} + +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; + + if (eol != 0) + return; + + dev_ctx_p_c->refresh = 0; + dev_ctx_p_c->mute = info->mute; + dev_ctx_p_c->volume = info->volume; +} + + /* ---- LOCAL THREADED FUNCTIONS ---- */ + +void* _pulse_play_thread_fn (void *ctx) { + + dev_ctx_pulse_T *dev_ctx_p_c = (dev_ctx_pulse_T *)ctx; + FILE *file = NULL; + char *buf = NULL; + long size; + int error; + + file = fopen (AUDIO_BUFFER, "rb"); + + while (dev_ctx_p_c->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) { + fseek (file, 0, SEEK_END); + size = ftell (file); + buf = (char*) realloc (buf, size * sizeof(char)); + + fseek (file, 0, SEEK_SET); + fread (buf, 1, size, file); + fflush (file); + + if (pa_simple_write (dev_ctx_p_c->pa, buf, size*2, &error) < 0) + fprintf (stderr, "Error writing to PulseAudio : %s\n", pa_strerror (error)); + /* pa_simple_drain (dev_ctx_p_c->pa); */ + } + if (buf) free(buf); + if (file) fclose(file); + + dev_ctx_p_c->thr_finished = 1; + return 0; +} diff --git a/bindings/audio/audio-pulse.h b/bindings/audio/audio-pulse.h new file mode 100644 index 00000000..ad8ff490 --- /dev/null +++ b/bindings/audio/audio-pulse.h @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2016 "IoT.bzh" + * Author "Manuel Bachmann" + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +#ifndef AUDIO_PULSE_H +#define AUDIO_PULSE_H + +#include +#include +#include +#include + +#include "audio-alsa.h" + +typedef struct dev_ctx_pulse dev_ctx_pulse_T; +typedef struct alsa_info alsa_info_T; + +struct dev_ctx_pulse { + char *sink_name; + char **card_name; + pa_mainloop *pa_loop; + pa_context *pa_context; + pa_simple *pa; + pa_cvolume volume; + int mute; + unsigned char refresh; + pthread_t thr; + unsigned char thr_should_run; + unsigned char thr_finished; +}; + +struct alsa_info { + char *device; + char *synonyms; +}; + +unsigned char _pulse_init (const char *, audioCtxHandleT *); +void _pulse_free (audioCtxHandleT *); +void _pulse_play (audioCtxHandleT *); +void _pulse_stop (audioCtxHandleT *); +unsigned int _pulse_get_volume (audioCtxHandleT *, unsigned int); +void _pulse_set_volume (audioCtxHandleT *, unsigned int, unsigned int); +void _pulse_set_volume_all (audioCtxHandleT *, unsigned int); +unsigned char _pulse_get_mute (audioCtxHandleT *); +void _pulse_set_mute (audioCtxHandleT *, unsigned char); + +void _pulse_context_cb (pa_context *, void *); +void _pulse_sink_list_cb (pa_context *, const pa_sink_info *, int, void *); +void _pulse_sink_info_cb (pa_context *, const pa_sink_info *, int, void *); +void* _pulse_play_thread_fn (void *); +void _pulse_refresh_sink (dev_ctx_pulse_T *); + +#endif /* AUDIO_PULSE_H */ diff --git a/bindings/audio/export.map b/bindings/audio/export.map new file mode 100644 index 00000000..0ef1ac79 --- /dev/null +++ b/bindings/audio/export.map @@ -0,0 +1 @@ +{ global: afbBindingV1Register; local: *; }; -- cgit 1.2.3-korg