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