Update Audio plugin, re-enable ALSA/Pulse linking
[src/app-framework-binder.git] / plugins / audio / audio-alsa.c
1 /*
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Manuel Bachmann"
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include "audio-api.h"
19 #include "audio-alsa.h"
20
21 snd_mixer_selem_channel_id_t SCHANNELS[8] = {
22  SND_MIXER_SCHN_FRONT_LEFT,
23  SND_MIXER_SCHN_FRONT_RIGHT,
24  SND_MIXER_SCHN_FRONT_CENTER,
25  SND_MIXER_SCHN_REAR_LEFT,
26  SND_MIXER_SCHN_REAR_RIGHT,
27  SND_MIXER_SCHN_REAR_CENTER,
28  SND_MIXER_SCHN_SIDE_LEFT,
29  SND_MIXER_SCHN_SIDE_RIGHT
30 };
31
32 static struct dev_ctx_alsa **dev_ctx_a = NULL;
33
34
35 unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
36
37     snd_pcm_t *dev;
38     snd_pcm_hw_params_t *params;
39     snd_mixer_t *mixer;
40     snd_mixer_selem_id_t *mixer_sid;
41     snd_mixer_elem_t *mixer_elm;
42     snd_mixer_elem_t *mixer_elm_m;
43     unsigned int rate = 22050;
44     long vol, vol_min, vol_max;
45     int num, i;
46
47     if (snd_pcm_open (&dev, name, SND_PCM_STREAM_PLAYBACK, 0) < 0)
48         return 0;
49
50     snd_pcm_hw_params_malloc (&params);
51     snd_pcm_hw_params_any (dev, params);
52     snd_pcm_hw_params_set_access (dev, params, SND_PCM_ACCESS_RW_INTERLEAVED);
53     snd_pcm_hw_params_set_format (dev, params, SND_PCM_FORMAT_S16_LE);
54     snd_pcm_hw_params_set_rate_near (dev, params, &rate, 0);
55     snd_pcm_hw_params_set_channels (dev, params, ctx->channels);
56     if (snd_pcm_hw_params (dev, params) < 0) {
57         snd_pcm_hw_params_free (params);
58         return 0;
59     }
60     snd_pcm_prepare (dev);
61
62     snd_mixer_open (&mixer, 0);
63     if (snd_mixer_attach (mixer, name) < 0) {
64         snd_pcm_hw_params_free (params);
65         return 0;
66     }
67     snd_mixer_selem_register (mixer, NULL, NULL);
68     snd_mixer_load (mixer);
69
70     snd_mixer_selem_id_alloca (&mixer_sid);
71     snd_mixer_selem_id_set_index (mixer_sid, 0);
72     snd_mixer_selem_id_set_name (mixer_sid, "Master");
73
74     mixer_elm = snd_mixer_find_selem (mixer, mixer_sid);
75     mixer_elm_m = NULL;
76
77     if (!mixer_elm) {
78         /* no "Master" mixer ; we are probably on a board... search ! */
79         for (mixer_elm = snd_mixer_first_elem (mixer); mixer_elm != NULL;
80              mixer_elm = snd_mixer_elem_next (mixer_elm)) {
81             if (snd_mixer_elem_info (mixer_elm) < 0)
82                 continue;
83             snd_mixer_selem_get_id (mixer_elm, mixer_sid);
84             if (strstr (snd_mixer_selem_id_get_name (mixer_sid), "DVC Out")) {
85
86                 /* this is Porter... let us found the specific mute switch */
87                 snd_mixer_selem_id_set_index (mixer_sid, 0);
88                 snd_mixer_selem_id_set_name (mixer_sid, "DVC Out Mute");
89                 mixer_elm_m = snd_mixer_find_selem (mixer, mixer_sid);
90
91                 break;
92             }
93         }
94     }
95
96     if (mixer_elm) {
97         snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
98         snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
99     }
100
101     /* allocate the global array if it hasn't been done */
102     if (!dev_ctx_a) {
103         dev_ctx_a = (dev_ctx_alsa_T**) malloc (sizeof(dev_ctx_alsa_T*));
104         dev_ctx_a[0] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
105         dev_ctx_a[0]->name = NULL;
106         dev_ctx_a[0]->dev = NULL;
107     }
108
109     /* is a card with similar name already opened ? */
110     for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) {
111         if (dev_ctx_a[num]->name &&
112            !strcmp (dev_ctx_a[num]->name, name))
113             return 0;
114     }
115     num++;
116
117     /* it's not... let us add it to the global array */
118     dev_ctx_a = (dev_ctx_alsa_T**) realloc (dev_ctx_a, (num+1)*sizeof(dev_ctx_alsa_T*));
119     dev_ctx_a[num] = (dev_ctx_alsa_T*) malloc (sizeof(dev_ctx_alsa_T));
120     dev_ctx_a[num]->name = strdup (name);
121     dev_ctx_a[num]->dev = dev;
122     dev_ctx_a[num]->params = params;
123     dev_ctx_a[num]->mixer_elm = mixer_elm;
124     dev_ctx_a[num]->mixer_elm_m = mixer_elm_m;
125     dev_ctx_a[num]->vol_max = vol_max;
126     dev_ctx_a[num]->vol = vol;
127     dev_ctx_a[num]->thr_should_run = 0;
128     dev_ctx_a[num]->thr_finished = 0;
129
130     /* make the client context aware of current card state */
131     for (i = 0; i < 8; i++)
132         ctx->volume[i] = _alsa_get_volume (num, i);
133     ctx->mute = _alsa_get_mute (num);
134     ctx->idx = num;
135     ctx->name = strdup (name);
136
137     fprintf (stderr, "Successfully initialized ALSA backend.\n");
138
139     return 1;
140 }
141
142 void _alsa_free (const char *name) {
143
144     int num;
145
146     for (num = 0; num < (sizeof(dev_ctx_a)/sizeof(dev_ctx_alsa_T*)); num++) {
147         if (dev_ctx_a[num]->name &&
148            !strcmp (dev_ctx_a[num]->name, name)) {
149             snd_pcm_close (dev_ctx_a[num]->dev);
150             snd_pcm_hw_params_free (dev_ctx_a[num]->params);
151             free (dev_ctx_a[num]->name);
152             dev_ctx_a[num]->dev = NULL;
153             dev_ctx_a[num]->name = NULL;
154             free (dev_ctx_a[num]);
155             return;
156         }
157     }
158 }
159
160 void _alsa_play (int num) {
161
162     if (!dev_ctx_a || !dev_ctx_a[num] || dev_ctx_a[num]->thr_should_run ||
163         access (AUDIO_BUFFER, F_OK) == -1)
164         return;
165
166     dev_ctx_a[num]->thr_should_run = 1;
167     dev_ctx_a[num]->thr_finished = 0;
168     pthread_create (&dev_ctx_a[num]->thr, NULL, _alsa_play_thread_fn, (void*)dev_ctx_a[num]);
169 }
170
171 void _alsa_stop (int num) {
172
173     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->thr_should_run)
174         return;
175
176     /* stop the "while" loop in thread */
177     dev_ctx_a[num]->thr_should_run = 0;
178
179     while (!dev_ctx_a[num]->thr_finished)
180         usleep(100000);
181
182     pthread_join (dev_ctx_a[num]->thr, NULL);
183 }
184
185 unsigned int _alsa_get_volume (int num, unsigned int channel) {
186
187     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
188         return 0;
189
190     snd_mixer_selem_get_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], &dev_ctx_a[num]->vol);
191
192     return (unsigned int)(dev_ctx_a[num]->vol*100)/dev_ctx_a[num]->vol_max;
193 }
194
195 void _alsa_set_volume (int num, unsigned int channel, unsigned int vol) {
196
197     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
198         vol > 100)
199         return;
200
201     snd_mixer_selem_set_playback_volume (dev_ctx_a[num]->mixer_elm, SCHANNELS[channel], (vol*dev_ctx_a[num]->vol_max)/100);
202 }
203
204 void _alsa_set_volume_all (int num, unsigned int vol) {
205
206     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm ||
207         vol > 100)
208
209     snd_mixer_selem_set_playback_volume_all (dev_ctx_a[num]->mixer_elm, (vol*dev_ctx_a[num]->vol_max)/100);
210 }
211
212 unsigned char _alsa_get_mute (int num) {
213
214     int mute = 0;
215     snd_mixer_elem_t *elm_m;
216
217     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm)
218         return 0;
219
220     dev_ctx_a[num]->mixer_elm_m ? (elm_m = dev_ctx_a[num]->mixer_elm_m) :
221                                   (elm_m = dev_ctx_a[num]->mixer_elm);
222
223     if (snd_mixer_selem_has_playback_switch (elm_m)) {
224         snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_LEFT, &mute);
225         snd_mixer_selem_get_playback_switch (elm_m, SND_MIXER_SCHN_FRONT_RIGHT, &mute);
226     }
227
228     if (dev_ctx_a[num]->mixer_elm_m)
229         return (unsigned char)mute;
230     else
231         return (unsigned char)!mute;
232 }
233
234 void _alsa_set_mute (int num, unsigned char tomute) {
235
236     snd_mixer_elem_t *elm_m;
237     int mute;
238
239     if (!dev_ctx_a || !dev_ctx_a[num] || !dev_ctx_a[num]->mixer_elm || 1 < tomute)
240         return;
241
242     if (dev_ctx_a[num]->mixer_elm_m) {
243         elm_m = dev_ctx_a[num]->mixer_elm_m;
244         mute = (int)!tomute;
245     } else {
246         elm_m = dev_ctx_a[num]->mixer_elm;
247         mute = (int)tomute;
248     }
249
250     if (snd_mixer_selem_has_playback_switch (elm_m))
251         snd_mixer_selem_set_playback_switch_all (elm_m, !mute);
252 }
253
254 void _alsa_set_rate (int num, unsigned int rate) {
255
256     if (!dev_ctx_a || !dev_ctx_a[num])
257         return;
258
259     snd_pcm_hw_params_set_rate_near (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, &rate, 0);
260 }
261
262 void _alsa_set_channels (int num, unsigned int channels) {
263
264     if (!dev_ctx_a || !dev_ctx_a[num])
265         return;
266
267     snd_pcm_hw_params_set_channels (dev_ctx_a[num]->dev, dev_ctx_a[num]->params, channels);
268 }
269
270  /* ---- LOCAL THREADED FUNCTIONS ---- */
271
272 void* _alsa_play_thread_fn (void *ctx) {
273
274     dev_ctx_alsa_T *dev_ctx_a = (dev_ctx_alsa_T *)ctx;
275     FILE *file = NULL;
276     char *buf = NULL;
277     long size;
278     int frames, res;
279
280     file = fopen (AUDIO_BUFFER, "rb");
281
282     while (dev_ctx_a->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
283         fseek (file, 0, SEEK_END);
284         size = ftell (file);
285         buf = (char*) realloc (buf, size * sizeof(char));
286         frames = (size * sizeof(char)) / 4;
287
288         fseek (file, 0, SEEK_SET);
289         fread (buf, 1, size, file);
290         fflush (file);
291
292         if ((res = snd_pcm_writei (dev_ctx_a->dev, buf, frames)) != frames) {
293             snd_pcm_recover (dev_ctx_a->dev, res, 0);
294             snd_pcm_prepare (dev_ctx_a->dev);
295         }
296         /* snd_pcm_drain (dev_ctx->dev); */
297     }
298     if (buf) free(buf);
299     if (file) fclose(file);
300
301     dev_ctx_a->thr_finished = 1;
302     return 0;
303 }