Commit ba2b9bd9 authored by Leif Johnson's avatar Leif Johnson
Browse files

+ got the alsa mixer implementation to work !

Original commit message from CVS:
+ got the alsa mixer implementation to work !
parent 80fece4f
......@@ -2,13 +2,16 @@
plugin_LTLIBRARIES = libgstalsa.la
libgstalsa_la_SOURCES = gstalsaplugin.c \
gstalsa.c gstalsaclock.c gstalsasink.c gstalsasrc.c gstalsamixer.c
gstalsa.c gstalsaclock.c gstalsasink.c gstalsasrc.c \
gstalsamixer.c gstalsamixertrack.c
libgstalsa_la_CFLAGS = $(GST_CFLAGS) $(ALSA_CFLAGS)
libgstalsa_la_LIBADD = $(ALSA_LIBS)
libgstalsa_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
libgstalsa_la_LIBADD = $(ALSA_LIBS) \
-L${top_builddir}/gst-libs/gst -lgstinterfaces-@GST_MAJORMINOR@
noinst_HEADERS = \
gstalsa.h gstalsaclock.h gstalsasink.h gstalsasrc.h gstalsamixer.h
gstalsa.h gstalsaclock.h gstalsasink.h gstalsasrc.h \
gstalsamixer.h gstalsamixertrack.h
EXTRA_DIST = README
This diff is collapsed.
......@@ -155,9 +155,6 @@ struct _GstAlsa {
GstClockTime max_discont; /* max difference between current
playback timestamp and buffers timestamps
*/
/* mixer */
GList *tracklist; /* list of available tracks */
snd_mixer_t * mixer_handle;
};
struct _GstAlsaClass {
......
/* GStreamer ALSA mixer implementation
/* ALSA mixer implementation.
* Copyright (C) 2003 Leif Johnson <leif@ambient.2y.net>
*
* This library is free software; you can redistribute it and/or
......@@ -23,90 +23,172 @@
#include "gstalsamixer.h"
static gboolean gst_alsa_mixer_supported (GstInterface *iface,
GType iface_type);
static const GList * gst_alsa_mixer_list_tracks (GstMixer *mixer);
static void gst_alsa_mixer_set_volume (GstMixer *mixer,
GstMixerTrack *track,
gint *volumes);
static void gst_alsa_mixer_get_volume (GstMixer *mixer,
GstMixerTrack *track,
gint *volumes);
static void gst_alsa_mixer_set_record (GstMixer *mixer,
GstMixerTrack *track,
gboolean record);
static void gst_alsa_mixer_set_mute (GstMixer *mixer,
GstMixerTrack *track,
gboolean mute);
GstMixerTrack *
gst_alsa_mixer_track_new (GstAlsa *alsa,
gint track_num,
gint channels,
gint flags)
/* elementfactory information */
static GstElementDetails gst_alsa_mixer_details = GST_ELEMENT_DETAILS (
"Alsa Mixer",
"Generic/Audio",
"Control sound input and output levels with ALSA",
"Leif Johnson <leif@ambient.2y.net>"
);
static void gst_alsa_interface_init (GstInterfaceClass *klass);
static void gst_alsa_mixer_class_init (gpointer g_class,
gpointer class_data);
static void gst_alsa_mixer_init (GstAlsaMixer * mixer);
static void gst_alsa_mixer_dispose (GObject * object);
static void gst_alsa_mixer_interface_init (GstMixerClass* klass);
static gboolean gst_alsa_mixer_supported (GstInterface * iface,
GType iface_type);
/* GStreamer stuff */
static GstElementStateReturn gst_alsa_mixer_change_state (GstElement * element);
static void gst_alsa_mixer_build_list (GstAlsaMixer * mixer);
static void gst_alsa_mixer_free_list (GstAlsaMixer * mixer);
/* interface implementation */
static const GList * gst_alsa_mixer_list_tracks (GstMixer * mixer);
static void gst_alsa_mixer_set_volume (GstMixer * mixer,
GstMixerTrack* track,
gint * volumes);
static void gst_alsa_mixer_get_volume (GstMixer * mixer,
GstMixerTrack* track,
gint * volumes);
static void gst_alsa_mixer_set_record (GstMixer * mixer,
GstMixerTrack* track,
gboolean record);
static void gst_alsa_mixer_set_mute (GstMixer * mixer,
GstMixerTrack* track,
gboolean mute);
/*** GOBJECT STUFF ************************************************************/
static GstAlsa *parent_class = NULL;
GType
gst_alsa_mixer_get_type (void)
{
gint i;
long min, max;
snd_mixer_elem_t *element = NULL;
GstMixerTrack *track = (GstMixerTrack *) g_new (GstAlsaMixerTrack, 1);
GstAlsaMixerTrack *alsa_track = (GstAlsaMixerTrack *) track;
static GType alsa_mixer_type = 0;
if (!alsa_mixer_type) {
static const GTypeInfo alsa_mixer_info = {
sizeof (GstAlsaMixerClass),
NULL,
NULL,
gst_alsa_mixer_class_init,
NULL,
NULL,
sizeof (GstAlsaMixer),
0,
(GInstanceInitFunc) gst_alsa_mixer_init,
};
static const GInterfaceInfo alsa_iface_info = {
(GInterfaceInitFunc) gst_alsa_interface_init,
NULL,
NULL,
};
static const GInterfaceInfo alsa_mixer_iface_info = {
(GInterfaceInitFunc) gst_alsa_mixer_interface_init,
NULL,
NULL,
};
alsa_mixer_type = g_type_register_static (GST_TYPE_ALSA, "GstAlsaMixer", &alsa_mixer_info, 0);
g_type_add_interface_static (alsa_mixer_type, GST_TYPE_INTERFACE, &alsa_iface_info);
g_type_add_interface_static (alsa_mixer_type, GST_TYPE_MIXER, &alsa_mixer_iface_info);
}
/* find a pointer to the track_num-th element in the mixer. */
i = 0;
element = snd_mixer_first_elem(alsa->mixer_handle);
while (i++ < track_num) element = snd_mixer_elem_next(element);
/* set basic information */
track->label = g_strdup_printf("%s", snd_mixer_selem_get_name(element));
track->num_channels = channels;
track->flags = flags;
alsa_track->element = element;
alsa_track->track_num = track_num;
/* set volume information */
snd_mixer_selem_get_playback_volume_range(element, &min, &max);
track->min_volume = (gint) min;
track->max_volume = (gint) max;
snd_mixer_selem_get_capture_volume_range(element, &min, &max);
alsa_track->min_rec_volume = (gint) min;
alsa_track->max_rec_volume = (gint) max;
for (i = 0; i < channels; i++) {
long tmp;
if (snd_mixer_selem_has_playback_channel(element, i)) {
snd_mixer_selem_get_playback_volume(element, i, &tmp);
alsa_track->volumes[i] = (gint) tmp;
} else if (snd_mixer_selem_has_capture_channel(element, i)) {
snd_mixer_selem_get_capture_volume(element, i, &tmp);
alsa_track->volumes[i] = (gint) tmp;
}
return alsa_mixer_type;
}
static void
gst_alsa_mixer_class_init (gpointer g_class, gpointer class_data)
{
GObjectClass *object_class;
GstElementClass *element_class;
GstAlsaClass *klass;
klass = (GstAlsaClass *)g_class;
object_class = (GObjectClass *) g_class;
element_class = (GstElementClass *) g_class;
if (parent_class == NULL)
parent_class = g_type_class_ref (GST_TYPE_ALSA);
object_class->dispose = gst_alsa_mixer_dispose;
element_class->change_state = gst_alsa_mixer_change_state;
gst_element_class_set_details (element_class, &gst_alsa_mixer_details);
}
static void
gst_alsa_mixer_init (GstAlsaMixer * mixer)
{
gint err;
GstAlsa *alsa = GST_ALSA (mixer);
mixer->mixer_handle = (snd_mixer_t *) -1;
/* open and initialize the mixer device */
err = snd_mixer_open(&mixer->mixer_handle, 0);
if (err < 0 || mixer->mixer_handle == NULL) {
GST_ERROR_OBJECT (GST_OBJECT (mixer), "Cannot open mixer device.");
mixer->mixer_handle = (snd_mixer_t *) -1;
return;
}
if ((err = snd_mixer_attach(mixer->mixer_handle, alsa->device)) < 0) {
GST_ERROR_OBJECT (GST_OBJECT (mixer),
"Cannot attach mixer to sound device `%s'.",
alsa->device);
goto error;
}
return track;
if ((err = snd_mixer_selem_register(mixer->mixer_handle, NULL, NULL)) < 0) {
GST_ERROR_OBJECT (GST_OBJECT (mixer), "Cannot register mixer elements.");
goto error;
}
if ((err = snd_mixer_load(mixer->mixer_handle)) < 0) {
GST_ERROR_OBJECT (GST_OBJECT (mixer), "Cannot load mixer settings.");
goto error;
}
return;
error:
snd_mixer_close (mixer->mixer_handle);
mixer->mixer_handle = (snd_mixer_t *) -1;
}
void
gst_alsa_mixer_track_free (GstMixerTrack *track)
static void
gst_alsa_mixer_dispose (GObject * object)
{
g_free (track->label);
g_free (track);
GstAlsaMixer *mixer = GST_ALSA_MIXER (object);
if (((gint) mixer->mixer_handle) != -1) return;
snd_mixer_close (mixer->mixer_handle);
mixer->mixer_handle = (snd_mixer_t *) -1;
G_OBJECT_CLASS (parent_class)->dispose (object);
}
void
static void
gst_alsa_interface_init (GstInterfaceClass *klass)
{
/* default virtual functions */
klass->supported = gst_alsa_mixer_supported;
}
void
static void
gst_alsa_mixer_interface_init (GstMixerClass *klass)
{
/* default virtual functions */
/* set up the interface hooks */
klass->list_tracks = gst_alsa_mixer_list_tracks;
klass->set_volume = gst_alsa_mixer_set_volume;
klass->get_volume = gst_alsa_mixer_get_volume;
......@@ -114,22 +196,109 @@ gst_alsa_mixer_interface_init (GstMixerClass *klass)
klass->set_record = gst_alsa_mixer_set_record;
}
static gboolean
gboolean
gst_alsa_mixer_supported (GstInterface *iface, GType iface_type)
{
g_assert (iface_type == GST_TYPE_MIXER);
return (((gint) GST_ALSA (iface)->mixer_handle) != -1);
return (((gint) GST_ALSA_MIXER (iface)->mixer_handle) != -1);
}
static void
gst_alsa_mixer_build_list (GstAlsaMixer *mixer)
{
gint i, count;
snd_mixer_elem_t *element;
g_return_if_fail (((gint) mixer->mixer_handle) != -1);
count = snd_mixer_get_count(mixer->mixer_handle);
element = snd_mixer_first_elem(mixer->mixer_handle);
/* build track list */
for (i = 0; i < count; i++) {
gint channels = 0;
if (! snd_mixer_selem_is_active(element)) continue;
/* find out if this element can be an input */
if (snd_mixer_selem_has_capture_channel(element, 0) ||
snd_mixer_selem_has_capture_switch(element) ||
snd_mixer_selem_is_capture_mono(element)) {
while (snd_mixer_selem_has_capture_channel(element, channels))
channels++;
GstMixerTrack *track = gst_alsa_mixer_track_new
(element, i, channels, GST_MIXER_TRACK_INPUT);
mixer->tracklist = g_list_append (mixer->tracklist, track);
}
/* find out if this element can also be an output */
channels = 0;
if (snd_mixer_selem_has_playback_channel(element, 0) ||
snd_mixer_selem_has_playback_switch(element) ||
snd_mixer_selem_is_playback_mono(element)) {
while (snd_mixer_selem_has_playback_channel(element, channels))
channels++;
GstMixerTrack *track = gst_alsa_mixer_track_new
(element, i, channels, GST_MIXER_TRACK_OUTPUT);
mixer->tracklist = g_list_append (mixer->tracklist, track);
}
element = snd_mixer_elem_next(element);
}
}
static void
gst_alsa_mixer_free_list (GstAlsaMixer *mixer)
{
g_return_if_fail (((gint) mixer->mixer_handle) != -1);
g_list_foreach (mixer->tracklist, (GFunc) gst_alsa_mixer_track_free, NULL);
g_list_free (mixer->tracklist);
mixer->tracklist = NULL;
}
/*** GSTREAMER FUNCTIONS ******************************************************/
static GstElementStateReturn
gst_alsa_mixer_change_state (GstElement *element)
{
GstAlsaMixer *this;
g_return_val_if_fail (element != NULL, FALSE);
this = GST_ALSA_MIXER (element);
switch (GST_STATE_TRANSITION (element)) {
case GST_STATE_NULL_TO_READY:
gst_alsa_mixer_build_list (this); break;
case GST_STATE_READY_TO_NULL:
gst_alsa_mixer_free_list (this); break;
default:
g_assert_not_reached();
}
if (GST_ELEMENT_CLASS (parent_class)->change_state)
return GST_ELEMENT_CLASS (parent_class)->change_state (element);
return GST_STATE_SUCCESS;
}
/*** INTERFACE IMPLEMENTATION *************************************************/
static const GList *
gst_alsa_mixer_list_tracks (GstMixer *mixer)
{
GstAlsa *alsa = GST_ALSA (mixer);
GstAlsaMixer *alsa_mixer = GST_ALSA_MIXER (mixer);
g_return_val_if_fail (((gint) alsa->mixer_handle) != -1, NULL);
g_return_val_if_fail (((gint) alsa_mixer->mixer_handle) != -1, NULL);
return (const GList *) GST_ALSA (mixer)->tracklist;
return (const GList *) alsa_mixer->tracklist;
}
static void
......@@ -138,10 +307,10 @@ gst_alsa_mixer_get_volume (GstMixer *mixer,
gint *volumes)
{
gint i;
GstAlsa *alsa = GST_ALSA (mixer);
GstAlsaMixer *alsa_mixer = GST_ALSA_MIXER (mixer);
GstAlsaMixerTrack *alsa_track = (GstAlsaMixerTrack *) track;
g_return_if_fail (((gint) alsa->mixer_handle) != -1);
g_return_if_fail (((gint) alsa_mixer->mixer_handle) != -1);
if (track->flags & GST_MIXER_TRACK_MUTE) {
for (i = 0; i < track->num_channels; i++)
......@@ -166,10 +335,10 @@ gst_alsa_mixer_set_volume (GstMixer *mixer,
gint *volumes)
{
gint i;
GstAlsa *alsa = GST_ALSA (mixer);
GstAlsaMixer *alsa_mixer = GST_ALSA_MIXER (mixer);
GstAlsaMixerTrack *alsa_track = (GstAlsaMixerTrack *) track;
g_return_if_fail (((gint) alsa->mixer_handle) != -1);
g_return_if_fail (((gint) alsa_mixer->mixer_handle) != -1);
/* only set the volume with ALSA lib if the track isn't muted. */
if (! (track->flags & GST_MIXER_TRACK_MUTE)) {
......@@ -191,10 +360,10 @@ gst_alsa_mixer_set_mute (GstMixer *mixer,
gboolean mute)
{
gint i;
GstAlsa *alsa = GST_ALSA (mixer);
GstAlsaMixer *alsa_mixer = GST_ALSA_MIXER (mixer);
GstAlsaMixerTrack *alsa_track = (GstAlsaMixerTrack *) track;
g_return_if_fail (((gint) alsa->mixer_handle) != -1);
g_return_if_fail (((gint) alsa_mixer->mixer_handle) != -1);
if (mute) {
track->flags |= GST_MIXER_TRACK_MUTE;
......@@ -224,10 +393,9 @@ gst_alsa_mixer_set_record (GstMixer *mixer,
GstMixerTrack *track,
gboolean record)
{
GstAlsa *alsa = GST_ALSA (mixer);
/* GstAlsaMixerTrack *alsa_track = (GstAlsaMixerTrack *) track; */
GstAlsaMixer *alsa_mixer = GST_ALSA_MIXER (mixer);
g_return_if_fail (((gint) alsa->mixer_handle) != -1);
g_return_if_fail (((gint) alsa_mixer->mixer_handle) != -1);
if (record) {
track->flags |= GST_MIXER_TRACK_RECORD;
......@@ -235,83 +403,3 @@ gst_alsa_mixer_set_record (GstMixer *mixer,
track->flags &= ~GST_MIXER_TRACK_RECORD;
}
}
void
gst_alsa_mixer_build_list (GstAlsa *alsa)
{
gint i, count, err;
snd_mixer_elem_t *element;
/* set up this mixer. */
if ((gint) alsa->mixer_handle == -1) {
if ((err = snd_mixer_open(&alsa->mixer_handle, 0)) != 0) {
GST_ERROR_OBJECT (GST_OBJECT (alsa), "Cannot open mixer device.");
alsa->mixer_handle = (snd_mixer_t *) -1;
return;
}
}
if ((err = snd_mixer_attach(alsa->mixer_handle, alsa->device)) != 0) {
GST_ERROR_OBJECT (GST_OBJECT (alsa),
"Cannot attach mixer to sound device '%s'.",
alsa->device);
return;
}
if ((err = snd_mixer_selem_register(alsa->mixer_handle, NULL, NULL)) != 0) {
GST_ERROR_OBJECT (GST_OBJECT (alsa), "Cannot register mixer elements.");
return;
}
if ((err = snd_mixer_load(alsa->mixer_handle)) != 0) {
GST_ERROR_OBJECT (GST_OBJECT (alsa), "Cannot load mixer settings.");
return;
}
count = snd_mixer_get_count(alsa->mixer_handle);
element = snd_mixer_first_elem(alsa->mixer_handle);
/* build track list */
for (i = 0; i < count; i++) {
GstMixerTrack *track;
gint channels = 0;
gboolean input = FALSE;
gint flags = 0;
/* find out if this is a capture track */
if (snd_mixer_selem_has_capture_channel(element, 0) ||
snd_mixer_selem_has_capture_switch(element) ||
snd_mixer_selem_is_capture_mono(element)) input = TRUE;
flags = (input ? GST_MIXER_TRACK_INPUT : GST_MIXER_TRACK_OUTPUT);
if (input) {
while (snd_mixer_selem_has_capture_channel(element, channels))
channels++;
} else {
while (snd_mixer_selem_has_playback_channel(element, channels))
channels++;
}
/* add track to list */
track = gst_alsa_mixer_track_new (alsa, i, channels, flags);
alsa->tracklist = g_list_append (alsa->tracklist, track);
element = snd_mixer_elem_next(element);
}
}
void
gst_alsa_mixer_free_list (GstAlsa *alsa)
{
g_return_if_fail (((gint) alsa->mixer_handle) != -1);
g_list_foreach (alsa->tracklist, (GFunc) gst_alsa_mixer_track_free, NULL);
g_list_free (alsa->tracklist);
alsa->tracklist = NULL;
snd_mixer_close (alsa->mixer_handle);
alsa->mixer_handle = (snd_mixer_t *) -1;
}
/*
/* ALSA mixer interface implementation.
* Copyright (C) 2003 Leif Johnson <leif@ambient.2y.net>
*
* This library is free software; you can redistribute it and/or
......@@ -20,32 +20,31 @@
#define __GST_ALSA_MIXER_H__
#include "gstalsa.h"
#include "gstalsamixertrack.h"
#include <gst/mixer/mixer.h>
G_BEGIN_DECLS
#define GST_ALSA_MIXER_TRACK(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ALSA_MIXER,GstAlsaMixer))
#define GST_ALSA_MIXER_TRACK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ALSA_MIXER,GstAlsaMixerClass))
#define GST_IS_ALSA_MIXER_TRACK(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ALSA_MIXER))
#define GST_IS_ALSA_MIXER_TRACK_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_ALSA_MIXER))
#define GST_TYPE_ALSA_MIXER_TRACK (gst_alsa_mixer_get_type())
typedef struct _GstAlsaMixerTrack GstAlsaMixerTrack;
typedef struct _GstAlsaMixerTrackClass GstAlsaMixerTrackClass;
struct _GstAlsaMixerTrack {
GstMixerTrack parent;
snd_mixer_elem_t *element; /* the ALSA mixer element for this track */
gint track_num;
gint min_rec_volume, max_rec_volume;
gint volumes[GST_ALSA_MAX_CHANNELS];
#define GST_ALSA_MIXER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ALSA_MIXER,GstAlsaMixer))
#define GST_ALSA_MIXER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ALSA_MIXER,GstAlsaMixerClass))
#define GST_IS_ALSA_MIXER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ALSA_MIXER))
#define GST_IS_ALSA_MIXER_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_ALSA_MIXER))
#define GST_TYPE_ALSA_MIXER (gst_alsa_mixer_get_type())
typedef struct _GstAlsaMixer GstAlsaMixer;
typedef struct _GstAlsaMixerClass GstAlsaMixerClass;
struct _GstAlsaMixer {
GstAlsa parent;
GList * tracklist; /* list of available tracks */
snd_mixer_t * mixer_handle;
};
struct _GstAlsaMixerTrackClass {
GstMixerTrackClass parent;
struct _GstAlsaMixerClass {
GstAlsaClass parent;
};
GType gst_alsa_mixer_get_type (void);
GType gst_alsa_mixer_get_type (void);
G_END_DECLS
......
/* ALSA mixer track 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 "gstalsamixertrack.h"
static void gst_alsa_mixer_track_init (GstAlsaMixerTrack * alsa_track);
static void gst_alsa_mixer_track_dispose (GObject * object);
static void gst_alsa_mixer_track_class_init (gpointer g_class,
gpointer class_data);
static GstMixerTrackClass *parent_class = NULL;
GType
gst_alsa_mixer_track_get_type (void)
{
static GType track_type = 0;
if (!track_type) {
static const GTypeInfo track_info = {