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