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 * @short_description: Send data to multiple file descriptors
25 * @see_also: tcpserversink
29 * This plugin writes incoming data to a set of file descriptors. The
30 * file descriptors can be added to multifdsink by emitting the "add" signal.
31 * For each descriptor added, the "client-added" signal will be called.
34 * As of version 0.10.8, a client can also be added with the "add-full" signal
35 * that allows for more control over what and how much data a client
39 * Clients can be removed from multifdsink by emitting the "remove" signal. For
40 * each descriptor removed, the "client-removed" signal will be called. The
41 * "client-removed" signal can also be fired when multifdsink decides that a
42 * client is not active anymore or, depending on the value of the
43 * "recover-policy" property, if the client is reading too slowly.
44 * In all cases, multifdsink will never close a file descriptor itself.
45 * The user of multifdsink is responsible for closing all file descriptors.
46 * This can for example be done in response to the "client-fd-removed" signal.
47 * Note that multifdsink still has a reference to the file descriptor when the
48 * "client-removed" signal is emitted, so that "get-stats" can be performed on
49 * the descriptor; it is therefore not safe to close the file descriptor in
50 * the "client-removed" signal handler, and you should use the
51 * "client-fd-removed" signal to safely close the fd.
54 * Multifdsink internally keeps a queue of the incoming buffers and uses a
55 * separate thread to send the buffers to the clients. This ensures that no
56 * client write can block the pipeline and that clients can read with different
60 * When adding a client to multifdsink, the "sync-method" property will define
61 * which buffer in the queued buffers will be sent first to the client. Clients
62 * can be sent the most recent buffer (which might not be decodable by the
63 * client if it is not a keyframe), the next keyframe received in
64 * multifdsink (which can take some time depending on the keyframe rate), or the
65 * last received keyframe (which will cause a simple burst-on-connect).
66 * Multifdsink will always keep at least one keyframe in its internal buffers
67 * when the sync-mode is set to latest-keyframe.
70 * As of version 0.10.8, there are additional values for the sync-method
71 * property to allow finer control over burst-on-connect behaviour. By selecting
72 * the 'burst' method a minimum burst size can be chosen, 'burst-keyframe'
73 * additionally requires that the burst begin with a keyframe, and
74 * 'burst-with-keyframe' attempts to burst beginning with a keyframe, but will
75 * prefer a minimum burst size even if it requires not starting with a keyframe.
78 * Multifdsink can be instructed to keep at least a minimum amount of data
79 * expressed in time or byte units in its internal queues with the the
80 * "time-min" and "bytes-min" properties respectively. These properties are
81 * useful if the application adds clients with the "add-full" signal to
82 * make sure that a burst connect can actually be honored.
85 * When streaming data, clients are allowed to read at a different rate than
86 * the rate at which multifdsink receives data. If the client is reading too
87 * fast, no data will be send to the client until multifdsink receives more
88 * data. If the client, however, reads too slowly, data for that client will be
89 * queued up in multifdsink. Two properties control the amount of data
90 * (buffers) that is queued in multifdsink: "buffers-max" and
91 * "buffers-soft-max". A client that falls behind by "buffers-max" is removed
92 * from multifdsink forcibly.
95 * A client with a lag of at least "buffers-soft-max" enters the recovery
96 * procedure which is controlled with the "recover-policy" property. A recover
97 * policy of NONE will do nothing, RESYNC_LATEST will send the most recently
98 * received buffer as the next buffer for the client, RESYNC_SOFT_LIMIT
99 * positions the client to the soft limit in the buffer queue and
100 * RESYNC_KEYFRAME positions the client at the most recent keyframe in the
104 * multifdsink will by default synchronize on the clock before serving the
105 * buffers to the clients. This behaviour can be disabled by setting the sync
106 * property to FALSE. Multifdsink will by default not do QoS and will never
111 * Last reviewed on 2006-09-12 (0.10.10)
117 #include <gst/gst-i18n-plugin.h>
119 #include <sys/ioctl.h>
122 #include <sys/types.h>
123 #include <sys/socket.h>
124 #include <sys/stat.h>
126 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
127 #include <sys/filio.h>
130 #include "gstmultifdsink.h"
131 #include "gsttcp-marshal.h"
133 #define NOT_IMPLEMENTED 0
135 /* the select call is also performed on the control sockets, that way
136 * we can send special commands to unblock or restart the select call */
137 #define CONTROL_RESTART 'R' /* restart the select call */
138 #define CONTROL_STOP 'S' /* stop the select call */
139 #define CONTROL_SOCKETS(sink) sink->control_sock
140 #define WRITE_SOCKET(sink) sink->control_sock[1]
141 #define READ_SOCKET(sink) sink->control_sock[0]
143 #define SEND_COMMAND(sink, command) \
145 unsigned char c; c = command; \
146 write (WRITE_SOCKET(sink).fd, &c, 1); \
149 #define READ_COMMAND(sink, command, res) \
151 res = read(READ_SOCKET(sink).fd, &command, 1);\
154 /* elementfactory information */
155 static const GstElementDetails gst_multi_fd_sink_details =
156 GST_ELEMENT_DETAILS ("Multi filedescriptor sink",
158 "Send data to multiple filedescriptors",
159 "Thomas Vander Stichele <thomas at apestaart dot org>, "
160 "Wim Taymans <wim@fluendo.com>");
162 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
165 GST_STATIC_CAPS_ANY);
167 GST_DEBUG_CATEGORY_STATIC (multifdsink_debug);
168 #define GST_CAT_DEFAULT (multifdsink_debug)
170 /* MultiFdSink signals and args */
182 SIGNAL_CLIENT_REMOVED,
183 SIGNAL_CLIENT_FD_REMOVED,
188 /* this is really arbitrarily chosen */
189 #define DEFAULT_PROTOCOL GST_TCP_PROTOCOL_NONE
190 #define DEFAULT_MODE GST_FDSET_MODE_POLL
191 #define DEFAULT_BUFFERS_MAX -1
192 #define DEFAULT_BUFFERS_SOFT_MAX -1
193 #define DEFAULT_TIME_MIN -1
194 #define DEFAULT_BYTES_MIN -1
195 #define DEFAULT_BUFFERS_MIN -1
196 #define DEFAULT_UNIT_TYPE GST_UNIT_TYPE_BUFFERS
197 #define DEFAULT_UNITS_MAX -1
198 #define DEFAULT_UNITS_SOFT_MAX -1
199 #define DEFAULT_RECOVER_POLICY GST_RECOVER_POLICY_NONE
200 #define DEFAULT_TIMEOUT 0
201 #define DEFAULT_SYNC_METHOD GST_SYNC_METHOD_LATEST
203 #define DEFAULT_BURST_UNIT GST_UNIT_TYPE_UNDEFINED
204 #define DEFAULT_BURST_VALUE 0
220 PROP_BUFFERS_SOFT_MAX,
236 #define GST_TYPE_RECOVER_POLICY (gst_recover_policy_get_type())
238 gst_recover_policy_get_type (void)
240 static GType recover_policy_type = 0;
241 static const GEnumValue recover_policy[] = {
242 {GST_RECOVER_POLICY_NONE,
243 "Do not try to recover", "none"},
244 {GST_RECOVER_POLICY_RESYNC_LATEST,
245 "Resync client to latest buffer", "latest"},
246 {GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT,
247 "Resync client to soft limit", "soft-limit"},
248 {GST_RECOVER_POLICY_RESYNC_KEYFRAME,
249 "Resync client to most recent keyframe", "keyframe"},
253 if (!recover_policy_type) {
254 recover_policy_type =
255 g_enum_register_static ("GstRecoverPolicy", recover_policy);
257 return recover_policy_type;
260 #define GST_TYPE_SYNC_METHOD (gst_sync_method_get_type())
262 gst_sync_method_get_type (void)
264 static GType sync_method_type = 0;
265 static const GEnumValue sync_method[] = {
266 {GST_SYNC_METHOD_LATEST,
267 "Serve starting from the latest buffer", "latest"},
268 {GST_SYNC_METHOD_NEXT_KEYFRAME,
269 "Serve starting from the next keyframe", "next-keyframe"},
270 {GST_SYNC_METHOD_LATEST_KEYFRAME,
271 "Serve everything since the latest keyframe (burst)",
273 {GST_SYNC_METHOD_BURST, "Serve burst-value data to client", "burst"},
274 {GST_SYNC_METHOD_BURST_KEYFRAME,
275 "Serve burst-value data starting on a keyframe",
277 {GST_SYNC_METHOD_BURST_WITH_KEYFRAME,
278 "Serve burst-value data preferably starting on a keyframe",
279 "burst-with-keyframe"},
283 if (!sync_method_type) {
284 sync_method_type = g_enum_register_static ("GstSyncMethod", sync_method);
286 return sync_method_type;
289 #define GST_TYPE_UNIT_TYPE (gst_unit_type_get_type())
291 gst_unit_type_get_type (void)
293 static GType unit_type_type = 0;
294 static const GEnumValue unit_type[] = {
295 {GST_UNIT_TYPE_UNDEFINED, "Undefined", "undefined"},
296 {GST_UNIT_TYPE_BUFFERS, "Buffers", "buffers"},
297 {GST_UNIT_TYPE_BYTES, "Bytes", "bytes"},
298 {GST_UNIT_TYPE_TIME, "Time", "time"},
302 if (!unit_type_type) {
303 unit_type_type = g_enum_register_static ("GstTCPUnitType", unit_type);
305 return unit_type_type;
308 #define GST_TYPE_CLIENT_STATUS (gst_client_status_get_type())
310 gst_client_status_get_type (void)
312 static GType client_status_type = 0;
313 static const GEnumValue client_status[] = {
314 {GST_CLIENT_STATUS_OK, "ok", "ok"},
315 {GST_CLIENT_STATUS_CLOSED, "Closed", "closed"},
316 {GST_CLIENT_STATUS_REMOVED, "Removed", "removed"},
317 {GST_CLIENT_STATUS_SLOW, "Too slow", "slow"},
318 {GST_CLIENT_STATUS_ERROR, "Error", "error"},
319 {GST_CLIENT_STATUS_DUPLICATE, "Duplicate", "duplicate"},
323 if (!client_status_type) {
325 g_enum_register_static ("GstClientStatus", client_status);
327 return client_status_type;
330 static void gst_multi_fd_sink_finalize (GObject * object);
332 static void gst_multi_fd_sink_remove_client_link (GstMultiFdSink * sink,
335 static GstFlowReturn gst_multi_fd_sink_render (GstBaseSink * bsink,
337 static GstStateChangeReturn gst_multi_fd_sink_change_state (GstElement *
338 element, GstStateChange transition);
340 static void gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
341 const GValue * value, GParamSpec * pspec);
342 static void gst_multi_fd_sink_get_property (GObject * object, guint prop_id,
343 GValue * value, GParamSpec * pspec);
345 GST_BOILERPLATE (GstMultiFdSink, gst_multi_fd_sink, GstBaseSink,
348 static guint gst_multi_fd_sink_signals[LAST_SIGNAL] = { 0 };
351 gst_multi_fd_sink_base_init (gpointer g_class)
353 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
355 gst_element_class_add_pad_template (element_class,
356 gst_static_pad_template_get (&sinktemplate));
358 gst_element_class_set_details (element_class, &gst_multi_fd_sink_details);
362 gst_multi_fd_sink_class_init (GstMultiFdSinkClass * klass)
364 GObjectClass *gobject_class;
365 GstElementClass *gstelement_class;
366 GstBaseSinkClass *gstbasesink_class;
368 gobject_class = (GObjectClass *) klass;
369 gstelement_class = (GstElementClass *) klass;
370 gstbasesink_class = (GstBaseSinkClass *) klass;
372 gobject_class->set_property = gst_multi_fd_sink_set_property;
373 gobject_class->get_property = gst_multi_fd_sink_get_property;
374 gobject_class->finalize = gst_multi_fd_sink_finalize;
376 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
377 g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in",
378 GST_TYPE_TCP_PROTOCOL, DEFAULT_PROTOCOL, G_PARAM_READWRITE));
379 g_object_class_install_property (gobject_class, PROP_MODE,
380 g_param_spec_enum ("mode", "Mode",
381 "The mode for selecting activity on the fds", GST_TYPE_FDSET_MODE,
382 DEFAULT_MODE, G_PARAM_READWRITE));
384 g_object_class_install_property (gobject_class, PROP_BUFFERS_MAX,
385 g_param_spec_int ("buffers-max", "Buffers max",
386 "max number of buffers to queue for a client (-1 = no limit)", -1,
387 G_MAXINT, DEFAULT_BUFFERS_MAX, G_PARAM_READWRITE));
388 g_object_class_install_property (gobject_class,
389 PROP_BUFFERS_SOFT_MAX, g_param_spec_int ("buffers-soft-max",
391 "Recover client when going over this limit (-1 = no limit)", -1,
392 G_MAXINT, DEFAULT_BUFFERS_SOFT_MAX, G_PARAM_READWRITE));
394 g_object_class_install_property (gobject_class, PROP_BYTES_MIN,
395 g_param_spec_int ("bytes-min", "Bytes min",
396 "min number of bytes to queue (-1 = as little as possible)", -1,
397 G_MAXINT, DEFAULT_BYTES_MIN, G_PARAM_READWRITE));
398 g_object_class_install_property (gobject_class, PROP_TIME_MIN,
399 g_param_spec_int64 ("time-min", "Time min",
400 "min number of time to queue (-1 = as little as possible)", -1,
401 G_MAXINT64, DEFAULT_TIME_MIN, G_PARAM_READWRITE));
402 g_object_class_install_property (gobject_class, PROP_BUFFERS_MIN,
403 g_param_spec_int ("buffers-min", "Buffers min",
404 "min number of buffers to queue (-1 = as few as possible)", -1,
405 G_MAXINT, DEFAULT_BUFFERS_MIN, G_PARAM_READWRITE));
407 g_object_class_install_property (gobject_class, PROP_UNIT_TYPE,
408 g_param_spec_enum ("unit-type", "Units type",
409 "The unit to measure the max/soft-max/queued properties",
410 GST_TYPE_UNIT_TYPE, DEFAULT_UNIT_TYPE, G_PARAM_READWRITE));
411 g_object_class_install_property (gobject_class, PROP_UNITS_MAX,
412 g_param_spec_int64 ("units-max", "Units max",
413 "max number of units to queue (-1 = no limit)", -1, G_MAXINT64,
414 DEFAULT_UNITS_MAX, G_PARAM_READWRITE));
415 g_object_class_install_property (gobject_class, PROP_UNITS_SOFT_MAX,
416 g_param_spec_int64 ("units-soft-max", "Units soft max",
417 "Recover client when going over this limit (-1 = no limit)", -1,
418 G_MAXINT64, DEFAULT_UNITS_SOFT_MAX, G_PARAM_READWRITE));
420 g_object_class_install_property (gobject_class, PROP_BUFFERS_QUEUED,
421 g_param_spec_uint ("buffers-queued", "Buffers queued",
422 "Number of buffers currently queued", 0, G_MAXUINT, 0,
425 g_object_class_install_property (gobject_class, PROP_BYTES_QUEUED,
426 g_param_spec_uint ("bytes-queued", "Bytes queued",
427 "Number of bytes currently queued", 0, G_MAXUINT, 0,
429 g_object_class_install_property (gobject_class, PROP_TIME_QUEUED,
430 g_param_spec_uint64 ("time-queued", "Time queued",
431 "Number of time currently queued", 0, G_MAXUINT64, 0,
435 g_object_class_install_property (gobject_class, PROP_RECOVER_POLICY,
436 g_param_spec_enum ("recover-policy", "Recover Policy",
437 "How to recover when client reaches the soft max",
438 GST_TYPE_RECOVER_POLICY, DEFAULT_RECOVER_POLICY, G_PARAM_READWRITE));
439 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
440 g_param_spec_uint64 ("timeout", "Timeout",
441 "Maximum inactivity timeout in nanoseconds for a client (0 = no limit)",
442 0, G_MAXUINT64, DEFAULT_TIMEOUT, G_PARAM_READWRITE));
443 g_object_class_install_property (gobject_class, PROP_SYNC_METHOD,
444 g_param_spec_enum ("sync-method", "Sync Method",
445 "How to sync new clients to the stream",
446 GST_TYPE_SYNC_METHOD, DEFAULT_SYNC_METHOD, G_PARAM_READWRITE));
447 g_object_class_install_property (gobject_class, PROP_BYTES_TO_SERVE,
448 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
449 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
451 g_object_class_install_property (gobject_class, PROP_BYTES_SERVED,
452 g_param_spec_uint64 ("bytes-served", "Bytes served",
453 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
456 g_object_class_install_property (gobject_class, PROP_BURST_UNIT,
457 g_param_spec_enum ("burst-unit", "Burst unit",
458 "The format of the burst units (when sync-method is burst[[-with]-keyframe])",
459 GST_TYPE_UNIT_TYPE, DEFAULT_BURST_UNIT, G_PARAM_READWRITE));
460 g_object_class_install_property (gobject_class, PROP_BURST_VALUE,
461 g_param_spec_uint64 ("burst-value", "Burst value",
462 "The amount of burst expressed in burst-unit",
463 0, G_MAXUINT64, DEFAULT_BURST_VALUE, G_PARAM_READWRITE));
466 * GstMultiFdSink::add:
467 * @gstmultifdsink: the multifdsink element to emit this signal on
468 * @fd: the file descriptor to add to multifdsink
470 * Hand the given open file descriptor to multifdsink to write to.
472 gst_multi_fd_sink_signals[SIGNAL_ADD] =
473 g_signal_new ("add", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
474 G_STRUCT_OFFSET (GstMultiFdSinkClass, add),
475 NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
477 * GstMultiFdSink::add-full:
478 * @gstmultifdsink: the multifdsink element to emit this signal on
479 * @fd: the file descriptor to add to multifdsink
480 * @keyframe: start bursting from a keyframe
481 * @unit_type_min: the unit-type of @value_min
482 * @value_min: the minimum amount of data to burst expressed in
483 * @unit_type_min units.
484 * @unit_type_max: the unit-type of @value_max
485 * @value_max: the maximum amount of data to burst expressed in
486 * @unit_type_max units.
488 * Hand the given open file descriptor to multifdsink to write to and
489 * specify the burst parameters for the new connection.
491 gst_multi_fd_sink_signals[SIGNAL_ADD_BURST] =
492 g_signal_new ("add-full", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
493 G_STRUCT_OFFSET (GstMultiFdSinkClass, add_full),
494 NULL, NULL, gst_tcp_marshal_VOID__INT_BOOLEAN_INT_UINT64_INT_UINT64,
495 G_TYPE_NONE, 6, G_TYPE_INT, G_TYPE_BOOLEAN, GST_TYPE_UNIT_TYPE,
496 G_TYPE_UINT64, GST_TYPE_UNIT_TYPE, G_TYPE_UINT64);
498 * GstMultiFdSink::remove:
499 * @gstmultifdsink: the multifdsink element to emit this signal on
500 * @fd: the file descriptor to remove from multifdsink
502 * Remove the given open file descriptor from multifdsink.
504 gst_multi_fd_sink_signals[SIGNAL_REMOVE] =
505 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
506 G_STRUCT_OFFSET (GstMultiFdSinkClass, remove),
507 NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
509 * GstMultiFdSink::clear:
510 * @gstmultifdsink: the multifdsink element to emit this signal on
512 * Remove all file descriptors from multifdsink. Since multifdsink did not
513 * open fd's itself, it does not explicitly close the fd. The application
514 * should do so by connecting to the client-fd-removed callback.
516 gst_multi_fd_sink_signals[SIGNAL_CLEAR] =
517 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
518 G_STRUCT_OFFSET (GstMultiFdSinkClass, clear),
519 NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
522 * GstMultiFdSink::get-stats:
523 * @gstmultifdsink: the multifdsink element to emit this signal on
524 * @fd: the file descriptor to get stats of from multifdsink
526 * Get statistics about @fd. This function returns a GValueArray to ease
527 * automatic wrapping for bindings.
529 * Returns: a GValueArray with the statistics. The array contains 5 guint64
530 * values that represent respectively total number of bytes sent, time
531 * when the client was added, time when the client was disconnected/removed,
532 * time the client is/was active, last activity time. All times are
533 * expressed in nanoseconds (GstClockTime).
535 gst_multi_fd_sink_signals[SIGNAL_GET_STATS] =
536 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
537 G_STRUCT_OFFSET (GstMultiFdSinkClass, get_stats),
538 NULL, NULL, gst_tcp_marshal_BOXED__INT, G_TYPE_VALUE_ARRAY, 1,
542 * GstMultiFdSink::client-added:
543 * @gstmultifdsink: the multifdsink element that emitted this signal
544 * @fd: the file descriptor that was added to multifdsink
546 * The given file descriptor was added to multifdsink. This signal will
547 * be emitted from the streaming thread so application should be prepared
550 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED] =
551 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
552 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass, client_added),
553 NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
555 * GstMultiFdSink::client-removed:
556 * @gstmultifdsink: the multifdsink element that emitted this signal
557 * @fd: the file descriptor that is to be removed from multifdsink
558 * @status: the reason why the client was removed
560 * The given file descriptor is about to be removed from multifdsink. This
561 * signal will be emitted from the streaming thread so applications should
562 * be prepared for that.
564 * @gstmultifdsink still holds a handle to @fd so it is possible to call
565 * the get-stats signal from this callback. For the same reason it is
566 * not safe to close() and reuse @fd in this callback.
568 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED] =
569 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
570 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass,
571 client_removed), NULL, NULL, gst_tcp_marshal_VOID__INT_BOXED,
572 G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CLIENT_STATUS);
574 * GstMultiFdSink::client-fd-removed:
575 * @gstmultifdsink: the multifdsink element that emitted this signal
576 * @fd: the file descriptor that was removed from multifdsink
578 * The given file descriptor was removed from multifdsink. This signal will
579 * be emitted from the streaming thread so applications should be prepared
582 * In this callback, @gstmultifdsink has removed all the information
583 * associated with @fd and it is therefore not possible to call get-stats
584 * with @fd. It is however safe to close() and reuse @fd in the callback.
588 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED] =
589 g_signal_new ("client-fd-removed", G_TYPE_FROM_CLASS (klass),
590 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass,
591 client_fd_removed), NULL, NULL, gst_tcp_marshal_VOID__INT,
592 G_TYPE_NONE, 1, G_TYPE_INT);
594 gstelement_class->change_state =
595 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_change_state);
597 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_render);
599 klass->add = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add);
600 klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add_full);
601 klass->remove = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove);
602 klass->clear = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_clear);
603 klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_get_stats);
605 GST_DEBUG_CATEGORY_INIT (multifdsink_debug, "multifdsink", 0, "FD sink");
609 gst_multi_fd_sink_init (GstMultiFdSink * this, GstMultiFdSinkClass * klass)
611 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_FD_SINK_OPEN);
613 this->protocol = DEFAULT_PROTOCOL;
614 this->mode = DEFAULT_MODE;
616 CLIENTS_LOCK_INIT (this);
617 this->clients = NULL;
618 this->fd_hash = g_hash_table_new (g_int_hash, g_int_equal);
620 this->bufqueue = g_array_new (FALSE, TRUE, sizeof (GstBuffer *));
621 this->unit_type = DEFAULT_UNIT_TYPE;
622 this->units_max = DEFAULT_UNITS_MAX;
623 this->units_soft_max = DEFAULT_UNITS_SOFT_MAX;
624 this->time_min = DEFAULT_TIME_MIN;
625 this->bytes_min = DEFAULT_BYTES_MIN;
626 this->buffers_min = DEFAULT_BUFFERS_MIN;
627 this->recover_policy = DEFAULT_RECOVER_POLICY;
629 this->timeout = DEFAULT_TIMEOUT;
630 this->def_sync_method = DEFAULT_SYNC_METHOD;
631 this->def_burst_unit = DEFAULT_BURST_UNIT;
632 this->def_burst_value = DEFAULT_BURST_VALUE;
634 this->header_flags = 0;
638 gst_multi_fd_sink_finalize (GObject * object)
640 GstMultiFdSink *this;
642 this = GST_MULTI_FD_SINK (object);
644 CLIENTS_LOCK_FREE (this);
645 g_hash_table_destroy (this->fd_hash);
646 g_array_free (this->bufqueue, TRUE);
648 G_OBJECT_CLASS (parent_class)->finalize (object);
651 /* "add-full" signal implementation */
653 gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
654 GstSyncMethod sync_method, GstUnitType min_unit, guint64 min_value,
655 GstUnitType max_unit, guint64 max_value)
657 GstTCPClient *client;
663 GST_DEBUG_OBJECT (sink, "[fd %5d] adding client, sync_method %d, "
664 "min_unit %d, min_value %" G_GUINT64_FORMAT
665 ", max_unit %d, max_value %" G_GUINT64_FORMAT, fd, sync_method,
666 min_unit, min_value, max_unit, max_value);
668 /* do limits check if we can */
669 if (min_unit == max_unit) {
670 if (max_value != -1 && min_value != -1 && max_value < min_value)
674 /* create client datastructure */
675 client = g_new0 (GstTCPClient, 1);
677 client->status = GST_CLIENT_STATUS_OK;
679 client->bufoffset = 0;
680 client->sending = NULL;
681 client->bytes_sent = 0;
682 client->dropped_buffers = 0;
683 client->avg_queue_size = 0;
684 client->new_connection = TRUE;
685 client->burst_min_unit = min_unit;
686 client->burst_min_value = min_value;
687 client->burst_max_unit = max_unit;
688 client->burst_max_value = max_value;
689 client->sync_method = sync_method;
691 /* update start time */
692 g_get_current_time (&now);
693 client->connect_time = GST_TIMEVAL_TO_TIME (now);
694 client->disconnect_time = 0;
695 /* set last activity time to connect time */
696 client->last_activity_time = client->connect_time;
700 /* check the hash to find a duplicate fd */
701 clink = g_hash_table_lookup (sink->fd_hash, &client->fd.fd);
705 /* we can add the fd now */
706 clink = sink->clients = g_list_prepend (sink->clients, client);
707 g_hash_table_insert (sink->fd_hash, &client->fd.fd, clink);
709 /* set the socket to non blocking */
710 res = fcntl (fd, F_SETFL, O_NONBLOCK);
711 /* we always read from a client */
712 gst_fdset_add_fd (sink->fdset, &client->fd);
714 /* we don't try to read from write only fds */
715 flags = fcntl (fd, F_GETFL, 0);
716 if ((flags & O_ACCMODE) != O_WRONLY) {
717 gst_fdset_fd_ctl_read (sink->fdset, &client->fd, TRUE);
719 /* figure out the mode, can't use send() for non sockets */
720 res = fstat (fd, &statbuf);
721 if (S_ISSOCK (statbuf.st_mode)) {
722 client->is_socket = TRUE;
725 SEND_COMMAND (sink, CONTROL_RESTART);
727 CLIENTS_UNLOCK (sink);
729 g_signal_emit (G_OBJECT (sink),
730 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED], 0, fd);
737 GST_WARNING_OBJECT (sink,
738 "[fd %5d] wrong values min =%" G_GUINT64_FORMAT ", max=%"
739 G_GUINT64_FORMAT ", unit %d specified when adding client", fd,
740 min_value, max_value, min_unit);
745 client->status = GST_CLIENT_STATUS_DUPLICATE;
746 CLIENTS_UNLOCK (sink);
747 GST_WARNING_OBJECT (sink, "[fd %5d] duplicate client found, refusing", fd);
748 g_signal_emit (G_OBJECT (sink),
749 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0, fd,
756 /* "add" signal implemntation */
758 gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd)
760 gst_multi_fd_sink_add_full (sink, fd, sink->def_sync_method,
761 sink->def_burst_unit, sink->def_burst_value, sink->def_burst_unit, -1);
764 /* "remove" signal implemntation */
766 gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd)
770 GST_DEBUG_OBJECT (sink, "[fd %5d] removing client", fd);
773 clink = g_hash_table_lookup (sink->fd_hash, &fd);
775 GstTCPClient *client = (GstTCPClient *) clink->data;
777 client->status = GST_CLIENT_STATUS_REMOVED;
778 gst_multi_fd_sink_remove_client_link (sink, clink);
779 SEND_COMMAND (sink, CONTROL_RESTART);
781 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this fd found!", fd);
783 CLIENTS_UNLOCK (sink);
786 /* can be called both through the signal (ie from any thread) or when stopping,
787 * after the writing thread has shut down */
789 gst_multi_fd_sink_clear (GstMultiFdSink * sink)
791 GList *clients, *next;
793 GST_DEBUG_OBJECT (sink, "clearing all clients");
796 for (clients = sink->clients; clients; clients = next) {
797 GstTCPClient *client;
799 client = (GstTCPClient *) clients->data;
800 next = g_list_next (clients);
802 client->status = GST_CLIENT_STATUS_REMOVED;
803 gst_multi_fd_sink_remove_client_link (sink, clients);
805 SEND_COMMAND (sink, CONTROL_RESTART);
806 CLIENTS_UNLOCK (sink);
809 /* "get-stats" signal implemntation
810 * the array returned contains:
812 * guint64 : bytes_sent
813 * guint64 : connect time (in nanoseconds)
814 * guint64 : disconnect time (in nanoseconds)
815 * guint64 : time the client is/was connected (in nanoseconds)
816 * guint64 : last activity time (in nanoseconds)
819 gst_multi_fd_sink_get_stats (GstMultiFdSink * sink, int fd)
821 GstTCPClient *client;
822 GValueArray *result = NULL;
826 clink = g_hash_table_lookup (sink->fd_hash, &fd);
827 client = (GstTCPClient *) clink->data;
828 if (client != NULL) {
829 GValue value = { 0 };
832 result = g_value_array_new (5);
834 g_value_init (&value, G_TYPE_UINT64);
835 g_value_set_uint64 (&value, client->bytes_sent);
836 result = g_value_array_append (result, &value);
837 g_value_unset (&value);
838 g_value_init (&value, G_TYPE_UINT64);
839 g_value_set_uint64 (&value, client->connect_time);
840 result = g_value_array_append (result, &value);
841 g_value_unset (&value);
842 if (client->disconnect_time == 0) {
845 g_get_current_time (&nowtv);
847 interval = GST_TIMEVAL_TO_TIME (nowtv) - client->connect_time;
849 interval = client->disconnect_time - client->connect_time;
851 g_value_init (&value, G_TYPE_UINT64);
852 g_value_set_uint64 (&value, client->disconnect_time);
853 result = g_value_array_append (result, &value);
854 g_value_unset (&value);
855 g_value_init (&value, G_TYPE_UINT64);
856 g_value_set_uint64 (&value, interval);
857 result = g_value_array_append (result, &value);
858 g_value_unset (&value);
859 g_value_init (&value, G_TYPE_UINT64);
860 g_value_set_uint64 (&value, client->last_activity_time);
861 result = g_value_array_append (result, &value);
863 CLIENTS_UNLOCK (sink);
865 /* python doesn't like a NULL pointer yet */
866 if (result == NULL) {
867 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this found!", fd);
868 result = g_value_array_new (0);
874 /* should be called with the clientslock helt.
875 * Note that we don't close the fd as we didn't open it in the first
876 * place. An application should connect to the client-removed signal and
877 * close the fd itself.
880 gst_multi_fd_sink_remove_client_link (GstMultiFdSink * sink, GList * link)
884 GstTCPClient *client = (GstTCPClient *) link->data;
885 GstMultiFdSinkClass *fclass;
887 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
891 /* FIXME: if we keep track of ip we can log it here and signal */
892 switch (client->status) {
893 case GST_CLIENT_STATUS_OK:
894 GST_WARNING_OBJECT (sink, "[fd %5d] removing client %p for no reason",
897 case GST_CLIENT_STATUS_CLOSED:
898 GST_DEBUG_OBJECT (sink, "[fd %5d] removing client %p because of close",
901 case GST_CLIENT_STATUS_REMOVED:
902 GST_DEBUG_OBJECT (sink,
903 "[fd %5d] removing client %p because the app removed it", fd, client);
905 case GST_CLIENT_STATUS_SLOW:
906 GST_INFO_OBJECT (sink,
907 "[fd %5d] removing client %p because it was too slow", fd, client);
909 case GST_CLIENT_STATUS_ERROR:
910 GST_WARNING_OBJECT (sink,
911 "[fd %5d] removing client %p because of error", fd, client);
914 GST_WARNING_OBJECT (sink,
915 "[fd %5d] removing client %p with invalid reason", fd, client);
919 gst_fdset_remove_fd (sink->fdset, &client->fd);
921 g_get_current_time (&now);
922 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
924 /* free client buffers */
925 g_slist_foreach (client->sending, (GFunc) gst_mini_object_unref, NULL);
926 g_slist_free (client->sending);
927 client->sending = NULL;
930 gst_caps_unref (client->caps);
933 /* unlock the mutex before signaling because the signal handler
934 * might query some properties */
935 CLIENTS_UNLOCK (sink);
937 g_signal_emit (G_OBJECT (sink),
938 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0, fd, client->status);
940 /* lock again before we remove the client completely */
943 /* fd cannot be reused in the above signal callback so we can safely
944 * remove it from the hashtable here */
945 if (!g_hash_table_remove (sink->fd_hash, &client->fd.fd)) {
946 GST_WARNING_OBJECT (sink,
947 "[fd %5d] error removing client %p from hash", client->fd.fd, client);
949 /* after releasing the lock above, the link could be invalid, more
950 * precisely, the next and prev pointers could point to invalid list
951 * links. One optimisation could be to add a cookie to the linked list
952 * and take a shortcut when it did not change between unlocking and locking
953 * our mutex. For now we just walk the list again. */
954 sink->clients = g_list_remove (sink->clients, client);
957 fclass->removed (sink, client->fd.fd);
960 CLIENTS_UNLOCK (sink);
962 /* and the fd is really gone now */
963 g_signal_emit (G_OBJECT (sink),
964 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED], 0, fd);
969 /* handle a read on a client fd,
970 * which either indicates a close or should be ignored
971 * returns FALSE if some error occured or the client closed. */
973 gst_multi_fd_sink_handle_client_read (GstMultiFdSink * sink,
974 GstTCPClient * client)
981 if (ioctl (fd, FIONREAD, &avail) < 0)
984 GST_DEBUG_OBJECT (sink, "[fd %5d] select reports client read of %d bytes",
990 /* client sent close, so remove it */
991 GST_DEBUG_OBJECT (sink, "[fd %5d] client asked for close, removing", fd);
992 client->status = GST_CLIENT_STATUS_CLOSED;
994 } else if (avail < 0) {
995 GST_WARNING_OBJECT (sink, "[fd %5d] avail < 0, removing", fd);
996 client->status = GST_CLIENT_STATUS_ERROR;
1002 /* just Read 'n' Drop, could also just drop the client as it's not supposed
1003 * to write to us except for closing the socket, I guess it's because we
1004 * like to listen to our customers. */
1006 /* this is the maximum we can read */
1007 gint to_read = MIN (avail, 512);
1009 GST_DEBUG_OBJECT (sink, "[fd %5d] client wants us to read %d bytes",
1012 nread = read (fd, dummy, to_read);
1014 GST_WARNING_OBJECT (sink, "[fd %5d] could not read %d bytes: %s (%d)",
1015 fd, to_read, g_strerror (errno), errno);
1016 client->status = GST_CLIENT_STATUS_ERROR;
1019 } else if (nread == 0) {
1020 GST_WARNING_OBJECT (sink, "[fd %5d] 0 bytes in read, removing", fd);
1021 client->status = GST_CLIENT_STATUS_ERROR;
1034 GST_WARNING_OBJECT (sink, "[fd %5d] ioctl failed: %s (%d)",
1035 fd, g_strerror (errno), errno);
1036 client->status = GST_CLIENT_STATUS_ERROR;
1041 /* Queue raw data for this client, creating a new buffer.
1042 * This takes ownership of the data by
1043 * setting it as GST_BUFFER_MALLOCDATA() on the created buffer so
1044 * be sure to pass g_free()-able @data.
1047 gst_multi_fd_sink_client_queue_data (GstMultiFdSink * sink,
1048 GstTCPClient * client, gchar * data, gint len)
1052 buf = gst_buffer_new ();
1053 GST_BUFFER_DATA (buf) = (guint8 *) data;
1054 GST_BUFFER_MALLOCDATA (buf) = (guint8 *) data;
1055 GST_BUFFER_SIZE (buf) = len;
1057 GST_LOG_OBJECT (sink, "[fd %5d] queueing data of length %d",
1058 client->fd.fd, len);
1060 client->sending = g_slist_append (client->sending, buf);
1065 /* GDP-encode given caps and queue them for sending */
1067 gst_multi_fd_sink_client_queue_caps (GstMultiFdSink * sink,
1068 GstTCPClient * client, const GstCaps * caps)
1075 g_return_val_if_fail (caps != NULL, FALSE);
1077 string = gst_caps_to_string (caps);
1078 GST_DEBUG_OBJECT (sink, "[fd %5d] Queueing caps %s through GDP",
1079 client->fd.fd, string);
1082 if (!gst_dp_packet_from_caps (caps, sink->header_flags, &length, &header,
1084 GST_DEBUG_OBJECT (sink, "Could not create GDP packet from caps");
1087 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) header, length);
1089 length = gst_dp_header_payload_length (header);
1090 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) payload, length);
1096 is_sync_frame (GstMultiFdSink * sink, GstBuffer * buffer)
1098 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1100 } else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS)) {
1107 /* queue the given buffer for the given client, possibly adding the GDP
1108 * header if GDP is being used */
1110 gst_multi_fd_sink_client_queue_buffer (GstMultiFdSink * sink,
1111 GstTCPClient * client, GstBuffer * buffer)
1115 /* TRUE: send them if the new caps have them */
1116 gboolean send_streamheader = FALSE;
1119 /* before we queue the buffer, we check if we need to queue streamheader
1120 * buffers (because it's a new client, or because they changed) */
1121 caps = gst_buffer_get_caps (buffer); /* cleaned up after streamheader */
1122 if (!client->caps) {
1123 GST_LOG_OBJECT (sink,
1124 "[fd %5d] no previous caps for this client, send streamheader",
1126 send_streamheader = TRUE;
1127 client->caps = gst_caps_ref (caps);
1129 /* there were previous caps recorded, so compare */
1130 if (!gst_caps_is_equal (caps, client->caps)) {
1131 const GValue *sh1, *sh2;
1133 /* caps are not equal, but could still have the same streamheader */
1134 s = gst_caps_get_structure (caps, 0);
1135 if (!gst_structure_has_field (s, "streamheader")) {
1136 /* no new streamheader, so nothing new to send */
1137 GST_LOG_OBJECT (sink,
1138 "[fd %5d] new caps do not have streamheader, not sending",
1141 /* there is a new streamheader */
1142 s = gst_caps_get_structure (client->caps, 0);
1143 if (!gst_structure_has_field (s, "streamheader")) {
1144 /* no previous streamheader, so send the new one */
1145 GST_LOG_OBJECT (sink,
1146 "[fd %5d] previous caps did not have streamheader, sending",
1148 send_streamheader = TRUE;
1150 /* both old and new caps have streamheader set */
1151 sh1 = gst_structure_get_value (s, "streamheader");
1152 s = gst_caps_get_structure (caps, 0);
1153 sh2 = gst_structure_get_value (s, "streamheader");
1154 if (gst_value_compare (sh1, sh2) != GST_VALUE_EQUAL) {
1155 GST_LOG_OBJECT (sink,
1156 "[fd %5d] new streamheader different from old, sending",
1158 send_streamheader = TRUE;
1165 if (G_UNLIKELY (send_streamheader)) {
1170 GST_LOG_OBJECT (sink,
1171 "[fd %5d] sending streamheader from caps %" GST_PTR_FORMAT,
1172 client->fd.fd, caps);
1173 s = gst_caps_get_structure (caps, 0);
1174 if (!gst_structure_has_field (s, "streamheader")) {
1175 GST_LOG_OBJECT (sink,
1176 "[fd %5d] no new streamheader, so nothing to send", client->fd.fd);
1178 GST_LOG_OBJECT (sink,
1179 "[fd %5d] sending streamheader from caps %" GST_PTR_FORMAT,
1180 client->fd.fd, caps);
1181 sh = gst_structure_get_value (s, "streamheader");
1182 g_assert (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
1183 buffers = g_value_peek_pointer (sh);
1184 for (i = 0; i < buffers->len; ++i) {
1188 bufval = &g_array_index (buffers, GValue, i);
1189 g_assert (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER);
1190 buffer = g_value_peek_pointer (bufval);
1191 GST_LOG_OBJECT (sink,
1192 "[fd %5d] queueing streamheader buffer of length %d",
1193 client->fd.fd, GST_BUFFER_SIZE (buffer));
1194 gst_buffer_ref (buffer);
1196 if (sink->protocol == GST_TCP_PROTOCOL_GDP) {
1200 if (!gst_dp_header_from_buffer (buffer, sink->header_flags, &len,
1202 GST_DEBUG_OBJECT (sink,
1203 "[fd %5d] could not create header, removing client",
1207 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) header,
1211 client->sending = g_slist_append (client->sending, buffer);
1216 gst_caps_unref (caps);
1218 /* now we can send the buffer, possibly sending a GDP header first */
1219 if (sink->protocol == GST_TCP_PROTOCOL_GDP) {
1223 if (!gst_dp_header_from_buffer (buffer, sink->header_flags, &len, &header)) {
1224 GST_DEBUG_OBJECT (sink,
1225 "[fd %5d] could not create header, removing client", client->fd.fd);
1228 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) header, len);
1231 GST_LOG_OBJECT (sink, "[fd %5d] queueing buffer of length %d",
1232 client->fd.fd, GST_BUFFER_SIZE (buffer));
1234 gst_buffer_ref (buffer);
1235 client->sending = g_slist_append (client->sending, buffer);
1240 /* find the keyframe in the list of buffers starting the
1241 * search from @idx. @direction as -1 will search backwards,
1242 * 1 will search forwards.
1243 * Returns: the index or -1 if there is no keyframe after idx.
1246 find_syncframe (GstMultiFdSink * sink, gint idx, gint direction)
1248 gint i, len, result;
1250 /* take length of queued buffers */
1251 len = sink->bufqueue->len;
1253 /* assume we don't find a keyframe */
1256 /* then loop over all buffers to find the first keyframe */
1257 for (i = idx; i >= 0 && i < len; i += direction) {
1260 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1261 if (is_sync_frame (sink, buf)) {
1262 GST_LOG_OBJECT (sink, "found keyframe at %d from %d, direction %d",
1271 #define find_next_syncframe(s,i) find_syncframe(s,i,1)
1272 #define find_prev_syncframe(s,i) find_syncframe(s,i,-1)
1274 /* Get the number of buffers from the buffer queue needed to satisfy
1275 * the maximum max in the configured units.
1276 * If units are not BUFFERS, and there are insufficient buffers in the
1277 * queue to satify the limit, return len(queue) + 1 */
1279 get_buffers_max (GstMultiFdSink * sink, gint64 max)
1281 switch (sink->unit_type) {
1282 case GST_UNIT_TYPE_BUFFERS:
1284 case GST_UNIT_TYPE_TIME:
1290 GstClockTime first = -1;
1292 len = sink->bufqueue->len;
1294 for (i = 0; i < len; i++) {
1295 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1296 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1298 first = GST_BUFFER_TIMESTAMP (buf);
1300 diff = first - GST_BUFFER_TIMESTAMP (buf);
1308 case GST_UNIT_TYPE_BYTES:
1315 len = sink->bufqueue->len;
1317 for (i = 0; i < len; i++) {
1318 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1319 acc += GST_BUFFER_SIZE (buf);
1331 /* find the positions in the buffer queue where *_min and *_max
1334 /* count the amount of data in the buffers and return the index
1335 * that satifies the given limits.
1337 * Returns: index @idx in the buffer queue so that the given limits are
1338 * satisfied. TRUE if all the limits could be satisfied, FALSE if not
1339 * enough data was in the queue.
1341 * FIXME, this code might now work if any of the units is in buffers...
1344 find_limits (GstMultiFdSink * sink,
1345 gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
1346 gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max)
1348 GstClockTime first, time;
1350 gboolean result, max_hit;
1352 /* take length of queue */
1353 len = sink->bufqueue->len;
1355 /* this must hold */
1358 GST_LOG_OBJECT (sink,
1359 "bytes_min %d, buffers_min %d, time_min %" GST_TIME_FORMAT
1360 ", bytes_max %d, buffers_max %d, time_max %" GST_TIME_FORMAT, bytes_min,
1361 buffers_min, GST_TIME_ARGS (time_min), bytes_max, buffers_max,
1362 GST_TIME_ARGS (time_max));
1364 /* do the trivial buffer limit test */
1365 if (buffers_min != -1 && len < buffers_min) {
1372 /* else count bytes and time */
1381 /* loop through the buffers, when a limit is ok, mark it
1382 * as -1, we have at least one buffer in the queue. */
1386 /* if we checked all min limits, update result */
1387 if (bytes_min == -1 && time_min == -1 && *min_idx == -1) {
1388 /* don't go below 0 */
1389 *min_idx = MAX (i - 1, 0);
1391 /* if we reached one max limit break out */
1393 /* i > 0 when we get here, we subtract one to get the position
1394 * of the previous buffer. */
1396 /* we have valid complete result if we found a min_idx too */
1397 result = *min_idx != -1;
1400 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1402 bytes += GST_BUFFER_SIZE (buf);
1404 /* take timestamp and save for the base first timestamp */
1405 if ((time = GST_BUFFER_TIMESTAMP (buf)) != -1) {
1406 GST_DEBUG_OBJECT (sink, "Ts %lld on buffer", time);
1410 /* increase max usage if we did not fill enough. Note that
1411 * buffers are sorted from new to old, so the first timestamp is
1412 * bigger than the next one. */
1413 if (time_min != -1 && first - time >= time_min)
1415 if (time_max != -1 && first - time >= time_max)
1418 GST_DEBUG_OBJECT (sink, "No timestamp on buffer");
1420 /* time is OK or unknown, check and increase if not enough bytes */
1421 if (bytes_min != -1) {
1422 if (bytes >= bytes_min)
1425 if (bytes_max != -1) {
1426 if (bytes >= bytes_max) {
1434 /* if we did not hit the max or min limit, set to buffer size */
1437 /* make sure min does not exceed max */
1439 *min_idx = *max_idx;
1444 /* parse the unit/value pair and assign it to the result value of the
1445 * right type, leave the other values untouched
1447 * Returns: FALSE if the unit is unknown or undefined. TRUE otherwise.
1450 assign_value (GstUnitType unit, guint64 value, gint * bytes, gint * buffers,
1451 GstClockTime * time)
1453 gboolean res = TRUE;
1455 /* set only the limit of the given format to the given value */
1457 case GST_UNIT_TYPE_BUFFERS:
1458 *buffers = (gint) value;
1460 case GST_UNIT_TYPE_TIME:
1463 case GST_UNIT_TYPE_BYTES:
1464 *bytes = (gint) value;
1466 case GST_UNIT_TYPE_UNDEFINED:
1474 /* count the index in the buffer queue to satisfy the given unit
1475 * and value pair starting from buffer at index 0.
1477 * Returns: TRUE if there was enough data in the queue to satisfy the
1478 * burst values. @idx contains the index in the buffer that contains enough
1479 * data to satisfy the limits or the last buffer in the queue when the
1480 * function returns FALSE.
1483 count_burst_unit (GstMultiFdSink * sink, gint * min_idx, GstUnitType min_unit,
1484 guint64 min_value, gint * max_idx, GstUnitType max_unit, guint64 max_value)
1486 gint bytes_min = -1, buffers_min = -1;
1487 gint bytes_max = -1, buffers_max = -1;
1488 GstClockTime time_min = -1, time_max = -1;
1490 assign_value (min_unit, min_value, &bytes_min, &buffers_min, &time_min);
1491 assign_value (max_unit, max_value, &bytes_max, &buffers_max, &time_max);
1493 return find_limits (sink, min_idx, bytes_min, buffers_min, time_min,
1494 max_idx, bytes_max, buffers_max, time_max);
1497 /* decide where in the current buffer queue this new client should start
1498 * receiving buffers from.
1499 * This function is called whenever a client is connected and has not yet
1500 * received a buffer.
1501 * If this returns -1, it means that we haven't found a good point to
1502 * start streaming from yet, and this function should be called again later
1503 * when more buffers have arrived.
1506 gst_multi_fd_sink_new_client (GstMultiFdSink * sink, GstTCPClient * client)
1510 GST_DEBUG_OBJECT (sink,
1511 "[fd %5d] new client, deciding where to start in queue", client->fd.fd);
1512 switch (client->sync_method) {
1513 case GST_SYNC_METHOD_LATEST:
1514 /* no syncing, we are happy with whatever the client is going to get */
1515 result = client->bufpos;
1516 GST_DEBUG_OBJECT (sink,
1517 "[fd %5d] SYNC_METHOD_LATEST, position %d", client->fd.fd, result);
1519 case GST_SYNC_METHOD_NEXT_KEYFRAME:
1521 /* if one of the new buffers (between client->bufpos and 0) in the queue
1522 * is a sync point, we can proceed, otherwise we need to keep waiting */
1523 GST_LOG_OBJECT (sink,
1524 "[fd %5d] new client, bufpos %d, waiting for keyframe", client->fd.fd,
1527 result = find_prev_syncframe (sink, client->bufpos);
1529 GST_DEBUG_OBJECT (sink,
1530 "[fd %5d] SYNC_METHOD_NEXT_KEYFRAME: result %d",
1531 client->fd.fd, result);
1535 /* client is not on a syncbuffer, need to skip these buffers and
1537 GST_LOG_OBJECT (sink,
1538 "[fd %5d] new client, skipping buffer(s), no syncpoint found",
1540 client->bufpos = -1;
1543 case GST_SYNC_METHOD_LATEST_KEYFRAME:
1545 GST_DEBUG_OBJECT (sink,
1546 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME", client->fd.fd);
1548 /* for new clients we initially scan the complete buffer queue for
1549 * a sync point when a buffer is added. If we don't find a keyframe,
1550 * we need to wait for the next keyframe and so we change the client's
1551 * sync method to GST_SYNC_METHOD_NEXT_KEYFRAME.
1553 result = find_next_syncframe (sink, 0);
1555 GST_DEBUG_OBJECT (sink,
1556 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME: result %d", client->fd.fd,
1561 GST_DEBUG_OBJECT (sink,
1562 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME: no keyframe found, "
1563 "switching to SYNC_METHOD_NEXT_KEYFRAME", client->fd.fd);
1564 /* throw client to the waiting state */
1565 client->bufpos = -1;
1566 /* and make client sync to next keyframe */
1567 client->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1570 case GST_SYNC_METHOD_BURST:
1575 /* move to the position where we satisfy the client's burst
1576 * parameters. If we could not satisfy the parameters because there
1577 * is not enough data, we just send what we have (which is in result).
1578 * We use the max value to limit the search
1580 ok = count_burst_unit (sink, &result, client->burst_min_unit,
1581 client->burst_min_value, &max, client->burst_max_unit,
1582 client->burst_max_value);
1583 GST_DEBUG_OBJECT (sink,
1584 "[fd %5d] SYNC_METHOD_BURST: burst_unit returned %d, result %d",
1585 client->fd.fd, ok, result);
1587 GST_LOG_OBJECT (sink, "min %d, max %d", result, max);
1589 /* we hit the max and it is below the min, use that then */
1590 if (max != -1 && max <= result) {
1591 result = MAX (max - 1, 0);
1592 GST_DEBUG_OBJECT (sink,
1593 "[fd %5d] SYNC_METHOD_BURST: result above max, taken down to %d",
1594 client->fd.fd, result);
1598 case GST_SYNC_METHOD_BURST_KEYFRAME:
1601 gint min_idx, max_idx;
1602 gint next_syncframe, prev_syncframe;
1606 * _always_ start sending a keyframe to the client. We first search
1607 * a keyframe between min/max limits. If there is none, we send it the
1608 * last keyframe before min. If there is none, the behaviour is like
1611 /* gather burst limits */
1612 ok = count_burst_unit (sink, &min_idx, client->burst_min_unit,
1613 client->burst_min_value, &max_idx, client->burst_max_unit,
1614 client->burst_max_value);
1616 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1618 /* first find a keyframe after min_idx */
1619 next_syncframe = find_next_syncframe (sink, min_idx);
1620 if (next_syncframe != -1 && next_syncframe < max_idx) {
1621 /* we have a valid keyframe and it's below the max */
1622 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1623 result = next_syncframe;
1627 /* no valid keyframe, try to find one below min */
1628 prev_syncframe = find_prev_syncframe (sink, min_idx);
1629 if (prev_syncframe != -1) {
1630 GST_WARNING_OBJECT (sink,
1631 "using keyframe below min in BURST_KEYFRAME sync mode");
1632 result = prev_syncframe;
1636 /* no prev keyframe or not enough data */
1637 GST_WARNING_OBJECT (sink,
1638 "no prev keyframe found in BURST_KEYFRAME sync mode, waiting for next");
1640 /* throw client to the waiting state */
1641 client->bufpos = -1;
1642 /* and make client sync to next keyframe */
1643 client->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1647 case GST_SYNC_METHOD_BURST_WITH_KEYFRAME:
1650 gint min_idx, max_idx;
1651 gint next_syncframe;
1653 /* BURST_WITH_KEYFRAME:
1655 * try to start sending a keyframe to the client. We first search
1656 * a keyframe between min/max limits. If there is none, we send it the
1657 * amount of data up 'till min.
1659 /* gather enough data to burst */
1660 ok = count_burst_unit (sink, &min_idx, client->burst_min_unit,
1661 client->burst_min_value, &max_idx, client->burst_max_unit,
1662 client->burst_max_value);
1664 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1666 /* first find a keyframe after min_idx */
1667 next_syncframe = find_next_syncframe (sink, min_idx);
1668 if (next_syncframe != -1 && next_syncframe < max_idx) {
1669 /* we have a valid keyframe and it's below the max */
1670 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1671 result = next_syncframe;
1675 /* no keyframe, send data from min_idx */
1676 GST_WARNING_OBJECT (sink, "using min in BURST_WITH_KEYFRAME sync mode");
1678 /* make sure we don't go over the max limit */
1679 if (max_idx != -1 && max_idx <= min_idx) {
1680 result = MAX (max_idx - 1, 0);
1688 g_warning ("unknown sync method %d", client->sync_method);
1689 result = client->bufpos;
1695 /* Handle a write on a client,
1696 * which indicates a read request from a client.
1698 * For each client we maintain a queue of GstBuffers that contain the raw bytes
1699 * we need to send to the client. In the case of the GDP protocol, we create
1700 * buffers out of the header bytes so that we can focus only on sending
1703 * We first check to see if we need to send caps (in GDP) and streamheaders.
1704 * If so, we queue them.
1706 * Then we run into the main loop that tries to send as many buffers as
1707 * possible. It will first exhaust the client->sending queue and if the queue
1708 * is empty, it will pick a buffer from the global queue.
1710 * Sending the buffers from the client->sending queue is basically writing
1711 * the bytes to the socket and maintaining a count of the bytes that were
1712 * sent. When the buffer is completely sent, it is removed from the
1713 * client->sending queue and we try to pick a new buffer for sending.
1715 * When the sending returns a partial buffer we stop sending more data as
1716 * the next send operation could block.
1718 * This functions returns FALSE if some error occured.
1721 gst_multi_fd_sink_handle_client_write (GstMultiFdSink * sink,
1722 GstTCPClient * client)
1724 int fd = client->fd.fd;
1730 g_get_current_time (&nowtv);
1731 now = GST_TIMEVAL_TO_TIME (nowtv);
1733 /* when using GDP, first check if we have queued caps yet */
1734 if (sink->protocol == GST_TCP_PROTOCOL_GDP) {
1735 if (!client->caps_sent) {
1739 peer = gst_pad_get_peer (GST_BASE_SINK_PAD (sink));
1741 GST_WARNING_OBJECT (sink, "pad has no peer");
1744 gst_object_unref (peer);
1746 caps = gst_pad_get_negotiated_caps (GST_BASE_SINK_PAD (sink));
1748 GST_WARNING_OBJECT (sink, "pad caps not yet negotiated");
1752 /* queue caps for sending */
1753 res = gst_multi_fd_sink_client_queue_caps (sink, client, caps);
1755 gst_caps_unref (caps);
1758 GST_DEBUG_OBJECT (sink, "Failed queueing caps, removing client");
1761 client->caps_sent = TRUE;
1769 if (!client->sending) {
1770 /* client is not working on a buffer */
1771 if (client->bufpos == -1) {
1772 /* client is too fast, remove from write queue until new buffer is
1774 gst_fdset_fd_ctl_write (sink->fdset, &client->fd, FALSE);
1777 /* client can pick a buffer from the global queue */
1780 /* for new connections, we need to find a good spot in the
1781 * bufqueue to start streaming from */
1782 if (client->new_connection) {
1783 gint position = gst_multi_fd_sink_new_client (sink, client);
1785 if (position >= 0) {
1786 /* we got a valid spot in the queue */
1787 client->new_connection = FALSE;
1788 client->bufpos = position;
1790 /* cannot send data to this client yet */
1791 gst_fdset_fd_ctl_write (sink->fdset, &client->fd, FALSE);
1797 buf = g_array_index (sink->bufqueue, GstBuffer *, client->bufpos);
1799 GST_LOG_OBJECT (sink, "[fd %5d] client %p at position %d",
1800 fd, client, client->bufpos);
1802 /* queueing a buffer will ref it */
1803 gst_multi_fd_sink_client_queue_buffer (sink, client, buf);
1805 /* need to start from the first byte for this new buffer */
1806 client->bufoffset = 0;
1810 /* see if we need to send something */
1811 if (client->sending) {
1815 /* pick first buffer from list */
1816 head = GST_BUFFER (client->sending->data);
1817 maxsize = GST_BUFFER_SIZE (head) - client->bufoffset;
1819 /* try to write the complete buffer */
1821 #define FLAGS MSG_NOSIGNAL
1825 if (client->is_socket) {
1827 send (fd, GST_BUFFER_DATA (head) + client->bufoffset, maxsize,
1830 wrote = write (fd, GST_BUFFER_DATA (head) + client->bufoffset, maxsize);
1835 if (errno == EAGAIN) {
1836 /* nothing serious, resource was unavailable, try again later */
1838 } else if (errno == ECONNRESET) {
1839 goto connection_reset;
1844 if (wrote < maxsize) {
1845 /* partial write means that the client cannot read more and we should
1846 * stop sending more */
1847 GST_LOG_OBJECT (sink, "partial write on %d of %d bytes", fd, wrote);
1848 client->bufoffset += wrote;
1851 /* complete buffer was written, we can proceed to the next one */
1852 client->sending = g_slist_remove (client->sending, head);
1853 gst_buffer_unref (head);
1854 /* make sure we start from byte 0 for the next buffer */
1855 client->bufoffset = 0;
1858 client->bytes_sent += wrote;
1859 client->last_activity_time = now;
1860 sink->bytes_served += wrote;
1870 GST_DEBUG_OBJECT (sink, "[fd %5d] connection reset by peer, removing", fd);
1871 client->status = GST_CLIENT_STATUS_CLOSED;
1876 GST_WARNING_OBJECT (sink,
1877 "[fd %5d] could not write, removing client: %s (%d)", fd,
1878 g_strerror (errno), errno);
1879 client->status = GST_CLIENT_STATUS_ERROR;
1884 /* calculate the new position for a client after recovery. This function
1885 * does not update the client position but merely returns the required
1889 gst_multi_fd_sink_recover_client (GstMultiFdSink * sink, GstTCPClient * client)
1893 GST_WARNING_OBJECT (sink,
1894 "[fd %5d] client %p is lagging at %d, recover using policy %d",
1895 client->fd.fd, client, client->bufpos, sink->recover_policy);
1897 switch (sink->recover_policy) {
1898 case GST_RECOVER_POLICY_NONE:
1899 /* do nothing, client will catch up or get kicked out when it reaches
1901 newbufpos = client->bufpos;
1903 case GST_RECOVER_POLICY_RESYNC_LATEST:
1904 /* move to beginning of queue */
1907 case GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT:
1908 /* move to beginning of soft max */
1909 newbufpos = get_buffers_max (sink, sink->units_soft_max);
1911 case GST_RECOVER_POLICY_RESYNC_KEYFRAME:
1912 /* find keyframe in buffers, we search backwards to find the
1913 * closest keyframe relative to what this client already received. */
1914 newbufpos = MIN (sink->bufqueue->len - 1,
1915 get_buffers_max (sink, sink->units_soft_max) - 1);
1917 while (newbufpos >= 0) {
1920 buf = g_array_index (sink->bufqueue, GstBuffer *, newbufpos);
1921 if (is_sync_frame (sink, buf)) {
1922 /* found a buffer that is not a delta unit */
1929 /* unknown recovery procedure */
1930 newbufpos = get_buffers_max (sink, sink->units_soft_max);
1936 /* Queue a buffer on the global queue.
1938 * This function adds the buffer to the front of a GArray. It removes the
1939 * tail buffer if the max queue size is exceeded, unreffing the queued buffer.
1940 * Note that unreffing the buffer is not a problem as clients who
1941 * started writing out this buffer will still have a reference to it in the
1942 * client->sending queue.
1944 * After adding the buffer, we update all client positions in the queue. If
1945 * a client moves over the soft max, we start the recovery procedure for this
1946 * slow client. If it goes over the hard max, it is put into the slow list
1949 * Special care is taken of clients that were waiting for a new buffer (they
1950 * had a position of -1) because they can proceed after adding this new buffer.
1951 * This is done by adding the client back into the write fd_set and signalling
1952 * the select thread that the fd_set changed.
1955 gst_multi_fd_sink_queue_buffer (GstMultiFdSink * sink, GstBuffer * buf)
1957 GList *clients, *next;
1959 gboolean need_signal = FALSE;
1960 gint max_buffer_usage;
1964 gint max_buffers, soft_max_buffers;
1966 g_get_current_time (&nowtv);
1967 now = GST_TIMEVAL_TO_TIME (nowtv);
1969 CLIENTS_LOCK (sink);
1970 /* add buffer to queue */
1971 g_array_prepend_val (sink->bufqueue, buf);
1972 queuelen = sink->bufqueue->len;
1974 if (sink->units_max > 0)
1975 max_buffers = get_buffers_max (sink, sink->units_max);
1979 if (sink->units_soft_max > 0)
1980 soft_max_buffers = get_buffers_max (sink, sink->units_soft_max);
1982 soft_max_buffers = -1;
1983 GST_LOG_OBJECT (sink, "Using max %d, softmax %d", max_buffers,
1986 /* then loop over the clients and update the positions */
1987 max_buffer_usage = 0;
1988 for (clients = sink->clients; clients; clients = next) {
1989 GstTCPClient *client;
1991 client = (GstTCPClient *) clients->data;
1992 next = g_list_next (clients);
1995 GST_LOG_OBJECT (sink, "[fd %5d] client %p at position %d",
1996 client->fd.fd, client, client->bufpos);
1997 /* check soft max if needed, recover client */
1998 if (soft_max_buffers > 0 && client->bufpos >= soft_max_buffers) {
2001 newpos = gst_multi_fd_sink_recover_client (sink, client);
2002 if (newpos != client->bufpos) {
2003 client->bufpos = newpos;
2004 client->discont = TRUE;
2005 GST_INFO_OBJECT (sink, "[fd %5d] client %p position reset to %d",
2006 client->fd.fd, client, client->bufpos);
2008 GST_INFO_OBJECT (sink,
2009 "[fd %5d] client %p not recovering position",
2010 client->fd.fd, client);
2013 /* check hard max and timeout, remove client */
2014 if ((max_buffers > 0 && client->bufpos >= max_buffers) ||
2016 && now - client->last_activity_time > sink->timeout)) {
2018 GST_WARNING_OBJECT (sink, "[fd %5d] client %p is too slow, removing",
2019 client->fd.fd, client);
2020 /* remove the client, the fd set will be cleared and the select thread
2021 * will be signaled */
2022 client->status = GST_CLIENT_STATUS_SLOW;
2023 gst_multi_fd_sink_remove_client_link (sink, clients);
2024 /* set client to invalid position while being removed */
2025 client->bufpos = -1;
2027 } else if (client->bufpos == 0 || client->new_connection) {
2028 /* can send data to this client now. need to signal the select thread that
2029 * the fd_set changed */
2030 gst_fdset_fd_ctl_write (sink->fdset, &client->fd, TRUE);
2033 /* keep track of maximum buffer usage */
2034 if (client->bufpos > max_buffer_usage) {
2035 max_buffer_usage = client->bufpos;
2039 /* make sure we respect bytes-min, buffers-min and time-min when they are set */
2043 GST_LOG_OBJECT (sink,
2044 "extending queue %d to respect time_min %" GST_TIME_FORMAT
2045 ", bytes_min %d, buffers_min %d", max_buffer_usage,
2046 GST_TIME_ARGS (sink->time_min), sink->bytes_min, sink->buffers_min);
2048 /* get index where the limits are ok, we don't really care if all limits
2049 * are ok, we just queue as much as we need. We also don't compare against
2050 * the max limits. */
2051 find_limits (sink, &usage, sink->bytes_min, sink->buffers_min,
2052 sink->time_min, &max, -1, -1, -1);
2054 max_buffer_usage = MAX (max_buffer_usage, usage + 1);
2055 GST_LOG_OBJECT (sink, "extended queue to %d", max_buffer_usage);
2058 /* now look for sync points and make sure there is at least one
2059 * sync point in the queue. We only do this if the LATEST_KEYFRAME or
2060 * BURST_KEYFRAME mode is selected */
2061 if (sink->def_sync_method == GST_SYNC_METHOD_LATEST_KEYFRAME ||
2062 sink->def_sync_method == GST_SYNC_METHOD_BURST_KEYFRAME) {
2063 /* no point in searching beyond the queue length */
2064 gint limit = queuelen;
2067 /* no point in searching beyond the soft-max if any. */
2068 if (soft_max_buffers) {
2069 limit = MIN (limit, soft_max_buffers);
2071 GST_LOG_OBJECT (sink, "extending queue to include sync point, now at %d",
2073 for (i = 0; i < limit; i++) {
2074 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
2075 if (is_sync_frame (sink, buf)) {
2076 /* found a sync frame, now extend the buffer usage to
2077 * include at least this frame. */
2078 max_buffer_usage = MAX (max_buffer_usage, i);
2082 GST_LOG_OBJECT (sink, "max buffer usage is now %d", max_buffer_usage);
2085 GST_LOG_OBJECT (sink, "len %d, usage %d", queuelen, max_buffer_usage);
2087 /* nobody is referencing units after max_buffer_usage so we can
2088 * remove them from the queue. We remove them in reverse order as
2089 * this is the most optimal for GArray. */
2090 for (i = queuelen - 1; i > max_buffer_usage; i--) {
2093 /* queue exceeded max size */
2095 old = g_array_index (sink->bufqueue, GstBuffer *, i);
2096 sink->bufqueue = g_array_remove_index (sink->bufqueue, i);
2098 /* unref tail buffer */
2099 gst_buffer_unref (old);
2101 /* save for stats */
2102 sink->buffers_queued = max_buffer_usage;
2103 CLIENTS_UNLOCK (sink);
2105 /* and send a signal to thread if fd_set changed */
2107 SEND_COMMAND (sink, CONTROL_RESTART);
2111 /* Handle the clients. Basically does a blocking select for one
2112 * of the client fds to become read or writable. We also have a
2113 * filedescriptor to receive commands on that we need to check.
2115 * After going out of the select call, we read and write to all
2116 * clients that can do so. Badly behaving clients are put on a
2117 * garbage list and removed.
2120 gst_multi_fd_sink_handle_clients (GstMultiFdSink * sink)
2123 GList *clients, *next;
2125 GstMultiFdSinkClass *fclass;
2127 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
2130 gboolean stop = FALSE;
2135 * - server socket input (ie, new client connections)
2136 * - client socket input (ie, clients saying goodbye)
2137 * - client socket output (ie, client reads) */
2138 GST_LOG_OBJECT (sink, "waiting on action on fdset");
2139 result = gst_fdset_wait (sink->fdset, -1);
2141 /* < 0 is an error, 0 just means a timeout happened, which is impossible */
2143 GST_WARNING_OBJECT (sink, "wait failed: %s (%d)", g_strerror (errno),
2145 if (errno == EBADF) {
2146 /* ok, so one or more of the fds is invalid. We loop over them to find
2147 * the ones that give an error to the F_GETFL fcntl. */
2148 CLIENTS_LOCK (sink);
2149 for (clients = sink->clients; clients; clients = next) {
2150 GstTCPClient *client;
2155 client = (GstTCPClient *) clients->data;
2156 next = g_list_next (clients);
2160 res = fcntl (fd, F_GETFL, &flags);
2162 GST_WARNING_OBJECT (sink, "fnctl failed for %d, removing: %s (%d)",
2163 fd, g_strerror (errno), errno);
2164 if (errno == EBADF) {
2165 client->status = GST_CLIENT_STATUS_ERROR;
2166 gst_multi_fd_sink_remove_client_link (sink, clients);
2170 CLIENTS_UNLOCK (sink);
2171 /* after this, go back in the select loop as the read/writefds
2174 } else if (errno == EINTR) {
2175 /* interrupted system call, just redo the select */
2178 /* this is quite bad... */
2179 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2180 ("select failed: %s (%d)", g_strerror (errno), errno));
2184 GST_LOG_OBJECT (sink, "wait done: %d sockets with events", result);
2185 /* read all commands */
2186 if (gst_fdset_fd_can_read (sink->fdset, &READ_SOCKET (sink))) {
2187 GST_LOG_OBJECT (sink, "have a command");
2192 READ_COMMAND (sink, command, res);
2194 GST_LOG_OBJECT (sink, "no more commands");
2195 /* no more commands */
2200 case CONTROL_RESTART:
2201 GST_LOG_OBJECT (sink, "restart");
2202 /* need to restart the select call as the fd_set changed */
2203 /* if other file descriptors than the READ_SOCKET had activity,
2204 * we don't restart just yet, but handle the other clients first
2210 /* break out of the select loop */
2211 GST_LOG_OBJECT (sink, "stop");
2212 /* stop this function */
2216 GST_WARNING_OBJECT (sink, "unkown");
2217 g_warning ("multifdsink: unknown control message received");
2226 } while (try_again);
2228 /* subclasses can check fdset with this virtual function */
2230 fclass->wait (sink, sink->fdset);
2232 /* Check the clients */
2233 CLIENTS_LOCK (sink);
2234 for (clients = sink->clients; clients; clients = next) {
2235 GstTCPClient *client;
2237 client = (GstTCPClient *) clients->data;
2238 next = g_list_next (clients);
2240 if (client->status != GST_CLIENT_STATUS_OK) {
2241 gst_multi_fd_sink_remove_client_link (sink, clients);
2245 if (gst_fdset_fd_has_closed (sink->fdset, &client->fd)) {
2246 client->status = GST_CLIENT_STATUS_CLOSED;
2247 gst_multi_fd_sink_remove_client_link (sink, clients);
2250 if (gst_fdset_fd_has_error (sink->fdset, &client->fd)) {
2251 GST_WARNING_OBJECT (sink, "gst_fdset_fd_has_error for %d", client->fd.fd);
2252 client->status = GST_CLIENT_STATUS_ERROR;
2253 gst_multi_fd_sink_remove_client_link (sink, clients);
2256 if (gst_fdset_fd_can_read (sink->fdset, &client->fd)) {
2257 /* handle client read */
2258 if (!gst_multi_fd_sink_handle_client_read (sink, client)) {
2259 gst_multi_fd_sink_remove_client_link (sink, clients);
2263 if (gst_fdset_fd_can_write (sink->fdset, &client->fd)) {
2264 /* handle client write */
2265 if (!gst_multi_fd_sink_handle_client_write (sink, client)) {
2266 gst_multi_fd_sink_remove_client_link (sink, clients);
2271 CLIENTS_UNLOCK (sink);
2274 /* we handle the client communication in another thread so that we do not block
2275 * the gstreamer thread while we select() on the client fds */
2277 gst_multi_fd_sink_thread (GstMultiFdSink * sink)
2279 while (sink->running) {
2280 gst_multi_fd_sink_handle_clients (sink);
2285 static GstFlowReturn
2286 gst_multi_fd_sink_render (GstBaseSink * bsink, GstBuffer * buf)
2288 GstMultiFdSink *sink;
2290 GstCaps *bufcaps, *padcaps;
2292 sink = GST_MULTI_FD_SINK (bsink);
2294 g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink, GST_MULTI_FD_SINK_OPEN),
2295 GST_FLOW_WRONG_STATE);
2297 /* since we check every buffer for streamheader caps, we need to make
2298 * sure every buffer has caps set */
2299 bufcaps = gst_buffer_get_caps (buf);
2300 padcaps = GST_PAD_CAPS (GST_BASE_SINK_PAD (bsink));
2302 /* make sure we have caps on the pad */
2303 if (!padcaps && !bufcaps)
2306 /* stamp the buffer with previous caps if no caps set */
2308 if (!gst_buffer_is_metadata_writable (buf)) {
2309 /* metadata is not writable, copy will be made and original buffer
2310 * will be unreffed so we need to ref so that we don't lose the
2311 * buffer in the render method. */
2312 gst_buffer_ref (buf);
2313 /* the new buffer is ours only, we keep it out of the scope of this
2315 buf = gst_buffer_make_metadata_writable (buf);
2317 /* else the metadata is writable, we ref because we keep the buffer
2318 * out of the scope of this method */
2319 gst_buffer_ref (buf);
2321 /* buffer metadata is writable now, set the caps */
2322 gst_buffer_set_caps (buf, padcaps);
2324 gst_caps_unref (bufcaps);
2326 /* since we keep this buffer out of the scope of this method */
2327 gst_buffer_ref (buf);
2330 in_caps = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
2332 GST_LOG_OBJECT (sink, "received buffer %p, in_caps: %d", buf, in_caps);
2334 /* if we get IN_CAPS buffers, but the previous buffer was not IN_CAPS,
2335 * it means we're getting new streamheader buffers, and we should clear
2337 if (in_caps && sink->previous_buffer_in_caps == FALSE) {
2338 GST_DEBUG_OBJECT (sink,
2339 "receiving new IN_CAPS buffers, clearing old streamheader");
2340 g_slist_foreach (sink->streamheader, (GFunc) gst_mini_object_unref, NULL);
2341 g_slist_free (sink->streamheader);
2342 sink->streamheader = NULL;
2345 /* save the current in_caps */
2346 sink->previous_buffer_in_caps = in_caps;
2348 /* if the incoming buffer is marked as IN CAPS, then we assume for now
2349 * it's a streamheader that needs to be sent to each new client, so we
2350 * put it on our internal list of streamheader buffers.
2351 * FIXME: we could check if the buffer's contents are in fact part of the
2352 * current streamheader.
2354 * We don't send the buffer to the client, since streamheaders are sent
2355 * separately when necessary. */
2357 GST_DEBUG_OBJECT (sink,
2358 "appending IN_CAPS buffer with length %d to streamheader",
2359 GST_BUFFER_SIZE (buf));
2360 sink->streamheader = g_slist_append (sink->streamheader, buf);
2362 /* queue the buffer, this is a regular data buffer. */
2363 gst_multi_fd_sink_queue_buffer (sink, buf);
2365 sink->bytes_to_serve += GST_BUFFER_SIZE (buf);
2372 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
2373 ("Received first buffer without caps set"));
2374 return GST_FLOW_NOT_NEGOTIATED;
2379 gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
2380 const GValue * value, GParamSpec * pspec)
2382 GstMultiFdSink *multifdsink;
2384 multifdsink = GST_MULTI_FD_SINK (object);
2388 multifdsink->protocol = g_value_get_enum (value);
2391 multifdsink->mode = g_value_get_enum (value);
2393 case PROP_BUFFERS_MAX:
2394 multifdsink->units_max = g_value_get_int (value);
2396 case PROP_BUFFERS_SOFT_MAX:
2397 multifdsink->units_soft_max = g_value_get_int (value);
2400 multifdsink->time_min = g_value_get_int64 (value);
2402 case PROP_BYTES_MIN:
2403 multifdsink->bytes_min = g_value_get_int (value);
2405 case PROP_BUFFERS_MIN:
2406 multifdsink->buffers_min = g_value_get_int (value);
2408 case PROP_UNIT_TYPE:
2409 multifdsink->unit_type = g_value_get_enum (value);
2411 case PROP_UNITS_MAX:
2412 multifdsink->units_max = g_value_get_int64 (value);
2414 case PROP_UNITS_SOFT_MAX:
2415 multifdsink->units_soft_max = g_value_get_int64 (value);
2417 case PROP_RECOVER_POLICY:
2418 multifdsink->recover_policy = g_value_get_enum (value);
2421 multifdsink->timeout = g_value_get_uint64 (value);
2423 case PROP_SYNC_METHOD:
2424 multifdsink->def_sync_method = g_value_get_enum (value);
2426 case PROP_BURST_UNIT:
2427 multifdsink->def_burst_unit = g_value_get_enum (value);
2429 case PROP_BURST_VALUE:
2430 multifdsink->def_burst_value = g_value_get_uint64 (value);
2434 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2440 gst_multi_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
2443 GstMultiFdSink *multifdsink;
2445 multifdsink = GST_MULTI_FD_SINK (object);
2449 g_value_set_enum (value, multifdsink->protocol);
2452 g_value_set_enum (value, multifdsink->mode);
2454 case PROP_BUFFERS_MAX:
2455 g_value_set_int (value, multifdsink->units_max);
2457 case PROP_BUFFERS_SOFT_MAX:
2458 g_value_set_int (value, multifdsink->units_soft_max);
2461 g_value_set_int64 (value, multifdsink->time_min);
2463 case PROP_BYTES_MIN:
2464 g_value_set_int (value, multifdsink->bytes_min);
2466 case PROP_BUFFERS_MIN:
2467 g_value_set_int (value, multifdsink->buffers_min);
2469 case PROP_BUFFERS_QUEUED:
2470 g_value_set_uint (value, multifdsink->buffers_queued);
2472 case PROP_BYTES_QUEUED:
2473 g_value_set_uint (value, multifdsink->bytes_queued);
2475 case PROP_TIME_QUEUED:
2476 g_value_set_uint64 (value, multifdsink->time_queued);
2478 case PROP_UNIT_TYPE:
2479 g_value_set_enum (value, multifdsink->unit_type);
2481 case PROP_UNITS_MAX:
2482 g_value_set_int64 (value, multifdsink->units_max);
2484 case PROP_UNITS_SOFT_MAX:
2485 g_value_set_int64 (value, multifdsink->units_soft_max);
2487 case PROP_RECOVER_POLICY:
2488 g_value_set_enum (value, multifdsink->recover_policy);
2491 g_value_set_uint64 (value, multifdsink->timeout);
2493 case PROP_SYNC_METHOD:
2494 g_value_set_enum (value, multifdsink->def_sync_method);
2496 case PROP_BYTES_TO_SERVE:
2497 g_value_set_uint64 (value, multifdsink->bytes_to_serve);
2499 case PROP_BYTES_SERVED:
2500 g_value_set_uint64 (value, multifdsink->bytes_served);
2502 case PROP_BURST_UNIT:
2503 g_value_set_enum (value, multifdsink->def_burst_unit);
2505 case PROP_BURST_VALUE:
2506 g_value_set_uint64 (value, multifdsink->def_burst_value);
2510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2516 /* create a socket for sending to remote machine */
2518 gst_multi_fd_sink_start (GstBaseSink * bsink)
2520 GstMultiFdSinkClass *fclass;
2521 int control_socket[2];
2522 GstMultiFdSink *this;
2524 if (GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_FD_SINK_OPEN))
2527 this = GST_MULTI_FD_SINK (bsink);
2528 fclass = GST_MULTI_FD_SINK_GET_CLASS (this);
2530 GST_INFO_OBJECT (this, "starting in mode %d", this->mode);
2531 this->fdset = gst_fdset_new (this->mode);
2533 if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_socket) < 0)
2536 READ_SOCKET (this).fd = control_socket[0];
2537 WRITE_SOCKET (this).fd = control_socket[1];
2539 gst_fdset_add_fd (this->fdset, &READ_SOCKET (this));
2540 gst_fdset_fd_ctl_read (this->fdset, &READ_SOCKET (this), TRUE);
2542 fcntl (READ_SOCKET (this).fd, F_SETFL, O_NONBLOCK);
2543 fcntl (WRITE_SOCKET (this).fd, F_SETFL, O_NONBLOCK);
2545 this->streamheader = NULL;
2546 this->bytes_to_serve = 0;
2547 this->bytes_served = 0;
2550 fclass->init (this);
2553 this->running = TRUE;
2554 this->thread = g_thread_create ((GThreadFunc) gst_multi_fd_sink_thread,
2557 GST_OBJECT_FLAG_SET (this, GST_MULTI_FD_SINK_OPEN);
2564 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ_WRITE, (NULL),
2571 multifdsink_hash_remove (gpointer key, gpointer value, gpointer data)
2577 gst_multi_fd_sink_stop (GstBaseSink * bsink)
2579 GstMultiFdSinkClass *fclass;
2580 GstMultiFdSink *this;
2584 this = GST_MULTI_FD_SINK (bsink);
2585 fclass = GST_MULTI_FD_SINK_GET_CLASS (this);
2587 if (!GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_FD_SINK_OPEN))
2590 this->running = FALSE;
2592 SEND_COMMAND (this, CONTROL_STOP);
2594 GST_DEBUG_OBJECT (this, "joining thread");
2595 g_thread_join (this->thread);
2596 GST_DEBUG_OBJECT (this, "joined thread");
2597 this->thread = NULL;
2600 /* free the clients */
2601 gst_multi_fd_sink_clear (this);
2603 close (READ_SOCKET (this).fd);
2604 close (WRITE_SOCKET (this).fd);
2606 if (this->streamheader) {
2607 g_slist_foreach (this->streamheader, (GFunc) gst_mini_object_unref, NULL);
2608 g_slist_free (this->streamheader);
2609 this->streamheader = NULL;
2613 fclass->close (this);
2616 gst_fdset_remove_fd (this->fdset, &READ_SOCKET (this));
2617 gst_fdset_free (this->fdset);
2620 g_hash_table_foreach_remove (this->fd_hash, multifdsink_hash_remove, this);
2622 /* remove all queued buffers */
2623 if (this->bufqueue) {
2624 GST_DEBUG_OBJECT (this, "Emptying bufqueue with %d buffers",
2625 this->bufqueue->len);
2626 for (i = this->bufqueue->len - 1; i >= 0; --i) {
2627 buf = g_array_index (this->bufqueue, GstBuffer *, i);
2628 GST_LOG_OBJECT (this, "Removing buffer %p (%d) with refcount %d", buf, i,
2629 GST_MINI_OBJECT_REFCOUNT (buf));
2630 gst_buffer_unref (buf);
2631 this->bufqueue = g_array_remove_index (this->bufqueue, i);
2633 /* freeing the array is done in _finalize */
2635 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_FD_SINK_OPEN);
2640 static GstStateChangeReturn
2641 gst_multi_fd_sink_change_state (GstElement * element, GstStateChange transition)
2643 GstMultiFdSink *sink;
2644 GstStateChangeReturn ret;
2646 sink = GST_MULTI_FD_SINK (element);
2648 /* we disallow changing the state from the streaming thread */
2649 if (g_thread_self () == sink->thread)
2650 return GST_STATE_CHANGE_FAILURE;
2653 switch (transition) {
2654 case GST_STATE_CHANGE_NULL_TO_READY:
2655 if (!gst_multi_fd_sink_start (GST_BASE_SINK (sink)))
2658 case GST_STATE_CHANGE_READY_TO_PAUSED:
2660 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2666 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2668 switch (transition) {
2669 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2671 case GST_STATE_CHANGE_PAUSED_TO_READY:
2673 case GST_STATE_CHANGE_READY_TO_NULL:
2674 gst_multi_fd_sink_stop (GST_BASE_SINK (sink));
2684 /* error message was posted */
2685 return GST_STATE_CHANGE_FAILURE;