shout2: port to 0.11
[platform/upstream/gst-plugins-good.git] / ext / shout2 / gstshout2.c
1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
4  * Copyright (C) <2012> Ralph Giles <giles@mozilla.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "gstshout2.h"
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "gst/gst-i18n-plugin.h"
31
32 GST_DEBUG_CATEGORY_STATIC (shout2_debug);
33 #define GST_CAT_DEFAULT shout2_debug
34
35
36 enum
37 {
38   SIGNAL_CONNECTION_PROBLEM,    /* 0.11 FIXME: remove this */
39   LAST_SIGNAL
40 };
41
42 enum
43 {
44   ARG_0,
45   ARG_IP,                       /* the ip of the server */
46   ARG_PORT,                     /* the encoder port number on the server */
47   ARG_PASSWORD,                 /* the encoder password on the server */
48   ARG_USERNAME,                 /* the encoder username on the server */
49   ARG_PUBLIC,                   /* is this stream public? */
50   ARG_STREAMNAME,               /* Name of the stream */
51   ARG_DESCRIPTION,              /* Description of the stream */
52   ARG_GENRE,                    /* Genre of the stream */
53
54   ARG_PROTOCOL,                 /* Protocol to connect with */
55
56   ARG_MOUNT,                    /* mountpoint of stream (icecast only) */
57   ARG_URL                       /* Url of stream (I'm guessing) */
58 };
59
60 #define DEFAULT_IP           "127.0.0.1"
61 #define DEFAULT_PORT         8000
62 #define DEFAULT_PASSWORD     "hackme"
63 #define DEFAULT_USERNAME     "source"
64 #define DEFAULT_PUBLIC     FALSE
65 #define DEFAULT_STREAMNAME   ""
66 #define DEFAULT_DESCRIPTION  ""
67 #define DEFAULT_GENRE        ""
68 #define DEFAULT_MOUNT        ""
69 #define DEFAULT_URL          ""
70 #define DEFAULT_PROTOCOL     SHOUT2SEND_PROTOCOL_HTTP
71
72 static GstElementClass *parent_class = NULL;
73
74 #ifdef SHOUT_FORMAT_WEBM
75 #define WEBM_CAPS "; video/webm"
76 #else
77 #define WEBM_CAPS ""
78 #endif
79 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
80     GST_PAD_SINK,
81     GST_PAD_ALWAYS,
82     GST_STATIC_CAPS ("application/ogg; "
83         "audio/mpeg, mpegversion = (int) 1, layer = (int) [ 1, 3 ]" WEBM_CAPS));
84
85 static void gst_shout2send_class_init (GstShout2sendClass * klass);
86 static void gst_shout2send_init (GstShout2send * shout2send);
87 static void gst_shout2send_finalize (GstShout2send * shout2send);
88
89 static gboolean gst_shout2send_event (GstBaseSink * sink, GstEvent * event);
90 static gboolean gst_shout2send_unlock (GstBaseSink * basesink);
91 static gboolean gst_shout2send_unlock_stop (GstBaseSink * basesink);
92 static GstFlowReturn gst_shout2send_render (GstBaseSink * sink,
93     GstBuffer * buffer);
94 static gboolean gst_shout2send_start (GstBaseSink * basesink);
95 static gboolean gst_shout2send_stop (GstBaseSink * basesink);
96
97 static void gst_shout2send_set_property (GObject * object, guint prop_id,
98     const GValue * value, GParamSpec * pspec);
99 static void gst_shout2send_get_property (GObject * object, guint prop_id,
100     GValue * value, GParamSpec * pspec);
101
102 static gboolean gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps);
103
104 static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 };
105
106 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
107 static GType
108 gst_shout2send_protocol_get_type (void)
109 {
110   static GType shout2send_protocol_type = 0;
111   static const GEnumValue shout2send_protocol[] = {
112     {SHOUT2SEND_PROTOCOL_XAUDIOCAST,
113         "Xaudiocast Protocol (icecast 1.3.x)", "xaudiocast"},
114     {SHOUT2SEND_PROTOCOL_ICY, "Icy Protocol (ShoutCast)", "icy"},
115     {SHOUT2SEND_PROTOCOL_HTTP, "Http Protocol (icecast 2.x)", "http"},
116     {0, NULL, NULL},
117   };
118
119   if (!shout2send_protocol_type) {
120     shout2send_protocol_type =
121         g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
122   }
123
124
125   return shout2send_protocol_type;
126 }
127
128 GType
129 gst_shout2send_get_type (void)
130 {
131   static GType shout2send_type = 0;
132
133   if (!shout2send_type) {
134     static const GTypeInfo shout2send_info = {
135       sizeof (GstShout2sendClass),
136       NULL,
137       NULL,
138       (GClassInitFunc) gst_shout2send_class_init,
139       NULL,
140       NULL,
141       sizeof (GstShout2send),
142       0,
143       (GInstanceInitFunc) gst_shout2send_init,
144     };
145
146     static const GInterfaceInfo tag_setter_info = {
147       NULL,
148       NULL,
149       NULL
150     };
151
152     shout2send_type =
153         g_type_register_static (GST_TYPE_BASE_SINK, "GstShout2send",
154         &shout2send_info, 0);
155
156     g_type_add_interface_static (shout2send_type, GST_TYPE_TAG_SETTER,
157         &tag_setter_info);
158
159   }
160   return shout2send_type;
161 }
162
163 static void
164 gst_shout2send_class_init (GstShout2sendClass * klass)
165 {
166   GObjectClass *gobject_class;
167   GstElementClass *gstelement_class;
168   GstBaseSinkClass *gstbasesink_class;
169
170   gobject_class = (GObjectClass *) klass;
171   gstelement_class = (GstElementClass *) klass;
172   gstbasesink_class = (GstBaseSinkClass *) klass;
173
174   parent_class = g_type_class_peek_parent (klass);
175
176   gobject_class->set_property = gst_shout2send_set_property;
177   gobject_class->get_property = gst_shout2send_get_property;
178   gobject_class->finalize = (GObjectFinalizeFunc) gst_shout2send_finalize;
179
180   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_IP,
181       g_param_spec_string ("ip", "ip", "ip", DEFAULT_IP,
182           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
184       g_param_spec_int ("port", "port", "port", 1, G_MAXUSHORT, DEFAULT_PORT,
185           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186
187   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PASSWORD,
188       g_param_spec_string ("password", "password", "password", DEFAULT_PASSWORD,
189           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
190
191   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_USERNAME,
192       g_param_spec_string ("username", "username", "username", DEFAULT_USERNAME,
193           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194
195   /* metadata */
196   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PUBLIC,
197       g_param_spec_boolean ("public", "public",
198           "If the stream should be listed on the server's stream directory",
199           DEFAULT_PUBLIC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200
201   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMNAME,
202       g_param_spec_string ("streamname", "streamname", "name of the stream",
203           DEFAULT_STREAMNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204
205   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESCRIPTION,
206       g_param_spec_string ("description", "description", "description",
207           DEFAULT_DESCRIPTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
208
209   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GENRE,
210       g_param_spec_string ("genre", "genre", "genre", DEFAULT_GENRE,
211           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212
213   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
214       g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
215           GST_TYPE_SHOUT_PROTOCOL, DEFAULT_PROTOCOL,
216           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
217
218
219   /* icecast only */
220   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MOUNT,
221       g_param_spec_string ("mount", "mount", "mount", DEFAULT_MOUNT,
222           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
223
224   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_URL,
225       g_param_spec_string ("url", "url", "url", DEFAULT_URL,
226           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
227
228   /* signals */
229   gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM] =
230       g_signal_new ("connection-problem", G_TYPE_FROM_CLASS (klass),
231       G_SIGNAL_RUN_CLEANUP, G_STRUCT_OFFSET (GstShout2sendClass,
232           connection_problem), NULL, NULL, g_cclosure_marshal_VOID__INT,
233       G_TYPE_NONE, 1, G_TYPE_INT);
234
235   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_shout2send_start);
236   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_shout2send_stop);
237   gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_shout2send_unlock);
238   gstbasesink_class->unlock_stop =
239       GST_DEBUG_FUNCPTR (gst_shout2send_unlock_stop);
240   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_shout2send_render);
241   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_shout2send_event);
242   gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_shout2send_setcaps);
243
244   gst_element_class_add_pad_template (gstelement_class,
245       gst_static_pad_template_get (&sink_template));
246
247   gst_element_class_set_details_simple (gstelement_class,
248       "Icecast network sink",
249       "Sink/Network", "Sends data to an icecast server",
250       "Wim Taymans <wim.taymans@chello.be>, "
251       "Pedro Corte-Real <typo@netcabo.pt>, "
252       "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
253
254   GST_DEBUG_CATEGORY_INIT (shout2_debug, "shout2", 0, "shout2send element");
255 }
256
257 static void
258 gst_shout2send_init (GstShout2send * shout2send)
259 {
260   gst_base_sink_set_sync (GST_BASE_SINK (shout2send), FALSE);
261
262   shout2send->timer = gst_poll_new_timer ();
263
264   shout2send->ip = g_strdup (DEFAULT_IP);
265   shout2send->port = DEFAULT_PORT;
266   shout2send->password = g_strdup (DEFAULT_PASSWORD);
267   shout2send->username = g_strdup (DEFAULT_USERNAME);
268   shout2send->streamname = g_strdup (DEFAULT_STREAMNAME);
269   shout2send->description = g_strdup (DEFAULT_DESCRIPTION);
270   shout2send->genre = g_strdup (DEFAULT_GENRE);
271   shout2send->mount = g_strdup (DEFAULT_MOUNT);
272   shout2send->url = g_strdup (DEFAULT_URL);
273   shout2send->protocol = DEFAULT_PROTOCOL;
274   shout2send->ispublic = DEFAULT_PUBLIC;
275
276   shout2send->tags = gst_tag_list_new_empty ();
277   shout2send->conn = NULL;
278   shout2send->audio_format = SHOUT_FORMAT_VORBIS;
279   shout2send->connected = FALSE;
280   shout2send->songmetadata = NULL;
281   shout2send->songartist = NULL;
282   shout2send->songtitle = NULL;
283 }
284
285 static void
286 gst_shout2send_finalize (GstShout2send * shout2send)
287 {
288   g_free (shout2send->ip);
289   g_free (shout2send->password);
290   g_free (shout2send->username);
291   g_free (shout2send->streamname);
292   g_free (shout2send->description);
293   g_free (shout2send->genre);
294   g_free (shout2send->mount);
295   g_free (shout2send->url);
296
297   gst_tag_list_free (shout2send->tags);
298
299   gst_poll_free (shout2send->timer);
300
301   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (shout2send));
302 }
303
304 static void
305 set_shout_metadata (const GstTagList * list, const gchar * tag,
306     gpointer user_data)
307 {
308   GstShout2send *shout2send = (GstShout2send *) user_data;
309   char **shout_metadata = &(shout2send->songmetadata);
310   char **song_artist = &(shout2send->songartist);
311   char **song_title = &(shout2send->songtitle);
312
313   gchar *value;
314
315   GST_DEBUG ("tag: %s being added", tag);
316   if (strcmp (tag, GST_TAG_ARTIST) == 0) {
317     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
318       if (!gst_tag_list_get_string (list, tag, &value)) {
319         GST_DEBUG ("Error reading \"%s\" tag value", tag);
320         return;
321       }
322
323       if (*song_artist != NULL)
324         g_free (*song_artist);
325
326       *song_artist = g_strdup (value);
327     }
328   } else if (strcmp (tag, GST_TAG_TITLE) == 0) {
329     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
330       if (!gst_tag_list_get_string (list, tag, &value)) {
331         GST_DEBUG ("Error reading \"%s\" tag value", tag);
332         return;
333       }
334
335       if (*song_title != NULL)
336         g_free (*song_title);
337
338       *song_title = g_strdup (value);
339     }
340   }
341
342   if (*shout_metadata != NULL)
343     g_free (*shout_metadata);
344
345
346   if (*song_title && *song_artist) {
347     *shout_metadata = g_strdup_printf ("%s - %s", *song_artist, *song_title);
348   } else if (*song_title && *song_artist == NULL) {
349     *shout_metadata = g_strdup_printf ("Unknown - %s", *song_title);
350   } else if (*song_title == NULL && *song_artist) {
351     *shout_metadata = g_strdup_printf ("%s - Unknown", *song_artist);
352   } else {
353     *shout_metadata = g_strdup_printf ("Unknown - Unknown");
354   }
355
356   GST_LOG ("shout metadata is now: %s", *shout_metadata);
357 }
358
359 #if 0
360 static void
361 gst_shout2send_set_metadata (GstShout2send * shout2send)
362 {
363   const GstTagList *user_tags;
364   GstTagList *copy;
365   char *tempmetadata;
366   shout_metadata_t *pmetadata;
367
368   g_return_if_fail (shout2send != NULL);
369   user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (shout2send));
370   if ((shout2send->tags == NULL) && (user_tags == NULL)) {
371     return;
372   }
373   copy = gst_tag_list_merge (user_tags, shout2send->tags,
374       gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
375   /* lets get the artist and song tags */
376   tempmetadata = NULL;
377   gst_tag_list_foreach ((GstTagList *) copy, set_shout_metadata,
378       (gpointer) & tempmetadata);
379   if (tempmetadata) {
380     pmetadata = shout_metadata_new ();
381     shout_metadata_add (pmetadata, "song", tempmetadata);
382     shout_set_metadata (shout2send->conn, pmetadata);
383     shout_metadata_free (pmetadata);
384   }
385
386   gst_tag_list_free (copy);
387 }
388 #endif
389
390
391 static gboolean
392 gst_shout2send_event (GstBaseSink * sink, GstEvent * event)
393 {
394   GstShout2send *shout2send;
395   gboolean ret = TRUE;
396
397   shout2send = GST_SHOUT2SEND (sink);
398
399   GST_LOG_OBJECT (shout2send, "got %s event", GST_EVENT_TYPE_NAME (event));
400
401   switch (GST_EVENT_TYPE (event)) {
402     case GST_EVENT_TAG:{
403       /* vorbis audio doesnt need metadata setting on the icecast level, only mp3 */
404       if (shout2send->tags && shout2send->audio_format == SHOUT_FORMAT_MP3) {
405         GstTagList *list;
406
407         gst_event_parse_tag (event, &list);
408         GST_DEBUG_OBJECT (shout2send, "tags=%" GST_PTR_FORMAT, list);
409         gst_tag_list_insert (shout2send->tags,
410             list,
411             gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
412         /* lets get the artist and song tags */
413         gst_tag_list_foreach ((GstTagList *) list,
414             set_shout_metadata, shout2send);
415         if (shout2send->songmetadata && shout2send->connected) {
416           shout_metadata_t *pmetadata;
417
418           GST_DEBUG_OBJECT (shout2send, "metadata now: %s",
419               shout2send->songmetadata);
420
421           pmetadata = shout_metadata_new ();
422           shout_metadata_add (pmetadata, "song", shout2send->songmetadata);
423           shout_set_metadata (shout2send->conn, pmetadata);
424           shout_metadata_free (pmetadata);
425         }
426       }
427       break;
428     }
429     default:{
430       GST_LOG_OBJECT (shout2send, "let base class handle event");
431       if (GST_BASE_SINK_CLASS (parent_class)->event) {
432         event = gst_event_ref (event);
433         ret = GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
434       }
435       break;
436     }
437   }
438
439   return ret;
440 }
441
442 static gboolean
443 gst_shout2send_start (GstBaseSink * basesink)
444 {
445   GstShout2send *sink = GST_SHOUT2SEND (basesink);
446   const gchar *cur_prop;
447   gshort proto = 3;
448   gchar *version_string;
449
450   GST_DEBUG_OBJECT (sink, "starting");
451
452   sink->conn = shout_new ();
453
454   switch (sink->protocol) {
455     case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
456       proto = SHOUT_PROTOCOL_XAUDIOCAST;
457       break;
458     case SHOUT2SEND_PROTOCOL_ICY:
459       proto = SHOUT_PROTOCOL_ICY;
460       break;
461     case SHOUT2SEND_PROTOCOL_HTTP:
462       proto = SHOUT_PROTOCOL_HTTP;
463       break;
464   }
465
466   cur_prop = "protocol";
467   GST_DEBUG_OBJECT (sink, "setting protocol: %d", sink->protocol);
468   if (shout_set_protocol (sink->conn, proto) != SHOUTERR_SUCCESS)
469     goto set_failed;
470
471   /* --- FIXME: shout requires an ip, and fails if it is given a host. */
472   /* may want to put convert_to_ip(shout2send->ip) here */
473   cur_prop = "ip";
474   GST_DEBUG_OBJECT (sink, "setting ip: %s", sink->ip);
475   if (shout_set_host (sink->conn, sink->ip) != SHOUTERR_SUCCESS)
476     goto set_failed;
477
478   cur_prop = "port";
479   GST_DEBUG_OBJECT (sink, "setting port: %u", sink->port);
480   if (shout_set_port (sink->conn, sink->port) != SHOUTERR_SUCCESS)
481     goto set_failed;
482
483   cur_prop = "password";
484   GST_DEBUG_OBJECT (sink, "setting password: %s", sink->password);
485   if (shout_set_password (sink->conn, sink->password) != SHOUTERR_SUCCESS)
486     goto set_failed;
487
488   cur_prop = "public";
489   GST_DEBUG_OBJECT (sink, "setting %s: %u", cur_prop, sink->ispublic);
490   if (shout_set_public (sink->conn,
491           (sink->ispublic ? 1 : 0)) != SHOUTERR_SUCCESS)
492     goto set_failed;
493
494   cur_prop = "streamname";
495   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->streamname);
496   if (shout_set_name (sink->conn, sink->streamname) != SHOUTERR_SUCCESS)
497     goto set_failed;
498
499   cur_prop = "description";
500   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->description);
501   if (shout_set_description (sink->conn, sink->description) != SHOUTERR_SUCCESS)
502     goto set_failed;
503
504   cur_prop = "genre";
505   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->genre);
506   if (shout_set_genre (sink->conn, sink->genre) != SHOUTERR_SUCCESS)
507     goto set_failed;
508
509   cur_prop = "mount";
510   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->mount);
511   if (shout_set_mount (sink->conn, sink->mount) != SHOUTERR_SUCCESS)
512     goto set_failed;
513
514   cur_prop = "username";
515   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, "source");
516   if (shout_set_user (sink->conn, sink->username) != SHOUTERR_SUCCESS)
517     goto set_failed;
518
519   version_string = gst_version_string ();
520   cur_prop = "agent";
521   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, version_string);
522   if (shout_set_agent (sink->conn, version_string) != SHOUTERR_SUCCESS) {
523     g_free (version_string);
524     goto set_failed;
525   }
526
527   g_free (version_string);
528   return TRUE;
529
530 /* ERROR */
531 set_failed:
532   {
533     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
534         ("Error setting %s: %s", cur_prop, shout_get_error (sink->conn)));
535     return FALSE;
536   }
537 }
538
539 static gboolean
540 gst_shout2send_connect (GstShout2send * sink)
541 {
542   const char *format =
543       (sink->audio_format == SHOUT_FORMAT_VORBIS) ? "vorbis" :
544       ((sink->audio_format == SHOUT_FORMAT_MP3) ? "mp3" : "unknown");
545 #ifdef SHOUT_FORMAT_WEBM
546   if (sink->audio_format == SHOUT_FORMAT_WEBM)
547     format = "webm";
548 #endif
549   GST_DEBUG_OBJECT (sink, "Connection format is: %s", format);
550
551   if (shout_set_format (sink->conn, sink->audio_format) != SHOUTERR_SUCCESS)
552     goto could_not_set_format;
553
554   if (shout_open (sink->conn) != SHOUTERR_SUCCESS)
555     goto could_not_connect;
556
557   GST_DEBUG_OBJECT (sink, "connected to server");
558   sink->connected = TRUE;
559
560   /* let's set metadata */
561   if (sink->songmetadata) {
562     shout_metadata_t *pmetadata;
563
564     GST_DEBUG_OBJECT (sink, "shout metadata now: %s", sink->songmetadata);
565     pmetadata = shout_metadata_new ();
566     shout_metadata_add (pmetadata, "song", sink->songmetadata);
567     shout_set_metadata (sink->conn, pmetadata);
568     shout_metadata_free (pmetadata);
569   }
570
571   return TRUE;
572
573 /* ERRORS */
574 could_not_set_format:
575   {
576     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
577         ("Error setting connection format: %s", shout_get_error (sink->conn)));
578     return FALSE;
579   }
580
581 could_not_connect:
582   {
583     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
584         (_("Could not connect to server")),
585         ("shout_open() failed: err=%s", shout_get_error (sink->conn)));
586     g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
587         shout_get_errno (sink->conn));
588     return FALSE;
589   }
590 }
591
592 static gboolean
593 gst_shout2send_stop (GstBaseSink * basesink)
594 {
595   GstShout2send *sink = GST_SHOUT2SEND (basesink);
596
597   if (sink->conn) {
598     if (sink->connected)
599       shout_close (sink->conn);
600     shout_free (sink->conn);
601     sink->conn = NULL;
602   }
603
604   if (sink->songmetadata) {
605     g_free (sink->songmetadata);
606     sink->songmetadata = NULL;
607   }
608
609   sink->connected = FALSE;
610
611   return TRUE;
612 }
613
614 static gboolean
615 gst_shout2send_unlock (GstBaseSink * basesink)
616 {
617   GstShout2send *sink;
618
619   sink = GST_SHOUT2SEND (basesink);
620
621   GST_DEBUG_OBJECT (basesink, "unlock");
622   gst_poll_set_flushing (sink->timer, TRUE);
623
624   return TRUE;
625 }
626
627 static gboolean
628 gst_shout2send_unlock_stop (GstBaseSink * basesink)
629 {
630   GstShout2send *sink;
631
632   sink = GST_SHOUT2SEND (basesink);
633
634   GST_DEBUG_OBJECT (basesink, "unlock_stop");
635   gst_poll_set_flushing (sink->timer, FALSE);
636
637   return TRUE;
638 }
639
640 static GstFlowReturn
641 gst_shout2send_render (GstBaseSink * basesink, GstBuffer * buf)
642 {
643   GstShout2send *sink;
644   glong ret;
645   gint delay;
646   GstFlowReturn fret;
647   GstMapInfo map;
648
649   sink = GST_SHOUT2SEND (basesink);
650
651   /* presumably we connect here because we need to know the format before
652    * we can set up the connection, which we don't know yet in _start() */
653   if (!sink->connected) {
654     if (!gst_shout2send_connect (sink))
655       return GST_FLOW_ERROR;
656   }
657
658   delay = shout_delay (sink->conn);
659
660   if (delay > 0) {
661     GST_LOG_OBJECT (sink, "waiting %d msec", delay);
662     if (gst_poll_wait (sink->timer, GST_MSECOND * delay) == -1) {
663       GST_LOG_OBJECT (sink, "unlocked");
664
665       fret = gst_base_sink_wait_preroll (basesink);
666       if (fret != GST_FLOW_OK)
667         return fret;
668     }
669   } else {
670     GST_LOG_OBJECT (sink, "we're %d msec late", -delay);
671   }
672
673   gst_buffer_map (buf, &map, GST_MAP_READ);
674   GST_LOG_OBJECT (sink, "sending %u bytes of data", (guint) map.size);
675   ret = shout_send (sink->conn, map.data, map.size);
676   gst_buffer_unmap (buf, &map);
677   if (ret != SHOUTERR_SUCCESS)
678     goto send_error;
679
680   return GST_FLOW_OK;
681
682 /* ERRORS */
683 send_error:
684   {
685     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
686         ("shout_send() failed: %s", shout_get_error (sink->conn)));
687     g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
688         shout_get_errno (sink->conn));
689     return GST_FLOW_ERROR;
690   }
691 }
692
693 static void
694 gst_shout2send_set_property (GObject * object, guint prop_id,
695     const GValue * value, GParamSpec * pspec)
696 {
697   GstShout2send *shout2send;
698
699   shout2send = GST_SHOUT2SEND (object);
700   switch (prop_id) {
701
702     case ARG_IP:
703       if (shout2send->ip)
704         g_free (shout2send->ip);
705       shout2send->ip = g_strdup (g_value_get_string (value));
706       break;
707     case ARG_PORT:
708       shout2send->port = g_value_get_int (value);
709       break;
710     case ARG_PASSWORD:
711       if (shout2send->password)
712         g_free (shout2send->password);
713       shout2send->password = g_strdup (g_value_get_string (value));
714       break;
715     case ARG_USERNAME:
716       if (shout2send->username)
717         g_free (shout2send->username);
718       shout2send->username = g_strdup (g_value_get_string (value));
719       break;
720     case ARG_PUBLIC:
721       shout2send->ispublic = g_value_get_boolean (value);
722       break;
723     case ARG_STREAMNAME:       /* Name of the stream */
724       if (shout2send->streamname)
725         g_free (shout2send->streamname);
726       shout2send->streamname = g_strdup (g_value_get_string (value));
727       break;
728     case ARG_DESCRIPTION:      /* Description of the stream */
729       if (shout2send->description)
730         g_free (shout2send->description);
731       shout2send->description = g_strdup (g_value_get_string (value));
732       break;
733     case ARG_GENRE:            /* Genre of the stream */
734       if (shout2send->genre)
735         g_free (shout2send->genre);
736       shout2send->genre = g_strdup (g_value_get_string (value));
737       break;
738     case ARG_PROTOCOL:         /* protocol to connect with */
739       shout2send->protocol = g_value_get_enum (value);
740       break;
741     case ARG_MOUNT:            /* mountpoint of stream (icecast only) */
742       if (shout2send->mount)
743         g_free (shout2send->mount);
744       shout2send->mount = g_strdup (g_value_get_string (value));
745       break;
746     case ARG_URL:              /* Url of the stream (I'm guessing) */
747       if (shout2send->url)
748         g_free (shout2send->url);
749       shout2send->url = g_strdup (g_value_get_string (value));
750       break;
751     default:
752       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
753       break;
754   }
755 }
756
757 static void
758 gst_shout2send_get_property (GObject * object, guint prop_id,
759     GValue * value, GParamSpec * pspec)
760 {
761   GstShout2send *shout2send;
762
763   shout2send = GST_SHOUT2SEND (object);
764   switch (prop_id) {
765
766     case ARG_IP:
767       g_value_set_string (value, shout2send->ip);
768       break;
769     case ARG_PORT:
770       g_value_set_int (value, shout2send->port);
771       break;
772     case ARG_PASSWORD:
773       g_value_set_string (value, shout2send->password);
774       break;
775     case ARG_USERNAME:
776       g_value_set_string (value, shout2send->username);
777       break;
778     case ARG_PUBLIC:
779       g_value_set_boolean (value, shout2send->ispublic);
780       break;
781     case ARG_STREAMNAME:       /* Name of the stream */
782       g_value_set_string (value, shout2send->streamname);
783       break;
784     case ARG_DESCRIPTION:      /* Description of the stream */
785       g_value_set_string (value, shout2send->description);
786       break;
787     case ARG_GENRE:            /* Genre of the stream */
788       g_value_set_string (value, shout2send->genre);
789       break;
790     case ARG_PROTOCOL:         /* protocol to connect with */
791       g_value_set_enum (value, shout2send->protocol);
792       break;
793     case ARG_MOUNT:            /* mountpoint of stream (icecast only) */
794       g_value_set_string (value, shout2send->mount);
795       break;
796     case ARG_URL:              /* Url of stream (I'm guessing) */
797       g_value_set_string (value, shout2send->url);
798       break;
799     default:
800       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
801       break;
802   }
803 }
804
805 static gboolean
806 gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps)
807 {
808   const gchar *mimetype;
809   GstShout2send *shout2send;
810   gboolean ret = TRUE;
811
812   shout2send = GST_SHOUT2SEND (basesink);
813
814   mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
815
816   GST_DEBUG_OBJECT (shout2send, "mimetype of caps given is: %s", mimetype);
817
818   if (!strcmp (mimetype, "audio/mpeg")) {
819     shout2send->audio_format = SHOUT_FORMAT_MP3;
820   } else if (!strcmp (mimetype, "application/ogg")) {
821     shout2send->audio_format = SHOUT_FORMAT_VORBIS;
822 #ifdef SHOUT_FORMAT_WEBM
823   } else if (!strcmp (mimetype, "video/webm")) {
824     shout2send->audio_format = SHOUT_FORMAT_WEBM;
825 #endif
826   } else {
827     ret = FALSE;
828   }
829
830   return ret;
831 }
832
833 static gboolean
834 plugin_init (GstPlugin * plugin)
835 {
836 #ifdef ENABLE_NLS
837   setlocale (LC_ALL, "");
838   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
839   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
840 #endif /* ENABLE_NLS */
841
842   return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
843       GST_TYPE_SHOUT2SEND);
844 }
845
846 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
847     GST_VERSION_MINOR,
848     "shout2send",
849     "Sends data to an icecast server using libshout2",
850     plugin_init,
851     VERSION, "LGPL", "libshout2", "http://www.icecast.org/download.html")