X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=plugins%2Faudio%2Faudio-alsa.c;h=45ff2edb760d17d9ef399f3578ac70de83c7393d;hb=ed6b81ea46629ae69176e77aec14844257dc36af;hp=bc777fce6896209a4866c7f6a335159820299efd;hpb=fcfb3c0951b6e2e06e7bf282e867576d8abb00c4;p=src%2Fapp-framework-binder.git diff --git a/plugins/audio/audio-alsa.c b/plugins/audio/audio-alsa.c index bc777fce..45ff2edb 100644 --- a/plugins/audio/audio-alsa.c +++ b/plugins/audio/audio-alsa.c @@ -1,22 +1,22 @@ /* - * Copyright (C) 2015 "IoT.bzh" + * Copyright (C) 2015, 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 . + * 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, @@ -29,7 +29,10 @@ snd_mixer_selem_channel_id_t SCHANNELS[8] = { SND_MIXER_SCHN_SIDE_RIGHT }; -PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { +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; @@ -41,8 +44,10 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { long vol, vol_min, vol_max; int num, i; - if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0) - return -1; + 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); @@ -52,14 +57,16 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { snd_pcm_hw_params_set_channels (dev, params, ctx->channels); if (snd_pcm_hw_params (dev, params) < 0) { snd_pcm_hw_params_free (params); - return -1; + 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); - return -1; + 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); @@ -97,23 +104,26 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { /* 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 = (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++) { + 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)) - return -1; + !strcmp (dev_ctx_a[num]->name, name)) { + fprintf (stderr, "Card '%s' already locked by other ALSA backend session\n", name); + return 0; + } } - num++; + 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)); - dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T)); + 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; @@ -129,17 +139,18 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) { ctx->volume[i] = _alsa_get_volume (num, i); ctx->mute = _alsa_get_mute (num); ctx->idx = num; + ctx->name = strdup (name); - if (verbose) fprintf (stderr, "Successfully initialized ALSA backend.\n"); + fprintf (stderr, "Successfully initialized ALSA backend.\n"); - return 0; + return 1; } -PUBLIC void _alsa_free (const char *name) { +void _alsa_free (const char *name) { int num; - for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T)); 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); @@ -153,7 +164,7 @@ PUBLIC void _alsa_free (const char *name) { } } -PUBLIC void _alsa_play (unsigned int num) { +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) @@ -164,7 +175,7 @@ PUBLIC void _alsa_play (unsigned int num) { pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]); } -PUBLIC void _alsa_stop (unsigned int num) { +void _alsa_stop (int num) { if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run) return; @@ -178,41 +189,41 @@ PUBLIC void _alsa_stop (unsigned int num) { pthread_join (dev_ctx_a[num]->thr, NULL); } -PUBLIC int _alsa_get_volume (unsigned int num, unsigned int channel) { +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; + return 0; snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol); - return (int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max; + return (unsigned int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max; } -PUBLIC void _alsa_set_volume (unsigned int num, unsigned int channel, int vol) { +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 || - 0 > vol > 100) + 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); - } -PUBLIC void _alsa_set_volume_all (unsigned int num, int vol) { +void _alsa_set_volume_all (int num, unsigned int vol) { if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || - 0 > vol > 100) + 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); } -PUBLIC unsigned char _alsa_get_mute (unsigned int num) { +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; + 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); @@ -228,12 +239,12 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) { return (unsigned char)!mute; } -PUBLIC void _alsa_set_mute (unsigned int num, unsigned char tomute) { +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 < 0) + 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) { @@ -248,7 +259,7 @@ PUBLIC void _alsa_set_mute (unsigned int num, unsigned char tomute) { snd_mixer_selem_set_playback_switch_all (elm_m, !mute); } -PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) { +void _alsa_set_rate (int num, unsigned int rate) { if (!dev_ctx_a || !dev_ctx_a[num]) return; @@ -256,7 +267,7 @@ PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) { snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0); } -PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) { +void _alsa_set_channels (int num, unsigned int channels) { if (!dev_ctx_a || !dev_ctx_a[num]) return; @@ -266,7 +277,7 @@ PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) { /* ---- LOCAL THREADED FUNCTIONS ---- */ -STATIC void* _alsa_play_thread_fn (void *ctx) { +void* _alsa_play_thread_fn (void *ctx) { dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx; FILE *file = NULL;