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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * SECTION:element-multifdsink
25 * @see_also: tcpserversink
27 * This plugin writes incoming data to a set of file descriptors. The
28 * file descriptors can be added to multifdsink by emitting the #GstMultiFdSink::add signal.
29 * For each descriptor added, the #GstMultiFdSink::client-added signal will be called.
31 * The multifdsink element needs to be set into READY, PAUSED or PLAYING state
32 * before operations such as adding clients are possible.
34 * A client can also be added with the #GstMultiFdSink::add-full signal
35 * that allows for more control over what and how much data a client
38 * Clients can be removed from multifdsink by emitting the #GstMultiFdSink::remove signal. For
39 * each descriptor removed, the #GstMultiFdSink::client-removed signal will be called. The
40 * #GstMultiFdSink::client-removed signal can also be fired when multifdsink decides that a
41 * client is not active anymore or, depending on the value of the
42 * #GstMultiFdSink:recover-policy property, if the client is reading too slowly.
43 * In all cases, multifdsink will never close a file descriptor itself.
44 * The user of multifdsink is responsible for closing all file descriptors.
45 * This can for example be done in response to the #GstMultiFdSink::client-fd-removed signal.
46 * Note that multifdsink still has a reference to the file descriptor when the
47 * #GstMultiFdSink::client-removed signal is emitted, so that "get-stats" can be performed on
48 * the descriptor; it is therefore not safe to close the file descriptor in
49 * the #GstMultiFdSink::client-removed signal handler, and you should use the
50 * #GstMultiFdSink::client-fd-removed signal to safely close the fd.
52 * Multifdsink internally keeps a queue of the incoming buffers and uses a
53 * separate thread to send the buffers to the clients. This ensures that no
54 * client write can block the pipeline and that clients can read with different
57 * When adding a client to multifdsink, the #GstMultiFdSink:sync-method property will define
58 * which buffer in the queued buffers will be sent first to the client. Clients
59 * can be sent the most recent buffer (which might not be decodable by the
60 * client if it is not a keyframe), the next keyframe received in
61 * multifdsink (which can take some time depending on the keyframe rate), or the
62 * last received keyframe (which will cause a simple burst-on-connect).
63 * Multifdsink will always keep at least one keyframe in its internal buffers
64 * when the sync-mode is set to latest-keyframe.
66 * There are additional values for the #GstMultiFdSink:sync-method
67 * property to allow finer control over burst-on-connect behaviour. By selecting
68 * the 'burst' method a minimum burst size can be chosen, 'burst-keyframe'
69 * additionally requires that the burst begin with a keyframe, and
70 * 'burst-with-keyframe' attempts to burst beginning with a keyframe, but will
71 * prefer a minimum burst size even if it requires not starting with a keyframe.
73 * Multifdsink can be instructed to keep at least a minimum amount of data
74 * expressed in time or byte units in its internal queues with the
75 * #GstMultiFdSink:time-min and #GstMultiFdSink:bytes-min properties respectively.
76 * These properties are useful if the application adds clients with the
77 * #GstMultiFdSink::add-full signal to make sure that a burst connect can
78 * actually be honored.
80 * When streaming data, clients are allowed to read at a different rate than
81 * the rate at which multifdsink receives data. If the client is reading too
82 * fast, no data will be send to the client until multifdsink receives more
83 * data. If the client, however, reads too slowly, data for that client will be
84 * queued up in multifdsink. Two properties control the amount of data
85 * (buffers) that is queued in multifdsink: #GstMultiFdSink:buffers-max and
86 * #GstMultiFdSink:buffers-soft-max. A client that falls behind by
87 * #GstMultiFdSink:buffers-max is removed from multifdsink forcibly.
89 * A client with a lag of at least #GstMultiFdSink:buffers-soft-max enters the recovery
90 * procedure which is controlled with the #GstMultiFdSink:recover-policy property.
91 * A recover policy of NONE will do nothing, RESYNC_LATEST will send the most recently
92 * received buffer as the next buffer for the client, RESYNC_SOFT_LIMIT
93 * positions the client to the soft limit in the buffer queue and
94 * RESYNC_KEYFRAME positions the client at the most recent keyframe in the
97 * multifdsink will by default synchronize on the clock before serving the
98 * buffers to the clients. This behaviour can be disabled by setting the sync
99 * property to FALSE. Multifdsink will by default not do QoS and will never
107 #include <gst/gst-i18n-plugin.h>
109 #include <sys/ioctl.h>
117 #include <sys/types.h>
118 #include <sys/socket.h>
119 #include <sys/stat.h>
120 #include <netinet/in.h>
122 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
123 #include <sys/filio.h>
126 #include "gstmultifdsink.h"
128 #define NOT_IMPLEMENTED 0
130 GST_DEBUG_CATEGORY_STATIC (multifdsink_debug);
131 #define GST_CAT_DEFAULT (multifdsink_debug)
133 /* MultiFdSink signals and args */
145 SIGNAL_CLIENT_REMOVED,
146 SIGNAL_CLIENT_FD_REMOVED,
151 /* this is really arbitrarily chosen */
152 #define DEFAULT_HANDLE_READ TRUE
160 static void gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink);
161 static void gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink);
162 static gboolean gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink);
163 static gpointer gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink);
165 static void gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd);
166 static void gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
167 GstSyncMethod sync, GstFormat min_format, guint64 min_value,
168 GstFormat max_format, guint64 max_value);
169 static void gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd);
170 static void gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd);
171 static GstStructure *gst_multi_fd_sink_get_stats (GstMultiFdSink * sink,
174 static void gst_multi_fd_sink_emit_client_added (GstMultiHandleSink * mhsink,
175 GstMultiSinkHandle handle);
176 static void gst_multi_fd_sink_emit_client_removed (GstMultiHandleSink * mhsink,
177 GstMultiSinkHandle handle, GstClientStatus status);
179 static GstMultiHandleClient *gst_multi_fd_sink_new_client (GstMultiHandleSink *
180 mhsink, GstMultiSinkHandle handle, GstSyncMethod sync_method);
181 static void gst_multi_fd_sink_client_free (GstMultiHandleSink * m,
182 GstMultiHandleClient * client);
183 static int gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client);
184 static void gst_multi_fd_sink_handle_debug (GstMultiSinkHandle handle,
186 static gpointer gst_multi_fd_sink_handle_hash_key (GstMultiSinkHandle handle);
187 static void gst_multi_fd_sink_hash_adding (GstMultiHandleSink * mhsink,
188 GstMultiHandleClient * mhclient);
189 static void gst_multi_fd_sink_hash_removing (GstMultiHandleSink * mhsink,
190 GstMultiHandleClient * mhclient);
192 static void gst_multi_fd_sink_hash_changed (GstMultiHandleSink * mhsink);
194 static void gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
195 const GValue * value, GParamSpec * pspec);
196 static void gst_multi_fd_sink_get_property (GObject * object, guint prop_id,
197 GValue * value, GParamSpec * pspec);
199 #define gst_multi_fd_sink_parent_class parent_class
200 G_DEFINE_TYPE (GstMultiFdSink, gst_multi_fd_sink, GST_TYPE_MULTI_HANDLE_SINK);
202 static guint gst_multi_fd_sink_signals[LAST_SIGNAL] = { 0 };
205 gst_multi_fd_sink_class_init (GstMultiFdSinkClass * klass)
207 GObjectClass *gobject_class;
208 GstElementClass *gstelement_class;
209 GstMultiHandleSinkClass *gstmultihandlesink_class;
211 gobject_class = (GObjectClass *) klass;
212 gstelement_class = (GstElementClass *) klass;
213 gstmultihandlesink_class = (GstMultiHandleSinkClass *) klass;
215 gobject_class->set_property = gst_multi_fd_sink_set_property;
216 gobject_class->get_property = gst_multi_fd_sink_get_property;
219 * GstMultiFdSink::handle-read
221 * Handle read requests from clients and discard the data.
223 g_object_class_install_property (gobject_class, PROP_HANDLE_READ,
224 g_param_spec_boolean ("handle-read", "Handle Read",
225 "Handle client reads and discard the data",
226 DEFAULT_HANDLE_READ, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 * GstMultiFdSink::add:
230 * @gstmultifdsink: the multifdsink element to emit this signal on
231 * @fd: the file descriptor to add to multifdsink
233 * Hand the given open file descriptor to multifdsink to write to.
235 gst_multi_fd_sink_signals[SIGNAL_ADD] =
236 g_signal_new ("add", G_TYPE_FROM_CLASS (klass),
237 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
238 G_STRUCT_OFFSET (GstMultiFdSinkClass, add), NULL, NULL,
239 NULL, G_TYPE_NONE, 1, G_TYPE_INT);
241 * GstMultiFdSink::add-full:
242 * @gstmultifdsink: the multifdsink element to emit this signal on
243 * @fd: the file descriptor to add to multifdsink
244 * @sync: the sync method to use
245 * @format_min: the format of @value_min
246 * @value_min: the minimum amount of data to burst expressed in
248 * @format_max: the format of @value_max
249 * @value_max: the maximum amount of data to burst expressed in
252 * Hand the given open file descriptor to multifdsink to write to and
253 * specify the burst parameters for the new connection.
255 gst_multi_fd_sink_signals[SIGNAL_ADD_BURST] =
256 g_signal_new ("add-full", G_TYPE_FROM_CLASS (klass),
257 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
258 G_STRUCT_OFFSET (GstMultiFdSinkClass, add_full), NULL, NULL,
259 NULL, G_TYPE_NONE, 6, G_TYPE_INT, GST_TYPE_SYNC_METHOD, GST_TYPE_FORMAT,
260 G_TYPE_UINT64, GST_TYPE_FORMAT, G_TYPE_UINT64);
262 * GstMultiFdSink::remove:
263 * @gstmultifdsink: the multifdsink element to emit this signal on
264 * @fd: the file descriptor to remove from multifdsink
266 * Remove the given open file descriptor from multifdsink.
268 gst_multi_fd_sink_signals[SIGNAL_REMOVE] =
269 g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
270 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
271 G_STRUCT_OFFSET (GstMultiFdSinkClass, remove), NULL, NULL,
272 NULL, G_TYPE_NONE, 1, G_TYPE_INT);
274 * GstMultiFdSink::remove-flush:
275 * @gstmultifdsink: the multifdsink element to emit this signal on
276 * @fd: the file descriptor to remove from multifdsink
278 * Remove the given open file descriptor from multifdsink after flushing all
279 * the pending data to the fd.
281 gst_multi_fd_sink_signals[SIGNAL_REMOVE_FLUSH] =
282 g_signal_new ("remove-flush", G_TYPE_FROM_CLASS (klass),
283 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
284 G_STRUCT_OFFSET (GstMultiFdSinkClass, remove_flush), NULL, NULL,
285 NULL, G_TYPE_NONE, 1, G_TYPE_INT);
288 * GstMultiFdSink::get-stats:
289 * @gstmultifdsink: the multifdsink element to emit this signal on
290 * @fd: the file descriptor to get stats of from multifdsink
292 * Get statistics about @fd. This function returns a #GstStructure to ease
293 * automatic wrapping for bindings.
295 * Returns: a #GstStructure with the statistics. The structures
296 * contains guint64 values that represent respectively: total
297 * number of bytes sent (bytes-sent), time when the client was
298 * added (connect-time), time when the client was
299 * disconnected/removed (disconnect-time), time the client
300 * is/was active (connect-duration), last activity time (in
301 * epoch seconds) (last-activity-time), number of buffers
302 * dropped (buffers-dropped), the timestamp of the first buffer
303 * (first-buffer-ts) and of the last buffer (last-buffer-ts).
304 * All times are expressed in nanoseconds (GstClockTime). The
305 * structure can be empty if the client was not found.
307 gst_multi_fd_sink_signals[SIGNAL_GET_STATS] =
308 g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
309 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
310 G_STRUCT_OFFSET (GstMultiFdSinkClass, get_stats), NULL, NULL,
311 NULL, GST_TYPE_STRUCTURE, 1, G_TYPE_INT);
314 * GstMultiFdSink::client-added:
315 * @gstmultifdsink: the multifdsink element that emitted this signal
316 * @fd: the file descriptor that was added to multifdsink
318 * The given file descriptor was added to multifdsink. This signal will
319 * be emitted from the streaming thread so application should be prepared
322 gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED] =
323 g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
324 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_INT);
326 * GstMultiFdSink::client-removed:
327 * @gstmultifdsink: the multifdsink element that emitted this signal
328 * @fd: the file descriptor that is to be removed from multifdsink
329 * @status: the reason why the client was removed
331 * The given file descriptor is about to be removed from multifdsink. This
332 * signal will be emitted from the streaming thread so applications should
333 * be prepared for that.
335 * @gstmultifdsink still holds a handle to @fd so it is possible to call
336 * the get-stats signal from this callback. For the same reason it is
337 * not safe to `close()` and reuse @fd in this callback.
339 gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED] =
340 g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
341 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 2, G_TYPE_INT,
342 GST_TYPE_CLIENT_STATUS);
344 * GstMultiFdSink::client-fd-removed:
345 * @gstmultifdsink: the multifdsink element that emitted this signal
346 * @fd: the file descriptor that was removed from multifdsink
348 * The given file descriptor was removed from multifdsink. This signal will
349 * be emitted from the streaming thread so applications should be prepared
352 * In this callback, @gstmultifdsink has removed all the information
353 * associated with @fd and it is therefore not possible to call get-stats
354 * with @fd. It is however safe to `close()` and reuse @fd in the callback.
356 gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED] =
357 g_signal_new ("client-fd-removed", G_TYPE_FROM_CLASS (klass),
358 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 1, G_TYPE_INT);
360 gst_element_class_set_static_metadata (gstelement_class,
361 "Multi filedescriptor sink", "Sink/Network",
362 "Send data to multiple filedescriptors",
363 "Thomas Vander Stichele <thomas at apestaart dot org>, "
364 "Wim Taymans <wim@fluendo.com>");
366 klass->add = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add);
367 klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add_full);
368 klass->remove = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove);
369 klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove_flush);
370 klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_get_stats);
372 gstmultihandlesink_class->emit_client_added =
373 gst_multi_fd_sink_emit_client_added;
374 gstmultihandlesink_class->emit_client_removed =
375 gst_multi_fd_sink_emit_client_removed;
377 gstmultihandlesink_class->stop_pre =
378 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_pre);
379 gstmultihandlesink_class->stop_post =
380 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_post);
381 gstmultihandlesink_class->start_pre =
382 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_start_pre);
383 gstmultihandlesink_class->thread =
384 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_thread);
385 gstmultihandlesink_class->new_client =
386 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_new_client);
387 gstmultihandlesink_class->client_free = gst_multi_fd_sink_client_free;
388 gstmultihandlesink_class->client_get_fd =
389 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_client_get_fd);
390 gstmultihandlesink_class->handle_debug =
391 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_handle_debug);
392 gstmultihandlesink_class->handle_hash_key =
393 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_handle_hash_key);
394 gstmultihandlesink_class->hash_changed =
395 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_changed);
396 gstmultihandlesink_class->hash_adding =
397 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_adding);
398 gstmultihandlesink_class->hash_removing =
399 GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_removing);
401 GST_DEBUG_CATEGORY_INIT (multifdsink_debug, "multifdsink", 0, "FD sink");
405 gst_multi_fd_sink_init (GstMultiFdSink * this)
407 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (this);
409 mhsink->handle_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
411 this->handle_read = DEFAULT_HANDLE_READ;
414 /* methods to emit signals */
417 gst_multi_fd_sink_emit_client_added (GstMultiHandleSink * mhsink,
418 GstMultiSinkHandle handle)
420 g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED], 0,
425 gst_multi_fd_sink_emit_client_removed (GstMultiHandleSink * mhsink,
426 GstMultiSinkHandle handle, GstClientStatus status)
428 g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0,
433 gst_multi_fd_sink_client_free (GstMultiHandleSink * mhsink,
434 GstMultiHandleClient * client)
436 g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED],
437 0, client->handle.fd);
443 gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd)
445 GstMultiSinkHandle handle;
448 gst_multi_handle_sink_add (GST_MULTI_HANDLE_SINK_CAST (sink), handle);
452 gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
453 GstSyncMethod sync, GstFormat min_format, guint64 min_value,
454 GstFormat max_format, guint64 max_value)
456 GstMultiSinkHandle handle;
459 gst_multi_handle_sink_add_full (GST_MULTI_HANDLE_SINK_CAST (sink), handle,
460 sync, min_format, min_value, max_format, max_value);
464 gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd)
466 GstMultiSinkHandle handle;
469 gst_multi_handle_sink_remove (GST_MULTI_HANDLE_SINK_CAST (sink), handle);
473 gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd)
475 GstMultiSinkHandle handle;
478 gst_multi_handle_sink_remove_flush (GST_MULTI_HANDLE_SINK_CAST (sink),
482 static GstStructure *
483 gst_multi_fd_sink_get_stats (GstMultiFdSink * sink, int fd)
485 GstMultiSinkHandle handle;
488 return gst_multi_handle_sink_get_stats (GST_MULTI_HANDLE_SINK_CAST (sink),
494 static GstMultiHandleClient *
495 gst_multi_fd_sink_new_client (GstMultiHandleSink * mhsink,
496 GstMultiSinkHandle handle, GstSyncMethod sync_method)
499 GstTCPClient *client;
500 GstMultiHandleClient *mhclient;
501 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
502 GstMultiHandleSinkClass *mhsinkclass =
503 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
505 /* create client datastructure */
506 client = g_new0 (GstTCPClient, 1);
507 mhclient = (GstMultiHandleClient *) client;
509 mhclient->handle = handle;
511 gst_poll_fd_init (&client->gfd);
512 client->gfd.fd = mhclient->handle.fd;
514 gst_multi_handle_sink_client_init (mhclient, sync_method);
515 mhsinkclass->handle_debug (handle, mhclient->debug);
517 /* set the socket to non blocking */
518 if (fcntl (handle.fd, F_SETFL, O_NONBLOCK) < 0) {
519 GST_ERROR_OBJECT (mhsink, "failed to make socket %s non-blocking: %s",
520 mhclient->debug, g_strerror (errno));
523 /* we always read from a client */
524 gst_poll_add_fd (sink->fdset, &client->gfd);
526 /* we don't try to read from write only fds */
527 if (sink->handle_read) {
530 flags = fcntl (handle.fd, F_GETFL, 0);
531 if ((flags & O_ACCMODE) != O_WRONLY) {
532 gst_poll_fd_ctl_read (sink->fdset, &client->gfd, TRUE);
535 /* figure out the mode, can't use send() for non sockets */
536 if (fstat (handle.fd, &statbuf) == 0 && S_ISSOCK (statbuf.st_mode)) {
537 client->is_socket = TRUE;
538 gst_multi_handle_sink_setup_dscp_client (mhsink, mhclient);
545 gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client)
547 GstTCPClient *tclient = (GstTCPClient *) client;
549 return tclient->gfd.fd;
553 gst_multi_fd_sink_handle_debug (GstMultiSinkHandle handle, gchar debug[30])
555 g_snprintf (debug, 30, "[fd %5d]", handle.fd);
559 gst_multi_fd_sink_handle_hash_key (GstMultiSinkHandle handle)
561 return GINT_TO_POINTER (handle.fd);
565 gst_multi_fd_sink_hash_changed (GstMultiHandleSink * mhsink)
567 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
569 gst_poll_restart (sink->fdset);
572 /* handle a read on a client fd,
573 * which either indicates a close or should be ignored
574 * returns FALSE if some error occurred or the client closed. */
576 gst_multi_fd_sink_handle_client_read (GstMultiFdSink * sink,
577 GstTCPClient * client)
581 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
585 if (ioctl (fd, FIONREAD, &avail) < 0)
588 GST_DEBUG_OBJECT (sink, "%s select reports client read of %d bytes",
589 mhclient->debug, avail);
594 /* client sent close, so remove it */
595 GST_DEBUG_OBJECT (sink, "%s client asked for close, removing",
597 mhclient->status = GST_CLIENT_STATUS_CLOSED;
599 } else if (avail < 0) {
600 GST_WARNING_OBJECT (sink, "%s avail < 0, removing", mhclient->debug);
601 mhclient->status = GST_CLIENT_STATUS_ERROR;
607 /* just Read 'n' Drop, could also just drop the client as it's not supposed
608 * to write to us except for closing the socket, I guess it's because we
609 * like to listen to our customers. */
611 /* this is the maximum we can read */
612 gint to_read = MIN (avail, 512);
614 GST_DEBUG_OBJECT (sink, "%s client wants us to read %d bytes",
615 mhclient->debug, to_read);
617 nread = read (fd, dummy, to_read);
619 GST_WARNING_OBJECT (sink, "%s could not read %d bytes: %s (%d)",
620 mhclient->debug, to_read, g_strerror (errno), errno);
621 mhclient->status = GST_CLIENT_STATUS_ERROR;
624 } else if (nread == 0) {
625 GST_WARNING_OBJECT (sink, "%s 0 bytes in read, removing",
627 mhclient->status = GST_CLIENT_STATUS_ERROR;
640 GST_WARNING_OBJECT (sink, "%s ioctl failed: %s (%d)",
641 mhclient->debug, g_strerror (errno), errno);
642 mhclient->status = GST_CLIENT_STATUS_ERROR;
647 /* Handle a write on a client,
648 * which indicates a read request from a client.
650 * For each client we maintain a queue of GstBuffers that contain the raw bytes
651 * we need to send to the client.
653 * We first check to see if we need to send streamheaders. If so, we queue them.
655 * Then we run into the main loop that tries to send as many buffers as
656 * possible. It will first exhaust the mhclient->sending queue and if the queue
657 * is empty, it will pick a buffer from the global queue.
659 * Sending the buffers from the mhclient->sending queue is basically writing
660 * the bytes to the socket and maintaining a count of the bytes that were
661 * sent. When the buffer is completely sent, it is removed from the
662 * mhclient->sending queue and we try to pick a new buffer for sending.
664 * When the sending returns a partial buffer we stop sending more data as
665 * the next send operation could block.
667 * This functions returns FALSE if some error occurred.
670 gst_multi_fd_sink_handle_client_write (GstMultiFdSink * sink,
671 GstTCPClient * client)
677 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
678 GstMultiHandleSinkClass *mhsinkclass =
679 GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
680 GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
681 int fd = mhclient->handle.fd;
683 flushing = mhclient->status == GST_CLIENT_STATUS_FLUSHING;
689 g_get_current_time (&nowtv);
690 now = GST_TIMEVAL_TO_TIME (nowtv);
692 if (!mhclient->sending) {
693 /* client is not working on a buffer */
694 if (mhclient->bufpos == -1) {
695 /* client is too fast, remove from write queue until new buffer is
697 /* FIXME: specific */
698 gst_poll_fd_ctl_write (sink->fdset, &client->gfd, FALSE);
700 /* if we flushed out all of the client buffers, we can stop */
701 if (mhclient->flushcount == 0)
706 /* client can pick a buffer from the global queue */
708 GstClockTime timestamp;
710 /* for new connections, we need to find a good spot in the
711 * bufqueue to start streaming from */
712 if (mhclient->new_connection && !flushing) {
714 gst_multi_handle_sink_new_client_position (mhsink, mhclient);
717 /* we got a valid spot in the queue */
718 mhclient->new_connection = FALSE;
719 mhclient->bufpos = position;
721 /* cannot send data to this client yet */
722 /* FIXME: specific */
723 gst_poll_fd_ctl_write (sink->fdset, &client->gfd, FALSE);
728 /* we flushed all remaining buffers, no need to get a new one */
729 if (mhclient->flushcount == 0)
733 buf = g_array_index (mhsink->bufqueue, GstBuffer *, mhclient->bufpos);
737 timestamp = GST_BUFFER_TIMESTAMP (buf);
738 if (mhclient->first_buffer_ts == GST_CLOCK_TIME_NONE)
739 mhclient->first_buffer_ts = timestamp;
741 mhclient->last_buffer_ts = timestamp;
743 /* decrease flushcount */
744 if (mhclient->flushcount != -1)
745 mhclient->flushcount--;
747 GST_LOG_OBJECT (sink, "%s client %p at position %d",
748 mhclient->debug, client, mhclient->bufpos);
750 /* queueing a buffer will ref it */
751 mhsinkclass->client_queue_buffer (mhsink, mhclient, buf);
753 /* need to start from the first byte for this new buffer */
754 mhclient->bufoffset = 0;
758 /* see if we need to send something */
759 if (mhclient->sending) {
765 /* pick first buffer from list */
766 head = GST_BUFFER (mhclient->sending->data);
768 if (!gst_buffer_map (head, &info, GST_MAP_READ))
769 g_return_val_if_reached (FALSE);
772 maxsize = info.size - mhclient->bufoffset;
774 /* FIXME: specific */
775 /* try to write the complete buffer */
777 #define FLAGS MSG_NOSIGNAL
781 if (client->is_socket) {
782 wrote = send (fd, data + mhclient->bufoffset, maxsize, FLAGS);
784 wrote = write (fd, data + mhclient->bufoffset, maxsize);
786 gst_buffer_unmap (head, &info);
790 if (errno == EAGAIN) {
791 /* nothing serious, resource was unavailable, try again later */
793 } else if (errno == ECONNRESET) {
794 goto connection_reset;
799 if (wrote < maxsize) {
800 /* partial write means that the client cannot read more and we should
801 * stop sending more */
802 GST_LOG_OBJECT (sink,
803 "partial write on %s of %" G_GSSIZE_FORMAT " bytes",
804 mhclient->debug, wrote);
805 mhclient->bufoffset += wrote;
808 /* complete buffer was written, we can proceed to the next one */
809 mhclient->sending = g_slist_remove (mhclient->sending, head);
810 gst_buffer_unref (head);
811 /* make sure we start from byte 0 for the next buffer */
812 mhclient->bufoffset = 0;
815 mhclient->bytes_sent += wrote;
816 mhclient->last_activity_time = now;
817 mhsink->bytes_served += wrote;
827 GST_DEBUG_OBJECT (sink, "%s flushed, removing", mhclient->debug);
828 mhclient->status = GST_CLIENT_STATUS_REMOVED;
833 GST_DEBUG_OBJECT (sink, "%s connection reset by peer, removing",
835 mhclient->status = GST_CLIENT_STATUS_CLOSED;
840 GST_WARNING_OBJECT (sink,
841 "%s could not write, removing client: %s (%d)", mhclient->debug,
842 g_strerror (errno), errno);
843 mhclient->status = GST_CLIENT_STATUS_ERROR;
849 gst_multi_fd_sink_hash_adding (GstMultiHandleSink * mhsink,
850 GstMultiHandleClient * mhclient)
852 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
853 GstTCPClient *client = (GstTCPClient *) mhclient;
855 gst_poll_fd_ctl_write (sink->fdset, &client->gfd, TRUE);
859 gst_multi_fd_sink_hash_removing (GstMultiHandleSink * mhsink,
860 GstMultiHandleClient * mhclient)
862 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
863 GstTCPClient *client = (GstTCPClient *) mhclient;
865 gst_poll_remove_fd (sink->fdset, &client->gfd);
869 /* Handle the clients. Basically does a blocking select for one
870 * of the client fds to become read or writable. We also have a
871 * filedescriptor to receive commands on that we need to check.
873 * After going out of the select call, we read and write to all
874 * clients that can do so. Badly behaving clients are put on a
875 * garbage list and removed.
878 gst_multi_fd_sink_handle_clients (GstMultiFdSink * sink)
881 GList *clients, *next;
883 GstMultiFdSinkClass *fclass;
885 GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
889 fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
895 * - server socket input (ie, new client connections)
896 * - client socket input (ie, clients saying goodbye)
897 * - client socket output (ie, client reads) */
898 GST_LOG_OBJECT (sink, "waiting on action on fdset");
901 gst_poll_wait (sink->fdset,
902 mhsink->timeout != 0 ? mhsink->timeout : GST_CLOCK_TIME_NONE);
904 /* Handle the special case in which the sink is not receiving more buffers
905 * and will not disconnect inactive client in the streaming thread. */
906 if (G_UNLIKELY (result == 0)) {
910 g_get_current_time (&nowtv);
911 now = GST_TIMEVAL_TO_TIME (nowtv);
913 CLIENTS_LOCK (mhsink);
914 for (clients = mhsink->clients; clients; clients = next) {
915 GstTCPClient *client;
916 GstMultiHandleClient *mhclient;
918 client = (GstTCPClient *) clients->data;
919 mhclient = (GstMultiHandleClient *) client;
920 next = g_list_next (clients);
921 if (mhsink->timeout > 0
922 && now - mhclient->last_activity_time > mhsink->timeout) {
923 mhclient->status = GST_CLIENT_STATUS_SLOW;
924 gst_multi_handle_sink_remove_client_link (mhsink, clients);
927 CLIENTS_UNLOCK (mhsink);
929 } else if (result < 0) {
930 GST_WARNING_OBJECT (sink, "wait failed: %s (%d)", g_strerror (errno),
932 if (errno == EBADF) {
933 /* ok, so one or more of the fds is invalid. We loop over them to find
934 * the ones that give an error to the F_GETFL fcntl. */
935 CLIENTS_LOCK (mhsink);
937 cookie = mhsink->clients_cookie;
938 for (clients = mhsink->clients; clients; clients = next) {
939 GstTCPClient *client;
940 GstMultiHandleClient *mhclient;
944 if (cookie != mhsink->clients_cookie) {
945 GST_DEBUG_OBJECT (sink, "Cookie changed finding bad fd");
949 client = (GstTCPClient *) clients->data;
950 mhclient = (GstMultiHandleClient *) client;
951 next = g_list_next (clients);
955 res = fcntl (fd, F_GETFL, &flags);
957 GST_WARNING_OBJECT (sink, "fcntl failed for %d, removing: %s (%d)",
958 fd, g_strerror (errno), errno);
959 if (errno == EBADF) {
960 mhclient->status = GST_CLIENT_STATUS_ERROR;
961 /* releases the CLIENTS lock */
962 gst_multi_handle_sink_remove_client_link (mhsink, clients);
966 CLIENTS_UNLOCK (mhsink);
967 /* after this, go back in the select loop as the read/writefds
970 } else if (errno == EINTR) {
971 /* interrupted system call, just redo the wait */
973 } else if (errno == EBUSY) {
974 /* the call to gst_poll_wait() was flushed */
977 /* this is quite bad... */
978 GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
979 ("select failed: %s (%d)", g_strerror (errno), errno));
983 GST_LOG_OBJECT (sink, "wait done: %d sockets with events", result);
987 /* subclasses can check fdset with this virtual function */
989 fclass->wait (sink, sink->fdset);
991 /* Check the clients */
992 CLIENTS_LOCK (mhsink);
995 cookie = mhsink->clients_cookie;
996 for (clients = mhsink->clients; clients; clients = next) {
997 GstTCPClient *client;
998 GstMultiHandleClient *mhclient;
1000 if (mhsink->clients_cookie != cookie) {
1001 GST_DEBUG_OBJECT (sink, "Restarting loop, cookie out of date");
1005 client = (GstTCPClient *) clients->data;
1006 mhclient = (GstMultiHandleClient *) client;
1007 next = g_list_next (clients);
1009 if (mhclient->status != GST_CLIENT_STATUS_FLUSHING
1010 && mhclient->status != GST_CLIENT_STATUS_OK) {
1011 gst_multi_handle_sink_remove_client_link (mhsink, clients);
1015 if (gst_poll_fd_has_closed (sink->fdset, &client->gfd)) {
1016 mhclient->status = GST_CLIENT_STATUS_CLOSED;
1017 gst_multi_handle_sink_remove_client_link (mhsink, clients);
1020 if (gst_poll_fd_has_error (sink->fdset, &client->gfd)) {
1021 GST_WARNING_OBJECT (sink, "gst_poll_fd_has_error for %d", client->gfd.fd);
1022 mhclient->status = GST_CLIENT_STATUS_ERROR;
1023 gst_multi_handle_sink_remove_client_link (mhsink, clients);
1026 if (gst_poll_fd_can_read (sink->fdset, &client->gfd)) {
1027 /* handle client read */
1028 if (!gst_multi_fd_sink_handle_client_read (sink, client)) {
1029 gst_multi_handle_sink_remove_client_link (mhsink, clients);
1033 if (gst_poll_fd_can_write (sink->fdset, &client->gfd)) {
1034 /* handle client write */
1035 if (!gst_multi_fd_sink_handle_client_write (sink, client)) {
1036 gst_multi_handle_sink_remove_client_link (mhsink, clients);
1041 CLIENTS_UNLOCK (mhsink);
1044 /* we handle the client communication in another thread so that we do not block
1045 * the gstreamer thread while we select() on the client fds */
1047 gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink)
1049 GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
1051 while (mhsink->running) {
1052 gst_multi_fd_sink_handle_clients (sink);
1058 gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
1059 const GValue * value, GParamSpec * pspec)
1061 GstMultiFdSink *multifdsink;
1063 multifdsink = GST_MULTI_FD_SINK (object);
1066 case PROP_HANDLE_READ:
1067 multifdsink->handle_read = g_value_get_boolean (value);
1071 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1077 gst_multi_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
1080 GstMultiFdSink *multifdsink;
1082 multifdsink = GST_MULTI_FD_SINK (object);
1085 case PROP_HANDLE_READ:
1086 g_value_set_boolean (value, multifdsink->handle_read);
1090 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1096 gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink)
1098 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
1100 GST_INFO_OBJECT (mfsink, "starting");
1101 if ((mfsink->fdset = gst_poll_new (TRUE)) == NULL)
1109 GST_ELEMENT_ERROR (mfsink, RESOURCE, OPEN_READ_WRITE, (NULL),
1116 multifdsink_hash_remove (gpointer key, gpointer value, gpointer data)
1122 gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink)
1124 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
1126 gst_poll_set_flushing (mfsink->fdset, TRUE);
1130 gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink)
1132 GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
1134 if (mfsink->fdset) {
1135 gst_poll_free (mfsink->fdset);
1136 mfsink->fdset = NULL;
1138 g_hash_table_foreach_remove (mhsink->handle_hash, multifdsink_hash_remove,