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>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-multifdsink
24 * @see_also: tcpserversink
26 * This plugin writes incoming data to a set of file descriptors. The
27 * file descriptors can be added to multifdsink by emitting the #GstMultiFdSink::add signal.
28 * For each descriptor added, the #GstMultiFdSink::client-added signal will be called.
30 * As of version 0.10.8, a client can also be added with the #GstMultiFdSink::add-full signal
31 * that allows for more control over what and how much data a client
34 * Clients can be removed from multifdsink by emitting the #GstMultiFdSink::remove signal. For
35 * each descriptor removed, the #GstMultiFdSink::client-removed signal will be called. The
36 * #GstMultiFdSink::client-removed signal can also be fired when multifdsink decides that a
37 * client is not active anymore or, depending on the value of the
38 * #GstMultiFdSink:recover-policy property, if the client is reading too slowly.
39 * In all cases, multifdsink will never close a file descriptor itself.
40 * The user of multifdsink is responsible for closing all file descriptors.
41 * This can for example be done in response to the #GstMultiFdSink::client-fd-removed signal.
42 * Note that multifdsink still has a reference to the file descriptor when the
43 * #GstMultiFdSink::client-removed signal is emitted, so that "get-stats" can be performed on
44 * the descriptor; it is therefore not safe to close the file descriptor in
45 * the #GstMultiFdSink::client-removed signal handler, and you should use the
46 * #GstMultiFdSink::client-fd-removed signal to safely close the fd.
48 * Multifdsink internally keeps a queue of the incoming buffers and uses a
49 * separate thread to send the buffers to the clients. This ensures that no
50 * client write can block the pipeline and that clients can read with different
53 * When adding a client to multifdsink, the #GstMultiFdSink:sync-method property will define
54 * which buffer in the queued buffers will be sent first to the client. Clients
55 * can be sent the most recent buffer (which might not be decodable by the
56 * client if it is not a keyframe), the next keyframe received in
57 * multifdsink (which can take some time depending on the keyframe rate), or the
58 * last received keyframe (which will cause a simple burst-on-connect).
59 * Multifdsink will always keep at least one keyframe in its internal buffers
60 * when the sync-mode is set to latest-keyframe.
62 * As of version 0.10.8, there are additional values for the #GstMultiFdSink:sync-method
63 * property to allow finer control over burst-on-connect behaviour. By selecting
64 * the 'burst' method a minimum burst size can be chosen, 'burst-keyframe'
65 * additionally requires that the burst begin with a keyframe, and
66 * 'burst-with-keyframe' attempts to burst beginning with a keyframe, but will
67 * prefer a minimum burst size even if it requires not starting with a keyframe.
69 * Multifdsink can be instructed to keep at least a minimum amount of data
70 * expressed in time or byte units in its internal queues with the
71 * #GstMultiFdSink:time-min and #GstMultiFdSink:bytes-min properties respectively.
72 * These properties are useful if the application adds clients with the
73 * #GstMultiFdSink::add-full signal to make sure that a burst connect can
74 * actually be honored.
76 * When streaming data, clients are allowed to read at a different rate than
77 * the rate at which multifdsink receives data. If the client is reading too
78 * fast, no data will be send to the client until multifdsink receives more
79 * data. If the client, however, reads too slowly, data for that client will be
80 * queued up in multifdsink. Two properties control the amount of data
81 * (buffers) that is queued in multifdsink: #GstMultiFdSink:buffers-max and
82 * #GstMultiFdSink:buffers-soft-max. A client that falls behind by
83 * #GstMultiFdSink:buffers-max is removed from multifdsink forcibly.
85 * A client with a lag of at least #GstMultiFdSink:buffers-soft-max enters the recovery
86 * procedure which is controlled with the #GstMultiFdSink:recover-policy property.
87 * A recover policy of NONE will do nothing, RESYNC_LATEST will send the most recently
88 * received buffer as the next buffer for the client, RESYNC_SOFT_LIMIT
89 * positions the client to the soft limit in the buffer queue and
90 * RESYNC_KEYFRAME positions the client at the most recent keyframe in the
93 * multifdsink will by default synchronize on the clock before serving the
94 * buffers to the clients. This behaviour can be disabled by setting the sync
95 * property to FALSE. Multifdsink will by default not do QoS and will never
98 * Last reviewed on 2006-09-12 (0.10.10)
105 #include <gst/gst-i18n-plugin.h>
107 #include <sys/ioctl.h>
114 #include <sys/types.h>
115 #include <sys/socket.h>
116 #include <sys/stat.h>
117 #include <netinet/in.h>
119 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
120 #include <sys/filio.h>
123 #include "gstmultifdsink.h"
124 #include "gsttcp-marshal.h"
126 #define NOT_IMPLEMENTED 0
128 GST_DEBUG_CATEGORY_STATIC (multifdsink_debug);
129 #define GST_CAT_DEFAULT (multifdsink_debug)
131 /* MultiFdSink signals and args */
143 SIGNAL_CLIENT_REMOVED,
144 SIGNAL_CLIENT_FD_REMOVED,
149 /* this is really arbitrarily chosen */
150 #define DEFAULT_MODE 1
151 #define DEFAULT_UNIT_FORMAT GST_FORMAT_BUFFERS
153 #define DEFAULT_BURST_FORMAT GST_FORMAT_UNDEFINED
154 #define DEFAULT_BURST_VALUE 0
156 #define DEFAULT_HANDLE_READ TRUE
175 /* For backward compat, we can't really select the poll mode anymore with
177 #define GST_TYPE_FDSET_MODE (gst_fdset_mode_get_type())
179 gst_fdset_mode_get_type (void)
181 static GType fdset_mode_type = 0;
182 static const GEnumValue fdset_mode[] = {
183 {0, "Select", "select"},
185 {2, "EPoll", "epoll"},
189 if (!fdset_mode_type) {
190 fdset_mode_type = g_enum_register_static ("GstFDSetMode", fdset_mode);
192 return fdset_mode_type;
195 #define GST_TYPE_UNIT_FORMAT (gst_unit_format_get_type())
197 gst_unit_format_get_type (void)
199 static GType unit_format_type = 0;
200 static const GEnumValue unit_format[] = {
201 {GST_TCP_UNIT_FORMAT_UNDEFINED, "Undefined", "undefined"},
202 {GST_TCP_UNIT_FORMAT_BYTES, "Bytes", "bytes"},
203 {GST_TCP_UNIT_FORMAT_TIME, "Time", "time"},
204 {GST_TCP_UNIT_FORMAT_BUFFERS, "Buffers", "buffers"},
208 if (!unit_format_type) {
209 unit_format_type = g_enum_register_static ("GstTCPUnitType", unit_format);
211 return unit_format_type;
214 static void gst_multi_fd_sink_finalize (GObject * object);
216 static void gst_multi_fd_sink_clear_post (GstMultiHandleSink * mhsink);
217 static void gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink);
218 static void gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink);
219 static gboolean gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink);
220 static gpointer gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink);
221 static void gst_multi_fd_sink_queue_buffer (GstMultiHandleSink * mhsink,
223 static gboolean gst_multi_fd_sink_client_queue_buffer (GstMultiHandleSink *
224 mhsink, GstMultiHandleClient * mhclient, GstBuffer * buffer);
225 static int gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client);
227 static void gst_multi_fd_sink_remove_client_link (GstMultiHandleSink * sink,
230 static void gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
231 const GValue * value, GParamSpec * pspec);
232 static void gst_multi_fd_sink_get_property (GObject * object, guint prop_id,
233 GValue * value, GParamSpec * pspec);
235 #define gst_multi_fd_sink_parent_class parent_class
236 G_DEFINE_TYPE (GstMultiFdSink, gst_multi_fd_sink, GST_TYPE_MULTI_HANDLE_SINK);
238 static guint gst_multi_fd_sink_signals[LAST_SIGNAL] = { 0 };
241 gst_multi_fd_sink_class_init (GstMultiFdSinkClass * klass)
243 GObjectClass *gobject_class;
244 GstElementClass *gstelement_class;
245 GstMultiHandleSinkClass *gstmultihandlesink_class;
247 gobject_class = (GObjectClass *) klass;
248 gstelement_class = (GstElementClass *) klass;
249 gstmultihandlesink_class = (GstMultiHandleSinkClass *) klass;
251 gobject_class->set_property = gst_multi_fd_sink_set_property;
252 gobject_class->get_property = gst_multi_fd_sink_get_property;
253 gobject_class->finalize = gst_multi_fd_sink_finalize;
256 * GstMultiFdSink::mode
258 * The mode for selecting activity on the fds.
260 * This property is deprecated since 0.10.18, if will now automatically
261 * select and use the most optimal method.
263 g_object_class_install_property (gobject_class, PROP_MODE,
264 g_param_spec_enum ("mode", "Mode",
265 "The mode for selecting activity on the fds (deprecated)",
266 GST_TYPE_FDSET_MODE, DEFAULT_MODE,
267 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
269 g_object_class_install_property (gobject_class, PROP_UNIT_FORMAT,
270 g_param_spec_enum ("unit-format", "Units format",
271 "The unit to measure the max/soft-max/queued properties",
272 GST_TYPE_FORMAT, DEFAULT_UNIT_FORMAT,
273 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275 g_object_class_install_property (gobject_class, PROP_BURST_FORMAT,
276 g_param_spec_enum ("burst-format", "Burst format",
277 "The format of the burst units (when sync-method is burst[[-with]-keyframe])",
278 GST_TYPE_FORMAT, DEFAULT_BURST_FORMAT,
279 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
280 g_object_class_install_property (gobject_class, PROP_BURST_VALUE,
281 g_param_spec_uint64 ("burst-value", "Burst value",
282 "The amount of burst expressed in burst-unit", 0, G_MAXUINT64,
283 DEFAULT_BURST_VALUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 * GstMultiFdSink::handle-read
288 * Handle read requests from clients and discard the data.
292 g_object_class_install_property (gobject_class, PROP_HANDLE_READ,
293 g_param_spec_boolean ("handle-read", "Handle Read",
294 "Handle client reads and discard the data",
295 DEFAULT_HANDLE_READ, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_object_class_install_property (gobject_class, PROP_NUM_FDS,
298 g_param_spec_uint ("num-fds", "Number of fds",
299 "The current number of client file descriptors.",
300 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
303 * GstMultiFdSink::add:
304 * @gstmultifdsink: the multifdsink element to emit this signal on
305 * @fd: the file descriptor to add to multifdsink
307 * Hand the given open file descriptor to multifdsink to write to.
309 gst_multi_fd_sink_signals[SIGNAL_ADD] =
310 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
311 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
312 add), NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1,
315 * GstMultiFdSink::add-full:
316 * @gstmultifdsink: the multifdsink element to emit this signal on
317 * @fd: the file descriptor to add to multifdsink
318 * @sync: the sync method to use
319 * @unit_format_min: the unit-format of @value_min
320 * @value_min: the minimum amount of data to burst expressed in
321 * @unit_format_min units.
322 * @unit_format_max: the unit-format of @value_max
323 * @value_max: the maximum amount of data to burst expressed in
324 * @unit_format_max units.
326 * Hand the given open file descriptor to multifdsink to write to and
327 * specify the burst parameters for the new connection.
329 gst_multi_fd_sink_signals[SIGNAL_ADD_BURST] =
330 g_signal_new ("add-full", G_TYPE_FROM_CLASS (klass),
331 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
332 add_full), NULL, NULL,
333 gst_tcp_marshal_VOID__INT_ENUM_INT_UINT64_INT_UINT64,
334 G_TYPE_NONE, 6, G_TYPE_INT, GST_TYPE_SYNC_METHOD, GST_TYPE_UNIT_FORMAT,
335 G_TYPE_UINT64, GST_TYPE_UNIT_FORMAT, G_TYPE_UINT64);
337 * GstMultiFdSink::remove:
338 * @gstmultifdsink: the multifdsink element to emit this signal on
339 * @fd: the file descriptor to remove from multifdsink
341 * Remove the given open file descriptor from multifdsink.
343 gst_multi_fd_sink_signals[SIGNAL_REMOVE] =
344 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
345 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
346 remove), NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE,
349 * GstMultiFdSink::remove-flush:
350 * @gstmultifdsink: the multifdsink element to emit this signal on
351 * @fd: the file descriptor to remove from multifdsink
353 * Remove the given open file descriptor from multifdsink after flushing all
354 * the pending data to the fd.
356 gst_multi_fd_sink_signals[SIGNAL_REMOVE_FLUSH] =
357 g_signal_new ("remove-flush", G_TYPE_FROM_CLASS (klass),
358 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
359 remove_flush), NULL, NULL, gst_tcp_marshal_VOID__INT,
360 G_TYPE_NONE, 1, G_TYPE_INT);
363 * GstMultiFdSink::get-stats:
364 * @gstmultifdsink: the multifdsink element to emit this signal on
365 * @fd: the file descriptor to get stats of from multifdsink
367 * Get statistics about @fd. This function returns a GValueArray to ease
368 * automatic wrapping for bindings.
370 * Returns: a GValueArray with the statistics. The array contains guint64
371 * values that represent respectively: total number of bytes sent, time
372 * when the client was added, time when the client was
373 * disconnected/removed, time the client is/was active, last activity
374 * time (in epoch seconds), number of buffers dropped.
375 * All times are expressed in nanoseconds (GstClockTime).
376 * The array can be 0-length if the client was not found.
378 gst_multi_fd_sink_signals[SIGNAL_GET_STATS] =
379 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
380 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
381 get_stats), NULL, NULL, gst_tcp_marshal_BOXED__INT,
382 G_TYPE_VALUE_ARRAY, 1, G_TYPE_INT);
385 * GstMultiFdSink::client-added:
386 * @gstmultifdsink: the multifdsink element that emitted this signal
387 * @fd: the file descriptor that was added to multifdsink
389 * The given file descriptor was added to multifdsink. This signal will
390 * be emitted from the streaming thread so application should be prepared
393 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED] =
394 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
395 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass, client_added),
396 NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
398 * GstMultiFdSink::client-removed:
399 * @gstmultifdsink: the multifdsink element that emitted this signal
400 * @fd: the file descriptor that is to be removed from multifdsink
401 * @status: the reason why the client was removed
403 * The given file descriptor is about to be removed from multifdsink. This
404 * signal will be emitted from the streaming thread so applications should
405 * be prepared for that.
407 * @gstmultifdsink still holds a handle to @fd so it is possible to call
408 * the get-stats signal from this callback. For the same reason it is
409 * not safe to close() and reuse @fd in this callback.
411 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED] =
412 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
413 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass,
414 client_removed), NULL, NULL, gst_tcp_marshal_VOID__INT_ENUM,
415 G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CLIENT_STATUS);
417 * GstMultiFdSink::client-fd-removed:
418 * @gstmultifdsink: the multifdsink element that emitted this signal
419 * @fd: the file descriptor that was removed from multifdsink
421 * The given file descriptor was removed from multifdsink. This signal will
422 * be emitted from the streaming thread so applications should be prepared
425 * In this callback, @gstmultifdsink has removed all the information
426 * associated with @fd and it is therefore not possible to call get-stats
427 * with @fd. It is however safe to close() and reuse @fd in the callback.
431 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED] =
432 g_signal_new ("client-fd-removed", G_TYPE_FROM_CLASS (klass),
433 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass,
434 client_fd_removed), NULL, NULL, gst_tcp_marshal_VOID__INT,
435 G_TYPE_NONE, 1, G_TYPE_INT);
437 gst_element_class_set_details_simple (gstelement_class,
438 "Multi filedescriptor sink", "Sink/Network",
439 "Send data to multiple filedescriptors",
440 "Thomas Vander Stichele <thomas at apestaart dot org>, "
441 "Wim Taymans <wim@fluendo.com>");
443 gstmultihandlesink_class->clear_post =
444 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_clear_post);
446 gstmultihandlesink_class->stop_pre =
447 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_pre);
448 gstmultihandlesink_class->stop_post =
449 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_post);
450 gstmultihandlesink_class->start_pre =
451 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_start_pre);
452 gstmultihandlesink_class->thread =
453 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_thread);
454 gstmultihandlesink_class->queue_buffer =
455 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_queue_buffer);
456 gstmultihandlesink_class->client_queue_buffer =
457 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_client_queue_buffer);
458 gstmultihandlesink_class->client_get_fd =
459 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_client_get_fd);
461 gstmultihandlesink_class->remove_client_link =
462 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove_client_link);
465 klass->add = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add);
466 klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add_full);
467 klass->remove = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove);
468 klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove_flush);
469 klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_get_stats);
471 GST_DEBUG_CATEGORY_INIT (multifdsink_debug, "multifdsink", 0, "FD sink");
475 gst_multi_fd_sink_init (GstMultiFdSink * this)
477 this->mode = DEFAULT_MODE;
479 this->fd_hash = g_hash_table_new (g_int_hash, g_int_equal);
481 this->unit_format = DEFAULT_UNIT_FORMAT;
483 this->def_burst_format = DEFAULT_BURST_FORMAT;
484 this->def_burst_value = DEFAULT_BURST_VALUE;
486 this->handle_read = DEFAULT_HANDLE_READ;
490 gst_multi_fd_sink_finalize (GObject * object)
492 GstMultiFdSink *this = GST_MULTI_FD_SINK (object);
494 g_hash_table_destroy (this->fd_hash);
496 G_OBJECT_CLASS (parent_class)->finalize (object);
500 gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client)
502 GstTCPClient *tclient = (GstTCPClient *) client;
504 return tclient->fd.fd;
507 /* "add-full" signal implementation */
509 gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
510 GstSyncMethod sync_method, GstFormat min_format, guint64 min_value,
511 GstFormat max_format, guint64 max_value)
513 GstTCPClient *client;
514 GstMultiHandleClient *mhclient;
518 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
520 GST_DEBUG_OBJECT (sink, "[fd %5d] adding client, sync_method %d, "
521 "min_format %d, min_value %" G_GUINT64_FORMAT
522 ", max_format %d, max_value %" G_GUINT64_FORMAT, fd, sync_method,
523 min_format, min_value, max_format, max_value);
525 /* do limits check if we can */
526 if (min_format == max_format) {
527 if (max_value != -1 && min_value != -1 && max_value < min_value)
531 /* create client datastructure */
532 client = g_new0 (GstTCPClient, 1);
533 mhclient = (GstMultiHandleClient *) client;
534 gst_multi_handle_sink_client_init (mhclient, sync_method);
535 g_snprintf (mhclient->debug, 30, "[fd %5d]", fd);
538 client->burst_min_format = min_format;
539 client->burst_min_value = min_value;
540 client->burst_max_format = max_format;
541 client->burst_max_value = max_value;
545 /* check the hash to find a duplicate fd */
546 clink = g_hash_table_lookup (sink->fd_hash, &client->fd.fd);
550 /* we can add the fd now */
551 clink = mhsink->clients = g_list_prepend (mhsink->clients, client);
552 g_hash_table_insert (sink->fd_hash, &client->fd.fd, clink);
553 mhsink->clients_cookie++;
555 /* set the socket to non blocking */
556 if (fcntl (fd, F_SETFL, O_NONBLOCK) < 0) {
557 GST_ERROR_OBJECT (sink, "failed to make socket %d non-blocking: %s", fd,
561 /* we always read from a client */
562 gst_poll_add_fd (sink->fdset, &client->fd);
564 /* we don't try to read from write only fds */
565 if (sink->handle_read) {
566 flags = fcntl (fd, F_GETFL, 0);
567 if ((flags & O_ACCMODE) != O_WRONLY) {
568 gst_poll_fd_ctl_read (sink->fdset, &client->fd, TRUE);
571 /* figure out the mode, can't use send() for non sockets */
572 if (fstat (fd, &statbuf) == 0 && S_ISSOCK (statbuf.st_mode)) {
573 client->is_socket = TRUE;
574 gst_multi_handle_sink_setup_dscp_client (mhsink, mhclient);
577 gst_poll_restart (sink->fdset);
579 CLIENTS_UNLOCK (sink);
581 g_signal_emit (G_OBJECT (sink),
582 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED], 0, fd);
589 GST_WARNING_OBJECT (sink,
590 "[fd %5d] wrong values min =%" G_GUINT64_FORMAT ", max=%"
591 G_GUINT64_FORMAT ", unit %d specified when adding client", fd,
592 min_value, max_value, min_format);
597 mhclient->status = GST_CLIENT_STATUS_DUPLICATE;
598 CLIENTS_UNLOCK (sink);
599 GST_WARNING_OBJECT (sink, "[fd %5d] duplicate client found, refusing", fd);
600 g_signal_emit (G_OBJECT (sink),
601 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0, fd,
608 /* "add" signal implementation */
610 gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd)
612 GstMultiHandleSink *mhsink;
614 mhsink = GST_MULTI_HANDLE_SINK (sink);
615 gst_multi_fd_sink_add_full (sink, fd, mhsink->def_sync_method,
616 sink->def_burst_format, sink->def_burst_value, sink->def_burst_format,
620 /* "remove" signal implementation */
622 gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd)
625 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
626 GstMultiHandleSinkClass *mhsinkclass =
627 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
629 GST_DEBUG_OBJECT (sink, "[fd %5d] removing client", fd);
632 clink = g_hash_table_lookup (sink->fd_hash, &fd);
634 GstTCPClient *client = (GstTCPClient *) clink->data;
635 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
637 if (mhclient->status != GST_CLIENT_STATUS_OK) {
638 GST_INFO_OBJECT (sink,
639 "[fd %5d] Client already disconnecting with status %d",
640 fd, mhclient->status);
644 mhclient->status = GST_CLIENT_STATUS_REMOVED;
645 mhsinkclass->remove_client_link (GST_MULTI_HANDLE_SINK (sink), clink);
646 // FIXME: specific poll
647 gst_poll_restart (sink->fdset);
649 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this fd found!", fd);
653 CLIENTS_UNLOCK (sink);
656 /* "remove-flush" signal implementation */
658 gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd)
662 GST_DEBUG_OBJECT (sink, "[fd %5d] flushing client", fd);
665 clink = g_hash_table_lookup (sink->fd_hash, &fd);
667 GstTCPClient *client = (GstTCPClient *) clink->data;
668 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
670 if (mhclient->status != GST_CLIENT_STATUS_OK) {
671 GST_INFO_OBJECT (sink,
672 "[fd %5d] Client already disconnecting with status %d",
673 fd, mhclient->status);
677 /* take the position of the client as the number of buffers left to flush.
678 * If the client was at position -1, we flush 0 buffers, 0 == flush 1
680 mhclient->flushcount = mhclient->bufpos + 1;
681 /* mark client as flushing. We can not remove the client right away because
682 * it might have some buffers to flush in the ->sending queue. */
683 mhclient->status = GST_CLIENT_STATUS_FLUSHING;
685 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this fd found!", fd);
688 CLIENTS_UNLOCK (sink);
691 /* called with the CLIENTS_LOCK held */
693 gst_multi_fd_sink_clear_post (GstMultiHandleSink * mhsink)
695 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
697 gst_poll_restart (sink->fdset);
700 /* "get-stats" signal implementation
701 * the array returned contains:
703 * guint64 : bytes_sent
704 * guint64 : connect time (in nanoseconds, since Epoch)
705 * guint64 : disconnect time (in nanoseconds, since Epoch)
706 * guint64 : time the client is/was connected (in nanoseconds)
707 * guint64 : last activity time (in nanoseconds, since Epoch)
708 * guint64 : buffers dropped due to recovery
709 * guint64 : timestamp of the first buffer sent (in nanoseconds)
710 * guint64 : timestamp of the last buffer sent (in nanoseconds)
713 gst_multi_fd_sink_get_stats (GstMultiFdSink * sink, int fd)
715 GstTCPClient *client;
716 GValueArray *result = NULL;
720 clink = g_hash_table_lookup (sink->fd_hash, &fd);
724 client = (GstTCPClient *) clink->data;
725 if (client != NULL) {
726 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
727 GValue value = { 0 };
730 result = g_value_array_new (7);
732 g_value_init (&value, G_TYPE_UINT64);
733 g_value_set_uint64 (&value, mhclient->bytes_sent);
734 result = g_value_array_append (result, &value);
735 g_value_unset (&value);
736 g_value_init (&value, G_TYPE_UINT64);
737 g_value_set_uint64 (&value, mhclient->connect_time);
738 result = g_value_array_append (result, &value);
739 g_value_unset (&value);
740 if (mhclient->disconnect_time == 0) {
743 g_get_current_time (&nowtv);
745 interval = GST_TIMEVAL_TO_TIME (nowtv) - mhclient->connect_time;
747 interval = mhclient->disconnect_time - mhclient->connect_time;
749 g_value_init (&value, G_TYPE_UINT64);
750 g_value_set_uint64 (&value, mhclient->disconnect_time);
751 result = g_value_array_append (result, &value);
752 g_value_unset (&value);
753 g_value_init (&value, G_TYPE_UINT64);
754 g_value_set_uint64 (&value, interval);
755 result = g_value_array_append (result, &value);
756 g_value_unset (&value);
757 g_value_init (&value, G_TYPE_UINT64);
758 g_value_set_uint64 (&value, mhclient->last_activity_time);
759 result = g_value_array_append (result, &value);
760 g_value_unset (&value);
761 g_value_init (&value, G_TYPE_UINT64);
762 g_value_set_uint64 (&value, mhclient->dropped_buffers);
763 result = g_value_array_append (result, &value);
764 g_value_unset (&value);
765 g_value_init (&value, G_TYPE_UINT64);
766 g_value_set_uint64 (&value, mhclient->first_buffer_ts);
767 result = g_value_array_append (result, &value);
768 g_value_unset (&value);
769 g_value_init (&value, G_TYPE_UINT64);
770 g_value_set_uint64 (&value, mhclient->last_buffer_ts);
771 result = g_value_array_append (result, &value);
775 CLIENTS_UNLOCK (sink);
777 /* python doesn't like a NULL pointer yet */
778 if (result == NULL) {
779 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this found!", fd);
780 result = g_value_array_new (0);
786 /* should be called with the clientslock helt.
787 * Note that we don't close the fd as we didn't open it in the first
788 * place. An application should connect to the client-fd-removed signal and
789 * close the fd itself.
792 gst_multi_fd_sink_remove_client_link (GstMultiHandleSink * sink, GList * link)
796 GstTCPClient *client = (GstTCPClient *) link->data;
797 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
798 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (sink);
799 GstMultiFdSinkClass *fclass;
801 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
805 if (mhclient->currently_removing) {
806 GST_WARNING_OBJECT (sink, "%s client is already being removed",
810 mhclient->currently_removing = TRUE;
813 /* FIXME: if we keep track of ip we can log it here and signal */
814 switch (mhclient->status) {
815 case GST_CLIENT_STATUS_OK:
816 GST_WARNING_OBJECT (sink, "%s removing client %p for no reason",
817 mhclient->debug, client);
819 case GST_CLIENT_STATUS_CLOSED:
820 GST_DEBUG_OBJECT (sink, "%s removing client %p because of close",
821 mhclient->debug, client);
823 case GST_CLIENT_STATUS_REMOVED:
824 GST_DEBUG_OBJECT (sink,
825 "%s removing client %p because the app removed it", mhclient->debug,
828 case GST_CLIENT_STATUS_SLOW:
829 GST_INFO_OBJECT (sink,
830 "%s removing client %p because it was too slow", mhclient->debug,
833 case GST_CLIENT_STATUS_ERROR:
834 GST_WARNING_OBJECT (sink,
835 "%s removing client %p because of error", mhclient->debug, client);
837 case GST_CLIENT_STATUS_FLUSHING:
839 GST_WARNING_OBJECT (sink,
840 "%s removing client %p with invalid reason %d", mhclient->debug,
841 client, mhclient->status);
845 gst_poll_remove_fd (mfsink->fdset, &client->fd);
847 g_get_current_time (&now);
848 mhclient->disconnect_time = GST_TIMEVAL_TO_TIME (now);
850 /* free client buffers */
851 g_slist_foreach (mhclient->sending, (GFunc) gst_mini_object_unref, NULL);
852 g_slist_free (mhclient->sending);
853 mhclient->sending = NULL;
856 gst_caps_unref (mhclient->caps);
857 mhclient->caps = NULL;
859 /* unlock the mutex before signaling because the signal handler
860 * might query some properties */
861 CLIENTS_UNLOCK (sink);
863 g_signal_emit (G_OBJECT (sink),
864 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0, fd,
867 /* lock again before we remove the client completely */
870 /* fd cannot be reused in the above signal callback so we can safely
871 * remove it from the hashtable here */
872 if (!g_hash_table_remove (mfsink->fd_hash, &client->fd.fd)) {
873 GST_WARNING_OBJECT (sink,
874 "[fd %5d] error removing client %p from hash", client->fd.fd, client);
876 /* after releasing the lock above, the link could be invalid, more
877 * precisely, the next and prev pointers could point to invalid list
878 * links. One optimisation could be to add a cookie to the linked list
879 * and take a shortcut when it did not change between unlocking and locking
880 * our mutex. For now we just walk the list again. */
881 sink->clients = g_list_remove (sink->clients, client);
882 sink->clients_cookie++;
885 fclass->removed (mfsink, client->fd.fd);
888 CLIENTS_UNLOCK (sink);
890 /* and the fd is really gone now */
891 g_signal_emit (G_OBJECT (sink),
892 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED], 0, fd);
897 /* handle a read on a client fd,
898 * which either indicates a close or should be ignored
899 * returns FALSE if some error occured or the client closed. */
901 gst_multi_fd_sink_handle_client_read (GstMultiFdSink * sink,
902 GstTCPClient * client)
906 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
910 if (ioctl (fd, FIONREAD, &avail) < 0)
913 GST_DEBUG_OBJECT (sink, "[fd %5d] select reports client read of %d bytes",
919 /* client sent close, so remove it */
920 GST_DEBUG_OBJECT (sink, "[fd %5d] client asked for close, removing", fd);
921 mhclient->status = GST_CLIENT_STATUS_CLOSED;
923 } else if (avail < 0) {
924 GST_WARNING_OBJECT (sink, "[fd %5d] avail < 0, removing", fd);
925 mhclient->status = GST_CLIENT_STATUS_ERROR;
931 /* just Read 'n' Drop, could also just drop the client as it's not supposed
932 * to write to us except for closing the socket, I guess it's because we
933 * like to listen to our customers. */
935 /* this is the maximum we can read */
936 gint to_read = MIN (avail, 512);
938 GST_DEBUG_OBJECT (sink, "[fd %5d] client wants us to read %d bytes",
941 nread = read (fd, dummy, to_read);
943 GST_WARNING_OBJECT (sink, "[fd %5d] could not read %d bytes: %s (%d)",
944 fd, to_read, g_strerror (errno), errno);
945 mhclient->status = GST_CLIENT_STATUS_ERROR;
948 } else if (nread == 0) {
949 GST_WARNING_OBJECT (sink, "[fd %5d] 0 bytes in read, removing", fd);
950 mhclient->status = GST_CLIENT_STATUS_ERROR;
963 GST_WARNING_OBJECT (sink, "[fd %5d] ioctl failed: %s (%d)",
964 fd, g_strerror (errno), errno);
965 mhclient->status = GST_CLIENT_STATUS_ERROR;
970 /* queue the given buffer for the given client */
972 gst_multi_fd_sink_client_queue_buffer (GstMultiHandleSink * mhsink,
973 GstMultiHandleClient * mhclient, GstBuffer * buffer)
977 /* TRUE: send them if the new caps have them */
978 gboolean send_streamheader = FALSE;
980 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
981 GstTCPClient *client = (GstTCPClient *) mhclient;
983 /* before we queue the buffer, we check if we need to queue streamheader
984 * buffers (because it's a new client, or because they changed) */
985 caps = gst_pad_get_current_caps (GST_BASE_SINK_PAD (sink));
987 if (!mhclient->caps) {
988 GST_DEBUG_OBJECT (sink,
989 "[fd %5d] no previous caps for this client, send streamheader",
991 send_streamheader = TRUE;
992 mhclient->caps = gst_caps_ref (caps);
994 /* there were previous caps recorded, so compare */
995 if (!gst_caps_is_equal (caps, mhclient->caps)) {
996 const GValue *sh1, *sh2;
998 /* caps are not equal, but could still have the same streamheader */
999 s = gst_caps_get_structure (caps, 0);
1000 if (!gst_structure_has_field (s, "streamheader")) {
1001 /* no new streamheader, so nothing new to send */
1002 GST_DEBUG_OBJECT (sink,
1003 "[fd %5d] new caps do not have streamheader, not sending",
1006 /* there is a new streamheader */
1007 s = gst_caps_get_structure (mhclient->caps, 0);
1008 if (!gst_structure_has_field (s, "streamheader")) {
1009 /* no previous streamheader, so send the new one */
1010 GST_DEBUG_OBJECT (sink,
1011 "[fd %5d] previous caps did not have streamheader, sending",
1013 send_streamheader = TRUE;
1015 /* both old and new caps have streamheader set */
1016 if (!mhsink->resend_streamheader) {
1017 GST_DEBUG_OBJECT (sink,
1018 "[fd %5d] asked to not resend the streamheader, not sending",
1020 send_streamheader = FALSE;
1022 sh1 = gst_structure_get_value (s, "streamheader");
1023 s = gst_caps_get_structure (caps, 0);
1024 sh2 = gst_structure_get_value (s, "streamheader");
1025 if (gst_value_compare (sh1, sh2) != GST_VALUE_EQUAL) {
1026 GST_DEBUG_OBJECT (sink,
1027 "[fd %5d] new streamheader different from old, sending",
1029 send_streamheader = TRUE;
1035 /* Replace the old caps */
1036 gst_caps_unref (mhclient->caps);
1037 mhclient->caps = gst_caps_ref (caps);
1040 if (G_UNLIKELY (send_streamheader)) {
1045 GST_LOG_OBJECT (sink,
1046 "[fd %5d] sending streamheader from caps %" GST_PTR_FORMAT,
1047 client->fd.fd, caps);
1048 s = gst_caps_get_structure (caps, 0);
1049 if (!gst_structure_has_field (s, "streamheader")) {
1050 GST_DEBUG_OBJECT (sink,
1051 "[fd %5d] no new streamheader, so nothing to send", client->fd.fd);
1053 GST_LOG_OBJECT (sink,
1054 "[fd %5d] sending streamheader from caps %" GST_PTR_FORMAT,
1055 client->fd.fd, caps);
1056 sh = gst_structure_get_value (s, "streamheader");
1057 g_assert (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
1058 buffers = g_value_peek_pointer (sh);
1059 GST_DEBUG_OBJECT (sink, "%d streamheader buffers", buffers->len);
1060 for (i = 0; i < buffers->len; ++i) {
1064 bufval = &g_array_index (buffers, GValue, i);
1065 g_assert (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER);
1066 buffer = g_value_peek_pointer (bufval);
1067 GST_DEBUG_OBJECT (sink,
1068 "[fd %5d] queueing streamheader buffer of length %" G_GSIZE_FORMAT,
1069 client->fd.fd, gst_buffer_get_size (buffer));
1070 gst_buffer_ref (buffer);
1072 mhclient->sending = g_slist_append (mhclient->sending, buffer);
1077 gst_caps_unref (caps);
1080 GST_LOG_OBJECT (sink, "[fd %5d] queueing buffer of length %" G_GSIZE_FORMAT,
1081 client->fd.fd, gst_buffer_get_size (buffer));
1083 gst_buffer_ref (buffer);
1084 mhclient->sending = g_slist_append (mhclient->sending, buffer);
1089 /* Get the number of buffers from the buffer queue needed to satisfy
1090 * the maximum max in the configured units.
1091 * If units are not BUFFERS, and there are insufficient buffers in the
1092 * queue to satify the limit, return len(queue) + 1 */
1094 get_buffers_max (GstMultiFdSink * sink, gint64 max)
1096 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1098 switch (sink->unit_format) {
1099 case GST_TCP_UNIT_FORMAT_BUFFERS:
1101 case GST_TCP_UNIT_FORMAT_TIME:
1107 GstClockTime first = GST_CLOCK_TIME_NONE;
1109 len = mhsink->bufqueue->len;
1111 for (i = 0; i < len; i++) {
1112 buf = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1113 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1115 first = GST_BUFFER_TIMESTAMP (buf);
1117 diff = first - GST_BUFFER_TIMESTAMP (buf);
1125 case GST_TCP_UNIT_FORMAT_BYTES:
1132 len = mhsink->bufqueue->len;
1134 for (i = 0; i < len; i++) {
1135 buf = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1136 acc += gst_buffer_get_size (buf);
1148 /* find the positions in the buffer queue where *_min and *_max
1151 /* count the amount of data in the buffers and return the index
1152 * that satifies the given limits.
1154 * Returns: index @idx in the buffer queue so that the given limits are
1155 * satisfied. TRUE if all the limits could be satisfied, FALSE if not
1156 * enough data was in the queue.
1158 * FIXME, this code might now work if any of the units is in buffers...
1161 find_limits (GstMultiFdSink * sink,
1162 gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
1163 gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max)
1165 GstClockTime first, time;
1167 gboolean result, max_hit;
1168 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1170 /* take length of queue */
1171 len = mhsink->bufqueue->len;
1173 /* this must hold */
1176 GST_LOG_OBJECT (sink,
1177 "bytes_min %d, buffers_min %d, time_min %" GST_TIME_FORMAT
1178 ", bytes_max %d, buffers_max %d, time_max %" GST_TIME_FORMAT, bytes_min,
1179 buffers_min, GST_TIME_ARGS (time_min), bytes_max, buffers_max,
1180 GST_TIME_ARGS (time_max));
1182 /* do the trivial buffer limit test */
1183 if (buffers_min != -1 && len < buffers_min) {
1190 /* else count bytes and time */
1199 /* loop through the buffers, when a limit is ok, mark it
1200 * as -1, we have at least one buffer in the queue. */
1204 /* if we checked all min limits, update result */
1205 if (bytes_min == -1 && time_min == -1 && *min_idx == -1) {
1206 /* don't go below 0 */
1207 *min_idx = MAX (i - 1, 0);
1209 /* if we reached one max limit break out */
1211 /* i > 0 when we get here, we subtract one to get the position
1212 * of the previous buffer. */
1214 /* we have valid complete result if we found a min_idx too */
1215 result = *min_idx != -1;
1218 buf = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1220 bytes += gst_buffer_get_size (buf);
1222 /* take timestamp and save for the base first timestamp */
1223 if ((time = GST_BUFFER_TIMESTAMP (buf)) != -1) {
1224 GST_LOG_OBJECT (sink, "Ts %" GST_TIME_FORMAT " on buffer",
1225 GST_TIME_ARGS (time));
1229 /* increase max usage if we did not fill enough. Note that
1230 * buffers are sorted from new to old, so the first timestamp is
1231 * bigger than the next one. */
1232 if (time_min != -1 && first - time >= time_min)
1234 if (time_max != -1 && first - time >= time_max)
1237 GST_LOG_OBJECT (sink, "No timestamp on buffer");
1239 /* time is OK or unknown, check and increase if not enough bytes */
1240 if (bytes_min != -1) {
1241 if (bytes >= bytes_min)
1244 if (bytes_max != -1) {
1245 if (bytes >= bytes_max) {
1253 /* if we did not hit the max or min limit, set to buffer size */
1256 /* make sure min does not exceed max */
1258 *min_idx = *max_idx;
1263 /* parse the unit/value pair and assign it to the result value of the
1264 * right type, leave the other values untouched
1266 * Returns: FALSE if the unit is unknown or undefined. TRUE otherwise.
1269 assign_value (GstFormat format, guint64 value, gint * bytes, gint * buffers,
1270 GstClockTime * time)
1272 gboolean res = TRUE;
1274 /* set only the limit of the given format to the given value */
1276 case GST_TCP_UNIT_FORMAT_BUFFERS:
1277 *buffers = (gint) value;
1279 case GST_TCP_UNIT_FORMAT_TIME:
1282 case GST_TCP_UNIT_FORMAT_BYTES:
1283 *bytes = (gint) value;
1285 case GST_TCP_UNIT_FORMAT_UNDEFINED:
1293 /* count the index in the buffer queue to satisfy the given unit
1294 * and value pair starting from buffer at index 0.
1296 * Returns: TRUE if there was enough data in the queue to satisfy the
1297 * burst values. @idx contains the index in the buffer that contains enough
1298 * data to satisfy the limits or the last buffer in the queue when the
1299 * function returns FALSE.
1302 count_burst_format (GstMultiFdSink * sink, gint * min_idx,
1303 GstFormat min_format, guint64 min_value, gint * max_idx,
1304 GstFormat max_format, guint64 max_value)
1306 gint bytes_min = -1, buffers_min = -1;
1307 gint bytes_max = -1, buffers_max = -1;
1308 GstClockTime time_min = GST_CLOCK_TIME_NONE, time_max = GST_CLOCK_TIME_NONE;
1310 assign_value (min_format, min_value, &bytes_min, &buffers_min, &time_min);
1311 assign_value (max_format, max_value, &bytes_max, &buffers_max, &time_max);
1313 return find_limits (sink, min_idx, bytes_min, buffers_min, time_min,
1314 max_idx, bytes_max, buffers_max, time_max);
1317 /* decide where in the current buffer queue this new client should start
1318 * receiving buffers from.
1319 * This function is called whenever a client is connected and has not yet
1320 * received a buffer.
1321 * If this returns -1, it means that we haven't found a good point to
1322 * start streaming from yet, and this function should be called again later
1323 * when more buffers have arrived.
1326 gst_multi_fd_sink_new_client (GstMultiFdSink * sink, GstTCPClient * client)
1329 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1330 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1332 GST_DEBUG_OBJECT (sink,
1333 "[fd %5d] new client, deciding where to start in queue", client->fd.fd);
1334 GST_DEBUG_OBJECT (sink, "queue is currently %d buffers long",
1335 mhsink->bufqueue->len);
1336 switch (mhclient->sync_method) {
1337 case GST_SYNC_METHOD_LATEST:
1338 /* no syncing, we are happy with whatever the client is going to get */
1339 result = mhclient->bufpos;
1340 GST_DEBUG_OBJECT (sink,
1341 "[fd %5d] SYNC_METHOD_LATEST, position %d", client->fd.fd, result);
1343 case GST_SYNC_METHOD_NEXT_KEYFRAME:
1345 /* if one of the new buffers (between mhclient->bufpos and 0) in the queue
1346 * is a sync point, we can proceed, otherwise we need to keep waiting */
1347 GST_LOG_OBJECT (sink,
1348 "[fd %5d] new client, bufpos %d, waiting for keyframe", client->fd.fd,
1351 result = find_prev_syncframe (mhsink, mhclient->bufpos);
1353 GST_DEBUG_OBJECT (sink,
1354 "[fd %5d] SYNC_METHOD_NEXT_KEYFRAME: result %d",
1355 client->fd.fd, result);
1359 /* client is not on a syncbuffer, need to skip these buffers and
1361 GST_LOG_OBJECT (sink,
1362 "[fd %5d] new client, skipping buffer(s), no syncpoint found",
1364 mhclient->bufpos = -1;
1367 case GST_SYNC_METHOD_LATEST_KEYFRAME:
1369 GST_DEBUG_OBJECT (sink,
1370 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME", client->fd.fd);
1372 /* for new clients we initially scan the complete buffer queue for
1373 * a sync point when a buffer is added. If we don't find a keyframe,
1374 * we need to wait for the next keyframe and so we change the client's
1375 * sync method to GST_SYNC_METHOD_NEXT_KEYFRAME.
1377 result = find_next_syncframe (mhsink, 0);
1379 GST_DEBUG_OBJECT (sink,
1380 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME: result %d", client->fd.fd,
1385 GST_DEBUG_OBJECT (sink,
1386 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME: no keyframe found, "
1387 "switching to SYNC_METHOD_NEXT_KEYFRAME", client->fd.fd);
1388 /* throw client to the waiting state */
1389 mhclient->bufpos = -1;
1390 /* and make client sync to next keyframe */
1391 mhclient->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1394 case GST_SYNC_METHOD_BURST:
1399 /* move to the position where we satisfy the client's burst
1400 * parameters. If we could not satisfy the parameters because there
1401 * is not enough data, we just send what we have (which is in result).
1402 * We use the max value to limit the search
1404 ok = count_burst_format (sink, &result, client->burst_min_format,
1405 client->burst_min_value, &max, client->burst_max_format,
1406 client->burst_max_value);
1407 GST_DEBUG_OBJECT (sink,
1408 "[fd %5d] SYNC_METHOD_BURST: burst_format returned %d, result %d",
1409 client->fd.fd, ok, result);
1411 GST_LOG_OBJECT (sink, "min %d, max %d", result, max);
1413 /* we hit the max and it is below the min, use that then */
1414 if (max != -1 && max <= result) {
1415 result = MAX (max - 1, 0);
1416 GST_DEBUG_OBJECT (sink,
1417 "[fd %5d] SYNC_METHOD_BURST: result above max, taken down to %d",
1418 client->fd.fd, result);
1422 case GST_SYNC_METHOD_BURST_KEYFRAME:
1424 gint min_idx, max_idx;
1425 gint next_syncframe, prev_syncframe;
1429 * _always_ start sending a keyframe to the client. We first search
1430 * a keyframe between min/max limits. If there is none, we send it the
1431 * last keyframe before min. If there is none, the behaviour is like
1434 /* gather burst limits */
1435 count_burst_format (sink, &min_idx, client->burst_min_format,
1436 client->burst_min_value, &max_idx, client->burst_max_format,
1437 client->burst_max_value);
1439 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1441 /* first find a keyframe after min_idx */
1442 next_syncframe = find_next_syncframe (mhsink, min_idx);
1443 if (next_syncframe != -1 && next_syncframe < max_idx) {
1444 /* we have a valid keyframe and it's below the max */
1445 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1446 result = next_syncframe;
1450 /* no valid keyframe, try to find one below min */
1451 prev_syncframe = find_prev_syncframe (mhsink, min_idx);
1452 if (prev_syncframe != -1) {
1453 GST_WARNING_OBJECT (sink,
1454 "using keyframe below min in BURST_KEYFRAME sync mode");
1455 result = prev_syncframe;
1459 /* no prev keyframe or not enough data */
1460 GST_WARNING_OBJECT (sink,
1461 "no prev keyframe found in BURST_KEYFRAME sync mode, waiting for next");
1463 /* throw client to the waiting state */
1464 mhclient->bufpos = -1;
1465 /* and make client sync to next keyframe */
1466 mhclient->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1470 case GST_SYNC_METHOD_BURST_WITH_KEYFRAME:
1472 gint min_idx, max_idx;
1473 gint next_syncframe;
1475 /* BURST_WITH_KEYFRAME:
1477 * try to start sending a keyframe to the client. We first search
1478 * a keyframe between min/max limits. If there is none, we send it the
1479 * amount of data up 'till min.
1481 /* gather enough data to burst */
1482 count_burst_format (sink, &min_idx, client->burst_min_format,
1483 client->burst_min_value, &max_idx, client->burst_max_format,
1484 client->burst_max_value);
1486 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1488 /* first find a keyframe after min_idx */
1489 next_syncframe = find_next_syncframe (mhsink, min_idx);
1490 if (next_syncframe != -1 && next_syncframe < max_idx) {
1491 /* we have a valid keyframe and it's below the max */
1492 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1493 result = next_syncframe;
1497 /* no keyframe, send data from min_idx */
1498 GST_WARNING_OBJECT (sink, "using min in BURST_WITH_KEYFRAME sync mode");
1500 /* make sure we don't go over the max limit */
1501 if (max_idx != -1 && max_idx <= min_idx) {
1502 result = MAX (max_idx - 1, 0);
1510 g_warning ("unknown sync method %d", mhclient->sync_method);
1511 result = mhclient->bufpos;
1517 /* Handle a write on a client,
1518 * which indicates a read request from a client.
1520 * For each client we maintain a queue of GstBuffers that contain the raw bytes
1521 * we need to send to the client.
1523 * We first check to see if we need to send streamheaders. If so, we queue them.
1525 * Then we run into the main loop that tries to send as many buffers as
1526 * possible. It will first exhaust the mhclient->sending queue and if the queue
1527 * is empty, it will pick a buffer from the global queue.
1529 * Sending the buffers from the mhclient->sending queue is basically writing
1530 * the bytes to the socket and maintaining a count of the bytes that were
1531 * sent. When the buffer is completely sent, it is removed from the
1532 * mhclient->sending queue and we try to pick a new buffer for sending.
1534 * When the sending returns a partial buffer we stop sending more data as
1535 * the next send operation could block.
1537 * This functions returns FALSE if some error occured.
1540 gst_multi_fd_sink_handle_client_write (GstMultiFdSink * sink,
1541 GstTCPClient * client)
1543 int fd = client->fd.fd;
1548 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1549 GstMultiHandleSinkClass *mhsinkclass =
1550 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
1551 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1554 g_get_current_time (&nowtv);
1555 now = GST_TIMEVAL_TO_TIME (nowtv);
1557 flushing = mhclient->status == GST_CLIENT_STATUS_FLUSHING;
1563 if (!mhclient->sending) {
1564 /* client is not working on a buffer */
1565 if (mhclient->bufpos == -1) {
1566 /* client is too fast, remove from write queue until new buffer is
1568 gst_poll_fd_ctl_write (sink->fdset, &client->fd, FALSE);
1569 /* if we flushed out all of the client buffers, we can stop */
1570 if (mhclient->flushcount == 0)
1575 /* client can pick a buffer from the global queue */
1577 GstClockTime timestamp;
1579 /* for new connections, we need to find a good spot in the
1580 * bufqueue to start streaming from */
1581 if (mhclient->new_connection && !flushing) {
1582 gint position = gst_multi_fd_sink_new_client (sink, client);
1584 if (position >= 0) {
1585 /* we got a valid spot in the queue */
1586 mhclient->new_connection = FALSE;
1587 mhclient->bufpos = position;
1589 /* cannot send data to this client yet */
1590 gst_poll_fd_ctl_write (sink->fdset, &client->fd, FALSE);
1595 /* we flushed all remaining buffers, no need to get a new one */
1596 if (mhclient->flushcount == 0)
1600 buf = g_array_index (mhsink->bufqueue, GstBuffer *, mhclient->bufpos);
1604 timestamp = GST_BUFFER_TIMESTAMP (buf);
1605 if (mhclient->first_buffer_ts == GST_CLOCK_TIME_NONE)
1606 mhclient->first_buffer_ts = timestamp;
1607 if (timestamp != -1)
1608 mhclient->last_buffer_ts = timestamp;
1610 /* decrease flushcount */
1611 if (mhclient->flushcount != -1)
1612 mhclient->flushcount--;
1614 GST_LOG_OBJECT (sink, "[fd %5d] client %p at position %d",
1615 fd, client, mhclient->bufpos);
1617 /* queueing a buffer will ref it */
1618 mhsinkclass->client_queue_buffer (mhsink, mhclient, buf);
1620 /* need to start from the first byte for this new buffer */
1621 mhclient->bufoffset = 0;
1625 /* see if we need to send something */
1626 if (mhclient->sending) {
1632 /* pick first buffer from list */
1633 head = GST_BUFFER (mhclient->sending->data);
1635 g_assert (gst_buffer_map (head, &info, GST_MAP_READ));
1637 maxsize = info.size - mhclient->bufoffset;
1639 /* try to write the complete buffer */
1641 #define FLAGS MSG_NOSIGNAL
1645 if (client->is_socket) {
1646 wrote = send (fd, data + mhclient->bufoffset, maxsize, FLAGS);
1648 wrote = write (fd, data + mhclient->bufoffset, maxsize);
1650 gst_buffer_unmap (head, &info);
1654 if (errno == EAGAIN) {
1655 /* nothing serious, resource was unavailable, try again later */
1657 } else if (errno == ECONNRESET) {
1658 goto connection_reset;
1663 if (wrote < maxsize) {
1664 /* partial write means that the client cannot read more and we should
1665 * stop sending more */
1666 GST_LOG_OBJECT (sink,
1667 "partial write on %d of %" G_GSSIZE_FORMAT " bytes", fd, wrote);
1668 mhclient->bufoffset += wrote;
1671 /* complete buffer was written, we can proceed to the next one */
1672 mhclient->sending = g_slist_remove (mhclient->sending, head);
1673 gst_buffer_unref (head);
1674 /* make sure we start from byte 0 for the next buffer */
1675 mhclient->bufoffset = 0;
1678 mhclient->bytes_sent += wrote;
1679 mhclient->last_activity_time = now;
1680 mhsink->bytes_served += wrote;
1690 GST_DEBUG_OBJECT (sink, "[fd %5d] flushed, removing", fd);
1691 mhclient->status = GST_CLIENT_STATUS_REMOVED;
1696 GST_DEBUG_OBJECT (sink, "[fd %5d] connection reset by peer, removing", fd);
1697 mhclient->status = GST_CLIENT_STATUS_CLOSED;
1702 GST_WARNING_OBJECT (sink,
1703 "[fd %5d] could not write, removing client: %s (%d)", fd,
1704 g_strerror (errno), errno);
1705 mhclient->status = GST_CLIENT_STATUS_ERROR;
1710 /* calculate the new position for a client after recovery. This function
1711 * does not update the client position but merely returns the required
1715 gst_multi_fd_sink_recover_client (GstMultiFdSink * sink, GstTCPClient * client)
1718 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1719 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
1721 GST_WARNING_OBJECT (sink,
1722 "[fd %5d] client %p is lagging at %d, recover using policy %d",
1723 client->fd.fd, client, mhclient->bufpos, mhsink->recover_policy);
1725 switch (mhsink->recover_policy) {
1726 case GST_RECOVER_POLICY_NONE:
1727 /* do nothing, client will catch up or get kicked out when it reaches
1729 newbufpos = mhclient->bufpos;
1731 case GST_RECOVER_POLICY_RESYNC_LATEST:
1732 /* move to beginning of queue */
1735 case GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT:
1736 /* move to beginning of soft max */
1737 newbufpos = get_buffers_max (sink, mhsink->units_soft_max);
1739 case GST_RECOVER_POLICY_RESYNC_KEYFRAME:
1740 /* find keyframe in buffers, we search backwards to find the
1741 * closest keyframe relative to what this client already received. */
1742 newbufpos = MIN (mhsink->bufqueue->len - 1,
1743 get_buffers_max (sink, mhsink->units_soft_max) - 1);
1745 while (newbufpos >= 0) {
1748 buf = g_array_index (mhsink->bufqueue, GstBuffer *, newbufpos);
1749 if (is_sync_frame (mhsink, buf)) {
1750 /* found a buffer that is not a delta unit */
1757 /* unknown recovery procedure */
1758 newbufpos = get_buffers_max (sink, mhsink->units_soft_max);
1764 /* Queue a buffer on the global queue.
1766 * This function adds the buffer to the front of a GArray. It removes the
1767 * tail buffer if the max queue size is exceeded, unreffing the queued buffer.
1768 * Note that unreffing the buffer is not a problem as clients who
1769 * started writing out this buffer will still have a reference to it in the
1770 * mhclient->sending queue.
1772 * After adding the buffer, we update all client positions in the queue. If
1773 * a client moves over the soft max, we start the recovery procedure for this
1774 * slow client. If it goes over the hard max, it is put into the slow list
1777 * Special care is taken of clients that were waiting for a new buffer (they
1778 * had a position of -1) because they can proceed after adding this new buffer.
1779 * This is done by adding the client back into the write fd_set and signaling
1780 * the select thread that the fd_set changed.
1783 gst_multi_fd_sink_queue_buffer (GstMultiHandleSink * mhsink, GstBuffer * buffer)
1785 GList *clients, *next;
1787 gboolean need_signal = FALSE;
1788 gint max_buffer_usage;
1792 gint max_buffers, soft_max_buffers;
1794 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
1795 GstMultiHandleSinkClass *mhsinkclass =
1796 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
1798 g_get_current_time (&nowtv);
1799 now = GST_TIMEVAL_TO_TIME (nowtv);
1801 CLIENTS_LOCK (mhsink);
1802 /* add buffer to queue */
1803 g_array_prepend_val (mhsink->bufqueue, buffer);
1804 queuelen = mhsink->bufqueue->len;
1806 if (mhsink->units_max > 0)
1807 max_buffers = get_buffers_max (sink, mhsink->units_max);
1811 if (mhsink->units_soft_max > 0)
1812 soft_max_buffers = get_buffers_max (sink, mhsink->units_soft_max);
1814 soft_max_buffers = -1;
1815 GST_LOG_OBJECT (sink, "Using max %d, softmax %d", max_buffers,
1818 /* then loop over the clients and update the positions */
1819 max_buffer_usage = 0;
1822 cookie = mhsink->clients_cookie;
1823 for (clients = mhsink->clients; clients; clients = next) {
1824 GstTCPClient *client;
1825 GstMultiHandleClient *mhclient;
1827 if (cookie != mhsink->clients_cookie) {
1828 GST_DEBUG_OBJECT (sink, "Clients cookie outdated, restarting");
1832 client = (GstTCPClient *) clients->data;
1833 mhclient = (GstMultiHandleClient *) client;
1834 next = g_list_next (clients);
1837 GST_LOG_OBJECT (sink, "[fd %5d] client %p at position %d",
1838 client->fd.fd, client, mhclient->bufpos);
1839 /* check soft max if needed, recover client */
1840 if (soft_max_buffers > 0 && mhclient->bufpos >= soft_max_buffers) {
1843 newpos = gst_multi_fd_sink_recover_client (sink, client);
1844 if (newpos != mhclient->bufpos) {
1845 mhclient->dropped_buffers += mhclient->bufpos - newpos;
1846 mhclient->bufpos = newpos;
1847 mhclient->discont = TRUE;
1848 GST_INFO_OBJECT (sink, "[fd %5d] client %p position reset to %d",
1849 client->fd.fd, client, mhclient->bufpos);
1851 GST_INFO_OBJECT (sink,
1852 "[fd %5d] client %p not recovering position",
1853 client->fd.fd, client);
1856 /* check hard max and timeout, remove client */
1857 if ((max_buffers > 0 && mhclient->bufpos >= max_buffers) ||
1858 (mhsink->timeout > 0
1859 && now - mhclient->last_activity_time > mhsink->timeout)) {
1861 GST_WARNING_OBJECT (sink, "[fd %5d] client %p is too slow, removing",
1862 client->fd.fd, client);
1863 /* remove the client, the fd set will be cleared and the select thread
1864 * will be signaled */
1865 mhclient->status = GST_CLIENT_STATUS_SLOW;
1866 /* set client to invalid position while being removed */
1867 mhclient->bufpos = -1;
1868 mhsinkclass->remove_client_link (mhsink, clients);
1871 } else if (mhclient->bufpos == 0 || mhclient->new_connection) {
1872 /* can send data to this client now. need to signal the select thread that
1873 * the fd_set changed */
1874 gst_poll_fd_ctl_write (sink->fdset, &client->fd, TRUE);
1877 /* keep track of maximum buffer usage */
1878 if (mhclient->bufpos > max_buffer_usage) {
1879 max_buffer_usage = mhclient->bufpos;
1883 /* make sure we respect bytes-min, buffers-min and time-min when they are set */
1887 GST_LOG_OBJECT (sink,
1888 "extending queue %d to respect time_min %" GST_TIME_FORMAT
1889 ", bytes_min %d, buffers_min %d", max_buffer_usage,
1890 GST_TIME_ARGS (mhsink->time_min), mhsink->bytes_min,
1891 mhsink->buffers_min);
1893 /* get index where the limits are ok, we don't really care if all limits
1894 * are ok, we just queue as much as we need. We also don't compare against
1895 * the max limits. */
1896 find_limits (sink, &usage, mhsink->bytes_min, mhsink->buffers_min,
1897 mhsink->time_min, &max, -1, -1, -1);
1899 max_buffer_usage = MAX (max_buffer_usage, usage + 1);
1900 GST_LOG_OBJECT (sink, "extended queue to %d", max_buffer_usage);
1903 /* now look for sync points and make sure there is at least one
1904 * sync point in the queue. We only do this if the LATEST_KEYFRAME or
1905 * BURST_KEYFRAME mode is selected */
1906 if (mhsink->def_sync_method == GST_SYNC_METHOD_LATEST_KEYFRAME ||
1907 mhsink->def_sync_method == GST_SYNC_METHOD_BURST_KEYFRAME) {
1908 /* no point in searching beyond the queue length */
1909 gint limit = queuelen;
1912 /* no point in searching beyond the soft-max if any. */
1913 if (soft_max_buffers > 0) {
1914 limit = MIN (limit, soft_max_buffers);
1916 GST_LOG_OBJECT (sink,
1917 "extending queue to include sync point, now at %d, limit is %d",
1918 max_buffer_usage, limit);
1919 for (i = 0; i < limit; i++) {
1920 buf = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1921 if (is_sync_frame (mhsink, buf)) {
1922 /* found a sync frame, now extend the buffer usage to
1923 * include at least this frame. */
1924 max_buffer_usage = MAX (max_buffer_usage, i);
1928 GST_LOG_OBJECT (sink, "max buffer usage is now %d", max_buffer_usage);
1931 GST_LOG_OBJECT (sink, "len %d, usage %d", queuelen, max_buffer_usage);
1933 /* nobody is referencing units after max_buffer_usage so we can
1934 * remove them from the queue. We remove them in reverse order as
1935 * this is the most optimal for GArray. */
1936 for (i = queuelen - 1; i > max_buffer_usage; i--) {
1939 /* queue exceeded max size */
1941 old = g_array_index (mhsink->bufqueue, GstBuffer *, i);
1942 mhsink->bufqueue = g_array_remove_index (mhsink->bufqueue, i);
1944 /* unref tail buffer */
1945 gst_buffer_unref (old);
1947 /* save for stats */
1948 mhsink->buffers_queued = max_buffer_usage;
1949 CLIENTS_UNLOCK (sink);
1951 /* and send a signal to thread if fd_set changed */
1953 gst_poll_restart (sink->fdset);
1957 /* Handle the clients. Basically does a blocking select for one
1958 * of the client fds to become read or writable. We also have a
1959 * filedescriptor to receive commands on that we need to check.
1961 * After going out of the select call, we read and write to all
1962 * clients that can do so. Badly behaving clients are put on a
1963 * garbage list and removed.
1966 gst_multi_fd_sink_handle_clients (GstMultiFdSink * sink)
1969 GList *clients, *next;
1971 GstMultiFdSinkClass *fclass;
1973 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
1974 GstMultiHandleSinkClass *mhsinkclass =
1975 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
1978 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
1984 * - server socket input (ie, new client connections)
1985 * - client socket input (ie, clients saying goodbye)
1986 * - client socket output (ie, client reads) */
1987 GST_LOG_OBJECT (sink, "waiting on action on fdset");
1990 gst_poll_wait (sink->fdset,
1991 mhsink->timeout != 0 ? mhsink->timeout : GST_CLOCK_TIME_NONE);
1993 /* Handle the special case in which the sink is not receiving more buffers
1994 * and will not disconnect inactive client in the streaming thread. */
1995 if (G_UNLIKELY (result == 0)) {
1999 g_get_current_time (&nowtv);
2000 now = GST_TIMEVAL_TO_TIME (nowtv);
2002 CLIENTS_LOCK (sink);
2003 for (clients = mhsink->clients; clients; clients = next) {
2004 GstTCPClient *client;
2005 GstMultiHandleClient *mhclient;
2007 client = (GstTCPClient *) clients->data;
2008 mhclient = (GstMultiHandleClient *) client;
2009 next = g_list_next (clients);
2010 if (mhsink->timeout > 0
2011 && now - mhclient->last_activity_time > mhsink->timeout) {
2012 mhclient->status = GST_CLIENT_STATUS_SLOW;
2013 mhsinkclass->remove_client_link (mhsink, clients);
2016 CLIENTS_UNLOCK (sink);
2018 } else if (result < 0) {
2019 GST_WARNING_OBJECT (sink, "wait failed: %s (%d)", g_strerror (errno),
2021 if (errno == EBADF) {
2022 /* ok, so one or more of the fds is invalid. We loop over them to find
2023 * the ones that give an error to the F_GETFL fcntl. */
2024 CLIENTS_LOCK (sink);
2026 cookie = mhsink->clients_cookie;
2027 for (clients = mhsink->clients; clients; clients = next) {
2028 GstTCPClient *client;
2029 GstMultiHandleClient *mhclient;
2034 if (cookie != mhsink->clients_cookie) {
2035 GST_DEBUG_OBJECT (sink, "Cookie changed finding bad fd");
2039 client = (GstTCPClient *) clients->data;
2040 mhclient = (GstMultiHandleClient *) client;
2041 next = g_list_next (clients);
2045 res = fcntl (fd, F_GETFL, &flags);
2047 GST_WARNING_OBJECT (sink, "fnctl failed for %d, removing: %s (%d)",
2048 fd, g_strerror (errno), errno);
2049 if (errno == EBADF) {
2050 mhclient->status = GST_CLIENT_STATUS_ERROR;
2051 /* releases the CLIENTS lock */
2052 mhsinkclass->remove_client_link (mhsink, clients);
2056 CLIENTS_UNLOCK (sink);
2057 /* after this, go back in the select loop as the read/writefds
2060 } else if (errno == EINTR) {
2061 /* interrupted system call, just redo the wait */
2063 } else if (errno == EBUSY) {
2064 /* the call to gst_poll_wait() was flushed */
2067 /* this is quite bad... */
2068 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2069 ("select failed: %s (%d)", g_strerror (errno), errno));
2073 GST_LOG_OBJECT (sink, "wait done: %d sockets with events", result);
2075 } while (try_again);
2077 /* subclasses can check fdset with this virtual function */
2079 fclass->wait (sink, sink->fdset);
2081 /* Check the clients */
2082 CLIENTS_LOCK (sink);
2085 cookie = mhsink->clients_cookie;
2086 for (clients = mhsink->clients; clients; clients = next) {
2087 GstTCPClient *client;
2088 GstMultiHandleClient *mhclient;
2090 if (mhsink->clients_cookie != cookie) {
2091 GST_DEBUG_OBJECT (sink, "Restarting loop, cookie out of date");
2095 client = (GstTCPClient *) clients->data;
2096 mhclient = (GstMultiHandleClient *) client;
2097 next = g_list_next (clients);
2099 if (mhclient->status != GST_CLIENT_STATUS_FLUSHING
2100 && mhclient->status != GST_CLIENT_STATUS_OK) {
2101 mhsinkclass->remove_client_link (mhsink, clients);
2105 if (gst_poll_fd_has_closed (sink->fdset, &client->fd)) {
2106 mhclient->status = GST_CLIENT_STATUS_CLOSED;
2107 mhsinkclass->remove_client_link (mhsink, clients);
2110 if (gst_poll_fd_has_error (sink->fdset, &client->fd)) {
2111 GST_WARNING_OBJECT (sink, "gst_poll_fd_has_error for %d", client->fd.fd);
2112 mhclient->status = GST_CLIENT_STATUS_ERROR;
2113 mhsinkclass->remove_client_link (mhsink, clients);
2116 if (gst_poll_fd_can_read (sink->fdset, &client->fd)) {
2117 /* handle client read */
2118 if (!gst_multi_fd_sink_handle_client_read (sink, client)) {
2119 mhsinkclass->remove_client_link (mhsink, clients);
2123 if (gst_poll_fd_can_write (sink->fdset, &client->fd)) {
2124 /* handle client write */
2125 if (!gst_multi_fd_sink_handle_client_write (sink, client)) {
2126 mhsinkclass->remove_client_link (mhsink, clients);
2131 CLIENTS_UNLOCK (sink);
2134 /* we handle the client communication in another thread so that we do not block
2135 * the gstreamer thread while we select() on the client fds */
2137 gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink)
2139 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
2141 while (mhsink->running) {
2142 gst_multi_fd_sink_handle_clients (sink);
2148 gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
2149 const GValue * value, GParamSpec * pspec)
2151 GstMultiFdSink *multifdsink;
2153 multifdsink = GST_MULTI_FD_SINK (object);
2157 multifdsink->mode = g_value_get_enum (value);
2159 case PROP_UNIT_FORMAT:
2160 multifdsink->unit_format = g_value_get_enum (value);
2162 case PROP_BURST_FORMAT:
2163 multifdsink->def_burst_format = g_value_get_enum (value);
2165 case PROP_BURST_VALUE:
2166 multifdsink->def_burst_value = g_value_get_uint64 (value);
2168 case PROP_HANDLE_READ:
2169 multifdsink->handle_read = g_value_get_boolean (value);
2173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2179 gst_multi_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
2182 GstMultiFdSink *multifdsink;
2184 multifdsink = GST_MULTI_FD_SINK (object);
2188 g_value_set_enum (value, multifdsink->mode);
2190 case PROP_UNIT_FORMAT:
2191 g_value_set_enum (value, multifdsink->unit_format);
2193 case PROP_BURST_FORMAT:
2194 g_value_set_enum (value, multifdsink->def_burst_format);
2196 case PROP_BURST_VALUE:
2197 g_value_set_uint64 (value, multifdsink->def_burst_value);
2199 case PROP_HANDLE_READ:
2200 g_value_set_boolean (value, multifdsink->handle_read);
2203 g_value_set_uint (value, g_hash_table_size (multifdsink->fd_hash));
2207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2213 gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink)
2215 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
2217 GST_INFO_OBJECT (mfsink, "starting in mode %d", mfsink->mode);
2218 if ((mfsink->fdset = gst_poll_new (TRUE)) == NULL)
2226 GST_ELEMENT_ERROR (mfsink, RESOURCE, OPEN_READ_WRITE, (NULL),
2233 multifdsink_hash_remove (gpointer key, gpointer value, gpointer data)
2239 gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink)
2241 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
2243 gst_poll_set_flushing (mfsink->fdset, TRUE);
2248 gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink)
2250 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
2252 if (mfsink->fdset) {
2253 gst_poll_free (mfsink->fdset);
2254 mfsink->fdset = NULL;
2256 g_hash_table_foreach_remove (mfsink->fd_hash, multifdsink_hash_remove,