2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
4 * Copyright (C) <2012> Ralph Giles <giles@mozilla.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-shout2send
25 * shout2send pushes a media stream to an Icecast server
28 * <title>Example launch line</title>
30 * gst-launch-1.0 uridecodebin uri=file:///path/to/audiofile ! audioconvert ! vorbisenc ! oggmux ! shout2send mount=/stream.ogg port=8000 username=source password=somepassword ip=server_IP_address_or_hostname
31 * ]| This pipeline demuxes, decodes, re-encodes and re-muxes an audio
32 * media file into oggvorbis and sends the resulting stream to an Icecast
33 * server. Properties mount, port, username and password are all server-config
42 #include "gstshout2.h"
46 #include "gst/gst-i18n-plugin.h"
48 GST_DEBUG_CATEGORY_STATIC (shout2_debug);
49 #define GST_CAT_DEFAULT shout2_debug
54 SIGNAL_CONNECTION_PROBLEM, /* FIXME 2.0: remove this */
61 ARG_IP, /* the IP address or hostname of the server */
62 ARG_PORT, /* the encoder port number on the server */
63 ARG_PASSWORD, /* the encoder password on the server */
64 ARG_USERNAME, /* the encoder username on the server */
65 ARG_PUBLIC, /* is this stream public? */
66 ARG_STREAMNAME, /* Name of the stream */
67 ARG_DESCRIPTION, /* Description of the stream */
68 ARG_GENRE, /* Genre of the stream */
70 ARG_PROTOCOL, /* Protocol to connect with */
72 ARG_MOUNT, /* mountpoint of stream (icecast only) */
73 ARG_URL, /* the stream's homepage URL */
75 ARG_TIMEOUT /* The max amount of time to wait for
79 #define DEFAULT_IP "127.0.0.1"
80 #define DEFAULT_PORT 8000
81 #define DEFAULT_PASSWORD "hackme"
82 #define DEFAULT_USERNAME "source"
83 #define DEFAULT_PUBLIC FALSE
84 #define DEFAULT_STREAMNAME ""
85 #define DEFAULT_DESCRIPTION ""
86 #define DEFAULT_GENRE ""
87 #define DEFAULT_MOUNT ""
88 #define DEFAULT_URL ""
89 #define DEFAULT_PROTOCOL SHOUT2SEND_PROTOCOL_HTTP
90 #define DEFAULT_TIMEOUT 10000
92 #ifdef SHOUT_FORMAT_WEBM
93 #define WEBM_CAPS "; video/webm; audio/webm"
97 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
100 GST_STATIC_CAPS ("application/ogg; audio/ogg; video/ogg; "
101 "audio/mpeg, mpegversion = (int) 1, layer = (int) [ 1, 3 ]" WEBM_CAPS));
103 static void gst_shout2send_finalize (GstShout2send * shout2send);
105 static gboolean gst_shout2send_event (GstBaseSink * sink, GstEvent * event);
106 static gboolean gst_shout2send_unlock (GstBaseSink * basesink);
107 static gboolean gst_shout2send_unlock_stop (GstBaseSink * basesink);
108 static GstFlowReturn gst_shout2send_render (GstBaseSink * sink,
110 static gboolean gst_shout2send_start (GstBaseSink * basesink);
111 static gboolean gst_shout2send_stop (GstBaseSink * basesink);
113 static void gst_shout2send_set_property (GObject * object, guint prop_id,
114 const GValue * value, GParamSpec * pspec);
115 static void gst_shout2send_get_property (GObject * object, guint prop_id,
116 GValue * value, GParamSpec * pspec);
118 static gboolean gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps);
120 static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 };
122 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
124 gst_shout2send_protocol_get_type (void)
126 static GType shout2send_protocol_type = 0;
127 static const GEnumValue shout2send_protocol[] = {
128 {SHOUT2SEND_PROTOCOL_XAUDIOCAST,
129 "Xaudiocast Protocol (icecast 1.3.x)", "xaudiocast"},
130 {SHOUT2SEND_PROTOCOL_ICY, "Icy Protocol (ShoutCast)", "icy"},
131 {SHOUT2SEND_PROTOCOL_HTTP, "Http Protocol (icecast 2.x)", "http"},
135 if (!shout2send_protocol_type) {
136 shout2send_protocol_type =
137 g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
141 return shout2send_protocol_type;
144 #define gst_shout2send_parent_class parent_class
145 G_DEFINE_TYPE_WITH_CODE (GstShout2send, gst_shout2send, GST_TYPE_BASE_SINK,
146 G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
149 gst_shout2send_class_init (GstShout2sendClass * klass)
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
153 GstBaseSinkClass *gstbasesink_class;
155 gobject_class = (GObjectClass *) klass;
156 gstelement_class = (GstElementClass *) klass;
157 gstbasesink_class = (GstBaseSinkClass *) klass;
159 parent_class = g_type_class_peek_parent (klass);
161 gobject_class->set_property = gst_shout2send_set_property;
162 gobject_class->get_property = gst_shout2send_get_property;
163 gobject_class->finalize = (GObjectFinalizeFunc) gst_shout2send_finalize;
165 /* FIXME: 2.0 Should probably change this prop name to "server" */
166 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_IP,
167 g_param_spec_string ("ip", "ip", "IP address or hostname", DEFAULT_IP,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PORT,
170 g_param_spec_int ("port", "port", "port", 1, G_MAXUSHORT, DEFAULT_PORT,
171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PASSWORD,
174 g_param_spec_string ("password", "password", "password", DEFAULT_PASSWORD,
175 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_USERNAME,
178 g_param_spec_string ("username", "username", "username", DEFAULT_USERNAME,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PUBLIC,
183 g_param_spec_boolean ("public", "public",
184 "If the stream should be listed on the server's stream directory",
185 DEFAULT_PUBLIC, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STREAMNAME,
188 g_param_spec_string ("streamname", "streamname", "name of the stream",
189 DEFAULT_STREAMNAME, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DESCRIPTION,
192 g_param_spec_string ("description", "description", "description",
193 DEFAULT_DESCRIPTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_GENRE,
196 g_param_spec_string ("genre", "genre", "genre", DEFAULT_GENRE,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
199 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PROTOCOL,
200 g_param_spec_enum ("protocol", "protocol", "Connection Protocol to use",
201 GST_TYPE_SHOUT_PROTOCOL, DEFAULT_PROTOCOL,
202 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
206 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_MOUNT,
207 g_param_spec_string ("mount", "mount", "mount", DEFAULT_MOUNT,
208 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_URL,
211 g_param_spec_string ("url", "url", "the stream's homepage URL",
212 DEFAULT_URL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
214 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TIMEOUT,
215 g_param_spec_uint ("timeout", "timeout",
216 "Max amount of time to wait for network activity, in milliseconds",
217 1, G_MAXUINT, DEFAULT_TIMEOUT,
218 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
221 gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM] =
222 g_signal_new ("connection-problem", G_TYPE_FROM_CLASS (klass),
223 G_SIGNAL_RUN_CLEANUP, G_STRUCT_OFFSET (GstShout2sendClass,
224 connection_problem), NULL, NULL, g_cclosure_marshal_VOID__INT,
225 G_TYPE_NONE, 1, G_TYPE_INT);
227 gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_shout2send_start);
228 gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_shout2send_stop);
229 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_shout2send_unlock);
230 gstbasesink_class->unlock_stop =
231 GST_DEBUG_FUNCPTR (gst_shout2send_unlock_stop);
232 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_shout2send_render);
233 gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_shout2send_event);
234 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_shout2send_setcaps);
236 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
238 gst_element_class_set_static_metadata (gstelement_class,
239 "Icecast network sink",
240 "Sink/Network", "Sends data to an icecast server",
241 "Wim Taymans <wim.taymans@chello.be>, "
242 "Pedro Corte-Real <typo@netcabo.pt>, "
243 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
245 GST_DEBUG_CATEGORY_INIT (shout2_debug, "shout2", 0, "shout2send element");
249 gst_shout2send_init (GstShout2send * shout2send)
251 gst_base_sink_set_sync (GST_BASE_SINK (shout2send), FALSE);
253 shout2send->timer = gst_poll_new (TRUE);
255 shout2send->ip = g_strdup (DEFAULT_IP);
256 shout2send->port = DEFAULT_PORT;
257 shout2send->password = g_strdup (DEFAULT_PASSWORD);
258 shout2send->username = g_strdup (DEFAULT_USERNAME);
259 shout2send->streamname = g_strdup (DEFAULT_STREAMNAME);
260 shout2send->description = g_strdup (DEFAULT_DESCRIPTION);
261 shout2send->genre = g_strdup (DEFAULT_GENRE);
262 shout2send->mount = g_strdup (DEFAULT_MOUNT);
263 shout2send->url = g_strdup (DEFAULT_URL);
264 shout2send->protocol = DEFAULT_PROTOCOL;
265 shout2send->ispublic = DEFAULT_PUBLIC;
266 shout2send->timeout = DEFAULT_TIMEOUT;
268 shout2send->format = -1;
269 shout2send->tags = gst_tag_list_new_empty ();
270 shout2send->conn = NULL;
271 shout2send->connected = FALSE;
272 shout2send->songmetadata = NULL;
273 shout2send->songartist = NULL;
274 shout2send->songtitle = NULL;
278 gst_shout2send_finalize (GstShout2send * shout2send)
280 g_free (shout2send->ip);
281 g_free (shout2send->password);
282 g_free (shout2send->username);
283 g_free (shout2send->streamname);
284 g_free (shout2send->description);
285 g_free (shout2send->genre);
286 g_free (shout2send->mount);
287 g_free (shout2send->url);
289 gst_tag_list_unref (shout2send->tags);
291 gst_poll_free (shout2send->timer);
293 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (shout2send));
297 set_shout_metadata (const GstTagList * list, const gchar * tag,
300 GstShout2send *shout2send = (GstShout2send *) user_data;
301 char **shout_metadata = &(shout2send->songmetadata);
302 char **song_artist = &(shout2send->songartist);
303 char **song_title = &(shout2send->songtitle);
307 GST_DEBUG ("tag: %s being added", tag);
308 if (strcmp (tag, GST_TAG_ARTIST) == 0) {
309 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
310 if (!gst_tag_list_get_string (list, tag, &value)) {
311 GST_DEBUG ("Error reading \"%s\" tag value", tag);
315 if (*song_artist != NULL)
316 g_free (*song_artist);
318 *song_artist = g_strdup (value);
320 } else if (strcmp (tag, GST_TAG_TITLE) == 0) {
321 if (gst_tag_get_type (tag) == G_TYPE_STRING) {
322 if (!gst_tag_list_get_string (list, tag, &value)) {
323 GST_DEBUG ("Error reading \"%s\" tag value", tag);
327 if (*song_title != NULL)
328 g_free (*song_title);
330 *song_title = g_strdup (value);
334 if (*shout_metadata != NULL)
335 g_free (*shout_metadata);
338 if (*song_title && *song_artist) {
339 *shout_metadata = g_strdup_printf ("%s - %s", *song_artist, *song_title);
340 } else if (*song_title && *song_artist == NULL) {
341 *shout_metadata = g_strdup_printf ("Unknown - %s", *song_title);
342 } else if (*song_title == NULL && *song_artist) {
343 *shout_metadata = g_strdup_printf ("%s - Unknown", *song_artist);
345 *shout_metadata = g_strdup_printf ("Unknown - Unknown");
348 GST_LOG ("shout metadata is now: %s", *shout_metadata);
353 gst_shout2send_set_metadata (GstShout2send * shout2send)
355 const GstTagList *user_tags;
358 shout_metadata_t *pmetadata;
360 g_return_if_fail (shout2send != NULL);
361 user_tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (shout2send));
362 if ((shout2send->tags == NULL) && (user_tags == NULL)) {
365 copy = gst_tag_list_merge (user_tags, shout2send->tags,
366 gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
367 /* lets get the artist and song tags */
369 gst_tag_list_foreach ((GstTagList *) copy, set_shout_metadata,
370 (gpointer) & tempmetadata);
372 pmetadata = shout_metadata_new ();
373 shout_metadata_add (pmetadata, "song", tempmetadata);
374 shout_set_metadata (shout2send->conn, pmetadata);
375 shout_metadata_free (pmetadata);
378 gst_tag_list_unref (copy);
384 gst_shout2send_event (GstBaseSink * sink, GstEvent * event)
386 GstShout2send *shout2send;
389 shout2send = GST_SHOUT2SEND (sink);
391 GST_LOG_OBJECT (shout2send, "got %s event", GST_EVENT_TYPE_NAME (event));
393 switch (GST_EVENT_TYPE (event)) {
395 /* vorbis audio doesn't need metadata setting on the icecast level, only mp3 */
396 if (shout2send->tags && shout2send->format == SHOUT_FORMAT_MP3) {
399 gst_event_parse_tag (event, &list);
400 GST_DEBUG_OBJECT (shout2send, "tags=%" GST_PTR_FORMAT, list);
401 gst_tag_list_insert (shout2send->tags,
403 gst_tag_setter_get_tag_merge_mode (GST_TAG_SETTER (shout2send)));
404 /* lets get the artist and song tags */
405 gst_tag_list_foreach ((GstTagList *) list,
406 set_shout_metadata, shout2send);
407 if (shout2send->songmetadata && shout2send->connected) {
408 shout_metadata_t *pmetadata;
410 GST_DEBUG_OBJECT (shout2send, "metadata now: %s",
411 shout2send->songmetadata);
413 pmetadata = shout_metadata_new ();
414 shout_metadata_add (pmetadata, "song", shout2send->songmetadata);
415 shout_set_metadata (shout2send->conn, pmetadata);
416 shout_metadata_free (pmetadata);
422 GST_LOG_OBJECT (shout2send, "let base class handle event");
423 if (GST_BASE_SINK_CLASS (parent_class)->event) {
424 event = gst_event_ref (event);
425 ret = GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
435 gst_shout2send_start (GstBaseSink * basesink)
437 GstShout2send *sink = GST_SHOUT2SEND (basesink);
438 const gchar *cur_prop;
440 gchar *version_string;
442 GST_DEBUG_OBJECT (sink, "starting");
444 sink->conn = shout_new ();
446 switch (sink->protocol) {
447 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
448 proto = SHOUT_PROTOCOL_XAUDIOCAST;
450 case SHOUT2SEND_PROTOCOL_ICY:
451 proto = SHOUT_PROTOCOL_ICY;
453 case SHOUT2SEND_PROTOCOL_HTTP:
454 proto = SHOUT_PROTOCOL_HTTP;
458 cur_prop = "protocol";
459 GST_DEBUG_OBJECT (sink, "setting protocol: %d", sink->protocol);
460 if (shout_set_protocol (sink->conn, proto) != SHOUTERR_SUCCESS)
464 GST_DEBUG_OBJECT (sink, "setting IP/hostname: %s", sink->ip);
465 if (shout_set_host (sink->conn, sink->ip) != SHOUTERR_SUCCESS)
469 GST_DEBUG_OBJECT (sink, "setting port: %u", sink->port);
470 if (shout_set_port (sink->conn, sink->port) != SHOUTERR_SUCCESS)
473 cur_prop = "password";
474 GST_DEBUG_OBJECT (sink, "setting password: %s", sink->password);
475 if (shout_set_password (sink->conn, sink->password) != SHOUTERR_SUCCESS)
479 GST_DEBUG_OBJECT (sink, "setting %s: %u", cur_prop, sink->ispublic);
480 if (shout_set_public (sink->conn,
481 (sink->ispublic ? 1 : 0)) != SHOUTERR_SUCCESS)
484 cur_prop = "streamname";
485 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->streamname);
486 if (shout_set_name (sink->conn, sink->streamname) != SHOUTERR_SUCCESS)
489 cur_prop = "description";
490 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->description);
491 if (shout_set_description (sink->conn, sink->description) != SHOUTERR_SUCCESS)
495 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->genre);
496 if (shout_set_genre (sink->conn, sink->genre) != SHOUTERR_SUCCESS)
500 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->mount);
501 if (shout_set_mount (sink->conn, sink->mount) != SHOUTERR_SUCCESS)
504 cur_prop = "username";
505 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->username);
506 if (shout_set_user (sink->conn, sink->username) != SHOUTERR_SUCCESS)
509 version_string = gst_version_string ();
511 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, version_string);
512 if (shout_set_agent (sink->conn, version_string) != SHOUTERR_SUCCESS) {
513 g_free (version_string);
517 g_free (version_string);
523 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
524 ("Error setting %s: %s", cur_prop, shout_get_error (sink->conn)));
530 gst_shout2send_connect (GstShout2send * sink)
532 GstFlowReturn fret = GST_FLOW_OK;
534 GstClockTime start_ts;
536 GST_DEBUG_OBJECT (sink, "Connection format is: %d", sink->format);
538 if (sink->format == -1)
541 if (shout_set_nonblocking (sink->conn, 1) != SHOUTERR_SUCCESS)
542 goto could_not_set_nonblocking;
544 if (shout_set_format (sink->conn, sink->format) != SHOUTERR_SUCCESS)
545 goto could_not_set_format;
547 GST_DEBUG_OBJECT (sink, "connecting");
549 start_ts = gst_util_get_timestamp ();
550 ret = shout_open (sink->conn);
552 /* wait for connection or timeout */
553 while (ret == SHOUTERR_BUSY) {
554 if (gst_util_get_timestamp () - start_ts > sink->timeout * GST_MSECOND) {
555 goto connection_timeout;
557 if (gst_poll_wait (sink->timer, 10 * GST_MSECOND) == -1) {
558 GST_LOG_OBJECT (sink, "unlocked");
560 fret = gst_base_sink_wait_preroll (GST_BASE_SINK (sink));
561 if (fret != GST_FLOW_OK)
564 ret = shout_get_connected (sink->conn);
567 if (ret != SHOUTERR_CONNECTED && ret != SHOUTERR_SUCCESS)
568 goto could_not_connect;
570 GST_DEBUG_OBJECT (sink, "connected to server");
571 sink->connected = TRUE;
573 /* initialize sending rate monitoring */
574 sink->prev_queuelen = 0;
576 sink->stalled = TRUE;
577 sink->datasent_reset_ts = sink->stalled_ts = gst_util_get_timestamp ();
579 /* let's set metadata */
580 if (sink->songmetadata) {
581 shout_metadata_t *pmetadata;
583 GST_DEBUG_OBJECT (sink, "shout metadata now: %s", sink->songmetadata);
584 pmetadata = shout_metadata_new ();
585 shout_metadata_add (pmetadata, "song", sink->songmetadata);
586 shout_set_metadata (sink->conn, pmetadata);
587 shout_metadata_free (pmetadata);
596 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
597 ("No input caps received."));
598 return GST_FLOW_NOT_NEGOTIATED;
601 could_not_set_nonblocking:
603 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
604 ("Error configuring libshout to use non-blocking i/o: %s",
605 shout_get_error (sink->conn)));
606 return GST_FLOW_ERROR;
609 could_not_set_format:
611 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
612 ("Error setting connection format: %s", shout_get_error (sink->conn)));
613 return GST_FLOW_ERROR;
618 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
619 (_("Could not connect to server")),
620 ("shout_open() failed: err=%s", shout_get_error (sink->conn)));
621 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
622 shout_get_errno (sink->conn));
623 return GST_FLOW_ERROR;
628 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
629 (_("Could not connect to server")), ("connection timed out"));
630 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
631 shout_get_errno (sink->conn));
632 return GST_FLOW_ERROR;
637 gst_shout2send_stop (GstBaseSink * basesink)
639 GstShout2send *sink = GST_SHOUT2SEND (basesink);
643 shout_close (sink->conn);
644 shout_free (sink->conn);
648 if (sink->songmetadata) {
649 g_free (sink->songmetadata);
650 sink->songmetadata = NULL;
653 sink->connected = FALSE;
660 gst_shout2send_unlock (GstBaseSink * basesink)
664 sink = GST_SHOUT2SEND (basesink);
666 GST_DEBUG_OBJECT (basesink, "unlock");
667 gst_poll_set_flushing (sink->timer, TRUE);
673 gst_shout2send_unlock_stop (GstBaseSink * basesink)
677 sink = GST_SHOUT2SEND (basesink);
679 GST_DEBUG_OBJECT (basesink, "unlock_stop");
680 gst_poll_set_flushing (sink->timer, FALSE);
686 gst_shout2send_render (GstBaseSink * basesink, GstBuffer * buf)
691 GstFlowReturn fret = GST_FLOW_OK;
696 sink = GST_SHOUT2SEND (basesink);
698 /* we connect here because we need to know the format before we can set up
699 * the connection, which we don't know yet in _start(), and also because we
700 * don't want to block the application thread */
701 if (!sink->connected) {
702 fret = gst_shout2send_connect (sink);
703 if (fret != GST_FLOW_OK)
707 delay = shout_delay (sink->conn);
710 GST_LOG_OBJECT (sink, "waiting %d msec", delay);
711 if (gst_poll_wait (sink->timer, GST_MSECOND * delay) == -1) {
712 GST_LOG_OBJECT (sink, "unlocked");
714 fret = gst_base_sink_wait_preroll (basesink);
715 if (fret != GST_FLOW_OK)
719 GST_LOG_OBJECT (sink, "we're %d msec late", -delay);
722 /* accumulate how much data have actually been sent
723 * to the network since the last call to shout_send() */
724 queuelen = shout_queuelen (sink->conn);
725 if (sink->prev_queuelen > 0)
726 sink->data_sent += sink->prev_queuelen - queuelen;
728 gst_buffer_map (buf, &map, GST_MAP_READ);
730 /* add map.size instead of re-reading the queue length because
731 * the data may actually be sent immediately */
732 sink->prev_queuelen = queuelen + map.size;
734 GST_LOG_OBJECT (sink, "sending %u bytes of data, queue length now is %"
735 G_GUINT64_FORMAT, (guint) map.size, sink->prev_queuelen);
737 ret = shout_send (sink->conn, map.data, map.size);
739 gst_buffer_unmap (buf, &map);
740 if (ret != SHOUTERR_SUCCESS)
743 now = gst_util_get_timestamp ();
744 if (now - sink->datasent_reset_ts >= 500 * GST_MSECOND) {
747 send_rate = gst_util_uint64_scale (sink->data_sent, GST_SECOND,
748 now - sink->datasent_reset_ts);
750 if (send_rate == 0 && !sink->stalled) {
751 sink->stalled = TRUE;
752 sink->stalled_ts = now;
753 } else if (send_rate > 0 && sink->stalled) {
754 sink->stalled = FALSE;
758 sink->datasent_reset_ts = now;
760 GST_DEBUG_OBJECT (sink, "sending rate is %" G_GUINT64_FORMAT " bps, "
761 "stalled %d, stalled_ts %" GST_TIME_FORMAT, send_rate, sink->stalled,
762 GST_TIME_ARGS (sink->stalled_ts));
764 if (sink->stalled && now - sink->stalled_ts >= sink->timeout * GST_MSECOND) {
765 GST_WARNING_OBJECT (sink, "network send queue is stalled for too long");
777 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
778 ("shout_send() failed: %s", shout_get_error (sink->conn)));
779 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
780 shout_get_errno (sink->conn));
781 return GST_FLOW_ERROR;
786 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
787 ("network timeout reached"));
788 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
790 return GST_FLOW_ERROR;
795 gst_shout2send_set_property (GObject * object, guint prop_id,
796 const GValue * value, GParamSpec * pspec)
798 GstShout2send *shout2send;
800 shout2send = GST_SHOUT2SEND (object);
804 g_free (shout2send->ip);
805 shout2send->ip = g_strdup (g_value_get_string (value));
808 shout2send->port = g_value_get_int (value);
811 g_free (shout2send->password);
812 shout2send->password = g_strdup (g_value_get_string (value));
815 g_free (shout2send->username);
816 shout2send->username = g_strdup (g_value_get_string (value));
819 shout2send->ispublic = g_value_get_boolean (value);
821 case ARG_STREAMNAME: /* Name of the stream */
822 g_free (shout2send->streamname);
823 shout2send->streamname = g_strdup (g_value_get_string (value));
825 case ARG_DESCRIPTION: /* Description of the stream */
826 g_free (shout2send->description);
827 shout2send->description = g_strdup (g_value_get_string (value));
829 case ARG_GENRE: /* Genre of the stream */
830 g_free (shout2send->genre);
831 shout2send->genre = g_strdup (g_value_get_string (value));
833 case ARG_PROTOCOL: /* protocol to connect with */
834 shout2send->protocol = g_value_get_enum (value);
836 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
837 g_free (shout2send->mount);
838 shout2send->mount = g_strdup (g_value_get_string (value));
840 case ARG_URL: /* the stream's homepage URL */
841 g_free (shout2send->url);
842 shout2send->url = g_strdup (g_value_get_string (value));
845 shout2send->timeout = g_value_get_uint (value);
848 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
854 gst_shout2send_get_property (GObject * object, guint prop_id,
855 GValue * value, GParamSpec * pspec)
857 GstShout2send *shout2send;
859 shout2send = GST_SHOUT2SEND (object);
863 g_value_set_string (value, shout2send->ip);
866 g_value_set_int (value, shout2send->port);
869 g_value_set_string (value, shout2send->password);
872 g_value_set_string (value, shout2send->username);
875 g_value_set_boolean (value, shout2send->ispublic);
877 case ARG_STREAMNAME: /* Name of the stream */
878 g_value_set_string (value, shout2send->streamname);
880 case ARG_DESCRIPTION: /* Description of the stream */
881 g_value_set_string (value, shout2send->description);
883 case ARG_GENRE: /* Genre of the stream */
884 g_value_set_string (value, shout2send->genre);
886 case ARG_PROTOCOL: /* protocol to connect with */
887 g_value_set_enum (value, shout2send->protocol);
889 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
890 g_value_set_string (value, shout2send->mount);
892 case ARG_URL: /* the stream's homepage URL */
893 g_value_set_string (value, shout2send->url);
896 g_value_set_uint (value, shout2send->timeout);
899 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
905 gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps)
907 const gchar *mimetype;
908 GstShout2send *shout2send;
911 shout2send = GST_SHOUT2SEND (basesink);
913 mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
915 GST_DEBUG_OBJECT (shout2send, "mimetype of caps given is: %s", mimetype);
917 if (!strcmp (mimetype, "audio/mpeg")) {
918 shout2send->format = SHOUT_FORMAT_MP3;
919 } else if (g_str_has_suffix (mimetype, "/ogg")) {
920 shout2send->format = SHOUT_FORMAT_OGG;
921 #ifdef SHOUT_FORMAT_WEBM
922 } else if (g_str_has_suffix (mimetype, "/webm")) {
923 shout2send->format = SHOUT_FORMAT_WEBM;
933 plugin_init (GstPlugin * plugin)
936 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
937 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
938 #endif /* ENABLE_NLS */
940 return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
941 GST_TYPE_SHOUT2SEND);
944 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
947 "Sends data to an icecast server using libshout2",
948 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)