2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gstshout2.h"
29 #include "gst/gst-i18n-plugin.h"
31 GST_DEBUG_CATEGORY_STATIC (shout2_debug);
32 #define GST_CAT_DEFAULT shout2_debug
34 static const GstElementDetails shout2send_details =
35 GST_ELEMENT_DETAILS ("Icecast network sink",
37 "Sends data to an icecast server",
38 "Wim Taymans <wim.taymans@chello.be>\n"
39 "Pedro Corte-Real <typo@netcabo.pt>\n"
40 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
45 SIGNAL_CONNECTION_PROBLEM, /* 0.11 FIXME: remove this */
52 ARG_IP, /* the ip of the server */
53 ARG_PORT, /* the encoder port number on the server */
54 ARG_PASSWORD, /* the encoder password on the server */
55 ARG_PUBLIC, /* is this stream public? */
56 ARG_STREAMNAME, /* Name of the stream */
57 ARG_DESCRIPTION, /* Description of the stream */
58 ARG_GENRE, /* Genre of the stream */
60 ARG_PROTOCOL, /* Protocol to connect with */
62 ARG_MOUNT, /* mountpoint of stream (icecast only) */
63 ARG_URL /* Url of stream (I'm guessing) */
66 #define DEFAULT_IP "127.0.0.1"
67 #define DEFAULT_PORT 8000
68 #define DEFAULT_PASSWORD "hackme"
69 #define DEFAULT_STREAMNAME ""
70 #define DEFAULT_DESCRIPTION ""
71 #define DEFAULT_GENRE ""
72 #define DEFAULT_MOUNT ""
73 #define DEFAULT_URL ""
74 #define DEFAULT_PROTOCOL SHOUT2SEND_PROTOCOL_HTTP
76 static GstElementClass *parent_class = NULL;
78 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
81 GST_STATIC_CAPS ("application/ogg; "
82 "audio/mpeg, mpegversion = (int) 1, layer = (int) [ 1, 3 ]")
84 static void gst_shout2send_class_init (GstShout2sendClass * klass);
85 static void gst_shout2send_base_init (GstShout2sendClass * klass);
86 static void gst_shout2send_init (GstShout2send * shout2send);
88 static gboolean gst_shout2send_event (GstBaseSink * sink, GstEvent * event);
89 static GstFlowReturn gst_shout2send_render (GstBaseSink * sink,
91 static gboolean gst_shout2send_start (GstBaseSink * basesink);
92 static gboolean gst_shout2send_stop (GstBaseSink * basesink);
94 static void gst_shout2send_set_property (GObject * object, guint prop_id,
95 const GValue * value, GParamSpec * pspec);
96 static void gst_shout2send_get_property (GObject * object, guint prop_id,
97 GValue * value, GParamSpec * pspec);
99 static gboolean gst_shout2send_setcaps (GstPad * pad, GstCaps * caps);
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_XAUDIOCAST,
110 "Xaudiocast Protocol (icecast 1.3.x)", "xaudiocast"},
111 {SHOUT2SEND_PROTOCOL_ICY, "Icy Protocol (ShoutCast)", "icy"},
112 {SHOUT2SEND_PROTOCOL_HTTP, "Http Protocol (icecast 2.x)", "http"},
116 if (!shout2send_protocol_type) {
117 shout2send_protocol_type =
118 g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
122 return shout2send_protocol_type;
126 gst_shout2send_get_type (void)
128 static GType shout2send_type = 0;
130 if (!shout2send_type) {
131 static const GTypeInfo shout2send_info = {
132 sizeof (GstShout2sendClass),
133 (GBaseInitFunc) gst_shout2send_base_init,
135 (GClassInitFunc) gst_shout2send_class_init,
138 sizeof (GstShout2send),
140 (GInstanceInitFunc) gst_shout2send_init,
143 static const GInterfaceInfo tag_setter_info = {
150 g_type_register_static (GST_TYPE_BASE_SINK, "GstShout2send",
151 &shout2send_info, 0);
153 g_type_add_interface_static (shout2send_type, GST_TYPE_TAG_SETTER,
157 return shout2send_type;
161 gst_shout2send_base_init (GstShout2sendClass * klass)
163 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
165 gst_element_class_add_pad_template (element_class,
166 gst_static_pad_template_get (&sink_template));
167 gst_element_class_set_details (element_class, &shout2send_details);
169 GST_DEBUG_CATEGORY_INIT (shout2_debug, "shout2", 0, "shout2send element");
173 gst_shout2send_class_init (GstShout2sendClass * klass)
175 GObjectClass *gobject_class;
176 GstBaseSinkClass *gstbasesink_class;
178 gobject_class = (GObjectClass *) klass;
179 gstbasesink_class = (GstBaseSinkClass *) klass;
181 parent_class = g_type_class_peek_parent (klass);
183 gobject_class->set_property = gst_shout2send_set_property;
184 gobject_class->get_property = gst_shout2send_get_property;
186 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_IP,
187 g_param_spec_string ("ip", "ip", "ip", DEFAULT_IP, G_PARAM_READWRITE));
188 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
189 g_param_spec_int ("port", "port", "port", 1, G_MAXUSHORT, DEFAULT_PORT,
192 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PASSWORD,
193 g_param_spec_string ("password", "password", "password", DEFAULT_PASSWORD,
197 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMNAME,
198 g_param_spec_string ("streamname", "streamname", "name of the stream",
199 DEFAULT_STREAMNAME, G_PARAM_READWRITE));
201 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESCRIPTION,
202 g_param_spec_string ("description", "description", "description",
203 DEFAULT_DESCRIPTION, G_PARAM_READWRITE));
205 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GENRE,
206 g_param_spec_string ("genre", "genre", "genre", DEFAULT_GENRE,
209 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
210 g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
211 GST_TYPE_SHOUT_PROTOCOL, DEFAULT_PROTOCOL, G_PARAM_READWRITE));
215 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MOUNT,
216 g_param_spec_string ("mount", "mount", "mount", DEFAULT_MOUNT,
219 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_URL,
220 g_param_spec_string ("url", "url", "url", DEFAULT_URL,
224 gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM] =
225 g_signal_new ("connection-problem", G_TYPE_FROM_CLASS (klass),
226 G_SIGNAL_RUN_CLEANUP, G_STRUCT_OFFSET (GstShout2sendClass,
227 connection_problem), NULL, NULL, g_cclosure_marshal_VOID__INT,
228 G_TYPE_NONE, 1, G_TYPE_INT);
230 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_shout2send_start);
231 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_shout2send_stop);
232 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_shout2send_render);
233 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_shout2send_event);
237 gst_shout2send_init (GstShout2send * shout2send)
239 gst_base_sink_set_sync (GST_BASE_SINK (shout2send), FALSE);
241 gst_pad_set_setcaps_function (GST_BASE_SINK_PAD (shout2send),
242 GST_DEBUG_FUNCPTR (gst_shout2send_setcaps));
244 shout2send->ip = g_strdup (DEFAULT_IP);
245 shout2send->port = DEFAULT_PORT;
246 shout2send->password = g_strdup (DEFAULT_PASSWORD);
247 shout2send->streamname = g_strdup (DEFAULT_STREAMNAME);
248 shout2send->description = g_strdup (DEFAULT_DESCRIPTION);
249 shout2send->genre = g_strdup (DEFAULT_GENRE);
250 shout2send->mount = g_strdup (DEFAULT_MOUNT);
251 shout2send->url = g_strdup (DEFAULT_URL);
252 shout2send->protocol = DEFAULT_PROTOCOL;
254 shout2send->tags = gst_tag_list_new ();
255 shout2send->conn = NULL;
256 shout2send->audio_format = SHOUT_FORMAT_VORBIS;
257 shout2send->connected = FALSE;
258 shout2send->songmetadata = NULL;
262 set_shout_metadata (const GstTagList * list, const gchar * tag,
265 char **shout_metadata = (char **) user_data;
268 GST_DEBUG ("tag: %s being added", tag);
269 if (strcmp (tag, GST_TAG_ARTIST) == 0) {
270 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
271 if (!gst_tag_list_get_string (list, tag, &value)) {
272 GST_DEBUG ("Error reading \"%s\" tag value", tag);
275 /* shout_metadata should be NULL if title is after artist in list */
276 if (*shout_metadata == NULL) {
277 *shout_metadata = g_strdup (value);
279 temp = g_strdup_printf ("%s - %s", value, *shout_metadata);
280 g_free (*shout_metadata);
281 *shout_metadata = temp;
285 } else if (strcmp (tag, GST_TAG_TITLE) == 0) {
286 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
287 if (!gst_tag_list_get_string (list, tag, &value)) {
288 GST_DEBUG ("Error reading \"%s\" tag value", tag);
291 /* shout_metadata should be NULL if title is before artist in list */
292 if (*shout_metadata == NULL) {
293 *shout_metadata = g_strdup (value);
295 temp = g_strdup_printf ("%s - %s", *shout_metadata, value);
296 g_free (*shout_metadata);
297 *shout_metadata = temp;
301 GST_LOG ("shout metadata is now: %s", *shout_metadata);
306 gst_shout2send_set_metadata (GstShout2send * shout2send)
308 const GstTagList *user_tags;
311 shout_metadata_t *pmetadata;
313 g_return_if_fail (shout2send != NULL);
314 user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (shout2send));
315 if ((shout2send->tags == NULL) && (user_tags == NULL)) {
318 copy = gst_tag_list_merge (user_tags, shout2send->tags,
319 gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
320 /* lets get the artist and song tags */
322 gst_tag_list_foreach ((GstTagList *) copy, set_shout_metadata,
323 (gpointer) & tempmetadata);
325 pmetadata = shout_metadata_new ();
326 shout_metadata_add (pmetadata, "song", tempmetadata);
327 shout_set_metadata (shout2send->conn, pmetadata);
328 shout_metadata_free (pmetadata);
331 gst_tag_list_free (copy);
337 gst_shout2send_event (GstBaseSink * sink, GstEvent * event)
339 GstShout2send *shout2send;
342 shout2send = GST_SHOUT2SEND (sink);
344 GST_LOG_OBJECT (shout2send, "got %s event", GST_EVENT_TYPE_NAME (event));
346 switch (GST_EVENT_TYPE (event)) {
348 /* vorbis audio doesnt need metadata setting on the icecast level, only mp3 */
349 if (shout2send->tags && shout2send->audio_format == SHOUT_FORMAT_MP3) {
352 gst_event_parse_tag (event, &list);
353 GST_DEBUG_OBJECT (shout2send, "tags=%" GST_PTR_FORMAT, list);
354 gst_tag_list_insert (shout2send->tags,
356 gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
357 /* lets get the artist and song tags */
358 gst_tag_list_foreach ((GstTagList *) shout2send->tags,
359 set_shout_metadata, &shout2send->songmetadata);
360 if (shout2send->songmetadata && shout2send->connected) {
361 shout_metadata_t *pmetadata;
363 GST_DEBUG_OBJECT (shout2send, "metadata now: %s",
364 shout2send->songmetadata);
366 pmetadata = shout_metadata_new ();
367 shout_metadata_add (pmetadata, "song", shout2send->songmetadata);
368 shout_set_metadata (shout2send->conn, pmetadata);
369 shout_metadata_free (pmetadata);
375 GST_LOG_OBJECT (shout2send, "let base class handle event");
376 if (GST_BASE_SINK_CLASS (parent_class)->event) {
377 event = gst_event_ref (event);
378 ret = GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
388 gst_shout2send_start (GstBaseSink * basesink)
390 GstShout2send *sink = GST_SHOUT2SEND (basesink);
391 const gchar *cur_prop;
393 gchar *version_string;
395 GST_DEBUG_OBJECT (sink, "starting");
397 sink->conn = shout_new ();
399 switch (sink->protocol) {
400 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
401 proto = SHOUT_PROTOCOL_XAUDIOCAST;
403 case SHOUT2SEND_PROTOCOL_ICY:
404 proto = SHOUT_PROTOCOL_ICY;
406 case SHOUT2SEND_PROTOCOL_HTTP:
407 proto = SHOUT_PROTOCOL_HTTP;
411 cur_prop = "protocol";
412 GST_DEBUG_OBJECT (sink, "setting protocol: %d", sink->protocol);
413 if (shout_set_protocol (sink->conn, proto) != SHOUTERR_SUCCESS)
416 /* --- FIXME: shout requires an ip, and fails if it is given a host. */
417 /* may want to put convert_to_ip(shout2send->ip) here */
419 GST_DEBUG_OBJECT (sink, "setting ip: %s", sink->ip);
420 if (shout_set_host (sink->conn, sink->ip) != SHOUTERR_SUCCESS)
424 GST_DEBUG_OBJECT (sink, "setting port: %u", sink->port);
425 if (shout_set_port (sink->conn, sink->port) != SHOUTERR_SUCCESS)
428 cur_prop = "password";
429 GST_DEBUG_OBJECT (sink, "setting password: %s", sink->password);
430 if (shout_set_password (sink->conn, sink->password) != SHOUTERR_SUCCESS)
433 cur_prop = "streamname";
434 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->streamname);
435 if (shout_set_name (sink->conn, sink->streamname) != SHOUTERR_SUCCESS)
438 cur_prop = "description";
439 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->description);
440 if (shout_set_description (sink->conn, sink->description) != SHOUTERR_SUCCESS)
444 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->genre);
445 if (shout_set_genre (sink->conn, sink->genre) != SHOUTERR_SUCCESS)
449 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->mount);
450 if (shout_set_mount (sink->conn, sink->mount) != SHOUTERR_SUCCESS)
454 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, "source");
455 if (shout_set_user (sink->conn, "source") != SHOUTERR_SUCCESS)
458 version_string = gst_version_string ();
460 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, version_string);
461 if (shout_set_agent (sink->conn, version_string) != SHOUTERR_SUCCESS) {
462 g_free (version_string);
466 g_free (version_string);
472 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
473 ("Error setting %s: %s", cur_prop, shout_get_error (sink->conn)));
479 gst_shout2send_connect (GstShout2send * sink)
481 GST_DEBUG_OBJECT (sink, "Connection format is: %s",
482 (sink->audio_format == SHOUT_FORMAT_VORBIS) ? "vorbis" :
483 ((sink->audio_format == SHOUT_FORMAT_MP3) ? "mp3" : "unknown"));
485 if (shout_set_format (sink->conn, sink->audio_format) != SHOUTERR_SUCCESS)
486 goto could_not_set_format;
488 if (shout_open (sink->conn) != SHOUTERR_SUCCESS)
489 goto could_not_connect;
491 GST_DEBUG_OBJECT (sink, "connected to server");
492 sink->connected = TRUE;
494 /* let's set metadata */
495 if (sink->songmetadata) {
496 shout_metadata_t *pmetadata;
498 GST_DEBUG_OBJECT (sink, "shout metadata now: %s", sink->songmetadata);
499 pmetadata = shout_metadata_new ();
500 shout_metadata_add (pmetadata, "song", sink->songmetadata);
501 shout_set_metadata (sink->conn, pmetadata);
502 shout_metadata_free (pmetadata);
508 could_not_set_format:
510 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
511 ("Error setting connection format: %s", shout_get_error (sink->conn)));
517 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
518 (_("Could not connect to server")),
519 ("shout_open() failed: err=%s", shout_get_error (sink->conn)));
520 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
521 shout_get_errno (sink->conn));
527 gst_shout2send_stop (GstBaseSink * basesink)
529 GstShout2send *sink = GST_SHOUT2SEND (basesink);
533 shout_close (sink->conn);
534 shout_free (sink->conn);
538 if (sink->songmetadata) {
539 g_free (sink->songmetadata);
540 sink->songmetadata = NULL;
543 sink->connected = FALSE;
549 gst_shout2send_render (GstBaseSink * basesink, GstBuffer * buf)
554 sink = GST_SHOUT2SEND (basesink);
556 /* presumably we connect here because we need to know the format before
557 * we can set up the connection, which we don't know yet in _start() */
558 if (!sink->connected) {
559 if (!gst_shout2send_connect (sink))
560 return GST_FLOW_ERROR;
563 /* FIXME: do we want to do syncing here at all? (tpm) */
564 /* GST_LOG_OBJECT (sink, "using libshout to sync"); */
565 shout_sync (sink->conn);
567 GST_LOG_OBJECT (sink, "sending %u bytes of data", GST_BUFFER_SIZE (buf));
568 ret = shout_send (sink->conn, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
569 if (ret != SHOUTERR_SUCCESS)
577 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
578 ("shout_send() failed: %s", shout_get_error (sink->conn)));
579 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
580 shout_get_errno (sink->conn));
581 return GST_FLOW_ERROR;
586 gst_shout2send_set_property (GObject * object, guint prop_id,
587 const GValue * value, GParamSpec * pspec)
589 GstShout2send *shout2send;
591 shout2send = GST_SHOUT2SEND (object);
596 g_free (shout2send->ip);
597 shout2send->ip = g_strdup (g_value_get_string (value));
600 shout2send->port = g_value_get_int (value);
603 if (shout2send->password)
604 g_free (shout2send->password);
605 shout2send->password = g_strdup (g_value_get_string (value));
607 case ARG_STREAMNAME: /* Name of the stream */
608 if (shout2send->streamname)
609 g_free (shout2send->streamname);
610 shout2send->streamname = g_strdup (g_value_get_string (value));
612 case ARG_DESCRIPTION: /* Description of the stream */
613 if (shout2send->description)
614 g_free (shout2send->description);
615 shout2send->description = g_strdup (g_value_get_string (value));
617 case ARG_GENRE: /* Genre of the stream */
618 if (shout2send->genre)
619 g_free (shout2send->genre);
620 shout2send->genre = g_strdup (g_value_get_string (value));
622 case ARG_PROTOCOL: /* protocol to connect with */
623 shout2send->protocol = g_value_get_enum (value);
625 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
626 if (shout2send->mount)
627 g_free (shout2send->mount);
628 shout2send->mount = g_strdup (g_value_get_string (value));
630 case ARG_URL: /* Url of the stream (I'm guessing) */
632 g_free (shout2send->url);
633 shout2send->url = g_strdup (g_value_get_string (value));
636 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
642 gst_shout2send_get_property (GObject * object, guint prop_id,
643 GValue * value, GParamSpec * pspec)
645 GstShout2send *shout2send;
647 shout2send = GST_SHOUT2SEND (object);
651 g_value_set_string (value, shout2send->ip);
654 g_value_set_int (value, shout2send->port);
657 g_value_set_string (value, shout2send->password);
659 case ARG_STREAMNAME: /* Name of the stream */
660 g_value_set_string (value, shout2send->streamname);
662 case ARG_DESCRIPTION: /* Description of the stream */
663 g_value_set_string (value, shout2send->description);
665 case ARG_GENRE: /* Genre of the stream */
666 g_value_set_string (value, shout2send->genre);
668 case ARG_PROTOCOL: /* protocol to connect with */
669 g_value_set_enum (value, shout2send->protocol);
671 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
672 g_value_set_string (value, shout2send->mount);
674 case ARG_URL: /* Url of stream (I'm guessing) */
675 g_value_set_string (value, shout2send->url);
678 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
684 gst_shout2send_setcaps (GstPad * pad, GstCaps * caps)
686 const gchar *mimetype;
687 GstShout2send *shout2send;
690 shout2send = GST_SHOUT2SEND (GST_OBJECT_PARENT (pad));
692 mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
694 GST_DEBUG_OBJECT (shout2send, "mimetype of caps given is: %s", mimetype);
696 if (!strcmp (mimetype, "audio/mpeg")) {
697 shout2send->audio_format = SHOUT_FORMAT_MP3;
698 } else if (!strcmp (mimetype, "application/ogg")) {
699 shout2send->audio_format = SHOUT_FORMAT_VORBIS;
708 plugin_init (GstPlugin * plugin)
711 setlocale (LC_ALL, "");
712 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
713 #endif /* ENABLE_NLS */
715 return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
716 GST_TYPE_SHOUT2SEND);
719 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
722 "Sends data to an icecast server using libshout2",
724 VERSION, "LGPL", "libshout2", "http://www.icecast.org/download.html")