Make sure gettext returns translations in UTF-8 encoding rather than in the current...
[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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstshout2.h"
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "gst/gst-i18n-plugin.h"
30
31 GST_DEBUG_CATEGORY_STATIC (shout2_debug);
32 #define GST_CAT_DEFAULT shout2_debug
33
34 static const GstElementDetails shout2send_details =
35 GST_ELEMENT_DETAILS ("Icecast network sink",
36     "Sink/Network",
37     "Sends data to an icecast server",
38     "Wim Taymans <wim.taymans@chello.be>\n"
39     "Pedro Corte-Real <typo@netcabo.pt>\n"
40     "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
41
42
43 enum
44 {
45   SIGNAL_CONNECTION_PROBLEM,    /* 0.11 FIXME: remove this */
46   LAST_SIGNAL
47 };
48
49 enum
50 {
51   ARG_0,
52   ARG_IP,                       /* the ip of the server */
53   ARG_PORT,                     /* the encoder port number on the server */
54   ARG_PASSWORD,                 /* the encoder password on the server */
55   ARG_USERNAME,                 /* the encoder username on the server */
56   ARG_PUBLIC,                   /* is this stream public? */
57   ARG_STREAMNAME,               /* Name of the stream */
58   ARG_DESCRIPTION,              /* Description of the stream */
59   ARG_GENRE,                    /* Genre of the stream */
60
61   ARG_PROTOCOL,                 /* Protocol to connect with */
62
63   ARG_MOUNT,                    /* mountpoint of stream (icecast only) */
64   ARG_URL                       /* Url of stream (I'm guessing) */
65 };
66
67 #define DEFAULT_IP           "127.0.0.1"
68 #define DEFAULT_PORT         8000
69 #define DEFAULT_PASSWORD     "hackme"
70 #define DEFAULT_USERNAME     "source"
71 #define DEFAULT_STREAMNAME   ""
72 #define DEFAULT_DESCRIPTION  ""
73 #define DEFAULT_GENRE        ""
74 #define DEFAULT_MOUNT        ""
75 #define DEFAULT_URL          ""
76 #define DEFAULT_PROTOCOL     SHOUT2SEND_PROTOCOL_HTTP
77
78 static GstElementClass *parent_class = NULL;
79
80 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
81     GST_PAD_SINK,
82     GST_PAD_ALWAYS,
83     GST_STATIC_CAPS ("application/ogg; "
84         "audio/mpeg, mpegversion = (int) 1, layer = (int) [ 1, 3 ]")
85     );
86 static void gst_shout2send_class_init (GstShout2sendClass * klass);
87 static void gst_shout2send_base_init (GstShout2sendClass * klass);
88 static void gst_shout2send_init (GstShout2send * shout2send);
89 static void gst_shout2send_finalize (GstShout2send * shout2send);
90
91 static gboolean gst_shout2send_event (GstBaseSink * sink, GstEvent * event);
92 static GstFlowReturn gst_shout2send_render (GstBaseSink * sink,
93     GstBuffer * buffer);
94 static gboolean gst_shout2send_start (GstBaseSink * basesink);
95 static gboolean gst_shout2send_stop (GstBaseSink * basesink);
96
97 static void gst_shout2send_set_property (GObject * object, guint prop_id,
98     const GValue * value, GParamSpec * pspec);
99 static void gst_shout2send_get_property (GObject * object, guint prop_id,
100     GValue * value, GParamSpec * pspec);
101
102 static gboolean gst_shout2send_setcaps (GstPad * pad, GstCaps * caps);
103
104 static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 };
105
106 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
107 static GType
108 gst_shout2send_protocol_get_type (void)
109 {
110   static GType shout2send_protocol_type = 0;
111   static const GEnumValue shout2send_protocol[] = {
112     {SHOUT2SEND_PROTOCOL_XAUDIOCAST,
113         "Xaudiocast Protocol (icecast 1.3.x)", "xaudiocast"},
114     {SHOUT2SEND_PROTOCOL_ICY, "Icy Protocol (ShoutCast)", "icy"},
115     {SHOUT2SEND_PROTOCOL_HTTP, "Http Protocol (icecast 2.x)", "http"},
116     {0, NULL, NULL},
117   };
118
119   if (!shout2send_protocol_type) {
120     shout2send_protocol_type =
121         g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
122   }
123
124
125   return shout2send_protocol_type;
126 }
127
128 GType
129 gst_shout2send_get_type (void)
130 {
131   static GType shout2send_type = 0;
132
133   if (!shout2send_type) {
134     static const GTypeInfo shout2send_info = {
135       sizeof (GstShout2sendClass),
136       (GBaseInitFunc) gst_shout2send_base_init,
137       NULL,
138       (GClassInitFunc) gst_shout2send_class_init,
139       NULL,
140       NULL,
141       sizeof (GstShout2send),
142       0,
143       (GInstanceInitFunc) gst_shout2send_init,
144     };
145
146     static const GInterfaceInfo tag_setter_info = {
147       NULL,
148       NULL,
149       NULL
150     };
151
152     shout2send_type =
153         g_type_register_static (GST_TYPE_BASE_SINK, "GstShout2send",
154         &shout2send_info, 0);
155
156     g_type_add_interface_static (shout2send_type, GST_TYPE_TAG_SETTER,
157         &tag_setter_info);
158
159   }
160   return shout2send_type;
161 }
162
163 static void
164 gst_shout2send_base_init (GstShout2sendClass * klass)
165 {
166   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
167
168   gst_element_class_add_pad_template (element_class,
169       gst_static_pad_template_get (&sink_template));
170   gst_element_class_set_details (element_class, &shout2send_details);
171
172   GST_DEBUG_CATEGORY_INIT (shout2_debug, "shout2", 0, "shout2send element");
173 }
174
175 static void
176 gst_shout2send_class_init (GstShout2sendClass * klass)
177 {
178   GObjectClass *gobject_class;
179   GstBaseSinkClass *gstbasesink_class;
180
181   gobject_class = (GObjectClass *) klass;
182   gstbasesink_class = (GstBaseSinkClass *) klass;
183
184   parent_class = g_type_class_peek_parent (klass);
185
186   gobject_class->set_property = gst_shout2send_set_property;
187   gobject_class->get_property = gst_shout2send_get_property;
188   gobject_class->finalize = (GObjectFinalizeFunc) gst_shout2send_finalize;
189
190   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_IP,
191       g_param_spec_string ("ip", "ip", "ip", DEFAULT_IP, G_PARAM_READWRITE));
192   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
193       g_param_spec_int ("port", "port", "port", 1, G_MAXUSHORT, DEFAULT_PORT,
194           G_PARAM_READWRITE));
195
196   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PASSWORD,
197       g_param_spec_string ("password", "password", "password", DEFAULT_PASSWORD,
198           G_PARAM_READWRITE));
199
200   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_USERNAME,
201       g_param_spec_string ("username", "username", "username", DEFAULT_USERNAME,
202           G_PARAM_READWRITE));
203
204   /* metadata */
205   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMNAME,
206       g_param_spec_string ("streamname", "streamname", "name of the stream",
207           DEFAULT_STREAMNAME, G_PARAM_READWRITE));
208
209   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESCRIPTION,
210       g_param_spec_string ("description", "description", "description",
211           DEFAULT_DESCRIPTION, G_PARAM_READWRITE));
212
213   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GENRE,
214       g_param_spec_string ("genre", "genre", "genre", DEFAULT_GENRE,
215           G_PARAM_READWRITE));
216
217   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
218       g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
219           GST_TYPE_SHOUT_PROTOCOL, DEFAULT_PROTOCOL, G_PARAM_READWRITE));
220
221
222   /* icecast only */
223   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MOUNT,
224       g_param_spec_string ("mount", "mount", "mount", DEFAULT_MOUNT,
225           G_PARAM_READWRITE));
226
227   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_URL,
228       g_param_spec_string ("url", "url", "url", DEFAULT_URL,
229           G_PARAM_READWRITE));
230
231   /* signals */
232   gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM] =
233       g_signal_new ("connection-problem", G_TYPE_FROM_CLASS (klass),
234       G_SIGNAL_RUN_CLEANUP, G_STRUCT_OFFSET (GstShout2sendClass,
235           connection_problem), NULL, NULL, g_cclosure_marshal_VOID__INT,
236       G_TYPE_NONE, 1, G_TYPE_INT);
237
238   gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_shout2send_start);
239   gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_shout2send_stop);
240   gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_shout2send_render);
241   gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_shout2send_event);
242 }
243
244 static void
245 gst_shout2send_init (GstShout2send * shout2send)
246 {
247   gst_base_sink_set_sync (GST_BASE_SINK (shout2send), FALSE);
248
249   gst_pad_set_setcaps_function (GST_BASE_SINK_PAD (shout2send),
250       GST_DEBUG_FUNCPTR (gst_shout2send_setcaps));
251
252   shout2send->ip = g_strdup (DEFAULT_IP);
253   shout2send->port = DEFAULT_PORT;
254   shout2send->password = g_strdup (DEFAULT_PASSWORD);
255   shout2send->username = g_strdup (DEFAULT_USERNAME);
256   shout2send->streamname = g_strdup (DEFAULT_STREAMNAME);
257   shout2send->description = g_strdup (DEFAULT_DESCRIPTION);
258   shout2send->genre = g_strdup (DEFAULT_GENRE);
259   shout2send->mount = g_strdup (DEFAULT_MOUNT);
260   shout2send->url = g_strdup (DEFAULT_URL);
261   shout2send->protocol = DEFAULT_PROTOCOL;
262
263   shout2send->tags = gst_tag_list_new ();
264   shout2send->conn = NULL;
265   shout2send->audio_format = SHOUT_FORMAT_VORBIS;
266   shout2send->connected = FALSE;
267   shout2send->songmetadata = NULL;
268   shout2send->songartist = NULL;
269   shout2send->songtitle = NULL;
270 }
271
272 static void
273 gst_shout2send_finalize (GstShout2send * shout2send)
274 {
275   g_free (shout2send->ip);
276   g_free (shout2send->password);
277   g_free (shout2send->username);
278   g_free (shout2send->streamname);
279   g_free (shout2send->description);
280   g_free (shout2send->genre);
281   g_free (shout2send->mount);
282   g_free (shout2send->url);
283
284   gst_tag_list_free (shout2send->tags);
285
286   G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (shout2send));
287 }
288
289 static void
290 set_shout_metadata (const GstTagList * list, const gchar * tag,
291     gpointer user_data)
292 {
293   GstShout2send *shout2send = (GstShout2send *) user_data;
294   char **shout_metadata = &(shout2send->songmetadata);
295   char **song_artist = &(shout2send->songartist);
296   char **song_title = &(shout2send->songtitle);
297
298   gchar *value;
299
300   GST_DEBUG ("tag: %s being added", tag);
301   if (strcmp (tag, GST_TAG_ARTIST) == 0) {
302     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
303       if (!gst_tag_list_get_string (list, tag, &value)) {
304         GST_DEBUG ("Error reading \"%s\" tag value", tag);
305         return;
306       }
307
308       if (*song_artist != NULL)
309         g_free (*song_artist);
310
311       *song_artist = g_strdup (value);
312     }
313   } else if (strcmp (tag, GST_TAG_TITLE) == 0) {
314     if (gst_tag_get_type (tag) == G_TYPE_STRING) {
315       if (!gst_tag_list_get_string (list, tag, &value)) {
316         GST_DEBUG ("Error reading \"%s\" tag value", tag);
317         return;
318       }
319
320       if (*song_title != NULL)
321         g_free (*song_title);
322
323       *song_title = g_strdup (value);
324     }
325   }
326
327   if (*shout_metadata != NULL)
328     g_free (*shout_metadata);
329
330
331   if (*song_title && *song_artist) {
332     *shout_metadata = g_strdup_printf ("%s - %s", *song_artist, *song_title);
333   } else if (*song_title && *song_artist == NULL) {
334     *shout_metadata = g_strdup_printf ("Unknown - %s", *song_title);
335   } else if (*song_title == NULL && *song_artist) {
336     *shout_metadata = g_strdup_printf ("%s - Unknown", *song_artist);
337   } else {
338     *shout_metadata = g_strdup_printf ("Unknown - Unknown");
339   }
340
341   GST_LOG ("shout metadata is now: %s", *shout_metadata);
342 }
343
344 #if 0
345 static void
346 gst_shout2send_set_metadata (GstShout2send * shout2send)
347 {
348   const GstTagList *user_tags;
349   GstTagList *copy;
350   char *tempmetadata;
351   shout_metadata_t *pmetadata;
352
353   g_return_if_fail (shout2send != NULL);
354   user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (shout2send));
355   if ((shout2send->tags == NULL) && (user_tags == NULL)) {
356     return;
357   }
358   copy = gst_tag_list_merge (user_tags, shout2send->tags,
359       gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
360   /* lets get the artist and song tags */
361   tempmetadata = NULL;
362   gst_tag_list_foreach ((GstTagList *) copy, set_shout_metadata,
363       (gpointer) & tempmetadata);
364   if (tempmetadata) {
365     pmetadata = shout_metadata_new ();
366     shout_metadata_add (pmetadata, "song", tempmetadata);
367     shout_set_metadata (shout2send->conn, pmetadata);
368     shout_metadata_free (pmetadata);
369   }
370
371   gst_tag_list_free (copy);
372 }
373 #endif
374
375
376 static gboolean
377 gst_shout2send_event (GstBaseSink * sink, GstEvent * event)
378 {
379   GstShout2send *shout2send;
380   gboolean ret = TRUE;
381
382   shout2send = GST_SHOUT2SEND (sink);
383
384   GST_LOG_OBJECT (shout2send, "got %s event", GST_EVENT_TYPE_NAME (event));
385
386   switch (GST_EVENT_TYPE (event)) {
387     case GST_EVENT_TAG:{
388       /* vorbis audio doesnt need metadata setting on the icecast level, only mp3 */
389       if (shout2send->tags && shout2send->audio_format == SHOUT_FORMAT_MP3) {
390         GstTagList *list;
391
392         gst_event_parse_tag (event, &list);
393         GST_DEBUG_OBJECT (shout2send, "tags=%" GST_PTR_FORMAT, list);
394         gst_tag_list_insert (shout2send->tags,
395             list,
396             gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
397         /* lets get the artist and song tags */
398         gst_tag_list_foreach ((GstTagList *) list,
399             set_shout_metadata, shout2send);
400         if (shout2send->songmetadata && shout2send->connected) {
401           shout_metadata_t *pmetadata;
402
403           GST_DEBUG_OBJECT (shout2send, "metadata now: %s",
404               shout2send->songmetadata);
405
406           pmetadata = shout_metadata_new ();
407           shout_metadata_add (pmetadata, "song", shout2send->songmetadata);
408           shout_set_metadata (shout2send->conn, pmetadata);
409           shout_metadata_free (pmetadata);
410         }
411       }
412       break;
413     }
414     default:{
415       GST_LOG_OBJECT (shout2send, "let base class handle event");
416       if (GST_BASE_SINK_CLASS (parent_class)->event) {
417         event = gst_event_ref (event);
418         ret = GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
419       }
420       break;
421     }
422   }
423
424   return ret;
425 }
426
427 static gboolean
428 gst_shout2send_start (GstBaseSink * basesink)
429 {
430   GstShout2send *sink = GST_SHOUT2SEND (basesink);
431   const gchar *cur_prop;
432   gshort proto = 3;
433   gchar *version_string;
434
435   GST_DEBUG_OBJECT (sink, "starting");
436
437   sink->conn = shout_new ();
438
439   switch (sink->protocol) {
440     case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
441       proto = SHOUT_PROTOCOL_XAUDIOCAST;
442       break;
443     case SHOUT2SEND_PROTOCOL_ICY:
444       proto = SHOUT_PROTOCOL_ICY;
445       break;
446     case SHOUT2SEND_PROTOCOL_HTTP:
447       proto = SHOUT_PROTOCOL_HTTP;
448       break;
449   }
450
451   cur_prop = "protocol";
452   GST_DEBUG_OBJECT (sink, "setting protocol: %d", sink->protocol);
453   if (shout_set_protocol (sink->conn, proto) != SHOUTERR_SUCCESS)
454     goto set_failed;
455
456   /* --- FIXME: shout requires an ip, and fails if it is given a host. */
457   /* may want to put convert_to_ip(shout2send->ip) here */
458   cur_prop = "ip";
459   GST_DEBUG_OBJECT (sink, "setting ip: %s", sink->ip);
460   if (shout_set_host (sink->conn, sink->ip) != SHOUTERR_SUCCESS)
461     goto set_failed;
462
463   cur_prop = "port";
464   GST_DEBUG_OBJECT (sink, "setting port: %u", sink->port);
465   if (shout_set_port (sink->conn, sink->port) != SHOUTERR_SUCCESS)
466     goto set_failed;
467
468   cur_prop = "password";
469   GST_DEBUG_OBJECT (sink, "setting password: %s", sink->password);
470   if (shout_set_password (sink->conn, sink->password) != SHOUTERR_SUCCESS)
471     goto set_failed;
472
473   cur_prop = "streamname";
474   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->streamname);
475   if (shout_set_name (sink->conn, sink->streamname) != SHOUTERR_SUCCESS)
476     goto set_failed;
477
478   cur_prop = "description";
479   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->description);
480   if (shout_set_description (sink->conn, sink->description) != SHOUTERR_SUCCESS)
481     goto set_failed;
482
483   cur_prop = "genre";
484   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->genre);
485   if (shout_set_genre (sink->conn, sink->genre) != SHOUTERR_SUCCESS)
486     goto set_failed;
487
488   cur_prop = "mount";
489   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->mount);
490   if (shout_set_mount (sink->conn, sink->mount) != SHOUTERR_SUCCESS)
491     goto set_failed;
492
493   cur_prop = "username";
494   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, "source");
495   if (shout_set_user (sink->conn, sink->username) != SHOUTERR_SUCCESS)
496     goto set_failed;
497
498   version_string = gst_version_string ();
499   cur_prop = "agent";
500   GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, version_string);
501   if (shout_set_agent (sink->conn, version_string) != SHOUTERR_SUCCESS) {
502     g_free (version_string);
503     goto set_failed;
504   }
505
506   g_free (version_string);
507   return TRUE;
508
509 /* ERROR */
510 set_failed:
511   {
512     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
513         ("Error setting %s: %s", cur_prop, shout_get_error (sink->conn)));
514     return FALSE;
515   }
516 }
517
518 static gboolean
519 gst_shout2send_connect (GstShout2send * sink)
520 {
521   GST_DEBUG_OBJECT (sink, "Connection format is: %s",
522       (sink->audio_format == SHOUT_FORMAT_VORBIS) ? "vorbis" :
523       ((sink->audio_format == SHOUT_FORMAT_MP3) ? "mp3" : "unknown"));
524
525   if (shout_set_format (sink->conn, sink->audio_format) != SHOUTERR_SUCCESS)
526     goto could_not_set_format;
527
528   if (shout_open (sink->conn) != SHOUTERR_SUCCESS)
529     goto could_not_connect;
530
531   GST_DEBUG_OBJECT (sink, "connected to server");
532   sink->connected = TRUE;
533
534   /* let's set metadata */
535   if (sink->songmetadata) {
536     shout_metadata_t *pmetadata;
537
538     GST_DEBUG_OBJECT (sink, "shout metadata now: %s", sink->songmetadata);
539     pmetadata = shout_metadata_new ();
540     shout_metadata_add (pmetadata, "song", sink->songmetadata);
541     shout_set_metadata (sink->conn, pmetadata);
542     shout_metadata_free (pmetadata);
543   }
544
545   return TRUE;
546
547 /* ERRORS */
548 could_not_set_format:
549   {
550     GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
551         ("Error setting connection format: %s", shout_get_error (sink->conn)));
552     return FALSE;
553   }
554
555 could_not_connect:
556   {
557     GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
558         (_("Could not connect to server")),
559         ("shout_open() failed: err=%s", shout_get_error (sink->conn)));
560     g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
561         shout_get_errno (sink->conn));
562     return FALSE;
563   }
564 }
565
566 static gboolean
567 gst_shout2send_stop (GstBaseSink * basesink)
568 {
569   GstShout2send *sink = GST_SHOUT2SEND (basesink);
570
571   if (sink->conn) {
572     if (sink->connected)
573       shout_close (sink->conn);
574     shout_free (sink->conn);
575     sink->conn = NULL;
576   }
577
578   if (sink->songmetadata) {
579     g_free (sink->songmetadata);
580     sink->songmetadata = NULL;
581   }
582
583   sink->connected = FALSE;
584
585   return TRUE;
586 }
587
588 static GstFlowReturn
589 gst_shout2send_render (GstBaseSink * basesink, GstBuffer * buf)
590 {
591   GstShout2send *sink;
592   glong ret;
593
594   sink = GST_SHOUT2SEND (basesink);
595
596   /* presumably we connect here because we need to know the format before
597    * we can set up the connection, which we don't know yet in _start() */
598   if (!sink->connected) {
599     if (!gst_shout2send_connect (sink))
600       return GST_FLOW_ERROR;
601   }
602
603   /* FIXME: do we want to do syncing here at all? (tpm) */
604   /* GST_LOG_OBJECT (sink, "using libshout to sync"); */
605   shout_sync (sink->conn);
606
607   GST_LOG_OBJECT (sink, "sending %u bytes of data", GST_BUFFER_SIZE (buf));
608   ret = shout_send (sink->conn, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
609   if (ret != SHOUTERR_SUCCESS)
610     goto send_error;
611
612   return GST_FLOW_OK;
613
614 /* ERRORS */
615 send_error:
616   {
617     GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
618         ("shout_send() failed: %s", shout_get_error (sink->conn)));
619     g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
620         shout_get_errno (sink->conn));
621     return GST_FLOW_ERROR;
622   }
623 }
624
625 static void
626 gst_shout2send_set_property (GObject * object, guint prop_id,
627     const GValue * value, GParamSpec * pspec)
628 {
629   GstShout2send *shout2send;
630
631   shout2send = GST_SHOUT2SEND (object);
632   switch (prop_id) {
633
634     case ARG_IP:
635       if (shout2send->ip)
636         g_free (shout2send->ip);
637       shout2send->ip = g_strdup (g_value_get_string (value));
638       break;
639     case ARG_PORT:
640       shout2send->port = g_value_get_int (value);
641       break;
642     case ARG_PASSWORD:
643       if (shout2send->password)
644         g_free (shout2send->password);
645       shout2send->password = g_strdup (g_value_get_string (value));
646       break;
647     case ARG_USERNAME:
648       if (shout2send->username)
649         g_free (shout2send->username);
650       shout2send->username = g_strdup (g_value_get_string (value));
651       break;
652     case ARG_STREAMNAME:       /* Name of the stream */
653       if (shout2send->streamname)
654         g_free (shout2send->streamname);
655       shout2send->streamname = g_strdup (g_value_get_string (value));
656       break;
657     case ARG_DESCRIPTION:      /* Description of the stream */
658       if (shout2send->description)
659         g_free (shout2send->description);
660       shout2send->description = g_strdup (g_value_get_string (value));
661       break;
662     case ARG_GENRE:            /* Genre of the stream */
663       if (shout2send->genre)
664         g_free (shout2send->genre);
665       shout2send->genre = g_strdup (g_value_get_string (value));
666       break;
667     case ARG_PROTOCOL:         /* protocol to connect with */
668       shout2send->protocol = g_value_get_enum (value);
669       break;
670     case ARG_MOUNT:            /* mountpoint of stream (icecast only) */
671       if (shout2send->mount)
672         g_free (shout2send->mount);
673       shout2send->mount = g_strdup (g_value_get_string (value));
674       break;
675     case ARG_URL:              /* Url of the stream (I'm guessing) */
676       if (shout2send->url)
677         g_free (shout2send->url);
678       shout2send->url = g_strdup (g_value_get_string (value));
679       break;
680     default:
681       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
682       break;
683   }
684 }
685
686 static void
687 gst_shout2send_get_property (GObject * object, guint prop_id,
688     GValue * value, GParamSpec * pspec)
689 {
690   GstShout2send *shout2send;
691
692   shout2send = GST_SHOUT2SEND (object);
693   switch (prop_id) {
694
695     case ARG_IP:
696       g_value_set_string (value, shout2send->ip);
697       break;
698     case ARG_PORT:
699       g_value_set_int (value, shout2send->port);
700       break;
701     case ARG_PASSWORD:
702       g_value_set_string (value, shout2send->password);
703       break;
704     case ARG_USERNAME:
705       g_value_set_string (value, shout2send->username);
706       break;
707     case ARG_STREAMNAME:       /* Name of the stream */
708       g_value_set_string (value, shout2send->streamname);
709       break;
710     case ARG_DESCRIPTION:      /* Description of the stream */
711       g_value_set_string (value, shout2send->description);
712       break;
713     case ARG_GENRE:            /* Genre of the stream */
714       g_value_set_string (value, shout2send->genre);
715       break;
716     case ARG_PROTOCOL:         /* protocol to connect with */
717       g_value_set_enum (value, shout2send->protocol);
718       break;
719     case ARG_MOUNT:            /* mountpoint of stream (icecast only) */
720       g_value_set_string (value, shout2send->mount);
721       break;
722     case ARG_URL:              /* Url of stream (I'm guessing) */
723       g_value_set_string (value, shout2send->url);
724       break;
725     default:
726       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
727       break;
728   }
729 }
730
731 static gboolean
732 gst_shout2send_setcaps (GstPad * pad, GstCaps * caps)
733 {
734   const gchar *mimetype;
735   GstShout2send *shout2send;
736   gboolean ret = TRUE;
737
738   shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
739
740   mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
741
742   GST_DEBUG_OBJECT (shout2send, "mimetype of caps given is: %s", mimetype);
743
744   if (!strcmp (mimetype, "audio/mpeg")) {
745     shout2send->audio_format = SHOUT_FORMAT_MP3;
746   } else if (!strcmp (mimetype, "application/ogg")) {
747     shout2send->audio_format = SHOUT_FORMAT_VORBIS;
748   } else {
749     ret = FALSE;
750   }
751
752   return ret;
753 }
754
755 static gboolean
756 plugin_init (GstPlugin * plugin)
757 {
758 #ifdef ENABLE_NLS
759   setlocale (LC_ALL, "");
760   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
761   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
762 #endif /* ENABLE_NLS */
763
764   return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
765       GST_TYPE_SHOUT2SEND);
766 }
767
768 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
769     GST_VERSION_MINOR,
770     "shout2send",
771     "Sends data to an icecast server using libshout2",
772     plugin_init,
773     VERSION, "LGPL", "libshout2", "http://www.icecast.org/download.html")