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