fix shout2send compilation problem with -Wall -Werror
[platform/upstream/gst-plugins-good.git] / ext / shout2 / gstshout2.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 #include "gstshout2.h"
24 #include <stdlib.h>
25 #include <string.h>
26
27 /* elementfactory information */
28 static GstElementDetails shout2send_details = {
29   "An Icecast plugin",
30   "Sink/Network",
31   "Sends data to an icecast server",
32   "Wim Taymans <wim.taymans@chello.be>\n" "Pedro Corte-Real <typo@netcabo.pt>"
33 };
34
35 unsigned int audio_format = 100;
36
37 /* Shout2send signals and args */
38 enum
39 {
40   /* FILL ME */
41   SIGNAL_CONNECTION_PROBLEM,
42   LAST_SIGNAL
43 };
44
45 enum
46 {
47   ARG_0,
48   ARG_IP,                       /* the ip of the server */
49   ARG_PORT,                     /* the encoder port number on the server */
50   ARG_PASSWORD,                 /* the encoder password on the server */
51   ARG_PUBLIC,                   /* is this stream public? */
52   ARG_NAME,                     /* Name of the stream */
53   ARG_DESCRIPTION,              /* Description of the stream */
54   ARG_GENRE,                    /* Genre of the stream */
55
56   ARG_PROTOCOL,                 /* Protocol to connect with */
57
58   ARG_MOUNT,                    /* mountpoint of stream (icecast only) */
59   ARG_URL                       /* Url of stream (I'm guessing) */
60 };
61
62 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
63     GST_PAD_SINK,
64     GST_PAD_ALWAYS,
65     GST_STATIC_CAPS ("application/ogg; "
66         "audio/mpeg, mpegversion = (int) 1, layer = (int) [ 1, 3 ]")
67     );
68
69 static void gst_shout2send_class_init (GstShout2sendClass * klass);
70 static void gst_shout2send_base_init (GstShout2sendClass * klass);
71 static void gst_shout2send_init (GstShout2send * shout2send);
72
73 static void gst_shout2send_chain (GstPad * pad, GstData * _data);
74 static GstPadLinkReturn gst_shout2send_connect (GstPad * pad,
75     const GstCaps * caps);
76
77 static void gst_shout2send_set_property (GObject * object, guint prop_id,
78     const GValue * value, GParamSpec * pspec);
79 static void gst_shout2send_get_property (GObject * object, guint prop_id,
80     GValue * value, GParamSpec * pspec);
81
82 static GstElementStateReturn gst_shout2send_change_state (GstElement * element);
83
84 static GstElementClass *parent_class = NULL;
85
86 static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 };
87
88 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
89 static GType
90 gst_shout2send_protocol_get_type (void)
91 {
92   static GType shout2send_protocol_type = 0;
93   static GEnumValue shout2send_protocol[] = {
94     {SHOUT2SEND_PROTOCOL_XAUDIOCAST, "1",
95         "Xaudiocast Protocol (icecast 1.3.x)"},
96     {SHOUT2SEND_PROTOCOL_ICY, "2", "Icy Protocol (ShoutCast)"},
97     {SHOUT2SEND_PROTOCOL_HTTP, "3", "Http Protocol (icecast 2.x)"},
98     {0, NULL, NULL},
99   };
100
101   if (!shout2send_protocol_type) {
102     shout2send_protocol_type =
103         g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
104   }
105   return shout2send_protocol_type;
106 }
107
108 GType
109 gst_shout2send_get_type (void)
110 {
111   static GType shout2send_type = 0;
112
113   if (!shout2send_type) {
114     static const GTypeInfo shout2send_info = {
115       sizeof (GstShout2sendClass),
116       (GBaseInitFunc) gst_shout2send_base_init,
117       NULL,
118       (GClassInitFunc) gst_shout2send_class_init,
119       NULL,
120       NULL,
121       sizeof (GstShout2send),
122       0,
123       (GInstanceInitFunc) gst_shout2send_init,
124     };
125
126     shout2send_type =
127         g_type_register_static (GST_TYPE_ELEMENT, "GstShout2send",
128         &shout2send_info, 0);
129   }
130   return shout2send_type;
131 }
132
133 static void
134 gst_shout2send_base_init (GstShout2sendClass * klass)
135 {
136   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
137
138   gst_element_class_add_pad_template (element_class,
139       gst_static_pad_template_get (&sink_template));
140   gst_element_class_set_details (element_class, &shout2send_details);
141 }
142
143 static void
144 gst_shout2send_class_init (GstShout2sendClass * klass)
145 {
146   GObjectClass *gobject_class;
147   GstElementClass *gstelement_class;
148
149   gobject_class = (GObjectClass *) klass;
150   gstelement_class = (GstElementClass *) klass;
151
152   parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
153
154   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_IP, g_param_spec_string ("ip", "ip", "ip", NULL, G_PARAM_READWRITE));    /* CHECKME */
155   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT, g_param_spec_int ("port", "port", "port", 1, G_MAXUSHORT, 8000, G_PARAM_READWRITE));       /* CHECKME */
156
157   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PASSWORD, g_param_spec_string ("password", "password", "password", NULL, G_PARAM_READWRITE));    /* CHECKME */
158
159   /* metadata */
160   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NAME, g_param_spec_string ("name", "name", "name", NULL, G_PARAM_READWRITE));    /* CHECKME */
161
162   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESCRIPTION, g_param_spec_string ("description", "description", "description", NULL, G_PARAM_READWRITE));        /* CHECKME */
163
164   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GENRE, g_param_spec_string ("genre", "genre", "genre", NULL, G_PARAM_READWRITE));        /* CHECKME */
165
166   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
167       g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
168           GST_TYPE_SHOUT_PROTOCOL, SHOUT2SEND_PROTOCOL_HTTP,
169           G_PARAM_READWRITE));
170
171
172   /* icecast only */
173   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MOUNT, g_param_spec_string ("mount", "mount", "mount", NULL, G_PARAM_READWRITE));        /* CHECKME */
174
175   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_URL, g_param_spec_string ("url", "url", "url", NULL, G_PARAM_READWRITE));        /* CHECKME */
176
177
178   /* signals */
179   gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM] =
180       g_signal_new ("connection-problem", G_TYPE_FROM_CLASS (klass),
181       G_SIGNAL_RUN_CLEANUP, G_STRUCT_OFFSET (GstShout2sendClass,
182           connection_problem), NULL, NULL, g_cclosure_marshal_VOID__INT,
183       G_TYPE_NONE, 1, G_TYPE_INT);
184   gobject_class->set_property = gst_shout2send_set_property;
185   gobject_class->get_property = gst_shout2send_get_property;
186
187   gstelement_class->change_state = gst_shout2send_change_state;
188 }
189
190 static void
191 gst_shout2send_init (GstShout2send * shout2send)
192 {
193   shout2send->sinkpad =
194       gst_pad_new_from_template (gst_static_pad_template_get (&sink_template),
195       "sink");
196   gst_element_add_pad (GST_ELEMENT (shout2send), shout2send->sinkpad);
197   gst_pad_set_chain_function (shout2send->sinkpad, gst_shout2send_chain);
198
199   gst_pad_set_link_function (shout2send->sinkpad, gst_shout2send_connect);
200
201   shout2send->ip = g_strdup ("127.0.0.1");
202   shout2send->port = 8000;
203   shout2send->password = g_strdup ("hackme");
204   shout2send->name = g_strdup ("");
205   shout2send->description = g_strdup ("");
206   shout2send->genre = g_strdup ("");
207   shout2send->mount = g_strdup ("");
208   shout2send->url = g_strdup ("");
209   shout2send->protocol = SHOUT2SEND_PROTOCOL_HTTP;
210 }
211
212 static void
213 gst_shout2send_chain (GstPad * pad, GstData * _data)
214 {
215   GstBuffer *buf = GST_BUFFER (_data);
216   GstShout2send *shout2send;
217   glong ret;
218
219   g_return_if_fail (pad != NULL);
220   g_return_if_fail (GST_IS_PAD (pad));
221   g_return_if_fail (buf != NULL);
222
223   shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
224
225   g_return_if_fail (shout2send != NULL);
226   g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
227
228   ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
229       GST_BUFFER_SIZE (buf));
230   if (ret != SHOUTERR_SUCCESS) {
231     GST_WARNING ("send error: %s...\n", shout_get_error (shout2send->conn));
232     g_signal_emit (G_OBJECT (shout2send),
233         gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
234         shout_get_errno (shout2send->conn));
235   }
236
237   shout_sync (shout2send->conn);
238
239   gst_buffer_unref (buf);
240 }
241
242 static void
243 gst_shout2send_set_property (GObject * object, guint prop_id,
244     const GValue * value, GParamSpec * pspec)
245 {
246   GstShout2send *shout2send;
247
248   /* it's not null if we got it, but it might not be ours */
249   g_return_if_fail (GST_IS_SHOUT2SEND (object));
250   shout2send = GST_SHOUT2SEND (object);
251
252   switch (prop_id) {
253
254     case ARG_IP:
255       if (shout2send->ip)
256         g_free (shout2send->ip);
257       shout2send->ip = g_strdup (g_value_get_string (value));
258       break;
259
260     case ARG_PORT:
261       shout2send->port = g_value_get_int (value);
262       break;
263
264     case ARG_PASSWORD:
265       if (shout2send->password)
266         g_free (shout2send->password);
267       shout2send->password = g_strdup (g_value_get_string (value));
268       break;
269
270     case ARG_NAME:             /* Name of the stream */
271       if (shout2send->name)
272         g_free (shout2send->name);
273       shout2send->name = g_strdup (g_value_get_string (value));
274       break;
275
276     case ARG_DESCRIPTION:      /* Description of the stream */
277       if (shout2send->description)
278         g_free (shout2send->description);
279       shout2send->description = g_strdup (g_value_get_string (value));
280       break;
281
282     case ARG_GENRE:            /* Genre of the stream */
283       if (shout2send->genre)
284         g_free (shout2send->genre);
285       shout2send->genre = g_strdup (g_value_get_string (value));
286       break;
287
288     case ARG_PROTOCOL:         /* protocol to connect with */
289       shout2send->protocol = g_value_get_enum (value);
290       break;
291
292     case ARG_MOUNT:            /* mountpoint of stream (icecast only) */
293       if (shout2send->mount)
294         g_free (shout2send->mount);
295       shout2send->mount = g_strdup (g_value_get_string (value));
296       break;
297
298     case ARG_URL:              /* Url of the stream (I'm guessing) */
299       if (shout2send->url)
300         g_free (shout2send->url);
301       shout2send->url = g_strdup (g_value_get_string (value));
302       break;
303
304     default:
305       break;
306   }
307 }
308
309 static void
310 gst_shout2send_get_property (GObject * object, guint prop_id, GValue * value,
311     GParamSpec * pspec)
312 {
313   GstShout2send *shout2send;
314
315   /* it's not null if we got it, but it might not be ours */
316   g_return_if_fail (GST_IS_SHOUT2SEND (object));
317   shout2send = GST_SHOUT2SEND (object);
318
319   switch (prop_id) {
320
321     case ARG_IP:
322       g_value_set_string (value, shout2send->ip);
323       break;
324     case ARG_PORT:
325       g_value_set_int (value, shout2send->port);
326       break;
327     case ARG_PASSWORD:
328       g_value_set_string (value, shout2send->password);
329       break;
330
331     case ARG_NAME:             /* Name of the stream */
332       g_value_set_string (value, shout2send->name);
333       break;
334
335     case ARG_DESCRIPTION:      /* Description of the stream */
336       g_value_set_string (value, shout2send->description);
337       break;
338
339     case ARG_GENRE:            /* Genre of the stream */
340       g_value_set_string (value, shout2send->genre);
341       break;
342
343     case ARG_PROTOCOL:         /* protocol to connect with */
344       g_value_set_enum (value, shout2send->protocol);
345       break;
346
347     case ARG_MOUNT:            /* mountpoint of stream (icecast only) */
348       g_value_set_string (value, shout2send->mount);
349       break;
350
351     case ARG_URL:              /* Url of stream (I'm guessing) */
352       g_value_set_string (value, shout2send->url);
353       break;
354
355
356     default:
357       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
358       break;
359   }
360 }
361
362 static GstPadLinkReturn
363 gst_shout2send_connect (GstPad * pad, const GstCaps * caps)
364 {
365   const gchar *mimetype;
366
367   mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
368   if (!strcmp (mimetype, "audio/mpeg")) {
369     audio_format = SHOUT_FORMAT_MP3;
370     return GST_PAD_LINK_OK;
371   }
372
373   if (!strcmp (mimetype, "application/ogg")) {
374     audio_format = SHOUT_FORMAT_VORBIS;
375     return GST_PAD_LINK_OK;
376   } else {
377     return GST_PAD_LINK_REFUSED;
378   }
379
380 }
381
382 static GstElementStateReturn
383 gst_shout2send_change_state (GstElement * element)
384 {
385   GstShout2send *shout2send;
386
387   guint major, minor, micro;
388   gshort proto = 3;
389
390   gchar *version_string;
391
392   g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
393
394   shout2send = GST_SHOUT2SEND (element);
395
396   GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
397
398   /* if going down into NULL state, close the file if it's open */
399   switch (GST_STATE_TRANSITION (element)) {
400     case GST_STATE_NULL_TO_READY:
401       shout2send->conn = shout_new ();
402
403       switch (shout2send->protocol) {
404         case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
405           proto = SHOUT_PROTOCOL_XAUDIOCAST;
406           break;
407         case SHOUT2SEND_PROTOCOL_ICY:
408           proto = SHOUT_PROTOCOL_ICY;
409           break;
410         case SHOUT2SEND_PROTOCOL_HTTP:
411           proto = SHOUT_PROTOCOL_HTTP;
412           break;
413       }
414
415       if (shout_set_protocol (shout2send->conn, proto) != SHOUTERR_SUCCESS) {
416         g_error ("Error setting protocol: %s\n",
417             shout_get_error (shout2send->conn));
418       }
419
420       /* --- FIXME: shout requires an ip, and fails if it is given a host. */
421       /* may want to put convert_to_ip(shout2send->ip) here */
422
423
424       if (shout_set_host (shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS) {
425         g_error ("Error setting host: %s\n",
426             shout_get_error (shout2send->conn));
427       }
428       /* --- */
429
430       if (shout_set_port (shout2send->conn,
431               shout2send->port) != SHOUTERR_SUCCESS) {
432         g_error ("Error setting port: %s\n",
433             shout_get_error (shout2send->conn));
434       }
435
436       if (shout_set_password (shout2send->conn,
437               shout2send->password) != SHOUTERR_SUCCESS) {
438         g_error ("Error setting password: %s\n",
439             shout_get_error (shout2send->conn));
440       }
441
442       if (shout_set_name (shout2send->conn,
443               shout2send->name) != SHOUTERR_SUCCESS) {
444         g_error ("Error setting name: %s\n",
445             shout_get_error (shout2send->conn));
446       }
447
448       if (shout_set_description (shout2send->conn,
449               shout2send->description) != SHOUTERR_SUCCESS) {
450         g_error ("Error setting name: %s\n",
451             shout_get_error (shout2send->conn));
452       }
453
454       if (shout_set_genre (shout2send->conn,
455               shout2send->genre) != SHOUTERR_SUCCESS) {
456         g_error ("Error setting name: %s\n",
457             shout_get_error (shout2send->conn));
458       }
459
460       if (shout_set_mount (shout2send->conn,
461               shout2send->mount) != SHOUTERR_SUCCESS) {
462         g_error ("Error setting mount point: %s\n",
463             shout_get_error (shout2send->conn));
464       }
465
466       if (shout_set_user (shout2send->conn, "source") != SHOUTERR_SUCCESS) {
467         g_error ("Error setting user: %s\n",
468             shout_get_error (shout2send->conn));
469       }
470
471       gst_version (&major, &minor, &micro);
472
473       version_string =
474           g_strdup_printf ("GStreamer %d.%d.%d", major, minor, micro);
475
476       if (shout_set_agent (shout2send->conn,
477               version_string) != SHOUTERR_SUCCESS) {
478         g_error ("Error setting agent: %s\n",
479             shout_get_error (shout2send->conn));
480       }
481
482       g_free (version_string);
483
484
485
486       break;
487     case GST_STATE_READY_TO_PAUSED:
488
489       /* This sets the format acording to the capabilities of what
490          we are being given as input. */
491
492       if (shout_set_format (shout2send->conn, audio_format) != SHOUTERR_SUCCESS) {
493         g_error ("Error setting connection format: %s\n",
494             shout_get_error (shout2send->conn));
495       }
496
497       if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
498         g_print ("connected to server...\n");
499       } else {
500         g_warning ("Couldn't connect to server: %s",
501             shout_get_error (shout2send->conn));
502         shout_close (shout2send->conn);
503         shout_free (shout2send->conn);
504         return GST_STATE_FAILURE;
505       }
506       break;
507     case GST_STATE_PAUSED_TO_READY:
508       shout_close (shout2send->conn);
509       shout_free (shout2send->conn);
510       break;
511     default:
512       break;
513   }
514
515   /* if we haven't failed already, give the parent class a chance to ;-) */
516   if (GST_ELEMENT_CLASS (parent_class)->change_state)
517     return GST_ELEMENT_CLASS (parent_class)->change_state (element);
518
519   return GST_STATE_SUCCESS;
520 }
521
522 static gboolean
523 plugin_init (GstPlugin * plugin)
524 {
525   return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
526       GST_TYPE_SHOUT2SEND);
527 }
528
529 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
530     GST_VERSION_MINOR,
531     "shout2send",
532     "Sends data to an icecast server using libshout2",
533     plugin_init,
534     VERSION, "LGPL", "libshout2", "http://www.icecast.org/download.html")