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, GstData *_data);
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, GstData *_data)
222 GstBuffer *buf = GST_BUFFER (_data);
223 GstShout2send *shout2send;
226 g_return_if_fail(pad != NULL);
227 g_return_if_fail(GST_IS_PAD(pad));
228 g_return_if_fail(buf != NULL);
230 shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
232 g_return_if_fail (shout2send != NULL);
233 g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
235 ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
236 GST_BUFFER_SIZE (buf));
237 if (ret != SHOUTERR_SUCCESS) {
238 g_warning ("send error: %s...\n", shout_get_error(shout2send->conn));
241 shout_sync (shout2send->conn);
243 gst_buffer_unref (buf);
247 gst_shout2send_set_property (GObject *object, guint prop_id, 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, GParamSpec *pspec)
315 GstShout2send *shout2send;
317 /* it's not null if we got it, but it might not be ours */
318 g_return_if_fail(GST_IS_SHOUT2SEND(object));
319 shout2send = GST_SHOUT2SEND(object);
324 g_value_set_string (value, shout2send->ip);
327 g_value_set_int (value, shout2send->port);
330 g_value_set_string (value, shout2send->password);
333 case ARG_NAME: /* Name of the stream */
334 g_value_set_string (value, shout2send->name);
337 case ARG_DESCRIPTION: /* Description of the stream */
338 g_value_set_string (value, shout2send->description);
341 case ARG_GENRE: /* Genre of the stream */
342 g_value_set_string (value, shout2send->genre);
345 case ARG_PROTOCOL: /* protocol to connect with */
346 g_value_set_enum (value, shout2send->protocol);
349 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
350 g_value_set_string (value, shout2send->mount);
353 case ARG_URL: /* Url of stream (I'm guessing) */
354 g_value_set_string (value, shout2send->url);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
364 static GstPadLinkReturn
365 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
369 if (!strcmp(gst_caps_get_mime (caps), "audio/mpeg"))
371 audio_format = SHOUT_FORMAT_MP3;
372 return GST_PAD_LINK_OK;
375 if (!strcmp(gst_caps_get_mime (caps), "application/ogg"))
377 audio_format = SHOUT_FORMAT_VORBIS;
378 return GST_PAD_LINK_OK;
381 return GST_PAD_LINK_REFUSED;
386 static GstElementStateReturn
387 gst_shout2send_change_state (GstElement *element)
389 GstShout2send *shout2send;
391 guint major, minor, micro;
394 gchar *version_string;
396 g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
398 shout2send = GST_SHOUT2SEND(element);
400 GST_DEBUG ("state pending %d", GST_STATE_PENDING (element));
402 /* if going down into NULL state, close the file if it's open */
403 switch (GST_STATE_TRANSITION (element)) {
404 case GST_STATE_NULL_TO_READY:
405 shout2send->conn = shout_new();
407 switch (shout2send->protocol) {
408 case SHOUT2SEND_PROTOCOL_ICE:
409 proto = SHOUT_PROTOCOL_ICE;
411 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
412 proto = SHOUT_PROTOCOL_XAUDIOCAST;
414 case SHOUT2SEND_PROTOCOL_ICY:
415 proto = SHOUT_PROTOCOL_ICY;
417 case SHOUT2SEND_PROTOCOL_HTTP:
418 proto = SHOUT_PROTOCOL_HTTP;
422 if (shout_set_protocol(shout2send->conn, proto) != SHOUTERR_SUCCESS)
424 g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
427 /* --- FIXME: shout requires an ip, and fails if it is given a host. */
428 /* may want to put convert_to_ip(shout2send->ip) here */
431 if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
433 g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn));
437 if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
439 g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn));
442 if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
444 g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn));
447 if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
449 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
452 if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
454 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
457 if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
459 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
462 if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
464 g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn));
467 if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
469 g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn));
472 gst_version(&major,&minor,µ);
474 version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
476 if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
478 g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn));
481 g_free (version_string);
486 case GST_STATE_READY_TO_PAUSED:
488 /* This sets the format acording to the capabilities of what
489 we are being given as input. */
491 if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
493 g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn));
496 if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
497 g_print ("connected to server...\n");
500 g_warning ("Couldn't connect to server: %s", shout_get_error(shout2send->conn));
501 shout_close (shout2send->conn);
502 shout_free (shout2send->conn);
503 return GST_STATE_FAILURE;
506 case GST_STATE_PAUSED_TO_READY:
507 shout_close (shout2send->conn);
508 shout_free (shout2send->conn);
514 /* if we haven't failed already, give the parent class a chance to ;-) */
515 if (GST_ELEMENT_CLASS (parent_class)->change_state)
516 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
518 return GST_STATE_SUCCESS;
522 plugin_init (GModule *module, GstPlugin *plugin)
524 GstElementFactory *factory;
526 factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
527 g_return_val_if_fail(factory != NULL, FALSE);
529 gst_element_factory_add_pad_template (factory, sink_template_factory ());
531 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
536 GstPluginDesc plugin_desc = {