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