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 = {
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_MOUNT, /* mountpoint of stream (icecast only) */
53 ARG_URL, /* Url of stream (I'm guessing) */
56 static GstPadTemplate*
57 sink_template_factory (void)
59 static GstPadTemplate *template = NULL;
62 template = gst_pad_template_new (
80 static void gst_shout2send_class_init (GstShout2sendClass *klass);
81 static void gst_shout2send_init (GstShout2send *shout2send);
83 static void gst_shout2send_chain (GstPad *pad, GstBuffer *buf);
84 static GstPadConnectReturn gst_shout2send_connect (GstPad *pad, GstCaps *caps);
86 static void gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
87 static void gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
89 static GstElementStateReturn gst_shout2send_change_state (GstElement *element);
91 static GstElementClass *parent_class = NULL;
92 /*static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 }; */
95 gst_shout2send_get_type(void)
97 static GType shout2send_type = 0;
99 if (!shout2send_type) {
100 static const GTypeInfo shout2send_info = {
101 sizeof(GstShout2sendClass), NULL,
103 (GClassInitFunc)gst_shout2send_class_init,
106 sizeof(GstShout2send),
108 (GInstanceInitFunc)gst_shout2send_init,
110 shout2send_type = g_type_register_static(GST_TYPE_ELEMENT, "GstShout2send", &shout2send_info, 0);
112 return shout2send_type;
116 gst_shout2send_class_init (GstShout2sendClass *klass)
118 GObjectClass *gobject_class;
119 GstElementClass *gstelement_class;
121 gobject_class = (GObjectClass*)klass;
122 gstelement_class = (GstElementClass*)klass;
124 parent_class = g_type_class_ref(GST_TYPE_ELEMENT);
126 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_IP,
127 g_param_spec_string("ip","ip","ip",
128 NULL, G_PARAM_READWRITE)); /* CHECKME */
129 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PORT,
130 g_param_spec_int("port","port","port",
131 1,G_MAXUSHORT,8000,G_PARAM_READWRITE)); /* CHECKME */
133 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_PASSWORD,
134 g_param_spec_string("password","password","password",
135 NULL, G_PARAM_READWRITE)); /* CHECKME */
138 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_NAME,
139 g_param_spec_string("name","name","name",
140 NULL, G_PARAM_READWRITE)); /* CHECKME */
142 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_DESCRIPTION,
143 g_param_spec_string("description","description","description",
144 NULL, G_PARAM_READWRITE)); /* CHECKME */
146 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_GENRE,
147 g_param_spec_string("genre","genre","genre",
148 NULL, G_PARAM_READWRITE)); /* CHECKME */
151 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_MOUNT,
152 g_param_spec_string("mount","mount","mount",
153 NULL, G_PARAM_READWRITE)); /* CHECKME */
155 g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_URL,
156 g_param_spec_string("url","url","url",
157 NULL, G_PARAM_READWRITE)); /* CHECKME */
161 gobject_class->set_property = gst_shout2send_set_property;
162 gobject_class->get_property = gst_shout2send_get_property;
164 gstelement_class->change_state = gst_shout2send_change_state;
168 gst_shout2send_init (GstShout2send *shout2send)
170 shout2send->sinkpad = gst_pad_new_from_template (sink_template_factory (), "sink");
171 gst_element_add_pad(GST_ELEMENT(shout2send),shout2send->sinkpad);
172 gst_pad_set_chain_function(shout2send->sinkpad,gst_shout2send_chain);
174 gst_pad_set_connect_function (shout2send->sinkpad, gst_shout2send_connect);
176 shout2send->ip = g_strdup ("127.0.0.1");
177 shout2send->port = 8000;
178 shout2send->password = g_strdup ("hackme");
179 shout2send->name = g_strdup ("");
180 shout2send->description = g_strdup ("");
181 shout2send->genre = g_strdup ("");
182 shout2send->mount = g_strdup ("");
183 shout2send->url = g_strdup ("");
187 gst_shout2send_chain (GstPad *pad, GstBuffer *buf)
189 GstShout2send *shout2send;
192 g_return_if_fail(pad != NULL);
193 g_return_if_fail(GST_IS_PAD(pad));
194 g_return_if_fail(buf != NULL);
196 shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
198 g_return_if_fail (shout2send != NULL);
199 g_return_if_fail (GST_IS_SHOUT2SEND (shout2send));
201 ret = shout_send (shout2send->conn, GST_BUFFER_DATA (buf),
202 GST_BUFFER_SIZE (buf));
203 if (ret != SHOUTERR_SUCCESS) {
204 g_warning ("send error: %s...\n", shout_get_error(shout2send->conn));
207 shout_sync (shout2send->conn);
209 gst_buffer_unref (buf);
213 gst_shout2send_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
215 GstShout2send *shout2send;
217 /* it's not null if we got it, but it might not be ours */
218 g_return_if_fail(GST_IS_SHOUT2SEND(object));
219 shout2send = GST_SHOUT2SEND(object);
225 g_free (shout2send->ip);
226 shout2send->ip = g_strdup (g_value_get_string (value));
230 shout2send->port = g_value_get_int (value);
234 if (shout2send->password)
235 g_free (shout2send->password);
236 shout2send->password = g_strdup (g_value_get_string (value));
239 case ARG_NAME: /* Name of the stream */
240 if (shout2send->name)
241 g_free (shout2send->name);
242 shout2send->name = g_strdup (g_value_get_string (value));
245 case ARG_DESCRIPTION: /* Description of the stream */
246 if (shout2send->description)
247 g_free (shout2send->description);
248 shout2send->description = g_strdup (g_value_get_string (value));
251 case ARG_GENRE: /* Genre of the stream */
252 if (shout2send->genre)
253 g_free (shout2send->genre);
254 shout2send->genre = g_strdup (g_value_get_string (value));
257 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
258 if (shout2send->mount)
259 g_free (shout2send->mount);
260 shout2send->mount = g_strdup (g_value_get_string (value));
263 case ARG_URL: /* Url of the stream (I'm guessing) */
265 g_free (shout2send->url);
266 shout2send->url = g_strdup (g_value_get_string (value));
275 gst_shout2send_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
277 GstShout2send *shout2send;
279 /* it's not null if we got it, but it might not be ours */
280 g_return_if_fail(GST_IS_SHOUT2SEND(object));
281 shout2send = GST_SHOUT2SEND(object);
286 g_value_set_string (value, shout2send->ip);
289 g_value_set_int (value, shout2send->port);
292 g_value_set_string (value, shout2send->password);
295 case ARG_NAME: /* Name of the stream */
296 g_value_set_string (value, shout2send->name);
299 case ARG_DESCRIPTION: /* Description of the stream */
300 g_value_set_string (value, shout2send->description);
303 case ARG_GENRE: /* Genre of the stream */
304 g_value_set_string (value, shout2send->genre);
307 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
308 g_value_set_string (value, shout2send->mount);
311 case ARG_URL: /* Url of stream (I'm guessing) */
312 g_value_set_string (value, shout2send->url);
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322 static GstPadConnectReturn
323 gst_shout2send_connect (GstPad *pad, GstCaps *caps)
327 if (!strcmp(gst_caps_get_mime (caps), "audio/x-mp3"))
329 audio_format = SHOUT_FORMAT_MP3;
330 return GST_PAD_CONNECT_OK;
333 if (!strcmp(gst_caps_get_mime (caps), "application/x-ogg"))
335 audio_format = SHOUT_FORMAT_VORBIS;
336 return GST_PAD_CONNECT_OK;
339 return GST_PAD_CONNECT_REFUSED;
344 static GstElementStateReturn
345 gst_shout2send_change_state (GstElement *element)
347 GstShout2send *shout2send;
349 guint major, minor, micro;
351 gchar *version_string;
353 g_return_val_if_fail (GST_IS_SHOUT2SEND (element), GST_STATE_FAILURE);
355 shout2send = GST_SHOUT2SEND(element);
357 GST_DEBUG (0,"state pending %d", GST_STATE_PENDING (element));
359 /* if going down into NULL state, close the file if it's open */
360 switch (GST_STATE_TRANSITION (element)) {
361 case GST_STATE_NULL_TO_READY:
362 shout2send->conn = shout_new();
364 /* There are other choices for protocols (_ICE, _XAUDIOCAST and _ICY)
365 adding a property to set this might be in order */
367 if (shout_set_protocol(shout2send->conn, SHOUT_PROTOCOL_HTTP) != SHOUTERR_SUCCESS)
369 g_error ("Error setting protocol: %s\n", shout_get_error(shout2send->conn));
372 /* --- FIXME: shout requires an ip, and fails if it is given a host. */
373 /* may want to put convert_to_ip(shout2send->ip) here */
376 if (shout_set_host(shout2send->conn, shout2send->ip) != SHOUTERR_SUCCESS)
378 g_error ("Error setting host: %s\n", shout_get_error(shout2send->conn));
382 if (shout_set_port(shout2send->conn, shout2send->port) != SHOUTERR_SUCCESS)
384 g_error ("Error setting port: %s\n", shout_get_error(shout2send->conn));
387 if(shout_set_password(shout2send->conn, shout2send->password) != SHOUTERR_SUCCESS)
389 g_error ("Error setting password: %s\n", shout_get_error(shout2send->conn));
392 if (shout_set_name(shout2send->conn, shout2send->name) != SHOUTERR_SUCCESS)
394 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
397 if (shout_set_description(shout2send->conn, shout2send->description) != SHOUTERR_SUCCESS)
399 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
402 if (shout_set_genre(shout2send->conn, shout2send->genre) != SHOUTERR_SUCCESS)
404 g_error ("Error setting name: %s\n", shout_get_error(shout2send->conn));
407 if (shout_set_mount(shout2send->conn, shout2send->mount) != SHOUTERR_SUCCESS)
409 g_error ("Error setting mount point: %s\n", shout_get_error(shout2send->conn));
412 if (shout_set_user(shout2send->conn, "source") != SHOUTERR_SUCCESS)
414 g_error ("Error setting user: %s\n", shout_get_error(shout2send->conn));
417 gst_version(&major,&minor,µ);
419 version_string = g_strdup_printf("GStreamer %d.%d.%d", major,minor,micro);
421 if (shout_set_agent(shout2send->conn, version_string) != SHOUTERR_SUCCESS)
423 g_error ("Error setting agent: %s\n", shout_get_error(shout2send->conn));
426 g_free (version_string);
431 case GST_STATE_READY_TO_PAUSED:
433 /* This sets the format acording to the capabilities of what
434 we are being given as input. */
436 if (shout_set_format(shout2send->conn, audio_format) != SHOUTERR_SUCCESS)
438 g_error ("Error setting connection format: %s\n", shout_get_error(shout2send->conn));
441 if (shout_open (shout2send->conn) == SHOUTERR_SUCCESS) {
442 g_print ("connected to server...\n");
445 /* changed from g_warning, and included result code lookup. */
446 g_error ("couldn't connect to server...");
447 shout_close (shout2send->conn);
448 shout_free (shout2send->conn);
449 return GST_STATE_FAILURE;
452 case GST_STATE_READY_TO_NULL:
453 shout_close (shout2send->conn);
454 shout_free (shout2send->conn);
460 /* if we haven't failed already, give the parent class a chance to ;-) */
461 if (GST_ELEMENT_CLASS (parent_class)->change_state)
462 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
464 return GST_STATE_SUCCESS;
468 plugin_init (GModule *module, GstPlugin *plugin)
470 GstElementFactory *factory;
472 factory = gst_element_factory_new("shout2send", GST_TYPE_SHOUT2SEND, &shout2send_details);
473 g_return_val_if_fail(factory != NULL, FALSE);
475 gst_element_factory_add_pad_template (factory, sink_template_factory ());
477 gst_plugin_add_feature (plugin, GST_PLUGIN_FEATURE (factory));
482 GstPluginDesc plugin_desc = {