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->mutex = g_mutex_new ();
204 souphttpsink->cond = g_cond_new ();
206 souphttpsink->location = NULL;
207 souphttpsink->automatic_redirect = TRUE;
208 souphttpsink->user_agent = g_strdup (DEFAULT_USER_AGENT);
209 souphttpsink->user_id = NULL;
210 souphttpsink->user_pw = NULL;
211 souphttpsink->proxy_id = NULL;
212 souphttpsink->proxy_pw = NULL;
213 souphttpsink->prop_session = NULL;
214 souphttpsink->timeout = 1;
215 proxy = g_getenv ("http_proxy");
216 if (proxy && !gst_soup_http_sink_set_proxy (souphttpsink, proxy)) {
217 GST_WARNING_OBJECT (souphttpsink,
218 "The proxy in the http_proxy env var (\"%s\") cannot be parsed.",
222 gst_soup_http_sink_reset (souphttpsink);
226 gst_soup_http_sink_reset (GstSoupHttpSink * souphttpsink)
228 g_free (souphttpsink->reason_phrase);
229 souphttpsink->reason_phrase = NULL;
230 souphttpsink->status_code = 0;
231 souphttpsink->offset = 0;
236 gst_soup_http_sink_set_proxy (GstSoupHttpSink * souphttpsink, const gchar * uri)
238 if (souphttpsink->proxy) {
239 soup_uri_free (souphttpsink->proxy);
240 souphttpsink->proxy = NULL;
242 if (g_str_has_prefix (uri, "http://")) {
243 souphttpsink->proxy = soup_uri_new (uri);
245 gchar *new_uri = g_strconcat ("http://", uri, NULL);
247 souphttpsink->proxy = soup_uri_new (new_uri);
255 gst_soup_http_sink_set_property (GObject * object, guint property_id,
256 const GValue * value, GParamSpec * pspec)
258 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
260 g_mutex_lock (souphttpsink->mutex);
261 switch (property_id) {
263 if (souphttpsink->prop_session) {
264 g_object_unref (souphttpsink->prop_session);
266 souphttpsink->prop_session = g_value_dup_object (value);
269 g_free (souphttpsink->location);
270 souphttpsink->location = g_value_dup_string (value);
271 souphttpsink->offset = 0;
273 case PROP_USER_AGENT:
274 g_free (souphttpsink->user_agent);
275 souphttpsink->user_agent = g_value_dup_string (value);
277 case PROP_AUTOMATIC_REDIRECT:
278 souphttpsink->automatic_redirect = g_value_get_boolean (value);
281 g_free (souphttpsink->user_id);
282 souphttpsink->user_id = g_value_dup_string (value);
285 g_free (souphttpsink->user_pw);
286 souphttpsink->user_pw = g_value_dup_string (value);
289 g_free (souphttpsink->proxy_id);
290 souphttpsink->proxy_id = g_value_dup_string (value);
293 g_free (souphttpsink->proxy_pw);
294 souphttpsink->proxy_pw = g_value_dup_string (value);
300 proxy = g_value_get_string (value);
303 GST_WARNING ("proxy property cannot be NULL");
306 if (!gst_soup_http_sink_set_proxy (souphttpsink, proxy)) {
307 GST_WARNING ("badly formatted proxy URI");
313 g_strfreev (souphttpsink->cookies);
314 souphttpsink->cookies = g_strdupv (g_value_get_boxed (value));
317 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
321 g_mutex_unlock (souphttpsink->mutex);
325 gst_soup_http_sink_get_property (GObject * object, guint property_id,
326 GValue * value, GParamSpec * pspec)
328 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
330 switch (property_id) {
332 g_value_set_object (value, souphttpsink->prop_session);
335 g_value_set_string (value, souphttpsink->location);
337 case PROP_AUTOMATIC_REDIRECT:
338 g_value_set_boolean (value, souphttpsink->automatic_redirect);
340 case PROP_USER_AGENT:
341 g_value_set_string (value, souphttpsink->user_agent);
344 g_value_set_string (value, souphttpsink->user_id);
347 g_value_set_string (value, souphttpsink->user_pw);
350 g_value_set_string (value, souphttpsink->proxy_id);
353 g_value_set_string (value, souphttpsink->proxy_pw);
356 if (souphttpsink->proxy == NULL)
357 g_value_set_static_string (value, "");
359 char *proxy = soup_uri_to_string (souphttpsink->proxy, FALSE);
361 g_value_set_string (value, proxy);
366 g_value_set_boxed (value, g_strdupv (souphttpsink->cookies));
369 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
375 gst_soup_http_sink_dispose (GObject * object)
377 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
379 /* clean up as possible. may be called multiple times */
380 if (souphttpsink->prop_session)
381 g_object_unref (souphttpsink->prop_session);
382 souphttpsink->prop_session = NULL;
384 G_OBJECT_CLASS (parent_class)->dispose (object);
388 gst_soup_http_sink_finalize (GObject * object)
390 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
392 /* clean up object here */
394 g_free (souphttpsink->user_agent);
395 g_free (souphttpsink->user_id);
396 g_free (souphttpsink->user_pw);
397 g_free (souphttpsink->proxy_id);
398 g_free (souphttpsink->proxy_pw);
399 if (souphttpsink->proxy)
400 soup_uri_free (souphttpsink->proxy);
401 g_free (souphttpsink->location);
403 g_cond_free (souphttpsink->cond);
404 g_mutex_free (souphttpsink->mutex);
406 G_OBJECT_CLASS (parent_class)->finalize (object);
412 gst_soup_http_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
414 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
415 GstStructure *structure;
416 const GValue *value_array;
419 structure = gst_caps_get_structure (caps, 0);
420 value_array = gst_structure_get_value (structure, "streamheader");
422 free_buffer_list (souphttpsink->streamheader_buffers);
423 souphttpsink->streamheader_buffers = NULL;
425 n = gst_value_array_get_size (value_array);
426 for (i = 0; i < n; i++) {
429 value = gst_value_array_get_value (value_array, i);
430 buffer = GST_BUFFER (gst_value_get_buffer (value));
431 souphttpsink->streamheader_buffers =
432 g_list_append (souphttpsink->streamheader_buffers,
433 gst_buffer_ref (buffer));
441 gst_soup_http_sink_get_times (GstBaseSink * sink, GstBuffer * buffer,
442 GstClockTime * start, GstClockTime * end)
448 thread_func (gpointer ptr)
450 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (ptr);
452 GST_DEBUG ("thread start");
454 souphttpsink->loop = g_main_loop_new (souphttpsink->context, TRUE);
455 g_main_loop_run (souphttpsink->loop);
457 GST_DEBUG ("thread quit");
463 gst_soup_http_sink_start (GstBaseSink * sink)
465 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
467 if (souphttpsink->prop_session) {
468 souphttpsink->session = souphttpsink->prop_session;
470 GError *error = NULL;
472 souphttpsink->context = g_main_context_new ();
474 souphttpsink->thread = g_thread_create (thread_func, souphttpsink,
477 souphttpsink->session =
478 soup_session_async_new_with_options (SOUP_SESSION_ASYNC_CONTEXT,
479 souphttpsink->context, SOUP_SESSION_USER_AGENT,
480 souphttpsink->user_agent, SOUP_SESSION_TIMEOUT, souphttpsink->timeout,
483 //soup_session_add_feature (souphttpsink->session,
484 // SOUP_SESSION_FEATURE (soup_logger_new (SOUP_LOGGER_LOG_BODY, 100)));
486 g_signal_connect (souphttpsink->session, "authenticate",
487 G_CALLBACK (authenticate), souphttpsink);
494 gst_soup_http_sink_stop (GstBaseSink * sink)
496 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
500 if (souphttpsink->prop_session == NULL) {
501 soup_session_abort (souphttpsink->session);
502 g_object_unref (souphttpsink->session);
505 if (souphttpsink->loop) {
506 g_main_loop_quit (souphttpsink->loop);
507 g_thread_join (souphttpsink->thread);
508 g_main_loop_unref (souphttpsink->loop);
509 souphttpsink->loop = NULL;
511 if (souphttpsink->context) {
512 g_main_context_unref (souphttpsink->context);
513 souphttpsink->context = NULL;
516 gst_soup_http_sink_reset (souphttpsink);
522 gst_soup_http_sink_unlock (GstBaseSink * sink)
524 GST_DEBUG ("unlock");
530 gst_soup_http_sink_event (GstBaseSink * sink, GstEvent * event)
532 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
534 GST_DEBUG_OBJECT (souphttpsink, "event");
536 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
537 GST_DEBUG_OBJECT (souphttpsink, "got eos");
538 g_mutex_lock (souphttpsink->mutex);
539 while (souphttpsink->message) {
540 GST_DEBUG_OBJECT (souphttpsink, "waiting");
541 g_cond_wait (souphttpsink->cond, souphttpsink->mutex);
543 g_mutex_unlock (souphttpsink->mutex);
544 GST_DEBUG_OBJECT (souphttpsink, "finished eos");
551 gst_soup_http_sink_preroll (GstBaseSink * sink, GstBuffer * buffer)
553 GST_DEBUG ("preroll");
559 free_buffer_list (GList * list)
562 for (g = list; g; g = g_list_next (g)) {
563 GstBuffer *buffer = g->data;
564 gst_buffer_unref (buffer);
570 send_message_locked (GstSoupHttpSink * souphttpsink)
575 if (souphttpsink->queued_buffers == NULL || souphttpsink->message) {
579 /* If the URI went away, drop all these buffers */
580 if (souphttpsink->location == NULL) {
581 free_buffer_list (souphttpsink->queued_buffers);
582 souphttpsink->queued_buffers = NULL;
586 souphttpsink->message = soup_message_new ("PUT", souphttpsink->location);
589 if (souphttpsink->offset == 0) {
590 for (g = souphttpsink->streamheader_buffers; g; g = g_list_next (g)) {
591 GstBuffer *buffer = g->data;
592 soup_message_body_append (souphttpsink->message->request_body,
593 SOUP_MEMORY_STATIC, GST_BUFFER_DATA (buffer),
594 GST_BUFFER_SIZE (buffer));
595 n += GST_BUFFER_SIZE (buffer);
599 for (g = souphttpsink->queued_buffers; g; g = g_list_next (g)) {
600 GstBuffer *buffer = g->data;
601 if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS)) {
602 soup_message_body_append (souphttpsink->message->request_body,
603 SOUP_MEMORY_STATIC, GST_BUFFER_DATA (buffer),
604 GST_BUFFER_SIZE (buffer));
605 n += GST_BUFFER_SIZE (buffer);
609 if (souphttpsink->offset != 0) {
611 s = g_strdup_printf ("bytes %" G_GUINT64_FORMAT "-%" G_GUINT64_FORMAT "/*",
612 souphttpsink->offset, souphttpsink->offset + n - 1);
613 soup_message_headers_append (souphttpsink->message->request_headers,
619 free_buffer_list (souphttpsink->queued_buffers);
620 souphttpsink->queued_buffers = NULL;
621 g_object_unref (souphttpsink->message);
622 souphttpsink->message = NULL;
626 souphttpsink->sent_buffers = souphttpsink->queued_buffers;
627 souphttpsink->queued_buffers = NULL;
629 GST_DEBUG_OBJECT (souphttpsink,
630 "queue message %" G_GUINT64_FORMAT " %" G_GUINT64_FORMAT,
631 souphttpsink->offset, n);
632 soup_session_queue_message (souphttpsink->session, souphttpsink->message,
633 callback, souphttpsink);
635 souphttpsink->offset += n;
639 send_message (GstSoupHttpSink * souphttpsink)
641 g_mutex_lock (souphttpsink->mutex);
642 send_message_locked (souphttpsink);
643 g_mutex_unlock (souphttpsink->mutex);
649 callback (SoupSession * session, SoupMessage * msg, gpointer user_data)
651 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (user_data);
653 GST_DEBUG_OBJECT (souphttpsink, "callback status=%d %s",
654 msg->status_code, msg->reason_phrase);
656 g_mutex_lock (souphttpsink->mutex);
657 g_cond_signal (souphttpsink->cond);
658 souphttpsink->message = NULL;
660 if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) {
661 souphttpsink->status_code = msg->status_code;
662 souphttpsink->reason_phrase = g_strdup (msg->reason_phrase);
663 g_mutex_unlock (souphttpsink->mutex);
667 free_buffer_list (souphttpsink->sent_buffers);
668 souphttpsink->sent_buffers = NULL;
670 send_message_locked (souphttpsink);
671 g_mutex_unlock (souphttpsink->mutex);
675 gst_soup_http_sink_render (GstBaseSink * sink, GstBuffer * buffer)
677 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
681 if (souphttpsink->status_code != 0) {
682 /* FIXME we should allow a moderate amount of retries. */
683 GST_ELEMENT_ERROR (souphttpsink, RESOURCE, WRITE,
684 ("Could not write to HTTP URI"),
685 ("error: %d %s", souphttpsink->status_code,
686 souphttpsink->reason_phrase));
687 return GST_FLOW_ERROR;
690 g_mutex_lock (souphttpsink->mutex);
691 if (souphttpsink->location != NULL) {
692 wake = (souphttpsink->queued_buffers == NULL);
693 souphttpsink->queued_buffers =
694 g_list_append (souphttpsink->queued_buffers, gst_buffer_ref (buffer));
697 source = g_idle_source_new ();
698 g_source_set_callback (source, (GSourceFunc) (send_message),
700 g_source_attach (source, souphttpsink->context);
701 g_source_unref (source);
704 g_mutex_unlock (souphttpsink->mutex);
710 authenticate (SoupSession * session, SoupMessage * msg,
711 SoupAuth * auth, gboolean retrying, gpointer user_data)
713 GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (user_data);
716 if (souphttpsink->user_id && souphttpsink->user_pw) {
717 soup_auth_authenticate (auth,
718 souphttpsink->user_id, souphttpsink->user_pw);