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)));
525 shout_free (sink->conn);
532 gst_shout2send_connect (GstShout2send * sink)
534 GstFlowReturn fret = GST_FLOW_OK;
536 GstClockTime start_ts;
538 GST_DEBUG_OBJECT (sink, "Connection format is: %d", sink->format);
540 if (sink->format == -1)
543 if (shout_set_nonblocking (sink->conn, 1) != SHOUTERR_SUCCESS)
544 goto could_not_set_nonblocking;
546 if (shout_set_format (sink->conn, sink->format) != SHOUTERR_SUCCESS)
547 goto could_not_set_format;
549 GST_DEBUG_OBJECT (sink, "connecting");
551 start_ts = gst_util_get_timestamp ();
552 ret = shout_open (sink->conn);
554 /* wait for connection or timeout */
555 while (ret == SHOUTERR_BUSY) {
556 if (gst_util_get_timestamp () - start_ts > sink->timeout * GST_MSECOND) {
557 goto connection_timeout;
559 if (gst_poll_wait (sink->timer, 10 * GST_MSECOND) == -1) {
560 GST_LOG_OBJECT (sink, "unlocked");
562 fret = gst_base_sink_wait_preroll (GST_BASE_SINK (sink));
563 if (fret != GST_FLOW_OK)
566 ret = shout_get_connected (sink->conn);
569 if (ret != SHOUTERR_CONNECTED && ret != SHOUTERR_SUCCESS)
570 goto could_not_connect;
572 GST_DEBUG_OBJECT (sink, "connected to server");
573 sink->connected = TRUE;
575 /* initialize sending rate monitoring */
576 sink->prev_queuelen = 0;
578 sink->stalled = TRUE;
579 sink->datasent_reset_ts = sink->stalled_ts = gst_util_get_timestamp ();
581 /* let's set metadata */
582 if (sink->songmetadata) {
583 shout_metadata_t *pmetadata;
585 GST_DEBUG_OBJECT (sink, "shout metadata now: %s", sink->songmetadata);
586 pmetadata = shout_metadata_new ();
587 shout_metadata_add (pmetadata, "song", sink->songmetadata);
588 shout_set_metadata (sink->conn, pmetadata);
589 shout_metadata_free (pmetadata);
598 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
599 ("No input caps received."));
600 return GST_FLOW_NOT_NEGOTIATED;
603 could_not_set_nonblocking:
605 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
606 ("Error configuring libshout to use non-blocking i/o: %s",
607 shout_get_error (sink->conn)));
608 return GST_FLOW_ERROR;
611 could_not_set_format:
613 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
614 ("Error setting connection format: %s", shout_get_error (sink->conn)));
615 return GST_FLOW_ERROR;
620 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
621 (_("Could not connect to server")),
622 ("shout_open() failed: err=%s", shout_get_error (sink->conn)));
623 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
624 shout_get_errno (sink->conn));
625 return GST_FLOW_ERROR;
630 GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
631 (_("Could not connect to server")), ("connection timed out"));
632 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
633 shout_get_errno (sink->conn));
634 return GST_FLOW_ERROR;
639 gst_shout2send_stop (GstBaseSink * basesink)
641 GstShout2send *sink = GST_SHOUT2SEND (basesink);
645 shout_close (sink->conn);
646 shout_free (sink->conn);
650 if (sink->songmetadata) {
651 g_free (sink->songmetadata);
652 sink->songmetadata = NULL;
655 sink->connected = FALSE;
662 gst_shout2send_unlock (GstBaseSink * basesink)
666 sink = GST_SHOUT2SEND (basesink);
668 GST_DEBUG_OBJECT (basesink, "unlock");
669 gst_poll_set_flushing (sink->timer, TRUE);
675 gst_shout2send_unlock_stop (GstBaseSink * basesink)
679 sink = GST_SHOUT2SEND (basesink);
681 GST_DEBUG_OBJECT (basesink, "unlock_stop");
682 gst_poll_set_flushing (sink->timer, FALSE);
688 gst_shout2send_render (GstBaseSink * basesink, GstBuffer * buf)
693 GstFlowReturn fret = GST_FLOW_OK;
698 sink = GST_SHOUT2SEND (basesink);
700 /* we connect here because we need to know the format before we can set up
701 * the connection, which we don't know yet in _start(), and also because we
702 * don't want to block the application thread */
703 if (!sink->connected) {
704 fret = gst_shout2send_connect (sink);
705 if (fret != GST_FLOW_OK)
709 delay = shout_delay (sink->conn);
712 GST_LOG_OBJECT (sink, "waiting %d msec", delay);
713 if (gst_poll_wait (sink->timer, GST_MSECOND * delay) == -1) {
714 GST_LOG_OBJECT (sink, "unlocked");
716 fret = gst_base_sink_wait_preroll (basesink);
717 if (fret != GST_FLOW_OK)
721 GST_LOG_OBJECT (sink, "we're %d msec late", -delay);
724 /* accumulate how much data have actually been sent
725 * to the network since the last call to shout_send() */
726 queuelen = shout_queuelen (sink->conn);
727 if (sink->prev_queuelen > 0)
728 sink->data_sent += sink->prev_queuelen - queuelen;
730 gst_buffer_map (buf, &map, GST_MAP_READ);
732 /* add map.size instead of re-reading the queue length because
733 * the data may actually be sent immediately */
734 sink->prev_queuelen = queuelen + map.size;
736 GST_LOG_OBJECT (sink, "sending %u bytes of data, queue length now is %"
737 G_GUINT64_FORMAT, (guint) map.size, sink->prev_queuelen);
739 ret = shout_send (sink->conn, map.data, map.size);
741 gst_buffer_unmap (buf, &map);
742 if (ret != SHOUTERR_SUCCESS)
745 now = gst_util_get_timestamp ();
746 if (now - sink->datasent_reset_ts >= 500 * GST_MSECOND) {
749 send_rate = gst_util_uint64_scale (sink->data_sent, GST_SECOND,
750 now - sink->datasent_reset_ts);
752 if (send_rate == 0 && !sink->stalled) {
753 sink->stalled = TRUE;
754 sink->stalled_ts = now;
755 } else if (send_rate > 0 && sink->stalled) {
756 sink->stalled = FALSE;
760 sink->datasent_reset_ts = now;
762 GST_DEBUG_OBJECT (sink, "sending rate is %" G_GUINT64_FORMAT " bps, "
763 "stalled %d, stalled_ts %" GST_TIME_FORMAT, send_rate, sink->stalled,
764 GST_TIME_ARGS (sink->stalled_ts));
766 if (sink->stalled && now - sink->stalled_ts >= sink->timeout * GST_MSECOND) {
767 GST_WARNING_OBJECT (sink, "network send queue is stalled for too long");
779 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
780 ("shout_send() failed: %s", shout_get_error (sink->conn)));
781 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
782 shout_get_errno (sink->conn));
783 return GST_FLOW_ERROR;
788 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
789 ("network timeout reached"));
790 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
792 return GST_FLOW_ERROR;
797 gst_shout2send_set_property (GObject * object, guint prop_id,
798 const GValue * value, GParamSpec * pspec)
800 GstShout2send *shout2send;
802 shout2send = GST_SHOUT2SEND (object);
806 g_free (shout2send->ip);
807 shout2send->ip = g_strdup (g_value_get_string (value));
810 shout2send->port = g_value_get_int (value);
813 g_free (shout2send->password);
814 shout2send->password = g_strdup (g_value_get_string (value));
817 g_free (shout2send->username);
818 shout2send->username = g_strdup (g_value_get_string (value));
821 shout2send->ispublic = g_value_get_boolean (value);
823 case ARG_STREAMNAME: /* Name of the stream */
824 g_free (shout2send->streamname);
825 shout2send->streamname = g_strdup (g_value_get_string (value));
827 case ARG_DESCRIPTION: /* Description of the stream */
828 g_free (shout2send->description);
829 shout2send->description = g_strdup (g_value_get_string (value));
831 case ARG_GENRE: /* Genre of the stream */
832 g_free (shout2send->genre);
833 shout2send->genre = g_strdup (g_value_get_string (value));
835 case ARG_PROTOCOL: /* protocol to connect with */
836 shout2send->protocol = g_value_get_enum (value);
838 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
839 g_free (shout2send->mount);
840 shout2send->mount = g_strdup (g_value_get_string (value));
842 case ARG_URL: /* the stream's homepage URL */
843 g_free (shout2send->url);
844 shout2send->url = g_strdup (g_value_get_string (value));
847 shout2send->timeout = g_value_get_uint (value);
850 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
856 gst_shout2send_get_property (GObject * object, guint prop_id,
857 GValue * value, GParamSpec * pspec)
859 GstShout2send *shout2send;
861 shout2send = GST_SHOUT2SEND (object);
865 g_value_set_string (value, shout2send->ip);
868 g_value_set_int (value, shout2send->port);
871 g_value_set_string (value, shout2send->password);
874 g_value_set_string (value, shout2send->username);
877 g_value_set_boolean (value, shout2send->ispublic);
879 case ARG_STREAMNAME: /* Name of the stream */
880 g_value_set_string (value, shout2send->streamname);
882 case ARG_DESCRIPTION: /* Description of the stream */
883 g_value_set_string (value, shout2send->description);
885 case ARG_GENRE: /* Genre of the stream */
886 g_value_set_string (value, shout2send->genre);
888 case ARG_PROTOCOL: /* protocol to connect with */
889 g_value_set_enum (value, shout2send->protocol);
891 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
892 g_value_set_string (value, shout2send->mount);
894 case ARG_URL: /* the stream's homepage URL */
895 g_value_set_string (value, shout2send->url);
898 g_value_set_uint (value, shout2send->timeout);
901 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
907 gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps)
909 const gchar *mimetype;
910 GstShout2send *shout2send;
913 shout2send = GST_SHOUT2SEND (basesink);
915 mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
917 GST_DEBUG_OBJECT (shout2send, "mimetype of caps given is: %s", mimetype);
919 if (!strcmp (mimetype, "audio/mpeg")) {
920 shout2send->format = SHOUT_FORMAT_MP3;
921 } else if (g_str_has_suffix (mimetype, "/ogg")) {
922 shout2send->format = SHOUT_FORMAT_OGG;
923 #ifdef SHOUT_FORMAT_WEBM
924 } else if (g_str_has_suffix (mimetype, "/webm")) {
925 shout2send->format = SHOUT_FORMAT_WEBM;
935 plugin_init (GstPlugin * plugin)
938 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
939 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
940 #endif /* ENABLE_NLS */
942 return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
943 GST_TYPE_SHOUT2SEND);
946 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
949 "Sends data to an icecast server using libshout2",
950 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)