Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.git] / sys / oss / gstossmixertrack.c
1 /* GStreamer OSS Mixer implementation
2  * Copyright (C) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  *
4  * gstossmixer.c: mixer interface implementation for OSS
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/ioctl.h>
33
34 #ifdef HAVE_OSS_INCLUDE_IN_SYS
35 # include <sys/soundcard.h>
36 #else
37 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
38 #  include <soundcard.h>
39 # else
40 #  ifdef HAVE_OSS_INCLUDE_IN_MACHINE
41 #   include <machine/soundcard.h>
42 #  else
43 #   error "What to include?"
44 #  endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
45 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
46 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
47
48 #include <gst/gst-i18n-plugin.h>
49
50 #include "gstossmixertrack.h"
51
52 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
53 #define GST_CAT_DEFAULT oss_debug
54
55 #define MASK_BIT_IS_SET(mask, bit) \
56   (mask & (1 << bit))
57
58 G_DEFINE_TYPE (GstOssMixerTrack, gst_ossmixer_track, GST_TYPE_MIXER_TRACK);
59
60 static void
61 gst_ossmixer_track_class_init (GstOssMixerTrackClass * klass)
62 {
63   /* nop */
64 }
65
66 static void
67 gst_ossmixer_track_init (GstOssMixerTrack * track)
68 {
69   track->lvol = track->rvol = 0;
70   track->track_num = 0;
71 }
72
73 static const gchar **labels = NULL;
74
75 /* three functions: firstly, OSS has the nasty habit of inserting
76  * spaces in the labels, we want to get rid of them. Secondly,
77  * i18n is impossible with OSS' way of providing us with mixer
78  * labels, so we make a 'given' list of i18n'ed labels. Thirdly, I
79  * personally don't like the "1337" names that OSS gives to their
80  * labels ("Vol", "Mic", "Rec"), I'd rather see full names. */
81
82 static void
83 fill_labels (void)
84 {
85   gint i, pos;
86   const gchar *origs[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_LABELS;
87   const struct
88   {
89     const gchar *given;
90     const gchar *wanted;
91   }
92   cases[] = {
93     /* Note: this list is simply ripped from soundcard.h. For
94      * some people, some values might be missing (3D surround,
95      * etc.) - feel free to add them. That's the reason why
96      * I'm doing this in such a horribly complicated way. */
97     {
98     "Vol  ", _("Volume")}, {
99     "Bass ", _("Bass")}, {
100     "Trebl", _("Treble")}, {
101     "Synth", _("Synth")}, {
102     "Pcm  ", _("PCM")}, {
103     "Spkr ", _("Speaker")}, {
104     "Line ", _("Line-in")}, {
105     "Mic  ", _("Microphone")}, {
106     "CD   ", _("CD")}, {
107     "Mix  ", _("Mixer")}, {
108     "Pcm2 ", _("PCM-2")}, {
109     "Rec  ", _("Record")}, {
110     "IGain", _("In-gain")}, {
111     "OGain", _("Out-gain")}, {
112     "Line1", _("Line-1")}, {
113     "Line2", _("Line-2")}, {
114     "Line3", _("Line-3")}, {
115     "Digital1", _("Digital-1")}, {
116     "Digital2", _("Digital-2")}, {
117     "Digital3", _("Digital-3")}, {
118     "PhoneIn", _("Phone-in")}, {
119     "PhoneOut", _("Phone-out")}, {
120     "Video", _("Video")}, {
121     "Radio", _("Radio")}, {
122     "Monitor", _("Monitor")}, {
123     NULL, NULL}
124   };
125
126   labels = g_malloc (sizeof (gchar *) * SOUND_MIXER_NRDEVICES);
127
128   for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
129     for (pos = 0; cases[pos].given != NULL; pos++) {
130       if (!strcmp (cases[pos].given, origs[i])) {
131         labels[i] = g_strdup (cases[pos].wanted);
132         break;
133       }
134     }
135     if (cases[pos].given == NULL)
136       labels[i] = g_strdup (origs[i]);
137   }
138 }
139
140 GstMixerTrack *
141 gst_ossmixer_track_new (gint mixer_fd,
142     gint track_num, gint max_chans, gint flags)
143 {
144   GstOssMixerTrack *osstrack;
145   GstMixerTrack *track;
146   gint volume;
147
148   if (!labels)
149     fill_labels ();
150
151   osstrack = g_object_new (GST_TYPE_OSSMIXER_TRACK, NULL);
152   track = GST_MIXER_TRACK (osstrack);
153   track->label = g_strdup (labels[track_num]);
154   track->num_channels = max_chans;
155   track->flags = flags;
156   track->min_volume = 0;
157   track->max_volume = 100;
158   osstrack->track_num = track_num;
159
160   /* volume */
161   if (ioctl (mixer_fd, MIXER_READ (osstrack->track_num), &volume) < 0) {
162     g_warning ("Error getting device (%d) volume: %s",
163         osstrack->track_num, strerror (errno));
164     volume = 0;
165   }
166   osstrack->lvol = (volume & 0xff);
167   if (track->num_channels == 2) {
168     osstrack->rvol = ((volume >> 8) & 0xff);
169   }
170
171   return track;
172 }