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