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