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