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 = {
32 "Sends data to an icecast server",
34 "Wim Taymans <wim.taymans@chello.be>, Pedro Corte-Real <typo@netcabo.pt>",
38 unsigned int audio_format = 100;
40 /* Shout2send signals and args */
48 ARG_IP, /* the ip of the server */
49 ARG_PORT, /* the encoder port number on the server */
50 ARG_PASSWORD, /* the encoder password on the server */
51 ARG_PUBLIC, /* is this stream public? */
52 ARG_NAME, /* Name of the stream */
53 ARG_DESCRIPTION, /* Description of the stream */
54 ARG_GENRE, /* Genre of the stream */
56 ARG_PROTOCOL, /* Protocol to connect with */
58 ARG_MOUNT, /* mountpoint of stream (icecast only) */
59 ARG_URL, /* Url of stream (I'm guessing) */
62 static GstPadTemplate*
63 sink_template_factory (void)
65 static GstPadTemplate *template = NULL;
68 template = gst_pad_template_new (
80 "layer", GST_PROPS_INT_RANGE (1, 3),
89 static void gst_shout2send_class_init (GstShout2sendClass *klass);
90 static void gst_shout2send_init (GstShout2send *shout2send);
92 static void gst_shout2send_chain (GstPad *pad, GstBuffer *buf);
93 static GstPadLinkReturn gst_shout2send_connect (GstPad *pad, GstCaps *caps);
95 static void gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
96 static void gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
98 static GstElementStateReturn gst_shout2send_change_state (GstElement *element);
100 static GstElementClass *parent_class = NULL;
101 /*static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 }; */
103 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
105 gst_shout2send_protocol_get_type (void)
107 static GType shout2send_protocol_type = 0;
108 static GEnumValue shout2send_protocol[] = {
109 { SHOUT2SEND_PROTOCOL_ICE, "1", "Ice Protocol"},
110 { SHOUT2SEND_PROTOCOL_XAUDIOCAST, "2", "Xaudiocast Protocol (icecast 1.3.x)"},
111 { SHOUT2SEND_PROTOCOL_ICY, "3", "Icy Protocol (ShoutCast)"},
112 { SHOUT2SEND_PROTOCOL_HTTP, "4", "Http Protocol (icecast 2.x)"},
115 if (!shout2send_protocol_type) {
116 shout2send_protocol_type = 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), NULL,
130 (GClassInitFunc)gst_shout2send_class_init,
133 sizeof(GstShout2send),
135 (GInstanceInitFunc)gst_shout2send_init,
137 shout2send_type = g_type_register_static(GST_TYPE_ELEMENT, "GstShout2send", &shout2send_info, 0);
139 return shout2send_type;
143 gst_shout2send_class_init (GstShout2sendClass *klass)
145 GObjectClass *gobject_class;
146 GstElementClass *gstelement_class;
148 gobject_class = (GObjectClass*)klass;
149 gstelement_class = (GstElementClass*)klass;
151 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
153 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_IP,
154 g_param_spec_string("ip","ip","ip",
155 NULL, G_PARAM_READWRITE)); /* CHECKME */
156 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT,
157 g_param_spec_int("port","port","port",
158 1,G_MAXUSHORT,8000,G_PARAM_READWRITE)); /* CHECKME */
160 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PASSWORD,
161 g_param_spec_string("password","password","password",
162 NULL, G_PARAM_READWRITE)); /* CHECKME */
165 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NAME,
166 g_param_spec_string("name","name","name",
167 NULL, G_PARAM_READWRITE)); /* CHECKME */
169 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DESCRIPTION,
170 g_param_spec_string("description","description","description",
171 NULL, G_PARAM_READWRITE)); /* CHECKME */
173 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_GENRE,
174 g_param_spec_string("genre","genre","genre",
175 NULL, G_PARAM_READWRITE)); /* CHECKME */
177 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
178 g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
179 GST_TYPE_SHOUT_PROTOCOL, SHOUT2SEND_PROTOCOL_HTTP, G_PARAM_READWRITE));
183 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MOUNT,
184 g_param_spec_string("mount","mount","mount",
185 NULL, G_PARAM_READWRITE)); /* CHECKME */
187 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_URL,
188 g_param_spec_string("url","url","url",
189 NULL, G_PARAM_READWRITE)); /* CHECKME */
193 gobject_class->set_property = gst_shout2send_set_property;
194 gobject_class->get_property = gst_shout2send_get_property;
196 gstelement_class->change_state = gst_shout2send_change_state;
200 gst_shout2send_init (GstShout2send *shout2send)
202 shout2send->sinkpad = gst_pad_new_from_template (sink_template_factory (), "sink");
203 gst_element_add_pad(GST_ELEMENT(shout2send),shout2send->sinkpad);
204 gst_pad_set_chain_function(shout2send->sinkpad,gst_shout2send_chain);
206 gst_pad_set_link_function (shout2send->sinkpad, gst_shout2send_connect);
208 shout2send->ip = g_strdup ("127.0.0.1");
209 shout2send->port = 8000;
210 shout2send->password = g_strdup ("hackme");
211 shout2send->name = g_strdup ("");
212 shout2send->description = g_strdup ("");
213 shout2send->genre = g_strdup ("");
214 shout2send->mount = g_strdup ("");
215 shout2send->url = g_strdup ("");
216 shout2send->protocol = SHOUT2SEND_PROTOCOL_HTTP;
220 gst_shout2send_chain (GstPad *pad, GstBuffer *buf)
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, const GValue *value, GParamSpec *pspec)
248 GstShout2send *shout2send;
250 /* it's not null if we got it, but it might not be ours */
251 g_return_if_fail(GST_IS_SHOUT2SEND(object));
252 shout2send = GST_SHOUT2SEND(object);
258 g_free (shout2send->ip);
259 shout2send->ip = g_strdup (g_value_get_string (value));
263 shout2send->port = g_value_get_int (value);
267 if (shout2send->password)
268 g_free (shout2send->password);
269 shout2send->password = g_strdup (g_value_get_string (value));
272 case ARG_NAME: /* Name of the stream */
273 if (shout2send->name)
274 g_free (shout2send->name);
275 shout2send->name = g_strdup (g_value_get_string (value));
278 case ARG_DESCRIPTION: /* Description of the stream */
279 if (shout2send->description)
280 g_free (shout2send->description);
281 shout2send->description = g_strdup (g_value_get_string (value));
284 case ARG_GENRE: /* Genre of the stream */
285 if (shout2send->genre)
286 g_free (shout2send->genre);
287 shout2send->genre = g_strdup (g_value_get_string (value));
290 case ARG_PROTOCOL: /* protocol to connect with */
291 shout2send->protocol = g_value_get_enum (value);
294 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
295 if (shout2send->mount)
296 g_free (shout2send->mount);
297 shout2send->mount = g_strdup (g_value_get_string (value));
300 case ARG_URL: /* Url of the stream (I'm guessing) */
302 g_free (shout2send->url);
303 shout2send->url = g_strdup (g_value_get_string (value));
312 gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
314 GstShout2send *shout2send;
316 /* it's not null if we got it, but it might not be ours */
317 g_return_if_fail(GST_IS_SHOUT2SEND(object));
318 shout2send = GST_SHOUT2SEND(object);
323 g_value_set_string (value, shout2send->ip);
326 g_value_set_int (value, shout2send->port);
329 g_value_set_string (value, shout2send->password);
332 case ARG_NAME: /* Name of the stream */
333 g_value_set_string (value, shout2send->name);
336 case ARG_DESCRIPTION: /* Description of the stream */
337 g_value_set_string (value, shout2send->description);
340 case ARG_GENRE: /* Genre of the stream */
341 g_value_set_string (value, shout2send->genre);
344 case ARG_PROTOCOL: /* protocol to connect with */
345 g_value_set_enum (value, shout2send->protocol);
348 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
349 g_value_set_string (value, shout2send->mount);
352 case ARG_URL: /* Url of stream (I'm guessing) */
353 g_value_set_string (value, shout2send->url);
358 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363 static GstPadLinkReturn
364 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
368 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"))
376 audio_format = SHOUT_FORMAT_VORBIS;
377 return GST_PAD_LINK_OK;
380 return GST_PAD_LINK_REFUSED;
385 static GstElementStateReturn
386 gst_shout2send_change_state (GstElement *element)
388 GstShout2send *shout2send;
390 guint major, minor, micro;
393 gchar *version_string;
395 g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
397 shout2send = GST_SHOUT2SEND(element);
399 GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
401 /* if going down into NULL state, close the file if it's open */
402 switch (GST_STATE_TRANSITION (element)) {
403 case GST_STATE_NULL_TO_READY:
404 shout2send->conn = shout_new();
406 switch (shout2send->protocol) {
407 case SHOUT2SEND_PROTOCOL_ICE:
408 proto = SHOUT_PROTOCOL_ICE;
410 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
411 proto = SHOUT_PROTOCOL_XAUDIOCAST;
413 case SHOUT2SEND_PROTOCOL_ICY:
414 proto = SHOUT_PROTOCOL_ICY;
416 case SHOUT2SEND_PROTOCOL_HTTP:
417 proto = SHOUT_PROTOCOL_HTTP;
421 if (shout_set_protocol(shout2send->conn, proto) != SHOUTERR_SUCCESS)
423 g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
426 /* --- FIXME: shout requires an ip, and fails if it is given a host. */
427 /* may want to put convert_to_ip(shout2send->ip) here */
430 if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
432 g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn));
436 if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
438 g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn));
441 if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
443 g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn));
446 if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
448 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
451 if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
453 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
456 if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
458 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
461 if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
463 g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn));
466 if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
468 g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn));
471 gst_version(&major,&minor,µ);
473 version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
475 if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
477 g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn));
480 g_free (version_string);
485 case GST_STATE_READY_TO_PAUSED:
487 /* This sets the format acording to the capabilities of what
488 we are being given as input. */
490 if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
492 g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn));
495 if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
496 g_print ("connected to server...\n");
499 g_warning ("Couldn't connect to server: %s", shout_get_error(shout2send->conn));
500 shout_close (shout2send->conn);
501 shout_free (shout2send->conn);
502 return GST_STATE_FAILURE;
505 case GST_STATE_PAUSED_TO_READY:
506 shout_close (shout2send->conn);
507 shout_free (shout2send->conn);
513 /* if we haven't failed already, give the parent class a chance to ;-) */
514 if (GST_ELEMENT_CLASS (parent_class)->change_state)
515 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
517 return GST_STATE_SUCCESS;
521 plugin_init (GModule *module, GstPlugin *plugin)
523 GstElementFactory *factory;
525 factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
526 g_return_val_if_fail(factory != NULL, FALSE);
528 gst_element_factory_add_pad_template (factory, sink_template_factory ());
530 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
535 GstPluginDesc plugin_desc = {