]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-audio-sink.c
Add API to get and set the volume
[empathy.git] / libempathy-gtk / empathy-audio-sink.c
1 /*
2  * empathy-gst-audio-sink.c - Source for EmpathyGstAudioSink
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 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  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <gst/farsight/fs-element-added-notifier.h>
26
27 #include "empathy-audio-sink.h"
28
29
30 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
31
32 /* signal enum */
33 #if 0
34 enum
35 {
36     LAST_SIGNAL
37 };
38
39 static guint signals[LAST_SIGNAL] = {0};
40 #endif
41
42 enum {
43   PROP_VOLUME = 1,
44 };
45
46 /* private structure */
47 typedef struct _EmpathyGstAudioSinkPrivate EmpathyGstAudioSinkPrivate;
48
49 struct _EmpathyGstAudioSinkPrivate
50 {
51   gboolean dispose_has_run;
52   GstElement *sink;
53   GstElement *volume;
54   FsElementAddedNotifier *notifier;
55 };
56
57 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
58   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
59   EmpathyGstAudioSinkPrivate))
60
61 static void
62 empathy_audio_sink_element_added_cb (FsElementAddedNotifier *notifier,
63   GstBin *bin, GstElement *element, EmpathyGstAudioSink *self)
64 {
65   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
66
67   if (g_object_class_find_property (G_OBJECT_CLASS (element), "volume"))
68     {
69       priv->volume = element;
70     }
71 }
72
73 static void
74 empathy_audio_sink_init (EmpathyGstAudioSink *obj)
75 {
76   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (obj);
77   GstElement *resample;
78   GstPad *ghost, *sink;
79
80   priv->notifier = fs_element_added_notifier_new ();
81   g_signal_connect (priv->notifier, "element-added",
82     G_CALLBACK (empathy_audio_sink_element_added_cb), obj);
83
84   resample = gst_element_factory_make ("audioresample", NULL);
85   priv->volume = gst_element_factory_make ("volume", NULL);
86   priv->sink = gst_element_factory_make ("gconfaudiosink", NULL);
87
88   fs_element_added_notifier_add (priv->notifier, GST_BIN (priv->sink));
89
90
91   gst_bin_add_many (GST_BIN (obj), resample, priv->volume, priv->sink, NULL);
92   gst_element_link_many (resample, priv->volume, priv->sink, NULL);
93
94   sink = gst_element_get_static_pad (resample, "sink");
95
96   ghost = gst_ghost_pad_new ("sink", sink);
97   gst_element_add_pad (GST_ELEMENT (obj), ghost);
98
99   gst_object_unref (G_OBJECT (sink));
100 }
101
102 static void empathy_audio_sink_dispose (GObject *object);
103 static void empathy_audio_sink_finalize (GObject *object);
104
105 static void
106 empathy_audio_sink_set_property (GObject *object,
107   guint property_id, const GValue *value, GParamSpec *pspec)
108 {
109   switch (property_id)
110     {
111       case PROP_VOLUME:
112         empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (object),
113           g_value_get_double (value));
114         break;
115       default:
116         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
117     }
118 }
119
120 static void
121 empathy_audio_sink_get_property (GObject *object,
122   guint property_id, GValue *value, GParamSpec *pspec)
123 {
124   switch (property_id)
125     {
126       case PROP_VOLUME:
127         g_value_set_double (value,
128           empathy_audio_sink_get_volume (EMPATHY_GST_AUDIO_SINK (object)));
129         break;
130       default:
131         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
132     }
133 }
134
135
136
137 static void
138 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
139   *empathy_audio_sink_class)
140 {
141   GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
142   GParamSpec *param_spec;
143
144   g_type_class_add_private (empathy_audio_sink_class,
145     sizeof (EmpathyGstAudioSinkPrivate));
146
147   object_class->dispose = empathy_audio_sink_dispose;
148   object_class->finalize = empathy_audio_sink_finalize;
149
150   object_class->set_property = empathy_audio_sink_set_property;
151   object_class->get_property = empathy_audio_sink_get_property;
152
153   param_spec = g_param_spec_double ("volume", "Volume", "volume factory",
154     0.0, 5.0, 1.0,
155     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
156   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
157 }
158
159 void
160 empathy_audio_sink_dispose (GObject *object)
161 {
162   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
163   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
164
165   if (priv->dispose_has_run)
166     return;
167
168   priv->dispose_has_run = TRUE;
169
170   if (priv->notifier != NULL)
171     g_object_unref (priv->notifier);
172   priv->notifier = NULL;
173
174
175   if (G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose)
176     G_OBJECT_CLASS (empathy_audio_sink_parent_class)->dispose (object);
177 }
178
179 void
180 empathy_audio_sink_finalize (GObject *object)
181 {
182   //EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (object);
183   //EmpathyGstAudioSinkPrivate *priv =
184   //  EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
185
186   /* free any data held directly by the object here */
187
188   G_OBJECT_CLASS (empathy_audio_sink_parent_class)->finalize (object);
189 }
190
191 GstElement *
192 empathy_audio_sink_new (void)
193 {
194   return GST_ELEMENT (g_object_new (EMPATHY_TYPE_GST_AUDIO_SINK, NULL));
195 }
196
197 void
198 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
199 {
200   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
201
202   g_object_set (G_OBJECT (priv->volume), "volume", volume, NULL);
203 }
204
205 gdouble
206 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
207 {
208   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
209   gdouble volume;
210
211   g_object_get (G_OBJECT (priv->volume), "volume", &volume, NULL);
212
213   return volume;
214 }