2 * GStreamer - SunAudio mixer interface element
3 * Copyright (C) 2005,2006,2008,2009 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 * Copyright (C) 2009 Sun Microsystems, Inc.,
8 * Garrett D'Amore <garrett.damore@sun.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
36 #include <sys/ioctl.h>
37 #include <sys/audio.h>
38 #include <sys/mixer.h>
40 #include <gst/gst-i18n-plugin.h>
42 #include "gstsunaudiomixerctrl.h"
43 #include "gstsunaudiomixertrack.h"
44 #include "gstsunaudiomixeroptions.h"
46 GST_DEBUG_CATEGORY_EXTERN (sunaudio_debug);
47 #define GST_CAT_DEFAULT sunaudio_debug
50 gst_sunaudiomixer_ctrl_open (GstSunAudioMixerCtrl * mixer)
54 /* First try to open non-blocking */
55 fd = open (mixer->device, O_RDWR | O_NONBLOCK);
59 fd = open (mixer->device, O_WRONLY);
63 GST_DEBUG_OBJECT (mixer,
64 "Failed to open mixer device %s, mixing disabled: %s", mixer->device,
71 /* Try to set the multiple open flag if we can, but ignore errors */
72 ioctl (mixer->mixer_fd, AUDIO_MIXER_MULTIPLE_OPEN);
78 gst_sunaudiomixer_ctrl_build_list (GstSunAudioMixerCtrl * mixer)
81 GstMixerOptions *options;
83 struct audio_info audioinfo;
86 * Do not continue appending the same 3 static tracks onto the list
88 if (mixer->tracklist == NULL) {
89 g_return_if_fail (mixer->mixer_fd != -1);
91 /* query available ports */
92 if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
93 g_warning ("Error getting audio device volume");
97 /* Output & should be MASTER when it's the only one. */
98 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_OUTPUT);
99 mixer->tracklist = g_list_append (mixer->tracklist, track);
102 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_RECORD);
103 mixer->tracklist = g_list_append (mixer->tracklist, track);
106 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_MONITOR);
107 mixer->tracklist = g_list_append (mixer->tracklist, track);
109 if (audioinfo.play.avail_ports & AUDIO_SPEAKER) {
110 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_SPEAKER);
111 mixer->tracklist = g_list_append (mixer->tracklist, track);
113 if (audioinfo.play.avail_ports & AUDIO_HEADPHONE) {
114 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_HP);
115 mixer->tracklist = g_list_append (mixer->tracklist, track);
117 if (audioinfo.play.avail_ports & AUDIO_LINE_OUT) {
118 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_LINEOUT);
119 mixer->tracklist = g_list_append (mixer->tracklist, track);
121 if (audioinfo.play.avail_ports & AUDIO_SPDIF_OUT) {
122 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_SPDIFOUT);
123 mixer->tracklist = g_list_append (mixer->tracklist, track);
125 if (audioinfo.play.avail_ports & AUDIO_AUX1_OUT) {
126 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_AUX1OUT);
127 mixer->tracklist = g_list_append (mixer->tracklist, track);
129 if (audioinfo.play.avail_ports & AUDIO_AUX2_OUT) {
130 track = gst_sunaudiomixer_track_new (GST_SUNAUDIO_TRACK_AUX2OUT);
131 mixer->tracklist = g_list_append (mixer->tracklist, track);
134 if (audioinfo.record.avail_ports != AUDIO_NONE) {
136 gst_sunaudiomixer_options_new (mixer, GST_SUNAUDIO_TRACK_RECSRC);
137 mixer->tracklist = g_list_append (mixer->tracklist, options);
142 GstSunAudioMixerCtrl *
143 gst_sunaudiomixer_ctrl_new (const char *device)
145 GstSunAudioMixerCtrl *ret = NULL;
147 g_return_val_if_fail (device != NULL, NULL);
149 ret = g_new0 (GstSunAudioMixerCtrl, 1);
151 ret->device = g_strdup (device);
153 ret->tracklist = NULL;
155 if (!gst_sunaudiomixer_ctrl_open (ret))
162 gst_sunaudiomixer_ctrl_free (ret);
168 gst_sunaudiomixer_ctrl_free (GstSunAudioMixerCtrl * mixer)
170 g_return_if_fail (mixer != NULL);
173 g_free (mixer->device);
174 mixer->device = NULL;
177 if (mixer->tracklist) {
178 g_list_foreach (mixer->tracklist, (GFunc) g_object_unref, NULL);
179 g_list_free (mixer->tracklist);
180 mixer->tracklist = NULL;
183 if (mixer->mixer_fd != -1) {
184 close (mixer->mixer_fd);
185 mixer->mixer_fd = -1;
192 gst_sunaudiomixer_ctrl_get_mixer_flags (GstSunAudioMixerCtrl * mixer)
194 return GST_MIXER_FLAG_HAS_WHITELIST | GST_MIXER_FLAG_GROUPING;
198 gst_sunaudiomixer_ctrl_list_tracks (GstSunAudioMixerCtrl * mixer)
200 gst_sunaudiomixer_ctrl_build_list (mixer);
202 return (const GList *) mixer->tracklist;
206 gst_sunaudiomixer_ctrl_get_volume (GstSunAudioMixerCtrl * mixer,
207 GstMixerTrack * track, gint * volumes)
211 struct audio_info audioinfo;
212 GstSunAudioMixerTrack *sunaudiotrack;
214 g_return_if_fail (GST_IS_SUNAUDIO_MIXER_TRACK (track));
215 sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
217 g_return_if_fail (mixer->mixer_fd != -1);
219 if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
220 g_warning ("Error getting audio device volume");
224 balance = AUDIO_MID_BALANCE;
227 switch (sunaudiotrack->track_num) {
228 case GST_SUNAUDIO_TRACK_OUTPUT:
229 gain = (int) audioinfo.play.gain;
230 balance = audioinfo.play.balance;
232 case GST_SUNAUDIO_TRACK_RECORD:
233 gain = (int) audioinfo.record.gain;
234 balance = audioinfo.record.balance;
236 case GST_SUNAUDIO_TRACK_MONITOR:
237 gain = (int) audioinfo.monitor_gain;
238 balance = audioinfo.record.balance;
240 case GST_SUNAUDIO_TRACK_SPEAKER:
241 if (audioinfo.play.port & AUDIO_SPEAKER)
242 gain = AUDIO_MAX_GAIN;
244 case GST_SUNAUDIO_TRACK_HP:
245 if (audioinfo.play.port & AUDIO_HEADPHONE)
246 gain = AUDIO_MAX_GAIN;
248 case GST_SUNAUDIO_TRACK_LINEOUT:
249 if (audioinfo.play.port & AUDIO_LINE_OUT)
250 gain = AUDIO_MAX_GAIN;
252 case GST_SUNAUDIO_TRACK_SPDIFOUT:
253 if (audioinfo.play.port & AUDIO_SPDIF_OUT)
254 gain = AUDIO_MAX_GAIN;
256 case GST_SUNAUDIO_TRACK_AUX1OUT:
257 if (audioinfo.play.port & AUDIO_AUX1_OUT)
258 gain = AUDIO_MAX_GAIN;
260 case GST_SUNAUDIO_TRACK_AUX2OUT:
261 if (audioinfo.play.port & AUDIO_AUX2_OUT)
262 gain = AUDIO_MAX_GAIN;
268 switch (track->num_channels) {
270 if (balance == AUDIO_MID_BALANCE) {
273 } else if (balance < AUDIO_MID_BALANCE) {
275 ratio = 1 - (float) (AUDIO_MID_BALANCE - balance) /
276 (float) AUDIO_MID_BALANCE;
277 volumes[1] = (int) ((float) gain * ratio + 0.5);
280 ratio = 1 - (float) (balance - AUDIO_MID_BALANCE) /
281 (float) AUDIO_MID_BALANCE;
282 volumes[0] = (int) ((float) gain * ratio + 0.5);
290 /* Likewise reset MUTE */
291 if ((sunaudiotrack->track_num == GST_SUNAUDIO_TRACK_OUTPUT &&
292 audioinfo.output_muted == 1) ||
293 (sunaudiotrack->track_num != GST_SUNAUDIO_TRACK_OUTPUT && gain == 0)) {
295 * If MUTE is set, then gain is always 0, so don't bother
296 * resetting our internal value.
298 track->flags |= GST_MIXER_TRACK_MUTE;
300 sunaudiotrack->gain = gain;
301 sunaudiotrack->balance = balance;
302 track->flags &= ~GST_MIXER_TRACK_MUTE;
307 gst_sunaudiomixer_ctrl_set_volume (GstSunAudioMixerCtrl * mixer,
308 GstMixerTrack * track, gint * volumes)
315 struct audio_info audioinfo;
316 GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
318 l_real_gain = volumes[0];
319 r_real_gain = volumes[1];
321 if (l_real_gain == r_real_gain) {
323 balance = AUDIO_MID_BALANCE;
324 } else if (l_real_gain < r_real_gain) {
326 ratio = (float) l_real_gain / (float) r_real_gain;
328 AUDIO_RIGHT_BALANCE - (int) (ratio * (float) AUDIO_MID_BALANCE + 0.5);
331 ratio = (float) r_real_gain / (float) l_real_gain;
333 AUDIO_LEFT_BALANCE + (int) (ratio * (float) AUDIO_MID_BALANCE + 0.5);
336 sunaudiotrack->gain = gain;
337 sunaudiotrack->balance = balance;
339 if (GST_MIXER_TRACK_HAS_FLAG (track, GST_MIXER_TRACK_MUTE)) {
340 if (sunaudiotrack->track_num == GST_SUNAUDIO_TRACK_OUTPUT) {
342 } else if (gain == 0) {
346 * If the volume is set to a non-zero value for LINE_IN
347 * or MONITOR, then unset MUTE.
349 track->flags &= ~GST_MIXER_TRACK_MUTE;
354 AUDIO_INITINFO (&audioinfo);
356 switch (sunaudiotrack->track_num) {
357 case GST_SUNAUDIO_TRACK_OUTPUT:
358 audioinfo.play.gain = gain;
359 audioinfo.play.balance = balance;
361 case GST_SUNAUDIO_TRACK_RECORD:
362 audioinfo.record.gain = gain;
363 audioinfo.record.balance = balance;
365 case GST_SUNAUDIO_TRACK_MONITOR:
366 audioinfo.monitor_gain = gain;
367 audioinfo.record.balance = balance;
373 g_return_if_fail (mixer->mixer_fd != -1);
375 if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
376 g_warning ("Error setting audio device volume");
382 gst_sunaudiomixer_ctrl_set_mute (GstSunAudioMixerCtrl * mixer,
383 GstMixerTrack * track, gboolean mute)
385 struct audio_info audioinfo;
386 struct audio_info oldinfo;
387 GstSunAudioMixerTrack *sunaudiotrack = GST_SUNAUDIO_MIXER_TRACK (track);
388 gint volume, balance;
390 AUDIO_INITINFO (&audioinfo);
392 if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &oldinfo) < 0) {
393 g_warning ("Error getting audio device volume");
399 track->flags |= GST_MIXER_TRACK_MUTE;
401 volume = sunaudiotrack->gain;
402 track->flags &= ~GST_MIXER_TRACK_MUTE;
405 balance = sunaudiotrack->balance;
407 switch (sunaudiotrack->track_num) {
408 case GST_SUNAUDIO_TRACK_OUTPUT:
410 audioinfo.output_muted = 1;
412 audioinfo.output_muted = 0;
414 audioinfo.play.gain = volume;
415 audioinfo.play.balance = balance;
417 case GST_SUNAUDIO_TRACK_RECORD:
418 audioinfo.record.gain = volume;
419 audioinfo.record.balance = balance;
421 case GST_SUNAUDIO_TRACK_MONITOR:
422 audioinfo.monitor_gain = volume;
423 audioinfo.record.balance = balance;
425 case GST_SUNAUDIO_TRACK_SPEAKER:
427 audioinfo.play.port = oldinfo.play.port & ~AUDIO_SPEAKER;
429 audioinfo.play.port = oldinfo.play.port | AUDIO_SPEAKER;
432 case GST_SUNAUDIO_TRACK_HP:
434 audioinfo.play.port = oldinfo.play.port & ~AUDIO_HEADPHONE;
436 audioinfo.play.port = oldinfo.play.port | AUDIO_HEADPHONE;
439 case GST_SUNAUDIO_TRACK_LINEOUT:
441 audioinfo.play.port = oldinfo.play.port & ~AUDIO_LINE_OUT;
443 audioinfo.play.port = oldinfo.play.port | AUDIO_LINE_OUT;
446 case GST_SUNAUDIO_TRACK_SPDIFOUT:
448 audioinfo.play.port = oldinfo.play.port & ~AUDIO_SPDIF_OUT;
450 audioinfo.play.port = oldinfo.play.port | AUDIO_SPDIF_OUT;
453 case GST_SUNAUDIO_TRACK_AUX1OUT:
455 audioinfo.play.port = oldinfo.play.port & ~AUDIO_AUX1_OUT;
457 audioinfo.play.port = oldinfo.play.port | AUDIO_AUX1_OUT;
460 case GST_SUNAUDIO_TRACK_AUX2OUT:
462 audioinfo.play.port = oldinfo.play.port & ~AUDIO_AUX2_OUT;
464 audioinfo.play.port = oldinfo.play.port | AUDIO_AUX2_OUT;
471 if (audioinfo.play.port != ((unsigned) ~0)) {
472 /* mask off ports we can't modify */
473 audioinfo.play.port &= oldinfo.play.mod_ports;
474 /* and add in any that are forced to be on */
475 audioinfo.play.port |= (oldinfo.play.port & ~oldinfo.play.mod_ports);
477 g_return_if_fail (mixer->mixer_fd != -1);
479 if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
480 g_warning ("Error setting audio settings");
486 gst_sunaudiomixer_ctrl_set_record (GstSunAudioMixerCtrl * mixer,
487 GstMixerTrack * track, gboolean record)
492 gst_sunaudiomixer_ctrl_set_option (GstSunAudioMixerCtrl * mixer,
493 GstMixerOptions * options, gchar * value)
495 struct audio_info audioinfo;
496 GstMixerTrack *track;
497 GstSunAudioMixerOptions *opts;
501 g_return_if_fail (mixer != NULL);
502 g_return_if_fail (mixer->mixer_fd != -1);
503 g_return_if_fail (value != NULL);
504 g_return_if_fail (GST_IS_SUNAUDIO_MIXER_OPTIONS (options));
506 track = GST_MIXER_TRACK (options);
507 opts = GST_SUNAUDIO_MIXER_OPTIONS (options);
509 if (opts->track_num != GST_SUNAUDIO_TRACK_RECSRC) {
510 g_warning ("set_option not supported on track %s", track->label);
514 q = g_quark_try_string (value);
516 g_warning ("unknown option '%s'", value);
520 for (i = 0; i < 8; i++) {
521 if (opts->names[i] == q) {
526 if (((1 << (i)) & opts->avail) == 0) {
527 g_warning ("Record port %s not available", g_quark_to_string (q));
531 AUDIO_INITINFO (&audioinfo);
532 audioinfo.record.port = (1 << (i));
534 if (ioctl (mixer->mixer_fd, AUDIO_SETINFO, &audioinfo) < 0) {
535 g_warning ("Error setting audio record port");
540 gst_sunaudiomixer_ctrl_get_option (GstSunAudioMixerCtrl * mixer,
541 GstMixerOptions * options)
543 GstMixerTrack *track;
544 GstSunAudioMixerOptions *opts;
545 struct audio_info audioinfo;
548 g_return_val_if_fail (mixer != NULL, NULL);
549 g_return_val_if_fail (mixer->fd != -1, NULL);
550 g_return_val_if_fail (GST_IS_SUNAUDIO_MIXER_OPTIONS (options), NULL);
552 track = GST_MIXER_TRACK (options);
553 opts = GST_SUNAUDIO_MIXER_OPTIONS (options);
555 g_return_val_if_fail (opts->track_num == GST_SUNAUDIO_TRACK_RECSRC, NULL);
557 if (ioctl (mixer->mixer_fd, AUDIO_GETINFO, &audioinfo) < 0) {
558 g_warning ("Error getting audio device settings");
562 for (i = 0; i < 8; i++) {
563 if ((1 << i) == audioinfo.record.port) {
564 return (g_quark_to_string (opts->names[i]));
568 g_warning ("Record port value %d seems illegal", audioinfo.record.port);