gstalsamixerelement.c 6.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* ALSA mixer implementation.
 * Copyright (C) 2003 Leif Johnson <leif@ambient.2y.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gstalsamixerelement.h"
26
27
#include "gstalsadeviceprobe.h"

Wim Taymans's avatar
Wim Taymans committed
28
29
30
#define DEFAULT_PROP_DEVICE          "default"
#define DEFAULT_PROP_DEVICE_NAME     ""

31
32
enum
{
Wim Taymans's avatar
Wim Taymans committed
33
34
  PROP_0,
  PROP_DEVICE,
35
36
  PROP_DEVICE_NAME
};
37
38


Stefan Kost's avatar
Stefan Kost committed
39
static const GstElementDetails gst_alsa_mixer_element_details =
j^'s avatar
j^ committed
40
GST_ELEMENT_DETAILS ("Alsa mixer",
41
42
43
44
    "Generic/Audio",
    "Control sound input and output levels with ALSA",
    "Leif Johnson <leif@ambient.2y.net>");

45
46
47
48
49
50
51
static void gst_alsa_mixer_element_init_interfaces (GType type);

GST_BOILERPLATE_FULL (GstAlsaMixerElement, gst_alsa_mixer_element,
    GstElement, GST_TYPE_ELEMENT, gst_alsa_mixer_element_init_interfaces)

/* massive macro that takes care of all the GstMixer stuff */
    GST_IMPLEMENT_ALSA_MIXER_METHODS (GstAlsaMixerElement, gst_alsa_mixer_element);
52

53
54
55
56
57
     static void gst_alsa_mixer_element_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
     static void gst_alsa_mixer_element_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
     static void gst_alsa_mixer_element_finalize (GObject * object);
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
     static GstStateChangeReturn gst_alsa_mixer_element_change_state (GstElement
    * element, GstStateChange transition);

     static gboolean
         gst_alsa_mixer_element_interface_supported (GstAlsaMixerElement * this,
    GType interface_type)
{
  if (interface_type == GST_TYPE_MIXER) {
    return gst_alsa_mixer_element_supported (this, interface_type);
  }

  g_return_val_if_reached (FALSE);
}

static void
gst_implements_interface_init (GstImplementsInterfaceClass * klass)
{
  klass->supported = (gpointer) gst_alsa_mixer_element_interface_supported;
}
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
static void
gst_alsa_mixer_element_init_interfaces (GType type)
{
  static const GInterfaceInfo implements_iface_info = {
    (GInterfaceInitFunc) gst_implements_interface_init,
    NULL,
    NULL,
  };
  static const GInterfaceInfo mixer_iface_info = {
    (GInterfaceInitFunc) gst_alsa_mixer_element_interface_init,
    NULL,
    NULL,
  };

  g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
      &implements_iface_info);
  g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);

97
  gst_alsa_type_add_device_property_probe_interface (type);
98
}
99
100
101
102
103
104
105
106
107
108
109
110

static void
gst_alsa_mixer_element_base_init (gpointer klass)
{
  gst_element_class_set_details (GST_ELEMENT_CLASS (klass),
      &gst_alsa_mixer_element_details);
}

static void
gst_alsa_mixer_element_class_init (GstAlsaMixerElementClass * klass)
{
  GstElementClass *element_class;
111
  GObjectClass *gobject_class;
112
113

  element_class = (GstElementClass *) klass;
114
115
116
117
118
  gobject_class = (GObjectClass *) klass;

  gobject_class->finalize = gst_alsa_mixer_element_finalize;
  gobject_class->get_property = gst_alsa_mixer_element_get_property;
  gobject_class->set_property = gst_alsa_mixer_element_set_property;
119

120
121
122
  g_object_class_install_property (gobject_class, PROP_DEVICE,
      g_param_spec_string ("device", "Device",
          "ALSA device, as defined in an asound configuration file",
Wim Taymans's avatar
Wim Taymans committed
123
          DEFAULT_PROP_DEVICE, G_PARAM_READWRITE));
124
125
126

  g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
      g_param_spec_string ("device-name", "Device name",
Wim Taymans's avatar
Wim Taymans committed
127
128
          "Human-readable name of the sound device",
          DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE));
129
130
131
132
133
134
135
136
137
138
139
140
141

  element_class->change_state =
      GST_DEBUG_FUNCPTR (gst_alsa_mixer_element_change_state);
}

static void
gst_alsa_mixer_element_finalize (GObject * obj)
{
  GstAlsaMixerElement *this = GST_ALSA_MIXER_ELEMENT (obj);

  g_free (this->device);

  G_OBJECT_CLASS (parent_class)->finalize (obj);
142
143
144
}

static void
145
146
gst_alsa_mixer_element_init (GstAlsaMixerElement * this,
    GstAlsaMixerElementClass * klass)
147
148
{
  this->mixer = NULL;
Wim Taymans's avatar
Wim Taymans committed
149
  this->device = g_strdup (DEFAULT_PROP_DEVICE);
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
}

static void
gst_alsa_mixer_element_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstAlsaMixerElement *this = GST_ALSA_MIXER_ELEMENT (object);

  switch (prop_id) {
    case PROP_DEVICE:{
      GST_OBJECT_LOCK (this);
      g_free (this->device);
      this->device = g_value_dup_string (value);
      GST_OBJECT_UNLOCK (this);
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_alsa_mixer_element_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec)
{
  GstAlsaMixerElement *this = GST_ALSA_MIXER_ELEMENT (object);

  switch (prop_id) {
    case PROP_DEVICE:{
      GST_OBJECT_LOCK (this);
      g_value_set_string (value, this->device);
      GST_OBJECT_UNLOCK (this);
      break;
    }
    case PROP_DEVICE_NAME:{
      GST_OBJECT_LOCK (this);
      if (this->mixer) {
        g_value_set_string (value, this->mixer->cardname);
      } else {
        g_value_set_string (value, NULL);
      }
      GST_OBJECT_UNLOCK (this);
      break;
    }
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
199
200
}

201
202
203
static GstStateChangeReturn
gst_alsa_mixer_element_change_state (GstElement * element,
    GstStateChange transition)
204
{
205
  GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
206
207
  GstAlsaMixerElement *this = GST_ALSA_MIXER_ELEMENT (element);

208
209
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
210
      if (!this->mixer) {
211
212
213
214
215
216
217
        const gchar *device = (this->device) ? this->device : "default";

        this->mixer = gst_alsa_mixer_new (device, GST_ALSA_MIXER_ALL);
        if (!this->mixer) {
          GST_ELEMENT_ERROR (element, RESOURCE, OPEN_READ_WRITE, (NULL),
              ("Failed to open alsa mixer device '%s'", device));
          return GST_STATE_CHANGE_FAILURE;
218
        }
219
220
      }
      break;
221
222
223
224
225
226
227
228
229
    default:
      break;
  }

  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (ret == GST_STATE_CHANGE_FAILURE)
    return ret;

  switch (transition) {
230
    case GST_STATE_CHANGE_READY_TO_NULL:
231
232
233
234
235
236
237
238
239
      if (this->mixer) {
        gst_alsa_mixer_free (this->mixer);
        this->mixer = NULL;
      }
      break;
    default:
      break;
  }

240
  return ret;
241
}