2 * Copyright (C) 2011 David Schleef <ds@entropywave.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
20 * SECTION:element-gstsouphttpsink
22 * The souphttpsink element sends pipeline data to an HTTP server
23 * using HTTP PUT commands.
26 * <title>Example launch line</title>
28 * gst-launch -v videotestsrc num-buffers=300 ! theoraenc ! oggmux !
29 * souphttpsink location=http://server/filename.ogv
32 * This example encodes 10 seconds of video and sends it to the HTTP
33 * server "server" using HTTP PUT commands.
42 #include <gst/base/gstbasesink.h>
43 #include "gstsouphttpsink.h"
45 GST_DEBUG_CATEGORY_STATIC (gst_soup_http_sink_debug_category);
46 #define GST_CAT_DEFAULT gst_soup_http_sink_debug_category
51 static void gst_soup_http_sink_set_property (GObject * object,
52 guint property_id, const GValue * value, GParamSpec * pspec);
53 static void gst_soup_http_sink_get_property (GObject * object,
54 guint property_id, GValue * value, GParamSpec * pspec);
55 static void gst_soup_http_sink_dispose (GObject * object);
56 static void gst_soup_http_sink_finalize (GObject * object);
58 static gboolean gst_soup_http_sink_set_caps (GstBaseSink * sink,
60 static void gst_soup_http_sink_get_times (GstBaseSink * sink,
61 GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
62 static gboolean gst_soup_http_sink_start (GstBaseSink * sink);
63 static gboolean gst_soup_http_sink_stop (GstBaseSink * sink);
64 static gboolean gst_soup_http_sink_unlock (GstBaseSink * sink);
65 static gboolean gst_soup_http_sink_event (GstBaseSink * sink, GstEvent * event);
67 gst_soup_http_sink_preroll (GstBaseSink * sink, GstBuffer * buffer);
69 gst_soup_http_sink_render (GstBaseSink * sink, GstBuffer * buffer);
71 static void free_buffer_list (GList * list);
72 static void gst_soup_http_sink_reset (GstSoupHttpSink * souphttpsink);
73 static void authenticate (SoupSession * session, SoupMessage * msg,
74 SoupAuth * auth, gboolean retrying, gpointer user_data);
76 callback (SoupSession * session, SoupMessage * msg, gpointer user_data);
78 gst_soup_http_sink_set_proxy (GstSoupHttpSink * souphttpsink,
86 PROP_AUTOMATIC_REDIRECT,
96 #define DEFAULT_USER_AGENT "GStreamer souphttpsink "
100 static GstStaticPadTemplate gst_soup_http_sink_sink_template =
101 GST_STATIC_PAD_TEMPLATE ("sink",
104 GST_STATIC_CAPS_ANY);
107 /* class initialization */
109 #define DEBUG_INIT(bla) \
110 GST_DEBUG_CATEGORY_INIT (gst_soup_http_sink_debug_category, "souphttpsink", 0, \
111 "debug category for souphttpsink element");
113 GST_BOILERPLATE_FULL (GstSoupHttpSink, gst_soup_http_sink, GstBaseSink,
114 GST_TYPE_BASE_SINK, DEBUG_INIT);
117 gst_soup_http_sink_base_init (gpointer g_class)
119 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
121 gst_element_class_add_pad_template (element_class,
122 gst_static_pad_template_get (&gst_soup_http_sink_sink_template));
124 gst_element_class_set_details_simple (element_class, "HTTP client sink",
125 "Generic", "Sends streams to HTTP server via PUT",
126 "David Schleef <ds@entropywave.com>");
130 gst_soup_http_sink_class_init (GstSoupHttpSinkClass * klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 GstBaseSinkClass *base_sink_class = GST_BASE_SINK_CLASS (klass);
135 gobject_class->set_property = gst_soup_http_sink_set_property;
136 gobject_class->get_property = gst_soup_http_sink_get_property;
137 gobject_class->dispose = gst_soup_http_sink_dispose;
138 gobject_class->finalize = gst_soup_http_sink_finalize;
139 base_sink_class->set_caps = GST_DEBUG_FUNCPTR (gst_soup_http_sink_set_caps);
141 base_sink_class->get_times =
142 GST_DEBUG_FUNCPTR (gst_soup_http_sink_get_times);
143 base_sink_class->start = GST_DEBUG_FUNCPTR (gst_soup_http_sink_start);
144 base_sink_class->stop = GST_DEBUG_FUNCPTR (gst_soup_http_sink_stop);
145 base_sink_class->unlock = GST_DEBUG_FUNCPTR (gst_soup_http_sink_unlock);
146 base_sink_class->event = GST_DEBUG_FUNCPTR (gst_soup_http_sink_event);
148 base_sink_class->preroll = GST_DEBUG_FUNCPTR (gst_soup_http_sink_preroll);
149 base_sink_class->render = GST_DEBUG_FUNCPTR (gst_soup_http_sink_render);
151 g_object_class_install_property (gobject_class,
153 g_param_spec_string ("location", "Location",
154 "URI to send to", "", G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 g_object_class_install_property (gobject_class,
157 g_param_spec_string ("user-agent", "User-Agent",
158 "Value of the User-Agent HTTP request header field",
159 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
160 g_object_class_install_property (gobject_class,
161 PROP_AUTOMATIC_REDIRECT,
162 g_param_spec_boolean ("automatic-redirect", "automatic-redirect",
163 "Automatically follow HTTP redirects (HTTP Status Code 3xx)",
164 TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 g_object_class_install_property (gobject_class,
167 g_param_spec_string ("proxy", "Proxy",
168 "HTTP proxy server URI", "",
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
170 g_object_class_install_property (gobject_class,
172 g_param_spec_string ("user-id", "user-id",
173 "user id for authentication", "",
174 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
175 g_object_class_install_property (gobject_class, PROP_USER_PW,
176 g_param_spec_string ("user-pw", "user-pw",
177 "user password for authentication", "",
178 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
179 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
180 g_param_spec_string ("proxy-id", "proxy-id",
181 "user id for proxy authentication", "",
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
184 g_param_spec_string ("proxy-pw", "proxy-pw",
185 "user password for proxy authentication", "",
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187 g_object_class_install_property (gobject_class, PROP_SESSION,
188 g_param_spec_object ("session", "session",
189 "SoupSession object to use for communication",
190 SOUP_TYPE_SESSION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
191 g_object_class_install_property (gobject_class, PROP_COOKIES,
192 g_param_spec_boxed ("cookies", "Cookies", "HTTP request cookies",
193 G_TYPE_STRV, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 gst_soup_http_sink_init (GstSoupHttpSink * souphttpsink,
199 GstSoupHttpSinkClass * souphttpsink_class)
203 souphttpsink->sinkpad =
204 gst_pad_new_from_static_template (&gst_soup_http_sink_sink_template,
207 souphttpsink->mutex = g_mutex_new ();
208 souphttpsink->cond = g_cond_new ();
210 souphttpsink->location = NULL;
211 souphttpsink->automatic_redirect = TRUE;
212 souphttpsink->user_agent = g_strdup (DEFAULT_USER_AGENT);
213 souphttpsink->user_id = NULL;
214 souphttpsink->user_pw = NULL;
215 souphttpsink->proxy_id = NULL;
216 souphttpsink->proxy_pw = NULL;
217 souphttpsink->prop_session = NULL;
218 souphttpsink->timeout = 1;
219 proxy = g_getenv ("http_proxy");
220 if (proxy && !gst_soup_http_sink_set_proxy (souphttpsink, proxy)) {
221 GST_WARNING_OBJECT (souphttpsink,
222 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
226 gst_soup_http_sink_reset (souphttpsink);
230 gst_soup_http_sink_reset (GstSoupHttpSink * souphttpsink)
232 g_free (souphttpsink->reason_phrase);
233 souphttpsink->reason_phrase = NULL;
234 souphttpsink->status_code = 0;
235 souphttpsink->offset = 0;
240 gst_soup_http_sink_set_proxy (GstSoupHttpSink * souphttpsink, const gchar * uri)
242 if (souphttpsink->proxy) {
243 soup_uri_free (souphttpsink->proxy);
244 souphttpsink->proxy = NULL;
246 if (g_str_has_prefix (uri, "http://")) {
247 souphttpsink->proxy = soup_uri_new (uri);
249 gchar *new_uri = g_strconcat ("http://", uri, NULL);
251 souphttpsink->proxy = soup_uri_new (new_uri);
259 gst_soup_http_sink_set_property (GObject * object, guint property_id,
260 const GValue * value, GParamSpec * pspec)
262 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
264 g_mutex_lock (souphttpsink->mutex);
265 switch (property_id) {
267 if (souphttpsink->prop_session) {
268 g_object_unref (souphttpsink->prop_session);
270 souphttpsink->prop_session = g_value_dup_object (value);
273 g_free (souphttpsink->location);
274 souphttpsink->location = g_value_dup_string (value);
275 souphttpsink->offset = 0;
277 case PROP_USER_AGENT:
278 g_free (souphttpsink->user_agent);
279 souphttpsink->user_agent = g_value_dup_string (value);
281 case PROP_AUTOMATIC_REDIRECT:
282 souphttpsink->automatic_redirect = g_value_get_boolean (value);
285 g_free (souphttpsink->user_id);
286 souphttpsink->user_id = g_value_dup_string (value);
289 g_free (souphttpsink->user_pw);
290 souphttpsink->user_pw = g_value_dup_string (value);
293 g_free (souphttpsink->proxy_id);
294 souphttpsink->proxy_id = g_value_dup_string (value);
297 g_free (souphttpsink->proxy_pw);
298 souphttpsink->proxy_pw = g_value_dup_string (value);
304 proxy = g_value_get_string (value);
307 GST_WARNING ("proxy property cannot be NULL");
310 if (!gst_soup_http_sink_set_proxy (souphttpsink, proxy)) {
311 GST_WARNING ("badly formatted proxy URI");
317 g_strfreev (souphttpsink->cookies);
318 souphttpsink->cookies = g_strdupv (g_value_get_boxed (value));
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
325 g_mutex_unlock (souphttpsink->mutex);
329 gst_soup_http_sink_get_property (GObject * object, guint property_id,
330 GValue * value, GParamSpec * pspec)
332 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
334 switch (property_id) {
336 g_value_set_object (value, souphttpsink->prop_session);
339 g_value_set_string (value, souphttpsink->location);
341 case PROP_AUTOMATIC_REDIRECT:
342 g_value_set_boolean (value, souphttpsink->automatic_redirect);
344 case PROP_USER_AGENT:
345 g_value_set_string (value, souphttpsink->user_agent);
348 g_value_set_string (value, souphttpsink->user_id);
351 g_value_set_string (value, souphttpsink->user_pw);
354 g_value_set_string (value, souphttpsink->proxy_id);
357 g_value_set_string (value, souphttpsink->proxy_pw);
360 if (souphttpsink->proxy == NULL)
361 g_value_set_static_string (value, "");
363 char *proxy = soup_uri_to_string (souphttpsink->proxy, FALSE);
365 g_value_set_string (value, proxy);
370 g_value_set_boxed (value, g_strdupv (souphttpsink->cookies));
373 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
379 gst_soup_http_sink_dispose (GObject * object)
381 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
383 /* clean up as possible. may be called multiple times */
384 if (souphttpsink->prop_session)
385 g_object_unref (souphttpsink->prop_session);
386 souphttpsink->prop_session = NULL;
388 G_OBJECT_CLASS (parent_class)->dispose (object);
392 gst_soup_http_sink_finalize (GObject * object)
394 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
396 /* clean up object here */
398 g_free (souphttpsink->user_agent);
399 g_free (souphttpsink->user_id);
400 g_free (souphttpsink->user_pw);
401 g_free (souphttpsink->proxy_id);
402 g_free (souphttpsink->proxy_pw);
403 if (souphttpsink->proxy)
404 soup_uri_free (souphttpsink->proxy);
405 g_free (souphttpsink->location);
407 g_cond_free (souphttpsink->cond);
408 g_mutex_free (souphttpsink->mutex);
410 G_OBJECT_CLASS (parent_class)->finalize (object);
416 gst_soup_http_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
418 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
419 GstStructure *structure;
420 const GValue *value_array;
423 structure = gst_caps_get_structure (caps, 0);
424 value_array = gst_structure_get_value (structure, "streamheader");
426 free_buffer_list (souphttpsink->streamheader_buffers);
427 souphttpsink->streamheader_buffers = NULL;
429 n = gst_value_array_get_size (value_array);
430 for (i = 0; i < n; i++) {
433 value = gst_value_array_get_value (value_array, i);
434 buffer = GST_BUFFER (gst_value_get_buffer (value));
435 souphttpsink->streamheader_buffers =
436 g_list_append (souphttpsink->streamheader_buffers,
437 gst_buffer_ref (buffer));
445 gst_soup_http_sink_get_times (GstBaseSink * sink, GstBuffer * buffer,
446 GstClockTime * start, GstClockTime * end)
452 thread_func (gpointer ptr)
454 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (ptr);
456 GST_DEBUG ("thread start");
458 souphttpsink->loop = g_main_loop_new (souphttpsink->context, TRUE);
459 g_main_loop_run (souphttpsink->loop);
461 GST_DEBUG ("thread quit");
467 gst_soup_http_sink_start (GstBaseSink * sink)
469 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
471 if (souphttpsink->prop_session) {
472 souphttpsink->session = souphttpsink->prop_session;
474 GError *error = NULL;
476 souphttpsink->context = g_main_context_new ();
478 souphttpsink->thread = g_thread_create (thread_func, souphttpsink,
481 souphttpsink->session =
482 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
483 souphttpsink->context, SOUP_SESSION_USER_AGENT,
484 souphttpsink->user_agent, SOUP_SESSION_TIMEOUT, souphttpsink->timeout,
487 //soup_session_add_feature (souphttpsink->session,
488 // SOUP_SESSION_FEATURE (soup_logger_new (SOUP_LOGGER_LOG_BODY, 100)));
490 g_signal_connect (souphttpsink->session, "authenticate",
491 G_CALLBACK (authenticate), souphttpsink);
498 gst_soup_http_sink_stop (GstBaseSink * sink)
500 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
504 if (souphttpsink->prop_session == NULL) {
505 soup_session_abort (souphttpsink->session);
506 g_object_unref (souphttpsink->session);
509 if (souphttpsink->loop) {
510 g_main_loop_quit (souphttpsink->loop);
511 g_thread_join (souphttpsink->thread);
512 g_main_loop_unref (souphttpsink->loop);
513 souphttpsink->loop = NULL;
515 if (souphttpsink->context) {
516 g_main_context_unref (souphttpsink->context);
517 souphttpsink->context = NULL;
520 gst_soup_http_sink_reset (souphttpsink);
526 gst_soup_http_sink_unlock (GstBaseSink * sink)
528 GST_DEBUG ("unlock");
534 gst_soup_http_sink_event (GstBaseSink * sink, GstEvent * event)
536 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
538 GST_DEBUG_OBJECT (souphttpsink, "event");
540 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
541 GST_DEBUG_OBJECT (souphttpsink, "got eos");
542 g_mutex_lock (souphttpsink->mutex);
543 while (souphttpsink->message) {
544 GST_DEBUG_OBJECT (souphttpsink, "waiting");
545 g_cond_wait (souphttpsink->cond, souphttpsink->mutex);
547 g_mutex_unlock (souphttpsink->mutex);
548 GST_DEBUG_OBJECT (souphttpsink, "finished eos");
555 gst_soup_http_sink_preroll (GstBaseSink * sink, GstBuffer * buffer)
557 GST_DEBUG ("preroll");
563 free_buffer_list (GList * list)
566 for (g = list; g; g = g_list_next (g)) {
567 GstBuffer *buffer = g->data;
568 gst_buffer_unref (buffer);
574 send_message_locked (GstSoupHttpSink * souphttpsink)
579 if (souphttpsink->queued_buffers == NULL || souphttpsink->message) {
583 /* If the URI went away, drop all these buffers */
584 if (souphttpsink->location == NULL) {
585 free_buffer_list (souphttpsink->queued_buffers);
586 souphttpsink->queued_buffers = NULL;
590 souphttpsink->message = soup_message_new ("PUT", souphttpsink->location);
593 if (souphttpsink->offset == 0) {
594 for (g = souphttpsink->streamheader_buffers; g; g = g_list_next (g)) {
595 GstBuffer *buffer = g->data;
596 soup_message_body_append (souphttpsink->message->request_body,
597 SOUP_MEMORY_STATIC, GST_BUFFER_DATA (buffer),
598 GST_BUFFER_SIZE (buffer));
599 n += GST_BUFFER_SIZE (buffer);
603 for (g = souphttpsink->queued_buffers; g; g = g_list_next (g)) {
604 GstBuffer *buffer = g->data;
605 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS)) {
606 soup_message_body_append (souphttpsink->message->request_body,
607 SOUP_MEMORY_STATIC, GST_BUFFER_DATA (buffer),
608 GST_BUFFER_SIZE (buffer));
609 n += GST_BUFFER_SIZE (buffer);
613 if (souphttpsink->offset != 0) {
615 s = g_strdup_printf ("bytes %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "/*",
616 souphttpsink->offset, souphttpsink->offset + n - 1);
617 soup_message_headers_append (souphttpsink->message->request_headers,
623 free_buffer_list (souphttpsink->queued_buffers);
624 souphttpsink->queued_buffers = NULL;
625 g_object_unref (souphttpsink->message);
626 souphttpsink->message = NULL;
630 souphttpsink->sent_buffers = souphttpsink->queued_buffers;
631 souphttpsink->queued_buffers = NULL;
633 GST_DEBUG_OBJECT (souphttpsink,
634 "queue message %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT,
635 souphttpsink->offset, n);
636 soup_session_queue_message (souphttpsink->session, souphttpsink->message,
637 callback, souphttpsink);
639 souphttpsink->offset += n;
643 send_message (GstSoupHttpSink * souphttpsink)
645 g_mutex_lock (souphttpsink->mutex);
646 send_message_locked (souphttpsink);
647 g_mutex_unlock (souphttpsink->mutex);
653 callback (SoupSession * session, SoupMessage * msg, gpointer user_data)
655 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (user_data);
657 GST_DEBUG_OBJECT (souphttpsink, "callback status=%d %s",
658 msg->status_code, msg->reason_phrase);
660 g_mutex_lock (souphttpsink->mutex);
661 g_cond_signal (souphttpsink->cond);
662 souphttpsink->message = NULL;
664 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
665 souphttpsink->status_code = msg->status_code;
666 souphttpsink->reason_phrase = g_strdup (msg->reason_phrase);
667 g_mutex_unlock (souphttpsink->mutex);
671 free_buffer_list (souphttpsink->sent_buffers);
672 souphttpsink->sent_buffers = NULL;
674 send_message_locked (souphttpsink);
675 g_mutex_unlock (souphttpsink->mutex);
679 gst_soup_http_sink_render (GstBaseSink * sink, GstBuffer * buffer)
681 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
685 if (souphttpsink->status_code != 0) {
686 /* FIXME we should allow a moderate amount of retries. */
687 GST_ELEMENT_ERROR (souphttpsink, RESOURCE, WRITE,
688 ("Could not write to HTTP URI"),
689 ("error: %d %s", souphttpsink->status_code,
690 souphttpsink->reason_phrase));
691 return GST_FLOW_ERROR;
694 g_mutex_lock (souphttpsink->mutex);
695 if (souphttpsink->location != NULL) {
696 wake = (souphttpsink->queued_buffers == NULL);
697 souphttpsink->queued_buffers =
698 g_list_append (souphttpsink->queued_buffers, gst_buffer_ref (buffer));
701 source = g_idle_source_new ();
702 g_source_set_callback (source, (GSourceFunc) (send_message),
704 g_source_attach (source, souphttpsink->context);
705 g_source_unref (source);
708 g_mutex_unlock (souphttpsink->mutex);
714 authenticate (SoupSession * session, SoupMessage * msg,
715 SoupAuth * auth, gboolean retrying, gpointer user_data)
717 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (user_data);
720 if (souphttpsink->user_id && souphttpsink->user_pw) {
721 soup_auth_authenticate (auth,
722 souphttpsink->user_id, souphttpsink->user_pw);