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