2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
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.
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.
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.
23 #include "gstshout2.h"
27 /* elementfactory information */
28 static GstElementDetails shout2send_details = {
31 "Sends data to an icecast server",
32 "Wim Taymans <wim.taymans@chello.be>\n" "Pedro Corte-Real <typo@netcabo.pt>"
35 unsigned int audio_format = 100;
37 /* Shout2send signals and args */
47 ARG_IP, /* the ip of the server */
48 ARG_PORT, /* the encoder port number on the server */
49 ARG_PASSWORD, /* the encoder password on the server */
50 ARG_PUBLIC, /* is this stream public? */
51 ARG_NAME, /* Name of the stream */
52 ARG_DESCRIPTION, /* Description of the stream */
53 ARG_GENRE, /* Genre of the stream */
55 ARG_PROTOCOL, /* Protocol to connect with */
57 ARG_MOUNT, /* mountpoint of stream (icecast only) */
58 ARG_URL, /* Url of stream (I'm guessing) */
61 static GstPadTemplate *
62 sink_template_factory (void)
64 static GstPadTemplate *template = NULL;
67 template = gst_pad_template_new ("sink",
70 gst_caps_new ("shout2send_sink",
73 gst_caps_new ("shout2send_sink",
75 gst_props_new ("mpegversion", GST_PROPS_INT (1),
76 "layer", GST_PROPS_INT_RANGE (1, 3), NULL)), NULL);
82 static void gst_shout2send_class_init (GstShout2sendClass * klass);
83 static void gst_shout2send_base_init (GstShout2sendClass * klass);
84 static void gst_shout2send_init (GstShout2send * shout2send);
86 static void gst_shout2send_chain (GstPad * pad, GstData * _data);
87 static GstPadLinkReturn gst_shout2send_connect (GstPad * pad, GstCaps * caps);
89 static void gst_shout2send_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_shout2send_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
94 static GstElementStateReturn gst_shout2send_change_state (GstElement * element);
96 static GstElementClass *parent_class = NULL;
98 /*static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 }; */
100 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
102 gst_shout2send_protocol_get_type (void)
104 static GType shout2send_protocol_type = 0;
105 static GEnumValue shout2send_protocol[] = {
106 {SHOUT2SEND_PROTOCOL_ICE, "1", "Ice Protocol"},
107 {SHOUT2SEND_PROTOCOL_XAUDIOCAST, "2",
108 "Xaudiocast Protocol (icecast 1.3.x)"},
109 {SHOUT2SEND_PROTOCOL_ICY, "3", "Icy Protocol (ShoutCast)"},
110 {SHOUT2SEND_PROTOCOL_HTTP, "4", "Http Protocol (icecast 2.x)"},
114 if (!shout2send_protocol_type) {
115 shout2send_protocol_type =
116 g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
118 return shout2send_protocol_type;
122 gst_shout2send_get_type (void)
124 static GType shout2send_type = 0;
126 if (!shout2send_type) {
127 static const GTypeInfo shout2send_info = {
128 sizeof (GstShout2sendClass),
129 (GBaseInitFunc) gst_shout2send_base_init,
131 (GClassInitFunc) gst_shout2send_class_init,
134 sizeof (GstShout2send),
136 (GInstanceInitFunc) gst_shout2send_init,
140 g_type_register_static (GST_TYPE_ELEMENT, "GstShout2send",
141 &shout2send_info, 0);
143 return shout2send_type;
147 gst_shout2send_base_init (GstShout2sendClass * klass)
149 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
151 gst_element_class_add_pad_template (element_class, sink_template_factory ());
152 gst_element_class_set_details (element_class, &shout2send_details);
156 gst_shout2send_class_init (GstShout2sendClass * klass)
158 GObjectClass *gobject_class;
159 GstElementClass *gstelement_class;
161 gobject_class = (GObjectClass *) klass;
162 gstelement_class = (GstElementClass *) klass;
164 parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
166 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_IP, g_param_spec_string ("ip", "ip", "ip", NULL, G_PARAM_READWRITE)); /* CHECKME */
167 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 */
169 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PASSWORD, g_param_spec_string ("password", "password", "password", NULL, G_PARAM_READWRITE)); /* CHECKME */
172 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NAME, g_param_spec_string ("name", "name", "name", NULL, G_PARAM_READWRITE)); /* CHECKME */
174 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESCRIPTION, g_param_spec_string ("description", "description", "description", NULL, G_PARAM_READWRITE)); /* CHECKME */
176 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GENRE, g_param_spec_string ("genre", "genre", "genre", NULL, G_PARAM_READWRITE)); /* CHECKME */
178 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
179 g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
180 GST_TYPE_SHOUT_PROTOCOL, SHOUT2SEND_PROTOCOL_HTTP,
185 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MOUNT, g_param_spec_string ("mount", "mount", "mount", NULL, G_PARAM_READWRITE)); /* CHECKME */
187 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_URL, g_param_spec_string ("url", "url", "url", NULL, G_PARAM_READWRITE)); /* CHECKME */
191 gobject_class->set_property = gst_shout2send_set_property;
192 gobject_class->get_property = gst_shout2send_get_property;
194 gstelement_class->change_state = gst_shout2send_change_state;
198 gst_shout2send_init (GstShout2send * shout2send)
200 shout2send->sinkpad =
201 gst_pad_new_from_template (sink_template_factory (), "sink");
202 gst_element_add_pad (GST_ELEMENT (shout2send), shout2send->sinkpad);
203 gst_pad_set_chain_function (shout2send->sinkpad, gst_shout2send_chain);
205 gst_pad_set_link_function (shout2send->sinkpad, gst_shout2send_connect);
207 shout2send->ip = g_strdup ("127.0.0.1");
208 shout2send->port = 8000;
209 shout2send->password = g_strdup ("hackme");
210 shout2send->name = g_strdup ("");
211 shout2send->description = g_strdup ("");
212 shout2send->genre = g_strdup ("");
213 shout2send->mount = g_strdup ("");
214 shout2send->url = g_strdup ("");
215 shout2send->protocol = SHOUT2SEND_PROTOCOL_HTTP;
219 gst_shout2send_chain (GstPad * pad, GstData * _data)
221 GstBuffer *buf = GST_BUFFER (_data);
222 GstShout2send *shout2send;
225 g_return_if_fail (pad != NULL);
226 g_return_if_fail (GST_IS_PAD (pad));
227 g_return_if_fail (buf != NULL);
229 shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
231 g_return_if_fail (shout2send != NULL);
232 g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
234 ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
235 GST_BUFFER_SIZE (buf));
236 if (ret != SHOUTERR_SUCCESS) {
237 g_warning ("send error: %s...\n", shout_get_error (shout2send->conn));
240 shout_sync (shout2send->conn);
242 gst_buffer_unref (buf);
246 gst_shout2send_set_property (GObject * object, guint prop_id,
247 const GValue * value, GParamSpec * pspec)
249 GstShout2send *shout2send;
251 /* it's not null if we got it, but it might not be ours */
252 g_return_if_fail (GST_IS_SHOUT2SEND (object));
253 shout2send = GST_SHOUT2SEND (object);
259 g_free (shout2send->ip);
260 shout2send->ip = g_strdup (g_value_get_string (value));
264 shout2send->port = g_value_get_int (value);
268 if (shout2send->password)
269 g_free (shout2send->password);
270 shout2send->password = g_strdup (g_value_get_string (value));
273 case ARG_NAME: /* Name of the stream */
274 if (shout2send->name)
275 g_free (shout2send->name);
276 shout2send->name = g_strdup (g_value_get_string (value));
279 case ARG_DESCRIPTION: /* Description of the stream */
280 if (shout2send->description)
281 g_free (shout2send->description);
282 shout2send->description = g_strdup (g_value_get_string (value));
285 case ARG_GENRE: /* Genre of the stream */
286 if (shout2send->genre)
287 g_free (shout2send->genre);
288 shout2send->genre = g_strdup (g_value_get_string (value));
291 case ARG_PROTOCOL: /* protocol to connect with */
292 shout2send->protocol = g_value_get_enum (value);
295 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
296 if (shout2send->mount)
297 g_free (shout2send->mount);
298 shout2send->mount = g_strdup (g_value_get_string (value));
301 case ARG_URL: /* Url of the stream (I'm guessing) */
303 g_free (shout2send->url);
304 shout2send->url = g_strdup (g_value_get_string (value));
313 gst_shout2send_get_property (GObject * object, guint prop_id, GValue * value,
316 GstShout2send *shout2send;
318 /* it's not null if we got it, but it might not be ours */
319 g_return_if_fail (GST_IS_SHOUT2SEND (object));
320 shout2send = GST_SHOUT2SEND (object);
325 g_value_set_string (value, shout2send->ip);
328 g_value_set_int (value, shout2send->port);
331 g_value_set_string (value, shout2send->password);
334 case ARG_NAME: /* Name of the stream */
335 g_value_set_string (value, shout2send->name);
338 case ARG_DESCRIPTION: /* Description of the stream */
339 g_value_set_string (value, shout2send->description);
342 case ARG_GENRE: /* Genre of the stream */
343 g_value_set_string (value, shout2send->genre);
346 case ARG_PROTOCOL: /* protocol to connect with */
347 g_value_set_enum (value, shout2send->protocol);
350 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
351 g_value_set_string (value, shout2send->mount);
354 case ARG_URL: /* Url of stream (I'm guessing) */
355 g_value_set_string (value, shout2send->url);
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 static GstPadLinkReturn
366 gst_shout2send_connect (GstPad * pad, GstCaps * caps)
369 if (!strcmp (gst_caps_get_mime (caps), "audio/mpeg")) {
370 audio_format = SHOUT_FORMAT_MP3;
371 return GST_PAD_LINK_OK;
374 if (!strcmp (gst_caps_get_mime (caps), "application/ogg")) {
375 audio_format = SHOUT_FORMAT_VORBIS;
376 return GST_PAD_LINK_OK;
378 return GST_PAD_LINK_REFUSED;
383 static GstElementStateReturn
384 gst_shout2send_change_state (GstElement * element)
386 GstShout2send *shout2send;
388 guint major, minor, micro;
391 gchar *version_string;
393 g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
395 shout2send = GST_SHOUT2SEND (element);
397 GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
399 /* if going down into NULL state, close the file if it's open */
400 switch (GST_STATE_TRANSITION (element)) {
401 case GST_STATE_NULL_TO_READY:
402 shout2send->conn = shout_new ();
404 switch (shout2send->protocol) {
405 case SHOUT2SEND_PROTOCOL_ICE:
406 proto = SHOUT_PROTOCOL_ICE;
408 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
409 proto = SHOUT_PROTOCOL_XAUDIOCAST;
411 case SHOUT2SEND_PROTOCOL_ICY:
412 proto = SHOUT_PROTOCOL_ICY;
414 case SHOUT2SEND_PROTOCOL_HTTP:
415 proto = SHOUT_PROTOCOL_HTTP;
419 if (shout_set_protocol (shout2send->conn, proto) != SHOUTERR_SUCCESS) {
420 g_error ("Error setting protocol: %s\n",
421 shout_get_error (shout2send->conn));
424 /* --- FIXME: shout requires an ip, and fails if it is given a host. */
425 /* may want to put convert_to_ip(shout2send->ip) here */
428 if (shout_set_host (shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS) {
429 g_error ("Error setting host: %s\n",
430 shout_get_error (shout2send->conn));
434 if (shout_set_port (shout2send->conn,
435 shout2send->port) != SHOUTERR_SUCCESS) {
436 g_error ("Error setting port: %s\n",
437 shout_get_error (shout2send->conn));
440 if (shout_set_password (shout2send->conn,
441 shout2send->password) != SHOUTERR_SUCCESS) {
442 g_error ("Error setting password: %s\n",
443 shout_get_error (shout2send->conn));
446 if (shout_set_name (shout2send->conn,
447 shout2send->name) != SHOUTERR_SUCCESS) {
448 g_error ("Error setting name: %s\n",
449 shout_get_error (shout2send->conn));
452 if (shout_set_description (shout2send->conn,
453 shout2send->description) != SHOUTERR_SUCCESS) {
454 g_error ("Error setting name: %s\n",
455 shout_get_error (shout2send->conn));
458 if (shout_set_genre (shout2send->conn,
459 shout2send->genre) != SHOUTERR_SUCCESS) {
460 g_error ("Error setting name: %s\n",
461 shout_get_error (shout2send->conn));
464 if (shout_set_mount (shout2send->conn,
465 shout2send->mount) != SHOUTERR_SUCCESS) {
466 g_error ("Error setting mount point: %s\n",
467 shout_get_error (shout2send->conn));
470 if (shout_set_user (shout2send->conn, "source") != SHOUTERR_SUCCESS) {
471 g_error ("Error setting user: %s\n",
472 shout_get_error (shout2send->conn));
475 gst_version (&major, &minor, µ);
478 g_strdup_printf ("GStreamer %d.%d.%d", major, minor, micro);
480 if (shout_set_agent (shout2send->conn,
481 version_string) != SHOUTERR_SUCCESS) {
482 g_error ("Error setting agent: %s\n",
483 shout_get_error (shout2send->conn));
486 g_free (version_string);
491 case GST_STATE_READY_TO_PAUSED:
493 /* This sets the format acording to the capabilities of what
494 we are being given as input. */
496 if (shout_set_format (shout2send->conn, audio_format) != SHOUTERR_SUCCESS) {
497 g_error ("Error setting connection format: %s\n",
498 shout_get_error (shout2send->conn));
501 if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
502 g_print ("connected to server...\n");
504 g_warning ("Couldn't connect to server: %s",
505 shout_get_error (shout2send->conn));
506 shout_close (shout2send->conn);
507 shout_free (shout2send->conn);
508 return GST_STATE_FAILURE;
511 case GST_STATE_PAUSED_TO_READY:
512 shout_close (shout2send->conn);
513 shout_free (shout2send->conn);
519 /* if we haven't failed already, give the parent class a chance to ;-) */
520 if (GST_ELEMENT_CLASS (parent_class)->change_state)
521 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
523 return GST_STATE_SUCCESS;
527 plugin_init (GstPlugin * plugin)
529 return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
530 GST_TYPE_SHOUT2SEND);
533 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
536 "Sends data to an icecast server using libshout2",
538 VERSION, "LGPL", "libshout2", "http://www.icecast.org/download.html")