2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4 * Copyright (C) 2006 Wim Taymans <wim at fluendo dot com>
5 * Copyright (C) <2011> Collabora Ltd.
6 * Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
25 * SECTION:element-multihandlesink
26 * @see_also: tcpserversink
28 * This plugin writes incoming data to a set of file descriptors. The
29 * file descriptors can be added to multihandlesink by emitting the #GstMultiHandleSink::add signal.
30 * For each descriptor added, the #GstMultiHandleSink::client-added signal will be called.
32 * As of version 0.10.8, a client can also be added with the #GstMultiHandleSink::add-full signal
33 * that allows for more control over what and how much data a client
36 * Clients can be removed from multihandlesink by emitting the #GstMultiHandleSink::remove signal. For
37 * each descriptor removed, the #GstMultiHandleSink::client-removed signal will be called. The
38 * #GstMultiHandleSink::client-removed signal can also be fired when multihandlesink decides that a
39 * client is not active anymore or, depending on the value of the
40 * #GstMultiHandleSink:recover-policy property, if the client is reading too slowly.
41 * In all cases, multihandlesink will never close a file descriptor itself.
42 * The user of multihandlesink is responsible for closing all file descriptors.
43 * This can for example be done in response to the #GstMultiHandleSink::client-fd-removed signal.
44 * Note that multihandlesink still has a reference to the file descriptor when the
45 * #GstMultiHandleSink::client-removed signal is emitted, so that "get-stats" can be performed on
46 * the descriptor; it is therefore not safe to close the file descriptor in
47 * the #GstMultiHandleSink::client-removed signal handler, and you should use the
48 * #GstMultiHandleSink::client-fd-removed signal to safely close the fd.
50 * Multisocketsink internally keeps a queue of the incoming buffers and uses a
51 * separate thread to send the buffers to the clients. This ensures that no
52 * client write can block the pipeline and that clients can read with different
55 * When adding a client to multihandlesink, the #GstMultiHandleSink:sync-method property will define
56 * which buffer in the queued buffers will be sent first to the client. Clients
57 * can be sent the most recent buffer (which might not be decodable by the
58 * client if it is not a keyframe), the next keyframe received in
59 * multihandlesink (which can take some time depending on the keyframe rate), or the
60 * last received keyframe (which will cause a simple burst-on-connect).
61 * Multisocketsink will always keep at least one keyframe in its internal buffers
62 * when the sync-mode is set to latest-keyframe.
64 * As of version 0.10.8, there are additional values for the #GstMultiHandleSink:sync-method
65 * property to allow finer control over burst-on-connect behaviour. By selecting
66 * the 'burst' method a minimum burst size can be chosen, 'burst-keyframe'
67 * additionally requires that the burst begin with a keyframe, and
68 * 'burst-with-keyframe' attempts to burst beginning with a keyframe, but will
69 * prefer a minimum burst size even if it requires not starting with a keyframe.
71 * Multisocketsink can be instructed to keep at least a minimum amount of data
72 * expressed in time or byte units in its internal queues with the
73 * #GstMultiHandleSink:time-min and #GstMultiHandleSink:bytes-min properties respectively.
74 * These properties are useful if the application adds clients with the
75 * #GstMultiHandleSink::add-full signal to make sure that a burst connect can
76 * actually be honored.
78 * When streaming data, clients are allowed to read at a different rate than
79 * the rate at which multihandlesink receives data. If the client is reading too
80 * fast, no data will be send to the client until multihandlesink receives more
81 * data. If the client, however, reads too slowly, data for that client will be
82 * queued up in multihandlesink. Two properties control the amount of data
83 * (buffers) that is queued in multihandlesink: #GstMultiHandleSink:buffers-max and
84 * #GstMultiHandleSink:buffers-soft-max. A client that falls behind by
85 * #GstMultiHandleSink:buffers-max is removed from multihandlesink forcibly.
87 * A client with a lag of at least #GstMultiHandleSink:buffers-soft-max enters the recovery
88 * procedure which is controlled with the #GstMultiHandleSink:recover-policy property.
89 * A recover policy of NONE will do nothing, RESYNC_LATEST will send the most recently
90 * received buffer as the next buffer for the client, RESYNC_SOFT_LIMIT
91 * positions the client to the soft limit in the buffer queue and
92 * RESYNC_KEYFRAME positions the client at the most recent keyframe in the
95 * multihandlesink will by default synchronize on the clock before serving the
96 * buffers to the clients. This behaviour can be disabled by setting the sync
97 * property to FALSE. Multisocketsink will by default not do QoS and will never
100 * Last reviewed on 2006-09-12 (0.10.10)
107 #include <gst/gst-i18n-plugin.h>
109 #include "gstmultihandlesink.h"
110 #include "gsttcp-marshal.h"
113 #include <netinet/in.h>
116 #define NOT_IMPLEMENTED 0
118 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
121 GST_STATIC_CAPS_ANY);
123 GST_DEBUG_CATEGORY_STATIC (multihandlesink_debug);
124 #define GST_CAT_DEFAULT (multihandlesink_debug)
126 /* MultiHandleSink signals and args */
129 GST_MULTI_SINK_LAST_SIGNAL,
138 SIGNAL_CLIENT_REMOVED,
139 SIGNAL_CLIENT_SOCKET_REMOVED,
145 /* this is really arbitrarily chosen */
146 #define DEFAULT_BUFFERS_MAX -1
147 #define DEFAULT_BUFFERS_SOFT_MAX -1
148 #define DEFAULT_TIME_MIN -1
149 #define DEFAULT_BYTES_MIN -1
150 #define DEFAULT_BUFFERS_MIN -1
151 #define DEFAULT_UNIT_FORMAT GST_FORMAT_BUFFERS
152 #define DEFAULT_UNITS_MAX -1
153 #define DEFAULT_UNITS_SOFT_MAX -1
154 #define DEFAULT_RECOVER_POLICY GST_RECOVER_POLICY_NONE
155 #define DEFAULT_TIMEOUT 0
156 #define DEFAULT_SYNC_METHOD GST_SYNC_METHOD_LATEST
158 #define DEFAULT_BURST_FORMAT GST_FORMAT_UNDEFINED
159 #define DEFAULT_BURST_VALUE 0
161 #define DEFAULT_QOS_DSCP -1
163 #define DEFAULT_RESEND_STREAMHEADER TRUE
177 PROP_BUFFERS_SOFT_MAX,
194 PROP_RESEND_STREAMHEADER,
202 gst_multi_handle_sink_recover_policy_get_type (void)
204 static GType recover_policy_type = 0;
205 static const GEnumValue recover_policy[] = {
206 {GST_RECOVER_POLICY_NONE,
207 "Do not try to recover", "none"},
208 {GST_RECOVER_POLICY_RESYNC_LATEST,
209 "Resync client to latest buffer", "latest"},
210 {GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT,
211 "Resync client to soft limit", "soft-limit"},
212 {GST_RECOVER_POLICY_RESYNC_KEYFRAME,
213 "Resync client to most recent keyframe", "keyframe"},
217 if (!recover_policy_type) {
218 recover_policy_type =
219 g_enum_register_static ("GstMultiHandleSinkRecoverPolicy",
222 return recover_policy_type;
226 gst_multi_handle_sink_sync_method_get_type (void)
228 static GType sync_method_type = 0;
229 static const GEnumValue sync_method[] = {
230 {GST_SYNC_METHOD_LATEST,
231 "Serve starting from the latest buffer", "latest"},
232 {GST_SYNC_METHOD_NEXT_KEYFRAME,
233 "Serve starting from the next keyframe", "next-keyframe"},
234 {GST_SYNC_METHOD_LATEST_KEYFRAME,
235 "Serve everything since the latest keyframe (burst)",
237 {GST_SYNC_METHOD_BURST, "Serve burst-value data to client", "burst"},
238 {GST_SYNC_METHOD_BURST_KEYFRAME,
239 "Serve burst-value data starting on a keyframe",
241 {GST_SYNC_METHOD_BURST_WITH_KEYFRAME,
242 "Serve burst-value data preferably starting on a keyframe",
243 "burst-with-keyframe"},
247 if (!sync_method_type) {
249 g_enum_register_static ("GstMultiHandleSinkSyncMethod", sync_method);
251 return sync_method_type;
255 gst_multi_handle_sink_client_status_get_type (void)
257 static GType client_status_type = 0;
258 static const GEnumValue client_status[] = {
259 {GST_CLIENT_STATUS_OK, "ok", "ok"},
260 {GST_CLIENT_STATUS_CLOSED, "Closed", "closed"},
261 {GST_CLIENT_STATUS_REMOVED, "Removed", "removed"},
262 {GST_CLIENT_STATUS_SLOW, "Too slow", "slow"},
263 {GST_CLIENT_STATUS_ERROR, "Error", "error"},
264 {GST_CLIENT_STATUS_DUPLICATE, "Duplicate", "duplicate"},
265 {GST_CLIENT_STATUS_FLUSHING, "Flushing", "flushing"},
269 if (!client_status_type) {
271 g_enum_register_static ("GstMultiHandleSinkClientStatus",
274 return client_status_type;
277 static void gst_multi_handle_sink_finalize (GObject * object);
278 static void gst_multi_handle_sink_clear (GstMultiHandleSink * mhsink);
280 static GstFlowReturn gst_multi_handle_sink_render (GstBaseSink * bsink,
282 static void gst_multi_handle_sink_queue_buffer (GstMultiHandleSink * mhsink,
284 static gboolean gst_multi_handle_sink_client_queue_buffer (GstMultiHandleSink *
285 mhsink, GstMultiHandleClient * mhclient, GstBuffer * buffer);
286 static GstStateChangeReturn gst_multi_handle_sink_change_state (GstElement *
287 element, GstStateChange transition);
289 static void gst_multi_handle_sink_set_property (GObject * object, guint prop_id,
290 const GValue * value, GParamSpec * pspec);
291 static void gst_multi_handle_sink_get_property (GObject * object, guint prop_id,
292 GValue * value, GParamSpec * pspec);
294 #define gst_multi_handle_sink_parent_class parent_class
295 G_DEFINE_TYPE (GstMultiHandleSink, gst_multi_handle_sink, GST_TYPE_BASE_SINK);
297 static guint gst_multi_handle_sink_signals[LAST_SIGNAL] = { 0 };
300 find_syncframe (GstMultiHandleSink * sink, gint idx, gint direction);
301 #define find_next_syncframe(s,i) find_syncframe(s,i,1)
302 #define find_prev_syncframe(s,i) find_syncframe(s,i,-1)
303 static gboolean is_sync_frame (GstMultiHandleSink * sink, GstBuffer * buffer);
304 static gboolean gst_multi_handle_sink_stop (GstBaseSink * bsink);
305 static gboolean gst_multi_handle_sink_start (GstBaseSink * bsink);
306 static gint get_buffers_max (GstMultiHandleSink * sink, gint64 max);
308 gst_multi_handle_sink_recover_client (GstMultiHandleSink * sink,
309 GstMultiHandleClient * client);
310 static void gst_multi_handle_sink_setup_dscp (GstMultiHandleSink * mhsink);
312 find_limits (GstMultiHandleSink * sink,
313 gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
314 gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max);
318 gst_multi_handle_sink_class_init (GstMultiHandleSinkClass * klass)
320 GObjectClass *gobject_class;
321 GstElementClass *gstelement_class;
322 GstBaseSinkClass *gstbasesink_class;
324 gobject_class = (GObjectClass *) klass;
325 gstelement_class = (GstElementClass *) klass;
326 gstbasesink_class = (GstBaseSinkClass *) klass;
328 gobject_class->set_property = gst_multi_handle_sink_set_property;
329 gobject_class->get_property = gst_multi_handle_sink_get_property;
330 gobject_class->finalize = gst_multi_handle_sink_finalize;
332 g_object_class_install_property (gobject_class, PROP_BUFFERS_MAX,
333 g_param_spec_int ("buffers-max", "Buffers max",
334 "max number of buffers to queue for a client (-1 = no limit)", -1,
335 G_MAXINT, DEFAULT_BUFFERS_MAX,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_BUFFERS_SOFT_MAX,
338 g_param_spec_int ("buffers-soft-max", "Buffers soft max",
339 "Recover client when going over this limit (-1 = no limit)", -1,
340 G_MAXINT, DEFAULT_BUFFERS_SOFT_MAX,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_BYTES_MIN,
344 g_param_spec_int ("bytes-min", "Bytes min",
345 "min number of bytes to queue (-1 = as little as possible)", -1,
346 G_MAXINT, DEFAULT_BYTES_MIN,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
348 g_object_class_install_property (gobject_class, PROP_TIME_MIN,
349 g_param_spec_int64 ("time-min", "Time min",
350 "min number of time to queue (-1 = as little as possible)", -1,
351 G_MAXINT64, DEFAULT_TIME_MIN,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
353 g_object_class_install_property (gobject_class, PROP_BUFFERS_MIN,
354 g_param_spec_int ("buffers-min", "Buffers min",
355 "min number of buffers to queue (-1 = as few as possible)", -1,
356 G_MAXINT, DEFAULT_BUFFERS_MIN,
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 g_object_class_install_property (gobject_class, PROP_UNIT_FORMAT,
360 g_param_spec_enum ("unit-format", "Units format",
361 "The unit to measure the max/soft-max/queued properties",
362 GST_TYPE_FORMAT, DEFAULT_UNIT_FORMAT,
363 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
364 g_object_class_install_property (gobject_class, PROP_UNITS_MAX,
365 g_param_spec_int64 ("units-max", "Units max",
366 "max number of units to queue (-1 = no limit)", -1, G_MAXINT64,
367 DEFAULT_UNITS_MAX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 g_object_class_install_property (gobject_class, PROP_UNITS_SOFT_MAX,
369 g_param_spec_int64 ("units-soft-max", "Units soft max",
370 "Recover client when going over this limit (-1 = no limit)", -1,
371 G_MAXINT64, DEFAULT_UNITS_SOFT_MAX,
372 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 g_object_class_install_property (gobject_class, PROP_BUFFERS_QUEUED,
375 g_param_spec_uint ("buffers-queued", "Buffers queued",
376 "Number of buffers currently queued", 0, G_MAXUINT, 0,
377 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
379 g_object_class_install_property (gobject_class, PROP_BYTES_QUEUED,
380 g_param_spec_uint ("bytes-queued", "Bytes queued",
381 "Number of bytes currently queued", 0, G_MAXUINT, 0,
382 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_TIME_QUEUED,
384 g_param_spec_uint64 ("time-queued", "Time queued",
385 "Number of time currently queued", 0, G_MAXUINT64, 0,
386 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
389 g_object_class_install_property (gobject_class, PROP_RECOVER_POLICY,
390 g_param_spec_enum ("recover-policy", "Recover Policy",
391 "How to recover when client reaches the soft max",
392 GST_TYPE_RECOVER_POLICY, DEFAULT_RECOVER_POLICY,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
395 g_param_spec_uint64 ("timeout", "Timeout",
396 "Maximum inactivity timeout in nanoseconds for a client (0 = no limit)",
397 0, G_MAXUINT64, DEFAULT_TIMEOUT,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 g_object_class_install_property (gobject_class, PROP_SYNC_METHOD,
400 g_param_spec_enum ("sync-method", "Sync Method",
401 "How to sync new clients to the stream", GST_TYPE_SYNC_METHOD,
402 DEFAULT_SYNC_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 g_object_class_install_property (gobject_class, PROP_BYTES_TO_SERVE,
404 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
405 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
406 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
407 g_object_class_install_property (gobject_class, PROP_BYTES_SERVED,
408 g_param_spec_uint64 ("bytes-served", "Bytes served",
409 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
410 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_BURST_FORMAT,
413 g_param_spec_enum ("burst-format", "Burst format",
414 "The format of the burst units (when sync-method is burst[[-with]-keyframe])",
415 GST_TYPE_FORMAT, DEFAULT_BURST_FORMAT,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 g_object_class_install_property (gobject_class, PROP_BURST_VALUE,
418 g_param_spec_uint64 ("burst-value", "Burst value",
419 "The amount of burst expressed in burst-unit", 0, G_MAXUINT64,
420 DEFAULT_BURST_VALUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422 g_object_class_install_property (gobject_class, PROP_QOS_DSCP,
423 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
424 "Quality of Service, differentiated services code point (-1 default)",
425 -1, 63, DEFAULT_QOS_DSCP,
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 * GstMultiHandleSink::resend-streamheader
431 * Resend the streamheaders to existing clients when they change.
435 g_object_class_install_property (gobject_class, PROP_RESEND_STREAMHEADER,
436 g_param_spec_boolean ("resend-streamheader", "Resend streamheader",
437 "Resend the streamheader if it changes in the caps",
438 DEFAULT_RESEND_STREAMHEADER,
439 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_NUM_HANDLES,
442 g_param_spec_uint ("num-handles", "Number of handles",
443 "The current number of client handles",
444 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
447 * GstMultiHandleSink::clear:
448 * @gstmultihandlesink: the multihandlesink element to emit this signal on
450 * Remove all sockets from multihandlesink. Since multihandlesink did not
451 * open sockets itself, it does not explicitly close the sockets. The application
452 * should do so by connecting to the client-socket-removed callback.
454 gst_multi_handle_sink_signals[SIGNAL_CLEAR] =
455 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
456 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
457 G_STRUCT_OFFSET (GstMultiHandleSinkClass, clear), NULL, NULL,
458 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
460 gst_element_class_add_pad_template (gstelement_class,
461 gst_static_pad_template_get (&sinktemplate));
463 gst_element_class_set_details_simple (gstelement_class,
464 "Multi socket sink", "Sink/Network",
465 "Send data to multiple sockets",
466 "Thomas Vander Stichele <thomas at apestaart dot org>, "
467 "Wim Taymans <wim@fluendo.com>, "
468 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
470 gstelement_class->change_state =
471 GST_DEBUG_FUNCPTR (gst_multi_handle_sink_change_state);
473 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_render);
474 klass->client_queue_buffer =
475 GST_DEBUG_FUNCPTR (gst_multi_handle_sink_client_queue_buffer);
477 klass->add = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_add);
478 klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_add_full);
479 klass->remove = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_remove);
480 klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_remove_flush);
481 klass->clear = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_clear);
482 klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_handle_sink_get_stats);
484 GST_DEBUG_CATEGORY_INIT (multihandlesink_debug, "multihandlesink", 0,
485 "Multi socket sink");
489 gst_multi_handle_sink_init (GstMultiHandleSink * this)
491 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_HANDLE_SINK_OPEN);
493 CLIENTS_LOCK_INIT (this);
494 this->clients = NULL;
496 this->bufqueue = g_array_new (FALSE, TRUE, sizeof (GstBuffer *));
497 this->unit_format = DEFAULT_UNIT_FORMAT;
498 this->units_max = DEFAULT_UNITS_MAX;
499 this->units_soft_max = DEFAULT_UNITS_SOFT_MAX;
500 this->time_min = DEFAULT_TIME_MIN;
501 this->bytes_min = DEFAULT_BYTES_MIN;
502 this->buffers_min = DEFAULT_BUFFERS_MIN;
503 this->recover_policy = DEFAULT_RECOVER_POLICY;
505 this->timeout = DEFAULT_TIMEOUT;
506 this->def_sync_method = DEFAULT_SYNC_METHOD;
508 this->def_burst_format = DEFAULT_BURST_FORMAT;
509 this->def_burst_value = DEFAULT_BURST_VALUE;
511 this->qos_dscp = DEFAULT_QOS_DSCP;
513 this->resend_streamheader = DEFAULT_RESEND_STREAMHEADER;
517 gst_multi_handle_sink_finalize (GObject * object)
519 GstMultiHandleSink *this;
521 this = GST_MULTI_HANDLE_SINK (object);
523 CLIENTS_LOCK_CLEAR (this);
524 g_array_free (this->bufqueue, TRUE);
525 g_hash_table_destroy (this->handle_hash);
527 G_OBJECT_CLASS (parent_class)->finalize (object);
531 gst_multi_handle_sink_setup_dscp_client (GstMultiHandleSink * sink,
532 GstMultiHandleClient * client)
543 struct sockaddr_in6 sa_in6;
544 struct sockaddr_storage sa_stor;
546 socklen_t slen = sizeof (sa);
548 GstMultiHandleSinkClass *mhsinkclass = GST_MULTI_HANDLE_SINK_GET_CLASS (sink);
551 if (sink->qos_dscp < 0)
554 fd = mhsinkclass->client_get_fd (client);
556 if ((ret = getsockname (fd, &sa.sa, &slen)) < 0) {
557 GST_DEBUG_OBJECT (sink, "could not get sockname: %s", g_strerror (errno));
561 af = sa.sa.sa_family;
563 /* if this is an IPv4-mapped address then do IPv4 QoS */
564 if (af == AF_INET6) {
566 GST_DEBUG_OBJECT (sink, "check IP6 socket");
567 if (IN6_IS_ADDR_V4MAPPED (&(sa.sa_in6.sin6_addr))) {
568 GST_DEBUG_OBJECT (sink, "mapped to IPV4");
573 /* extract and shift 6 bits of the DSCP */
574 tos = (sink->qos_dscp & 0x3f) << 2;
578 ret = setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos));
582 ret = setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos));
587 GST_ERROR_OBJECT (sink, "unsupported AF");
591 GST_DEBUG_OBJECT (sink, "could not set DSCP: %s", g_strerror (errno));
598 gst_multi_handle_sink_client_init (GstMultiHandleClient * client,
599 GstSyncMethod sync_method)
603 client->status = GST_CLIENT_STATUS_OK;
605 client->flushcount = -1;
606 client->bufoffset = 0;
607 client->sending = NULL;
608 client->bytes_sent = 0;
609 client->dropped_buffers = 0;
610 client->avg_queue_size = 0;
611 client->first_buffer_ts = GST_CLOCK_TIME_NONE;
612 client->last_buffer_ts = GST_CLOCK_TIME_NONE;
613 client->new_connection = TRUE;
614 client->sync_method = sync_method;
615 client->currently_removing = FALSE;
617 /* update start time */
618 g_get_current_time (&now);
619 client->connect_time = GST_TIMEVAL_TO_TIME (now);
620 client->disconnect_time = 0;
621 /* set last activity time to connect time */
622 client->last_activity_time = client->connect_time;
626 gst_multi_handle_sink_setup_dscp (GstMultiHandleSink * mhsink)
630 CLIENTS_LOCK (mhsink);
631 for (clients = mhsink->clients; clients; clients = clients->next) {
632 GstMultiHandleClient *client;
634 client = clients->data;
636 gst_multi_handle_sink_setup_dscp_client (mhsink, client);
638 CLIENTS_UNLOCK (mhsink);
642 gst_multi_handle_sink_add_full (GstMultiHandleSink * sink,
643 GstMultiSinkHandle handle, GstSyncMethod sync_method, GstFormat min_format,
644 guint64 min_value, GstFormat max_format, guint64 max_value)
646 GstMultiHandleClient *mhclient;
648 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
650 GstMultiHandleSinkClass *mhsinkclass =
651 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
653 mhsinkclass->handle_debug (handle, debug);
654 GST_DEBUG_OBJECT (sink, "%s adding client, sync_method %d, "
655 "min_format %d, min_value %" G_GUINT64_FORMAT
656 ", max_format %d, max_value %" G_GUINT64_FORMAT, debug,
657 sync_method, min_format, min_value, max_format, max_value);
659 /* do limits check if we can */
660 if (min_format == max_format) {
661 if (max_value != -1 && min_value != -1 && max_value < min_value)
667 /* check the hash to find a duplicate handle */
668 clink = g_hash_table_lookup (mhsink->handle_hash,
669 mhsinkclass->handle_hash_key (handle));
673 mhclient = mhsinkclass->new_client (mhsink, handle, sync_method);
675 /* we can add the handle now */
676 clink = mhsink->clients = g_list_prepend (mhsink->clients, mhclient);
677 g_hash_table_insert (mhsink->handle_hash,
678 mhsinkclass->handle_hash_key (mhclient->handle), clink);
679 mhsink->clients_cookie++;
682 mhclient->burst_min_format = min_format;
683 mhclient->burst_min_value = min_value;
684 mhclient->burst_max_format = max_format;
685 mhclient->burst_max_value = max_value;
687 if (mhsinkclass->hash_changed)
688 mhsinkclass->hash_changed (mhsink);
690 CLIENTS_UNLOCK (sink);
692 g_signal_emit (G_OBJECT (sink),
693 mhsinkclass->signals[GST_MULTI_HANDLE_SIGNAL_CLIENT_ADDED], 0, handle);
700 GST_WARNING_OBJECT (sink,
701 "%s wrong values min =%" G_GUINT64_FORMAT ", max=%"
702 G_GUINT64_FORMAT ", unit %d specified when adding client",
703 debug, min_value, max_value, min_format);
708 CLIENTS_UNLOCK (sink);
709 GST_WARNING_OBJECT (sink, "%s duplicate client found, refusing", debug);
710 g_signal_emit (G_OBJECT (sink),
711 mhsinkclass->signals[GST_MULTI_HANDLE_SIGNAL_CLIENT_REMOVED], 0,
712 handle, GST_CLIENT_STATUS_DUPLICATE);
717 /* "add" signal implementation */
719 gst_multi_handle_sink_add (GstMultiHandleSink * sink, GstMultiSinkHandle handle)
721 gst_multi_handle_sink_add_full (sink, handle, sink->def_sync_method,
722 sink->def_burst_format, sink->def_burst_value, sink->def_burst_format,
726 /* "remove" signal implementation */
728 gst_multi_handle_sink_remove (GstMultiHandleSink * sink,
729 GstMultiSinkHandle handle)
732 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
733 GstMultiHandleSinkClass *mhsinkclass =
734 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
737 mhsinkclass->handle_debug (handle, debug);
739 GST_DEBUG_OBJECT (sink, "%s removing client", debug);
742 clink = g_hash_table_lookup (mhsink->handle_hash,
743 mhsinkclass->handle_hash_key (handle));
745 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) clink->data;
747 if (mhclient->status != GST_CLIENT_STATUS_OK) {
748 GST_INFO_OBJECT (sink,
749 "%s Client already disconnecting with status %d",
750 debug, mhclient->status);
754 mhclient->status = GST_CLIENT_STATUS_REMOVED;
755 gst_multi_handle_sink_remove_client_link (GST_MULTI_HANDLE_SINK (sink),
757 if (mhsinkclass->hash_changed)
758 mhsinkclass->hash_changed (mhsink);
760 GST_WARNING_OBJECT (sink, "%s no client with this handle found!", debug);
764 CLIENTS_UNLOCK (sink);
767 /* "remove-flush" signal implementation */
769 gst_multi_handle_sink_remove_flush (GstMultiHandleSink * sink,
770 GstMultiSinkHandle handle)
773 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
774 GstMultiHandleSinkClass *mhsinkclass =
775 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
778 mhsinkclass->handle_debug (handle, debug);
780 GST_DEBUG_OBJECT (sink, "%s flushing client", debug);
783 clink = g_hash_table_lookup (mhsink->handle_hash,
784 mhsinkclass->handle_hash_key (handle));
786 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) clink->data;
788 if (mhclient->status != GST_CLIENT_STATUS_OK) {
789 GST_INFO_OBJECT (sink,
790 "%s Client already disconnecting with status %d",
791 mhclient->debug, mhclient->status);
795 /* take the position of the client as the number of buffers left to flush.
796 * If the client was at position -1, we flush 0 buffers, 0 == flush 1
798 mhclient->flushcount = mhclient->bufpos + 1;
799 /* mark client as flushing. We can not remove the client right away because
800 * it might have some buffers to flush in the ->sending queue. */
801 mhclient->status = GST_CLIENT_STATUS_FLUSHING;
803 GST_WARNING_OBJECT (sink, "%s no client with this handle found!", debug);
806 CLIENTS_UNLOCK (sink);
809 /* can be called both through the signal (i.e. from any thread) or when
810 * stopping, after the writing thread has shut down */
812 gst_multi_handle_sink_clear (GstMultiHandleSink * mhsink)
814 GList *clients, *next;
816 GstMultiHandleSinkClass *mhsinkclass =
817 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
819 GST_DEBUG_OBJECT (mhsink, "clearing all clients");
821 CLIENTS_LOCK (mhsink);
823 cookie = mhsink->clients_cookie;
824 for (clients = mhsink->clients; clients; clients = next) {
825 GstMultiHandleClient *mhclient;
827 if (cookie != mhsink->clients_cookie) {
828 GST_DEBUG_OBJECT (mhsink, "cookie changed while removing all clients");
832 mhclient = (GstMultiHandleClient *) clients->data;
833 next = g_list_next (clients);
835 mhclient->status = GST_CLIENT_STATUS_REMOVED;
836 /* the next call changes the list, which is why we iterate
837 * with a temporary next pointer */
838 gst_multi_handle_sink_remove_client_link (mhsink, clients);
840 if (mhsinkclass->hash_changed)
841 mhsinkclass->hash_changed (mhsink);
843 CLIENTS_UNLOCK (mhsink);
847 /* "get-stats" signal implementation
850 gst_multi_handle_sink_get_stats (GstMultiHandleSink * sink,
851 GstMultiSinkHandle handle)
853 GstMultiHandleClient *client;
854 GstStructure *result = NULL;
856 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
857 GstMultiHandleSinkClass *mhsinkclass =
858 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
861 mhsinkclass->handle_debug (handle, debug);
864 clink = g_hash_table_lookup (mhsink->handle_hash,
865 mhsinkclass->handle_hash_key (handle));
869 client = clink->data;
870 if (client != NULL) {
871 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
874 result = gst_structure_new_empty ("multihandlesink-stats");
876 if (mhclient->disconnect_time == 0) {
879 g_get_current_time (&nowtv);
881 interval = GST_TIMEVAL_TO_TIME (nowtv) - mhclient->connect_time;
883 interval = mhclient->disconnect_time - mhclient->connect_time;
886 gst_structure_set (result,
887 "bytes-sent", G_TYPE_UINT64, mhclient->bytes_sent,
888 "connect-time", G_TYPE_UINT64, mhclient->connect_time,
889 "disconnect-time", G_TYPE_UINT64, mhclient->disconnect_time,
890 "connect-duration", G_TYPE_UINT64, interval,
891 "last-activitity-time", G_TYPE_UINT64, mhclient->last_activity_time,
892 "buffers-dropped", G_TYPE_UINT64, mhclient->dropped_buffers,
893 "first-buffer-ts", G_TYPE_UINT64, mhclient->first_buffer_ts,
894 "last-buffer-ts", G_TYPE_UINT64, mhclient->last_buffer_ts, NULL);
898 CLIENTS_UNLOCK (sink);
900 /* python doesn't like a NULL pointer yet */
901 if (result == NULL) {
902 GST_WARNING_OBJECT (sink, "%s no client with this found!", debug);
903 result = gst_structure_new_empty ("multihandlesink-stats");
909 /* should be called with the clientslock held.
910 * Note that we don't close the fd as we didn't open it in the first
911 * place. An application should connect to the client-fd-removed signal and
912 * close the fd itself.
915 gst_multi_handle_sink_remove_client_link (GstMultiHandleSink * sink,
919 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) link->data;
920 GstMultiHandleSinkClass *mhsinkclass = GST_MULTI_HANDLE_SINK_GET_CLASS (sink);
922 if (mhclient->currently_removing) {
923 GST_WARNING_OBJECT (sink, "%s client is already being removed",
927 mhclient->currently_removing = TRUE;
930 /* FIXME: if we keep track of ip we can log it here and signal */
931 switch (mhclient->status) {
932 case GST_CLIENT_STATUS_OK:
933 GST_WARNING_OBJECT (sink, "%s removing client %p for no reason",
934 mhclient->debug, mhclient);
936 case GST_CLIENT_STATUS_CLOSED:
937 GST_DEBUG_OBJECT (sink, "%s removing client %p because of close",
938 mhclient->debug, mhclient);
940 case GST_CLIENT_STATUS_REMOVED:
941 GST_DEBUG_OBJECT (sink,
942 "%s removing client %p because the app removed it", mhclient->debug,
945 case GST_CLIENT_STATUS_SLOW:
946 GST_INFO_OBJECT (sink,
947 "%s removing client %p because it was too slow", mhclient->debug,
950 case GST_CLIENT_STATUS_ERROR:
951 GST_WARNING_OBJECT (sink,
952 "%s removing client %p because of error", mhclient->debug, mhclient);
954 case GST_CLIENT_STATUS_FLUSHING:
956 GST_WARNING_OBJECT (sink,
957 "%s removing client %p with invalid reason %d", mhclient->debug,
958 mhclient, mhclient->status);
962 mhsinkclass->hash_removing (sink, mhclient);
964 g_get_current_time (&now);
965 mhclient->disconnect_time = GST_TIMEVAL_TO_TIME (now);
967 /* free client buffers */
968 g_slist_foreach (mhclient->sending, (GFunc) gst_mini_object_unref, NULL);
969 g_slist_free (mhclient->sending);
970 mhclient->sending = NULL;
973 gst_caps_unref (mhclient->caps);
974 mhclient->caps = NULL;
976 /* unlock the mutex before signaling because the signal handler
977 * might query some properties */
978 CLIENTS_UNLOCK (sink);
980 g_signal_emit (G_OBJECT (sink),
981 mhsinkclass->signals[GST_MULTI_HANDLE_SIGNAL_CLIENT_REMOVED], 0,
982 mhclient->handle, mhclient->status);
984 /* lock again before we remove the client completely */
987 /* handle cannot be reused in the above signal callback so we can safely
988 * remove it from the hashtable here */
989 if (!g_hash_table_remove (sink->handle_hash,
990 mhsinkclass->handle_hash_key (mhclient->handle))) {
991 GST_WARNING_OBJECT (sink,
992 "%s error removing client %p from hash", mhclient->debug, mhclient);
994 /* after releasing the lock above, the link could be invalid, more
995 * precisely, the next and prev pointers could point to invalid list
996 * links. One optimisation could be to add a cookie to the linked list
997 * and take a shortcut when it did not change between unlocking and locking
998 * our mutex. For now we just walk the list again. */
999 sink->clients = g_list_remove (sink->clients, mhclient);
1000 sink->clients_cookie++;
1002 if (mhsinkclass->removed)
1003 mhsinkclass->removed (sink, mhclient->handle);
1005 CLIENTS_UNLOCK (sink);
1007 /* and the handle is really gone now */
1008 g_signal_emit (G_OBJECT (sink),
1009 mhsinkclass->signals[GST_MULTI_HANDLE_SIGNAL_CLIENT_HANDLE_REMOVED],
1010 0, mhclient->handle);
1012 if (mhsinkclass->client_free)
1013 mhsinkclass->client_free (mhclient);
1016 CLIENTS_LOCK (sink);
1020 gst_multi_handle_sink_client_queue_buffer (GstMultiHandleSink * mhsink,
1021 GstMultiHandleClient * mhclient, GstBuffer * buffer)
1023 GstMultiHandleSink *sink = GST_MULTI_HANDLE_SINK (mhsink);
1026 /* TRUE: send them if the new caps have them */
1027 gboolean send_streamheader = FALSE;
1030 /* before we queue the buffer, we check if we need to queue streamheader
1031 * buffers (because it's a new client, or because they changed) */
1032 caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (sink));
1034 if (!mhclient->caps) {
1035 GST_DEBUG_OBJECT (sink,
1036 "%s no previous caps for this client, send streamheader",
1038 send_streamheader = TRUE;
1039 mhclient->caps = gst_caps_ref (caps);
1041 /* there were previous caps recorded, so compare */
1042 if (!gst_caps_is_equal (caps, mhclient->caps)) {
1043 const GValue *sh1, *sh2;
1045 /* caps are not equal, but could still have the same streamheader */
1046 s = gst_caps_get_structure (caps, 0);
1047 if (!gst_structure_has_field (s, "streamheader")) {
1048 /* no new streamheader, so nothing new to send */
1049 GST_DEBUG_OBJECT (sink,
1050 "%s new caps do not have streamheader, not sending",
1053 /* there is a new streamheader */
1054 s = gst_caps_get_structure (mhclient->caps, 0);
1055 if (!gst_structure_has_field (s, "streamheader")) {
1056 /* no previous streamheader, so send the new one */
1057 GST_DEBUG_OBJECT (sink,
1058 "%s previous caps did not have streamheader, sending",
1060 send_streamheader = TRUE;
1062 /* both old and new caps have streamheader set */
1063 if (!mhsink->resend_streamheader) {
1064 GST_DEBUG_OBJECT (sink,
1065 "%s asked to not resend the streamheader, not sending",
1067 send_streamheader = FALSE;
1069 sh1 = gst_structure_get_value (s, "streamheader");
1070 s = gst_caps_get_structure (caps, 0);
1071 sh2 = gst_structure_get_value (s, "streamheader");
1072 if (gst_value_compare (sh1, sh2) != GST_VALUE_EQUAL) {
1073 GST_DEBUG_OBJECT (sink,
1074 "%s new streamheader different from old, sending",
1076 send_streamheader = TRUE;
1082 /* Replace the old caps */
1083 gst_caps_unref (mhclient->caps);
1084 mhclient->caps = gst_caps_ref (caps);
1087 if (G_UNLIKELY (send_streamheader)) {
1092 GST_LOG_OBJECT (sink,
1093 "%s sending streamheader from caps %" GST_PTR_FORMAT,
1094 mhclient->debug, caps);
1095 s = gst_caps_get_structure (caps, 0);
1096 if (!gst_structure_has_field (s, "streamheader")) {
1097 GST_DEBUG_OBJECT (sink,
1098 "%s no new streamheader, so nothing to send", mhclient->debug);
1100 GST_LOG_OBJECT (sink,
1101 "%s sending streamheader from caps %" GST_PTR_FORMAT,
1102 mhclient->debug, caps);
1103 sh = gst_structure_get_value (s, "streamheader");
1104 g_assert (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
1105 buffers = g_value_peek_pointer (sh);
1106 GST_DEBUG_OBJECT (sink, "%d streamheader buffers", buffers->len);
1107 for (i = 0; i < buffers->len; ++i) {
1111 bufval = &g_array_index (buffers, GValue, i);
1112 g_assert (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER);
1113 buffer = g_value_peek_pointer (bufval);
1114 GST_DEBUG_OBJECT (sink,
1115 "%s queueing streamheader buffer of length %" G_GSIZE_FORMAT,
1116 mhclient->debug, gst_buffer_get_size (buffer));
1117 gst_buffer_ref (buffer);
1119 mhclient->sending = g_slist_append (mhclient->sending, buffer);
1124 gst_caps_unref (caps);
1127 GST_LOG_OBJECT (sink, "%s queueing buffer of length %" G_GSIZE_FORMAT,
1128 mhclient->debug, gst_buffer_get_size (buffer));
1130 gst_buffer_ref (buffer);
1131 mhclient->sending = g_slist_append (mhclient->sending, buffer);
1137 is_sync_frame (GstMultiHandleSink * sink, GstBuffer * buffer)
1139 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1141 } else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
1148 /* find the keyframe in the list of buffers starting the
1149 * search from @idx. @direction as -1 will search backwards,
1150 * 1 will search forwards.
1151 * Returns: the index or -1 if there is no keyframe after idx.
1154 find_syncframe (GstMultiHandleSink * sink, gint idx, gint direction)
1156 gint i, len, result;
1158 /* take length of queued buffers */
1159 len = sink->bufqueue->len;
1161 /* assume we don't find a keyframe */
1164 /* then loop over all buffers to find the first keyframe */
1165 for (i = idx; i >= 0 && i < len; i += direction) {
1168 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1169 if (is_sync_frame (sink, buf)) {
1170 GST_LOG_OBJECT (sink, "found keyframe at %d from %d, direction %d",
1179 /* Get the number of buffers from the buffer queue needed to satisfy
1180 * the maximum max in the configured units.
1181 * If units are not BUFFERS, and there are insufficient buffers in the
1182 * queue to satify the limit, return len(queue) + 1 */
1184 get_buffers_max (GstMultiHandleSink * sink, gint64 max)
1186 switch (sink->unit_format) {
1187 case GST_FORMAT_BUFFERS:
1189 case GST_FORMAT_TIME:
1195 GstClockTime first = GST_CLOCK_TIME_NONE;
1197 len = sink->bufqueue->len;
1199 for (i = 0; i < len; i++) {
1200 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1201 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1203 first = GST_BUFFER_TIMESTAMP (buf);
1205 diff = first - GST_BUFFER_TIMESTAMP (buf);
1213 case GST_FORMAT_BYTES:
1220 len = sink->bufqueue->len;
1222 for (i = 0; i < len; i++) {
1223 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1224 acc += gst_buffer_get_size (buf);
1236 /* find the positions in the buffer queue where *_min and *_max
1239 /* count the amount of data in the buffers and return the index
1240 * that satifies the given limits.
1242 * Returns: index @idx in the buffer queue so that the given limits are
1243 * satisfied. TRUE if all the limits could be satisfied, FALSE if not
1244 * enough data was in the queue.
1246 * FIXME, this code might now work if any of the units is in buffers...
1249 find_limits (GstMultiHandleSink * sink,
1250 gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
1251 gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max)
1253 GstClockTime first, time;
1255 gboolean result, max_hit;
1257 /* take length of queue */
1258 len = sink->bufqueue->len;
1260 /* this must hold */
1263 GST_LOG_OBJECT (sink,
1264 "bytes_min %d, buffers_min %d, time_min %" GST_TIME_FORMAT
1265 ", bytes_max %d, buffers_max %d, time_max %" GST_TIME_FORMAT, bytes_min,
1266 buffers_min, GST_TIME_ARGS (time_min), bytes_max, buffers_max,
1267 GST_TIME_ARGS (time_max));
1269 /* do the trivial buffer limit test */
1270 if (buffers_min != -1 && len < buffers_min) {
1277 /* else count bytes and time */
1286 /* loop through the buffers, when a limit is ok, mark it
1287 * as -1, we have at least one buffer in the queue. */
1291 /* if we checked all min limits, update result */
1292 if (bytes_min == -1 && time_min == -1 && *min_idx == -1) {
1293 /* don't go below 0 */
1294 *min_idx = MAX (i - 1, 0);
1296 /* if we reached one max limit break out */
1298 /* i > 0 when we get here, we subtract one to get the position
1299 * of the previous buffer. */
1301 /* we have valid complete result if we found a min_idx too */
1302 result = *min_idx != -1;
1305 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1307 bytes += gst_buffer_get_size (buf);
1309 /* take timestamp and save for the base first timestamp */
1310 if ((time = GST_BUFFER_TIMESTAMP (buf)) != -1) {
1311 GST_LOG_OBJECT (sink, "Ts %" GST_TIME_FORMAT " on buffer",
1312 GST_TIME_ARGS (time));
1316 /* increase max usage if we did not fill enough. Note that
1317 * buffers are sorted from new to old, so the first timestamp is
1318 * bigger than the next one. */
1319 if (time_min != -1 && first - time >= time_min)
1321 if (time_max != -1 && first - time >= time_max)
1324 GST_LOG_OBJECT (sink, "No timestamp on buffer");
1326 /* time is OK or unknown, check and increase if not enough bytes */
1327 if (bytes_min != -1) {
1328 if (bytes >= bytes_min)
1331 if (bytes_max != -1) {
1332 if (bytes >= bytes_max) {
1340 /* if we did not hit the max or min limit, set to buffer size */
1343 /* make sure min does not exceed max */
1345 *min_idx = *max_idx;
1350 /* parse the unit/value pair and assign it to the result value of the
1351 * right type, leave the other values untouched
1353 * Returns: FALSE if the unit is unknown or undefined. TRUE otherwise.
1356 assign_value (GstFormat format, guint64 value, gint * bytes, gint * buffers,
1357 GstClockTime * time)
1359 gboolean res = TRUE;
1361 /* set only the limit of the given format to the given value */
1363 case GST_FORMAT_BUFFERS:
1364 *buffers = (gint) value;
1366 case GST_FORMAT_TIME:
1369 case GST_FORMAT_BYTES:
1370 *bytes = (gint) value;
1372 case GST_FORMAT_UNDEFINED:
1380 /* count the index in the buffer queue to satisfy the given unit
1381 * and value pair starting from buffer at index 0.
1383 * Returns: TRUE if there was enough data in the queue to satisfy the
1384 * burst values. @idx contains the index in the buffer that contains enough
1385 * data to satisfy the limits or the last buffer in the queue when the
1386 * function returns FALSE.
1389 count_burst_unit (GstMultiHandleSink * sink, gint * min_idx,
1390 GstFormat min_format, guint64 min_value, gint * max_idx,
1391 GstFormat max_format, guint64 max_value)
1393 gint bytes_min = -1, buffers_min = -1;
1394 gint bytes_max = -1, buffers_max = -1;
1395 GstClockTime time_min = GST_CLOCK_TIME_NONE, time_max = GST_CLOCK_TIME_NONE;
1397 assign_value (min_format, min_value, &bytes_min, &buffers_min, &time_min);
1398 assign_value (max_format, max_value, &bytes_max, &buffers_max, &time_max);
1400 return find_limits (sink, min_idx, bytes_min, buffers_min, time_min,
1401 max_idx, bytes_max, buffers_max, time_max);
1404 /* decide where in the current buffer queue this new client should start
1405 * receiving buffers from.
1406 * This function is called whenever a client is connected and has not yet
1407 * received a buffer.
1408 * If this returns -1, it means that we haven't found a good point to
1409 * start streaming from yet, and this function should be called again later
1410 * when more buffers have arrived.
1413 gst_multi_handle_sink_new_client_position (GstMultiHandleSink * sink,
1414 GstMultiHandleClient * client)
1418 GST_DEBUG_OBJECT (sink,
1419 "%s new client, deciding where to start in queue", client->debug);
1420 GST_DEBUG_OBJECT (sink, "queue is currently %d buffers long",
1421 sink->bufqueue->len);
1422 switch (client->sync_method) {
1423 case GST_SYNC_METHOD_LATEST:
1424 /* no syncing, we are happy with whatever the client is going to get */
1425 result = client->bufpos;
1426 GST_DEBUG_OBJECT (sink,
1427 "%s SYNC_METHOD_LATEST, position %d", client->debug, result);
1429 case GST_SYNC_METHOD_NEXT_KEYFRAME:
1431 /* if one of the new buffers (between client->bufpos and 0) in the queue
1432 * is a sync point, we can proceed, otherwise we need to keep waiting */
1433 GST_LOG_OBJECT (sink,
1434 "%s new client, bufpos %d, waiting for keyframe",
1435 client->debug, client->bufpos);
1437 result = find_prev_syncframe (sink, client->bufpos);
1439 GST_DEBUG_OBJECT (sink,
1440 "%s SYNC_METHOD_NEXT_KEYFRAME: result %d", client->debug, result);
1444 /* client is not on a syncbuffer, need to skip these buffers and
1446 GST_LOG_OBJECT (sink,
1447 "%s new client, skipping buffer(s), no syncpoint found",
1449 client->bufpos = -1;
1452 case GST_SYNC_METHOD_LATEST_KEYFRAME:
1454 GST_DEBUG_OBJECT (sink, "%s SYNC_METHOD_LATEST_KEYFRAME", client->debug);
1456 /* for new clients we initially scan the complete buffer queue for
1457 * a sync point when a buffer is added. If we don't find a keyframe,
1458 * we need to wait for the next keyframe and so we change the client's
1459 * sync method to GST_SYNC_METHOD_NEXT_KEYFRAME.
1461 result = find_next_syncframe (sink, 0);
1463 GST_DEBUG_OBJECT (sink,
1464 "%s SYNC_METHOD_LATEST_KEYFRAME: result %d", client->debug, result);
1468 GST_DEBUG_OBJECT (sink,
1469 "%s SYNC_METHOD_LATEST_KEYFRAME: no keyframe found, "
1470 "switching to SYNC_METHOD_NEXT_KEYFRAME", client->debug);
1471 /* throw client to the waiting state */
1472 client->bufpos = -1;
1473 /* and make client sync to next keyframe */
1474 client->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1477 case GST_SYNC_METHOD_BURST:
1482 /* move to the position where we satisfy the client's burst
1483 * parameters. If we could not satisfy the parameters because there
1484 * is not enough data, we just send what we have (which is in result).
1485 * We use the max value to limit the search
1487 ok = count_burst_unit (sink, &result, client->burst_min_format,
1488 client->burst_min_value, &max, client->burst_max_format,
1489 client->burst_max_value);
1490 GST_DEBUG_OBJECT (sink,
1491 "%s SYNC_METHOD_BURST: burst_unit returned %d, result %d",
1492 client->debug, ok, result);
1494 GST_LOG_OBJECT (sink, "min %d, max %d", result, max);
1496 /* we hit the max and it is below the min, use that then */
1497 if (max != -1 && max <= result) {
1498 result = MAX (max - 1, 0);
1499 GST_DEBUG_OBJECT (sink,
1500 "%s SYNC_METHOD_BURST: result above max, taken down to %d",
1501 client->debug, result);
1505 case GST_SYNC_METHOD_BURST_KEYFRAME:
1507 gint min_idx, max_idx;
1508 gint next_syncframe, prev_syncframe;
1512 * _always_ start sending a keyframe to the client. We first search
1513 * a keyframe between min/max limits. If there is none, we send it the
1514 * last keyframe before min. If there is none, the behaviour is like
1517 /* gather burst limits */
1518 count_burst_unit (sink, &min_idx, client->burst_min_format,
1519 client->burst_min_value, &max_idx, client->burst_max_format,
1520 client->burst_max_value);
1522 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1524 /* first find a keyframe after min_idx */
1525 next_syncframe = find_next_syncframe (sink, min_idx);
1526 if (next_syncframe != -1 && next_syncframe < max_idx) {
1527 /* we have a valid keyframe and it's below the max */
1528 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1529 result = next_syncframe;
1533 /* no valid keyframe, try to find one below min */
1534 prev_syncframe = find_prev_syncframe (sink, min_idx);
1535 if (prev_syncframe != -1) {
1536 GST_WARNING_OBJECT (sink,
1537 "using keyframe below min in BURST_KEYFRAME sync mode");
1538 result = prev_syncframe;
1542 /* no prev keyframe or not enough data */
1543 GST_WARNING_OBJECT (sink,
1544 "no prev keyframe found in BURST_KEYFRAME sync mode, waiting for next");
1546 /* throw client to the waiting state */
1547 client->bufpos = -1;
1548 /* and make client sync to next keyframe */
1549 client->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1553 case GST_SYNC_METHOD_BURST_WITH_KEYFRAME:
1555 gint min_idx, max_idx;
1556 gint next_syncframe;
1558 /* BURST_WITH_KEYFRAME:
1560 * try to start sending a keyframe to the client. We first search
1561 * a keyframe between min/max limits. If there is none, we send it the
1562 * amount of data up 'till min.
1564 /* gather enough data to burst */
1565 count_burst_unit (sink, &min_idx, client->burst_min_format,
1566 client->burst_min_value, &max_idx, client->burst_max_format,
1567 client->burst_max_value);
1569 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1571 /* first find a keyframe after min_idx */
1572 next_syncframe = find_next_syncframe (sink, min_idx);
1573 if (next_syncframe != -1 && next_syncframe < max_idx) {
1574 /* we have a valid keyframe and it's below the max */
1575 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1576 result = next_syncframe;
1580 /* no keyframe, send data from min_idx */
1581 GST_WARNING_OBJECT (sink, "using min in BURST_WITH_KEYFRAME sync mode");
1583 /* make sure we don't go over the max limit */
1584 if (max_idx != -1 && max_idx <= min_idx) {
1585 result = MAX (max_idx - 1, 0);
1593 g_warning ("unknown sync method %d", client->sync_method);
1594 result = client->bufpos;
1600 /* calculate the new position for a client after recovery. This function
1601 * does not update the client position but merely returns the required
1605 gst_multi_handle_sink_recover_client (GstMultiHandleSink * sink,
1606 GstMultiHandleClient * client)
1610 GST_WARNING_OBJECT (sink,
1611 "%s client %p is lagging at %d, recover using policy %d",
1612 client->debug, client, client->bufpos, sink->recover_policy);
1614 switch (sink->recover_policy) {
1615 case GST_RECOVER_POLICY_NONE:
1616 /* do nothing, client will catch up or get kicked out when it reaches
1618 newbufpos = client->bufpos;
1620 case GST_RECOVER_POLICY_RESYNC_LATEST:
1621 /* move to beginning of queue */
1624 case GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT:
1625 /* move to beginning of soft max */
1626 newbufpos = get_buffers_max (sink, sink->units_soft_max);
1628 case GST_RECOVER_POLICY_RESYNC_KEYFRAME:
1629 /* find keyframe in buffers, we search backwards to find the
1630 * closest keyframe relative to what this client already received. */
1631 newbufpos = MIN (sink->bufqueue->len - 1,
1632 get_buffers_max (sink, sink->units_soft_max) - 1);
1634 while (newbufpos >= 0) {
1637 buf = g_array_index (sink->bufqueue, GstBuffer *, newbufpos);
1638 if (is_sync_frame (sink, buf)) {
1639 /* found a buffer that is not a delta unit */
1646 /* unknown recovery procedure */
1647 newbufpos = get_buffers_max (sink, sink->units_soft_max);
1653 /* Queue a buffer on the global queue.
1655 * This function adds the buffer to the front of a GArray. It removes the
1656 * tail buffer if the max queue size is exceeded, unreffing the queued buffer.
1657 * Note that unreffing the buffer is not a problem as clients who
1658 * started writing out this buffer will still have a reference to it in the
1659 * mhclient->sending queue.
1661 * After adding the buffer, we update all client positions in the queue. If
1662 * a client moves over the soft max, we start the recovery procedure for this
1663 * slow client. If it goes over the hard max, it is put into the slow list
1666 * Special care is taken of clients that were waiting for a new buffer (they
1667 * had a position of -1) because they can proceed after adding this new buffer.
1668 * This is done by adding the client back into the write fd_set and signaling
1669 * the select thread that the fd_set changed.
1672 gst_multi_handle_sink_queue_buffer (GstMultiHandleSink * mhsink,
1675 GList *clients, *next;
1677 gboolean hash_changed = FALSE;
1678 gint max_buffer_usage;
1682 gint max_buffers, soft_max_buffers;
1684 GstMultiHandleSink *sink = GST_MULTI_HANDLE_SINK (mhsink);
1685 GstMultiHandleSinkClass *mhsinkclass =
1686 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
1688 g_get_current_time (&nowtv);
1689 now = GST_TIMEVAL_TO_TIME (nowtv);
1691 CLIENTS_LOCK (mhsink);
1692 /* add buffer to queue */
1693 g_array_prepend_val (mhsink->bufqueue, buffer);
1694 queuelen = mhsink->bufqueue->len;
1696 if (mhsink->units_max > 0)
1697 max_buffers = get_buffers_max (mhsink, mhsink->units_max);
1701 if (mhsink->units_soft_max > 0)
1702 soft_max_buffers = get_buffers_max (mhsink, mhsink->units_soft_max);
1704 soft_max_buffers = -1;
1705 GST_LOG_OBJECT (sink, "Using max %d, softmax %d", max_buffers,
1708 /* then loop over the clients and update the positions */
1709 max_buffer_usage = 0;
1712 cookie = mhsink->clients_cookie;
1713 for (clients = mhsink->clients; clients; clients = next) {
1714 GstMultiHandleClient *mhclient = clients->data;
1716 if (cookie != mhsink->clients_cookie) {
1717 GST_DEBUG_OBJECT (sink, "Clients cookie outdated, restarting");
1721 next = g_list_next (clients);
1724 GST_LOG_OBJECT (sink, "%s client %p at position %d",
1725 mhclient->debug, mhclient, mhclient->bufpos);
1726 /* check soft max if needed, recover client */
1727 if (soft_max_buffers > 0 && mhclient->bufpos >= soft_max_buffers) {
1730 newpos = gst_multi_handle_sink_recover_client (mhsink, mhclient);
1731 if (newpos != mhclient->bufpos) {
1732 mhclient->dropped_buffers += mhclient->bufpos - newpos;
1733 mhclient->bufpos = newpos;
1734 mhclient->discont = TRUE;
1735 GST_INFO_OBJECT (sink, "%s client %p position reset to %d",
1736 mhclient->debug, mhclient, mhclient->bufpos);
1738 GST_INFO_OBJECT (sink,
1739 "%s client %p not recovering position", mhclient->debug, mhclient);
1742 /* check hard max and timeout, remove client */
1743 if ((max_buffers > 0 && mhclient->bufpos >= max_buffers) ||
1744 (mhsink->timeout > 0
1745 && now - mhclient->last_activity_time > mhsink->timeout)) {
1747 GST_WARNING_OBJECT (sink, "%s client %p is too slow, removing",
1748 mhclient->debug, mhclient);
1749 /* remove the client, the handle set will be cleared and the select thread
1750 * will be signaled */
1751 mhclient->status = GST_CLIENT_STATUS_SLOW;
1752 /* set client to invalid position while being removed */
1753 mhclient->bufpos = -1;
1754 gst_multi_handle_sink_remove_client_link (mhsink, clients);
1755 hash_changed = TRUE;
1757 } else if (mhclient->bufpos == 0 || mhclient->new_connection) {
1758 /* can send data to this client now. need to signal the select thread that
1759 * the handle_set changed */
1760 mhsinkclass->hash_adding (mhsink, mhclient);
1761 hash_changed = TRUE;
1763 /* keep track of maximum buffer usage */
1764 if (mhclient->bufpos > max_buffer_usage) {
1765 max_buffer_usage = mhclient->bufpos;
1769 /* make sure we respect bytes-min, buffers-min and time-min when they are set */
1773 GST_LOG_OBJECT (sink,
1774 "extending queue %d to respect time_min %" GST_TIME_FORMAT
1775 ", bytes_min %d, buffers_min %d", max_buffer_usage,
1776 GST_TIME_ARGS (mhsink->time_min), mhsink->bytes_min,
1777 mhsink->buffers_min);
1779 /* get index where the limits are ok, we don't really care if all limits
1780 * are ok, we just queue as much as we need. We also don't compare against
1781 * the max limits. */
1782 find_limits (mhsink, &usage, mhsink->bytes_min, mhsink->buffers_min,
1783 mhsink->time_min, &max, -1, -1, -1);
1785 max_buffer_usage = MAX (max_buffer_usage, usage + 1);
1786 GST_LOG_OBJECT (sink, "extended queue to %d", max_buffer_usage);
1789 /* now look for sync points and make sure there is at least one
1790 * sync point in the queue. We only do this if the LATEST_KEYFRAME or
1791 * BURST_KEYFRAME mode is selected */
1792 if (mhsink->def_sync_method == GST_SYNC_METHOD_LATEST_KEYFRAME ||
1793 mhsink->def_sync_method == GST_SYNC_METHOD_BURST_KEYFRAME) {
1794 /* no point in searching beyond the queue length */
1795 gint limit = queuelen;
1798 /* no point in searching beyond the soft-max if any. */
1799 if (soft_max_buffers > 0) {
1800 limit = MIN (limit, soft_max_buffers);
1802 GST_LOG_OBJECT (sink,
1803 "extending queue to include sync point, now at %d, limit is %d",
1804 max_buffer_usage, limit);
1805 for (i = 0; i < limit; i++) {
1806 buf = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1807 if (is_sync_frame (mhsink, buf)) {
1808 /* found a sync frame, now extend the buffer usage to
1809 * include at least this frame. */
1810 max_buffer_usage = MAX (max_buffer_usage, i);
1814 GST_LOG_OBJECT (sink, "max buffer usage is now %d", max_buffer_usage);
1817 GST_LOG_OBJECT (sink, "len %d, usage %d", queuelen, max_buffer_usage);
1819 /* nobody is referencing units after max_buffer_usage so we can
1820 * remove them from the queue. We remove them in reverse order as
1821 * this is the most optimal for GArray. */
1822 for (i = queuelen - 1; i > max_buffer_usage; i--) {
1825 /* queue exceeded max size */
1827 old = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1828 mhsink->bufqueue = g_array_remove_index (mhsink->bufqueue, i);
1830 /* unref tail buffer */
1831 gst_buffer_unref (old);
1833 /* save for stats */
1834 mhsink->buffers_queued = max_buffer_usage;
1835 CLIENTS_UNLOCK (sink);
1837 /* and send a signal to thread if handle_set changed */
1838 if (hash_changed && mhsinkclass->hash_changed) {
1839 mhsinkclass->hash_changed (mhsink);
1843 static GstFlowReturn
1844 gst_multi_handle_sink_render (GstBaseSink * bsink, GstBuffer * buf)
1848 GstCaps *bufcaps, *padcaps;
1851 GstMultiHandleSink *sink = GST_MULTI_HANDLE_SINK (bsink);
1853 g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink,
1854 GST_MULTI_HANDLE_SINK_OPEN), GST_FLOW_FLUSHING);
1857 /* since we check every buffer for streamheader caps, we need to make
1858 * sure every buffer has caps set */
1859 bufcaps = gst_buffer_get_caps (buf);
1860 padcaps = GST_PAD_CAPS (GST_BASE_SINK_PAD (bsink));
1862 /* make sure we have caps on the pad */
1863 if (!padcaps && !bufcaps)
1867 /* get IN_CAPS first, code below might mess with the flags */
1868 in_caps = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER);
1871 /* stamp the buffer with previous caps if no caps set */
1873 if (!gst_buffer_is_writable (buf)) {
1874 /* metadata is not writable, copy will be made and original buffer
1875 * will be unreffed so we need to ref so that we don't lose the
1876 * buffer in the render method. */
1877 gst_buffer_ref (buf);
1878 /* the new buffer is ours only, we keep it out of the scope of this
1880 buf = gst_buffer_make_writable (buf);
1882 /* else the metadata is writable, we ref because we keep the buffer
1883 * out of the scope of this method */
1884 gst_buffer_ref (buf);
1886 /* buffer metadata is writable now, set the caps */
1887 gst_buffer_set_caps (buf, padcaps);
1889 gst_caps_unref (bufcaps);
1891 /* since we keep this buffer out of the scope of this method */
1892 gst_buffer_ref (buf);
1895 gst_buffer_ref (buf);
1897 GST_LOG_OBJECT (sink, "received buffer %p, in_caps: %s, offset %"
1898 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
1899 ", timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
1900 buf, in_caps ? "yes" : "no", GST_BUFFER_OFFSET (buf),
1901 GST_BUFFER_OFFSET_END (buf),
1902 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
1903 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
1905 /* if we get IN_CAPS buffers, but the previous buffer was not IN_CAPS,
1906 * it means we're getting new streamheader buffers, and we should clear
1908 if (in_caps && sink->previous_buffer_in_caps == FALSE) {
1909 GST_DEBUG_OBJECT (sink,
1910 "receiving new HEADER buffers, clearing old streamheader");
1911 g_slist_foreach (sink->streamheader, (GFunc) gst_mini_object_unref, NULL);
1912 g_slist_free (sink->streamheader);
1913 sink->streamheader = NULL;
1916 /* save the current in_caps */
1917 sink->previous_buffer_in_caps = in_caps;
1919 /* if the incoming buffer is marked as IN CAPS, then we assume for now
1920 * it's a streamheader that needs to be sent to each new client, so we
1921 * put it on our internal list of streamheader buffers.
1922 * FIXME: we could check if the buffer's contents are in fact part of the
1923 * current streamheader.
1925 * We don't send the buffer to the client, since streamheaders are sent
1926 * separately when necessary. */
1928 GST_DEBUG_OBJECT (sink, "appending HEADER buffer with length %"
1929 G_GSIZE_FORMAT " to streamheader", gst_buffer_get_size (buf));
1930 sink->streamheader = g_slist_append (sink->streamheader, buf);
1932 /* queue the buffer, this is a regular data buffer. */
1933 gst_multi_handle_sink_queue_buffer (sink, buf);
1935 sink->bytes_to_serve += gst_buffer_get_size (buf);
1943 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
1944 ("Received first buffer without caps set"));
1945 return GST_FLOW_NOT_NEGOTIATED;
1951 gst_multi_handle_sink_set_property (GObject * object, guint prop_id,
1952 const GValue * value, GParamSpec * pspec)
1954 GstMultiHandleSink *multihandlesink;
1956 multihandlesink = GST_MULTI_HANDLE_SINK (object);
1959 case PROP_BUFFERS_MAX:
1960 multihandlesink->units_max = g_value_get_int (value);
1962 case PROP_BUFFERS_SOFT_MAX:
1963 multihandlesink->units_soft_max = g_value_get_int (value);
1966 multihandlesink->time_min = g_value_get_int64 (value);
1968 case PROP_BYTES_MIN:
1969 multihandlesink->bytes_min = g_value_get_int (value);
1971 case PROP_BUFFERS_MIN:
1972 multihandlesink->buffers_min = g_value_get_int (value);
1974 case PROP_UNIT_FORMAT:
1975 multihandlesink->unit_format = g_value_get_enum (value);
1977 case PROP_UNITS_MAX:
1978 multihandlesink->units_max = g_value_get_int64 (value);
1980 case PROP_UNITS_SOFT_MAX:
1981 multihandlesink->units_soft_max = g_value_get_int64 (value);
1983 case PROP_RECOVER_POLICY:
1984 multihandlesink->recover_policy = g_value_get_enum (value);
1987 multihandlesink->timeout = g_value_get_uint64 (value);
1989 case PROP_SYNC_METHOD:
1990 multihandlesink->def_sync_method = g_value_get_enum (value);
1992 case PROP_BURST_FORMAT:
1993 multihandlesink->def_burst_format = g_value_get_enum (value);
1995 case PROP_BURST_VALUE:
1996 multihandlesink->def_burst_value = g_value_get_uint64 (value);
1999 multihandlesink->qos_dscp = g_value_get_int (value);
2000 gst_multi_handle_sink_setup_dscp (multihandlesink);
2003 case PROP_RESEND_STREAMHEADER:
2004 multihandlesink->resend_streamheader = g_value_get_boolean (value);
2008 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2014 gst_multi_handle_sink_get_property (GObject * object, guint prop_id,
2015 GValue * value, GParamSpec * pspec)
2017 GstMultiHandleSink *multihandlesink;
2019 multihandlesink = GST_MULTI_HANDLE_SINK (object);
2022 case PROP_BUFFERS_MAX:
2023 g_value_set_int (value, multihandlesink->units_max);
2025 case PROP_BUFFERS_SOFT_MAX:
2026 g_value_set_int (value, multihandlesink->units_soft_max);
2029 g_value_set_int64 (value, multihandlesink->time_min);
2031 case PROP_BYTES_MIN:
2032 g_value_set_int (value, multihandlesink->bytes_min);
2034 case PROP_BUFFERS_MIN:
2035 g_value_set_int (value, multihandlesink->buffers_min);
2037 case PROP_BUFFERS_QUEUED:
2038 g_value_set_uint (value, multihandlesink->buffers_queued);
2040 case PROP_BYTES_QUEUED:
2041 g_value_set_uint (value, multihandlesink->bytes_queued);
2043 case PROP_TIME_QUEUED:
2044 g_value_set_uint64 (value, multihandlesink->time_queued);
2046 case PROP_UNIT_FORMAT:
2047 g_value_set_enum (value, multihandlesink->unit_format);
2049 case PROP_UNITS_MAX:
2050 g_value_set_int64 (value, multihandlesink->units_max);
2052 case PROP_UNITS_SOFT_MAX:
2053 g_value_set_int64 (value, multihandlesink->units_soft_max);
2055 case PROP_RECOVER_POLICY:
2056 g_value_set_enum (value, multihandlesink->recover_policy);
2059 g_value_set_uint64 (value, multihandlesink->timeout);
2061 case PROP_SYNC_METHOD:
2062 g_value_set_enum (value, multihandlesink->def_sync_method);
2064 case PROP_BYTES_TO_SERVE:
2065 g_value_set_uint64 (value, multihandlesink->bytes_to_serve);
2067 case PROP_BYTES_SERVED:
2068 g_value_set_uint64 (value, multihandlesink->bytes_served);
2070 case PROP_BURST_FORMAT:
2071 g_value_set_enum (value, multihandlesink->def_burst_format);
2073 case PROP_BURST_VALUE:
2074 g_value_set_uint64 (value, multihandlesink->def_burst_value);
2077 g_value_set_int (value, multihandlesink->qos_dscp);
2079 case PROP_RESEND_STREAMHEADER:
2080 g_value_set_boolean (value, multihandlesink->resend_streamheader);
2082 case PROP_NUM_HANDLES:
2083 g_value_set_uint (value,
2084 g_hash_table_size (multihandlesink->handle_hash));
2087 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2092 /* create a socket for sending to remote machine */
2094 gst_multi_handle_sink_start (GstBaseSink * bsink)
2096 GstMultiHandleSinkClass *mhsclass;
2097 GstMultiHandleSink *mhsink;
2099 if (GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_HANDLE_SINK_OPEN))
2102 mhsink = GST_MULTI_HANDLE_SINK (bsink);
2103 mhsclass = GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
2105 if (!mhsclass->start_pre (mhsink))
2108 mhsink->streamheader = NULL;
2109 mhsink->bytes_to_serve = 0;
2110 mhsink->bytes_served = 0;
2112 if (mhsclass->init) {
2113 mhsclass->init (mhsink);
2116 mhsink->running = TRUE;
2118 mhsink->thread = g_thread_new ("multihandlesink",
2119 (GThreadFunc) mhsclass->thread, mhsink);
2121 GST_OBJECT_FLAG_SET (bsink, GST_MULTI_HANDLE_SINK_OPEN);
2127 gst_multi_handle_sink_stop (GstBaseSink * bsink)
2129 GstMultiHandleSinkClass *mhclass;
2132 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (bsink);
2134 mhclass = GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
2136 if (!GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_HANDLE_SINK_OPEN))
2139 mhsink->running = FALSE;
2141 mhclass->stop_pre (mhsink);
2143 if (mhsink->thread) {
2144 GST_DEBUG_OBJECT (mhsink, "joining thread");
2145 g_thread_join (mhsink->thread);
2146 GST_DEBUG_OBJECT (mhsink, "joined thread");
2147 mhsink->thread = NULL;
2150 /* free the clients */
2151 mhclass->clear (GST_MULTI_HANDLE_SINK (mhsink));
2153 if (mhsink->streamheader) {
2154 g_slist_foreach (mhsink->streamheader, (GFunc) gst_mini_object_unref, NULL);
2155 g_slist_free (mhsink->streamheader);
2156 mhsink->streamheader = NULL;
2160 mhclass->close (mhsink);
2162 mhclass->stop_post (mhsink);
2164 /* remove all queued buffers */
2165 if (mhsink->bufqueue) {
2166 GST_DEBUG_OBJECT (mhsink, "Emptying bufqueue with %d buffers",
2167 mhsink->bufqueue->len);
2168 for (i = mhsink->bufqueue->len - 1; i >= 0; --i) {
2169 buf = g_array_index (mhsink->bufqueue, GstBuffer *, i);
2170 GST_LOG_OBJECT (mhsink, "Removing buffer %p (%d) with refcount %d", buf,
2171 i, GST_MINI_OBJECT_REFCOUNT (buf));
2172 gst_buffer_unref (buf);
2173 mhsink->bufqueue = g_array_remove_index (mhsink->bufqueue, i);
2175 /* freeing the array is done in _finalize */
2177 GST_OBJECT_FLAG_UNSET (mhsink, GST_MULTI_HANDLE_SINK_OPEN);
2182 static GstStateChangeReturn
2183 gst_multi_handle_sink_change_state (GstElement * element,
2184 GstStateChange transition)
2186 GstMultiHandleSink *sink;
2187 GstStateChangeReturn ret;
2189 sink = GST_MULTI_HANDLE_SINK (element);
2191 /* we disallow changing the state from the streaming thread */
2192 if (g_thread_self () == sink->thread)
2193 return GST_STATE_CHANGE_FAILURE;
2196 switch (transition) {
2197 case GST_STATE_CHANGE_NULL_TO_READY:
2198 if (!gst_multi_handle_sink_start (GST_BASE_SINK (sink)))
2201 case GST_STATE_CHANGE_READY_TO_PAUSED:
2203 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2209 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2211 switch (transition) {
2212 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2214 case GST_STATE_CHANGE_PAUSED_TO_READY:
2216 case GST_STATE_CHANGE_READY_TO_NULL:
2217 gst_multi_handle_sink_stop (GST_BASE_SINK (sink));
2227 /* error message was posted */
2228 return GST_STATE_CHANGE_FAILURE;