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