1 /* GStreamer
2  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3  * Copyright (C) <2004> Thomas Vander Stichele <thomas at apestaart dot org>
4  * Copyright (C) 2006 Wim Taymans <wim at fluendo dot com>
5  *
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.
10  *
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.
15  *
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.
20  */
21 
22 /**
23  * SECTION:element-multifdsink
24  * @title: multifdsink
25  * @see_also: tcpserversink
26  *
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.
30  *
31  * The multifdsink element needs to be set into READY, PAUSED or PLAYING state
32  * before operations such as adding clients are possible.
33  *
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
36  * initially receives.
37  *
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.
51  *
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
55  * speeds.
56  *
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.
65  *
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.
72  *
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.
79  *
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.
88  *
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
95  * buffer queue.
96  *
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
100  * drop late buffers.
101  */
102 
103 #ifdef HAVE_CONFIG_H
104 #include "config.h"
105 #endif
106 
107 #include <gst/gst-i18n-plugin.h>
108 
109 #include <sys/ioctl.h>
110 
111 #ifdef HAVE_UNISTD_H
112 #include <unistd.h>
113 #endif
114 
115 #include <string.h>
116 #include <fcntl.h>
117 #include <sys/types.h>
118 #include <sys/socket.h>
119 #include <sys/stat.h>
120 #include <netinet/in.h>
121 
122 #ifdef HAVE_FIONREAD_IN_SYS_FILIO
123 #include <sys/filio.h>
124 #endif
125 
126 #include "gstmultifdsink.h"
127 
128 #define NOT_IMPLEMENTED 0
129 
130 GST_DEBUG_CATEGORY_STATIC (multifdsink_debug);
131 #define GST_CAT_DEFAULT (multifdsink_debug)
132 
133 /* MultiFdSink signals and args */
134 enum
135 {
136   /* methods */
137   SIGNAL_ADD,
138   SIGNAL_ADD_BURST,
139   SIGNAL_REMOVE,
140   SIGNAL_REMOVE_FLUSH,
141   SIGNAL_GET_STATS,
142 
143   /* signals */
144   SIGNAL_CLIENT_ADDED,
145   SIGNAL_CLIENT_REMOVED,
146   SIGNAL_CLIENT_FD_REMOVED,
147 
148   LAST_SIGNAL
149 };
150 
151 /* this is really arbitrarily chosen */
152 #define DEFAULT_HANDLE_READ             TRUE
153 
154 enum
155 {
156   PROP_0,
157   PROP_HANDLE_READ
158 };
159 
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);
164 
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,
172     int fd);
173 
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);
178 
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,
185     gchar debug[30]);
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);
191 
192 static void gst_multi_fd_sink_hash_changed (GstMultiHandleSink * mhsink);
193 
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);
198 
199 #define gst_multi_fd_sink_parent_class parent_class
200 G_DEFINE_TYPE (GstMultiFdSink, gst_multi_fd_sink, GST_TYPE_MULTI_HANDLE_SINK);
201 
202 static guint gst_multi_fd_sink_signals[LAST_SIGNAL] = { 0 };
203 
204 static void
gst_multi_fd_sink_class_init(GstMultiFdSinkClass * klass)205 gst_multi_fd_sink_class_init (GstMultiFdSinkClass * klass)
206 {
207   GObjectClass *gobject_class;
208   GstElementClass *gstelement_class;
209   GstMultiHandleSinkClass *gstmultihandlesink_class;
210 
211   gobject_class = (GObjectClass *) klass;
212   gstelement_class = (GstElementClass *) klass;
213   gstmultihandlesink_class = (GstMultiHandleSinkClass *) klass;
214 
215   gobject_class->set_property = gst_multi_fd_sink_set_property;
216   gobject_class->get_property = gst_multi_fd_sink_get_property;
217 
218   /**
219    * GstMultiFdSink::handle-read
220    *
221    * Handle read requests from clients and discard the data.
222    */
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));
227 
228   /**
229    * GstMultiFdSink::add:
230    * @gstmultifdsink: the multifdsink element to emit this signal on
231    * @fd:             the file descriptor to add to multifdsink
232    *
233    * Hand the given open file descriptor to multifdsink to write to.
234    */
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       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
240   /**
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
247    *                   @format_min units.
248    * @format_max:      the format of @value_max
249    * @value_max:       the maximum amount of data to burst expressed in
250    *                   @format_max units.
251    *
252    * Hand the given open file descriptor to multifdsink to write to and
253    * specify the burst parameters for the new connection.
254    */
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       g_cclosure_marshal_generic, G_TYPE_NONE, 6,
260       G_TYPE_INT, GST_TYPE_SYNC_METHOD, GST_TYPE_FORMAT, G_TYPE_UINT64,
261       GST_TYPE_FORMAT, G_TYPE_UINT64);
262   /**
263    * GstMultiFdSink::remove:
264    * @gstmultifdsink: the multifdsink element to emit this signal on
265    * @fd:             the file descriptor to remove from multifdsink
266    *
267    * Remove the given open file descriptor from multifdsink.
268    */
269   gst_multi_fd_sink_signals[SIGNAL_REMOVE] =
270       g_signal_new ("remove", G_TYPE_FROM_CLASS (klass),
271       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
272       G_STRUCT_OFFSET (GstMultiFdSinkClass, remove), NULL, NULL,
273       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
274   /**
275    * GstMultiFdSink::remove-flush:
276    * @gstmultifdsink: the multifdsink element to emit this signal on
277    * @fd:             the file descriptor to remove from multifdsink
278    *
279    * Remove the given open file descriptor from multifdsink after flushing all
280    * the pending data to the fd.
281    */
282   gst_multi_fd_sink_signals[SIGNAL_REMOVE_FLUSH] =
283       g_signal_new ("remove-flush", G_TYPE_FROM_CLASS (klass),
284       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
285       G_STRUCT_OFFSET (GstMultiFdSinkClass, remove_flush), NULL, NULL,
286       g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
287 
288   /**
289    * GstMultiFdSink::get-stats:
290    * @gstmultifdsink: the multifdsink element to emit this signal on
291    * @fd:             the file descriptor to get stats of from multifdsink
292    *
293    * Get statistics about @fd. This function returns a #GstStructure to ease
294    * automatic wrapping for bindings.
295    *
296    * Returns: a #GstStructure with the statistics. The structures
297    *     contains guint64 values that represent respectively: total
298    *     number of bytes sent (bytes-sent), time when the client was
299    *     added (connect-time), time when the client was
300    *     disconnected/removed (disconnect-time), time the client
301    *     is/was active (connect-duration), last activity time (in
302    *     epoch seconds) (last-activity-time), number of buffers
303    *     dropped (buffers-dropped), the timestamp of the first buffer
304    *     (first-buffer-ts) and of the last buffer (last-buffer-ts).
305    *     All times are expressed in nanoseconds (GstClockTime).  The
306    *     structure can be empty if the client was not found.
307    */
308   gst_multi_fd_sink_signals[SIGNAL_GET_STATS] =
309       g_signal_new ("get-stats", G_TYPE_FROM_CLASS (klass),
310       G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
311       G_STRUCT_OFFSET (GstMultiFdSinkClass, get_stats), NULL, NULL,
312       g_cclosure_marshal_generic, GST_TYPE_STRUCTURE, 1, G_TYPE_INT);
313 
314   /**
315    * GstMultiFdSink::client-added:
316    * @gstmultifdsink: the multifdsink element that emitted this signal
317    * @fd:             the file descriptor that was added to multifdsink
318    *
319    * The given file descriptor was added to multifdsink. This signal will
320    * be emitted from the streaming thread so application should be prepared
321    * for that.
322    */
323   gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED] =
324       g_signal_new ("client-added", G_TYPE_FROM_CLASS (klass),
325       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE,
326       1, G_TYPE_INT);
327   /**
328    * GstMultiFdSink::client-removed:
329    * @gstmultifdsink: the multifdsink element that emitted this signal
330    * @fd:             the file descriptor that is to be removed from multifdsink
331    * @status:         the reason why the client was removed
332    *
333    * The given file descriptor is about to be removed from multifdsink. This
334    * signal will be emitted from the streaming thread so applications should
335    * be prepared for that.
336    *
337    * @gstmultifdsink still holds a handle to @fd so it is possible to call
338    * the get-stats signal from this callback. For the same reason it is
339    * not safe to close() and reuse @fd in this callback.
340    */
341   gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED] =
342       g_signal_new ("client-removed", G_TYPE_FROM_CLASS (klass),
343       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
344       G_TYPE_NONE, 2, G_TYPE_INT, GST_TYPE_CLIENT_STATUS);
345   /**
346    * GstMultiFdSink::client-fd-removed:
347    * @gstmultifdsink: the multifdsink element that emitted this signal
348    * @fd:             the file descriptor that was removed from multifdsink
349    *
350    * The given file descriptor was removed from multifdsink. This signal will
351    * be emitted from the streaming thread so applications should be prepared
352    * for that.
353    *
354    * In this callback, @gstmultifdsink has removed all the information
355    * associated with @fd and it is therefore not possible to call get-stats
356    * with @fd. It is however safe to close() and reuse @fd in the callback.
357    */
358   gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED] =
359       g_signal_new ("client-fd-removed", G_TYPE_FROM_CLASS (klass),
360       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
361       G_TYPE_NONE, 1, G_TYPE_INT);
362 
363   gst_element_class_set_static_metadata (gstelement_class,
364       "Multi filedescriptor sink", "Sink/Network",
365       "Send data to multiple filedescriptors",
366       "Thomas Vander Stichele <thomas at apestaart dot org>, "
367       "Wim Taymans <wim@fluendo.com>");
368 
369   klass->add = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add);
370   klass->add_full = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_add_full);
371   klass->remove = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove);
372   klass->remove_flush = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_remove_flush);
373   klass->get_stats = GST_DEBUG_FUNCPTR (gst_multi_fd_sink_get_stats);
374 
375   gstmultihandlesink_class->emit_client_added =
376       gst_multi_fd_sink_emit_client_added;
377   gstmultihandlesink_class->emit_client_removed =
378       gst_multi_fd_sink_emit_client_removed;
379 
380   gstmultihandlesink_class->stop_pre =
381       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_pre);
382   gstmultihandlesink_class->stop_post =
383       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_stop_post);
384   gstmultihandlesink_class->start_pre =
385       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_start_pre);
386   gstmultihandlesink_class->thread =
387       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_thread);
388   gstmultihandlesink_class->new_client =
389       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_new_client);
390   gstmultihandlesink_class->client_free = gst_multi_fd_sink_client_free;
391   gstmultihandlesink_class->client_get_fd =
392       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_client_get_fd);
393   gstmultihandlesink_class->handle_debug =
394       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_handle_debug);
395   gstmultihandlesink_class->handle_hash_key =
396       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_handle_hash_key);
397   gstmultihandlesink_class->hash_changed =
398       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_changed);
399   gstmultihandlesink_class->hash_adding =
400       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_adding);
401   gstmultihandlesink_class->hash_removing =
402       GST_DEBUG_FUNCPTR (gst_multi_fd_sink_hash_removing);
403 
404   GST_DEBUG_CATEGORY_INIT (multifdsink_debug, "multifdsink", 0, "FD sink");
405 }
406 
407 static void
gst_multi_fd_sink_init(GstMultiFdSink * this)408 gst_multi_fd_sink_init (GstMultiFdSink * this)
409 {
410   GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (this);
411 
412   mhsink->handle_hash = g_hash_table_new (g_direct_hash, g_direct_equal);
413 
414   this->handle_read = DEFAULT_HANDLE_READ;
415 }
416 
417 /* methods to emit signals */
418 
419 static void
gst_multi_fd_sink_emit_client_added(GstMultiHandleSink * mhsink,GstMultiSinkHandle handle)420 gst_multi_fd_sink_emit_client_added (GstMultiHandleSink * mhsink,
421     GstMultiSinkHandle handle)
422 {
423   g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_ADDED], 0,
424       handle.fd);
425 }
426 
427 static void
gst_multi_fd_sink_emit_client_removed(GstMultiHandleSink * mhsink,GstMultiSinkHandle handle,GstClientStatus status)428 gst_multi_fd_sink_emit_client_removed (GstMultiHandleSink * mhsink,
429     GstMultiSinkHandle handle, GstClientStatus status)
430 {
431   g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_REMOVED], 0,
432       handle.fd, status);
433 }
434 
435 static void
gst_multi_fd_sink_client_free(GstMultiHandleSink * mhsink,GstMultiHandleClient * client)436 gst_multi_fd_sink_client_free (GstMultiHandleSink * mhsink,
437     GstMultiHandleClient * client)
438 {
439   g_signal_emit (mhsink, gst_multi_fd_sink_signals[SIGNAL_CLIENT_FD_REMOVED],
440       0, client->handle.fd);
441 }
442 
443 /* action signals */
444 
445 static void
gst_multi_fd_sink_add(GstMultiFdSink * sink,int fd)446 gst_multi_fd_sink_add (GstMultiFdSink * sink, int fd)
447 {
448   GstMultiSinkHandle handle;
449 
450   handle.fd = fd;
451   gst_multi_handle_sink_add (GST_MULTI_HANDLE_SINK_CAST (sink), handle);
452 }
453 
454 static void
gst_multi_fd_sink_add_full(GstMultiFdSink * sink,int fd,GstSyncMethod sync,GstFormat min_format,guint64 min_value,GstFormat max_format,guint64 max_value)455 gst_multi_fd_sink_add_full (GstMultiFdSink * sink, int fd,
456     GstSyncMethod sync, GstFormat min_format, guint64 min_value,
457     GstFormat max_format, guint64 max_value)
458 {
459   GstMultiSinkHandle handle;
460 
461   handle.fd = fd;
462   gst_multi_handle_sink_add_full (GST_MULTI_HANDLE_SINK_CAST (sink), handle,
463       sync, min_format, min_value, max_format, max_value);
464 }
465 
466 static void
gst_multi_fd_sink_remove(GstMultiFdSink * sink,int fd)467 gst_multi_fd_sink_remove (GstMultiFdSink * sink, int fd)
468 {
469   GstMultiSinkHandle handle;
470 
471   handle.fd = fd;
472   gst_multi_handle_sink_remove (GST_MULTI_HANDLE_SINK_CAST (sink), handle);
473 }
474 
475 static void
gst_multi_fd_sink_remove_flush(GstMultiFdSink * sink,int fd)476 gst_multi_fd_sink_remove_flush (GstMultiFdSink * sink, int fd)
477 {
478   GstMultiSinkHandle handle;
479 
480   handle.fd = fd;
481   gst_multi_handle_sink_remove_flush (GST_MULTI_HANDLE_SINK_CAST (sink),
482       handle);
483 }
484 
485 static GstStructure *
gst_multi_fd_sink_get_stats(GstMultiFdSink * sink,int fd)486 gst_multi_fd_sink_get_stats (GstMultiFdSink * sink, int fd)
487 {
488   GstMultiSinkHandle handle;
489 
490   handle.fd = fd;
491   return gst_multi_handle_sink_get_stats (GST_MULTI_HANDLE_SINK_CAST (sink),
492       handle);
493 }
494 
495 /* vfuncs */
496 
497 static GstMultiHandleClient *
gst_multi_fd_sink_new_client(GstMultiHandleSink * mhsink,GstMultiSinkHandle handle,GstSyncMethod sync_method)498 gst_multi_fd_sink_new_client (GstMultiHandleSink * mhsink,
499     GstMultiSinkHandle handle, GstSyncMethod sync_method)
500 {
501   struct stat statbuf;
502   GstTCPClient *client;
503   GstMultiHandleClient *mhclient;
504   GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
505   GstMultiHandleSinkClass *mhsinkclass =
506       GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
507 
508   /* create client datastructure */
509   client = g_new0 (GstTCPClient, 1);
510   mhclient = (GstMultiHandleClient *) client;
511 
512   mhclient->handle = handle;
513 
514   gst_poll_fd_init (&client->gfd);
515   client->gfd.fd = mhclient->handle.fd;
516 
517   gst_multi_handle_sink_client_init (mhclient, sync_method);
518   mhsinkclass->handle_debug (handle, mhclient->debug);
519 
520   /* set the socket to non blocking */
521   if (fcntl (handle.fd, F_SETFL, O_NONBLOCK) < 0) {
522     GST_ERROR_OBJECT (mhsink, "failed to make socket %s non-blocking: %s",
523         mhclient->debug, g_strerror (errno));
524   }
525 
526   /* we always read from a client */
527   gst_poll_add_fd (sink->fdset, &client->gfd);
528 
529   /* we don't try to read from write only fds */
530   if (sink->handle_read) {
531     gint flags;
532 
533     flags = fcntl (handle.fd, F_GETFL, 0);
534     if ((flags & O_ACCMODE) != O_WRONLY) {
535       gst_poll_fd_ctl_read (sink->fdset, &client->gfd, TRUE);
536     }
537   }
538   /* figure out the mode, can't use send() for non sockets */
539   if (fstat (handle.fd, &statbuf) == 0 && S_ISSOCK (statbuf.st_mode)) {
540     client->is_socket = TRUE;
541     gst_multi_handle_sink_setup_dscp_client (mhsink, mhclient);
542   }
543 
544   return mhclient;
545 }
546 
547 static int
gst_multi_fd_sink_client_get_fd(GstMultiHandleClient * client)548 gst_multi_fd_sink_client_get_fd (GstMultiHandleClient * client)
549 {
550   GstTCPClient *tclient = (GstTCPClient *) client;
551 
552   return tclient->gfd.fd;
553 }
554 
555 static void
gst_multi_fd_sink_handle_debug(GstMultiSinkHandle handle,gchar debug[30])556 gst_multi_fd_sink_handle_debug (GstMultiSinkHandle handle, gchar debug[30])
557 {
558   g_snprintf (debug, 30, "[fd %5d]", handle.fd);
559 }
560 
561 static gpointer
gst_multi_fd_sink_handle_hash_key(GstMultiSinkHandle handle)562 gst_multi_fd_sink_handle_hash_key (GstMultiSinkHandle handle)
563 {
564   return GINT_TO_POINTER (handle.fd);
565 }
566 
567 static void
gst_multi_fd_sink_hash_changed(GstMultiHandleSink * mhsink)568 gst_multi_fd_sink_hash_changed (GstMultiHandleSink * mhsink)
569 {
570   GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
571 
572   gst_poll_restart (sink->fdset);
573 }
574 
575 /* handle a read on a client fd,
576  * which either indicates a close or should be ignored
577  * returns FALSE if some error occured or the client closed. */
578 static gboolean
gst_multi_fd_sink_handle_client_read(GstMultiFdSink * sink,GstTCPClient * client)579 gst_multi_fd_sink_handle_client_read (GstMultiFdSink * sink,
580     GstTCPClient * client)
581 {
582   int avail, fd;
583   gboolean ret;
584   GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
585 
586   fd = client->gfd.fd;
587 
588   if (ioctl (fd, FIONREAD, &avail) < 0)
589     goto ioctl_failed;
590 
591   GST_DEBUG_OBJECT (sink, "%s select reports client read of %d bytes",
592       mhclient->debug, avail);
593 
594   ret = TRUE;
595 
596   if (avail == 0) {
597     /* client sent close, so remove it */
598     GST_DEBUG_OBJECT (sink, "%s client asked for close, removing",
599         mhclient->debug);
600     mhclient->status = GST_CLIENT_STATUS_CLOSED;
601     ret = FALSE;
602   } else if (avail < 0) {
603     GST_WARNING_OBJECT (sink, "%s avail < 0, removing", mhclient->debug);
604     mhclient->status = GST_CLIENT_STATUS_ERROR;
605     ret = FALSE;
606   } else {
607     guint8 dummy[512];
608     gint nread;
609 
610     /* just Read 'n' Drop, could also just drop the client as it's not supposed
611      * to write to us except for closing the socket, I guess it's because we
612      * like to listen to our customers. */
613     do {
614       /* this is the maximum we can read */
615       gint to_read = MIN (avail, 512);
616 
617       GST_DEBUG_OBJECT (sink, "%s client wants us to read %d bytes",
618           mhclient->debug, to_read);
619 
620       nread = read (fd, dummy, to_read);
621       if (nread < -1) {
622         GST_WARNING_OBJECT (sink, "%s could not read %d bytes: %s (%d)",
623             mhclient->debug, to_read, g_strerror (errno), errno);
624         mhclient->status = GST_CLIENT_STATUS_ERROR;
625         ret = FALSE;
626         break;
627       } else if (nread == 0) {
628         GST_WARNING_OBJECT (sink, "%s 0 bytes in read, removing",
629             mhclient->debug);
630         mhclient->status = GST_CLIENT_STATUS_ERROR;
631         ret = FALSE;
632         break;
633       }
634       avail -= nread;
635     }
636     while (avail > 0);
637   }
638   return ret;
639 
640   /* ERRORS */
641 ioctl_failed:
642   {
643     GST_WARNING_OBJECT (sink, "%s ioctl failed: %s (%d)",
644         mhclient->debug, g_strerror (errno), errno);
645     mhclient->status = GST_CLIENT_STATUS_ERROR;
646     return FALSE;
647   }
648 }
649 
650 /* Handle a write on a client,
651  * which indicates a read request from a client.
652  *
653  * For each client we maintain a queue of GstBuffers that contain the raw bytes
654  * we need to send to the client.
655  *
656  * We first check to see if we need to send streamheaders. If so, we queue them.
657  *
658  * Then we run into the main loop that tries to send as many buffers as
659  * possible. It will first exhaust the mhclient->sending queue and if the queue
660  * is empty, it will pick a buffer from the global queue.
661  *
662  * Sending the buffers from the mhclient->sending queue is basically writing
663  * the bytes to the socket and maintaining a count of the bytes that were
664  * sent. When the buffer is completely sent, it is removed from the
665  * mhclient->sending queue and we try to pick a new buffer for sending.
666  *
667  * When the sending returns a partial buffer we stop sending more data as
668  * the next send operation could block.
669  *
670  * This functions returns FALSE if some error occured.
671  */
672 static gboolean
gst_multi_fd_sink_handle_client_write(GstMultiFdSink * sink,GstTCPClient * client)673 gst_multi_fd_sink_handle_client_write (GstMultiFdSink * sink,
674     GstTCPClient * client)
675 {
676   gboolean more;
677   gboolean flushing;
678   GstClockTime now;
679   GTimeVal nowtv;
680   GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
681   GstMultiHandleSinkClass *mhsinkclass =
682       GST_MULTI_HANDLE_SINK_GET_CLASS (mhsink);
683   GstMultiHandleClient *mhclient = (GstMultiHandleClient *) client;
684   int fd = mhclient->handle.fd;
685 
686   flushing = mhclient->status == GST_CLIENT_STATUS_FLUSHING;
687 
688   more = TRUE;
689   do {
690     gint maxsize;
691 
692     g_get_current_time (&nowtv);
693     now = GST_TIMEVAL_TO_TIME (nowtv);
694 
695     if (!mhclient->sending) {
696       /* client is not working on a buffer */
697       if (mhclient->bufpos == -1) {
698         /* client is too fast, remove from write queue until new buffer is
699          * available */
700         /* FIXME: specific */
701         gst_poll_fd_ctl_write (sink->fdset, &client->gfd, FALSE);
702 
703         /* if we flushed out all of the client buffers, we can stop */
704         if (mhclient->flushcount == 0)
705           goto flushed;
706 
707         return TRUE;
708       } else {
709         /* client can pick a buffer from the global queue */
710         GstBuffer *buf;
711         GstClockTime timestamp;
712 
713         /* for new connections, we need to find a good spot in the
714          * bufqueue to start streaming from */
715         if (mhclient->new_connection && !flushing) {
716           gint position =
717               gst_multi_handle_sink_new_client_position (mhsink, mhclient);
718 
719           if (position >= 0) {
720             /* we got a valid spot in the queue */
721             mhclient->new_connection = FALSE;
722             mhclient->bufpos = position;
723           } else {
724             /* cannot send data to this client yet */
725             /* FIXME: specific */
726             gst_poll_fd_ctl_write (sink->fdset, &client->gfd, FALSE);
727             return TRUE;
728           }
729         }
730 
731         /* we flushed all remaining buffers, no need to get a new one */
732         if (mhclient->flushcount == 0)
733           goto flushed;
734 
735         /* grab buffer */
736         buf = g_array_index (mhsink->bufqueue, GstBuffer *, mhclient->bufpos);
737         mhclient->bufpos--;
738 
739         /* update stats */
740         timestamp = GST_BUFFER_TIMESTAMP (buf);
741         if (mhclient->first_buffer_ts == GST_CLOCK_TIME_NONE)
742           mhclient->first_buffer_ts = timestamp;
743         if (timestamp != -1)
744           mhclient->last_buffer_ts = timestamp;
745 
746         /* decrease flushcount */
747         if (mhclient->flushcount != -1)
748           mhclient->flushcount--;
749 
750         GST_LOG_OBJECT (sink, "%s client %p at position %d",
751             mhclient->debug, client, mhclient->bufpos);
752 
753         /* queueing a buffer will ref it */
754         mhsinkclass->client_queue_buffer (mhsink, mhclient, buf);
755 
756         /* need to start from the first byte for this new buffer */
757         mhclient->bufoffset = 0;
758       }
759     }
760 
761     /* see if we need to send something */
762     if (mhclient->sending) {
763       ssize_t wrote;
764       GstBuffer *head;
765       GstMapInfo info;
766       guint8 *data;
767 
768       /* pick first buffer from list */
769       head = GST_BUFFER (mhclient->sending->data);
770 
771       if (!gst_buffer_map (head, &info, GST_MAP_READ))
772         g_return_val_if_reached (FALSE);
773 
774       data = info.data;
775       maxsize = info.size - mhclient->bufoffset;
776 
777       /* FIXME: specific */
778       /* try to write the complete buffer */
779 #ifdef MSG_NOSIGNAL
780 #define FLAGS MSG_NOSIGNAL
781 #else
782 #define FLAGS 0
783 #endif
784       if (client->is_socket) {
785         wrote = send (fd, data + mhclient->bufoffset, maxsize, FLAGS);
786       } else {
787         wrote = write (fd, data + mhclient->bufoffset, maxsize);
788       }
789       gst_buffer_unmap (head, &info);
790 
791       if (wrote < 0) {
792         /* hmm error.. */
793         if (errno == EAGAIN) {
794           /* nothing serious, resource was unavailable, try again later */
795           more = FALSE;
796         } else if (errno == ECONNRESET) {
797           goto connection_reset;
798         } else {
799           goto write_error;
800         }
801       } else {
802         if (wrote < maxsize) {
803           /* partial write means that the client cannot read more and we should
804            * stop sending more */
805           GST_LOG_OBJECT (sink,
806               "partial write on %s of %" G_GSSIZE_FORMAT " bytes",
807               mhclient->debug, wrote);
808           mhclient->bufoffset += wrote;
809           more = FALSE;
810         } else {
811           /* complete buffer was written, we can proceed to the next one */
812           mhclient->sending = g_slist_remove (mhclient->sending, head);
813           gst_buffer_unref (head);
814           /* make sure we start from byte 0 for the next buffer */
815           mhclient->bufoffset = 0;
816         }
817         /* update stats */
818         mhclient->bytes_sent += wrote;
819         mhclient->last_activity_time = now;
820         mhsink->bytes_served += wrote;
821       }
822     }
823   } while (more);
824 
825   return TRUE;
826 
827   /* ERRORS */
828 flushed:
829   {
830     GST_DEBUG_OBJECT (sink, "%s flushed, removing", mhclient->debug);
831     mhclient->status = GST_CLIENT_STATUS_REMOVED;
832     return FALSE;
833   }
834 connection_reset:
835   {
836     GST_DEBUG_OBJECT (sink, "%s connection reset by peer, removing",
837         mhclient->debug);
838     mhclient->status = GST_CLIENT_STATUS_CLOSED;
839     return FALSE;
840   }
841 write_error:
842   {
843     GST_WARNING_OBJECT (sink,
844         "%s could not write, removing client: %s (%d)", mhclient->debug,
845         g_strerror (errno), errno);
846     mhclient->status = GST_CLIENT_STATUS_ERROR;
847     return FALSE;
848   }
849 }
850 
851 static void
gst_multi_fd_sink_hash_adding(GstMultiHandleSink * mhsink,GstMultiHandleClient * mhclient)852 gst_multi_fd_sink_hash_adding (GstMultiHandleSink * mhsink,
853     GstMultiHandleClient * mhclient)
854 {
855   GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
856   GstTCPClient *client = (GstTCPClient *) mhclient;
857 
858   gst_poll_fd_ctl_write (sink->fdset, &client->gfd, TRUE);
859 }
860 
861 static void
gst_multi_fd_sink_hash_removing(GstMultiHandleSink * mhsink,GstMultiHandleClient * mhclient)862 gst_multi_fd_sink_hash_removing (GstMultiHandleSink * mhsink,
863     GstMultiHandleClient * mhclient)
864 {
865   GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
866   GstTCPClient *client = (GstTCPClient *) mhclient;
867 
868   gst_poll_remove_fd (sink->fdset, &client->gfd);
869 }
870 
871 
872 /* Handle the clients. Basically does a blocking select for one
873  * of the client fds to become read or writable. We also have a
874  * filedescriptor to receive commands on that we need to check.
875  *
876  * After going out of the select call, we read and write to all
877  * clients that can do so. Badly behaving clients are put on a
878  * garbage list and removed.
879  */
880 static void
gst_multi_fd_sink_handle_clients(GstMultiFdSink * sink)881 gst_multi_fd_sink_handle_clients (GstMultiFdSink * sink)
882 {
883   int result;
884   GList *clients, *next;
885   gboolean try_again;
886   GstMultiFdSinkClass *fclass;
887   guint cookie;
888   GstMultiHandleSink *mhsink = GST_MULTI_HANDLE_SINK (sink);
889   int fd;
890 
891 
892   fclass = GST_MULTI_FD_SINK_GET_CLASS (sink);
893 
894   do {
895     try_again = FALSE;
896 
897     /* check for:
898      * - server socket input (ie, new client connections)
899      * - client socket input (ie, clients saying goodbye)
900      * - client socket output (ie, client reads)          */
901     GST_LOG_OBJECT (sink, "waiting on action on fdset");
902 
903     result =
904         gst_poll_wait (sink->fdset,
905         mhsink->timeout != 0 ? mhsink->timeout : GST_CLOCK_TIME_NONE);
906 
907     /* Handle the special case in which the sink is not receiving more buffers
908      * and will not disconnect inactive client in the streaming thread. */
909     if (G_UNLIKELY (result == 0)) {
910       GstClockTime now;
911       GTimeVal nowtv;
912 
913       g_get_current_time (&nowtv);
914       now = GST_TIMEVAL_TO_TIME (nowtv);
915 
916       CLIENTS_LOCK (mhsink);
917       for (clients = mhsink->clients; clients; clients = next) {
918         GstTCPClient *client;
919         GstMultiHandleClient *mhclient;
920 
921         client = (GstTCPClient *) clients->data;
922         mhclient = (GstMultiHandleClient *) client;
923         next = g_list_next (clients);
924         if (mhsink->timeout > 0
925             && now - mhclient->last_activity_time > mhsink->timeout) {
926           mhclient->status = GST_CLIENT_STATUS_SLOW;
927           gst_multi_handle_sink_remove_client_link (mhsink, clients);
928         }
929       }
930       CLIENTS_UNLOCK (mhsink);
931       return;
932     } else if (result < 0) {
933       GST_WARNING_OBJECT (sink, "wait failed: %s (%d)", g_strerror (errno),
934           errno);
935       if (errno == EBADF) {
936         /* ok, so one or more of the fds is invalid. We loop over them to find
937          * the ones that give an error to the F_GETFL fcntl. */
938         CLIENTS_LOCK (mhsink);
939       restart:
940         cookie = mhsink->clients_cookie;
941         for (clients = mhsink->clients; clients; clients = next) {
942           GstTCPClient *client;
943           GstMultiHandleClient *mhclient;
944           long flags;
945           int res;
946 
947           if (cookie != mhsink->clients_cookie) {
948             GST_DEBUG_OBJECT (sink, "Cookie changed finding bad fd");
949             goto restart;
950           }
951 
952           client = (GstTCPClient *) clients->data;
953           mhclient = (GstMultiHandleClient *) client;
954           next = g_list_next (clients);
955 
956           fd = client->gfd.fd;
957 
958           res = fcntl (fd, F_GETFL, &flags);
959           if (res == -1) {
960             GST_WARNING_OBJECT (sink, "fcntl failed for %d, removing: %s (%d)",
961                 fd, g_strerror (errno), errno);
962             if (errno == EBADF) {
963               mhclient->status = GST_CLIENT_STATUS_ERROR;
964               /* releases the CLIENTS lock */
965               gst_multi_handle_sink_remove_client_link (mhsink, clients);
966             }
967           }
968         }
969         CLIENTS_UNLOCK (mhsink);
970         /* after this, go back in the select loop as the read/writefds
971          * are not valid */
972         try_again = TRUE;
973       } else if (errno == EINTR) {
974         /* interrupted system call, just redo the wait */
975         try_again = TRUE;
976       } else if (errno == EBUSY) {
977         /* the call to gst_poll_wait() was flushed */
978         return;
979       } else {
980         /* this is quite bad... */
981         GST_ELEMENT_ERROR (sink, RESOURCE, READ, (NULL),
982             ("select failed: %s (%d)", g_strerror (errno), errno));
983         return;
984       }
985     } else {
986       GST_LOG_OBJECT (sink, "wait done: %d sockets with events", result);
987     }
988   } while (try_again);
989 
990   /* subclasses can check fdset with this virtual function */
991   if (fclass->wait)
992     fclass->wait (sink, sink->fdset);
993 
994   /* Check the clients */
995   CLIENTS_LOCK (mhsink);
996 
997 restart2:
998   cookie = mhsink->clients_cookie;
999   for (clients = mhsink->clients; clients; clients = next) {
1000     GstTCPClient *client;
1001     GstMultiHandleClient *mhclient;
1002 
1003     if (mhsink->clients_cookie != cookie) {
1004       GST_DEBUG_OBJECT (sink, "Restarting loop, cookie out of date");
1005       goto restart2;
1006     }
1007 
1008     client = (GstTCPClient *) clients->data;
1009     mhclient = (GstMultiHandleClient *) client;
1010     next = g_list_next (clients);
1011 
1012     if (mhclient->status != GST_CLIENT_STATUS_FLUSHING
1013         && mhclient->status != GST_CLIENT_STATUS_OK) {
1014       gst_multi_handle_sink_remove_client_link (mhsink, clients);
1015       continue;
1016     }
1017 
1018     if (gst_poll_fd_has_closed (sink->fdset, &client->gfd)) {
1019       mhclient->status = GST_CLIENT_STATUS_CLOSED;
1020       gst_multi_handle_sink_remove_client_link (mhsink, clients);
1021       continue;
1022     }
1023     if (gst_poll_fd_has_error (sink->fdset, &client->gfd)) {
1024       GST_WARNING_OBJECT (sink, "gst_poll_fd_has_error for %d", client->gfd.fd);
1025       mhclient->status = GST_CLIENT_STATUS_ERROR;
1026       gst_multi_handle_sink_remove_client_link (mhsink, clients);
1027       continue;
1028     }
1029     if (gst_poll_fd_can_read (sink->fdset, &client->gfd)) {
1030       /* handle client read */
1031       if (!gst_multi_fd_sink_handle_client_read (sink, client)) {
1032         gst_multi_handle_sink_remove_client_link (mhsink, clients);
1033         continue;
1034       }
1035     }
1036     if (gst_poll_fd_can_write (sink->fdset, &client->gfd)) {
1037       /* handle client write */
1038       if (!gst_multi_fd_sink_handle_client_write (sink, client)) {
1039         gst_multi_handle_sink_remove_client_link (mhsink, clients);
1040         continue;
1041       }
1042     }
1043   }
1044   CLIENTS_UNLOCK (mhsink);
1045 }
1046 
1047 /* we handle the client communication in another thread so that we do not block
1048  * the gstreamer thread while we select() on the client fds */
1049 static gpointer
gst_multi_fd_sink_thread(GstMultiHandleSink * mhsink)1050 gst_multi_fd_sink_thread (GstMultiHandleSink * mhsink)
1051 {
1052   GstMultiFdSink *sink = GST_MULTI_FD_SINK (mhsink);
1053 
1054   while (mhsink->running) {
1055     gst_multi_fd_sink_handle_clients (sink);
1056   }
1057   return NULL;
1058 }
1059 
1060 static void
gst_multi_fd_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1061 gst_multi_fd_sink_set_property (GObject * object, guint prop_id,
1062     const GValue * value, GParamSpec * pspec)
1063 {
1064   GstMultiFdSink *multifdsink;
1065 
1066   multifdsink = GST_MULTI_FD_SINK (object);
1067 
1068   switch (prop_id) {
1069     case PROP_HANDLE_READ:
1070       multifdsink->handle_read = g_value_get_boolean (value);
1071       break;
1072 
1073     default:
1074       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1075       break;
1076   }
1077 }
1078 
1079 static void
gst_multi_fd_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1080 gst_multi_fd_sink_get_property (GObject * object, guint prop_id, GValue * value,
1081     GParamSpec * pspec)
1082 {
1083   GstMultiFdSink *multifdsink;
1084 
1085   multifdsink = GST_MULTI_FD_SINK (object);
1086 
1087   switch (prop_id) {
1088     case PROP_HANDLE_READ:
1089       g_value_set_boolean (value, multifdsink->handle_read);
1090       break;
1091 
1092     default:
1093       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1094       break;
1095   }
1096 }
1097 
1098 static gboolean
gst_multi_fd_sink_start_pre(GstMultiHandleSink * mhsink)1099 gst_multi_fd_sink_start_pre (GstMultiHandleSink * mhsink)
1100 {
1101   GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
1102 
1103   GST_INFO_OBJECT (mfsink, "starting");
1104   if ((mfsink->fdset = gst_poll_new (TRUE)) == NULL)
1105     goto socket_pair;
1106 
1107   return TRUE;
1108 
1109   /* ERRORS */
1110 socket_pair:
1111   {
1112     GST_ELEMENT_ERROR (mfsink, RESOURCE, OPEN_READ_WRITE, (NULL),
1113         GST_ERROR_SYSTEM);
1114     return FALSE;
1115   }
1116 }
1117 
1118 static gboolean
multifdsink_hash_remove(gpointer key,gpointer value,gpointer data)1119 multifdsink_hash_remove (gpointer key, gpointer value, gpointer data)
1120 {
1121   return TRUE;
1122 }
1123 
1124 static void
gst_multi_fd_sink_stop_pre(GstMultiHandleSink * mhsink)1125 gst_multi_fd_sink_stop_pre (GstMultiHandleSink * mhsink)
1126 {
1127   GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
1128 
1129   gst_poll_set_flushing (mfsink->fdset, TRUE);
1130 }
1131 
1132 static void
gst_multi_fd_sink_stop_post(GstMultiHandleSink * mhsink)1133 gst_multi_fd_sink_stop_post (GstMultiHandleSink * mhsink)
1134 {
1135   GstMultiFdSink *mfsink = GST_MULTI_FD_SINK (mhsink);
1136 
1137   if (mfsink->fdset) {
1138     gst_poll_free (mfsink->fdset);
1139     mfsink->fdset = NULL;
1140   }
1141   g_hash_table_foreach_remove (mhsink->handle_hash, multifdsink_hash_remove,
1142       mfsink);
1143 }
1144