souphttpsink: don't create unused second sink pad object
[platform/upstream/gstreamer.git] / ext / soup / gstsouphttpsink.c
1 /* GStreamer
2  * Copyright (C) 2011 David Schleef <ds@entropywave.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 /**
20  * SECTION:element-gstsouphttpsink
21  *
22  * The souphttpsink element sends pipeline data to an HTTP server
23  * using HTTP PUT commands.
24  *
25  * <refsect2>
26  * <title>Example launch line</title>
27  * |[
28  * gst-launch -v videotestsrc num-buffers=300 ! theoraenc ! oggmux !
29  *   souphttpsink location=http://server/filename.ogv
30  * ]|
31  * 
32  * This example encodes 10 seconds of video and sends it to the HTTP
33  * server "server" using HTTP PUT commands.
34  * </refsect2>
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <gst/gst.h>
42 #include <gst/base/gstbasesink.h>
43 #include "gstsouphttpsink.h"
44
45 GST_DEBUG_CATEGORY_STATIC (gst_soup_http_sink_debug_category);
46 #define GST_CAT_DEFAULT gst_soup_http_sink_debug_category
47
48 /* prototypes */
49
50
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);
57
58 static gboolean gst_soup_http_sink_set_caps (GstBaseSink * sink,
59     GstCaps * caps);
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);
66 static GstFlowReturn
67 gst_soup_http_sink_preroll (GstBaseSink * sink, GstBuffer * buffer);
68 static GstFlowReturn
69 gst_soup_http_sink_render (GstBaseSink * sink, GstBuffer * buffer);
70
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);
75 static void
76 callback (SoupSession * session, SoupMessage * msg, gpointer user_data);
77 static gboolean
78 gst_soup_http_sink_set_proxy (GstSoupHttpSink * souphttpsink,
79     const gchar * uri);
80
81 enum
82 {
83   PROP_0,
84   PROP_LOCATION,
85   PROP_USER_AGENT,
86   PROP_AUTOMATIC_REDIRECT,
87   PROP_PROXY,
88   PROP_USER_ID,
89   PROP_USER_PW,
90   PROP_PROXY_ID,
91   PROP_PROXY_PW,
92   PROP_COOKIES,
93   PROP_SESSION
94 };
95
96 #define DEFAULT_USER_AGENT           "GStreamer souphttpsink "
97
98 /* pad templates */
99
100 static GstStaticPadTemplate gst_soup_http_sink_sink_template =
101 GST_STATIC_PAD_TEMPLATE ("sink",
102     GST_PAD_SINK,
103     GST_PAD_ALWAYS,
104     GST_STATIC_CAPS_ANY);
105
106
107 /* class initialization */
108
109 #define DEBUG_INIT(bla) \
110   GST_DEBUG_CATEGORY_INIT (gst_soup_http_sink_debug_category, "souphttpsink", 0, \
111       "debug category for souphttpsink element");
112
113 GST_BOILERPLATE_FULL (GstSoupHttpSink, gst_soup_http_sink, GstBaseSink,
114     GST_TYPE_BASE_SINK, DEBUG_INIT);
115
116 static void
117 gst_soup_http_sink_base_init (gpointer g_class)
118 {
119   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
120
121   gst_element_class_add_pad_template (element_class,
122       gst_static_pad_template_get (&gst_soup_http_sink_sink_template));
123
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>");
127 }
128
129 static void
130 gst_soup_http_sink_class_init (GstSoupHttpSinkClass * klass)
131 {
132   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133   GstBaseSinkClass *base_sink_class = GST_BASE_SINK_CLASS (klass);
134
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);
140   if (0)
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);
147   if (0)
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);
150
151   g_object_class_install_property (gobject_class,
152       PROP_LOCATION,
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,
156       PROP_USER_AGENT,
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,
166       PROP_PROXY,
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,
171       PROP_USER_ID,
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));
194
195 }
196
197 static void
198 gst_soup_http_sink_init (GstSoupHttpSink * souphttpsink,
199     GstSoupHttpSinkClass * souphttpsink_class)
200 {
201   const char *proxy;
202
203   souphttpsink->mutex = g_mutex_new ();
204   souphttpsink->cond = g_cond_new ();
205
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.",
219         proxy);
220   }
221
222   gst_soup_http_sink_reset (souphttpsink);
223 }
224
225 static void
226 gst_soup_http_sink_reset (GstSoupHttpSink * souphttpsink)
227 {
228   g_free (souphttpsink->reason_phrase);
229   souphttpsink->reason_phrase = NULL;
230   souphttpsink->status_code = 0;
231   souphttpsink->offset = 0;
232
233 }
234
235 static gboolean
236 gst_soup_http_sink_set_proxy (GstSoupHttpSink * souphttpsink, const gchar * uri)
237 {
238   if (souphttpsink->proxy) {
239     soup_uri_free (souphttpsink->proxy);
240     souphttpsink->proxy = NULL;
241   }
242   if (g_str_has_prefix (uri, "http://")) {
243     souphttpsink->proxy = soup_uri_new (uri);
244   } else {
245     gchar *new_uri = g_strconcat ("http://", uri, NULL);
246
247     souphttpsink->proxy = soup_uri_new (new_uri);
248     g_free (new_uri);
249   }
250
251   return TRUE;
252 }
253
254 void
255 gst_soup_http_sink_set_property (GObject * object, guint property_id,
256     const GValue * value, GParamSpec * pspec)
257 {
258   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
259
260   g_mutex_lock (souphttpsink->mutex);
261   switch (property_id) {
262     case PROP_SESSION:
263       if (souphttpsink->prop_session) {
264         g_object_unref (souphttpsink->prop_session);
265       }
266       souphttpsink->prop_session = g_value_dup_object (value);
267       break;
268     case PROP_LOCATION:
269       g_free (souphttpsink->location);
270       souphttpsink->location = g_value_dup_string (value);
271       souphttpsink->offset = 0;
272       break;
273     case PROP_USER_AGENT:
274       g_free (souphttpsink->user_agent);
275       souphttpsink->user_agent = g_value_dup_string (value);
276       break;
277     case PROP_AUTOMATIC_REDIRECT:
278       souphttpsink->automatic_redirect = g_value_get_boolean (value);
279       break;
280     case PROP_USER_ID:
281       g_free (souphttpsink->user_id);
282       souphttpsink->user_id = g_value_dup_string (value);
283       break;
284     case PROP_USER_PW:
285       g_free (souphttpsink->user_pw);
286       souphttpsink->user_pw = g_value_dup_string (value);
287       break;
288     case PROP_PROXY_ID:
289       g_free (souphttpsink->proxy_id);
290       souphttpsink->proxy_id = g_value_dup_string (value);
291       break;
292     case PROP_PROXY_PW:
293       g_free (souphttpsink->proxy_pw);
294       souphttpsink->proxy_pw = g_value_dup_string (value);
295       break;
296     case PROP_PROXY:
297     {
298       const gchar *proxy;
299
300       proxy = g_value_get_string (value);
301
302       if (proxy == NULL) {
303         GST_WARNING ("proxy property cannot be NULL");
304         goto done;
305       }
306       if (!gst_soup_http_sink_set_proxy (souphttpsink, proxy)) {
307         GST_WARNING ("badly formatted proxy URI");
308         goto done;
309       }
310       break;
311     }
312     case PROP_COOKIES:
313       g_strfreev (souphttpsink->cookies);
314       souphttpsink->cookies = g_strdupv (g_value_get_boxed (value));
315       break;
316     default:
317       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
318       break;
319   }
320 done:
321   g_mutex_unlock (souphttpsink->mutex);
322 }
323
324 void
325 gst_soup_http_sink_get_property (GObject * object, guint property_id,
326     GValue * value, GParamSpec * pspec)
327 {
328   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
329
330   switch (property_id) {
331     case PROP_SESSION:
332       g_value_set_object (value, souphttpsink->prop_session);
333       break;
334     case PROP_LOCATION:
335       g_value_set_string (value, souphttpsink->location);
336       break;
337     case PROP_AUTOMATIC_REDIRECT:
338       g_value_set_boolean (value, souphttpsink->automatic_redirect);
339       break;
340     case PROP_USER_AGENT:
341       g_value_set_string (value, souphttpsink->user_agent);
342       break;
343     case PROP_USER_ID:
344       g_value_set_string (value, souphttpsink->user_id);
345       break;
346     case PROP_USER_PW:
347       g_value_set_string (value, souphttpsink->user_pw);
348       break;
349     case PROP_PROXY_ID:
350       g_value_set_string (value, souphttpsink->proxy_id);
351       break;
352     case PROP_PROXY_PW:
353       g_value_set_string (value, souphttpsink->proxy_pw);
354       break;
355     case PROP_PROXY:
356       if (souphttpsink->proxy == NULL)
357         g_value_set_static_string (value, "");
358       else {
359         char *proxy = soup_uri_to_string (souphttpsink->proxy, FALSE);
360
361         g_value_set_string (value, proxy);
362         g_free (proxy);
363       }
364       break;
365     case PROP_COOKIES:
366       g_value_set_boxed (value, g_strdupv (souphttpsink->cookies));
367       break;
368     default:
369       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
370       break;
371   }
372 }
373
374 void
375 gst_soup_http_sink_dispose (GObject * object)
376 {
377   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
378
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;
383
384   G_OBJECT_CLASS (parent_class)->dispose (object);
385 }
386
387 void
388 gst_soup_http_sink_finalize (GObject * object)
389 {
390   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (object);
391
392   /* clean up object here */
393
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);
402
403   g_cond_free (souphttpsink->cond);
404   g_mutex_free (souphttpsink->mutex);
405
406   G_OBJECT_CLASS (parent_class)->finalize (object);
407 }
408
409
410
411 static gboolean
412 gst_soup_http_sink_set_caps (GstBaseSink * sink, GstCaps * caps)
413 {
414   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
415   GstStructure *structure;
416   const GValue *value_array;
417   int i, n;
418
419   structure = gst_caps_get_structure (caps, 0);
420   value_array = gst_structure_get_value (structure, "streamheader");
421   if (value_array) {
422     free_buffer_list (souphttpsink->streamheader_buffers);
423     souphttpsink->streamheader_buffers = NULL;
424
425     n = gst_value_array_get_size (value_array);
426     for (i = 0; i < n; i++) {
427       const GValue *value;
428       GstBuffer *buffer;
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));
434     }
435   }
436
437   return TRUE;
438 }
439
440 static void
441 gst_soup_http_sink_get_times (GstBaseSink * sink, GstBuffer * buffer,
442     GstClockTime * start, GstClockTime * end)
443 {
444
445 }
446
447 static gpointer
448 thread_func (gpointer ptr)
449 {
450   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (ptr);
451
452   GST_DEBUG ("thread start");
453
454   souphttpsink->loop = g_main_loop_new (souphttpsink->context, TRUE);
455   g_main_loop_run (souphttpsink->loop);
456
457   GST_DEBUG ("thread quit");
458
459   return NULL;
460 }
461
462 static gboolean
463 gst_soup_http_sink_start (GstBaseSink * sink)
464 {
465   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
466
467   if (souphttpsink->prop_session) {
468     souphttpsink->session = souphttpsink->prop_session;
469   } else {
470     GError *error = NULL;
471
472     souphttpsink->context = g_main_context_new ();
473
474     souphttpsink->thread = g_thread_create (thread_func, souphttpsink,
475         TRUE, &error);
476
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,
481         NULL);
482
483     //soup_session_add_feature (souphttpsink->session,
484     //    SOUP_SESSION_FEATURE (soup_logger_new (SOUP_LOGGER_LOG_BODY, 100)));
485
486     g_signal_connect (souphttpsink->session, "authenticate",
487         G_CALLBACK (authenticate), souphttpsink);
488   }
489
490   return TRUE;
491 }
492
493 static gboolean
494 gst_soup_http_sink_stop (GstBaseSink * sink)
495 {
496   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
497
498   GST_DEBUG ("stop");
499
500   if (souphttpsink->prop_session == NULL) {
501     soup_session_abort (souphttpsink->session);
502     g_object_unref (souphttpsink->session);
503   }
504
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;
510   }
511   if (souphttpsink->context) {
512     g_main_context_unref (souphttpsink->context);
513     souphttpsink->context = NULL;
514   }
515
516   gst_soup_http_sink_reset (souphttpsink);
517
518   return TRUE;
519 }
520
521 static gboolean
522 gst_soup_http_sink_unlock (GstBaseSink * sink)
523 {
524   GST_DEBUG ("unlock");
525
526   return TRUE;
527 }
528
529 static gboolean
530 gst_soup_http_sink_event (GstBaseSink * sink, GstEvent * event)
531 {
532   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
533
534   GST_DEBUG_OBJECT (souphttpsink, "event");
535
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);
542     }
543     g_mutex_unlock (souphttpsink->mutex);
544     GST_DEBUG_OBJECT (souphttpsink, "finished eos");
545   }
546
547   return TRUE;
548 }
549
550 static GstFlowReturn
551 gst_soup_http_sink_preroll (GstBaseSink * sink, GstBuffer * buffer)
552 {
553   GST_DEBUG ("preroll");
554
555   return GST_FLOW_OK;
556 }
557
558 static void
559 free_buffer_list (GList * list)
560 {
561   GList *g;
562   for (g = list; g; g = g_list_next (g)) {
563     GstBuffer *buffer = g->data;
564     gst_buffer_unref (buffer);
565   }
566   g_list_free (list);
567 }
568
569 static void
570 send_message_locked (GstSoupHttpSink * souphttpsink)
571 {
572   GList *g;
573   guint64 n;
574
575   if (souphttpsink->queued_buffers == NULL || souphttpsink->message) {
576     return;
577   }
578
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;
583     return;
584   }
585
586   souphttpsink->message = soup_message_new ("PUT", souphttpsink->location);
587
588   n = 0;
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);
596     }
597   }
598
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);
606     }
607   }
608
609   if (souphttpsink->offset != 0) {
610     char *s;
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,
614         "Content-Range", s);
615     g_free (s);
616   }
617
618   if (n == 0) {
619     free_buffer_list (souphttpsink->queued_buffers);
620     souphttpsink->queued_buffers = NULL;
621     g_object_unref (souphttpsink->message);
622     souphttpsink->message = NULL;
623     return;
624   }
625
626   souphttpsink->sent_buffers = souphttpsink->queued_buffers;
627   souphttpsink->queued_buffers = NULL;
628
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);
634
635   souphttpsink->offset += n;
636 }
637
638 static gboolean
639 send_message (GstSoupHttpSink * souphttpsink)
640 {
641   g_mutex_lock (souphttpsink->mutex);
642   send_message_locked (souphttpsink);
643   g_mutex_unlock (souphttpsink->mutex);
644
645   return FALSE;
646 }
647
648 static void
649 callback (SoupSession * session, SoupMessage * msg, gpointer user_data)
650 {
651   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (user_data);
652
653   GST_DEBUG_OBJECT (souphttpsink, "callback status=%d %s",
654       msg->status_code, msg->reason_phrase);
655
656   g_mutex_lock (souphttpsink->mutex);
657   g_cond_signal (souphttpsink->cond);
658   souphttpsink->message = NULL;
659
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);
664     return;
665   }
666
667   free_buffer_list (souphttpsink->sent_buffers);
668   souphttpsink->sent_buffers = NULL;
669
670   send_message_locked (souphttpsink);
671   g_mutex_unlock (souphttpsink->mutex);
672 }
673
674 static GstFlowReturn
675 gst_soup_http_sink_render (GstBaseSink * sink, GstBuffer * buffer)
676 {
677   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (sink);
678   GSource *source;
679   gboolean wake;
680
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;
688   }
689
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));
695
696     if (wake) {
697       source = g_idle_source_new ();
698       g_source_set_callback (source, (GSourceFunc) (send_message),
699           souphttpsink, NULL);
700       g_source_attach (source, souphttpsink->context);
701       g_source_unref (source);
702     }
703   }
704   g_mutex_unlock (souphttpsink->mutex);
705
706   return GST_FLOW_OK;
707 }
708
709 static void
710 authenticate (SoupSession * session, SoupMessage * msg,
711     SoupAuth * auth, gboolean retrying, gpointer user_data)
712 {
713   GstSoupHttpSink *souphttpsink = GST_SOUP_HTTP_SINK (user_data);
714
715   if (!retrying) {
716     if (souphttpsink->user_id && souphttpsink->user_pw) {
717       soup_auth_authenticate (auth,
718           souphttpsink->user_id, souphttpsink->user_pw);
719     }
720   }
721 }