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