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.
20 #include "gstshout2.h"
23 /* elementfactory information */
24 static GstElementDetails shout2send_details = {
26 "Sink/Network/Icecast2",
28 "Sends data to an icecast server",
30 "Wim Taymans <wim.taymans@chello.be>, Pedro Corte-Real <typo@netcabo.pt>",
34 unsigned int audio_format = 100;
36 /* Shout2send signals and args */
44 ARG_IP, /* the ip of the server */
45 ARG_PORT, /* the encoder port number on the server */
46 ARG_PASSWORD, /* the encoder password on the server */
47 ARG_PUBLIC, /* is this stream public? */
48 ARG_NAME, /* Name of the stream */
49 ARG_DESCRIPTION, /* Description of the stream */
50 ARG_GENRE, /* Genre of the stream */
52 ARG_PROTOCOL, /* Protocol to connect with */
54 ARG_MOUNT, /* mountpoint of stream (icecast only) */
55 ARG_URL, /* Url of stream (I'm guessing) */
58 static GstPadTemplate*
59 sink_template_factory (void)
61 static GstPadTemplate *template = NULL;
64 template = gst_pad_template_new (
82 static void gst_shout2send_class_init (GstShout2sendClass *klass);
83 static void gst_shout2send_init (GstShout2send *shout2send);
85 static void gst_shout2send_chain (GstPad *pad, GstBuffer *buf);
86 static GstPadConnectReturn gst_shout2send_connect (GstPad *pad, GstCaps *caps);
88 static void gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
89 static void gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
91 static GstElementStateReturn gst_shout2send_change_state (GstElement *element);
93 static GstElementClass *parent_class = NULL;
94 /*static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 }; */
96 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
98 gst_shout2send_protocol_get_type (void)
100 static GType shout2send_protocol_type = 0;
101 static GEnumValue shout2send_protocol[] = {
102 { SHOUT2SEND_PROTOCOL_ICE, "1", "Ice Protocol"},
103 { SHOUT2SEND_PROTOCOL_XAUDIOCAST, "2", "Xaudiocast Protocol (icecast 1.3.x)"},
104 { SHOUT2SEND_PROTOCOL_ICY, "3", "Icy Protocol (ShoutCast)"},
105 { SHOUT2SEND_PROTOCOL_HTTP, "4", "Http Protocol (icecast 2.x)"},
108 if (!shout2send_protocol_type) {
109 shout2send_protocol_type = g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
111 return shout2send_protocol_type;
115 gst_shout2send_get_type(void)
117 static GType shout2send_type = 0;
119 if (!shout2send_type) {
120 static const GTypeInfo shout2send_info = {
121 sizeof(GstShout2sendClass), NULL,
123 (GClassInitFunc)gst_shout2send_class_init,
126 sizeof(GstShout2send),
128 (GInstanceInitFunc)gst_shout2send_init,
130 shout2send_type = g_type_register_static(GST_TYPE_ELEMENT, "GstShout2send", &shout2send_info, 0);
132 return shout2send_type;
136 gst_shout2send_class_init (GstShout2sendClass *klass)
138 GObjectClass *gobject_class;
139 GstElementClass *gstelement_class;
141 gobject_class = (GObjectClass*)klass;
142 gstelement_class = (GstElementClass*)klass;
144 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
146 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_IP,
147 g_param_spec_string("ip","ip","ip",
148 NULL, G_PARAM_READWRITE)); /* CHECKME */
149 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT,
150 g_param_spec_int("port","port","port",
151 1,G_MAXUSHORT,8000,G_PARAM_READWRITE)); /* CHECKME */
153 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PASSWORD,
154 g_param_spec_string("password","password","password",
155 NULL, G_PARAM_READWRITE)); /* CHECKME */
158 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NAME,
159 g_param_spec_string("name","name","name",
160 NULL, G_PARAM_READWRITE)); /* CHECKME */
162 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DESCRIPTION,
163 g_param_spec_string("description","description","description",
164 NULL, G_PARAM_READWRITE)); /* CHECKME */
166 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_GENRE,
167 g_param_spec_string("genre","genre","genre",
168 NULL, G_PARAM_READWRITE)); /* CHECKME */
170 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
171 g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
172 GST_TYPE_SHOUT_PROTOCOL, SHOUT2SEND_PROTOCOL_HTTP, G_PARAM_READWRITE));
176 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MOUNT,
177 g_param_spec_string("mount","mount","mount",
178 NULL, G_PARAM_READWRITE)); /* CHECKME */
180 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_URL,
181 g_param_spec_string("url","url","url",
182 NULL, G_PARAM_READWRITE)); /* CHECKME */
186 gobject_class->set_property = gst_shout2send_set_property;
187 gobject_class->get_property = gst_shout2send_get_property;
189 gstelement_class->change_state = gst_shout2send_change_state;
193 gst_shout2send_init (GstShout2send *shout2send)
195 shout2send->sinkpad = gst_pad_new_from_template (sink_template_factory (), "sink");
196 gst_element_add_pad(GST_ELEMENT(shout2send),shout2send->sinkpad);
197 gst_pad_set_chain_function(shout2send->sinkpad,gst_shout2send_chain);
199 gst_pad_set_link_function (shout2send->sinkpad, gst_shout2send_connect);
201 shout2send->ip = g_strdup ("127.0.0.1");
202 shout2send->port = 8000;
203 shout2send->password = g_strdup ("hackme");
204 shout2send->name = g_strdup ("");
205 shout2send->description = g_strdup ("");
206 shout2send->genre = g_strdup ("");
207 shout2send->mount = g_strdup ("");
208 shout2send->url = g_strdup ("");
209 shout2send->protocol = SHOUT2SEND_PROTOCOL_HTTP;
213 gst_shout2send_chain (GstPad *pad, GstBuffer *buf)
215 GstShout2send *shout2send;
218 g_return_if_fail(pad != NULL);
219 g_return_if_fail(GST_IS_PAD(pad));
220 g_return_if_fail(buf != NULL);
222 shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
224 g_return_if_fail (shout2send != NULL);
225 g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
227 ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
228 GST_BUFFER_SIZE (buf));
229 if (ret != SHOUTERR_SUCCESS) {
230 g_warning ("send error: %s...\n", shout_get_error(shout2send->conn));
233 shout_sync (shout2send->conn);
235 gst_buffer_unref (buf);
239 gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
241 GstShout2send *shout2send;
243 /* it's not null if we got it, but it might not be ours */
244 g_return_if_fail(GST_IS_SHOUT2SEND(object));
245 shout2send = GST_SHOUT2SEND(object);
251 g_free (shout2send->ip);
252 shout2send->ip = g_strdup (g_value_get_string (value));
256 shout2send->port = g_value_get_int (value);
260 if (shout2send->password)
261 g_free (shout2send->password);
262 shout2send->password = g_strdup (g_value_get_string (value));
265 case ARG_NAME: /* Name of the stream */
266 if (shout2send->name)
267 g_free (shout2send->name);
268 shout2send->name = g_strdup (g_value_get_string (value));
271 case ARG_DESCRIPTION: /* Description of the stream */
272 if (shout2send->description)
273 g_free (shout2send->description);
274 shout2send->description = g_strdup (g_value_get_string (value));
277 case ARG_GENRE: /* Genre of the stream */
278 if (shout2send->genre)
279 g_free (shout2send->genre);
280 shout2send->genre = g_strdup (g_value_get_string (value));
283 case ARG_PROTOCOL: /* protocol to connect with */
284 shout2send->protocol = g_value_get_enum (value);
287 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
288 if (shout2send->mount)
289 g_free (shout2send->mount);
290 shout2send->mount = g_strdup (g_value_get_string (value));
293 case ARG_URL: /* Url of the stream (I'm guessing) */
295 g_free (shout2send->url);
296 shout2send->url = g_strdup (g_value_get_string (value));
305 gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
307 GstShout2send *shout2send;
309 /* it's not null if we got it, but it might not be ours */
310 g_return_if_fail(GST_IS_SHOUT2SEND(object));
311 shout2send = GST_SHOUT2SEND(object);
316 g_value_set_string (value, shout2send->ip);
319 g_value_set_int (value, shout2send->port);
322 g_value_set_string (value, shout2send->password);
325 case ARG_NAME: /* Name of the stream */
326 g_value_set_string (value, shout2send->name);
329 case ARG_DESCRIPTION: /* Description of the stream */
330 g_value_set_string (value, shout2send->description);
333 case ARG_GENRE: /* Genre of the stream */
334 g_value_set_string (value, shout2send->genre);
337 case ARG_PROTOCOL: /* protocol to connect with */
338 g_value_set_enum (value, shout2send->protocol);
341 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
342 g_value_set_string (value, shout2send->mount);
345 case ARG_URL: /* Url of stream (I'm guessing) */
346 g_value_set_string (value, shout2send->url);
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
356 static GstPadConnectReturn
357 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
361 if (!strcmp(gst_caps_get_mime (caps), "audio/x-mp3"))
363 audio_format = SHOUT_FORMAT_MP3;
364 return GST_PAD_LINK_OK;
367 if (!strcmp(gst_caps_get_mime (caps), "application/x-ogg"))
369 audio_format = SHOUT_FORMAT_VORBIS;
370 return GST_PAD_LINK_OK;
373 return GST_PAD_LINK_REFUSED;
378 static GstElementStateReturn
379 gst_shout2send_change_state (GstElement *element)
381 GstShout2send *shout2send;
383 guint major, minor, micro;
386 gchar *version_string;
388 g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
390 shout2send = GST_SHOUT2SEND(element);
392 GST_DEBUG (0,"state pending %d", GST_STATE_PENDING (element));
394 /* if going down into NULL state, close the file if it's open */
395 switch (GST_STATE_TRANSITION (element)) {
396 case GST_STATE_NULL_TO_READY:
397 shout2send->conn = shout_new();
399 switch (shout2send->protocol) {
400 case SHOUT2SEND_PROTOCOL_ICE:
401 proto = SHOUT_PROTOCOL_ICE;
403 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
404 proto = SHOUT_PROTOCOL_XAUDIOCAST;
406 case SHOUT2SEND_PROTOCOL_ICY:
407 proto = SHOUT_PROTOCOL_ICY;
409 case SHOUT2SEND_PROTOCOL_HTTP:
410 proto = SHOUT_PROTOCOL_HTTP;
414 if (shout_set_protocol(shout2send->conn, proto) != SHOUTERR_SUCCESS)
416 g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
419 /* --- FIXME: shout requires an ip, and fails if it is given a host. */
420 /* may want to put convert_to_ip(shout2send->ip) here */
423 if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
425 g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn));
429 if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
431 g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn));
434 if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
436 g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn));
439 if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
441 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
444 if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
446 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
449 if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
451 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
454 if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
456 g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn));
459 if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
461 g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn));
464 gst_version(&major,&minor,µ);
466 version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
468 if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
470 g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn));
473 g_free (version_string);
478 case GST_STATE_READY_TO_PAUSED:
480 /* This sets the format acording to the capabilities of what
481 we are being given as input. */
483 if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
485 g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn));
488 if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
489 g_print ("connected to server...\n");
492 /* changed from g_warning, and included result code lookup. */
493 g_error ("couldn't connect to server...");
494 shout_close (shout2send->conn);
495 shout_free (shout2send->conn);
496 return GST_STATE_FAILURE;
499 case GST_STATE_READY_TO_NULL:
500 shout_close (shout2send->conn);
501 shout_free (shout2send->conn);
507 /* if we haven't failed already, give the parent class a chance to ;-) */
508 if (GST_ELEMENT_CLASS (parent_class)->change_state)
509 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
511 return GST_STATE_SUCCESS;
515 plugin_init (GModule *module, GstPlugin *plugin)
517 GstElementFactory *factory;
519 factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
520 g_return_val_if_fail(factory != NULL, FALSE);
522 gst_element_factory_add_pad_template (factory, sink_template_factory ());
524 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
529 GstPluginDesc plugin_desc = {