compatibility fix for new GST_DEBUG stuff.
[platform/upstream/gstreamer.git] / ext / artsd / gstartsdsink.c
1 /* GStreamer
2  * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
3  *
4  * Based on example.c:
5  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "gstartsdsink.h"
27
28 /* elementfactory information */
29 static GstElementDetails artsdsink_details = {
30   "aRtsd audio sink",
31   "Sink/Audio",
32   "LGPL",
33   "Plays audio to an aRts server",
34   VERSION,
35   "Richard Boulton <richard-gst@tartarus.org>",
36   "(C) 2001",
37 };
38
39 /* Signals and args */
40 enum {
41   /* FILL ME */
42   LAST_SIGNAL
43 };
44
45 enum {
46   ARG_0,
47   ARG_MUTE,
48   ARG_DEPTH,
49   ARG_CHANNELS,
50   ARG_RATE,
51   ARG_NAME,
52 };
53
54 GST_PAD_TEMPLATE_FACTORY (sink_factory,
55   "sink",                                       /* the name of the pads */
56   GST_PAD_SINK,                         /* type of the pad */
57   GST_PAD_ALWAYS,                               /* ALWAYS/SOMETIMES */
58   GST_CAPS_NEW (
59     "artsdsink_sink",                           /* the name of the caps */
60     "audio/raw",                                /* the mime type of the caps */
61       "format",     GST_PROPS_STRING ("int"),
62       "law",        GST_PROPS_INT (0),
63       "endianness", GST_PROPS_INT (G_BYTE_ORDER),
64       "signed",     GST_PROPS_BOOLEAN (FALSE),
65       "width",      GST_PROPS_INT (8),
66       "depth",      GST_PROPS_INT (8),
67       "rate",       GST_PROPS_INT_RANGE (8000, 96000),
68       "channels",   GST_PROPS_LIST (GST_PROPS_INT (1), GST_PROPS_INT (2))
69   ),
70   GST_CAPS_NEW (
71     "artsdsink_sink",                           /* the name of the caps */
72     "audio/raw",                                /* the mime type of the caps */
73       "format",     GST_PROPS_STRING ("int"),
74       "law",        GST_PROPS_INT (0),
75       "endianness", GST_PROPS_INT (G_BYTE_ORDER),
76       "signed",     GST_PROPS_BOOLEAN (TRUE),
77       "width",      GST_PROPS_INT (16),
78       "depth",      GST_PROPS_INT (16),
79       "rate",       GST_PROPS_INT_RANGE (8000, 96000),
80       "channels",   GST_PROPS_LIST (GST_PROPS_INT (1), GST_PROPS_INT (2))
81   )
82 );
83
84 static void                     gst_artsdsink_class_init                (GstArtsdsinkClass *klass);
85 static void                     gst_artsdsink_init                      (GstArtsdsink *artsdsink);
86
87 static gboolean                 gst_artsdsink_open_audio                (GstArtsdsink *sink);
88 static void                     gst_artsdsink_close_audio               (GstArtsdsink *sink);
89 static GstElementStateReturn    gst_artsdsink_change_state              (GstElement *element);
90 static gboolean                 gst_artsdsink_sync_parms                (GstArtsdsink *artsdsink);
91
92 static void                     gst_artsdsink_chain                     (GstPad *pad, GstBuffer *buf);
93
94 static void                     gst_artsdsink_set_property              (GObject *object, guint prop_id, 
95                                                                          const GValue *value, GParamSpec *pspec);
96 static void                     gst_artsdsink_get_property              (GObject *object, guint prop_id, 
97                                                                          GValue *value, GParamSpec *pspec);
98
99 #define GST_TYPE_ARTSDSINK_DEPTHS (gst_artsdsink_depths_get_type())
100 static GType
101 gst_artsdsink_depths_get_type (void)
102 {
103   static GType artsdsink_depths_type = 0;
104   static GEnumValue artsdsink_depths[] = {
105     {8, "8", "8 Bits"},
106     {16, "16", "16 Bits"},
107     {0, NULL, NULL},
108   };
109   if (!artsdsink_depths_type) {
110     artsdsink_depths_type = g_enum_register_static("GstArtsdsinkDepths", artsdsink_depths);
111   }
112   return artsdsink_depths_type;
113 }
114
115 #define GST_TYPE_ARTSDSINK_CHANNELS (gst_artsdsink_channels_get_type())
116 static GType
117 gst_artsdsink_channels_get_type (void)
118 {
119   static GType artsdsink_channels_type = 0;
120   static GEnumValue artsdsink_channels[] = {
121     {1, "1", "Mono"},
122     {2, "2", "Stereo"},
123     {0, NULL, NULL},
124   };
125   if (!artsdsink_channels_type) {
126     artsdsink_channels_type = g_enum_register_static("GstArtsdsinkChannels", artsdsink_channels);
127   }
128   return artsdsink_channels_type;
129 }
130
131
132 static GstElementClass *parent_class = NULL;
133 /*static guint gst_artsdsink_signals[LAST_SIGNAL] = { 0 }; */
134
135 GType
136 gst_artsdsink_get_type (void)
137 {
138   static GType artsdsink_type = 0;
139
140   if (!artsdsink_type) {
141     static const GTypeInfo artsdsink_info = {
142       sizeof(GstArtsdsinkClass),      NULL,
143       NULL,
144       (GClassInitFunc)gst_artsdsink_class_init,
145       NULL,
146       NULL,
147       sizeof(GstArtsdsink),
148       0,
149       (GInstanceInitFunc)gst_artsdsink_init,
150     };
151     artsdsink_type = g_type_register_static(GST_TYPE_ELEMENT, "GstArtsdsink", &artsdsink_info, 0);
152   }
153   return artsdsink_type;
154 }
155
156 static void
157 gst_artsdsink_class_init (GstArtsdsinkClass *klass)
158 {
159   GObjectClass *gobject_class;
160   GstElementClass *gstelement_class;
161
162   gobject_class = (GObjectClass*)klass;
163   gstelement_class = (GstElementClass*)klass;
164
165   parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
166
167   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MUTE,
168     g_param_spec_boolean("mute","mute","mute",
169                          TRUE,G_PARAM_READWRITE)); /* CHECKME */
170   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DEPTH,
171     g_param_spec_enum("depth","depth","depth",
172                       GST_TYPE_ARTSDSINK_DEPTHS,16,G_PARAM_READWRITE)); /* CHECKME! */
173   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CHANNELS,
174     g_param_spec_enum("channels","channels","channels",
175                       GST_TYPE_ARTSDSINK_CHANNELS,2,G_PARAM_READWRITE)); /* CHECKME! */
176   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_RATE,
177     g_param_spec_int("frequency","frequency","frequency",
178                      G_MININT,G_MAXINT,0,G_PARAM_READWRITE)); /* CHECKME */
179   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NAME,
180     g_param_spec_string("name","name","name",
181                         NULL, G_PARAM_READWRITE)); /* CHECKME */
182
183   gobject_class->set_property = gst_artsdsink_set_property;
184   gobject_class->get_property = gst_artsdsink_get_property;
185
186   gstelement_class->change_state = gst_artsdsink_change_state;
187 }
188
189 static void
190 gst_artsdsink_init(GstArtsdsink *artsdsink)
191 {
192   artsdsink->sinkpad = gst_pad_new_from_template (
193                   GST_PAD_TEMPLATE_GET (sink_factory), "sink");
194   gst_element_add_pad(GST_ELEMENT(artsdsink), artsdsink->sinkpad);
195   gst_pad_set_chain_function(artsdsink->sinkpad, gst_artsdsink_chain);
196
197   artsdsink->connected = FALSE;
198   artsdsink->mute = FALSE;
199
200   /* FIXME: get default from somewhere better than just putting them inline. */
201   artsdsink->signd = TRUE;
202   artsdsink->depth = 16;
203   artsdsink->channels = 2;
204   artsdsink->frequency = 44100;
205   artsdsink->connect_name = NULL;
206 }
207
208 static gboolean
209 gst_artsdsink_sync_parms (GstArtsdsink *artsdsink)
210 {
211   g_return_val_if_fail (artsdsink != NULL, FALSE);
212   g_return_val_if_fail (GST_IS_ARTSDSINK (artsdsink), FALSE);
213
214   if (!artsdsink->connected) return TRUE;
215
216   /* Need to set stream to use new parameters: only way to do this is to reopen. */
217   gst_artsdsink_close_audio (artsdsink);
218   return gst_artsdsink_open_audio (artsdsink);
219 }
220
221
222 static void
223 gst_artsdsink_chain (GstPad *pad, GstBuffer *buf)
224 {
225   GstArtsdsink *artsdsink;
226
227   g_return_if_fail(pad != NULL);
228   g_return_if_fail(GST_IS_PAD(pad));
229   g_return_if_fail(buf != NULL);
230
231   artsdsink = GST_ARTSDSINK (gst_pad_get_parent (pad));
232
233   if (GST_BUFFER_DATA (buf) != NULL) {
234     gst_trace_add_entry(NULL, 0, GPOINTER_TO_INT(buf), "artsdsink: writing to server");
235     if (!artsdsink->mute && artsdsink->connected) {
236       int bytes;
237       void * bufptr = GST_BUFFER_DATA (buf);
238       int bufsize = GST_BUFFER_SIZE (buf);
239       GST_DEBUG ("artsdsink: stream=%p data=%p size=%d",
240                  artsdsink->stream, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
241
242       do {
243         bytes = arts_write (artsdsink->stream, bufptr, bufsize);
244         if(bytes < 0) {
245           fprintf(stderr,"arts_write error: %s\n", arts_error_text(bytes));
246           gst_buffer_unref (buf);
247           return;
248         }
249         bufptr += bytes;
250         bufsize -= bytes;
251       } while (bufsize > 0);
252     }
253   }
254   gst_buffer_unref (buf);
255 }
256
257 static void
258 gst_artsdsink_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
259 {
260   GstArtsdsink *artsdsink;
261
262   /* it's not null if we got it, but it might not be ours */
263   g_return_if_fail(GST_IS_ARTSDSINK(object));
264   artsdsink = GST_ARTSDSINK(object);
265
266   switch (prop_id) {
267     case ARG_MUTE:
268       artsdsink->mute = g_value_get_boolean (value);
269       break;
270     case ARG_DEPTH:
271       artsdsink->depth = g_value_get_enum (value);
272       gst_artsdsink_sync_parms (artsdsink);
273       break;
274     case ARG_CHANNELS:
275       artsdsink->channels = g_value_get_enum (value);
276       gst_artsdsink_sync_parms (artsdsink);
277       break;
278     case ARG_RATE:
279       artsdsink->frequency = g_value_get_int (value);
280       gst_artsdsink_sync_parms (artsdsink);
281       break;
282     case ARG_NAME:
283       if (artsdsink->connect_name != NULL) g_free(artsdsink->connect_name);
284       if (g_value_get_string (value) == NULL)
285           artsdsink->connect_name = NULL;
286       else
287           artsdsink->connect_name = g_strdup (g_value_get_string (value));
288       break;
289     default:
290       break;
291   }
292 }
293
294 static void
295 gst_artsdsink_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
296 {
297   GstArtsdsink *artsdsink;
298
299   /* it's not null if we got it, but it might not be ours */
300   g_return_if_fail(GST_IS_ARTSDSINK(object));
301   artsdsink = GST_ARTSDSINK(object);
302
303   switch (prop_id) {
304     case ARG_MUTE:
305       g_value_set_boolean (value, artsdsink->mute);
306       break;
307     case ARG_DEPTH:
308       g_value_set_enum (value, artsdsink->depth);
309       break;
310     case ARG_CHANNELS:
311       g_value_set_enum (value, artsdsink->channels);
312       break;
313     case ARG_RATE:
314       g_value_set_int (value, artsdsink->frequency);
315       break;
316     case ARG_NAME:
317       g_value_set_string (value, artsdsink->connect_name);
318       break;
319     default:
320       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321       break;
322   }
323 }
324
325 static gboolean
326 plugin_init (GModule *module, GstPlugin *plugin)
327 {
328   GstElementFactory *factory;
329
330   factory = gst_element_factory_new("artsdsink", GST_TYPE_ARTSDSINK,
331                                    &artsdsink_details);
332   g_return_val_if_fail(factory != NULL, FALSE);
333
334   gst_element_factory_add_pad_template(factory, GST_PAD_TEMPLATE_GET (sink_factory));
335
336   gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
337
338   return TRUE;
339 }
340
341 GstPluginDesc plugin_desc = {
342   GST_VERSION_MAJOR,
343   GST_VERSION_MINOR,
344   "artsdsink",
345   plugin_init
346 };
347
348 static gboolean
349 gst_artsdsink_open_audio (GstArtsdsink *sink)
350 {
351   const char * connname = "gstreamer";
352   int errcode;
353
354   /* Name used by aRtsd for this connection. */
355   if (sink->connect_name != NULL) connname = sink->connect_name;
356
357   /* FIXME: this should only ever happen once per process. */
358   /* Really, artsc needs to be made thread safe to fix this (and other related */
359   /* problems). */
360   errcode = arts_init();
361   if(errcode < 0) {
362       fprintf(stderr,"arts_init error: %s\n", arts_error_text(errcode));
363       return FALSE;
364   }
365
366   GST_DEBUG ("artsdsink: attempting to open connection to aRtsd server");
367   sink->stream = arts_play_stream(sink->frequency, sink->depth,
368                                   sink->channels, connname);
369   /* FIXME: check connection */
370   /*   GST_DEBUG ("artsdsink: can't open connection to aRtsd server"); */
371
372   GST_FLAG_SET (sink, GST_ARTSDSINK_OPEN);
373   sink->connected = TRUE;
374
375   return TRUE;
376 }
377
378 static void
379 gst_artsdsink_close_audio (GstArtsdsink *sink)
380 {
381   if (!sink->connected) return;
382
383   arts_close_stream(sink->stream);
384   arts_free();
385   GST_FLAG_UNSET (sink, GST_ARTSDSINK_OPEN);
386   sink->connected = FALSE;
387
388   g_print("artsdsink: closed connection\n");
389 }
390
391 static GstElementStateReturn
392 gst_artsdsink_change_state (GstElement *element)
393 {
394   g_return_val_if_fail (GST_IS_ARTSDSINK (element), FALSE);
395
396   /* if going down into NULL state, close the stream if it's open */
397   if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
398     if (GST_FLAG_IS_SET (element, GST_ARTSDSINK_OPEN))
399       gst_artsdsink_close_audio (GST_ARTSDSINK (element));
400     /* otherwise (READY or higher) we need to open the stream */
401   } else {
402     if (!GST_FLAG_IS_SET (element, GST_ARTSDSINK_OPEN)) {
403       if (!gst_artsdsink_open_audio (GST_ARTSDSINK (element)))
404         return GST_STATE_FAILURE;
405     }
406   }
407
408   if (GST_ELEMENT_CLASS (parent_class)->change_state)
409     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
410   return GST_STATE_SUCCESS;
411 }
412