1 /* GStreamer
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>
5 *
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.
10 *
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.
15 *
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.
20 */
21
22 /**
23 * SECTION:element-shout2send
24 *
25 * shout2send pushes a media stream to an Icecast server
26 *
27 * <refsect2>
28 * <title>Example launch line</title>
29 * |[
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
34 * dependent.
35 * </refsect2>
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include "gstshout2.h"
43 #include <stdlib.h>
44 #include <string.h>
45
46 #include "gst/gst-i18n-plugin.h"
47
48 GST_DEBUG_CATEGORY_STATIC (shout2_debug);
49 #define GST_CAT_DEFAULT shout2_debug
50
51
52 enum
53 {
54 SIGNAL_CONNECTION_PROBLEM, /* FIXME 2.0: remove this */
55 LAST_SIGNAL
56 };
57
58 enum
59 {
60 ARG_0,
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 */
69
70 ARG_PROTOCOL, /* Protocol to connect with */
71
72 ARG_MOUNT, /* mountpoint of stream (icecast only) */
73 ARG_URL, /* the stream's homepage URL */
74
75 ARG_TIMEOUT /* The max amount of time to wait for
76 network activity */
77 };
78
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
91
92 #ifdef SHOUT_FORMAT_WEBM
93 #define WEBM_CAPS "; video/webm; audio/webm"
94 #else
95 #define WEBM_CAPS ""
96 #endif
97 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
98 GST_PAD_SINK,
99 GST_PAD_ALWAYS,
100 GST_STATIC_CAPS ("application/ogg; audio/ogg; video/ogg; "
101 "audio/mpeg, mpegversion = (int) 1, layer = (int) [ 1, 3 ]" WEBM_CAPS));
102
103 static void gst_shout2send_finalize (GstShout2send * shout2send);
104
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,
109 GstBuffer * buffer);
110 static gboolean gst_shout2send_start (GstBaseSink * basesink);
111 static gboolean gst_shout2send_stop (GstBaseSink * basesink);
112
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);
117
118 static gboolean gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps);
119
120 static guint gst_shout2send_signals[LAST_SIGNAL] = { 0 };
121
122 #define GST_TYPE_SHOUT_PROTOCOL (gst_shout2send_protocol_get_type())
123 static GType
gst_shout2send_protocol_get_type(void)124 gst_shout2send_protocol_get_type (void)
125 {
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"},
132 {0, NULL, NULL},
133 };
134
135 if (!shout2send_protocol_type) {
136 shout2send_protocol_type =
137 g_enum_register_static ("GstShout2SendProtocol", shout2send_protocol);
138 }
139
140
141 return shout2send_protocol_type;
142 }
143
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));
147
148 static void
gst_shout2send_class_init(GstShout2sendClass * klass)149 gst_shout2send_class_init (GstShout2sendClass * klass)
150 {
151 GObjectClass *gobject_class;
152 GstElementClass *gstelement_class;
153 GstBaseSinkClass *gstbasesink_class;
154
155 gobject_class = (GObjectClass *) klass;
156 gstelement_class = (GstElementClass *) klass;
157 gstbasesink_class = (GstBaseSinkClass *) klass;
158
159 parent_class = g_type_class_peek_parent (klass);
160
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;
164
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));
172
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));
176
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));
180
181 /* metadata */
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));
186
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));
190
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));
194
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));
198
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));
203
204
205 /* icecast only */
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));
209
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));
213
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));
219
220 /* signals */
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);
226
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);
235
236 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
237
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>");
244
245 GST_DEBUG_CATEGORY_INIT (shout2_debug, "shout2", 0, "shout2send element");
246 }
247
248 static void
gst_shout2send_init(GstShout2send * shout2send)249 gst_shout2send_init (GstShout2send * shout2send)
250 {
251 gst_base_sink_set_sync (GST_BASE_SINK (shout2send), FALSE);
252
253 shout2send->timer = gst_poll_new (TRUE);
254
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;
267
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;
275 }
276
277 static void
gst_shout2send_finalize(GstShout2send * shout2send)278 gst_shout2send_finalize (GstShout2send * shout2send)
279 {
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);
288
289 gst_tag_list_unref (shout2send->tags);
290
291 gst_poll_free (shout2send->timer);
292
293 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (shout2send));
294 }
295
296 static void
set_shout_metadata(const GstTagList * list,const gchar * tag,gpointer user_data)297 set_shout_metadata (const GstTagList * list, const gchar * tag,
298 gpointer user_data)
299 {
300 GstShout2send *shout2send = (GstShout2send *) user_data;
301 char **shout_metadata = &(shout2send->songmetadata);
302 char **song_artist = &(shout2send->songartist);
303 char **song_title = &(shout2send->songtitle);
304
305 gchar *value;
306
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);
312 return;
313 }
314
315 if (*song_artist != NULL)
316 g_free (*song_artist);
317
318 *song_artist = g_strdup (value);
319 }
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);
324 return;
325 }
326
327 if (*song_title != NULL)
328 g_free (*song_title);
329
330 *song_title = g_strdup (value);
331 }
332 }
333
334 if (*shout_metadata != NULL)
335 g_free (*shout_metadata);
336
337
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);
344 } else {
345 *shout_metadata = g_strdup_printf ("Unknown - Unknown");
346 }
347
348 GST_LOG ("shout metadata is now: %s", *shout_metadata);
349 }
350
351 #if 0
352 static void
353 gst_shout2send_set_metadata (GstShout2send * shout2send)
354 {
355 const GstTagList *user_tags;
356 GstTagList *copy;
357 char *tempmetadata;
358 shout_metadata_t *pmetadata;
359
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)) {
363 return;
364 }
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 */
368 tempmetadata = NULL;
369 gst_tag_list_foreach ((GstTagList *) copy, set_shout_metadata,
370 (gpointer) & tempmetadata);
371 if (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);
376 }
377
378 gst_tag_list_unref (copy);
379 }
380 #endif
381
382
383 static gboolean
gst_shout2send_event(GstBaseSink * sink,GstEvent * event)384 gst_shout2send_event (GstBaseSink * sink, GstEvent * event)
385 {
386 GstShout2send *shout2send;
387 gboolean ret = TRUE;
388
389 shout2send = GST_SHOUT2SEND (sink);
390
391 GST_LOG_OBJECT (shout2send, "got %s event", GST_EVENT_TYPE_NAME (event));
392
393 switch (GST_EVENT_TYPE (event)) {
394 case GST_EVENT_TAG:{
395 /* vorbis audio doesn't need metadata setting on the icecast level, only mp3 */
396 if (shout2send->tags && shout2send->format == SHOUT_FORMAT_MP3) {
397 GstTagList *list;
398
399 gst_event_parse_tag (event, &list);
400 GST_DEBUG_OBJECT (shout2send, "tags=%" GST_PTR_FORMAT, list);
401 gst_tag_list_insert (shout2send->tags,
402 list,
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;
409
410 GST_DEBUG_OBJECT (shout2send, "metadata now: %s",
411 shout2send->songmetadata);
412
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);
417 }
418 }
419 break;
420 }
421 default:{
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);
426 }
427 break;
428 }
429 }
430
431 return ret;
432 }
433
434 static gboolean
gst_shout2send_start(GstBaseSink * basesink)435 gst_shout2send_start (GstBaseSink * basesink)
436 {
437 GstShout2send *sink = GST_SHOUT2SEND (basesink);
438 const gchar *cur_prop;
439 gshort proto = 3;
440 gchar *version_string;
441
442 GST_DEBUG_OBJECT (sink, "starting");
443
444 sink->conn = shout_new ();
445
446 switch (sink->protocol) {
447 case SHOUT2SEND_PROTOCOL_XAUDIOCAST:
448 proto = SHOUT_PROTOCOL_XAUDIOCAST;
449 break;
450 case SHOUT2SEND_PROTOCOL_ICY:
451 proto = SHOUT_PROTOCOL_ICY;
452 break;
453 case SHOUT2SEND_PROTOCOL_HTTP:
454 proto = SHOUT_PROTOCOL_HTTP;
455 break;
456 }
457
458 cur_prop = "protocol";
459 GST_DEBUG_OBJECT (sink, "setting protocol: %d", sink->protocol);
460 if (shout_set_protocol (sink->conn, proto) != SHOUTERR_SUCCESS)
461 goto set_failed;
462
463 cur_prop = "ip";
464 GST_DEBUG_OBJECT (sink, "setting IP/hostname: %s", sink->ip);
465 if (shout_set_host (sink->conn, sink->ip) != SHOUTERR_SUCCESS)
466 goto set_failed;
467
468 cur_prop = "port";
469 GST_DEBUG_OBJECT (sink, "setting port: %u", sink->port);
470 if (shout_set_port (sink->conn, sink->port) != SHOUTERR_SUCCESS)
471 goto set_failed;
472
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)
476 goto set_failed;
477
478 cur_prop = "public";
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)
482 goto set_failed;
483
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)
487 goto set_failed;
488
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)
492 goto set_failed;
493
494 cur_prop = "genre";
495 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->genre);
496 if (shout_set_genre (sink->conn, sink->genre) != SHOUTERR_SUCCESS)
497 goto set_failed;
498
499 cur_prop = "mount";
500 GST_DEBUG_OBJECT (sink, "setting %s: %s", cur_prop, sink->mount);
501 if (shout_set_mount (sink->conn, sink->mount) != SHOUTERR_SUCCESS)
502 goto set_failed;
503
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)
507 goto set_failed;
508
509 version_string = gst_version_string ();
510 cur_prop = "agent";
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);
514 goto set_failed;
515 }
516
517 g_free (version_string);
518 return TRUE;
519
520 /* ERROR */
521 set_failed:
522 {
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);
526 sink->conn = NULL;
527 return FALSE;
528 }
529 }
530
531 static GstFlowReturn
gst_shout2send_connect(GstShout2send * sink)532 gst_shout2send_connect (GstShout2send * sink)
533 {
534 GstFlowReturn fret = GST_FLOW_OK;
535 gint ret;
536 GstClockTime start_ts;
537
538 GST_DEBUG_OBJECT (sink, "Connection format is: %d", sink->format);
539
540 if (sink->format == -1)
541 goto no_caps;
542
543 if (shout_set_nonblocking (sink->conn, 1) != SHOUTERR_SUCCESS)
544 goto could_not_set_nonblocking;
545
546 if (shout_set_format (sink->conn, sink->format) != SHOUTERR_SUCCESS)
547 goto could_not_set_format;
548
549 GST_DEBUG_OBJECT (sink, "connecting");
550
551 start_ts = gst_util_get_timestamp ();
552 ret = shout_open (sink->conn);
553
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;
558 }
559 if (gst_poll_wait (sink->timer, 10 * GST_MSECOND) == -1) {
560 GST_LOG_OBJECT (sink, "unlocked");
561
562 fret = gst_base_sink_wait_preroll (GST_BASE_SINK (sink));
563 if (fret != GST_FLOW_OK)
564 goto done;
565 }
566 ret = shout_get_connected (sink->conn);
567 }
568
569 if (ret != SHOUTERR_CONNECTED && ret != SHOUTERR_SUCCESS)
570 goto could_not_connect;
571
572 GST_DEBUG_OBJECT (sink, "connected to server");
573 sink->connected = TRUE;
574
575 /* initialize sending rate monitoring */
576 sink->prev_queuelen = 0;
577 sink->data_sent = 0;
578 sink->stalled = TRUE;
579 sink->datasent_reset_ts = sink->stalled_ts = gst_util_get_timestamp ();
580
581 /* let's set metadata */
582 if (sink->songmetadata) {
583 shout_metadata_t *pmetadata;
584
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);
590 }
591
592 done:
593 return fret;
594
595 /* ERRORS */
596 no_caps:
597 {
598 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
599 ("No input caps received."));
600 return GST_FLOW_NOT_NEGOTIATED;
601 }
602
603 could_not_set_nonblocking:
604 {
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;
609 }
610
611 could_not_set_format:
612 {
613 GST_ELEMENT_ERROR (sink, LIBRARY, SETTINGS, (NULL),
614 ("Error setting connection format: %s", shout_get_error (sink->conn)));
615 return GST_FLOW_ERROR;
616 }
617
618 could_not_connect:
619 {
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;
626 }
627
628 connection_timeout:
629 {
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;
635 }
636 }
637
638 static gboolean
gst_shout2send_stop(GstBaseSink * basesink)639 gst_shout2send_stop (GstBaseSink * basesink)
640 {
641 GstShout2send *sink = GST_SHOUT2SEND (basesink);
642
643 if (sink->conn) {
644 if (sink->connected)
645 shout_close (sink->conn);
646 shout_free (sink->conn);
647 sink->conn = NULL;
648 }
649
650 if (sink->songmetadata) {
651 g_free (sink->songmetadata);
652 sink->songmetadata = NULL;
653 }
654
655 sink->connected = FALSE;
656 sink->format = -1;
657
658 return TRUE;
659 }
660
661 static gboolean
gst_shout2send_unlock(GstBaseSink * basesink)662 gst_shout2send_unlock (GstBaseSink * basesink)
663 {
664 GstShout2send *sink;
665
666 sink = GST_SHOUT2SEND (basesink);
667
668 GST_DEBUG_OBJECT (basesink, "unlock");
669 gst_poll_set_flushing (sink->timer, TRUE);
670
671 return TRUE;
672 }
673
674 static gboolean
gst_shout2send_unlock_stop(GstBaseSink * basesink)675 gst_shout2send_unlock_stop (GstBaseSink * basesink)
676 {
677 GstShout2send *sink;
678
679 sink = GST_SHOUT2SEND (basesink);
680
681 GST_DEBUG_OBJECT (basesink, "unlock_stop");
682 gst_poll_set_flushing (sink->timer, FALSE);
683
684 return TRUE;
685 }
686
687 static GstFlowReturn
gst_shout2send_render(GstBaseSink * basesink,GstBuffer * buf)688 gst_shout2send_render (GstBaseSink * basesink, GstBuffer * buf)
689 {
690 GstShout2send *sink;
691 glong ret;
692 gint delay;
693 GstFlowReturn fret = GST_FLOW_OK;
694 GstMapInfo map;
695 GstClockTime now;
696 ssize_t queuelen;
697
698 sink = GST_SHOUT2SEND (basesink);
699
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)
706 goto done;
707 }
708
709 delay = shout_delay (sink->conn);
710
711 if (delay > 0) {
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");
715
716 fret = gst_base_sink_wait_preroll (basesink);
717 if (fret != GST_FLOW_OK)
718 goto done;
719 }
720 } else {
721 GST_LOG_OBJECT (sink, "we're %d msec late", -delay);
722 }
723
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;
729
730 gst_buffer_map (buf, &map, GST_MAP_READ);
731
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;
735
736 GST_LOG_OBJECT (sink, "sending %u bytes of data, queue length now is %"
737 G_GUINT64_FORMAT, (guint) map.size, sink->prev_queuelen);
738
739 ret = shout_send (sink->conn, map.data, map.size);
740
741 gst_buffer_unmap (buf, &map);
742 if (ret != SHOUTERR_SUCCESS)
743 goto send_error;
744
745 now = gst_util_get_timestamp ();
746 if (now - sink->datasent_reset_ts >= 500 * GST_MSECOND) {
747 guint64 send_rate;
748
749 send_rate = gst_util_uint64_scale (sink->data_sent, GST_SECOND,
750 now - sink->datasent_reset_ts);
751
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;
757 }
758
759 sink->data_sent = 0;
760 sink->datasent_reset_ts = now;
761
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));
765
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");
768 goto network_error;
769 }
770 }
771
772 done:
773
774 return fret;
775
776 /* ERRORS */
777 send_error:
778 {
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;
784 }
785
786 network_error:
787 {
788 GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (NULL),
789 ("network timeout reached"));
790 g_signal_emit (sink, gst_shout2send_signals[SIGNAL_CONNECTION_PROBLEM], 0,
791 SHOUTERR_BUSY);
792 return GST_FLOW_ERROR;
793 }
794 }
795
796 static void
gst_shout2send_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)797 gst_shout2send_set_property (GObject * object, guint prop_id,
798 const GValue * value, GParamSpec * pspec)
799 {
800 GstShout2send *shout2send;
801
802 shout2send = GST_SHOUT2SEND (object);
803 switch (prop_id) {
804
805 case ARG_IP:
806 g_free (shout2send->ip);
807 shout2send->ip = g_strdup (g_value_get_string (value));
808 break;
809 case ARG_PORT:
810 shout2send->port = g_value_get_int (value);
811 break;
812 case ARG_PASSWORD:
813 g_free (shout2send->password);
814 shout2send->password = g_strdup (g_value_get_string (value));
815 break;
816 case ARG_USERNAME:
817 g_free (shout2send->username);
818 shout2send->username = g_strdup (g_value_get_string (value));
819 break;
820 case ARG_PUBLIC:
821 shout2send->ispublic = g_value_get_boolean (value);
822 break;
823 case ARG_STREAMNAME: /* Name of the stream */
824 g_free (shout2send->streamname);
825 shout2send->streamname = g_strdup (g_value_get_string (value));
826 break;
827 case ARG_DESCRIPTION: /* Description of the stream */
828 g_free (shout2send->description);
829 shout2send->description = g_strdup (g_value_get_string (value));
830 break;
831 case ARG_GENRE: /* Genre of the stream */
832 g_free (shout2send->genre);
833 shout2send->genre = g_strdup (g_value_get_string (value));
834 break;
835 case ARG_PROTOCOL: /* protocol to connect with */
836 shout2send->protocol = g_value_get_enum (value);
837 break;
838 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
839 g_free (shout2send->mount);
840 shout2send->mount = g_strdup (g_value_get_string (value));
841 break;
842 case ARG_URL: /* the stream's homepage URL */
843 g_free (shout2send->url);
844 shout2send->url = g_strdup (g_value_get_string (value));
845 break;
846 case ARG_TIMEOUT:
847 shout2send->timeout = g_value_get_uint (value);
848 break;
849 default:
850 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
851 break;
852 }
853 }
854
855 static void
gst_shout2send_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)856 gst_shout2send_get_property (GObject * object, guint prop_id,
857 GValue * value, GParamSpec * pspec)
858 {
859 GstShout2send *shout2send;
860
861 shout2send = GST_SHOUT2SEND (object);
862 switch (prop_id) {
863
864 case ARG_IP:
865 g_value_set_string (value, shout2send->ip);
866 break;
867 case ARG_PORT:
868 g_value_set_int (value, shout2send->port);
869 break;
870 case ARG_PASSWORD:
871 g_value_set_string (value, shout2send->password);
872 break;
873 case ARG_USERNAME:
874 g_value_set_string (value, shout2send->username);
875 break;
876 case ARG_PUBLIC:
877 g_value_set_boolean (value, shout2send->ispublic);
878 break;
879 case ARG_STREAMNAME: /* Name of the stream */
880 g_value_set_string (value, shout2send->streamname);
881 break;
882 case ARG_DESCRIPTION: /* Description of the stream */
883 g_value_set_string (value, shout2send->description);
884 break;
885 case ARG_GENRE: /* Genre of the stream */
886 g_value_set_string (value, shout2send->genre);
887 break;
888 case ARG_PROTOCOL: /* protocol to connect with */
889 g_value_set_enum (value, shout2send->protocol);
890 break;
891 case ARG_MOUNT: /* mountpoint of stream (icecast only) */
892 g_value_set_string (value, shout2send->mount);
893 break;
894 case ARG_URL: /* the stream's homepage URL */
895 g_value_set_string (value, shout2send->url);
896 break;
897 case ARG_TIMEOUT:
898 g_value_set_uint (value, shout2send->timeout);
899 break;
900 default:
901 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
902 break;
903 }
904 }
905
906 static gboolean
gst_shout2send_setcaps(GstBaseSink * basesink,GstCaps * caps)907 gst_shout2send_setcaps (GstBaseSink * basesink, GstCaps * caps)
908 {
909 const gchar *mimetype;
910 GstShout2send *shout2send;
911 gboolean ret = TRUE;
912
913 shout2send = GST_SHOUT2SEND (basesink);
914
915 mimetype = gst_structure_get_name (gst_caps_get_structure (caps, 0));
916
917 GST_DEBUG_OBJECT (shout2send, "mimetype of caps given is: %s", mimetype);
918
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;
926 #endif
927 } else {
928 ret = FALSE;
929 }
930
931 return ret;
932 }
933
934 static gboolean
plugin_init(GstPlugin * plugin)935 plugin_init (GstPlugin * plugin)
936 {
937 #ifdef ENABLE_NLS
938 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
939 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
940 #endif /* ENABLE_NLS */
941
942 return gst_element_register (plugin, "shout2send", GST_RANK_NONE,
943 GST_TYPE_SHOUT2SEND);
944 }
945
946 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
947 GST_VERSION_MINOR,
948 shout2,
949 "Sends data to an icecast server using libshout2",
950 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
951