b30a7bba794820259ce6fb294a5f06c32b52bc65
[framework/multimedia/gst-plugins-base0.10.git] / ext / alsa / gstalsamixer.c
1 /* ALSA mixer implementation.
2  * Copyright (C) 2003 Leif Johnson <leif@ambient.2y.net>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /**
21  * SECTION:element-alsamixer
22  * @see_also: alsasink, alsasrc
23  *
24  * This element controls various aspects such as the volume and balance
25  * of an audio device using the ALSA api.
26  *
27  * The application should query and use the interfaces provided by this 
28  * element to control the device.
29  *
30  * Last reviewed on 2006-03-01 (0.10.4)
31  */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "gstalsamixer.h"
38 #include <errno.h>
39
40 static void gst_alsa_mixer_update_option (GstAlsaMixer * mixer,
41     GstAlsaMixerOptions * alsa_opts);
42 static void gst_alsa_mixer_update_track (GstAlsaMixer * mixer,
43     GstAlsaMixerTrack * alsa_track);
44 static int gst_alsa_mixer_handle_callback (snd_mixer_t * handle,
45     unsigned int mask, snd_mixer_elem_t * elem);
46
47 /* First some utils, then the mixer implementation */
48 static gboolean
49 gst_alsa_mixer_open (GstAlsaMixer * mixer)
50 {
51   gint err;
52   snd_ctl_t *ctl;
53   snd_ctl_card_info_t *card_info;
54
55   g_return_val_if_fail (mixer->handle == NULL, FALSE);
56
57   /* open and initialize the mixer device */
58   err = snd_mixer_open (&mixer->handle, 0);
59   if (err < 0 || mixer->handle == NULL)
60     goto open_failed;
61
62   if ((err = snd_mixer_attach (mixer->handle, mixer->device)) < 0) {
63     GST_WARNING ("Cannot open mixer for sound device '%s': %s", mixer->device,
64         snd_strerror (err));
65     goto error;
66   }
67
68   if ((err = snd_mixer_selem_register (mixer->handle, NULL, NULL)) < 0) {
69     GST_WARNING ("Cannot register mixer elements: %s", snd_strerror (err));
70     goto error;
71   }
72
73   if ((err = snd_mixer_load (mixer->handle)) < 0) {
74     GST_WARNING ("Cannot load mixer settings: %s", snd_strerror (err));
75     goto error;
76   }
77
78   snd_mixer_set_callback_private (mixer->handle, mixer);
79   snd_mixer_set_callback (mixer->handle, gst_alsa_mixer_handle_callback);
80
81   /* now get the device name, any of this is not fatal */
82   g_free (mixer->cardname);
83   if ((err = snd_ctl_open (&ctl, mixer->device, 0)) < 0) {
84     GST_WARNING ("Cannot open CTL: %s", snd_strerror (err));
85     goto no_card_name;
86   }
87
88   snd_ctl_card_info_malloc (&card_info);
89   if ((err = snd_ctl_card_info (ctl, card_info)) < 0) {
90     GST_WARNING ("Cannot get card info: %s", snd_strerror (err));
91     snd_ctl_close (ctl);
92     goto no_card_name;
93   }
94
95   mixer->cardname = g_strdup (snd_ctl_card_info_get_name (card_info));
96   GST_DEBUG ("Card name = %s", GST_STR_NULL (mixer->cardname));
97   snd_ctl_card_info_free (card_info);
98   snd_ctl_close (ctl);
99
100 no_card_name:
101   if (mixer->cardname == NULL) {
102     mixer->cardname = g_strdup ("Unknown");
103     GST_DEBUG ("Cannot find card name");
104   }
105
106   GST_INFO ("Successfully opened mixer for device '%s'.", mixer->device);
107
108   return TRUE;
109
110   /* ERROR */
111 open_failed:
112   {
113     GST_WARNING ("Cannot open mixer: %s", snd_strerror (err));
114     mixer->handle = NULL;
115     return FALSE;
116   }
117 error:
118   {
119     snd_mixer_close (mixer->handle);
120     mixer->handle = NULL;
121     return FALSE;
122   }
123 }
124
125 static snd_mixer_elem_t *
126 gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
127 {
128   snd_mixer_elem_t *element;
129   gint i, count;
130
131   count = snd_mixer_get_count (handle);
132
133   g_static_rec_mutex_lock (mixer->rec_mutex);
134
135   /* Check if we have a playback mixer labelled as 'Master' */
136   element = snd_mixer_first_elem (handle);
137   for (i = 0; i < count; i++) {
138     if (snd_mixer_selem_has_playback_volume (element) &&
139         strcmp (snd_mixer_selem_get_name (element), "Master") == 0) {
140       g_static_rec_mutex_unlock (mixer->rec_mutex);
141       return element;
142     }
143     element = snd_mixer_elem_next (element);
144   }
145
146   /* If not, check if we have a playback mixer labelled as 'Front' */
147   element = snd_mixer_first_elem (handle);
148   for (i = 0; i < count; i++) {
149     if (snd_mixer_selem_has_playback_volume (element) &&
150         strcmp (snd_mixer_selem_get_name (element), "Front") == 0) {
151       g_static_rec_mutex_unlock (mixer->rec_mutex);
152       return element;
153     }
154     element = snd_mixer_elem_next (element);
155   }
156
157   /* If not, check if we have a playback mixer labelled as 'PCM' */
158   element = snd_mixer_first_elem (handle);
159   for (i = 0; i < count; i++) {
160     if (snd_mixer_selem_has_playback_volume (element) &&
161         strcmp (snd_mixer_selem_get_name (element), "PCM") == 0) {
162       g_static_rec_mutex_unlock (mixer->rec_mutex);
163       return element;
164     }
165     element = snd_mixer_elem_next (element);
166   }
167
168   /* If not, check if we have a playback mixer labelled as 'Speaker' */
169   element = snd_mixer_first_elem (handle);
170   for (i = 0; i < count; i++) {
171     if (snd_mixer_selem_has_playback_volume (element) &&
172         strcmp (snd_mixer_selem_get_name (element), "Speaker") == 0) {
173       g_static_rec_mutex_unlock (mixer->rec_mutex);
174       return element;
175     }
176     element = snd_mixer_elem_next (element);
177   }
178
179   /* If not, check if we have a playback mixer with both volume and switch that
180    * is not mono */
181   element = snd_mixer_first_elem (handle);
182   for (i = 0; i < count; i++) {
183     if (snd_mixer_selem_has_playback_volume (element) &&
184         snd_mixer_selem_has_playback_switch (element) &&
185         !snd_mixer_selem_is_playback_mono (element)) {
186       g_static_rec_mutex_unlock (mixer->rec_mutex);
187       return element;
188     }
189     element = snd_mixer_elem_next (element);
190   }
191
192   /* If not, check if we have any playback mixer with both volume and switch */
193   element = snd_mixer_first_elem (handle);
194   for (i = 0; i < count; i++) {
195     if (snd_mixer_selem_has_playback_volume (element) &&
196         snd_mixer_selem_has_playback_switch (element)) {
197       g_static_rec_mutex_unlock (mixer->rec_mutex);
198       return element;
199     }
200     element = snd_mixer_elem_next (element);
201   }
202
203   /* If not, take any playback mixer with a volume control */
204   element = snd_mixer_first_elem (handle);
205   for (i = 0; i < count; i++) {
206     if (snd_mixer_selem_has_playback_volume (element)) {
207       g_static_rec_mutex_unlock (mixer->rec_mutex);
208       return element;
209     }
210     element = snd_mixer_elem_next (element);
211   }
212
213   g_static_rec_mutex_unlock (mixer->rec_mutex);
214   /* Looks like we're out of luck ... */
215   return NULL;
216 }
217
218 static void
219 gst_alsa_mixer_update (GstAlsaMixer * mixer, snd_mixer_elem_t * elem)
220 {
221   GList *item;
222
223   g_return_if_fail (mixer != NULL);
224
225   g_static_rec_mutex_lock (mixer->rec_mutex);
226
227   for (item = mixer->tracklist; item != NULL; item = item->next) {
228     if (GST_IS_ALSA_MIXER_TRACK (item->data)) {
229       if (elem && (GST_ALSA_MIXER_TRACK (item->data)->element != elem))
230         continue;
231
232       gst_alsa_mixer_update_track (mixer, GST_ALSA_MIXER_TRACK (item->data));
233     } else if (GST_IS_ALSA_MIXER_OPTIONS (item->data)) {
234       if (elem && (GST_ALSA_MIXER_OPTIONS (item->data)->element != elem))
235         continue;
236
237       gst_alsa_mixer_update_option (mixer, GST_ALSA_MIXER_OPTIONS (item->data));
238     }
239   }
240
241   g_static_rec_mutex_unlock (mixer->rec_mutex);
242 }
243
244 static int
245 gst_alsa_mixer_elem_handle_callback (snd_mixer_elem_t * elem, unsigned int mask)
246 {
247   GstAlsaMixer *mixer =
248       (GstAlsaMixer *) snd_mixer_elem_get_callback_private (elem);
249
250   GST_LOG ("ALSA elem cb");
251
252   g_return_val_if_fail (mixer != NULL, 1);
253
254   gst_alsa_mixer_update (mixer, elem);
255
256   return 0;
257 }
258
259 static int
260 gst_alsa_mixer_handle_callback (snd_mixer_t * handle, unsigned int mask,
261     snd_mixer_elem_t * elem)
262 {
263   GstAlsaMixer *mixer =
264       (GstAlsaMixer *) snd_mixer_get_callback_private (handle);
265
266   GST_LOG ("ALSA cb");
267
268   g_return_val_if_fail (mixer != NULL, 1);
269
270   /* Hopefully won't be call recursively and will handle pending elem events */
271   snd_mixer_handle_events (mixer->handle);
272
273   gst_alsa_mixer_update (mixer, elem);
274
275   return 0;
276 }
277
278 static void
279 gst_alsa_mixer_ensure_track_list (GstAlsaMixer * mixer)
280 {
281   gint i, count;
282   snd_mixer_elem_t *element, *master;
283   GList *item;
284
285   g_return_if_fail (mixer->handle != NULL);
286
287   if (mixer->tracklist)
288     return;
289
290   g_static_rec_mutex_lock (mixer->rec_mutex);
291
292   master = gst_alsa_mixer_find_master_mixer (mixer, mixer->handle);
293
294   count = snd_mixer_get_count (mixer->handle);
295   element = snd_mixer_first_elem (mixer->handle);
296
297   /* build track list
298    *
299    * Some ALSA tracks may have playback and capture capabilities.
300    * Here we model them as two separate GStreamer tracks.
301    */
302
303   for (i = 0; i < count; i++) {
304     GstMixerTrack *play_track = NULL;
305     GstMixerTrack *cap_track = NULL;
306     const gchar *name;
307     GList *item;
308     gint samename = 0;
309
310     name = snd_mixer_selem_get_name (element);
311
312     /* prevent dup names */
313     for (item = mixer->tracklist; item != NULL; item = item->next) {
314       snd_mixer_elem_t *temp;
315
316       if (GST_IS_ALSA_MIXER_OPTIONS (item->data))
317         temp = GST_ALSA_MIXER_OPTIONS (item->data)->element;
318       else
319         temp = GST_ALSA_MIXER_TRACK (item->data)->element;
320
321       if (strcmp (name, snd_mixer_selem_get_name (temp)) == 0)
322         samename++;
323     }
324
325     GST_LOG ("[%s] probing element #%u, mixer->dir=%u", name, i, mixer->dir);
326
327     if (mixer->dir & GST_ALSA_MIXER_PLAYBACK) {
328       gboolean has_playback_switch, has_playback_volume;
329
330       has_playback_switch = snd_mixer_selem_has_playback_switch (element);
331       has_playback_volume = snd_mixer_selem_has_playback_volume (element);
332
333       GST_LOG ("[%s] PLAYBACK: has_playback_volume=%d, has_playback_switch=%d"
334           "%s", name, has_playback_volume, has_playback_switch,
335           (element == master) ? " MASTER" : "");
336
337       if (has_playback_volume) {
338         gint flags = GST_MIXER_TRACK_OUTPUT;
339
340         if (element == master)
341           flags |= GST_MIXER_TRACK_MASTER;
342
343         play_track = gst_alsa_mixer_track_new (element, samename, i,
344             flags, FALSE, NULL, FALSE);
345
346       } else if (has_playback_switch) {
347         /* simple mute switch */
348         play_track = gst_alsa_mixer_track_new (element, samename, i,
349             GST_MIXER_TRACK_OUTPUT, TRUE, NULL, FALSE);
350       }
351
352       if (snd_mixer_selem_is_enumerated (element)) {
353         GstMixerOptions *opts = gst_alsa_mixer_options_new (element, i);
354
355         GST_LOG ("[%s] is enumerated (%d)", name, i);
356         mixer->tracklist = g_list_append (mixer->tracklist, opts);
357       }
358     }
359
360     if (mixer->dir & GST_ALSA_MIXER_CAPTURE) {
361       gboolean has_capture_switch, has_common_switch;
362       gboolean has_capture_volume, has_common_volume;
363
364       has_capture_switch = snd_mixer_selem_has_capture_switch (element);
365       has_common_switch = snd_mixer_selem_has_common_switch (element);
366       has_capture_volume = snd_mixer_selem_has_capture_volume (element);
367       has_common_volume = snd_mixer_selem_has_common_volume (element);
368
369       GST_LOG ("[%s] CAPTURE: has_capture_volume=%d, has_common_volume=%d, "
370           "has_capture_switch=%d, has_common_switch=%d, play_track=%p", name,
371           has_capture_volume, has_common_volume, has_capture_switch,
372           has_common_switch, play_track);
373
374       if (has_capture_volume && !(play_track && has_common_volume)) {
375         cap_track = gst_alsa_mixer_track_new (element, samename, i,
376             GST_MIXER_TRACK_INPUT, FALSE, NULL, play_track != NULL);
377       } else if (has_capture_switch && !(play_track && has_common_switch)) {
378         cap_track = gst_alsa_mixer_track_new (element, samename, i,
379             GST_MIXER_TRACK_INPUT, TRUE, NULL, play_track != NULL);
380       }
381     }
382
383
384     if (play_track && cap_track) {
385       GST_ALSA_MIXER_TRACK (play_track)->shared_mute =
386           GST_ALSA_MIXER_TRACK (cap_track);
387       GST_ALSA_MIXER_TRACK (cap_track)->shared_mute =
388           GST_ALSA_MIXER_TRACK (play_track);
389     }
390
391     if (play_track)
392       mixer->tracklist = g_list_append (mixer->tracklist, play_track);
393
394     if (cap_track)
395       mixer->tracklist = g_list_append (mixer->tracklist, cap_track);
396
397     element = snd_mixer_elem_next (element);
398   }
399
400   for (item = mixer->tracklist; item != NULL; item = item->next) {
401     snd_mixer_elem_t *temp;
402
403     if (GST_IS_ALSA_MIXER_OPTIONS (item->data))
404       temp = GST_ALSA_MIXER_OPTIONS (item->data)->element;
405     else
406       temp = GST_ALSA_MIXER_TRACK (item->data)->element;
407
408     snd_mixer_elem_set_callback (temp, gst_alsa_mixer_elem_handle_callback);
409     snd_mixer_elem_set_callback_private (temp, mixer);
410   }
411
412   g_static_rec_mutex_unlock (mixer->rec_mutex);
413 }
414
415 static void
416 task_monitor_alsa (gpointer data)
417 {
418   struct pollfd *pfds;
419   int nfds, rnfds;
420   unsigned short revents;
421   GstAlsaMixer *mixer = (GstAlsaMixer *) data;
422   gint ret;
423
424   g_static_rec_mutex_lock (mixer->rec_mutex);
425
426   nfds = snd_mixer_poll_descriptors_count (mixer->handle);
427   if (nfds <= 0) {
428     GST_ERROR ("snd_mixer_poll_descriptors_count <= 0: %d", nfds);
429     /* FIXME: sleep ? stop monitoring ? */
430     g_static_rec_mutex_unlock (mixer->rec_mutex);
431     return;
432   }
433
434   pfds = g_newa (struct pollfd, nfds + 1);
435   rnfds = snd_mixer_poll_descriptors (mixer->handle, pfds, nfds);
436   g_assert (rnfds <= nfds);
437
438   if (rnfds < 0) {
439     GST_ELEMENT_ERROR (mixer, RESOURCE, READ, (NULL), ("alsa error: %s",
440             snd_strerror (rnfds)));
441     gst_task_pause (mixer->task);
442     g_static_rec_mutex_unlock (mixer->rec_mutex);
443     return;
444   }
445
446   pfds[rnfds].fd = mixer->pfd[0];
447   pfds[rnfds].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
448   pfds[rnfds].revents = 0;
449
450   g_static_rec_mutex_unlock (mixer->rec_mutex);
451
452   GST_LOG ("task loop");
453   ret = poll (pfds, rnfds + 1, -1);
454
455   if (ret < 0) {
456     GST_ELEMENT_ERROR (mixer, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
457     gst_task_pause (mixer->task);
458     return;
459   }
460
461   g_static_rec_mutex_lock (mixer->rec_mutex);
462
463   ret =
464       snd_mixer_poll_descriptors_revents (mixer->handle, pfds, nfds, &revents);
465   if (ret < 0) {
466     GST_ELEMENT_ERROR (mixer, RESOURCE, READ, (NULL), ("alsa error: %s",
467             snd_strerror (ret)));
468     gst_task_pause (mixer->task);
469   } else if (revents & (POLLIN | POLLPRI)) {
470     GST_DEBUG ("Handling events");
471     snd_mixer_handle_events (mixer->handle);
472   } else if (revents & (POLLERR | POLLNVAL | POLLHUP)) {
473     GST_ELEMENT_ERROR (mixer, RESOURCE, READ, (NULL), (NULL));
474     gst_task_pause (mixer->task);
475   }
476
477   g_static_rec_mutex_unlock (mixer->rec_mutex);
478 }
479
480 /* API */
481
482 GstAlsaMixer *
483 gst_alsa_mixer_new (const char *device, GstAlsaMixerDirection dir)
484 {
485   GstAlsaMixer *ret = NULL;
486
487   g_return_val_if_fail (device != NULL, NULL);
488
489   ret = g_new0 (GstAlsaMixer, 1);
490
491   if (pipe (ret->pfd) == -1)
492     goto error;
493
494   ret->rec_mutex = g_new (GStaticRecMutex, 1);
495   g_static_rec_mutex_init (ret->rec_mutex);
496
497   ret->task_mutex = g_new (GStaticRecMutex, 1);
498   g_static_rec_mutex_init (ret->task_mutex);
499
500   ret->task = gst_task_create (task_monitor_alsa, ret);
501   gst_task_set_lock (ret->task, ret->task_mutex);
502
503   ret->device = g_strdup (device);
504   ret->dir = dir;
505
506   if (!gst_alsa_mixer_open (ret))
507     goto error;
508
509   if (gst_task_start (ret->task) == FALSE) {
510     GST_WARNING ("Could not start alsamixer task");
511   }
512
513   return ret;
514
515   /* ERRORS */
516 error:
517   {
518     gst_alsa_mixer_free (ret);
519     return NULL;
520   }
521 }
522
523 void
524 gst_alsa_mixer_free (GstAlsaMixer * mixer)
525 {
526   g_return_if_fail (mixer != NULL);
527
528   if (mixer->task) {
529     if (write (mixer->pfd[1], "stop", 5) <= 0) {
530       GST_ERROR ("Cannot send " "stop" " to alsamixer task");
531       close (mixer->pfd[1]);
532       mixer->pfd[1] = -1;
533     }
534
535     if (gst_task_join (mixer->task) == FALSE) {
536       GST_ERROR ("Cannot join alsamixer task");
537     }
538
539     gst_object_unref (mixer->task);
540     mixer->task = NULL;
541   }
542
543   g_static_rec_mutex_free (mixer->task_mutex);
544   g_free (mixer->task_mutex);
545   mixer->task_mutex = NULL;
546
547   if (mixer->pfd[0] > 0) {
548     close (mixer->pfd[0]);
549     mixer->pfd[0] = -1;
550   }
551
552   if (mixer->pfd[1] > 0) {
553     close (mixer->pfd[1]);
554     mixer->pfd[1] = -1;
555   }
556
557   if (mixer->interface) {
558     g_object_unref (G_OBJECT (mixer->interface));
559     mixer->interface = NULL;
560   }
561
562   if (mixer->device) {
563     g_free (mixer->device);
564     mixer->device = NULL;
565   }
566
567   if (mixer->cardname) {
568     g_free (mixer->cardname);
569     mixer->cardname = NULL;
570   }
571
572   if (mixer->tracklist) {
573     g_list_foreach (mixer->tracklist, (GFunc) g_object_unref, NULL);
574     g_list_free (mixer->tracklist);
575     mixer->tracklist = NULL;
576   }
577
578   if (mixer->handle) {
579     snd_mixer_close (mixer->handle);
580     mixer->handle = NULL;
581   }
582
583   g_static_rec_mutex_free (mixer->rec_mutex);
584   g_free (mixer->rec_mutex);
585   mixer->rec_mutex = NULL;
586
587   g_free (mixer);
588 }
589
590 const GList *
591 gst_alsa_mixer_list_tracks (GstAlsaMixer * mixer)
592 {
593   g_return_val_if_fail (mixer->handle != NULL, NULL);
594
595   gst_alsa_mixer_ensure_track_list (mixer);
596
597   return (const GList *) mixer->tracklist;
598 }
599
600 void
601 gst_alsa_mixer_get_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
602     gint * volumes)
603 {
604   gint i;
605   GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
606
607   g_return_if_fail (mixer->handle != NULL);
608
609   g_static_rec_mutex_lock (mixer->rec_mutex);
610
611   gst_alsa_mixer_track_update (alsa_track);
612
613   if (track->flags & GST_MIXER_TRACK_OUTPUT) {  /* return playback volume */
614
615     /* Is emulated mute flag activated? */
616     if (track->flags & GST_MIXER_TRACK_MUTE &&
617         !(alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_PSWITCH)) {
618       for (i = 0; i < track->num_channels; i++)
619         volumes[i] = alsa_track->volumes[i];
620     } else {
621       for (i = 0; i < track->num_channels; i++) {
622         long tmp = 0;
623
624         snd_mixer_selem_get_playback_volume (alsa_track->element, i, &tmp);
625         alsa_track->volumes[i] = volumes[i] = (gint) tmp;
626       }
627     }
628
629   } else if (track->flags & GST_MIXER_TRACK_INPUT) {    /* return capture volume */
630
631     /* Is emulated record flag activated? */
632     if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH ||
633         track->flags & GST_MIXER_TRACK_RECORD) {
634       for (i = 0; i < track->num_channels; i++) {
635         long tmp = 0;
636
637         snd_mixer_selem_get_capture_volume (alsa_track->element, i, &tmp);
638         alsa_track->volumes[i] = volumes[i] = (gint) tmp;
639       }
640     } else {
641       for (i = 0; i < track->num_channels; i++)
642         volumes[i] = alsa_track->volumes[i];
643     }
644   }
645   g_static_rec_mutex_unlock (mixer->rec_mutex);
646 }
647
648 static gboolean
649 check_if_volumes_are_the_same (guint num_channels, gint * volumes)
650 {
651   guint i;
652
653   if (num_channels <= 1)
654     return TRUE;
655
656   for (i = 1; i < num_channels; i++) {
657     if (volumes[i] != volumes[0])
658       return FALSE;
659   }
660
661   return TRUE;
662 }
663
664 void
665 gst_alsa_mixer_set_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
666     gint * volumes)
667 {
668   GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
669   gint i;
670
671   g_return_if_fail (mixer->handle != NULL);
672
673   g_static_rec_mutex_lock (mixer->rec_mutex);
674
675   gst_alsa_mixer_track_update (alsa_track);
676
677   if (track->flags & GST_MIXER_TRACK_OUTPUT) {
678
679     /* Is emulated mute flag activated? */
680     if (track->flags & GST_MIXER_TRACK_MUTE &&
681         !(alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_PSWITCH)) {
682       for (i = 0; i < track->num_channels; i++)
683         alsa_track->volumes[i] = volumes[i];
684     } else {
685       if (check_if_volumes_are_the_same (track->num_channels, volumes)) {
686         snd_mixer_selem_set_playback_volume_all (alsa_track->element,
687             volumes[0]);
688         for (i = 0; i < track->num_channels; i++)
689           alsa_track->volumes[i] = volumes[0];
690       } else {
691         for (i = 0; i < track->num_channels; i++) {
692           alsa_track->volumes[i] = volumes[i];
693           snd_mixer_selem_set_playback_volume (alsa_track->element, i,
694               volumes[i]);
695         }
696       }
697     }
698
699   } else if (track->flags & GST_MIXER_TRACK_INPUT) {
700
701     /* Is emulated record flag activated? */
702     if (track->flags & GST_MIXER_TRACK_RECORD ||
703         alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH) {
704       if (check_if_volumes_are_the_same (track->num_channels, volumes)) {
705         snd_mixer_selem_set_capture_volume_all (alsa_track->element,
706             volumes[0]);
707         for (i = 0; i < track->num_channels; i++)
708           alsa_track->volumes[i] = volumes[0];
709       } else {
710         for (i = 0; i < track->num_channels; i++) {
711           alsa_track->volumes[i] = volumes[i];
712           snd_mixer_selem_set_capture_volume (alsa_track->element, i,
713               volumes[i]);
714         }
715       }
716     } else {
717       for (i = 0; i < track->num_channels; i++)
718         alsa_track->volumes[i] = volumes[i];
719     }
720   }
721   g_static_rec_mutex_unlock (mixer->rec_mutex);
722 }
723
724 void
725 gst_alsa_mixer_set_mute (GstAlsaMixer * mixer, GstMixerTrack * track,
726     gboolean mute)
727 {
728   GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
729
730   g_return_if_fail (mixer->handle != NULL);
731
732   g_static_rec_mutex_lock (mixer->rec_mutex);
733
734   gst_alsa_mixer_track_update (alsa_track);
735
736   if (! !(mute) == ! !(track->flags & GST_MIXER_TRACK_MUTE)) {
737     g_static_rec_mutex_unlock (mixer->rec_mutex);
738     return;
739   }
740   if (mute) {
741     track->flags |= GST_MIXER_TRACK_MUTE;
742
743     if (alsa_track->shared_mute)
744       ((GstMixerTrack *) (alsa_track->shared_mute))->flags |=
745           GST_MIXER_TRACK_MUTE;
746   } else {
747     track->flags &= ~GST_MIXER_TRACK_MUTE;
748
749     if (alsa_track->shared_mute)
750       ((GstMixerTrack *) (alsa_track->shared_mute))->flags &=
751           ~GST_MIXER_TRACK_MUTE;
752   }
753
754   if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_PSWITCH) {
755     snd_mixer_selem_set_playback_switch_all (alsa_track->element, mute ? 0 : 1);
756   } else {
757     gint i;
758     GstAlsaMixerTrack *ctrl_track;
759
760     if ((track->flags & GST_MIXER_TRACK_INPUT)
761         && alsa_track->shared_mute != NULL)
762       ctrl_track = alsa_track->shared_mute;
763     else
764       ctrl_track = alsa_track;
765
766     for (i = 0; i < ((GstMixerTrack *) ctrl_track)->num_channels; i++) {
767       long vol =
768           mute ? ((GstMixerTrack *) ctrl_track)->
769           min_volume : ctrl_track->volumes[i];
770       snd_mixer_selem_set_playback_volume (ctrl_track->element, i, vol);
771     }
772   }
773   g_static_rec_mutex_unlock (mixer->rec_mutex);
774 }
775
776 void
777 gst_alsa_mixer_set_record (GstAlsaMixer * mixer,
778     GstMixerTrack * track, gboolean record)
779 {
780   GstAlsaMixerTrack *alsa_track = GST_ALSA_MIXER_TRACK (track);
781
782   g_return_if_fail (mixer->handle != NULL);
783
784   g_static_rec_mutex_lock (mixer->rec_mutex);
785
786   gst_alsa_mixer_track_update (alsa_track);
787
788   if (! !(record) == ! !(track->flags & GST_MIXER_TRACK_RECORD)) {
789     g_static_rec_mutex_unlock (mixer->rec_mutex);
790     return;
791   }
792
793   if (record) {
794     track->flags |= GST_MIXER_TRACK_RECORD;
795   } else {
796     track->flags &= ~GST_MIXER_TRACK_RECORD;
797   }
798
799   if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH) {
800     snd_mixer_selem_set_capture_switch_all (alsa_track->element,
801         record ? 1 : 0);
802
803     /* update all tracks in same exlusive cswitch group */
804     if (alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH_EXCL) {
805       GList *item;
806
807       for (item = mixer->tracklist; item != NULL; item = item->next) {
808
809         if (GST_IS_ALSA_MIXER_TRACK (item->data)) {
810           GstAlsaMixerTrack *item_alsa_track =
811               GST_ALSA_MIXER_TRACK (item->data);
812
813           if (item_alsa_track->alsa_flags & GST_ALSA_MIXER_TRACK_CSWITCH_EXCL &&
814               item_alsa_track->capture_group == alsa_track->capture_group) {
815             gst_alsa_mixer_track_update (item_alsa_track);
816           }
817         }
818       }
819     }
820   } else {
821     gint i;
822
823     for (i = 0; i < track->num_channels; i++) {
824       long vol = record ? alsa_track->volumes[i] : track->min_volume;
825
826       snd_mixer_selem_set_capture_volume (alsa_track->element, i, vol);
827     }
828   }
829   g_static_rec_mutex_unlock (mixer->rec_mutex);
830 }
831
832 void
833 gst_alsa_mixer_set_option (GstAlsaMixer * mixer,
834     GstMixerOptions * opts, gchar * value)
835 {
836   gint idx = -1, n = 0;
837   GList *item;
838   GstAlsaMixerOptions *alsa_opts = GST_ALSA_MIXER_OPTIONS (opts);
839
840   g_return_if_fail (mixer->handle != NULL);
841
842   for (item = opts->values; item != NULL; item = item->next, n++) {
843     if (!strcmp (item->data, value)) {
844       idx = n;
845       break;
846     }
847   }
848   if (idx == -1)
849     return;
850
851   g_static_rec_mutex_lock (mixer->rec_mutex);
852   snd_mixer_selem_set_enum_item (alsa_opts->element, 0, idx);
853   g_static_rec_mutex_unlock (mixer->rec_mutex);
854 }
855
856 const gchar *
857 gst_alsa_mixer_get_option (GstAlsaMixer * mixer, GstMixerOptions * opts)
858 {
859   gint ret;
860   guint idx;
861   GstAlsaMixerOptions *alsa_opts = GST_ALSA_MIXER_OPTIONS (opts);
862
863   g_return_val_if_fail (mixer->handle != NULL, NULL);
864   g_static_rec_mutex_lock (mixer->rec_mutex);
865   ret = snd_mixer_selem_get_enum_item (alsa_opts->element, 0, &idx);
866   g_static_rec_mutex_unlock (mixer->rec_mutex);
867   if (ret == 0)
868     return g_list_nth_data (opts->values, idx);
869   else
870     return snd_strerror (ret);  /* feeble attempt at error handling */
871 }
872
873 GstMixerFlags
874 gst_alsa_mixer_get_mixer_flags (GstAlsaMixer * mixer)
875 {
876   g_return_val_if_fail (mixer != NULL, GST_MIXER_FLAG_NONE);
877
878   return GST_MIXER_FLAG_AUTO_NOTIFICATIONS;
879 }
880
881 static void
882 gst_alsa_mixer_update_option (GstAlsaMixer * mixer,
883     GstAlsaMixerOptions * alsa_opts)
884 {
885   gint ret;
886   guint idx;
887   /* const */ gchar *option;
888
889   if (mixer->interface == NULL) {
890     GST_WARNING ("Cannot send update notifications, no GstMixer * given");
891     return;
892   }
893   g_static_rec_mutex_lock (mixer->rec_mutex);
894   ret = snd_mixer_selem_get_enum_item (alsa_opts->element, 0, &idx);
895   g_static_rec_mutex_unlock (mixer->rec_mutex);
896   if (ret == 0) {
897     option = g_list_nth_data (GST_MIXER_OPTIONS (alsa_opts)->values, idx);
898     gst_mixer_option_changed (mixer->interface, GST_MIXER_OPTIONS (alsa_opts),
899         option);
900   }
901 }
902
903 static void
904 gst_alsa_mixer_update_track (GstAlsaMixer * mixer,
905     GstAlsaMixerTrack * alsa_track)
906 {
907   GstMixerTrack *track = (GstMixerTrack *) alsa_track;
908   gboolean old_mute;
909   gboolean old_record;
910   gint i, n_channels;
911   gint *old_volumes;
912
913   GST_DEBUG ("Updating track %" GST_PTR_FORMAT, alsa_track);
914
915   if (mixer->interface == NULL) {
916     GST_WARNING ("Cannot send update notifications, no GstMixer * given");
917     return;
918   }
919
920   old_mute = ! !(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE));
921   old_record = ! !(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD));
922   old_volumes = g_new (gint, track->num_channels);
923   n_channels = track->num_channels;
924   memcpy (old_volumes, alsa_track->volumes,
925       sizeof (gint) * track->num_channels);
926
927   gst_alsa_mixer_track_update (alsa_track);
928
929   if (old_record !=
930       ! !(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD))) {
931     gst_mixer_record_toggled (mixer->interface, track,
932         ! !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD));
933   }
934   if (old_mute != ! !(GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE))) {
935     gst_mixer_mute_toggled (mixer->interface, track,
936         ! !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE));
937   }
938
939   n_channels = MIN (n_channels, track->num_channels);
940   for (i = 0; i < n_channels; i++) {
941     if (old_volumes[i] != alsa_track->volumes[i]) {
942       gst_mixer_volume_changed (mixer->interface, track, alsa_track->volumes);
943       break;
944     }
945   }
946   g_free (old_volumes);
947 }
948
949 /* utility function for gstalsamixerelement to set the interface */
950 void
951 _gst_alsa_mixer_set_interface (GstAlsaMixer * mixer, GstMixer * interface)
952 {
953   g_return_if_fail (mixer != NULL && mixer->interface == NULL);
954   g_return_if_fail (interface != NULL);
955
956   mixer->interface = g_object_ref (G_OBJECT (interface));
957 }