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-multisocketsink
26 * @see_also: tcpserversink
28 * This plugin writes incoming data to a set of file descriptors. The
29 * file descriptors can be added to multisocketsink by emitting the #GstMultiSocketSink::add signal.
30 * For each descriptor added, the #GstMultiSocketSink::client-added signal will be called.
32 * As of version 0.10.8, a client can also be added with the #GstMultiSocketSink::add-full signal
33 * that allows for more control over what and how much data a client
36 * Clients can be removed from multisocketsink by emitting the #GstMultiSocketSink::remove signal. For
37 * each descriptor removed, the #GstMultiSocketSink::client-removed signal will be called. The
38 * #GstMultiSocketSink::client-removed signal can also be fired when multisocketsink decides that a
39 * client is not active anymore or, depending on the value of the
40 * #GstMultiSocketSink:recover-policy property, if the client is reading too slowly.
41 * In all cases, multisocketsink will never close a file descriptor itself.
42 * The user of multisocketsink is responsible for closing all file descriptors.
43 * This can for example be done in response to the #GstMultiSocketSink::client-fd-removed signal.
44 * Note that multisocketsink still has a reference to the file descriptor when the
45 * #GstMultiSocketSink::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 #GstMultiSocketSink::client-removed signal handler, and you should use the
48 * #GstMultiSocketSink::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 multisocketsink, the #GstMultiSocketSink: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 * multisocketsink (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 #GstMultiSocketSink: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 * #GstMultiSocketSink:time-min and #GstMultiSocketSink:bytes-min properties respectively.
74 * These properties are useful if the application adds clients with the
75 * #GstMultiSocketSink::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 multisocketsink receives data. If the client is reading too
80 * fast, no data will be send to the client until multisocketsink receives more
81 * data. If the client, however, reads too slowly, data for that client will be
82 * queued up in multisocketsink. Two properties control the amount of data
83 * (buffers) that is queued in multisocketsink: #GstMultiSocketSink:buffers-max and
84 * #GstMultiSocketSink:buffers-soft-max. A client that falls behind by
85 * #GstMultiSocketSink:buffers-max is removed from multisocketsink forcibly.
87 * A client with a lag of at least #GstMultiSocketSink:buffers-soft-max enters the recovery
88 * procedure which is controlled with the #GstMultiSocketSink: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 * multisocketsink 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 "gstmultisocketsink.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 (multisocketsink_debug);
124 #define GST_CAT_DEFAULT (multisocketsink_debug)
126 /* MultiSocketSink signals and args */
138 SIGNAL_CLIENT_REMOVED,
139 SIGNAL_CLIENT_SOCKET_REMOVED,
145 /* this is really arbitrarily chosen */
146 #define DEFAULT_MODE 1
147 #define DEFAULT_BUFFERS_MAX -1
148 #define DEFAULT_BUFFERS_SOFT_MAX -1
149 #define DEFAULT_UNIT_TYPE GST_FORMAT_BUFFERS
150 #define DEFAULT_UNITS_MAX -1
151 #define DEFAULT_UNITS_SOFT_MAX -1
153 #define DEFAULT_BURST_FORMAT GST_FORMAT_UNDEFINED
154 #define DEFAULT_BURST_VALUE 0
156 #define DEFAULT_QOS_DSCP -1
157 #define DEFAULT_HANDLE_READ TRUE
159 #define DEFAULT_RESEND_STREAMHEADER TRUE
174 PROP_BUFFERS_SOFT_MAX,
183 PROP_RESEND_STREAMHEADER,
191 static void gst_multi_socket_sink_finalize (GObject * object);
193 static void gst_multi_socket_sink_remove_client_link (GstMultiSocketSink * sink,
195 static gboolean gst_multi_socket_sink_socket_condition (GSocket * socket,
196 GIOCondition condition, GstMultiSocketSink * sink);
198 static GstFlowReturn gst_multi_socket_sink_render (GstBaseSink * bsink,
200 static gboolean gst_multi_socket_sink_unlock (GstBaseSink * bsink);
201 static gboolean gst_multi_socket_sink_unlock_stop (GstBaseSink * bsink);
202 static GstStateChangeReturn gst_multi_socket_sink_change_state (GstElement *
203 element, GstStateChange transition);
205 static void gst_multi_socket_sink_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec);
207 static void gst_multi_socket_sink_get_property (GObject * object, guint prop_id,
208 GValue * value, GParamSpec * pspec);
210 #define gst_multi_socket_sink_parent_class parent_class
211 G_DEFINE_TYPE (GstMultiSocketSink, gst_multi_socket_sink,
212 GST_TYPE_MULTI_HANDLE_SINK);
214 static guint gst_multi_socket_sink_signals[LAST_SIGNAL] = { 0 };
217 gst_multi_socket_sink_class_init (GstMultiSocketSinkClass * klass)
219 GObjectClass *gobject_class;
220 GstElementClass *gstelement_class;
221 GstBaseSinkClass *gstbasesink_class;
222 GstMultiHandleSinkClass *gstmultihandlesink_class;
224 gobject_class = (GObjectClass *) klass;
225 gstelement_class = (GstElementClass *) klass;
226 gstbasesink_class = (GstBaseSinkClass *) klass;
227 gstmultihandlesink_class = (GstMultiHandleSinkClass *) klass;
229 gobject_class->set_property = gst_multi_socket_sink_set_property;
230 gobject_class->get_property = gst_multi_socket_sink_get_property;
231 gobject_class->finalize = gst_multi_socket_sink_finalize;
233 g_object_class_install_property (gobject_class, PROP_BUFFERS_MAX,
234 g_param_spec_int ("buffers-max", "Buffers max",
235 "max number of buffers to queue for a client (-1 = no limit)", -1,
236 G_MAXINT, DEFAULT_BUFFERS_MAX,
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, PROP_BUFFERS_SOFT_MAX,
239 g_param_spec_int ("buffers-soft-max", "Buffers soft max",
240 "Recover client when going over this limit (-1 = no limit)", -1,
241 G_MAXINT, DEFAULT_BUFFERS_SOFT_MAX,
242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_UNIT_TYPE,
245 g_param_spec_enum ("unit-type", "Units type",
246 "The unit to measure the max/soft-max/queued properties",
247 GST_TYPE_FORMAT, DEFAULT_UNIT_TYPE,
248 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 g_object_class_install_property (gobject_class, PROP_UNITS_MAX,
250 g_param_spec_int64 ("units-max", "Units max",
251 "max number of units to queue (-1 = no limit)", -1, G_MAXINT64,
252 DEFAULT_UNITS_MAX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
253 g_object_class_install_property (gobject_class, PROP_UNITS_SOFT_MAX,
254 g_param_spec_int64 ("units-soft-max", "Units soft max",
255 "Recover client when going over this limit (-1 = no limit)", -1,
256 G_MAXINT64, DEFAULT_UNITS_SOFT_MAX,
257 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 g_object_class_install_property (gobject_class, PROP_BUFFERS_QUEUED,
260 g_param_spec_uint ("buffers-queued", "Buffers queued",
261 "Number of buffers currently queued", 0, G_MAXUINT, 0,
262 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
264 g_object_class_install_property (gobject_class, PROP_BYTES_QUEUED,
265 g_param_spec_uint ("bytes-queued", "Bytes queued",
266 "Number of bytes currently queued", 0, G_MAXUINT, 0,
267 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
268 g_object_class_install_property (gobject_class, PROP_TIME_QUEUED,
269 g_param_spec_uint64 ("time-queued", "Time queued",
270 "Number of time currently queued", 0, G_MAXUINT64, 0,
271 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
274 g_object_class_install_property (gobject_class, PROP_BURST_FORMAT,
275 g_param_spec_enum ("burst-format", "Burst format",
276 "The format of the burst units (when sync-method is burst[[-with]-keyframe])",
277 GST_TYPE_FORMAT, DEFAULT_BURST_FORMAT,
278 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (gobject_class, PROP_BURST_VALUE,
280 g_param_spec_uint64 ("burst-value", "Burst value",
281 "The amount of burst expressed in burst-unit", 0, G_MAXUINT64,
282 DEFAULT_BURST_VALUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_QOS_DSCP,
285 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
286 "Quality of Service, differentiated services code point (-1 default)",
287 -1, 63, DEFAULT_QOS_DSCP,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 * GstMultiSocketSink::handle-read
293 * Handle read requests from clients and discard the data.
297 g_object_class_install_property (gobject_class, PROP_HANDLE_READ,
298 g_param_spec_boolean ("handle-read", "Handle Read",
299 "Handle client reads and discard the data",
300 DEFAULT_HANDLE_READ, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
302 * GstMultiSocketSink::resend-streamheader
304 * Resend the streamheaders to existing clients when they change.
308 g_object_class_install_property (gobject_class, PROP_RESEND_STREAMHEADER,
309 g_param_spec_boolean ("resend-streamheader", "Resend streamheader",
310 "Resend the streamheader if it changes in the caps",
311 DEFAULT_RESEND_STREAMHEADER,
312 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
314 g_object_class_install_property (gobject_class, PROP_NUM_SOCKETS,
315 g_param_spec_uint ("num-sockets", "Number of sockets",
316 "The current number of client sockets",
317 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
320 * GstMultiSocketSink::add:
321 * @gstmultisocketsink: the multisocketsink element to emit this signal on
322 * @socket: the socket to add to multisocketsink
324 * Hand the given open socket to multisocketsink to write to.
326 gst_multi_socket_sink_signals[SIGNAL_ADD] =
327 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
328 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
329 G_STRUCT_OFFSET (GstMultiSocketSinkClass, add), NULL, NULL,
330 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_SOCKET);
332 * GstMultiSocketSink::add-full:
333 * @gstmultisocketsink: the multisocketsink element to emit this signal on
334 * @socket: the socket to add to multisocketsink
335 * @sync: the sync method to use
336 * @format_min: the format of @value_min
337 * @value_min: the minimum amount of data to burst expressed in
339 * @format_max: the format of @value_max
340 * @value_max: the maximum amount of data to burst expressed in
343 * Hand the given open socket to multisocketsink to write to and
344 * specify the burst parameters for the new connection.
346 gst_multi_socket_sink_signals[SIGNAL_ADD_BURST] =
347 g_signal_new ("add-full", G_TYPE_FROM_CLASS (klass),
348 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
349 G_STRUCT_OFFSET (GstMultiSocketSinkClass, add_full), NULL, NULL,
350 gst_tcp_marshal_VOID__OBJECT_ENUM_ENUM_UINT64_ENUM_UINT64, G_TYPE_NONE, 6,
351 G_TYPE_SOCKET, GST_TYPE_SYNC_METHOD, GST_TYPE_FORMAT, G_TYPE_UINT64,
352 GST_TYPE_FORMAT, G_TYPE_UINT64);
354 * GstMultiSocketSink::remove:
355 * @gstmultisocketsink: the multisocketsink element to emit this signal on
356 * @socket: the socket to remove from multisocketsink
358 * Remove the given open socket from multisocketsink.
360 gst_multi_socket_sink_signals[SIGNAL_REMOVE] =
361 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
362 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
363 G_STRUCT_OFFSET (GstMultiSocketSinkClass, remove), NULL, NULL,
364 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_SOCKET);
366 * GstMultiSocketSink::remove-flush:
367 * @gstmultisocketsink: the multisocketsink element to emit this signal on
368 * @socket: the socket to remove from multisocketsink
370 * Remove the given open socket from multisocketsink after flushing all
371 * the pending data to the socket.
373 gst_multi_socket_sink_signals[SIGNAL_REMOVE_FLUSH] =
374 g_signal_new ("remove-flush", G_TYPE_FROM_CLASS (klass),
375 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
376 G_STRUCT_OFFSET (GstMultiSocketSinkClass, remove_flush), NULL, NULL,
377 g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, G_TYPE_SOCKET);
380 * GstMultiSocketSink::get-stats:
381 * @gstmultisocketsink: the multisocketsink element to emit this signal on
382 * @socket: the socket to get stats of from multisocketsink
384 * Get statistics about @socket. This function returns a GstStructure.
386 * Returns: a GstStructure with the statistics. The structure contains
387 * values that represent: total number of bytes sent, time
388 * when the client was added, time when the client was
389 * disconnected/removed, time the client is/was active, last activity
390 * time (in epoch seconds), number of buffers dropped.
391 * All times are expressed in nanoseconds (GstClockTime).
393 gst_multi_socket_sink_signals[SIGNAL_GET_STATS] =
394 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
395 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
396 G_STRUCT_OFFSET (GstMultiSocketSinkClass, get_stats), NULL, NULL,
397 gst_tcp_marshal_BOXED__OBJECT, GST_TYPE_STRUCTURE, 1, G_TYPE_SOCKET);
400 * GstMultiSocketSink::client-added:
401 * @gstmultisocketsink: the multisocketsink element that emitted this signal
402 * @socket: the socket that was added to multisocketsink
404 * The given socket was added to multisocketsink. This signal will
405 * be emitted from the streaming thread so application should be prepared
408 gst_multi_socket_sink_signals[SIGNAL_CLIENT_ADDED] =
409 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
410 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiSocketSinkClass,
411 client_added), NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
412 G_TYPE_NONE, 1, G_TYPE_OBJECT);
414 * GstMultiSocketSink::client-removed:
415 * @gstmultisocketsink: the multisocketsink element that emitted this signal
416 * @socket: the socket that is to be removed from multisocketsink
417 * @status: the reason why the client was removed
419 * The given socket is about to be removed from multisocketsink. This
420 * signal will be emitted from the streaming thread so applications should
421 * be prepared for that.
423 * @gstmultisocketsink still holds a handle to @socket so it is possible to call
424 * the get-stats signal from this callback. For the same reason it is
425 * not safe to close() and reuse @socket in this callback.
427 gst_multi_socket_sink_signals[SIGNAL_CLIENT_REMOVED] =
428 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
429 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiSocketSinkClass,
430 client_removed), NULL, NULL, gst_tcp_marshal_VOID__OBJECT_ENUM,
431 G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CLIENT_STATUS);
433 * GstMultiSocketSink::client-socket-removed:
434 * @gstmultisocketsink: the multisocketsink element that emitted this signal
435 * @socket: the socket that was removed from multisocketsink
437 * The given socket was removed from multisocketsink. This signal will
438 * be emitted from the streaming thread so applications should be prepared
441 * In this callback, @gstmultisocketsink has removed all the information
442 * associated with @socket and it is therefore not possible to call get-stats
443 * with @socket. It is however safe to close() and reuse @fd in the callback.
447 gst_multi_socket_sink_signals[SIGNAL_CLIENT_SOCKET_REMOVED] =
448 g_signal_new ("client-socket-removed", G_TYPE_FROM_CLASS (klass),
449 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiSocketSinkClass,
450 client_socket_removed), NULL, NULL, g_cclosure_marshal_VOID__OBJECT,
451 G_TYPE_NONE, 1, G_TYPE_SOCKET);
453 gst_element_class_add_pad_template (gstelement_class,
454 gst_static_pad_template_get (&sinktemplate));
456 gst_element_class_set_details_simple (gstelement_class,
457 "Multi socket sink", "Sink/Network",
458 "Send data to multiple sockets",
459 "Thomas Vander Stichele <thomas at apestaart dot org>, "
460 "Wim Taymans <wim@fluendo.com>, "
461 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
463 gstelement_class->change_state =
464 GST_DEBUG_FUNCPTR (gst_multi_socket_sink_change_state);
466 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_render);
467 gstbasesink_class->unlock = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_unlock);
468 gstbasesink_class->unlock_stop =
469 GST_DEBUG_FUNCPTR (gst_multi_socket_sink_unlock_stop);
471 gstmultihandlesink_class->clear =
472 GST_DEBUG_FUNCPTR (gst_multi_socket_sink_clear);
474 klass->add = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_add);
475 klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_add_full);
476 klass->remove = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_remove);
477 klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_remove_flush);
478 klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_socket_sink_get_stats);
480 GST_DEBUG_CATEGORY_INIT (multisocketsink_debug, "multisocketsink", 0,
481 "Multi socket sink");
485 gst_multi_socket_sink_init (GstMultiSocketSink * this)
487 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_HANDLE_SINK_OPEN);
489 CLIENTS_LOCK_INIT (this);
490 this->clients = NULL;
491 this->socket_hash = g_hash_table_new (g_direct_hash, g_int_equal);
493 this->bufqueue = g_array_new (FALSE, TRUE, sizeof (GstBuffer *));
494 this->unit_type = DEFAULT_UNIT_TYPE;
495 this->units_max = DEFAULT_UNITS_MAX;
496 this->units_soft_max = DEFAULT_UNITS_SOFT_MAX;
498 this->def_burst_format = DEFAULT_BURST_FORMAT;
499 this->def_burst_value = DEFAULT_BURST_VALUE;
501 this->qos_dscp = DEFAULT_QOS_DSCP;
502 this->handle_read = DEFAULT_HANDLE_READ;
504 this->resend_streamheader = DEFAULT_RESEND_STREAMHEADER;
506 this->header_flags = 0;
507 this->cancellable = g_cancellable_new ();
511 gst_multi_socket_sink_finalize (GObject * object)
513 GstMultiSocketSink *this;
515 this = GST_MULTI_SOCKET_SINK (object);
517 CLIENTS_LOCK_CLEAR (this);
518 g_hash_table_destroy (this->socket_hash);
519 g_array_free (this->bufqueue, TRUE);
521 if (this->cancellable) {
522 g_object_unref (this->cancellable);
523 this->cancellable = NULL;
526 G_OBJECT_CLASS (parent_class)->finalize (object);
530 setup_dscp_client (GstMultiSocketSink * sink, GstSocketClient * client)
541 struct sockaddr_in6 sa_in6;
542 struct sockaddr_storage sa_stor;
544 socklen_t slen = sizeof (sa);
548 if (sink->qos_dscp < 0)
551 fd = g_socket_get_fd (client->socket);
553 if ((ret = getsockname (fd, &sa.sa, &slen)) < 0) {
554 GST_DEBUG_OBJECT (sink, "could not get sockname: %s", g_strerror (errno));
558 af = sa.sa.sa_family;
560 /* if this is an IPv4-mapped address then do IPv4 QoS */
561 if (af == AF_INET6) {
563 GST_DEBUG_OBJECT (sink, "check IP6 socket");
564 if (IN6_IS_ADDR_V4MAPPED (&(sa.sa_in6.sin6_addr))) {
565 GST_DEBUG_OBJECT (sink, "mapped to IPV4");
570 /* extract and shift 6 bits of the DSCP */
571 tos = (sink->qos_dscp & 0x3f) << 2;
575 ret = setsockopt (fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos));
579 ret = setsockopt (fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof (tos));
584 GST_ERROR_OBJECT (sink, "unsupported AF");
588 GST_DEBUG_OBJECT (sink, "could not set DSCP: %s", g_strerror (errno));
595 setup_dscp (GstMultiSocketSink * sink)
600 for (clients = sink->clients; clients; clients = clients->next) {
601 GstSocketClient *client;
603 client = clients->data;
605 setup_dscp_client (sink, client);
607 CLIENTS_UNLOCK (sink);
610 /* "add-full" signal implementation */
612 gst_multi_socket_sink_add_full (GstMultiSocketSink * sink, GSocket * socket,
613 GstSyncMethod sync_method, GstFormat min_format, guint64 min_value,
614 GstFormat max_format, guint64 max_value)
616 GstSocketClient *client;
617 GstMultiHandleClient *mhclient;
620 GST_DEBUG_OBJECT (sink, "[socket %p] adding client, sync_method %d, "
621 "min_format %d, min_value %" G_GUINT64_FORMAT
622 ", max_format %d, max_value %" G_GUINT64_FORMAT, socket,
623 sync_method, min_format, min_value, max_format, max_value);
625 /* do limits check if we can */
626 if (min_format == max_format) {
627 if (max_value != -1 && min_value != -1 && max_value < min_value)
631 /* create client datastructure */
632 client = g_new0 (GstSocketClient, 1);
633 mhclient = (GstMultiHandleClient *) client;
634 gst_multi_handle_sink_client_init (mhclient, sync_method);
635 client->socket = G_SOCKET (g_object_ref (socket));
637 client->burst_min_format = min_format;
638 client->burst_min_value = min_value;
639 client->burst_max_format = max_format;
640 client->burst_max_value = max_value;
644 /* check the hash to find a duplicate fd */
645 clink = g_hash_table_lookup (sink->socket_hash, socket);
649 /* we can add the fd now */
650 clink = sink->clients = g_list_prepend (sink->clients, client);
651 g_hash_table_insert (sink->socket_hash, socket, clink);
652 sink->clients_cookie++;
654 /* set the socket to non blocking */
655 g_socket_set_blocking (socket, FALSE);
657 /* we always read from a client */
658 if (sink->main_context) {
660 g_socket_create_source (client->socket,
661 G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP, sink->cancellable);
662 g_source_set_callback (client->source,
663 (GSourceFunc) gst_multi_socket_sink_socket_condition,
664 gst_object_ref (sink), (GDestroyNotify) gst_object_unref);
665 g_source_attach (client->source, sink->main_context);
668 setup_dscp_client (sink, client);
670 CLIENTS_UNLOCK (sink);
672 g_signal_emit (G_OBJECT (sink),
673 gst_multi_socket_sink_signals[SIGNAL_CLIENT_ADDED], 0, socket);
680 GST_WARNING_OBJECT (sink,
681 "[socket %p] wrong values min =%" G_GUINT64_FORMAT ", max=%"
682 G_GUINT64_FORMAT ", format %d specified when adding client", socket,
683 min_value, max_value, min_format);
688 mhclient->status = GST_CLIENT_STATUS_DUPLICATE;
689 CLIENTS_UNLOCK (sink);
690 GST_WARNING_OBJECT (sink, "[socket %p] duplicate client found, refusing",
692 g_signal_emit (G_OBJECT (sink),
693 gst_multi_socket_sink_signals[SIGNAL_CLIENT_REMOVED], 0, socket,
700 /* "add" signal implementation */
702 gst_multi_socket_sink_add (GstMultiSocketSink * sink, GSocket * socket)
704 GstMultiHandleSink *mhsink;
706 mhsink = GST_MULTI_HANDLE_SINK (sink);
707 gst_multi_socket_sink_add_full (sink, socket, mhsink->def_sync_method,
708 sink->def_burst_format, sink->def_burst_value, sink->def_burst_format,
712 /* "remove" signal implementation */
714 gst_multi_socket_sink_remove (GstMultiSocketSink * sink, GSocket * socket)
718 GST_DEBUG_OBJECT (sink, "[socket %p] removing client", socket);
721 clink = g_hash_table_lookup (sink->socket_hash, socket);
723 GstSocketClient *client = clink->data;
724 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
726 if (mhclient->status != GST_CLIENT_STATUS_OK) {
727 GST_INFO_OBJECT (sink,
728 "[socket %p] Client already disconnecting with status %d",
729 socket, mhclient->status);
733 mhclient->status = GST_CLIENT_STATUS_REMOVED;
734 gst_multi_socket_sink_remove_client_link (sink, clink);
736 GST_WARNING_OBJECT (sink, "[socket %p] no client with this socket found!",
741 CLIENTS_UNLOCK (sink);
744 /* "remove-flush" signal implementation */
746 gst_multi_socket_sink_remove_flush (GstMultiSocketSink * sink, GSocket * socket)
750 GST_DEBUG_OBJECT (sink, "[socket %p] flushing client", socket);
753 clink = g_hash_table_lookup (sink->socket_hash, socket);
755 GstSocketClient *client = clink->data;
756 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
758 if (mhclient->status != GST_CLIENT_STATUS_OK) {
759 GST_INFO_OBJECT (sink,
760 "[socket %p] Client already disconnecting with status %d",
761 socket, mhclient->status);
765 /* take the position of the client as the number of buffers left to flush.
766 * If the client was at position -1, we flush 0 buffers, 0 == flush 1
768 mhclient->flushcount = mhclient->bufpos + 1;
769 /* mark client as flushing. We can not remove the client right away because
770 * it might have some buffers to flush in the ->sending queue. */
771 mhclient->status = GST_CLIENT_STATUS_FLUSHING;
773 GST_WARNING_OBJECT (sink, "[socket %p] no client with this fd found!",
777 CLIENTS_UNLOCK (sink);
780 /* can be called both through the signal (i.e. from any thread) or when
781 * stopping, after the writing thread has shut down */
783 gst_multi_socket_sink_clear (GstMultiHandleSink * mhsink)
787 GstMultiSocketSink *sink = GST_MULTI_SOCKET_SINK (mhsink);
789 GST_DEBUG_OBJECT (sink, "clearing all clients");
793 cookie = sink->clients_cookie;
794 for (clients = sink->clients; clients; clients = clients->next) {
795 GstMultiHandleClient *mhclient;
797 if (cookie != sink->clients_cookie) {
798 GST_DEBUG_OBJECT (sink, "cookie changed while removing all clients");
802 mhclient = (GstMultiHandleClient *) clients->data;
803 mhclient->status = GST_CLIENT_STATUS_REMOVED;
804 gst_multi_socket_sink_remove_client_link (sink, clients);
807 CLIENTS_UNLOCK (sink);
810 /* "get-stats" signal implementation
813 gst_multi_socket_sink_get_stats (GstMultiSocketSink * sink, GSocket * socket)
815 GstSocketClient *client;
816 GstStructure *result = NULL;
820 clink = g_hash_table_lookup (sink->socket_hash, socket);
824 client = clink->data;
825 if (client != NULL) {
826 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
829 result = gst_structure_new_empty ("multisocketsink-stats");
831 if (mhclient->disconnect_time == 0) {
834 g_get_current_time (&nowtv);
836 interval = GST_TIMEVAL_TO_TIME (nowtv) - mhclient->connect_time;
838 interval = mhclient->disconnect_time - mhclient->connect_time;
841 gst_structure_set (result,
842 "bytes-sent", G_TYPE_UINT64, mhclient->bytes_sent,
843 "connect-time", G_TYPE_UINT64, mhclient->connect_time,
844 "disconnect-time", G_TYPE_UINT64, mhclient->disconnect_time,
845 "connected-duration", G_TYPE_UINT64, interval,
846 "last-activatity-time", G_TYPE_UINT64, mhclient->last_activity_time,
847 "dropped-buffers", G_TYPE_UINT64, mhclient->dropped_buffers,
848 "first-buffer-ts", G_TYPE_UINT64, mhclient->first_buffer_ts,
849 "last-buffer-ts", G_TYPE_UINT64, mhclient->last_buffer_ts, NULL);
853 CLIENTS_UNLOCK (sink);
855 /* python doesn't like a NULL pointer yet */
856 if (result == NULL) {
857 GST_WARNING_OBJECT (sink, "[socket %p] no client with this found!", socket);
858 result = gst_structure_new_empty ("multisocketsink-stats");
864 /* should be called with the clientslock helt.
865 * Note that we don't close the fd as we didn't open it in the first
866 * place. An application should connect to the client-fd-removed signal and
867 * close the fd itself.
870 gst_multi_socket_sink_remove_client_link (GstMultiSocketSink * sink,
875 GstSocketClient *client = link->data;
876 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
877 GstMultiSocketSinkClass *fclass;
879 fclass = GST_MULTI_SOCKET_SINK_GET_CLASS (sink);
881 socket = client->socket;
883 if (mhclient->currently_removing) {
884 GST_WARNING_OBJECT (sink, "[socket %p] client is already being removed",
888 mhclient->currently_removing = TRUE;
891 /* FIXME: if we keep track of ip we can log it here and signal */
892 switch (mhclient->status) {
893 case GST_CLIENT_STATUS_OK:
894 GST_WARNING_OBJECT (sink, "[socket %p] removing client %p for no reason",
897 case GST_CLIENT_STATUS_CLOSED:
898 GST_DEBUG_OBJECT (sink, "[socket %p] removing client %p because of close",
901 case GST_CLIENT_STATUS_REMOVED:
902 GST_DEBUG_OBJECT (sink,
903 "[socket %p] removing client %p because the app removed it", socket,
906 case GST_CLIENT_STATUS_SLOW:
907 GST_INFO_OBJECT (sink,
908 "[socket %p] removing client %p because it was too slow", socket,
911 case GST_CLIENT_STATUS_ERROR:
912 GST_WARNING_OBJECT (sink,
913 "[socket %p] removing client %p because of error", socket, client);
915 case GST_CLIENT_STATUS_FLUSHING:
917 GST_WARNING_OBJECT (sink,
918 "[socket %p] removing client %p with invalid reason %d", socket,
919 client, mhclient->status);
923 if (client->source) {
924 g_source_destroy (client->source);
925 g_source_unref (client->source);
926 client->source = NULL;
929 g_get_current_time (&now);
930 mhclient->disconnect_time = GST_TIMEVAL_TO_TIME (now);
932 /* free client buffers */
933 g_slist_foreach (mhclient->sending, (GFunc) gst_mini_object_unref, NULL);
934 g_slist_free (mhclient->sending);
935 mhclient->sending = NULL;
938 gst_caps_unref (mhclient->caps);
939 mhclient->caps = NULL;
941 /* unlock the mutex before signaling because the signal handler
942 * might query some properties */
943 CLIENTS_UNLOCK (sink);
945 g_signal_emit (G_OBJECT (sink),
946 gst_multi_socket_sink_signals[SIGNAL_CLIENT_REMOVED], 0, socket,
949 /* lock again before we remove the client completely */
952 /* fd cannot be reused in the above signal callback so we can safely
953 * remove it from the hashtable here */
954 if (!g_hash_table_remove (sink->socket_hash, socket)) {
955 GST_WARNING_OBJECT (sink,
956 "[socket %p] error removing client %p from hash", socket, client);
958 /* after releasing the lock above, the link could be invalid, more
959 * precisely, the next and prev pointers could point to invalid list
960 * links. One optimisation could be to add a cookie to the linked list
961 * and take a shortcut when it did not change between unlocking and locking
962 * our mutex. For now we just walk the list again. */
963 sink->clients = g_list_remove (sink->clients, client);
964 sink->clients_cookie++;
967 fclass->removed (sink, socket);
970 CLIENTS_UNLOCK (sink);
972 /* and the fd is really gone now */
973 g_signal_emit (G_OBJECT (sink),
974 gst_multi_socket_sink_signals[SIGNAL_CLIENT_SOCKET_REMOVED], 0, socket);
975 g_object_unref (socket);
980 /* handle a read on a client socket,
981 * which either indicates a close or should be ignored
982 * returns FALSE if some error occured or the client closed. */
984 gst_multi_socket_sink_handle_client_read (GstMultiSocketSink * sink,
985 GstSocketClient * client)
991 gboolean first = TRUE;
992 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
994 GST_DEBUG_OBJECT (sink, "[socket %p] select reports client read",
999 /* just Read 'n' Drop, could also just drop the client as it's not supposed
1000 * to write to us except for closing the socket, I guess it's because we
1001 * like to listen to our customers. */
1005 GST_DEBUG_OBJECT (sink, "[socket %p] client wants us to read",
1008 navail = g_socket_get_available_bytes (client->socket);
1013 g_socket_receive (client->socket, dummy, MIN (navail, sizeof (dummy)),
1014 sink->cancellable, &err);
1015 if (first && nread == 0) {
1016 /* client sent close, so remove it */
1017 GST_DEBUG_OBJECT (sink, "[socket %p] client asked for close, removing",
1019 mhclient->status = GST_CLIENT_STATUS_CLOSED;
1021 } else if (nread < 0) {
1022 GST_WARNING_OBJECT (sink, "[socket %p] could not read: %s",
1023 client->socket, err->message);
1024 mhclient->status = GST_CLIENT_STATUS_ERROR;
1029 } while (nread > 0);
1030 g_clear_error (&err);
1036 is_sync_frame (GstMultiSocketSink * sink, GstBuffer * buffer)
1038 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1040 } else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_HEADER)) {
1047 /* queue the given buffer for the given client */
1049 gst_multi_socket_sink_client_queue_buffer (GstMultiSocketSink * sink,
1050 GstSocketClient * client, GstBuffer * buffer)
1052 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1055 /* TRUE: send them if the new caps have them */
1056 gboolean send_streamheader = FALSE;
1059 /* before we queue the buffer, we check if we need to queue streamheader
1060 * buffers (because it's a new client, or because they changed) */
1061 caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (sink));
1063 if (!mhclient->caps) {
1064 GST_DEBUG_OBJECT (sink,
1065 "[socket %p] no previous caps for this client, send streamheader",
1067 send_streamheader = TRUE;
1068 mhclient->caps = gst_caps_ref (caps);
1070 /* there were previous caps recorded, so compare */
1071 if (!gst_caps_is_equal (caps, mhclient->caps)) {
1072 const GValue *sh1, *sh2;
1074 /* caps are not equal, but could still have the same streamheader */
1075 s = gst_caps_get_structure (caps, 0);
1076 if (!gst_structure_has_field (s, "streamheader")) {
1077 /* no new streamheader, so nothing new to send */
1078 GST_DEBUG_OBJECT (sink,
1079 "[socket %p] new caps do not have streamheader, not sending",
1082 /* there is a new streamheader */
1083 s = gst_caps_get_structure (mhclient->caps, 0);
1084 if (!gst_structure_has_field (s, "streamheader")) {
1085 /* no previous streamheader, so send the new one */
1086 GST_DEBUG_OBJECT (sink,
1087 "[socket %p] previous caps did not have streamheader, sending",
1089 send_streamheader = TRUE;
1091 /* both old and new caps have streamheader set */
1092 if (!sink->resend_streamheader) {
1093 GST_DEBUG_OBJECT (sink,
1094 "[socket %p] asked to not resend the streamheader, not sending",
1096 send_streamheader = FALSE;
1098 sh1 = gst_structure_get_value (s, "streamheader");
1099 s = gst_caps_get_structure (caps, 0);
1100 sh2 = gst_structure_get_value (s, "streamheader");
1101 if (gst_value_compare (sh1, sh2) != GST_VALUE_EQUAL) {
1102 GST_DEBUG_OBJECT (sink,
1103 "[socket %p] new streamheader different from old, sending",
1105 send_streamheader = TRUE;
1111 /* Replace the old caps */
1112 gst_caps_unref (mhclient->caps);
1113 mhclient->caps = gst_caps_ref (caps);
1116 if (G_UNLIKELY (send_streamheader)) {
1121 GST_LOG_OBJECT (sink,
1122 "[socket %p] sending streamheader from caps %" GST_PTR_FORMAT,
1123 client->socket, caps);
1124 s = gst_caps_get_structure (caps, 0);
1125 if (!gst_structure_has_field (s, "streamheader")) {
1126 GST_DEBUG_OBJECT (sink,
1127 "[socket %p] no new streamheader, so nothing to send",
1130 GST_LOG_OBJECT (sink,
1131 "[socket %p] sending streamheader from caps %" GST_PTR_FORMAT,
1132 client->socket, caps);
1133 sh = gst_structure_get_value (s, "streamheader");
1134 g_assert (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
1135 buffers = g_value_peek_pointer (sh);
1136 GST_DEBUG_OBJECT (sink, "%d streamheader buffers", buffers->len);
1137 for (i = 0; i < buffers->len; ++i) {
1141 bufval = &g_array_index (buffers, GValue, i);
1142 g_assert (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER);
1143 buffer = g_value_peek_pointer (bufval);
1144 GST_DEBUG_OBJECT (sink,
1145 "[socket %p] queueing streamheader buffer of length %"
1146 G_GSIZE_FORMAT, client->socket, gst_buffer_get_size (buffer));
1147 gst_buffer_ref (buffer);
1149 mhclient->sending = g_slist_append (mhclient->sending, buffer);
1154 gst_caps_unref (caps);
1157 GST_LOG_OBJECT (sink,
1158 "[socket %p] queueing buffer of length %" G_GSIZE_FORMAT, client->socket,
1159 gst_buffer_get_size (buffer));
1161 gst_buffer_ref (buffer);
1162 mhclient->sending = g_slist_append (mhclient->sending, buffer);
1167 /* find the keyframe in the list of buffers starting the
1168 * search from @idx. @direction as -1 will search backwards,
1169 * 1 will search forwards.
1170 * Returns: the index or -1 if there is no keyframe after idx.
1173 find_syncframe (GstMultiSocketSink * sink, gint idx, gint direction)
1175 gint i, len, result;
1177 /* take length of queued buffers */
1178 len = sink->bufqueue->len;
1180 /* assume we don't find a keyframe */
1183 /* then loop over all buffers to find the first keyframe */
1184 for (i = idx; i >= 0 && i < len; i += direction) {
1187 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1188 if (is_sync_frame (sink, buf)) {
1189 GST_LOG_OBJECT (sink, "found keyframe at %d from %d, direction %d",
1198 #define find_next_syncframe(s,i) find_syncframe(s,i,1)
1199 #define find_prev_syncframe(s,i) find_syncframe(s,i,-1)
1201 /* Get the number of buffers from the buffer queue needed to satisfy
1202 * the maximum max in the configured units.
1203 * If units are not BUFFERS, and there are insufficient buffers in the
1204 * queue to satify the limit, return len(queue) + 1 */
1206 get_buffers_max (GstMultiSocketSink * sink, gint64 max)
1208 switch (sink->unit_type) {
1209 case GST_FORMAT_BUFFERS:
1211 case GST_FORMAT_TIME:
1217 GstClockTime first = GST_CLOCK_TIME_NONE;
1219 len = sink->bufqueue->len;
1221 for (i = 0; i < len; i++) {
1222 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1223 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1225 first = GST_BUFFER_TIMESTAMP (buf);
1227 diff = first - GST_BUFFER_TIMESTAMP (buf);
1235 case GST_FORMAT_BYTES:
1242 len = sink->bufqueue->len;
1244 for (i = 0; i < len; i++) {
1245 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1246 acc += gst_buffer_get_size (buf);
1258 /* find the positions in the buffer queue where *_min and *_max
1261 /* count the amount of data in the buffers and return the index
1262 * that satifies the given limits.
1264 * Returns: index @idx in the buffer queue so that the given limits are
1265 * satisfied. TRUE if all the limits could be satisfied, FALSE if not
1266 * enough data was in the queue.
1268 * FIXME, this code might now work if any of the units is in buffers...
1271 find_limits (GstMultiSocketSink * sink,
1272 gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
1273 gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max)
1275 GstClockTime first, time;
1277 gboolean result, max_hit;
1279 /* take length of queue */
1280 len = sink->bufqueue->len;
1282 /* this must hold */
1285 GST_LOG_OBJECT (sink,
1286 "bytes_min %d, buffers_min %d, time_min %" GST_TIME_FORMAT
1287 ", bytes_max %d, buffers_max %d, time_max %" GST_TIME_FORMAT, bytes_min,
1288 buffers_min, GST_TIME_ARGS (time_min), bytes_max, buffers_max,
1289 GST_TIME_ARGS (time_max));
1291 /* do the trivial buffer limit test */
1292 if (buffers_min != -1 && len < buffers_min) {
1299 /* else count bytes and time */
1308 /* loop through the buffers, when a limit is ok, mark it
1309 * as -1, we have at least one buffer in the queue. */
1313 /* if we checked all min limits, update result */
1314 if (bytes_min == -1 && time_min == -1 && *min_idx == -1) {
1315 /* don't go below 0 */
1316 *min_idx = MAX (i - 1, 0);
1318 /* if we reached one max limit break out */
1320 /* i > 0 when we get here, we subtract one to get the position
1321 * of the previous buffer. */
1323 /* we have valid complete result if we found a min_idx too */
1324 result = *min_idx != -1;
1327 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1329 bytes += gst_buffer_get_size (buf);
1331 /* take timestamp and save for the base first timestamp */
1332 if ((time = GST_BUFFER_TIMESTAMP (buf)) != -1) {
1333 GST_LOG_OBJECT (sink, "Ts %" GST_TIME_FORMAT " on buffer",
1334 GST_TIME_ARGS (time));
1338 /* increase max usage if we did not fill enough. Note that
1339 * buffers are sorted from new to old, so the first timestamp is
1340 * bigger than the next one. */
1341 if (time_min != -1 && first - time >= time_min)
1343 if (time_max != -1 && first - time >= time_max)
1346 GST_LOG_OBJECT (sink, "No timestamp on buffer");
1348 /* time is OK or unknown, check and increase if not enough bytes */
1349 if (bytes_min != -1) {
1350 if (bytes >= bytes_min)
1353 if (bytes_max != -1) {
1354 if (bytes >= bytes_max) {
1362 /* if we did not hit the max or min limit, set to buffer size */
1365 /* make sure min does not exceed max */
1367 *min_idx = *max_idx;
1372 /* parse the unit/value pair and assign it to the result value of the
1373 * right type, leave the other values untouched
1375 * Returns: FALSE if the unit is unknown or undefined. TRUE otherwise.
1378 assign_value (GstFormat format, guint64 value, gint * bytes, gint * buffers,
1379 GstClockTime * time)
1381 gboolean res = TRUE;
1383 /* set only the limit of the given format to the given value */
1385 case GST_FORMAT_BUFFERS:
1386 *buffers = (gint) value;
1388 case GST_FORMAT_TIME:
1391 case GST_FORMAT_BYTES:
1392 *bytes = (gint) value;
1394 case GST_FORMAT_UNDEFINED:
1402 /* count the index in the buffer queue to satisfy the given unit
1403 * and value pair starting from buffer at index 0.
1405 * Returns: TRUE if there was enough data in the queue to satisfy the
1406 * burst values. @idx contains the index in the buffer that contains enough
1407 * data to satisfy the limits or the last buffer in the queue when the
1408 * function returns FALSE.
1411 count_burst_unit (GstMultiSocketSink * sink, gint * min_idx,
1412 GstFormat min_format, guint64 min_value, gint * max_idx,
1413 GstFormat max_format, guint64 max_value)
1415 gint bytes_min = -1, buffers_min = -1;
1416 gint bytes_max = -1, buffers_max = -1;
1417 GstClockTime time_min = GST_CLOCK_TIME_NONE, time_max = GST_CLOCK_TIME_NONE;
1419 assign_value (min_format, min_value, &bytes_min, &buffers_min, &time_min);
1420 assign_value (max_format, max_value, &bytes_max, &buffers_max, &time_max);
1422 return find_limits (sink, min_idx, bytes_min, buffers_min, time_min,
1423 max_idx, bytes_max, buffers_max, time_max);
1426 /* decide where in the current buffer queue this new client should start
1427 * receiving buffers from.
1428 * This function is called whenever a client is connected and has not yet
1429 * received a buffer.
1430 * If this returns -1, it means that we haven't found a good point to
1431 * start streaming from yet, and this function should be called again later
1432 * when more buffers have arrived.
1435 gst_multi_socket_sink_new_client (GstMultiSocketSink * sink,
1436 GstSocketClient * client)
1439 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1441 GST_DEBUG_OBJECT (sink,
1442 "[socket %p] new client, deciding where to start in queue",
1444 GST_DEBUG_OBJECT (sink, "queue is currently %d buffers long",
1445 sink->bufqueue->len);
1446 switch (mhclient->sync_method) {
1447 case GST_SYNC_METHOD_LATEST:
1448 /* no syncing, we are happy with whatever the client is going to get */
1449 result = mhclient->bufpos;
1450 GST_DEBUG_OBJECT (sink,
1451 "[socket %p] SYNC_METHOD_LATEST, position %d", client->socket,
1454 case GST_SYNC_METHOD_NEXT_KEYFRAME:
1456 /* if one of the new buffers (between mhclient->bufpos and 0) in the queue
1457 * is a sync point, we can proceed, otherwise we need to keep waiting */
1458 GST_LOG_OBJECT (sink,
1459 "[socket %p] new client, bufpos %d, waiting for keyframe",
1460 client->socket, mhclient->bufpos);
1462 result = find_prev_syncframe (sink, mhclient->bufpos);
1464 GST_DEBUG_OBJECT (sink,
1465 "[socket %p] SYNC_METHOD_NEXT_KEYFRAME: result %d",
1466 client->socket, result);
1470 /* client is not on a syncbuffer, need to skip these buffers and
1472 GST_LOG_OBJECT (sink,
1473 "[socket %p] new client, skipping buffer(s), no syncpoint found",
1475 mhclient->bufpos = -1;
1478 case GST_SYNC_METHOD_LATEST_KEYFRAME:
1480 GST_DEBUG_OBJECT (sink,
1481 "[socket %p] SYNC_METHOD_LATEST_KEYFRAME", client->socket);
1483 /* for new clients we initially scan the complete buffer queue for
1484 * a sync point when a buffer is added. If we don't find a keyframe,
1485 * we need to wait for the next keyframe and so we change the client's
1486 * sync method to GST_SYNC_METHOD_NEXT_KEYFRAME.
1488 result = find_next_syncframe (sink, 0);
1490 GST_DEBUG_OBJECT (sink,
1491 "[socket %p] SYNC_METHOD_LATEST_KEYFRAME: result %d",
1492 client->socket, result);
1496 GST_DEBUG_OBJECT (sink,
1497 "[socket %p] SYNC_METHOD_LATEST_KEYFRAME: no keyframe found, "
1498 "switching to SYNC_METHOD_NEXT_KEYFRAME", client->socket);
1499 /* throw client to the waiting state */
1500 mhclient->bufpos = -1;
1501 /* and make client sync to next keyframe */
1502 mhclient->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1505 case GST_SYNC_METHOD_BURST:
1510 /* move to the position where we satisfy the client's burst
1511 * parameters. If we could not satisfy the parameters because there
1512 * is not enough data, we just send what we have (which is in result).
1513 * We use the max value to limit the search
1515 ok = count_burst_unit (sink, &result, client->burst_min_format,
1516 client->burst_min_value, &max, client->burst_max_format,
1517 client->burst_max_value);
1518 GST_DEBUG_OBJECT (sink,
1519 "[socket %p] SYNC_METHOD_BURST: burst_unit returned %d, result %d",
1520 client->socket, ok, result);
1522 GST_LOG_OBJECT (sink, "min %d, max %d", result, max);
1524 /* we hit the max and it is below the min, use that then */
1525 if (max != -1 && max <= result) {
1526 result = MAX (max - 1, 0);
1527 GST_DEBUG_OBJECT (sink,
1528 "[socket %p] SYNC_METHOD_BURST: result above max, taken down to %d",
1529 client->socket, result);
1533 case GST_SYNC_METHOD_BURST_KEYFRAME:
1535 gint min_idx, max_idx;
1536 gint next_syncframe, prev_syncframe;
1540 * _always_ start sending a keyframe to the client. We first search
1541 * a keyframe between min/max limits. If there is none, we send it the
1542 * last keyframe before min. If there is none, the behaviour is like
1545 /* gather burst limits */
1546 count_burst_unit (sink, &min_idx, client->burst_min_format,
1547 client->burst_min_value, &max_idx, client->burst_max_format,
1548 client->burst_max_value);
1550 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1552 /* first find a keyframe after min_idx */
1553 next_syncframe = find_next_syncframe (sink, min_idx);
1554 if (next_syncframe != -1 && next_syncframe < max_idx) {
1555 /* we have a valid keyframe and it's below the max */
1556 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1557 result = next_syncframe;
1561 /* no valid keyframe, try to find one below min */
1562 prev_syncframe = find_prev_syncframe (sink, min_idx);
1563 if (prev_syncframe != -1) {
1564 GST_WARNING_OBJECT (sink,
1565 "using keyframe below min in BURST_KEYFRAME sync mode");
1566 result = prev_syncframe;
1570 /* no prev keyframe or not enough data */
1571 GST_WARNING_OBJECT (sink,
1572 "no prev keyframe found in BURST_KEYFRAME sync mode, waiting for next");
1574 /* throw client to the waiting state */
1575 mhclient->bufpos = -1;
1576 /* and make client sync to next keyframe */
1577 mhclient->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1581 case GST_SYNC_METHOD_BURST_WITH_KEYFRAME:
1583 gint min_idx, max_idx;
1584 gint next_syncframe;
1586 /* BURST_WITH_KEYFRAME:
1588 * try to start sending a keyframe to the client. We first search
1589 * a keyframe between min/max limits. If there is none, we send it the
1590 * amount of data up 'till min.
1592 /* gather enough data to burst */
1593 count_burst_unit (sink, &min_idx, client->burst_min_format,
1594 client->burst_min_value, &max_idx, client->burst_max_format,
1595 client->burst_max_value);
1597 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1599 /* first find a keyframe after min_idx */
1600 next_syncframe = find_next_syncframe (sink, min_idx);
1601 if (next_syncframe != -1 && next_syncframe < max_idx) {
1602 /* we have a valid keyframe and it's below the max */
1603 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1604 result = next_syncframe;
1608 /* no keyframe, send data from min_idx */
1609 GST_WARNING_OBJECT (sink, "using min in BURST_WITH_KEYFRAME sync mode");
1611 /* make sure we don't go over the max limit */
1612 if (max_idx != -1 && max_idx <= min_idx) {
1613 result = MAX (max_idx - 1, 0);
1621 g_warning ("unknown sync method %d", mhclient->sync_method);
1622 result = mhclient->bufpos;
1628 /* Handle a write on a client,
1629 * which indicates a read request from a client.
1631 * For each client we maintain a queue of GstBuffers that contain the raw bytes
1632 * we need to send to the client.
1634 * We first check to see if we need to send streamheaders. If so, we queue them.
1636 * Then we run into the main loop that tries to send as many buffers as
1637 * possible. It will first exhaust the mhclient->sending queue and if the queue
1638 * is empty, it will pick a buffer from the global queue.
1640 * Sending the buffers from the mhclient->sending queue is basically writing
1641 * the bytes to the socket and maintaining a count of the bytes that were
1642 * sent. When the buffer is completely sent, it is removed from the
1643 * mhclient->sending queue and we try to pick a new buffer for sending.
1645 * When the sending returns a partial buffer we stop sending more data as
1646 * the next send operation could block.
1648 * This functions returns FALSE if some error occured.
1651 gst_multi_socket_sink_handle_client_write (GstMultiSocketSink * sink,
1652 GstSocketClient * client)
1654 GSocket *socket = client->socket;
1660 GstMultiHandleSink *mhsink;
1661 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1663 mhsink = GST_MULTI_HANDLE_SINK (sink);
1665 g_get_current_time (&nowtv);
1666 now = GST_TIMEVAL_TO_TIME (nowtv);
1668 flushing = mhclient->status == GST_CLIENT_STATUS_FLUSHING;
1674 if (!mhclient->sending) {
1675 /* client is not working on a buffer */
1676 if (mhclient->bufpos == -1) {
1677 /* client is too fast, remove from write queue until new buffer is
1679 if (client->source) {
1680 g_source_destroy (client->source);
1681 g_source_unref (client->source);
1682 client->source = NULL;
1684 /* if we flushed out all of the client buffers, we can stop */
1685 if (mhclient->flushcount == 0)
1690 /* client can pick a buffer from the global queue */
1692 GstClockTime timestamp;
1694 /* for new connections, we need to find a good spot in the
1695 * bufqueue to start streaming from */
1696 if (mhclient->new_connection && !flushing) {
1697 gint position = gst_multi_socket_sink_new_client (sink, client);
1699 if (position >= 0) {
1700 /* we got a valid spot in the queue */
1701 mhclient->new_connection = FALSE;
1702 mhclient->bufpos = position;
1704 /* cannot send data to this client yet */
1705 if (client->source) {
1706 g_source_destroy (client->source);
1707 g_source_unref (client->source);
1708 client->source = NULL;
1714 /* we flushed all remaining buffers, no need to get a new one */
1715 if (mhclient->flushcount == 0)
1719 buf = g_array_index (sink->bufqueue, GstBuffer *, mhclient->bufpos);
1723 timestamp = GST_BUFFER_TIMESTAMP (buf);
1724 if (mhclient->first_buffer_ts == GST_CLOCK_TIME_NONE)
1725 mhclient->first_buffer_ts = timestamp;
1726 if (timestamp != -1)
1727 mhclient->last_buffer_ts = timestamp;
1729 /* decrease flushcount */
1730 if (mhclient->flushcount != -1)
1731 mhclient->flushcount--;
1733 GST_LOG_OBJECT (sink, "[socket %p] client %p at position %d",
1734 socket, client, mhclient->bufpos);
1736 /* queueing a buffer will ref it */
1737 gst_multi_socket_sink_client_queue_buffer (sink, client, buf);
1739 /* need to start from the first byte for this new buffer */
1740 mhclient->bufoffset = 0;
1744 /* see if we need to send something */
1745 if (mhclient->sending) {
1750 /* pick first buffer from list */
1751 head = GST_BUFFER (mhclient->sending->data);
1753 gst_buffer_map (head, &map, GST_MAP_READ);
1754 maxsize = map.size - mhclient->bufoffset;
1756 /* try to write the complete buffer */
1759 g_socket_send (socket, (gchar *) map.data + mhclient->bufoffset,
1760 maxsize, sink->cancellable, &err);
1761 gst_buffer_unmap (head, &map);
1765 if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CLOSED)) {
1766 goto connection_reset;
1771 if (wrote < maxsize) {
1772 /* partial write means that the client cannot read more and we should
1773 * stop sending more */
1774 GST_LOG_OBJECT (sink,
1775 "partial write on %p of %" G_GSSIZE_FORMAT " bytes", socket,
1777 mhclient->bufoffset += wrote;
1780 /* complete buffer was written, we can proceed to the next one */
1781 mhclient->sending = g_slist_remove (mhclient->sending, head);
1782 gst_buffer_unref (head);
1783 /* make sure we start from byte 0 for the next buffer */
1784 mhclient->bufoffset = 0;
1787 mhclient->bytes_sent += wrote;
1788 mhclient->last_activity_time = now;
1789 mhsink->bytes_served += wrote;
1799 GST_DEBUG_OBJECT (sink, "[socket %p] flushed, removing", socket);
1800 mhclient->status = GST_CLIENT_STATUS_REMOVED;
1805 GST_DEBUG_OBJECT (sink, "[socket %p] connection reset by peer, removing",
1807 mhclient->status = GST_CLIENT_STATUS_CLOSED;
1808 g_clear_error (&err);
1813 GST_WARNING_OBJECT (sink,
1814 "[socket %p] could not write, removing client: %s", socket,
1816 g_clear_error (&err);
1817 mhclient->status = GST_CLIENT_STATUS_ERROR;
1822 /* calculate the new position for a client after recovery. This function
1823 * does not update the client position but merely returns the required
1827 gst_multi_socket_sink_recover_client (GstMultiSocketSink * sink,
1828 GstSocketClient * client)
1831 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1832 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1834 GST_WARNING_OBJECT (sink,
1835 "[socket %p] client %p is lagging at %d, recover using policy %d",
1836 client->socket, client, mhclient->bufpos, mhsink->recover_policy);
1838 switch (mhsink->recover_policy) {
1839 case GST_RECOVER_POLICY_NONE:
1840 /* do nothing, client will catch up or get kicked out when it reaches
1842 newbufpos = mhclient->bufpos;
1844 case GST_RECOVER_POLICY_RESYNC_LATEST:
1845 /* move to beginning of queue */
1848 case GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT:
1849 /* move to beginning of soft max */
1850 newbufpos = get_buffers_max (sink, sink->units_soft_max);
1852 case GST_RECOVER_POLICY_RESYNC_KEYFRAME:
1853 /* find keyframe in buffers, we search backwards to find the
1854 * closest keyframe relative to what this client already received. */
1855 newbufpos = MIN (sink->bufqueue->len - 1,
1856 get_buffers_max (sink, sink->units_soft_max) - 1);
1858 while (newbufpos >= 0) {
1861 buf = g_array_index (sink->bufqueue, GstBuffer *, newbufpos);
1862 if (is_sync_frame (sink, buf)) {
1863 /* found a buffer that is not a delta unit */
1870 /* unknown recovery procedure */
1871 newbufpos = get_buffers_max (sink, sink->units_soft_max);
1877 /* Queue a buffer on the global queue.
1879 * This function adds the buffer to the front of a GArray. It removes the
1880 * tail buffer if the max queue size is exceeded, unreffing the queued buffer.
1881 * Note that unreffing the buffer is not a problem as clients who
1882 * started writing out this buffer will still have a reference to it in the
1883 * mhclient->sending queue.
1885 * After adding the buffer, we update all client positions in the queue. If
1886 * a client moves over the soft max, we start the recovery procedure for this
1887 * slow client. If it goes over the hard max, it is put into the slow list
1890 * Special care is taken of clients that were waiting for a new buffer (they
1891 * had a position of -1) because they can proceed after adding this new buffer.
1892 * This is done by adding the client back into the write fd_set and signaling
1893 * the select thread that the fd_set changed.
1896 gst_multi_socket_sink_queue_buffer (GstMultiSocketSink * sink, GstBuffer * buf)
1898 GList *clients, *next;
1900 gint max_buffer_usage;
1904 gint max_buffers, soft_max_buffers;
1906 GstMultiHandleSink *mhsink;
1908 mhsink = GST_MULTI_HANDLE_SINK (sink);
1910 g_get_current_time (&nowtv);
1911 now = GST_TIMEVAL_TO_TIME (nowtv);
1913 CLIENTS_LOCK (sink);
1914 /* add buffer to queue */
1915 gst_buffer_ref (buf);
1916 g_array_prepend_val (sink->bufqueue, buf);
1917 queuelen = sink->bufqueue->len;
1919 if (sink->units_max > 0)
1920 max_buffers = get_buffers_max (sink, sink->units_max);
1924 if (sink->units_soft_max > 0)
1925 soft_max_buffers = get_buffers_max (sink, sink->units_soft_max);
1927 soft_max_buffers = -1;
1928 GST_LOG_OBJECT (sink, "Using max %d, softmax %d", max_buffers,
1931 /* then loop over the clients and update the positions */
1932 max_buffer_usage = 0;
1935 cookie = sink->clients_cookie;
1936 for (clients = sink->clients; clients; clients = next) {
1937 GstSocketClient *client;
1938 GstMultiHandleClient *mhclient;
1940 if (cookie != sink->clients_cookie) {
1941 GST_DEBUG_OBJECT (sink, "Clients cookie outdated, restarting");
1945 client = clients->data;
1946 mhclient = (GstMultiHandleClient *) client;
1947 next = g_list_next (clients);
1950 GST_LOG_OBJECT (sink, "[socket %p] client %p at position %d",
1951 client->socket, client, mhclient->bufpos);
1952 /* check soft max if needed, recover client */
1953 if (soft_max_buffers > 0 && mhclient->bufpos >= soft_max_buffers) {
1956 newpos = gst_multi_socket_sink_recover_client (sink, client);
1957 if (newpos != mhclient->bufpos) {
1958 mhclient->dropped_buffers += mhclient->bufpos - newpos;
1959 mhclient->bufpos = newpos;
1960 mhclient->discont = TRUE;
1961 GST_INFO_OBJECT (sink, "[socket %p] client %p position reset to %d",
1962 client->socket, client, mhclient->bufpos);
1964 GST_INFO_OBJECT (sink,
1965 "[socket %p] client %p not recovering position",
1966 client->socket, client);
1969 /* check hard max and timeout, remove client */
1970 if ((max_buffers > 0 && mhclient->bufpos >= max_buffers) ||
1971 (mhsink->timeout > 0
1972 && now - mhclient->last_activity_time > mhsink->timeout)) {
1974 GST_WARNING_OBJECT (sink, "[socket %p] client %p is too slow, removing",
1975 client->socket, client);
1976 /* remove the client, the fd set will be cleared and the select thread
1977 * will be signaled */
1978 mhclient->status = GST_CLIENT_STATUS_SLOW;
1979 /* set client to invalid position while being removed */
1980 mhclient->bufpos = -1;
1981 gst_multi_socket_sink_remove_client_link (sink, clients);
1983 } else if (mhclient->bufpos == 0 || mhclient->new_connection) {
1984 /* can send data to this client now. need to signal the select thread that
1985 * the fd_set changed */
1986 if (!client->source) {
1988 g_socket_create_source (client->socket,
1989 G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP,
1991 g_source_set_callback (client->source,
1992 (GSourceFunc) gst_multi_socket_sink_socket_condition,
1993 gst_object_ref (sink), (GDestroyNotify) gst_object_unref);
1994 g_source_attach (client->source, sink->main_context);
1997 /* keep track of maximum buffer usage */
1998 if (mhclient->bufpos > max_buffer_usage) {
1999 max_buffer_usage = mhclient->bufpos;
2003 /* make sure we respect bytes-min, buffers-min and time-min when they are set */
2007 GST_LOG_OBJECT (sink,
2008 "extending queue %d to respect time_min %" GST_TIME_FORMAT
2009 ", bytes_min %d, buffers_min %d", max_buffer_usage,
2010 GST_TIME_ARGS (mhsink->time_min), mhsink->bytes_min,
2011 mhsink->buffers_min);
2013 /* get index where the limits are ok, we don't really care if all limits
2014 * are ok, we just queue as much as we need. We also don't compare against
2015 * the max limits. */
2016 find_limits (sink, &usage, mhsink->bytes_min, mhsink->buffers_min,
2017 mhsink->time_min, &max, -1, -1, -1);
2019 max_buffer_usage = MAX (max_buffer_usage, usage + 1);
2020 GST_LOG_OBJECT (sink, "extended queue to %d", max_buffer_usage);
2023 /* now look for sync points and make sure there is at least one
2024 * sync point in the queue. We only do this if the LATEST_KEYFRAME or
2025 * BURST_KEYFRAME mode is selected */
2026 if (mhsink->def_sync_method == GST_SYNC_METHOD_LATEST_KEYFRAME ||
2027 mhsink->def_sync_method == GST_SYNC_METHOD_BURST_KEYFRAME) {
2028 /* no point in searching beyond the queue length */
2029 gint limit = queuelen;
2032 /* no point in searching beyond the soft-max if any. */
2033 if (soft_max_buffers > 0) {
2034 limit = MIN (limit, soft_max_buffers);
2036 GST_LOG_OBJECT (sink,
2037 "extending queue to include sync point, now at %d, limit is %d",
2038 max_buffer_usage, limit);
2039 for (i = 0; i < limit; i++) {
2040 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
2041 if (is_sync_frame (sink, buf)) {
2042 /* found a sync frame, now extend the buffer usage to
2043 * include at least this frame. */
2044 max_buffer_usage = MAX (max_buffer_usage, i);
2048 GST_LOG_OBJECT (sink, "max buffer usage is now %d", max_buffer_usage);
2051 GST_LOG_OBJECT (sink, "len %d, usage %d", queuelen, max_buffer_usage);
2053 /* nobody is referencing units after max_buffer_usage so we can
2054 * remove them from the queue. We remove them in reverse order as
2055 * this is the most optimal for GArray. */
2056 for (i = queuelen - 1; i > max_buffer_usage; i--) {
2059 /* queue exceeded max size */
2061 old = g_array_index (sink->bufqueue, GstBuffer *, i);
2062 sink->bufqueue = g_array_remove_index (sink->bufqueue, i);
2064 /* unref tail buffer */
2065 gst_buffer_unref (old);
2067 /* save for stats */
2068 sink->buffers_queued = max_buffer_usage;
2069 CLIENTS_UNLOCK (sink);
2072 /* Handle the clients. This is called when a socket becomes ready
2073 * to read or writable. Badly behaving clients are put on a
2074 * garbage list and removed.
2077 gst_multi_socket_sink_socket_condition (GSocket * socket,
2078 GIOCondition condition, GstMultiSocketSink * sink)
2081 GstSocketClient *client;
2082 gboolean ret = TRUE;
2083 GstMultiHandleClient *mhclient;
2085 CLIENTS_LOCK (sink);
2086 clink = g_hash_table_lookup (sink->socket_hash, socket);
2087 if (clink == NULL) {
2092 client = clink->data;
2093 mhclient = (GstMultiHandleClient *) client;
2095 if (mhclient->status != GST_CLIENT_STATUS_FLUSHING
2096 && mhclient->status != GST_CLIENT_STATUS_OK) {
2097 gst_multi_socket_sink_remove_client_link (sink, clink);
2102 if ((condition & G_IO_ERR)) {
2103 GST_WARNING_OBJECT (sink, "Socket %p has error", client->socket);
2104 mhclient->status = GST_CLIENT_STATUS_ERROR;
2105 gst_multi_socket_sink_remove_client_link (sink, clink);
2108 } else if ((condition & G_IO_HUP)) {
2109 mhclient->status = GST_CLIENT_STATUS_CLOSED;
2110 gst_multi_socket_sink_remove_client_link (sink, clink);
2113 } else if ((condition & G_IO_IN) || (condition & G_IO_PRI)) {
2114 /* handle client read */
2115 if (!gst_multi_socket_sink_handle_client_read (sink, client)) {
2116 gst_multi_socket_sink_remove_client_link (sink, clink);
2120 } else if ((condition & G_IO_OUT)) {
2121 /* handle client write */
2122 if (!gst_multi_socket_sink_handle_client_write (sink, client)) {
2123 gst_multi_socket_sink_remove_client_link (sink, clink);
2130 CLIENTS_UNLOCK (sink);
2136 gst_multi_socket_sink_timeout (GstMultiSocketSink * sink)
2141 GstMultiHandleSink *mhsink;
2143 mhsink = GST_MULTI_HANDLE_SINK (sink);
2145 g_get_current_time (&nowtv);
2146 now = GST_TIMEVAL_TO_TIME (nowtv);
2148 CLIENTS_LOCK (sink);
2149 for (clients = sink->clients; clients; clients = clients->next) {
2150 GstSocketClient *client;
2151 GstMultiHandleClient *mhclient;
2153 client = clients->data;
2154 mhclient = (GstMultiHandleClient *) client;
2155 if (mhsink->timeout > 0
2156 && now - mhclient->last_activity_time > mhsink->timeout) {
2157 mhclient->status = GST_CLIENT_STATUS_SLOW;
2158 gst_multi_socket_sink_remove_client_link (sink, clients);
2161 CLIENTS_UNLOCK (sink);
2166 /* we handle the client communication in another thread so that we do not block
2167 * the gstreamer thread while we select() on the client fds */
2169 gst_multi_socket_sink_thread (GstMultiSocketSink * sink)
2171 GSource *timeout = NULL;
2172 GstMultiHandleSink *mhsink;
2174 mhsink = GST_MULTI_HANDLE_SINK (sink);
2176 while (sink->running) {
2177 if (mhsink->timeout > 0) {
2178 timeout = g_timeout_source_new (mhsink->timeout / GST_MSECOND);
2180 g_source_set_callback (timeout,
2181 (GSourceFunc) gst_multi_socket_sink_timeout, gst_object_ref (sink),
2182 (GDestroyNotify) gst_object_unref);
2183 g_source_attach (timeout, sink->main_context);
2186 /* Returns after handling all pending events or when
2187 * _wakeup() was called. In any case we have to add
2188 * a new timeout because something happened.
2190 g_main_context_iteration (sink->main_context, TRUE);
2193 g_source_destroy (timeout);
2194 g_source_unref (timeout);
2201 static GstFlowReturn
2202 gst_multi_socket_sink_render (GstBaseSink * bsink, GstBuffer * buf)
2204 GstMultiSocketSink *sink;
2207 GstCaps *bufcaps, *padcaps;
2209 GstMultiHandleSink *mhsink;
2211 sink = GST_MULTI_SOCKET_SINK (bsink);
2212 mhsink = GST_MULTI_HANDLE_SINK (sink);
2214 g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink,
2215 GST_MULTI_SOCKET_SINK_OPEN), GST_FLOW_FLUSHING);
2218 /* since we check every buffer for streamheader caps, we need to make
2219 * sure every buffer has caps set */
2220 bufcaps = gst_buffer_get_caps (buf);
2221 padcaps = GST_PAD_CAPS (GST_BASE_SINK_PAD (bsink));
2223 /* make sure we have caps on the pad */
2224 if (!padcaps && !bufcaps)
2228 /* get HEADER first, code below might mess with the flags */
2229 in_caps = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_HEADER);
2232 /* stamp the buffer with previous caps if no caps set */
2234 if (!gst_buffer_is_writable (buf)) {
2235 /* metadata is not writable, copy will be made and original buffer
2236 * will be unreffed so we need to ref so that we don't lose the
2237 * buffer in the render method. */
2238 gst_buffer_ref (buf);
2239 /* the new buffer is ours only, we keep it out of the scope of this
2241 buf = gst_buffer_make_writable (buf);
2243 /* else the metadata is writable, we ref because we keep the buffer
2244 * out of the scope of this method */
2245 gst_buffer_ref (buf);
2247 /* buffer metadata is writable now, set the caps */
2248 gst_buffer_set_caps (buf, padcaps);
2250 gst_caps_unref (bufcaps);
2252 /* since we keep this buffer out of the scope of this method */
2253 gst_buffer_ref (buf);
2257 GST_LOG_OBJECT (sink, "received buffer %p, in_caps: %s, offset %"
2258 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
2259 ", timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2260 buf, in_caps ? "yes" : "no", GST_BUFFER_OFFSET (buf),
2261 GST_BUFFER_OFFSET_END (buf),
2262 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
2263 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
2265 /* if we get HEADER buffers, but the previous buffer was not HEADER,
2266 * it means we're getting new streamheader buffers, and we should clear
2268 if (in_caps && sink->previous_buffer_in_caps == FALSE) {
2269 GST_DEBUG_OBJECT (sink,
2270 "receiving new HEADER buffers, clearing old streamheader");
2271 g_slist_foreach (sink->streamheader, (GFunc) gst_mini_object_unref, NULL);
2272 g_slist_free (sink->streamheader);
2273 sink->streamheader = NULL;
2276 /* save the current in_caps */
2277 sink->previous_buffer_in_caps = in_caps;
2279 /* if the incoming buffer is marked as IN CAPS, then we assume for now
2280 * it's a streamheader that needs to be sent to each new client, so we
2281 * put it on our internal list of streamheader buffers.
2282 * FIXME: we could check if the buffer's contents are in fact part of the
2283 * current streamheader.
2285 * We don't send the buffer to the client, since streamheaders are sent
2286 * separately when necessary. */
2288 GST_DEBUG_OBJECT (sink, "appending HEADER buffer with length %"
2289 G_GSIZE_FORMAT " to streamheader", gst_buffer_get_size (buf));
2290 sink->streamheader = g_slist_append (sink->streamheader, buf);
2292 /* queue the buffer, this is a regular data buffer. */
2293 gst_multi_socket_sink_queue_buffer (sink, buf);
2295 mhsink->bytes_to_serve += gst_buffer_get_size (buf);
2303 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
2304 ("Received first buffer without caps set"));
2305 return GST_FLOW_NOT_NEGOTIATED;
2311 gst_multi_socket_sink_set_property (GObject * object, guint prop_id,
2312 const GValue * value, GParamSpec * pspec)
2314 GstMultiSocketSink *multisocketsink;
2316 multisocketsink = GST_MULTI_SOCKET_SINK (object);
2319 case PROP_BUFFERS_MAX:
2320 multisocketsink->units_max = g_value_get_int (value);
2322 case PROP_BUFFERS_SOFT_MAX:
2323 multisocketsink->units_soft_max = g_value_get_int (value);
2325 case PROP_UNIT_TYPE:
2326 multisocketsink->unit_type = g_value_get_enum (value);
2328 case PROP_UNITS_MAX:
2329 multisocketsink->units_max = g_value_get_int64 (value);
2331 case PROP_UNITS_SOFT_MAX:
2332 multisocketsink->units_soft_max = g_value_get_int64 (value);
2334 case PROP_BURST_FORMAT:
2335 multisocketsink->def_burst_format = g_value_get_enum (value);
2337 case PROP_BURST_VALUE:
2338 multisocketsink->def_burst_value = g_value_get_uint64 (value);
2341 multisocketsink->qos_dscp = g_value_get_int (value);
2342 setup_dscp (multisocketsink);
2344 case PROP_HANDLE_READ:
2345 multisocketsink->handle_read = g_value_get_boolean (value);
2347 case PROP_RESEND_STREAMHEADER:
2348 multisocketsink->resend_streamheader = g_value_get_boolean (value);
2352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2358 gst_multi_socket_sink_get_property (GObject * object, guint prop_id,
2359 GValue * value, GParamSpec * pspec)
2361 GstMultiSocketSink *multisocketsink;
2363 multisocketsink = GST_MULTI_SOCKET_SINK (object);
2366 case PROP_BUFFERS_MAX:
2367 g_value_set_int (value, multisocketsink->units_max);
2369 case PROP_BUFFERS_SOFT_MAX:
2370 g_value_set_int (value, multisocketsink->units_soft_max);
2372 case PROP_BUFFERS_QUEUED:
2373 g_value_set_uint (value, multisocketsink->buffers_queued);
2375 case PROP_BYTES_QUEUED:
2376 g_value_set_uint (value, multisocketsink->bytes_queued);
2378 case PROP_TIME_QUEUED:
2379 g_value_set_uint64 (value, multisocketsink->time_queued);
2381 case PROP_UNIT_TYPE:
2382 g_value_set_enum (value, multisocketsink->unit_type);
2384 case PROP_UNITS_MAX:
2385 g_value_set_int64 (value, multisocketsink->units_max);
2387 case PROP_UNITS_SOFT_MAX:
2388 g_value_set_int64 (value, multisocketsink->units_soft_max);
2390 case PROP_BURST_FORMAT:
2391 g_value_set_enum (value, multisocketsink->def_burst_format);
2393 case PROP_BURST_VALUE:
2394 g_value_set_uint64 (value, multisocketsink->def_burst_value);
2397 g_value_set_int (value, multisocketsink->qos_dscp);
2399 case PROP_HANDLE_READ:
2400 g_value_set_boolean (value, multisocketsink->handle_read);
2402 case PROP_RESEND_STREAMHEADER:
2403 g_value_set_boolean (value, multisocketsink->resend_streamheader);
2405 case PROP_NUM_SOCKETS:
2406 g_value_set_uint (value,
2407 g_hash_table_size (multisocketsink->socket_hash));
2411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2417 /* create a socket for sending to remote machine */
2419 gst_multi_socket_sink_start (GstBaseSink * bsink)
2421 GstMultiSocketSinkClass *fclass;
2422 GstMultiSocketSink *this;
2423 GstMultiHandleSink *mhsink;
2427 if (GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_HANDLE_SINK_OPEN))
2430 this = GST_MULTI_SOCKET_SINK (bsink);
2431 mhsink = GST_MULTI_HANDLE_SINK (bsink);
2432 fclass = GST_MULTI_SOCKET_SINK_GET_CLASS (this);
2434 GST_INFO_OBJECT (this, "starting");
2436 this->main_context = g_main_context_new ();
2438 CLIENTS_LOCK (this);
2439 for (clients = this->clients; clients; clients = clients->next) {
2440 GstSocketClient *client;
2442 client = clients->data;
2446 g_socket_create_source (client->socket,
2447 G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP, this->cancellable);
2448 g_source_set_callback (client->source,
2449 (GSourceFunc) gst_multi_socket_sink_socket_condition,
2450 gst_object_ref (this), (GDestroyNotify) gst_object_unref);
2451 g_source_attach (client->source, this->main_context);
2453 CLIENTS_UNLOCK (this);
2455 this->streamheader = NULL;
2456 mhsink->bytes_to_serve = 0;
2457 mhsink->bytes_served = 0;
2460 fclass->init (this);
2463 this->running = TRUE;
2465 this->thread = g_thread_new ("multisocketsink",
2466 (GThreadFunc) gst_multi_socket_sink_thread, this);
2468 GST_OBJECT_FLAG_SET (this, GST_MULTI_HANDLE_SINK_OPEN);
2474 multisocketsink_hash_remove (gpointer key, gpointer value, gpointer data)
2480 gst_multi_socket_sink_stop (GstBaseSink * bsink)
2482 GstMultiSocketSinkClass *fclass;
2483 GstMultiHandleSinkClass *mhclass;
2484 GstMultiSocketSink *this;
2488 this = GST_MULTI_SOCKET_SINK (bsink);
2489 fclass = GST_MULTI_SOCKET_SINK_GET_CLASS (this);
2490 mhclass = GST_MULTI_HANDLE_SINK_GET_CLASS (this);
2492 if (!GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_HANDLE_SINK_OPEN))
2495 this->running = FALSE;
2497 if (this->main_context)
2498 g_main_context_wakeup (this->main_context);
2501 GST_DEBUG_OBJECT (this, "joining thread");
2502 g_thread_join (this->thread);
2503 GST_DEBUG_OBJECT (this, "joined thread");
2504 this->thread = NULL;
2507 /* free the clients */
2508 mhclass->clear (GST_MULTI_HANDLE_SINK (this));
2510 if (this->streamheader) {
2511 g_slist_foreach (this->streamheader, (GFunc) gst_mini_object_unref, NULL);
2512 g_slist_free (this->streamheader);
2513 this->streamheader = NULL;
2517 fclass->close (this);
2519 if (this->main_context) {
2520 g_main_context_unref (this->main_context);
2521 this->main_context = NULL;
2524 g_hash_table_foreach_remove (this->socket_hash, multisocketsink_hash_remove,
2527 /* remove all queued buffers */
2528 if (this->bufqueue) {
2529 GST_DEBUG_OBJECT (this, "Emptying bufqueue with %d buffers",
2530 this->bufqueue->len);
2531 for (i = this->bufqueue->len - 1; i >= 0; --i) {
2532 buf = g_array_index (this->bufqueue, GstBuffer *, i);
2533 GST_LOG_OBJECT (this, "Removing buffer %p (%d) with refcount %d", buf, i,
2534 GST_MINI_OBJECT_REFCOUNT (buf));
2535 gst_buffer_unref (buf);
2536 this->bufqueue = g_array_remove_index (this->bufqueue, i);
2538 /* freeing the array is done in _finalize */
2540 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_HANDLE_SINK_OPEN);
2545 static GstStateChangeReturn
2546 gst_multi_socket_sink_change_state (GstElement * element,
2547 GstStateChange transition)
2549 GstMultiSocketSink *sink;
2550 GstStateChangeReturn ret;
2552 sink = GST_MULTI_SOCKET_SINK (element);
2554 /* we disallow changing the state from the streaming thread */
2555 if (g_thread_self () == sink->thread)
2556 return GST_STATE_CHANGE_FAILURE;
2559 switch (transition) {
2560 case GST_STATE_CHANGE_NULL_TO_READY:
2561 if (!gst_multi_socket_sink_start (GST_BASE_SINK (sink)))
2564 case GST_STATE_CHANGE_READY_TO_PAUSED:
2566 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2572 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2574 switch (transition) {
2575 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2577 case GST_STATE_CHANGE_PAUSED_TO_READY:
2579 case GST_STATE_CHANGE_READY_TO_NULL:
2580 gst_multi_socket_sink_stop (GST_BASE_SINK (sink));
2590 /* error message was posted */
2591 return GST_STATE_CHANGE_FAILURE;
2596 gst_multi_socket_sink_unlock (GstBaseSink * bsink)
2598 GstMultiSocketSink *sink;
2600 sink = GST_MULTI_SOCKET_SINK (bsink);
2602 GST_DEBUG_OBJECT (sink, "set to flushing");
2603 g_cancellable_cancel (sink->cancellable);
2604 if (sink->main_context)
2605 g_main_context_wakeup (sink->main_context);
2610 /* will be called only between calls to start() and stop() */
2612 gst_multi_socket_sink_unlock_stop (GstBaseSink * bsink)
2614 GstMultiSocketSink *sink;
2616 sink = GST_MULTI_SOCKET_SINK (bsink);
2618 GST_DEBUG_OBJECT (sink, "unset flushing");
2619 g_cancellable_reset (sink->cancellable);