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 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)
104 #include <gst/gst-i18n-plugin.h>
106 #include <sys/ioctl.h>
113 #include <sys/types.h>
114 #include <sys/socket.h>
115 #include <sys/stat.h>
116 #include <netinet/in.h>
118 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
119 #include <sys/filio.h>
122 #include "gstmultifdsink.h"
123 #include "gsttcp-marshal.h"
125 #define NOT_IMPLEMENTED 0
127 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
130 GST_STATIC_CAPS_ANY);
132 GST_DEBUG_CATEGORY_STATIC (multifdsink_debug);
133 #define GST_CAT_DEFAULT (multifdsink_debug)
135 /* MultiFdSink signals and args */
148 SIGNAL_CLIENT_REMOVED,
149 SIGNAL_CLIENT_FD_REMOVED,
155 /* this is really arbitrarily chosen */
156 #define DEFAULT_PROTOCOL GST_TCP_PROTOCOL_NONE
157 #define DEFAULT_MODE 1
158 #define DEFAULT_BUFFERS_MAX -1
159 #define DEFAULT_BUFFERS_SOFT_MAX -1
160 #define DEFAULT_TIME_MIN -1
161 #define DEFAULT_BYTES_MIN -1
162 #define DEFAULT_BUFFERS_MIN -1
163 #define DEFAULT_UNIT_TYPE GST_TCP_UNIT_TYPE_BUFFERS
164 #define DEFAULT_UNITS_MAX -1
165 #define DEFAULT_UNITS_SOFT_MAX -1
166 #define DEFAULT_RECOVER_POLICY GST_RECOVER_POLICY_NONE
167 #define DEFAULT_TIMEOUT 0
168 #define DEFAULT_SYNC_METHOD GST_SYNC_METHOD_LATEST
170 #define DEFAULT_BURST_UNIT GST_TCP_UNIT_TYPE_UNDEFINED
171 #define DEFAULT_BURST_VALUE 0
173 #define DEFAULT_QOS_DSCP -1
174 #define DEFAULT_HANDLE_READ TRUE
176 #define DEFAULT_RESEND_STREAMHEADER TRUE
192 PROP_BUFFERS_SOFT_MAX,
211 PROP_RESEND_STREAMHEADER,
218 /* For backward compat, we can't really select the poll mode anymore with
220 #define GST_TYPE_FDSET_MODE (gst_fdset_mode_get_type())
222 gst_fdset_mode_get_type (void)
224 static GType fdset_mode_type = 0;
225 static const GEnumValue fdset_mode[] = {
226 {0, "Select", "select"},
228 {2, "EPoll", "epoll"},
232 if (!fdset_mode_type) {
233 fdset_mode_type = g_enum_register_static ("GstFDSetMode", fdset_mode);
235 return fdset_mode_type;
238 #define GST_TYPE_RECOVER_POLICY (gst_recover_policy_get_type())
240 gst_recover_policy_get_type (void)
242 static GType recover_policy_type = 0;
243 static const GEnumValue recover_policy[] = {
244 {GST_RECOVER_POLICY_NONE,
245 "Do not try to recover", "none"},
246 {GST_RECOVER_POLICY_RESYNC_LATEST,
247 "Resync client to latest buffer", "latest"},
248 {GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT,
249 "Resync client to soft limit", "soft-limit"},
250 {GST_RECOVER_POLICY_RESYNC_KEYFRAME,
251 "Resync client to most recent keyframe", "keyframe"},
255 if (!recover_policy_type) {
256 recover_policy_type =
257 g_enum_register_static ("GstRecoverPolicy", recover_policy);
259 return recover_policy_type;
262 #define GST_TYPE_SYNC_METHOD (gst_sync_method_get_type())
264 gst_sync_method_get_type (void)
266 static GType sync_method_type = 0;
267 static const GEnumValue sync_method[] = {
268 {GST_SYNC_METHOD_LATEST,
269 "Serve starting from the latest buffer", "latest"},
270 {GST_SYNC_METHOD_NEXT_KEYFRAME,
271 "Serve starting from the next keyframe", "next-keyframe"},
272 {GST_SYNC_METHOD_LATEST_KEYFRAME,
273 "Serve everything since the latest keyframe (burst)",
275 {GST_SYNC_METHOD_BURST, "Serve burst-value data to client", "burst"},
276 {GST_SYNC_METHOD_BURST_KEYFRAME,
277 "Serve burst-value data starting on a keyframe",
279 {GST_SYNC_METHOD_BURST_WITH_KEYFRAME,
280 "Serve burst-value data preferably starting on a keyframe",
281 "burst-with-keyframe"},
285 if (!sync_method_type) {
286 sync_method_type = g_enum_register_static ("GstSyncMethod", sync_method);
288 return sync_method_type;
291 #define GST_TYPE_UNIT_TYPE (gst_unit_type_get_type())
293 gst_unit_type_get_type (void)
295 static GType unit_type_type = 0;
296 static const GEnumValue unit_type[] = {
297 {GST_TCP_UNIT_TYPE_UNDEFINED, "Undefined", "undefined"},
298 {GST_TCP_UNIT_TYPE_BUFFERS, "Buffers", "buffers"},
299 {GST_TCP_UNIT_TYPE_BYTES, "Bytes", "bytes"},
300 {GST_TCP_UNIT_TYPE_TIME, "Time", "time"},
304 if (!unit_type_type) {
305 unit_type_type = g_enum_register_static ("GstTCPUnitType", unit_type);
307 return unit_type_type;
310 #define GST_TYPE_CLIENT_STATUS (gst_client_status_get_type())
312 gst_client_status_get_type (void)
314 static GType client_status_type = 0;
315 static const GEnumValue client_status[] = {
316 {GST_CLIENT_STATUS_OK, "ok", "ok"},
317 {GST_CLIENT_STATUS_CLOSED, "Closed", "closed"},
318 {GST_CLIENT_STATUS_REMOVED, "Removed", "removed"},
319 {GST_CLIENT_STATUS_SLOW, "Too slow", "slow"},
320 {GST_CLIENT_STATUS_ERROR, "Error", "error"},
321 {GST_CLIENT_STATUS_DUPLICATE, "Duplicate", "duplicate"},
322 {GST_CLIENT_STATUS_FLUSHING, "Flushing", "flushing"},
326 if (!client_status_type) {
328 g_enum_register_static ("GstClientStatus", client_status);
330 return client_status_type;
333 static void gst_multi_fd_sink_finalize (GObject * object);
335 static void gst_multi_fd_sink_remove_client_link (GstMultiFdSink * sink,
338 static GstFlowReturn gst_multi_fd_sink_render (GstBaseSink * bsink,
340 static GstStateChangeReturn gst_multi_fd_sink_change_state (GstElement *
341 element, GstStateChange transition);
343 static void gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
344 const GValue * value, GParamSpec * pspec);
345 static void gst_multi_fd_sink_get_property (GObject * object, guint prop_id,
346 GValue * value, GParamSpec * pspec);
348 GST_BOILERPLATE (GstMultiFdSink, gst_multi_fd_sink, GstBaseSink,
351 static guint gst_multi_fd_sink_signals[LAST_SIGNAL] = { 0 };
354 gst_multi_fd_sink_base_init (gpointer g_class)
356 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
358 gst_element_class_add_pad_template (element_class,
359 gst_static_pad_template_get (&sinktemplate));
361 gst_element_class_set_details_simple (element_class,
362 "Multi filedescriptor sink", "Sink/Network",
363 "Send data to multiple filedescriptors",
364 "Thomas Vander Stichele <thomas at apestaart dot org>, "
365 "Wim Taymans <wim@fluendo.com>");
369 gst_multi_fd_sink_class_init (GstMultiFdSinkClass * klass)
371 GObjectClass *gobject_class;
372 GstElementClass *gstelement_class;
373 GstBaseSinkClass *gstbasesink_class;
375 gobject_class = (GObjectClass *) klass;
376 gstelement_class = (GstElementClass *) klass;
377 gstbasesink_class = (GstBaseSinkClass *) klass;
379 gobject_class->set_property = gst_multi_fd_sink_set_property;
380 gobject_class->get_property = gst_multi_fd_sink_get_property;
381 gobject_class->finalize = gst_multi_fd_sink_finalize;
383 g_object_class_install_property (gobject_class, PROP_PROTOCOL,
384 g_param_spec_enum ("protocol", "Protocol", "The protocol to wrap data in"
385 ". GDP protocol here is deprecated. Please use gdppay element.",
386 GST_TYPE_TCP_PROTOCOL, DEFAULT_PROTOCOL,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
390 * GstMultiFdSink::mode
392 * The mode for selecting activity on the fds.
394 * This property is deprecated since 0.10.18, if will now automatically
395 * select and use the most optimal method.
397 g_object_class_install_property (gobject_class, PROP_MODE,
398 g_param_spec_enum ("mode", "Mode",
399 "The mode for selecting activity on the fds (deprecated)",
400 GST_TYPE_FDSET_MODE, DEFAULT_MODE,
401 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 g_object_class_install_property (gobject_class, PROP_BUFFERS_MAX,
404 g_param_spec_int ("buffers-max", "Buffers max",
405 "max number of buffers to queue for a client (-1 = no limit)", -1,
406 G_MAXINT, DEFAULT_BUFFERS_MAX,
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 g_object_class_install_property (gobject_class, PROP_BUFFERS_SOFT_MAX,
409 g_param_spec_int ("buffers-soft-max", "Buffers soft max",
410 "Recover client when going over this limit (-1 = no limit)", -1,
411 G_MAXINT, DEFAULT_BUFFERS_SOFT_MAX,
412 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_BYTES_MIN,
415 g_param_spec_int ("bytes-min", "Bytes min",
416 "min number of bytes to queue (-1 = as little as possible)", -1,
417 G_MAXINT, DEFAULT_BYTES_MIN,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 g_object_class_install_property (gobject_class, PROP_TIME_MIN,
420 g_param_spec_int64 ("time-min", "Time min",
421 "min number of time to queue (-1 = as little as possible)", -1,
422 G_MAXINT64, DEFAULT_TIME_MIN,
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
424 g_object_class_install_property (gobject_class, PROP_BUFFERS_MIN,
425 g_param_spec_int ("buffers-min", "Buffers min",
426 "min number of buffers to queue (-1 = as few as possible)", -1,
427 G_MAXINT, DEFAULT_BUFFERS_MIN,
428 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class, PROP_UNIT_TYPE,
431 g_param_spec_enum ("unit-type", "Units type",
432 "The unit to measure the max/soft-max/queued properties",
433 GST_TYPE_UNIT_TYPE, DEFAULT_UNIT_TYPE,
434 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 g_object_class_install_property (gobject_class, PROP_UNITS_MAX,
436 g_param_spec_int64 ("units-max", "Units max",
437 "max number of units to queue (-1 = no limit)", -1, G_MAXINT64,
438 DEFAULT_UNITS_MAX, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 g_object_class_install_property (gobject_class, PROP_UNITS_SOFT_MAX,
440 g_param_spec_int64 ("units-soft-max", "Units soft max",
441 "Recover client when going over this limit (-1 = no limit)", -1,
442 G_MAXINT64, DEFAULT_UNITS_SOFT_MAX,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
445 g_object_class_install_property (gobject_class, PROP_BUFFERS_QUEUED,
446 g_param_spec_uint ("buffers-queued", "Buffers queued",
447 "Number of buffers currently queued", 0, G_MAXUINT, 0,
448 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
450 g_object_class_install_property (gobject_class, PROP_BYTES_QUEUED,
451 g_param_spec_uint ("bytes-queued", "Bytes queued",
452 "Number of bytes currently queued", 0, G_MAXUINT, 0,
453 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_TIME_QUEUED,
455 g_param_spec_uint64 ("time-queued", "Time queued",
456 "Number of time currently queued", 0, G_MAXUINT64, 0,
457 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
460 g_object_class_install_property (gobject_class, PROP_RECOVER_POLICY,
461 g_param_spec_enum ("recover-policy", "Recover Policy",
462 "How to recover when client reaches the soft max",
463 GST_TYPE_RECOVER_POLICY, DEFAULT_RECOVER_POLICY,
464 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
465 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
466 g_param_spec_uint64 ("timeout", "Timeout",
467 "Maximum inactivity timeout in nanoseconds for a client (0 = no limit)",
468 0, G_MAXUINT64, DEFAULT_TIMEOUT,
469 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 g_object_class_install_property (gobject_class, PROP_SYNC_METHOD,
471 g_param_spec_enum ("sync-method", "Sync Method",
472 "How to sync new clients to the stream", GST_TYPE_SYNC_METHOD,
473 DEFAULT_SYNC_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 g_object_class_install_property (gobject_class, PROP_BYTES_TO_SERVE,
475 g_param_spec_uint64 ("bytes-to-serve", "Bytes to serve",
476 "Number of bytes received to serve to clients", 0, G_MAXUINT64, 0,
477 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
478 g_object_class_install_property (gobject_class, PROP_BYTES_SERVED,
479 g_param_spec_uint64 ("bytes-served", "Bytes served",
480 "Total number of bytes send to all clients", 0, G_MAXUINT64, 0,
481 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
483 g_object_class_install_property (gobject_class, PROP_BURST_UNIT,
484 g_param_spec_enum ("burst-unit", "Burst unit",
485 "The format of the burst units (when sync-method is burst[[-with]-keyframe])",
486 GST_TYPE_UNIT_TYPE, DEFAULT_BURST_UNIT,
487 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
488 g_object_class_install_property (gobject_class, PROP_BURST_VALUE,
489 g_param_spec_uint64 ("burst-value", "Burst value",
490 "The amount of burst expressed in burst-unit", 0, G_MAXUINT64,
491 DEFAULT_BURST_VALUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 g_object_class_install_property (gobject_class, PROP_QOS_DSCP,
494 g_param_spec_int ("qos-dscp", "QoS diff srv code point",
495 "Quality of Service, differentiated services code point (-1 default)",
496 -1, 63, DEFAULT_QOS_DSCP,
497 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
499 * GstMultiFdSink::handle-read
501 * Handle read requests from clients and discard the data.
505 g_object_class_install_property (gobject_class, PROP_HANDLE_READ,
506 g_param_spec_boolean ("handle-read", "Handle Read",
507 "Handle client reads and discard the data",
508 DEFAULT_HANDLE_READ, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
510 * GstMultiFdSink::resend-streamheader
512 * Resend the streamheaders to existing clients when they change.
516 g_object_class_install_property (gobject_class, PROP_RESEND_STREAMHEADER,
517 g_param_spec_boolean ("resend-streamheader", "Resend streamheader",
518 "Resend the streamheader if it changes in the caps",
519 DEFAULT_RESEND_STREAMHEADER,
520 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
522 g_object_class_install_property (gobject_class, PROP_NUM_FDS,
523 g_param_spec_uint ("num-fds", "Number of fds",
524 "The current number of client file descriptors.",
525 0, G_MAXUINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
528 * GstMultiFdSink::add:
529 * @gstmultifdsink: the multifdsink element to emit this signal on
530 * @fd: the file descriptor to add to multifdsink
532 * Hand the given open file descriptor to multifdsink to write to.
534 gst_multi_fd_sink_signals[SIGNAL_ADD] =
535 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
536 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
537 add), NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1,
540 * GstMultiFdSink::add-full:
541 * @gstmultifdsink: the multifdsink element to emit this signal on
542 * @fd: the file descriptor to add to multifdsink
543 * @sync: the sync method to use
544 * @unit_type_min: the unit-type of @value_min
545 * @value_min: the minimum amount of data to burst expressed in
546 * @unit_type_min units.
547 * @unit_type_max: the unit-type of @value_max
548 * @value_max: the maximum amount of data to burst expressed in
549 * @unit_type_max units.
551 * Hand the given open file descriptor to multifdsink to write to and
552 * specify the burst parameters for the new connection.
554 gst_multi_fd_sink_signals[SIGNAL_ADD_BURST] =
555 g_signal_new ("add-full", G_TYPE_FROM_CLASS (klass),
556 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
557 add_full), NULL, NULL,
558 gst_tcp_marshal_VOID__INT_ENUM_INT_UINT64_INT_UINT64, G_TYPE_NONE, 6,
559 G_TYPE_INT, GST_TYPE_SYNC_METHOD, GST_TYPE_UNIT_TYPE, G_TYPE_UINT64,
560 GST_TYPE_UNIT_TYPE, G_TYPE_UINT64);
562 * GstMultiFdSink::remove:
563 * @gstmultifdsink: the multifdsink element to emit this signal on
564 * @fd: the file descriptor to remove from multifdsink
566 * Remove the given open file descriptor from multifdsink.
568 gst_multi_fd_sink_signals[SIGNAL_REMOVE] =
569 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
570 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
571 remove), NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE, 1,
574 * GstMultiFdSink::remove-flush:
575 * @gstmultifdsink: the multifdsink element to emit this signal on
576 * @fd: the file descriptor to remove from multifdsink
578 * Remove the given open file descriptor from multifdsink after flushing all
579 * the pending data to the fd.
581 gst_multi_fd_sink_signals[SIGNAL_REMOVE_FLUSH] =
582 g_signal_new ("remove-flush", G_TYPE_FROM_CLASS (klass),
583 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
584 remove_flush), NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE, 1,
587 * GstMultiFdSink::clear:
588 * @gstmultifdsink: the multifdsink element to emit this signal on
590 * Remove all file descriptors from multifdsink. Since multifdsink did not
591 * open fd's itself, it does not explicitly close the fd. The application
592 * should do so by connecting to the client-fd-removed callback.
594 gst_multi_fd_sink_signals[SIGNAL_CLEAR] =
595 g_signal_new ("clear", G_TYPE_FROM_CLASS (klass),
596 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
597 clear), NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
600 * GstMultiFdSink::get-stats:
601 * @gstmultifdsink: the multifdsink element to emit this signal on
602 * @fd: the file descriptor to get stats of from multifdsink
604 * Get statistics about @fd. This function returns a GValueArray to ease
605 * automatic wrapping for bindings.
607 * Returns: a GValueArray with the statistics. The array contains guint64
608 * values that represent respectively: total number of bytes sent, time
609 * when the client was added, time when the client was
610 * disconnected/removed, time the client is/was active, last activity
611 * time (in epoch seconds), number of buffers dropped.
612 * All times are expressed in nanoseconds (GstClockTime).
613 * The array can be 0-length if the client was not found.
615 gst_multi_fd_sink_signals[SIGNAL_GET_STATS] =
616 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
617 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstMultiFdSinkClass,
618 get_stats), NULL, NULL, gst_tcp_marshal_BOXED__INT,
619 G_TYPE_VALUE_ARRAY, 1, G_TYPE_INT);
622 * GstMultiFdSink::client-added:
623 * @gstmultifdsink: the multifdsink element that emitted this signal
624 * @fd: the file descriptor that was added to multifdsink
626 * The given file descriptor was added to multifdsink. This signal will
627 * be emitted from the streaming thread so application should be prepared
630 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED] =
631 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
632 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass, client_added),
633 NULL, NULL, gst_tcp_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
635 * GstMultiFdSink::client-removed:
636 * @gstmultifdsink: the multifdsink element that emitted this signal
637 * @fd: the file descriptor that is to be removed from multifdsink
638 * @status: the reason why the client was removed
640 * The given file descriptor is about to be removed from multifdsink. This
641 * signal will be emitted from the streaming thread so applications should
642 * be prepared for that.
644 * @gstmultifdsink still holds a handle to @fd so it is possible to call
645 * the get-stats signal from this callback. For the same reason it is
646 * not safe to close() and reuse @fd in this callback.
648 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED] =
649 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
650 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass,
651 client_removed), NULL, NULL, gst_tcp_marshal_VOID__INT_BOXED,
652 G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CLIENT_STATUS);
654 * GstMultiFdSink::client-fd-removed:
655 * @gstmultifdsink: the multifdsink element that emitted this signal
656 * @fd: the file descriptor that was removed from multifdsink
658 * The given file descriptor was removed from multifdsink. This signal will
659 * be emitted from the streaming thread so applications should be prepared
662 * In this callback, @gstmultifdsink has removed all the information
663 * associated with @fd and it is therefore not possible to call get-stats
664 * with @fd. It is however safe to close() and reuse @fd in the callback.
668 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED] =
669 g_signal_new ("client-fd-removed", G_TYPE_FROM_CLASS (klass),
670 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstMultiFdSinkClass,
671 client_fd_removed), NULL, NULL, gst_tcp_marshal_VOID__INT,
672 G_TYPE_NONE, 1, G_TYPE_INT);
674 gstelement_class->change_state =
675 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_change_state);
677 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_render);
679 klass->add = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add);
680 klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add_full);
681 klass->remove = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove);
682 klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove_flush);
683 klass->clear = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_clear);
684 klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_get_stats);
686 GST_DEBUG_CATEGORY_INIT (multifdsink_debug, "multifdsink", 0, "FD sink");
690 gst_multi_fd_sink_init (GstMultiFdSink * this, GstMultiFdSinkClass * klass)
692 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_FD_SINK_OPEN);
694 this->protocol = DEFAULT_PROTOCOL;
695 this->mode = DEFAULT_MODE;
697 CLIENTS_LOCK_INIT (this);
698 this->clients = NULL;
699 this->fd_hash = g_hash_table_new (g_int_hash, g_int_equal);
701 this->bufqueue = g_array_new (FALSE, TRUE, sizeof (GstBuffer *));
702 this->unit_type = DEFAULT_UNIT_TYPE;
703 this->units_max = DEFAULT_UNITS_MAX;
704 this->units_soft_max = DEFAULT_UNITS_SOFT_MAX;
705 this->time_min = DEFAULT_TIME_MIN;
706 this->bytes_min = DEFAULT_BYTES_MIN;
707 this->buffers_min = DEFAULT_BUFFERS_MIN;
708 this->recover_policy = DEFAULT_RECOVER_POLICY;
710 this->timeout = DEFAULT_TIMEOUT;
711 this->def_sync_method = DEFAULT_SYNC_METHOD;
712 this->def_burst_unit = DEFAULT_BURST_UNIT;
713 this->def_burst_value = DEFAULT_BURST_VALUE;
715 this->qos_dscp = DEFAULT_QOS_DSCP;
716 this->handle_read = DEFAULT_HANDLE_READ;
718 this->resend_streamheader = DEFAULT_RESEND_STREAMHEADER;
720 this->header_flags = 0;
724 gst_multi_fd_sink_finalize (GObject * object)
726 GstMultiFdSink *this;
728 this = GST_MULTI_FD_SINK (object);
730 CLIENTS_LOCK_FREE (this);
731 g_hash_table_destroy (this->fd_hash);
732 g_array_free (this->bufqueue, TRUE);
734 G_OBJECT_CLASS (parent_class)->finalize (object);
738 setup_dscp_client (GstMultiFdSink * sink, GstTCPClient * client)
745 struct sockaddr_in6 sa_in6;
746 struct sockaddr_storage sa_stor;
748 socklen_t slen = sizeof (sa);
752 if (sink->qos_dscp < 0)
755 if ((ret = getsockname (client->fd.fd, &sa.sa, &slen)) < 0) {
756 GST_DEBUG_OBJECT (sink, "could not get sockname: %s", g_strerror (errno));
760 af = sa.sa.sa_family;
762 /* if this is an IPv4-mapped address then do IPv4 QoS */
763 if (af == AF_INET6) {
765 GST_DEBUG_OBJECT (sink, "check IP6 socket");
766 if (IN6_IS_ADDR_V4MAPPED (&(sa.sa_in6.sin6_addr))) {
767 GST_DEBUG_OBJECT (sink, "mapped to IPV4");
772 /* extract and shift 6 bits of the DSCP */
773 tos = (sink->qos_dscp & 0x3f) << 2;
777 ret = setsockopt (client->fd.fd, IPPROTO_IP, IP_TOS, &tos, sizeof (tos));
782 setsockopt (client->fd.fd, IPPROTO_IPV6, IPV6_TCLASS, &tos,
788 GST_ERROR_OBJECT (sink, "unsupported AF");
792 GST_DEBUG_OBJECT (sink, "could not set DSCP: %s", g_strerror (errno));
799 setup_dscp (GstMultiFdSink * sink)
801 GList *clients, *next;
804 for (clients = sink->clients; clients; clients = next) {
805 GstTCPClient *client;
807 client = (GstTCPClient *) clients->data;
808 next = g_list_next (clients);
810 setup_dscp_client (sink, client);
812 CLIENTS_UNLOCK (sink);
815 /* "add-full" signal implementation */
817 gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
818 GstSyncMethod sync_method, GstTCPUnitType min_unit, guint64 min_value,
819 GstTCPUnitType max_unit, guint64 max_value)
821 GstTCPClient *client;
827 GST_DEBUG_OBJECT (sink, "[fd %5d] adding client, sync_method %d, "
828 "min_unit %d, min_value %" G_GUINT64_FORMAT
829 ", max_unit %d, max_value %" G_GUINT64_FORMAT, fd, sync_method,
830 min_unit, min_value, max_unit, max_value);
832 /* do limits check if we can */
833 if (min_unit == max_unit) {
834 if (max_value != -1 && min_value != -1 && max_value < min_value)
838 /* create client datastructure */
839 client = g_new0 (GstTCPClient, 1);
841 client->status = GST_CLIENT_STATUS_OK;
843 client->flushcount = -1;
844 client->bufoffset = 0;
845 client->sending = NULL;
846 client->bytes_sent = 0;
847 client->dropped_buffers = 0;
848 client->avg_queue_size = 0;
849 client->first_buffer_ts = GST_CLOCK_TIME_NONE;
850 client->last_buffer_ts = GST_CLOCK_TIME_NONE;
851 client->new_connection = TRUE;
852 client->burst_min_unit = min_unit;
853 client->burst_min_value = min_value;
854 client->burst_max_unit = max_unit;
855 client->burst_max_value = max_value;
856 client->sync_method = sync_method;
857 client->currently_removing = FALSE;
859 /* update start time */
860 g_get_current_time (&now);
861 client->connect_time = GST_TIMEVAL_TO_TIME (now);
862 client->disconnect_time = 0;
863 /* set last activity time to connect time */
864 client->last_activity_time = client->connect_time;
868 /* check the hash to find a duplicate fd */
869 clink = g_hash_table_lookup (sink->fd_hash, &client->fd.fd);
873 /* we can add the fd now */
874 clink = sink->clients = g_list_prepend (sink->clients, client);
875 g_hash_table_insert (sink->fd_hash, &client->fd.fd, clink);
876 sink->clients_cookie++;
878 /* set the socket to non blocking */
879 if (fcntl (fd, F_SETFL, O_NONBLOCK) < 0) {
880 GST_ERROR_OBJECT (sink, "failed to make socket %d non-blocking: %s", fd,
884 /* we always read from a client */
885 gst_poll_add_fd (sink->fdset, &client->fd);
887 /* we don't try to read from write only fds */
888 if (sink->handle_read) {
889 flags = fcntl (fd, F_GETFL, 0);
890 if ((flags & O_ACCMODE) != O_WRONLY) {
891 gst_poll_fd_ctl_read (sink->fdset, &client->fd, TRUE);
894 /* figure out the mode, can't use send() for non sockets */
895 if (fstat (fd, &statbuf) == 0 && S_ISSOCK (statbuf.st_mode)) {
896 client->is_socket = TRUE;
897 setup_dscp_client (sink, client);
900 gst_poll_restart (sink->fdset);
902 CLIENTS_UNLOCK (sink);
904 g_signal_emit (G_OBJECT (sink),
905 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED], 0, fd);
912 GST_WARNING_OBJECT (sink,
913 "[fd %5d] wrong values min =%" G_GUINT64_FORMAT ", max=%"
914 G_GUINT64_FORMAT ", unit %d specified when adding client", fd,
915 min_value, max_value, min_unit);
920 client->status = GST_CLIENT_STATUS_DUPLICATE;
921 CLIENTS_UNLOCK (sink);
922 GST_WARNING_OBJECT (sink, "[fd %5d] duplicate client found, refusing", fd);
923 g_signal_emit (G_OBJECT (sink),
924 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0, fd,
931 /* "add" signal implemntation */
933 gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd)
935 gst_multi_fd_sink_add_full (sink, fd, sink->def_sync_method,
936 sink->def_burst_unit, sink->def_burst_value, sink->def_burst_unit, -1);
939 /* "remove" signal implementation */
941 gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd)
945 GST_DEBUG_OBJECT (sink, "[fd %5d] removing client", fd);
948 clink = g_hash_table_lookup (sink->fd_hash, &fd);
950 GstTCPClient *client = (GstTCPClient *) clink->data;
952 if (client->status != GST_CLIENT_STATUS_OK) {
953 GST_INFO_OBJECT (sink,
954 "[fd %5d] Client already disconnecting with status %d",
959 client->status = GST_CLIENT_STATUS_REMOVED;
960 gst_multi_fd_sink_remove_client_link (sink, clink);
961 gst_poll_restart (sink->fdset);
963 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this fd found!", fd);
967 CLIENTS_UNLOCK (sink);
970 /* "remove-flush" signal implementation */
972 gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd)
976 GST_DEBUG_OBJECT (sink, "[fd %5d] flushing client", fd);
979 clink = g_hash_table_lookup (sink->fd_hash, &fd);
981 GstTCPClient *client = (GstTCPClient *) clink->data;
983 if (client->status != GST_CLIENT_STATUS_OK) {
984 GST_INFO_OBJECT (sink,
985 "[fd %5d] Client already disconnecting with status %d",
990 /* take the position of the client as the number of buffers left to flush.
991 * If the client was at position -1, we flush 0 buffers, 0 == flush 1
993 client->flushcount = client->bufpos + 1;
994 /* mark client as flushing. We can not remove the client right away because
995 * it might have some buffers to flush in the ->sending queue. */
996 client->status = GST_CLIENT_STATUS_FLUSHING;
998 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this fd found!", fd);
1001 CLIENTS_UNLOCK (sink);
1004 /* can be called both through the signal (i.e. from any thread) or when
1005 * stopping, after the writing thread has shut down */
1007 gst_multi_fd_sink_clear (GstMultiFdSink * sink)
1009 GList *clients, *next;
1012 GST_DEBUG_OBJECT (sink, "clearing all clients");
1014 CLIENTS_LOCK (sink);
1016 cookie = sink->clients_cookie;
1017 for (clients = sink->clients; clients; clients = next) {
1018 GstTCPClient *client;
1020 if (cookie != sink->clients_cookie) {
1021 GST_DEBUG_OBJECT (sink, "cookie changed while removing all clients");
1025 client = (GstTCPClient *) clients->data;
1026 next = g_list_next (clients);
1028 client->status = GST_CLIENT_STATUS_REMOVED;
1029 gst_multi_fd_sink_remove_client_link (sink, clients);
1031 gst_poll_restart (sink->fdset);
1032 CLIENTS_UNLOCK (sink);
1035 /* "get-stats" signal implementation
1036 * the array returned contains:
1038 * guint64 : bytes_sent
1039 * guint64 : connect time (in nanoseconds, since Epoch)
1040 * guint64 : disconnect time (in nanoseconds, since Epoch)
1041 * guint64 : time the client is/was connected (in nanoseconds)
1042 * guint64 : last activity time (in nanoseconds, since Epoch)
1043 * guint64 : buffers dropped due to recovery
1044 * guint64 : timestamp of the first buffer sent (in nanoseconds)
1045 * guint64 : timestamp of the last buffer sent (in nanoseconds)
1048 gst_multi_fd_sink_get_stats (GstMultiFdSink * sink, int fd)
1050 GstTCPClient *client;
1051 GValueArray *result = NULL;
1054 CLIENTS_LOCK (sink);
1055 clink = g_hash_table_lookup (sink->fd_hash, &fd);
1059 client = (GstTCPClient *) clink->data;
1060 if (client != NULL) {
1061 GValue value = { 0 };
1064 result = g_value_array_new (7);
1066 g_value_init (&value, G_TYPE_UINT64);
1067 g_value_set_uint64 (&value, client->bytes_sent);
1068 result = g_value_array_append (result, &value);
1069 g_value_unset (&value);
1070 g_value_init (&value, G_TYPE_UINT64);
1071 g_value_set_uint64 (&value, client->connect_time);
1072 result = g_value_array_append (result, &value);
1073 g_value_unset (&value);
1074 if (client->disconnect_time == 0) {
1077 g_get_current_time (&nowtv);
1079 interval = GST_TIMEVAL_TO_TIME (nowtv) - client->connect_time;
1081 interval = client->disconnect_time - client->connect_time;
1083 g_value_init (&value, G_TYPE_UINT64);
1084 g_value_set_uint64 (&value, client->disconnect_time);
1085 result = g_value_array_append (result, &value);
1086 g_value_unset (&value);
1087 g_value_init (&value, G_TYPE_UINT64);
1088 g_value_set_uint64 (&value, interval);
1089 result = g_value_array_append (result, &value);
1090 g_value_unset (&value);
1091 g_value_init (&value, G_TYPE_UINT64);
1092 g_value_set_uint64 (&value, client->last_activity_time);
1093 result = g_value_array_append (result, &value);
1094 g_value_unset (&value);
1095 g_value_init (&value, G_TYPE_UINT64);
1096 g_value_set_uint64 (&value, client->dropped_buffers);
1097 result = g_value_array_append (result, &value);
1098 g_value_unset (&value);
1099 g_value_init (&value, G_TYPE_UINT64);
1100 g_value_set_uint64 (&value, client->first_buffer_ts);
1101 result = g_value_array_append (result, &value);
1102 g_value_unset (&value);
1103 g_value_init (&value, G_TYPE_UINT64);
1104 g_value_set_uint64 (&value, client->last_buffer_ts);
1105 result = g_value_array_append (result, &value);
1109 CLIENTS_UNLOCK (sink);
1111 /* python doesn't like a NULL pointer yet */
1112 if (result == NULL) {
1113 GST_WARNING_OBJECT (sink, "[fd %5d] no client with this found!", fd);
1114 result = g_value_array_new (0);
1120 /* should be called with the clientslock helt.
1121 * Note that we don't close the fd as we didn't open it in the first
1122 * place. An application should connect to the client-fd-removed signal and
1123 * close the fd itself.
1126 gst_multi_fd_sink_remove_client_link (GstMultiFdSink * sink, GList * link)
1130 GstTCPClient *client = (GstTCPClient *) link->data;
1131 GstMultiFdSinkClass *fclass;
1133 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
1137 if (client->currently_removing) {
1138 GST_WARNING_OBJECT (sink, "[fd %5d] client is already being removed", fd);
1141 client->currently_removing = TRUE;
1144 /* FIXME: if we keep track of ip we can log it here and signal */
1145 switch (client->status) {
1146 case GST_CLIENT_STATUS_OK:
1147 GST_WARNING_OBJECT (sink, "[fd %5d] removing client %p for no reason",
1150 case GST_CLIENT_STATUS_CLOSED:
1151 GST_DEBUG_OBJECT (sink, "[fd %5d] removing client %p because of close",
1154 case GST_CLIENT_STATUS_REMOVED:
1155 GST_DEBUG_OBJECT (sink,
1156 "[fd %5d] removing client %p because the app removed it", fd, client);
1158 case GST_CLIENT_STATUS_SLOW:
1159 GST_INFO_OBJECT (sink,
1160 "[fd %5d] removing client %p because it was too slow", fd, client);
1162 case GST_CLIENT_STATUS_ERROR:
1163 GST_WARNING_OBJECT (sink,
1164 "[fd %5d] removing client %p because of error", fd, client);
1166 case GST_CLIENT_STATUS_FLUSHING:
1168 GST_WARNING_OBJECT (sink,
1169 "[fd %5d] removing client %p with invalid reason %d", fd, client,
1174 gst_poll_remove_fd (sink->fdset, &client->fd);
1176 g_get_current_time (&now);
1177 client->disconnect_time = GST_TIMEVAL_TO_TIME (now);
1179 /* free client buffers */
1180 g_slist_foreach (client->sending, (GFunc) gst_mini_object_unref, NULL);
1181 g_slist_free (client->sending);
1182 client->sending = NULL;
1185 gst_caps_unref (client->caps);
1186 client->caps = NULL;
1188 /* unlock the mutex before signaling because the signal handler
1189 * might query some properties */
1190 CLIENTS_UNLOCK (sink);
1192 g_signal_emit (G_OBJECT (sink),
1193 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0, fd, client->status);
1195 /* lock again before we remove the client completely */
1196 CLIENTS_LOCK (sink);
1198 /* fd cannot be reused in the above signal callback so we can safely
1199 * remove it from the hashtable here */
1200 if (!g_hash_table_remove (sink->fd_hash, &client->fd.fd)) {
1201 GST_WARNING_OBJECT (sink,
1202 "[fd %5d] error removing client %p from hash", client->fd.fd, client);
1204 /* after releasing the lock above, the link could be invalid, more
1205 * precisely, the next and prev pointers could point to invalid list
1206 * links. One optimisation could be to add a cookie to the linked list
1207 * and take a shortcut when it did not change between unlocking and locking
1208 * our mutex. For now we just walk the list again. */
1209 sink->clients = g_list_remove (sink->clients, client);
1210 sink->clients_cookie++;
1212 if (fclass->removed)
1213 fclass->removed (sink, client->fd.fd);
1216 CLIENTS_UNLOCK (sink);
1218 /* and the fd is really gone now */
1219 g_signal_emit (G_OBJECT (sink),
1220 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED], 0, fd);
1222 CLIENTS_LOCK (sink);
1225 /* handle a read on a client fd,
1226 * which either indicates a close or should be ignored
1227 * returns FALSE if some error occured or the client closed. */
1229 gst_multi_fd_sink_handle_client_read (GstMultiFdSink * sink,
1230 GstTCPClient * client)
1237 if (ioctl (fd, FIONREAD, &avail) < 0)
1240 GST_DEBUG_OBJECT (sink, "[fd %5d] select reports client read of %d bytes",
1246 /* client sent close, so remove it */
1247 GST_DEBUG_OBJECT (sink, "[fd %5d] client asked for close, removing", fd);
1248 client->status = GST_CLIENT_STATUS_CLOSED;
1250 } else if (avail < 0) {
1251 GST_WARNING_OBJECT (sink, "[fd %5d] avail < 0, removing", fd);
1252 client->status = GST_CLIENT_STATUS_ERROR;
1258 /* just Read 'n' Drop, could also just drop the client as it's not supposed
1259 * to write to us except for closing the socket, I guess it's because we
1260 * like to listen to our customers. */
1262 /* this is the maximum we can read */
1263 gint to_read = MIN (avail, 512);
1265 GST_DEBUG_OBJECT (sink, "[fd %5d] client wants us to read %d bytes",
1268 nread = read (fd, dummy, to_read);
1270 GST_WARNING_OBJECT (sink, "[fd %5d] could not read %d bytes: %s (%d)",
1271 fd, to_read, g_strerror (errno), errno);
1272 client->status = GST_CLIENT_STATUS_ERROR;
1275 } else if (nread == 0) {
1276 GST_WARNING_OBJECT (sink, "[fd %5d] 0 bytes in read, removing", fd);
1277 client->status = GST_CLIENT_STATUS_ERROR;
1290 GST_WARNING_OBJECT (sink, "[fd %5d] ioctl failed: %s (%d)",
1291 fd, g_strerror (errno), errno);
1292 client->status = GST_CLIENT_STATUS_ERROR;
1297 /* Queue raw data for this client, creating a new buffer.
1298 * This takes ownership of the data by
1299 * setting it as GST_BUFFER_MALLOCDATA() on the created buffer so
1300 * be sure to pass g_free()-able @data.
1303 gst_multi_fd_sink_client_queue_data (GstMultiFdSink * sink,
1304 GstTCPClient * client, gchar * data, gint len)
1308 buf = gst_buffer_new ();
1309 GST_BUFFER_DATA (buf) = (guint8 *) data;
1310 GST_BUFFER_MALLOCDATA (buf) = (guint8 *) data;
1311 GST_BUFFER_SIZE (buf) = len;
1313 GST_LOG_OBJECT (sink, "[fd %5d] queueing data of length %d",
1314 client->fd.fd, len);
1316 client->sending = g_slist_append (client->sending, buf);
1321 /* GDP-encode given caps and queue them for sending */
1323 gst_multi_fd_sink_client_queue_caps (GstMultiFdSink * sink,
1324 GstTCPClient * client, const GstCaps * caps)
1331 g_return_val_if_fail (caps != NULL, FALSE);
1333 string = gst_caps_to_string (caps);
1334 GST_DEBUG_OBJECT (sink, "[fd %5d] Queueing caps %s through GDP",
1335 client->fd.fd, string);
1338 if (!gst_dp_packet_from_caps (caps, sink->header_flags, &length, &header,
1340 GST_DEBUG_OBJECT (sink, "Could not create GDP packet from caps");
1343 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) header, length);
1345 length = gst_dp_header_payload_length (header);
1346 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) payload, length);
1352 is_sync_frame (GstMultiFdSink * sink, GstBuffer * buffer)
1354 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)) {
1356 } else if (!GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_IN_CAPS)) {
1363 /* queue the given buffer for the given client, possibly adding the GDP
1364 * header if GDP is being used */
1366 gst_multi_fd_sink_client_queue_buffer (GstMultiFdSink * sink,
1367 GstTCPClient * client, GstBuffer * buffer)
1371 /* TRUE: send them if the new caps have them */
1372 gboolean send_streamheader = FALSE;
1375 /* before we queue the buffer, we check if we need to queue streamheader
1376 * buffers (because it's a new client, or because they changed) */
1377 caps = gst_buffer_get_caps (buffer); /* cleaned up after streamheader */
1378 if (!client->caps) {
1379 GST_DEBUG_OBJECT (sink,
1380 "[fd %5d] no previous caps for this client, send streamheader",
1382 send_streamheader = TRUE;
1383 client->caps = gst_caps_ref (caps);
1385 /* there were previous caps recorded, so compare */
1386 if (!gst_caps_is_equal (caps, client->caps)) {
1387 const GValue *sh1, *sh2;
1389 /* caps are not equal, but could still have the same streamheader */
1390 s = gst_caps_get_structure (caps, 0);
1391 if (!gst_structure_has_field (s, "streamheader")) {
1392 /* no new streamheader, so nothing new to send */
1393 GST_DEBUG_OBJECT (sink,
1394 "[fd %5d] new caps do not have streamheader, not sending",
1397 /* there is a new streamheader */
1398 s = gst_caps_get_structure (client->caps, 0);
1399 if (!gst_structure_has_field (s, "streamheader")) {
1400 /* no previous streamheader, so send the new one */
1401 GST_DEBUG_OBJECT (sink,
1402 "[fd %5d] previous caps did not have streamheader, sending",
1404 send_streamheader = TRUE;
1406 /* both old and new caps have streamheader set */
1407 if (!sink->resend_streamheader) {
1408 GST_DEBUG_OBJECT (sink,
1409 "[fd %5d] asked to not resend the streamheader, not sending",
1411 send_streamheader = FALSE;
1413 sh1 = gst_structure_get_value (s, "streamheader");
1414 s = gst_caps_get_structure (caps, 0);
1415 sh2 = gst_structure_get_value (s, "streamheader");
1416 if (gst_value_compare (sh1, sh2) != GST_VALUE_EQUAL) {
1417 GST_DEBUG_OBJECT (sink,
1418 "[fd %5d] new streamheader different from old, sending",
1420 send_streamheader = TRUE;
1426 /* Replace the old caps */
1427 gst_caps_unref (client->caps);
1428 client->caps = gst_caps_ref (caps);
1431 if (G_UNLIKELY (send_streamheader)) {
1436 GST_LOG_OBJECT (sink,
1437 "[fd %5d] sending streamheader from caps %" GST_PTR_FORMAT,
1438 client->fd.fd, caps);
1439 s = gst_caps_get_structure (caps, 0);
1440 if (!gst_structure_has_field (s, "streamheader")) {
1441 GST_DEBUG_OBJECT (sink,
1442 "[fd %5d] no new streamheader, so nothing to send", client->fd.fd);
1444 GST_LOG_OBJECT (sink,
1445 "[fd %5d] sending streamheader from caps %" GST_PTR_FORMAT,
1446 client->fd.fd, caps);
1447 sh = gst_structure_get_value (s, "streamheader");
1448 g_assert (G_VALUE_TYPE (sh) == GST_TYPE_ARRAY);
1449 buffers = g_value_peek_pointer (sh);
1450 GST_DEBUG_OBJECT (sink, "%d streamheader buffers", buffers->len);
1451 for (i = 0; i < buffers->len; ++i) {
1455 bufval = &g_array_index (buffers, GValue, i);
1456 g_assert (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER);
1457 buffer = g_value_peek_pointer (bufval);
1458 GST_DEBUG_OBJECT (sink,
1459 "[fd %5d] queueing streamheader buffer of length %d",
1460 client->fd.fd, GST_BUFFER_SIZE (buffer));
1461 gst_buffer_ref (buffer);
1463 if (sink->protocol == GST_TCP_PROTOCOL_GDP) {
1467 if (!gst_dp_header_from_buffer (buffer, sink->header_flags, &len,
1469 GST_DEBUG_OBJECT (sink,
1470 "[fd %5d] could not create header, removing client",
1474 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) header,
1478 client->sending = g_slist_append (client->sending, buffer);
1483 gst_caps_unref (caps);
1485 /* now we can send the buffer, possibly sending a GDP header first */
1486 if (sink->protocol == GST_TCP_PROTOCOL_GDP) {
1490 if (!gst_dp_header_from_buffer (buffer, sink->header_flags, &len, &header)) {
1491 GST_DEBUG_OBJECT (sink,
1492 "[fd %5d] could not create header, removing client", client->fd.fd);
1495 gst_multi_fd_sink_client_queue_data (sink, client, (gchar *) header, len);
1498 GST_LOG_OBJECT (sink, "[fd %5d] queueing buffer of length %d",
1499 client->fd.fd, GST_BUFFER_SIZE (buffer));
1501 gst_buffer_ref (buffer);
1502 client->sending = g_slist_append (client->sending, buffer);
1507 /* find the keyframe in the list of buffers starting the
1508 * search from @idx. @direction as -1 will search backwards,
1509 * 1 will search forwards.
1510 * Returns: the index or -1 if there is no keyframe after idx.
1513 find_syncframe (GstMultiFdSink * sink, gint idx, gint direction)
1515 gint i, len, result;
1517 /* take length of queued buffers */
1518 len = sink->bufqueue->len;
1520 /* assume we don't find a keyframe */
1523 /* then loop over all buffers to find the first keyframe */
1524 for (i = idx; i >= 0 && i < len; i += direction) {
1527 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1528 if (is_sync_frame (sink, buf)) {
1529 GST_LOG_OBJECT (sink, "found keyframe at %d from %d, direction %d",
1538 #define find_next_syncframe(s,i) find_syncframe(s,i,1)
1539 #define find_prev_syncframe(s,i) find_syncframe(s,i,-1)
1541 /* Get the number of buffers from the buffer queue needed to satisfy
1542 * the maximum max in the configured units.
1543 * If units are not BUFFERS, and there are insufficient buffers in the
1544 * queue to satify the limit, return len(queue) + 1 */
1546 get_buffers_max (GstMultiFdSink * sink, gint64 max)
1548 switch (sink->unit_type) {
1549 case GST_TCP_UNIT_TYPE_BUFFERS:
1551 case GST_TCP_UNIT_TYPE_TIME:
1557 GstClockTime first = GST_CLOCK_TIME_NONE;
1559 len = sink->bufqueue->len;
1561 for (i = 0; i < len; i++) {
1562 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1563 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1565 first = GST_BUFFER_TIMESTAMP (buf);
1567 diff = first - GST_BUFFER_TIMESTAMP (buf);
1575 case GST_TCP_UNIT_TYPE_BYTES:
1582 len = sink->bufqueue->len;
1584 for (i = 0; i < len; i++) {
1585 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1586 acc += GST_BUFFER_SIZE (buf);
1598 /* find the positions in the buffer queue where *_min and *_max
1601 /* count the amount of data in the buffers and return the index
1602 * that satifies the given limits.
1604 * Returns: index @idx in the buffer queue so that the given limits are
1605 * satisfied. TRUE if all the limits could be satisfied, FALSE if not
1606 * enough data was in the queue.
1608 * FIXME, this code might now work if any of the units is in buffers...
1611 find_limits (GstMultiFdSink * sink,
1612 gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
1613 gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max)
1615 GstClockTime first, time;
1617 gboolean result, max_hit;
1619 /* take length of queue */
1620 len = sink->bufqueue->len;
1622 /* this must hold */
1625 GST_LOG_OBJECT (sink,
1626 "bytes_min %d, buffers_min %d, time_min %" GST_TIME_FORMAT
1627 ", bytes_max %d, buffers_max %d, time_max %" GST_TIME_FORMAT, bytes_min,
1628 buffers_min, GST_TIME_ARGS (time_min), bytes_max, buffers_max,
1629 GST_TIME_ARGS (time_max));
1631 /* do the trivial buffer limit test */
1632 if (buffers_min != -1 && len < buffers_min) {
1639 /* else count bytes and time */
1648 /* loop through the buffers, when a limit is ok, mark it
1649 * as -1, we have at least one buffer in the queue. */
1653 /* if we checked all min limits, update result */
1654 if (bytes_min == -1 && time_min == -1 && *min_idx == -1) {
1655 /* don't go below 0 */
1656 *min_idx = MAX (i - 1, 0);
1658 /* if we reached one max limit break out */
1660 /* i > 0 when we get here, we subtract one to get the position
1661 * of the previous buffer. */
1663 /* we have valid complete result if we found a min_idx too */
1664 result = *min_idx != -1;
1667 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
1669 bytes += GST_BUFFER_SIZE (buf);
1671 /* take timestamp and save for the base first timestamp */
1672 if ((time = GST_BUFFER_TIMESTAMP (buf)) != -1) {
1673 GST_LOG_OBJECT (sink, "Ts %" GST_TIME_FORMAT " on buffer",
1674 GST_TIME_ARGS (time));
1678 /* increase max usage if we did not fill enough. Note that
1679 * buffers are sorted from new to old, so the first timestamp is
1680 * bigger than the next one. */
1681 if (time_min != -1 && first - time >= time_min)
1683 if (time_max != -1 && first - time >= time_max)
1686 GST_LOG_OBJECT (sink, "No timestamp on buffer");
1688 /* time is OK or unknown, check and increase if not enough bytes */
1689 if (bytes_min != -1) {
1690 if (bytes >= bytes_min)
1693 if (bytes_max != -1) {
1694 if (bytes >= bytes_max) {
1702 /* if we did not hit the max or min limit, set to buffer size */
1705 /* make sure min does not exceed max */
1707 *min_idx = *max_idx;
1712 /* parse the unit/value pair and assign it to the result value of the
1713 * right type, leave the other values untouched
1715 * Returns: FALSE if the unit is unknown or undefined. TRUE otherwise.
1718 assign_value (GstTCPUnitType unit, guint64 value, gint * bytes, gint * buffers,
1719 GstClockTime * time)
1721 gboolean res = TRUE;
1723 /* set only the limit of the given format to the given value */
1725 case GST_TCP_UNIT_TYPE_BUFFERS:
1726 *buffers = (gint) value;
1728 case GST_TCP_UNIT_TYPE_TIME:
1731 case GST_TCP_UNIT_TYPE_BYTES:
1732 *bytes = (gint) value;
1734 case GST_TCP_UNIT_TYPE_UNDEFINED:
1742 /* count the index in the buffer queue to satisfy the given unit
1743 * and value pair starting from buffer at index 0.
1745 * Returns: TRUE if there was enough data in the queue to satisfy the
1746 * burst values. @idx contains the index in the buffer that contains enough
1747 * data to satisfy the limits or the last buffer in the queue when the
1748 * function returns FALSE.
1751 count_burst_unit (GstMultiFdSink * sink, gint * min_idx,
1752 GstTCPUnitType min_unit, guint64 min_value, gint * max_idx,
1753 GstTCPUnitType max_unit, guint64 max_value)
1755 gint bytes_min = -1, buffers_min = -1;
1756 gint bytes_max = -1, buffers_max = -1;
1757 GstClockTime time_min = GST_CLOCK_TIME_NONE, time_max = GST_CLOCK_TIME_NONE;
1759 assign_value (min_unit, min_value, &bytes_min, &buffers_min, &time_min);
1760 assign_value (max_unit, max_value, &bytes_max, &buffers_max, &time_max);
1762 return find_limits (sink, min_idx, bytes_min, buffers_min, time_min,
1763 max_idx, bytes_max, buffers_max, time_max);
1766 /* decide where in the current buffer queue this new client should start
1767 * receiving buffers from.
1768 * This function is called whenever a client is connected and has not yet
1769 * received a buffer.
1770 * If this returns -1, it means that we haven't found a good point to
1771 * start streaming from yet, and this function should be called again later
1772 * when more buffers have arrived.
1775 gst_multi_fd_sink_new_client (GstMultiFdSink * sink, GstTCPClient * client)
1779 GST_DEBUG_OBJECT (sink,
1780 "[fd %5d] new client, deciding where to start in queue", client->fd.fd);
1781 GST_DEBUG_OBJECT (sink, "queue is currently %d buffers long",
1782 sink->bufqueue->len);
1783 switch (client->sync_method) {
1784 case GST_SYNC_METHOD_LATEST:
1785 /* no syncing, we are happy with whatever the client is going to get */
1786 result = client->bufpos;
1787 GST_DEBUG_OBJECT (sink,
1788 "[fd %5d] SYNC_METHOD_LATEST, position %d", client->fd.fd, result);
1790 case GST_SYNC_METHOD_NEXT_KEYFRAME:
1792 /* if one of the new buffers (between client->bufpos and 0) in the queue
1793 * is a sync point, we can proceed, otherwise we need to keep waiting */
1794 GST_LOG_OBJECT (sink,
1795 "[fd %5d] new client, bufpos %d, waiting for keyframe", client->fd.fd,
1798 result = find_prev_syncframe (sink, client->bufpos);
1800 GST_DEBUG_OBJECT (sink,
1801 "[fd %5d] SYNC_METHOD_NEXT_KEYFRAME: result %d",
1802 client->fd.fd, result);
1806 /* client is not on a syncbuffer, need to skip these buffers and
1808 GST_LOG_OBJECT (sink,
1809 "[fd %5d] new client, skipping buffer(s), no syncpoint found",
1811 client->bufpos = -1;
1814 case GST_SYNC_METHOD_LATEST_KEYFRAME:
1816 GST_DEBUG_OBJECT (sink,
1817 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME", client->fd.fd);
1819 /* for new clients we initially scan the complete buffer queue for
1820 * a sync point when a buffer is added. If we don't find a keyframe,
1821 * we need to wait for the next keyframe and so we change the client's
1822 * sync method to GST_SYNC_METHOD_NEXT_KEYFRAME.
1824 result = find_next_syncframe (sink, 0);
1826 GST_DEBUG_OBJECT (sink,
1827 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME: result %d", client->fd.fd,
1832 GST_DEBUG_OBJECT (sink,
1833 "[fd %5d] SYNC_METHOD_LATEST_KEYFRAME: no keyframe found, "
1834 "switching to SYNC_METHOD_NEXT_KEYFRAME", client->fd.fd);
1835 /* throw client to the waiting state */
1836 client->bufpos = -1;
1837 /* and make client sync to next keyframe */
1838 client->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1841 case GST_SYNC_METHOD_BURST:
1846 /* move to the position where we satisfy the client's burst
1847 * parameters. If we could not satisfy the parameters because there
1848 * is not enough data, we just send what we have (which is in result).
1849 * We use the max value to limit the search
1851 ok = count_burst_unit (sink, &result, client->burst_min_unit,
1852 client->burst_min_value, &max, client->burst_max_unit,
1853 client->burst_max_value);
1854 GST_DEBUG_OBJECT (sink,
1855 "[fd %5d] SYNC_METHOD_BURST: burst_unit returned %d, result %d",
1856 client->fd.fd, ok, result);
1858 GST_LOG_OBJECT (sink, "min %d, max %d", result, max);
1860 /* we hit the max and it is below the min, use that then */
1861 if (max != -1 && max <= result) {
1862 result = MAX (max - 1, 0);
1863 GST_DEBUG_OBJECT (sink,
1864 "[fd %5d] SYNC_METHOD_BURST: result above max, taken down to %d",
1865 client->fd.fd, result);
1869 case GST_SYNC_METHOD_BURST_KEYFRAME:
1871 gint min_idx, max_idx;
1872 gint next_syncframe, prev_syncframe;
1876 * _always_ start sending a keyframe to the client. We first search
1877 * a keyframe between min/max limits. If there is none, we send it the
1878 * last keyframe before min. If there is none, the behaviour is like
1881 /* gather burst limits */
1882 count_burst_unit (sink, &min_idx, client->burst_min_unit,
1883 client->burst_min_value, &max_idx, client->burst_max_unit,
1884 client->burst_max_value);
1886 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1888 /* first find a keyframe after min_idx */
1889 next_syncframe = find_next_syncframe (sink, min_idx);
1890 if (next_syncframe != -1 && next_syncframe < max_idx) {
1891 /* we have a valid keyframe and it's below the max */
1892 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1893 result = next_syncframe;
1897 /* no valid keyframe, try to find one below min */
1898 prev_syncframe = find_prev_syncframe (sink, min_idx);
1899 if (prev_syncframe != -1) {
1900 GST_WARNING_OBJECT (sink,
1901 "using keyframe below min in BURST_KEYFRAME sync mode");
1902 result = prev_syncframe;
1906 /* no prev keyframe or not enough data */
1907 GST_WARNING_OBJECT (sink,
1908 "no prev keyframe found in BURST_KEYFRAME sync mode, waiting for next");
1910 /* throw client to the waiting state */
1911 client->bufpos = -1;
1912 /* and make client sync to next keyframe */
1913 client->sync_method = GST_SYNC_METHOD_NEXT_KEYFRAME;
1917 case GST_SYNC_METHOD_BURST_WITH_KEYFRAME:
1919 gint min_idx, max_idx;
1920 gint next_syncframe;
1922 /* BURST_WITH_KEYFRAME:
1924 * try to start sending a keyframe to the client. We first search
1925 * a keyframe between min/max limits. If there is none, we send it the
1926 * amount of data up 'till min.
1928 /* gather enough data to burst */
1929 count_burst_unit (sink, &min_idx, client->burst_min_unit,
1930 client->burst_min_value, &max_idx, client->burst_max_unit,
1931 client->burst_max_value);
1933 GST_LOG_OBJECT (sink, "min %d, max %d", min_idx, max_idx);
1935 /* first find a keyframe after min_idx */
1936 next_syncframe = find_next_syncframe (sink, min_idx);
1937 if (next_syncframe != -1 && next_syncframe < max_idx) {
1938 /* we have a valid keyframe and it's below the max */
1939 GST_LOG_OBJECT (sink, "found keyframe in min/max limits");
1940 result = next_syncframe;
1944 /* no keyframe, send data from min_idx */
1945 GST_WARNING_OBJECT (sink, "using min in BURST_WITH_KEYFRAME sync mode");
1947 /* make sure we don't go over the max limit */
1948 if (max_idx != -1 && max_idx <= min_idx) {
1949 result = MAX (max_idx - 1, 0);
1957 g_warning ("unknown sync method %d", client->sync_method);
1958 result = client->bufpos;
1964 /* Handle a write on a client,
1965 * which indicates a read request from a client.
1967 * For each client we maintain a queue of GstBuffers that contain the raw bytes
1968 * we need to send to the client. In the case of the GDP protocol, we create
1969 * buffers out of the header bytes so that we can focus only on sending
1972 * We first check to see if we need to send caps (in GDP) and streamheaders.
1973 * If so, we queue them.
1975 * Then we run into the main loop that tries to send as many buffers as
1976 * possible. It will first exhaust the client->sending queue and if the queue
1977 * is empty, it will pick a buffer from the global queue.
1979 * Sending the buffers from the client->sending queue is basically writing
1980 * the bytes to the socket and maintaining a count of the bytes that were
1981 * sent. When the buffer is completely sent, it is removed from the
1982 * client->sending queue and we try to pick a new buffer for sending.
1984 * When the sending returns a partial buffer we stop sending more data as
1985 * the next send operation could block.
1987 * This functions returns FALSE if some error occured.
1990 gst_multi_fd_sink_handle_client_write (GstMultiFdSink * sink,
1991 GstTCPClient * client)
1993 int fd = client->fd.fd;
2000 g_get_current_time (&nowtv);
2001 now = GST_TIMEVAL_TO_TIME (nowtv);
2003 flushing = client->status == GST_CLIENT_STATUS_FLUSHING;
2005 /* when using GDP, first check if we have queued caps yet */
2006 if (sink->protocol == GST_TCP_PROTOCOL_GDP) {
2007 /* don't need to do anything when the client is flushing */
2008 if (!client->caps_sent && !flushing) {
2012 peer = gst_pad_get_peer (GST_BASE_SINK_PAD (sink));
2014 GST_WARNING_OBJECT (sink, "pad has no peer");
2017 gst_object_unref (peer);
2019 caps = gst_pad_get_negotiated_caps (GST_BASE_SINK_PAD (sink));
2021 GST_WARNING_OBJECT (sink, "pad caps not yet negotiated");
2025 /* queue caps for sending */
2026 res = gst_multi_fd_sink_client_queue_caps (sink, client, caps);
2028 gst_caps_unref (caps);
2031 GST_DEBUG_OBJECT (sink, "Failed queueing caps, removing client");
2034 client->caps_sent = TRUE;
2042 if (!client->sending) {
2043 /* client is not working on a buffer */
2044 if (client->bufpos == -1) {
2045 /* client is too fast, remove from write queue until new buffer is
2047 gst_poll_fd_ctl_write (sink->fdset, &client->fd, FALSE);
2048 /* if we flushed out all of the client buffers, we can stop */
2049 if (client->flushcount == 0)
2054 /* client can pick a buffer from the global queue */
2056 GstClockTime timestamp;
2058 /* for new connections, we need to find a good spot in the
2059 * bufqueue to start streaming from */
2060 if (client->new_connection && !flushing) {
2061 gint position = gst_multi_fd_sink_new_client (sink, client);
2063 if (position >= 0) {
2064 /* we got a valid spot in the queue */
2065 client->new_connection = FALSE;
2066 client->bufpos = position;
2068 /* cannot send data to this client yet */
2069 gst_poll_fd_ctl_write (sink->fdset, &client->fd, FALSE);
2074 /* we flushed all remaining buffers, no need to get a new one */
2075 if (client->flushcount == 0)
2079 buf = g_array_index (sink->bufqueue, GstBuffer *, client->bufpos);
2083 timestamp = GST_BUFFER_TIMESTAMP (buf);
2084 if (client->first_buffer_ts == GST_CLOCK_TIME_NONE)
2085 client->first_buffer_ts = timestamp;
2086 if (timestamp != -1)
2087 client->last_buffer_ts = timestamp;
2089 /* decrease flushcount */
2090 if (client->flushcount != -1)
2091 client->flushcount--;
2093 GST_LOG_OBJECT (sink, "[fd %5d] client %p at position %d",
2094 fd, client, client->bufpos);
2096 /* queueing a buffer will ref it */
2097 gst_multi_fd_sink_client_queue_buffer (sink, client, buf);
2099 /* need to start from the first byte for this new buffer */
2100 client->bufoffset = 0;
2104 /* see if we need to send something */
2105 if (client->sending) {
2109 /* pick first buffer from list */
2110 head = GST_BUFFER (client->sending->data);
2111 maxsize = GST_BUFFER_SIZE (head) - client->bufoffset;
2113 /* try to write the complete buffer */
2115 #define FLAGS MSG_NOSIGNAL
2119 if (client->is_socket) {
2121 send (fd, GST_BUFFER_DATA (head) + client->bufoffset, maxsize,
2124 wrote = write (fd, GST_BUFFER_DATA (head) + client->bufoffset, maxsize);
2129 if (errno == EAGAIN) {
2130 /* nothing serious, resource was unavailable, try again later */
2132 } else if (errno == ECONNRESET) {
2133 goto connection_reset;
2138 if (wrote < maxsize) {
2139 /* partial write means that the client cannot read more and we should
2140 * stop sending more */
2141 GST_LOG_OBJECT (sink,
2142 "partial write on %d of %" G_GSSIZE_FORMAT " bytes", fd, wrote);
2143 client->bufoffset += wrote;
2146 /* complete buffer was written, we can proceed to the next one */
2147 client->sending = g_slist_remove (client->sending, head);
2148 gst_buffer_unref (head);
2149 /* make sure we start from byte 0 for the next buffer */
2150 client->bufoffset = 0;
2153 client->bytes_sent += wrote;
2154 client->last_activity_time = now;
2155 sink->bytes_served += wrote;
2165 GST_DEBUG_OBJECT (sink, "[fd %5d] flushed, removing", fd);
2166 client->status = GST_CLIENT_STATUS_REMOVED;
2171 GST_DEBUG_OBJECT (sink, "[fd %5d] connection reset by peer, removing", fd);
2172 client->status = GST_CLIENT_STATUS_CLOSED;
2177 GST_WARNING_OBJECT (sink,
2178 "[fd %5d] could not write, removing client: %s (%d)", fd,
2179 g_strerror (errno), errno);
2180 client->status = GST_CLIENT_STATUS_ERROR;
2185 /* calculate the new position for a client after recovery. This function
2186 * does not update the client position but merely returns the required
2190 gst_multi_fd_sink_recover_client (GstMultiFdSink * sink, GstTCPClient * client)
2194 GST_WARNING_OBJECT (sink,
2195 "[fd %5d] client %p is lagging at %d, recover using policy %d",
2196 client->fd.fd, client, client->bufpos, sink->recover_policy);
2198 switch (sink->recover_policy) {
2199 case GST_RECOVER_POLICY_NONE:
2200 /* do nothing, client will catch up or get kicked out when it reaches
2202 newbufpos = client->bufpos;
2204 case GST_RECOVER_POLICY_RESYNC_LATEST:
2205 /* move to beginning of queue */
2208 case GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT:
2209 /* move to beginning of soft max */
2210 newbufpos = get_buffers_max (sink, sink->units_soft_max);
2212 case GST_RECOVER_POLICY_RESYNC_KEYFRAME:
2213 /* find keyframe in buffers, we search backwards to find the
2214 * closest keyframe relative to what this client already received. */
2215 newbufpos = MIN (sink->bufqueue->len - 1,
2216 get_buffers_max (sink, sink->units_soft_max) - 1);
2218 while (newbufpos >= 0) {
2221 buf = g_array_index (sink->bufqueue, GstBuffer *, newbufpos);
2222 if (is_sync_frame (sink, buf)) {
2223 /* found a buffer that is not a delta unit */
2230 /* unknown recovery procedure */
2231 newbufpos = get_buffers_max (sink, sink->units_soft_max);
2237 /* Queue a buffer on the global queue.
2239 * This function adds the buffer to the front of a GArray. It removes the
2240 * tail buffer if the max queue size is exceeded, unreffing the queued buffer.
2241 * Note that unreffing the buffer is not a problem as clients who
2242 * started writing out this buffer will still have a reference to it in the
2243 * client->sending queue.
2245 * After adding the buffer, we update all client positions in the queue. If
2246 * a client moves over the soft max, we start the recovery procedure for this
2247 * slow client. If it goes over the hard max, it is put into the slow list
2250 * Special care is taken of clients that were waiting for a new buffer (they
2251 * had a position of -1) because they can proceed after adding this new buffer.
2252 * This is done by adding the client back into the write fd_set and signalling
2253 * the select thread that the fd_set changed.
2256 gst_multi_fd_sink_queue_buffer (GstMultiFdSink * sink, GstBuffer * buf)
2258 GList *clients, *next;
2260 gboolean need_signal = FALSE;
2261 gint max_buffer_usage;
2265 gint max_buffers, soft_max_buffers;
2268 g_get_current_time (&nowtv);
2269 now = GST_TIMEVAL_TO_TIME (nowtv);
2271 CLIENTS_LOCK (sink);
2272 /* add buffer to queue */
2273 g_array_prepend_val (sink->bufqueue, buf);
2274 queuelen = sink->bufqueue->len;
2276 if (sink->units_max > 0)
2277 max_buffers = get_buffers_max (sink, sink->units_max);
2281 if (sink->units_soft_max > 0)
2282 soft_max_buffers = get_buffers_max (sink, sink->units_soft_max);
2284 soft_max_buffers = -1;
2285 GST_LOG_OBJECT (sink, "Using max %d, softmax %d", max_buffers,
2288 /* then loop over the clients and update the positions */
2289 max_buffer_usage = 0;
2292 cookie = sink->clients_cookie;
2293 for (clients = sink->clients; clients; clients = next) {
2294 GstTCPClient *client;
2296 if (cookie != sink->clients_cookie) {
2297 GST_DEBUG_OBJECT (sink, "Clients cookie outdated, restarting");
2301 client = (GstTCPClient *) clients->data;
2302 next = g_list_next (clients);
2305 GST_LOG_OBJECT (sink, "[fd %5d] client %p at position %d",
2306 client->fd.fd, client, client->bufpos);
2307 /* check soft max if needed, recover client */
2308 if (soft_max_buffers > 0 && client->bufpos >= soft_max_buffers) {
2311 newpos = gst_multi_fd_sink_recover_client (sink, client);
2312 if (newpos != client->bufpos) {
2313 client->dropped_buffers += client->bufpos - newpos;
2314 client->bufpos = newpos;
2315 client->discont = TRUE;
2316 GST_INFO_OBJECT (sink, "[fd %5d] client %p position reset to %d",
2317 client->fd.fd, client, client->bufpos);
2319 GST_INFO_OBJECT (sink,
2320 "[fd %5d] client %p not recovering position",
2321 client->fd.fd, client);
2324 /* check hard max and timeout, remove client */
2325 if ((max_buffers > 0 && client->bufpos >= max_buffers) ||
2327 && now - client->last_activity_time > sink->timeout)) {
2329 GST_WARNING_OBJECT (sink, "[fd %5d] client %p is too slow, removing",
2330 client->fd.fd, client);
2331 /* remove the client, the fd set will be cleared and the select thread
2332 * will be signaled */
2333 client->status = GST_CLIENT_STATUS_SLOW;
2334 /* set client to invalid position while being removed */
2335 client->bufpos = -1;
2336 gst_multi_fd_sink_remove_client_link (sink, clients);
2339 } else if (client->bufpos == 0 || client->new_connection) {
2340 /* can send data to this client now. need to signal the select thread that
2341 * the fd_set changed */
2342 gst_poll_fd_ctl_write (sink->fdset, &client->fd, TRUE);
2345 /* keep track of maximum buffer usage */
2346 if (client->bufpos > max_buffer_usage) {
2347 max_buffer_usage = client->bufpos;
2351 /* make sure we respect bytes-min, buffers-min and time-min when they are set */
2355 GST_LOG_OBJECT (sink,
2356 "extending queue %d to respect time_min %" GST_TIME_FORMAT
2357 ", bytes_min %d, buffers_min %d", max_buffer_usage,
2358 GST_TIME_ARGS (sink->time_min), sink->bytes_min, sink->buffers_min);
2360 /* get index where the limits are ok, we don't really care if all limits
2361 * are ok, we just queue as much as we need. We also don't compare against
2362 * the max limits. */
2363 find_limits (sink, &usage, sink->bytes_min, sink->buffers_min,
2364 sink->time_min, &max, -1, -1, -1);
2366 max_buffer_usage = MAX (max_buffer_usage, usage + 1);
2367 GST_LOG_OBJECT (sink, "extended queue to %d", max_buffer_usage);
2370 /* now look for sync points and make sure there is at least one
2371 * sync point in the queue. We only do this if the LATEST_KEYFRAME or
2372 * BURST_KEYFRAME mode is selected */
2373 if (sink->def_sync_method == GST_SYNC_METHOD_LATEST_KEYFRAME ||
2374 sink->def_sync_method == GST_SYNC_METHOD_BURST_KEYFRAME) {
2375 /* no point in searching beyond the queue length */
2376 gint limit = queuelen;
2379 /* no point in searching beyond the soft-max if any. */
2380 if (soft_max_buffers > 0) {
2381 limit = MIN (limit, soft_max_buffers);
2383 GST_LOG_OBJECT (sink,
2384 "extending queue to include sync point, now at %d, limit is %d",
2385 max_buffer_usage, limit);
2386 for (i = 0; i < limit; i++) {
2387 buf = g_array_index (sink->bufqueue, GstBuffer *, i);
2388 if (is_sync_frame (sink, buf)) {
2389 /* found a sync frame, now extend the buffer usage to
2390 * include at least this frame. */
2391 max_buffer_usage = MAX (max_buffer_usage, i);
2395 GST_LOG_OBJECT (sink, "max buffer usage is now %d", max_buffer_usage);
2398 GST_LOG_OBJECT (sink, "len %d, usage %d", queuelen, max_buffer_usage);
2400 /* nobody is referencing units after max_buffer_usage so we can
2401 * remove them from the queue. We remove them in reverse order as
2402 * this is the most optimal for GArray. */
2403 for (i = queuelen - 1; i > max_buffer_usage; i--) {
2406 /* queue exceeded max size */
2408 old = g_array_index (sink->bufqueue, GstBuffer *, i);
2409 sink->bufqueue = g_array_remove_index (sink->bufqueue, i);
2411 /* unref tail buffer */
2412 gst_buffer_unref (old);
2414 /* save for stats */
2415 sink->buffers_queued = max_buffer_usage;
2416 CLIENTS_UNLOCK (sink);
2418 /* and send a signal to thread if fd_set changed */
2420 gst_poll_restart (sink->fdset);
2424 /* Handle the clients. Basically does a blocking select for one
2425 * of the client fds to become read or writable. We also have a
2426 * filedescriptor to receive commands on that we need to check.
2428 * After going out of the select call, we read and write to all
2429 * clients that can do so. Badly behaving clients are put on a
2430 * garbage list and removed.
2433 gst_multi_fd_sink_handle_clients (GstMultiFdSink * sink)
2436 GList *clients, *next;
2438 GstMultiFdSinkClass *fclass;
2441 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
2447 * - server socket input (ie, new client connections)
2448 * - client socket input (ie, clients saying goodbye)
2449 * - client socket output (ie, client reads) */
2450 GST_LOG_OBJECT (sink, "waiting on action on fdset");
2452 result = gst_poll_wait (sink->fdset, sink->timeout != 0 ? sink->timeout :
2453 GST_CLOCK_TIME_NONE);
2455 /* Handle the special case in which the sink is not receiving more buffers
2456 * and will not disconnect innactive client in the streaming thread. */
2457 if (G_UNLIKELY (result == 0)) {
2461 g_get_current_time (&nowtv);
2462 now = GST_TIMEVAL_TO_TIME (nowtv);
2464 CLIENTS_LOCK (sink);
2465 for (clients = sink->clients; clients; clients = next) {
2466 GstTCPClient *client;
2468 client = (GstTCPClient *) clients->data;
2469 next = g_list_next (clients);
2470 if (sink->timeout > 0
2471 && now - client->last_activity_time > sink->timeout) {
2472 client->status = GST_CLIENT_STATUS_SLOW;
2473 gst_multi_fd_sink_remove_client_link (sink, clients);
2476 CLIENTS_UNLOCK (sink);
2478 } else if (result < 0) {
2479 GST_WARNING_OBJECT (sink, "wait failed: %s (%d)", g_strerror (errno),
2481 if (errno == EBADF) {
2482 /* ok, so one or more of the fds is invalid. We loop over them to find
2483 * the ones that give an error to the F_GETFL fcntl. */
2484 CLIENTS_LOCK (sink);
2486 cookie = sink->clients_cookie;
2487 for (clients = sink->clients; clients; clients = next) {
2488 GstTCPClient *client;
2493 if (cookie != sink->clients_cookie) {
2494 GST_DEBUG_OBJECT (sink, "Cookie changed finding bad fd");
2498 client = (GstTCPClient *) clients->data;
2499 next = g_list_next (clients);
2503 res = fcntl (fd, F_GETFL, &flags);
2505 GST_WARNING_OBJECT (sink, "fnctl failed for %d, removing: %s (%d)",
2506 fd, g_strerror (errno), errno);
2507 if (errno == EBADF) {
2508 client->status = GST_CLIENT_STATUS_ERROR;
2509 /* releases the CLIENTS lock */
2510 gst_multi_fd_sink_remove_client_link (sink, clients);
2514 CLIENTS_UNLOCK (sink);
2515 /* after this, go back in the select loop as the read/writefds
2518 } else if (errno == EINTR) {
2519 /* interrupted system call, just redo the wait */
2521 } else if (errno == EBUSY) {
2522 /* the call to gst_poll_wait() was flushed */
2525 /* this is quite bad... */
2526 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
2527 ("select failed: %s (%d)", g_strerror (errno), errno));
2531 GST_LOG_OBJECT (sink, "wait done: %d sockets with events", result);
2533 } while (try_again);
2535 /* subclasses can check fdset with this virtual function */
2537 fclass->wait (sink, sink->fdset);
2539 /* Check the clients */
2540 CLIENTS_LOCK (sink);
2543 cookie = sink->clients_cookie;
2544 for (clients = sink->clients; clients; clients = next) {
2545 GstTCPClient *client;
2547 if (sink->clients_cookie != cookie) {
2548 GST_DEBUG_OBJECT (sink, "Restarting loop, cookie out of date");
2552 client = (GstTCPClient *) clients->data;
2553 next = g_list_next (clients);
2555 if (client->status != GST_CLIENT_STATUS_FLUSHING
2556 && client->status != GST_CLIENT_STATUS_OK) {
2557 gst_multi_fd_sink_remove_client_link (sink, clients);
2561 if (gst_poll_fd_has_closed (sink->fdset, &client->fd)) {
2562 client->status = GST_CLIENT_STATUS_CLOSED;
2563 gst_multi_fd_sink_remove_client_link (sink, clients);
2566 if (gst_poll_fd_has_error (sink->fdset, &client->fd)) {
2567 GST_WARNING_OBJECT (sink, "gst_poll_fd_has_error for %d", client->fd.fd);
2568 client->status = GST_CLIENT_STATUS_ERROR;
2569 gst_multi_fd_sink_remove_client_link (sink, clients);
2572 if (gst_poll_fd_can_read (sink->fdset, &client->fd)) {
2573 /* handle client read */
2574 if (!gst_multi_fd_sink_handle_client_read (sink, client)) {
2575 gst_multi_fd_sink_remove_client_link (sink, clients);
2579 if (gst_poll_fd_can_write (sink->fdset, &client->fd)) {
2580 /* handle client write */
2581 if (!gst_multi_fd_sink_handle_client_write (sink, client)) {
2582 gst_multi_fd_sink_remove_client_link (sink, clients);
2587 CLIENTS_UNLOCK (sink);
2590 /* we handle the client communication in another thread so that we do not block
2591 * the gstreamer thread while we select() on the client fds */
2593 gst_multi_fd_sink_thread (GstMultiFdSink * sink)
2595 while (sink->running) {
2596 gst_multi_fd_sink_handle_clients (sink);
2601 static GstFlowReturn
2602 gst_multi_fd_sink_render (GstBaseSink * bsink, GstBuffer * buf)
2604 GstMultiFdSink *sink;
2606 GstCaps *bufcaps, *padcaps;
2608 sink = GST_MULTI_FD_SINK (bsink);
2610 g_return_val_if_fail (GST_OBJECT_FLAG_IS_SET (sink, GST_MULTI_FD_SINK_OPEN),
2611 GST_FLOW_WRONG_STATE);
2613 /* since we check every buffer for streamheader caps, we need to make
2614 * sure every buffer has caps set */
2615 bufcaps = gst_buffer_get_caps (buf);
2616 padcaps = GST_PAD_CAPS (GST_BASE_SINK_PAD (bsink));
2618 /* make sure we have caps on the pad */
2619 if (!padcaps && !bufcaps)
2622 /* get IN_CAPS first, code below might mess with the flags */
2623 in_caps = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
2625 /* stamp the buffer with previous caps if no caps set */
2627 if (!gst_buffer_is_metadata_writable (buf)) {
2628 /* metadata is not writable, copy will be made and original buffer
2629 * will be unreffed so we need to ref so that we don't lose the
2630 * buffer in the render method. */
2631 gst_buffer_ref (buf);
2632 /* the new buffer is ours only, we keep it out of the scope of this
2634 buf = gst_buffer_make_metadata_writable (buf);
2636 /* else the metadata is writable, we ref because we keep the buffer
2637 * out of the scope of this method */
2638 gst_buffer_ref (buf);
2640 /* buffer metadata is writable now, set the caps */
2641 gst_buffer_set_caps (buf, padcaps);
2643 gst_caps_unref (bufcaps);
2645 /* since we keep this buffer out of the scope of this method */
2646 gst_buffer_ref (buf);
2649 GST_LOG_OBJECT (sink, "received buffer %p, in_caps: %s, offset %"
2650 G_GINT64_FORMAT ", offset_end %" G_GINT64_FORMAT
2651 ", timestamp %" GST_TIME_FORMAT ", duration %" GST_TIME_FORMAT,
2652 buf, in_caps ? "yes" : "no", GST_BUFFER_OFFSET (buf),
2653 GST_BUFFER_OFFSET_END (buf),
2654 GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
2655 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
2657 /* if we get IN_CAPS buffers, but the previous buffer was not IN_CAPS,
2658 * it means we're getting new streamheader buffers, and we should clear
2660 if (in_caps && sink->previous_buffer_in_caps == FALSE) {
2661 GST_DEBUG_OBJECT (sink,
2662 "receiving new IN_CAPS buffers, clearing old streamheader");
2663 g_slist_foreach (sink->streamheader, (GFunc) gst_mini_object_unref, NULL);
2664 g_slist_free (sink->streamheader);
2665 sink->streamheader = NULL;
2668 /* save the current in_caps */
2669 sink->previous_buffer_in_caps = in_caps;
2671 /* if the incoming buffer is marked as IN CAPS, then we assume for now
2672 * it's a streamheader that needs to be sent to each new client, so we
2673 * put it on our internal list of streamheader buffers.
2674 * FIXME: we could check if the buffer's contents are in fact part of the
2675 * current streamheader.
2677 * We don't send the buffer to the client, since streamheaders are sent
2678 * separately when necessary. */
2680 GST_DEBUG_OBJECT (sink,
2681 "appending IN_CAPS buffer with length %d to streamheader",
2682 GST_BUFFER_SIZE (buf));
2683 sink->streamheader = g_slist_append (sink->streamheader, buf);
2685 /* queue the buffer, this is a regular data buffer. */
2686 gst_multi_fd_sink_queue_buffer (sink, buf);
2688 sink->bytes_to_serve += GST_BUFFER_SIZE (buf);
2695 GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
2696 ("Received first buffer without caps set"));
2697 return GST_FLOW_NOT_NEGOTIATED;
2702 gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
2703 const GValue * value, GParamSpec * pspec)
2705 GstMultiFdSink *multifdsink;
2707 multifdsink = GST_MULTI_FD_SINK (object);
2711 multifdsink->protocol = g_value_get_enum (value);
2714 multifdsink->mode = g_value_get_enum (value);
2716 case PROP_BUFFERS_MAX:
2717 multifdsink->units_max = g_value_get_int (value);
2719 case PROP_BUFFERS_SOFT_MAX:
2720 multifdsink->units_soft_max = g_value_get_int (value);
2723 multifdsink->time_min = g_value_get_int64 (value);
2725 case PROP_BYTES_MIN:
2726 multifdsink->bytes_min = g_value_get_int (value);
2728 case PROP_BUFFERS_MIN:
2729 multifdsink->buffers_min = g_value_get_int (value);
2731 case PROP_UNIT_TYPE:
2732 multifdsink->unit_type = g_value_get_enum (value);
2734 case PROP_UNITS_MAX:
2735 multifdsink->units_max = g_value_get_int64 (value);
2737 case PROP_UNITS_SOFT_MAX:
2738 multifdsink->units_soft_max = g_value_get_int64 (value);
2740 case PROP_RECOVER_POLICY:
2741 multifdsink->recover_policy = g_value_get_enum (value);
2744 multifdsink->timeout = g_value_get_uint64 (value);
2746 case PROP_SYNC_METHOD:
2747 multifdsink->def_sync_method = g_value_get_enum (value);
2749 case PROP_BURST_UNIT:
2750 multifdsink->def_burst_unit = g_value_get_enum (value);
2752 case PROP_BURST_VALUE:
2753 multifdsink->def_burst_value = g_value_get_uint64 (value);
2756 multifdsink->qos_dscp = g_value_get_int (value);
2757 setup_dscp (multifdsink);
2759 case PROP_HANDLE_READ:
2760 multifdsink->handle_read = g_value_get_boolean (value);
2762 case PROP_RESEND_STREAMHEADER:
2763 multifdsink->resend_streamheader = g_value_get_boolean (value);
2767 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2773 gst_multi_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
2776 GstMultiFdSink *multifdsink;
2778 multifdsink = GST_MULTI_FD_SINK (object);
2782 g_value_set_enum (value, multifdsink->protocol);
2785 g_value_set_enum (value, multifdsink->mode);
2787 case PROP_BUFFERS_MAX:
2788 g_value_set_int (value, multifdsink->units_max);
2790 case PROP_BUFFERS_SOFT_MAX:
2791 g_value_set_int (value, multifdsink->units_soft_max);
2794 g_value_set_int64 (value, multifdsink->time_min);
2796 case PROP_BYTES_MIN:
2797 g_value_set_int (value, multifdsink->bytes_min);
2799 case PROP_BUFFERS_MIN:
2800 g_value_set_int (value, multifdsink->buffers_min);
2802 case PROP_BUFFERS_QUEUED:
2803 g_value_set_uint (value, multifdsink->buffers_queued);
2805 case PROP_BYTES_QUEUED:
2806 g_value_set_uint (value, multifdsink->bytes_queued);
2808 case PROP_TIME_QUEUED:
2809 g_value_set_uint64 (value, multifdsink->time_queued);
2811 case PROP_UNIT_TYPE:
2812 g_value_set_enum (value, multifdsink->unit_type);
2814 case PROP_UNITS_MAX:
2815 g_value_set_int64 (value, multifdsink->units_max);
2817 case PROP_UNITS_SOFT_MAX:
2818 g_value_set_int64 (value, multifdsink->units_soft_max);
2820 case PROP_RECOVER_POLICY:
2821 g_value_set_enum (value, multifdsink->recover_policy);
2824 g_value_set_uint64 (value, multifdsink->timeout);
2826 case PROP_SYNC_METHOD:
2827 g_value_set_enum (value, multifdsink->def_sync_method);
2829 case PROP_BYTES_TO_SERVE:
2830 g_value_set_uint64 (value, multifdsink->bytes_to_serve);
2832 case PROP_BYTES_SERVED:
2833 g_value_set_uint64 (value, multifdsink->bytes_served);
2835 case PROP_BURST_UNIT:
2836 g_value_set_enum (value, multifdsink->def_burst_unit);
2838 case PROP_BURST_VALUE:
2839 g_value_set_uint64 (value, multifdsink->def_burst_value);
2842 g_value_set_int (value, multifdsink->qos_dscp);
2844 case PROP_HANDLE_READ:
2845 g_value_set_boolean (value, multifdsink->handle_read);
2847 case PROP_RESEND_STREAMHEADER:
2848 g_value_set_boolean (value, multifdsink->resend_streamheader);
2851 g_value_set_uint (value, g_hash_table_size (multifdsink->fd_hash));
2855 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2861 /* create a socket for sending to remote machine */
2863 gst_multi_fd_sink_start (GstBaseSink * bsink)
2865 GstMultiFdSinkClass *fclass;
2866 GstMultiFdSink *this;
2868 if (GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_FD_SINK_OPEN))
2871 this = GST_MULTI_FD_SINK (bsink);
2872 fclass = GST_MULTI_FD_SINK_GET_CLASS (this);
2874 GST_INFO_OBJECT (this, "starting in mode %d", this->mode);
2875 if ((this->fdset = gst_poll_new (TRUE)) == NULL)
2878 this->streamheader = NULL;
2879 this->bytes_to_serve = 0;
2880 this->bytes_served = 0;
2883 fclass->init (this);
2886 this->running = TRUE;
2887 this->thread = g_thread_create ((GThreadFunc) gst_multi_fd_sink_thread,
2890 GST_OBJECT_FLAG_SET (this, GST_MULTI_FD_SINK_OPEN);
2897 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_READ_WRITE, (NULL),
2904 multifdsink_hash_remove (gpointer key, gpointer value, gpointer data)
2910 gst_multi_fd_sink_stop (GstBaseSink * bsink)
2912 GstMultiFdSinkClass *fclass;
2913 GstMultiFdSink *this;
2917 this = GST_MULTI_FD_SINK (bsink);
2918 fclass = GST_MULTI_FD_SINK_GET_CLASS (this);
2920 if (!GST_OBJECT_FLAG_IS_SET (bsink, GST_MULTI_FD_SINK_OPEN))
2923 this->running = FALSE;
2925 gst_poll_set_flushing (this->fdset, TRUE);
2927 GST_DEBUG_OBJECT (this, "joining thread");
2928 g_thread_join (this->thread);
2929 GST_DEBUG_OBJECT (this, "joined thread");
2930 this->thread = NULL;
2933 /* free the clients */
2934 gst_multi_fd_sink_clear (this);
2936 if (this->streamheader) {
2937 g_slist_foreach (this->streamheader, (GFunc) gst_mini_object_unref, NULL);
2938 g_slist_free (this->streamheader);
2939 this->streamheader = NULL;
2943 fclass->close (this);
2946 gst_poll_free (this->fdset);
2949 g_hash_table_foreach_remove (this->fd_hash, multifdsink_hash_remove, this);
2951 /* remove all queued buffers */
2952 if (this->bufqueue) {
2953 GST_DEBUG_OBJECT (this, "Emptying bufqueue with %d buffers",
2954 this->bufqueue->len);
2955 for (i = this->bufqueue->len - 1; i >= 0; --i) {
2956 buf = g_array_index (this->bufqueue, GstBuffer *, i);
2957 GST_LOG_OBJECT (this, "Removing buffer %p (%d) with refcount %d", buf, i,
2958 GST_MINI_OBJECT_REFCOUNT (buf));
2959 gst_buffer_unref (buf);
2960 this->bufqueue = g_array_remove_index (this->bufqueue, i);
2962 /* freeing the array is done in _finalize */
2964 GST_OBJECT_FLAG_UNSET (this, GST_MULTI_FD_SINK_OPEN);
2969 static GstStateChangeReturn
2970 gst_multi_fd_sink_change_state (GstElement * element, GstStateChange transition)
2972 GstMultiFdSink *sink;
2973 GstStateChangeReturn ret;
2975 sink = GST_MULTI_FD_SINK (element);
2977 /* we disallow changing the state from the streaming thread */
2978 if (g_thread_self () == sink->thread)
2979 return GST_STATE_CHANGE_FAILURE;
2982 switch (transition) {
2983 case GST_STATE_CHANGE_NULL_TO_READY:
2984 if (!gst_multi_fd_sink_start (GST_BASE_SINK (sink)))
2987 case GST_STATE_CHANGE_READY_TO_PAUSED:
2989 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2995 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2997 switch (transition) {
2998 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3000 case GST_STATE_CHANGE_PAUSED_TO_READY:
3002 case GST_STATE_CHANGE_READY_TO_NULL:
3003 gst_multi_fd_sink_stop (GST_BASE_SINK (sink));
3013 /* error message was posted */
3014 return GST_STATE_CHANGE_FAILURE;