alsamixer: embed static mutexes into the mixer structure

instead of allocating them dynamically
This commit is contained in:
Tim-Philipp Müller 2011-12-04 20:21:26 +00:00
parent ff6cc8af82
commit 9c307bccc5
2 changed files with 44 additions and 49 deletions

View file

@ -131,14 +131,14 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
count = snd_mixer_get_count (handle);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
/* Check if we have a playback mixer labelled as 'Master' */
element = snd_mixer_first_elem (handle);
for (i = 0; i < count; i++) {
if (snd_mixer_selem_has_playback_volume (element) &&
strcmp (snd_mixer_selem_get_name (element), "Master") == 0) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
@ -149,7 +149,7 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
for (i = 0; i < count; i++) {
if (snd_mixer_selem_has_playback_volume (element) &&
strcmp (snd_mixer_selem_get_name (element), "Front") == 0) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
@ -160,7 +160,7 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
for (i = 0; i < count; i++) {
if (snd_mixer_selem_has_playback_volume (element) &&
strcmp (snd_mixer_selem_get_name (element), "PCM") == 0) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
@ -171,7 +171,7 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
for (i = 0; i < count; i++) {
if (snd_mixer_selem_has_playback_volume (element) &&
strcmp (snd_mixer_selem_get_name (element), "Speaker") == 0) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
@ -184,7 +184,7 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
if (snd_mixer_selem_has_playback_volume (element) &&
snd_mixer_selem_has_playback_switch (element) &&
!snd_mixer_selem_is_playback_mono (element)) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
@ -195,7 +195,7 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
for (i = 0; i < count; i++) {
if (snd_mixer_selem_has_playback_volume (element) &&
snd_mixer_selem_has_playback_switch (element)) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
@ -205,13 +205,13 @@ gst_alsa_mixer_find_master_mixer (GstAlsaMixer * mixer, snd_mixer_t * handle)
element = snd_mixer_first_elem (handle);
for (i = 0; i < count; i++) {
if (snd_mixer_selem_has_playback_volume (element)) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return element;
}
element = snd_mixer_elem_next (element);
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
/* Looks like we're out of luck ... */
return NULL;
}
@ -223,7 +223,7 @@ gst_alsa_mixer_update (GstAlsaMixer * mixer, snd_mixer_elem_t * elem)
g_return_if_fail (mixer != NULL);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
for (item = mixer->tracklist; item != NULL; item = item->next) {
if (GST_IS_ALSA_MIXER_TRACK (item->data)) {
@ -239,7 +239,7 @@ gst_alsa_mixer_update (GstAlsaMixer * mixer, snd_mixer_elem_t * elem)
}
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
static int
@ -288,7 +288,7 @@ gst_alsa_mixer_ensure_track_list (GstAlsaMixer * mixer)
if (mixer->tracklist)
return;
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
master = gst_alsa_mixer_find_master_mixer (mixer, mixer->handle);
@ -410,7 +410,7 @@ gst_alsa_mixer_ensure_track_list (GstAlsaMixer * mixer)
snd_mixer_elem_set_callback_private (temp, mixer);
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
static void
@ -422,13 +422,13 @@ task_monitor_alsa (gpointer data)
GstAlsaMixer *mixer = (GstAlsaMixer *) data;
gint ret;
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
nfds = snd_mixer_poll_descriptors_count (mixer->handle);
if (nfds <= 0) {
GST_ERROR ("snd_mixer_poll_descriptors_count <= 0: %d", nfds);
/* FIXME: sleep ? stop monitoring ? */
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return;
}
@ -440,7 +440,7 @@ task_monitor_alsa (gpointer data)
GST_ELEMENT_ERROR (mixer, RESOURCE, READ, (NULL), ("alsa error: %s",
snd_strerror (rnfds)));
gst_task_pause (mixer->task);
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return;
}
@ -448,7 +448,7 @@ task_monitor_alsa (gpointer data)
pfds[rnfds].events = POLLIN | POLLPRI | POLLHUP | POLLERR;
pfds[rnfds].revents = 0;
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
GST_LOG ("task loop");
ret = poll (pfds, rnfds + 1, -1);
@ -459,7 +459,7 @@ task_monitor_alsa (gpointer data)
return;
}
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
ret =
snd_mixer_poll_descriptors_revents (mixer->handle, pfds, nfds, &revents);
@ -475,7 +475,7 @@ task_monitor_alsa (gpointer data)
gst_task_pause (mixer->task);
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
/* API */
@ -492,14 +492,11 @@ gst_alsa_mixer_new (const char *device, GstAlsaMixerDirection dir)
if (pipe (ret->pfd) == -1)
goto error;
ret->rec_mutex = g_new (GStaticRecMutex, 1);
g_static_rec_mutex_init (ret->rec_mutex);
ret->task_mutex = g_new (GStaticRecMutex, 1);
g_static_rec_mutex_init (ret->task_mutex);
g_static_rec_mutex_init (&ret->rec_mutex);
g_static_rec_mutex_init (&ret->task_mutex);
ret->task = gst_task_create (task_monitor_alsa, ret);
gst_task_set_lock (ret->task, ret->task_mutex);
gst_task_set_lock (ret->task, &ret->task_mutex);
ret->device = g_strdup (device);
ret->dir = dir;
@ -541,9 +538,7 @@ gst_alsa_mixer_free (GstAlsaMixer * mixer)
mixer->task = NULL;
}
g_static_rec_mutex_free (mixer->task_mutex);
g_free (mixer->task_mutex);
mixer->task_mutex = NULL;
g_static_rec_mutex_free (&mixer->task_mutex);
if (mixer->pfd[0] > 0) {
close (mixer->pfd[0]);
@ -581,9 +576,7 @@ gst_alsa_mixer_free (GstAlsaMixer * mixer)
mixer->handle = NULL;
}
g_static_rec_mutex_free (mixer->rec_mutex);
g_free (mixer->rec_mutex);
mixer->rec_mutex = NULL;
g_static_rec_mutex_free (&mixer->rec_mutex);
g_free (mixer);
}
@ -607,7 +600,7 @@ gst_alsa_mixer_get_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
g_return_if_fail (mixer->handle != NULL);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
gst_alsa_mixer_track_update (alsa_track);
@ -643,7 +636,7 @@ gst_alsa_mixer_get_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
volumes[i] = alsa_track->volumes[i];
}
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
static gboolean
@ -671,7 +664,7 @@ gst_alsa_mixer_set_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
g_return_if_fail (mixer->handle != NULL);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
gst_alsa_mixer_track_update (alsa_track);
@ -719,7 +712,7 @@ gst_alsa_mixer_set_volume (GstAlsaMixer * mixer, GstMixerTrack * track,
alsa_track->volumes[i] = volumes[i];
}
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
void
@ -730,12 +723,12 @@ gst_alsa_mixer_set_mute (GstAlsaMixer * mixer, GstMixerTrack * track,
g_return_if_fail (mixer->handle != NULL);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
gst_alsa_mixer_track_update (alsa_track);
if (! !(mute) == ! !(track->flags & GST_MIXER_TRACK_MUTE)) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return;
}
if (mute) {
@ -771,7 +764,7 @@ gst_alsa_mixer_set_mute (GstAlsaMixer * mixer, GstMixerTrack * track,
snd_mixer_selem_set_playback_volume (ctrl_track->element, i, vol);
}
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
void
@ -782,12 +775,12 @@ gst_alsa_mixer_set_record (GstAlsaMixer * mixer,
g_return_if_fail (mixer->handle != NULL);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
gst_alsa_mixer_track_update (alsa_track);
if (! !(record) == ! !(track->flags & GST_MIXER_TRACK_RECORD)) {
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
return;
}
@ -827,7 +820,7 @@ gst_alsa_mixer_set_record (GstAlsaMixer * mixer,
snd_mixer_selem_set_capture_volume (alsa_track->element, i, vol);
}
}
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
void
@ -849,9 +842,9 @@ gst_alsa_mixer_set_option (GstAlsaMixer * mixer,
if (idx == -1)
return;
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
snd_mixer_selem_set_enum_item (alsa_opts->element, 0, idx);
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
}
const gchar *
@ -862,9 +855,9 @@ gst_alsa_mixer_get_option (GstAlsaMixer * mixer, GstMixerOptions * opts)
GstAlsaMixerOptions *alsa_opts = GST_ALSA_MIXER_OPTIONS (opts);
g_return_val_if_fail (mixer->handle != NULL, NULL);
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
ret = snd_mixer_selem_get_enum_item (alsa_opts->element, 0, &idx);
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
if (ret == 0)
return g_list_nth_data (opts->values, idx);
else
@ -891,9 +884,9 @@ gst_alsa_mixer_update_option (GstAlsaMixer * mixer,
GST_WARNING ("Cannot send update notifications, no GstMixer * given");
return;
}
g_static_rec_mutex_lock (mixer->rec_mutex);
GST_ALSA_MIXER_LOCK (mixer);
ret = snd_mixer_selem_get_enum_item (alsa_opts->element, 0, &idx);
g_static_rec_mutex_unlock (mixer->rec_mutex);
GST_ALSA_MIXER_UNLOCK (mixer);
if (ret == 0) {
option = g_list_nth_data (GST_MIXER_OPTIONS (alsa_opts)->values, idx);
gst_mixer_option_changed (mixer->interface, GST_MIXER_OPTIONS (alsa_opts),

View file

@ -53,8 +53,8 @@ struct _GstAlsaMixer
snd_mixer_t * handle;
GstTask * task;
GStaticRecMutex * task_mutex;
GStaticRecMutex * rec_mutex;
GStaticRecMutex task_mutex;
GStaticRecMutex rec_mutex;
int pfd[2];
@ -65,6 +65,8 @@ struct _GstAlsaMixer
GstAlsaMixerDirection dir;
};
#define GST_ALSA_MIXER_LOCK(mixer) g_static_rec_mutex_lock (&mixer->rec_mutex)
#define GST_ALSA_MIXER_UNLOCK(mixer) g_static_rec_mutex_unlock (&mixer->rec_mutex)
GstAlsaMixer* gst_alsa_mixer_new (const gchar *device,
GstAlsaMixerDirection dir);