sys/sunaudio/gstsunaudiomixerctrl.c: Set the mixer fd before calling ioctl() on it...
[platform/upstream/gstreamer.git] / sys / sunaudio / gstsunaudiomixerctrl.c
1 /*
2  * GStreamer - SunAudio mixer interface element
3  * Copyright (C) 2005,2006,2008 Sun Microsystems, Inc.,
4  *               Brian Cameron <brian.cameron@sun.com>
5  * Copyright (C) 2008 Sun Microsystems, Inc.,
6  *               Jan Schmidt <jan.schmidt@sun.com> 
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/ioctl.h>
35 #include <sys/mixer.h>
36
37 #include <gst/gst-i18n-plugin.h>
38
39 #include "gstsunaudiomixerctrl.h"
40 #include "gstsunaudiomixertrack.h"
41
42 GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
43 #define GST_CAT_DEFAULT sunaudio_debug
44
45 static gboolean
46 gst_sunaudiomixer_ctrl_open (GstSunAudioMixerCtrl * mixer)
47 {
48   int fd;
49
50   /* First try to open non-blocking */
51   fd = open (mixer->device, O_RDWR | O_NONBLOCK);
52
53   if (fd >= 0) {
54     close (fd);
55     fd = open (mixer->device, O_WRONLY);
56   }
57
58   if (fd == -1) {
59     GST_DEBUG_OBJECT (mixer,
60         "Failed to open mixer device %s, mixing disabled: %s", mixer->device,
61         strerror (errno));
62
63     return FALSE;
64   }
65   mixer->mixer_fd = fd;
66
67   /* Try to set the multiple open flag if we can, but ignore errors */
68   ioctl (mixer->mixer_fd, AUDIO_MIXER_MULTIPLE_OPEN);
69
70   return TRUE;
71 }
72
73 void
74 gst_sunaudiomixer_ctrl_build_list (GstSunAudioMixerCtrl * mixer)
75 {
76   GstMixerTrack *track;
77
78   struct audio_info audioinfo;
79
80   /*
81    * Do not continue appending the same 3 static tracks onto the list
82    */
83   if (mixer->tracklist == NULL) {
84     g_return_if_fail (mixer->mixer_fd != -1);
85
86     /* Output & should be MASTER when it's the only one. */
87     track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_OUTPUT,
88         2, GST_MIXER_TRACK_OUTPUT | GST_MIXER_TRACK_MASTER);
89     mixer->tracklist = g_list_append (mixer->tracklist, track);
90
91     /* Input */
92     track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_LINE_IN,
93         2, GST_MIXER_TRACK_INPUT);
94
95     /* Set whether we are recording from microphone or from line-in */
96     if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
97       g_warning ("Error getting audio device volume");
98       return;
99     }
100
101     /* Set initial RECORD status */
102     if (audioinfo.record.port == AUDIO_MICROPHONE) {
103       mixer->recdevs |= (1 << GST_SUNAUDIO_TRACK_LINE_IN);
104       track->flags |= GST_MIXER_TRACK_RECORD;
105     } else {
106       mixer->recdevs &= ~(1 << GST_SUNAUDIO_TRACK_LINE_IN);
107       track->flags &= ~GST_MIXER_TRACK_RECORD;
108     }
109
110     /* Monitor */
111     mixer->tracklist = g_list_append (mixer->tracklist, track);
112     track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_MONITOR,
113         2, GST_MIXER_TRACK_INPUT);
114     mixer->tracklist = g_list_append (mixer->tracklist, track);
115   }
116 }
117
118 GstSunAudioMixerCtrl *
119 gst_sunaudiomixer_ctrl_new (const char *device)
120 {
121   GstSunAudioMixerCtrl *ret = NULL;
122
123   g_return_val_if_fail (device != NULL, NULL);
124
125   ret = g_new0 (GstSunAudioMixerCtrl, 1);
126
127   ret->device = g_strdup (device);
128   ret->mixer_fd = -1;
129   ret->tracklist = NULL;
130
131   if (!gst_sunaudiomixer_ctrl_open (ret))
132     goto error;
133
134   return ret;
135
136 error:
137   if (ret)
138     gst_sunaudiomixer_ctrl_free (ret);
139
140   return NULL;
141 }
142
143 void
144 gst_sunaudiomixer_ctrl_free (GstSunAudioMixerCtrl * mixer)
145 {
146   g_return_if_fail (mixer != NULL);
147
148   if (mixer->device) {
149     g_free (mixer->device);
150     mixer->device = NULL;
151   }
152
153   if (mixer->tracklist) {
154     g_list_foreach (mixer->tracklist, (GFunc) g_object_unref, NULL);
155     g_list_free (mixer->tracklist);
156     mixer->tracklist = NULL;
157   }
158
159   if (mixer->mixer_fd != -1) {
160     close (mixer->mixer_fd);
161     mixer->mixer_fd = -1;
162   }
163
164   g_free (mixer);
165 }
166
167 const GList *
168 gst_sunaudiomixer_ctrl_list_tracks (GstSunAudioMixerCtrl * mixer)
169 {
170   gst_sunaudiomixer_ctrl_build_list (mixer);
171
172   return (const GList *) mixer->tracklist;
173 }
174
175 void
176 gst_sunaudiomixer_ctrl_get_volume (GstSunAudioMixerCtrl * mixer,
177     GstMixerTrack * track, gint * volumes)
178 {
179   gint gain, balance;
180
181   float ratio;
182
183   struct audio_info audioinfo;
184
185   GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
186
187   g_return_if_fail (mixer->mixer_fd != -1);
188
189   if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
190     g_warning ("Error getting audio device volume");
191     return;
192   }
193
194   switch (sunaudiotrack->track_num) {
195     case GST_SUNAUDIO_TRACK_OUTPUT:
196       gain = (int) audioinfo.play.gain;
197       balance = audioinfo.play.balance;
198       break;
199     case GST_SUNAUDIO_TRACK_LINE_IN:
200       gain = (int) audioinfo.record.gain;
201       balance = audioinfo.record.balance;
202       break;
203     case GST_SUNAUDIO_TRACK_MONITOR:
204       gain = (int) audioinfo.monitor_gain;
205       balance = audioinfo.record.balance;
206       break;
207   }
208
209   if (balance == AUDIO_MID_BALANCE) {
210     volumes[0] = gain;
211     volumes[1] = gain;
212   } else if (balance < AUDIO_MID_BALANCE) {
213     volumes[0] = gain;
214     ratio = 1 - (float) (AUDIO_MID_BALANCE - balance) /
215         (float) AUDIO_MID_BALANCE;
216     volumes[1] = (int) ((float) gain * ratio + 0.5);
217   } else {
218     volumes[1] = gain;
219     ratio = 1 - (float) (balance - AUDIO_MID_BALANCE) /
220         (float) AUDIO_MID_BALANCE;
221     volumes[0] = (int) ((float) gain * ratio + 0.5);
222   }
223
224   /*
225    * Reset whether we are recording from microphone or from line-in.
226    * This can change if another program resets the value (such as
227    * sdtaudiocontrol), so it is good to update the flag when we
228    * get the volume.  The gnome-volume-control program calls this
229    * function in a loop so the value will update properly when
230    * changed.
231    */
232   if ((audioinfo.record.port == AUDIO_MICROPHONE &&
233           !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) ||
234       (audioinfo.record.port == AUDIO_LINE_IN &&
235           GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD))) {
236
237     if (audioinfo.record.port == AUDIO_MICROPHONE) {
238       mixer->recdevs |= (1 << GST_SUNAUDIO_TRACK_LINE_IN);
239       track->flags |= GST_MIXER_TRACK_RECORD;
240     } else {
241       mixer->recdevs &= ~(1 << GST_SUNAUDIO_TRACK_LINE_IN);
242       track->flags &= ~GST_MIXER_TRACK_RECORD;
243     }
244   }
245
246   /* Likewise reset MUTE */
247   if ((sunaudiotrack->track_num == GST_SUNAUDIO_TRACK_OUTPUT &&
248           audioinfo.output_muted == 1) ||
249       (sunaudiotrack->track_num != GST_SUNAUDIO_TRACK_OUTPUT && gain == 0)) {
250     /*
251      * If MUTE is set, then gain is always 0, so don't bother
252      * resetting our internal value.
253      */
254     track->flags |= GST_MIXER_TRACK_MUTE;
255   } else {
256     sunaudiotrack->gain = gain;
257     sunaudiotrack->balance = balance;
258     track->flags &= ~GST_MIXER_TRACK_MUTE;
259   }
260 }
261
262 void
263 gst_sunaudiomixer_ctrl_set_volume (GstSunAudioMixerCtrl * mixer,
264     GstMixerTrack * track, gint * volumes)
265 {
266   gint gain;
267
268   gint balance;
269
270   gint l_real_gain;
271
272   gint r_real_gain;
273
274   float ratio;
275
276   gchar buf[100];
277
278   struct audio_info audioinfo;
279
280   GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
281
282   gint temp[2];
283
284   l_real_gain = volumes[0];
285   r_real_gain = volumes[1];
286
287   if (l_real_gain == r_real_gain) {
288     gain = l_real_gain;
289     balance = AUDIO_MID_BALANCE;
290   } else if (l_real_gain < r_real_gain) {
291     gain = r_real_gain;
292     ratio = (float) l_real_gain / (float) r_real_gain;
293     balance =
294         AUDIO_RIGHT_BALANCE - (int) (ratio * (float) AUDIO_MID_BALANCE + 0.5);
295   } else {
296     gain = l_real_gain;
297     ratio = (float) r_real_gain / (float) l_real_gain;
298     balance =
299         AUDIO_LEFT_BALANCE + (int) (ratio * (float) AUDIO_MID_BALANCE + 0.5);
300   }
301
302   sunaudiotrack->gain = gain;
303   sunaudiotrack->balance = balance;
304
305   if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE)) {
306     if (sunaudiotrack->track_num == GST_SUNAUDIO_TRACK_OUTPUT) {
307       return;
308     } else if (gain == 0) {
309       return;
310     } else {
311       /*
312        * If the volume is set to a non-zero value for LINE_IN
313        * or MONITOR, then unset MUTE.
314        */
315       track->flags &= ~GST_MIXER_TRACK_MUTE;
316     }
317   }
318
319   /* Set the volume */
320   AUDIO_INITINFO (&audioinfo);
321
322   switch (sunaudiotrack->track_num) {
323     case GST_SUNAUDIO_TRACK_OUTPUT:
324       audioinfo.play.gain = gain;
325       audioinfo.play.balance = balance;
326       break;
327     case GST_SUNAUDIO_TRACK_LINE_IN:
328       audioinfo.record.gain = gain;
329       audioinfo.record.balance = balance;
330       break;
331     case GST_SUNAUDIO_TRACK_MONITOR:
332       audioinfo.monitor_gain = gain;
333       audioinfo.record.balance = balance;
334       break;
335   }
336
337   g_return_if_fail (mixer->mixer_fd != -1);
338
339   if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
340     g_warning ("Error setting audio device volume");
341     return;
342   }
343 }
344
345 void
346 gst_sunaudiomixer_ctrl_set_mute (GstSunAudioMixerCtrl * mixer,
347     GstMixerTrack * track, gboolean mute)
348 {
349   struct audio_info audioinfo;
350
351   GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
352
353   gint volume, balance;
354
355   AUDIO_INITINFO (&audioinfo);
356
357   if (mute) {
358     volume = 0;
359     track->flags |= GST_MIXER_TRACK_MUTE;
360   } else {
361     volume = sunaudiotrack->gain;
362     track->flags &= ~GST_MIXER_TRACK_MUTE;
363   }
364
365   balance = sunaudiotrack->balance;
366
367   switch (sunaudiotrack->track_num) {
368     case GST_SUNAUDIO_TRACK_OUTPUT:
369
370       if (mute)
371         audioinfo.output_muted = 1;
372       else
373         audioinfo.output_muted = 0;
374
375       audioinfo.play.gain = volume;
376       audioinfo.play.balance = balance;
377       break;
378     case GST_SUNAUDIO_TRACK_LINE_IN:
379       audioinfo.record.gain = volume;
380       audioinfo.record.balance = balance;
381       break;
382     case GST_SUNAUDIO_TRACK_MONITOR:
383       audioinfo.monitor_gain = volume;
384       audioinfo.record.balance = balance;
385       break;
386   }
387
388   g_return_if_fail (mixer->mixer_fd != -1);
389
390   if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
391     g_warning ("Error setting audio device volume");
392     return;
393   }
394 }
395
396 void
397 gst_sunaudiomixer_ctrl_set_record (GstSunAudioMixerCtrl * mixer,
398     GstMixerTrack * track, gboolean record)
399 {
400   GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
401
402   struct audio_info audioinfo;
403
404   GList *trk;
405
406   /* Don't change the setting */
407   if ((record && GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)) ||
408       (!record && !GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_RECORD)))
409     return;
410
411   /*
412    * So there is probably no need to look for others, but reset them all
413    * to being off.
414    */
415   for (trk = mixer->tracklist; trk != NULL; trk = trk->next) {
416     GstMixerTrack *turn = (GstMixerTrack *) trk->data;
417
418     turn->flags &= ~GST_MIXER_TRACK_RECORD;
419   }
420   mixer->recdevs = 0;
421
422   /* Set the port */
423   AUDIO_INITINFO (&audioinfo);
424
425   if (record) {
426     audioinfo.record.port = AUDIO_MICROPHONE;
427     mixer->recdevs |= (1 << sunaudiotrack->track_num);
428     track->flags |= GST_MIXER_TRACK_RECORD;
429   } else {
430     audioinfo.record.port = AUDIO_LINE_IN;
431     mixer->recdevs &= ~(1 << sunaudiotrack->track_num);
432     track->flags &= ~GST_MIXER_TRACK_RECORD;
433   }
434
435   g_return_if_fail (mixer->mixer_fd != -1);
436
437   if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
438     g_warning ("Error setting audio device volume");
439     return;
440   }
441 }