multihandle: rename num-fds/-sockets to num-handles
[platform/upstream/gstreamer.git] / gst / tcp / gstmultihandlesink.h
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) <2011> Collabora Ltd.
5  *     Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23
24 #ifndef __GST_MULTI_HANDLE_SINK_H__
25 #define __GST_MULTI_HANDLE_SINK_H__
26
27 #include <gst/gst.h>
28 #include <gst/base/gstbasesink.h>
29 #include <gio/gio.h>
30
31 G_BEGIN_DECLS
32
33 #define GST_TYPE_MULTI_HANDLE_SINK \
34   (gst_multi_handle_sink_get_type())
35 #define GST_MULTI_HANDLE_SINK(obj) \
36   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTI_HANDLE_SINK,GstMultiHandleSink))
37 #define GST_MULTI_HANDLE_SINK_CLASS(klass) \
38   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MULTI_HANDLE_SINK,GstMultiHandleSinkClass))
39 #define GST_IS_MULTI_HANDLE_SINK(obj) \
40   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTI_HANDLE_SINK))
41 #define GST_IS_MULTI_HANDLE_SINK_CLASS(klass) \
42   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MULTI_HANDLE_SINK))
43 #define GST_MULTI_HANDLE_SINK_GET_CLASS(klass) \
44   (G_TYPE_INSTANCE_GET_CLASS ((klass), GST_TYPE_MULTI_HANDLE_SINK, GstMultiHandleSinkClass))
45
46
47 typedef struct _GstMultiHandleSink GstMultiHandleSink;
48 typedef struct _GstMultiHandleSinkClass GstMultiHandleSinkClass;
49
50 typedef enum {
51   GST_MULTI_HANDLE_SINK_OPEN             = (GST_ELEMENT_FLAG_LAST << 0),
52
53   GST_MULTI_HANDLE_SINK_FLAG_LAST        = (GST_ELEMENT_FLAG_LAST << 2)
54 } GstMultiHandleSinkFlags;
55
56 /**
57  * GstRecoverPolicy:
58  * @GST_RECOVER_POLICY_NONE             : no recovering is done
59  * @GST_RECOVER_POLICY_RESYNC_LATEST    : client is moved to last buffer
60  * @GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT: client is moved to the soft limit
61  * @GST_RECOVER_POLICY_RESYNC_KEYFRAME  : client is moved to latest keyframe
62  *
63  * Possible values for the recovery procedure to use when a client consumes
64  * data too slow and has a backlag of more that soft-limit buffers.
65  */
66 typedef enum
67 {
68   GST_RECOVER_POLICY_NONE,
69   GST_RECOVER_POLICY_RESYNC_LATEST,
70   GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT,
71   GST_RECOVER_POLICY_RESYNC_KEYFRAME
72 } GstRecoverPolicy;
73
74 /**
75  * GstSyncMethod:
76  * @GST_SYNC_METHOD_LATEST              : client receives most recent buffer
77  * @GST_SYNC_METHOD_NEXT_KEYFRAME       : client receives next keyframe
78  * @GST_SYNC_METHOD_LATEST_KEYFRAME     : client receives latest keyframe (burst)
79  * @GST_SYNC_METHOD_BURST               : client receives specific amount of data
80  * @GST_SYNC_METHOD_BURST_KEYFRAME      : client receives specific amount of data 
81  *                                        starting from latest keyframe
82  * @GST_SYNC_METHOD_BURST_WITH_KEYFRAME : client receives specific amount of data from
83  *                                        a keyframe, or if there is not enough data after
84  *                                        the keyframe, starting before the keyframe
85  *
86  * This enum defines the selection of the first buffer that is sent
87  * to a new client.
88  */
89 typedef enum
90 {
91   GST_SYNC_METHOD_LATEST,
92   GST_SYNC_METHOD_NEXT_KEYFRAME,
93   GST_SYNC_METHOD_LATEST_KEYFRAME,
94   GST_SYNC_METHOD_BURST,
95   GST_SYNC_METHOD_BURST_KEYFRAME,
96   GST_SYNC_METHOD_BURST_WITH_KEYFRAME
97 } GstSyncMethod;
98
99 /**
100  * GstClientStatus:
101  * @GST_CLIENT_STATUS_OK       : client is ok
102  * @GST_CLIENT_STATUS_CLOSED   : client closed the socket
103  * @GST_CLIENT_STATUS_REMOVED  : client is removed
104  * @GST_CLIENT_STATUS_SLOW     : client is too slow
105  * @GST_CLIENT_STATUS_ERROR    : client is in error
106  * @GST_CLIENT_STATUS_DUPLICATE: same client added twice
107  * @GST_CLIENT_STATUS_FLUSHING : client is flushing out the remaining buffers.
108  *
109  * This specifies the reason why a client was removed from
110  * multisocketsink and is received in the "client-removed" signal.
111  */
112 typedef enum
113 {
114   GST_CLIENT_STATUS_OK          = 0,
115   GST_CLIENT_STATUS_CLOSED      = 1,
116   GST_CLIENT_STATUS_REMOVED     = 2,
117   GST_CLIENT_STATUS_SLOW        = 3,
118   GST_CLIENT_STATUS_ERROR       = 4,
119   GST_CLIENT_STATUS_DUPLICATE   = 5,
120   GST_CLIENT_STATUS_FLUSHING    = 6
121 } GstClientStatus;
122
123 // FIXME: is it better to use GSocket * or a gpointer here ?
124 typedef union
125 {
126   int fd;
127   GSocket *socket;
128 } GstMultiSinkHandle;
129
130 /* structure for a client
131  */
132 typedef struct {
133   GstMultiSinkHandle handle;
134
135   gchar debug[30];              /* a debug string used in debug calls to
136                                    identify the client */
137   gint bufpos;                  /* position of this client in the global queue */
138   gint flushcount;              /* the remaining number of buffers to flush out or -1 if the 
139                                    client is not flushing. */
140
141   GstClientStatus status;
142
143   GSList *sending;              /* the buffers we need to send */
144   gint bufoffset;               /* offset in the first buffer */
145
146   gboolean discont;
147
148   gboolean new_connection;
149   gboolean currently_removing;
150
151
152   /* method to sync client when connecting */
153   GstSyncMethod sync_method;
154   GstFormat     burst_min_format;
155   guint64       burst_min_value;
156   GstFormat     burst_max_format;
157   guint64       burst_max_value;
158
159   GstCaps *caps;                /* caps of last queued buffer */
160
161   /* stats */
162   guint64 bytes_sent;
163   guint64 connect_time;
164   guint64 disconnect_time;
165   guint64 last_activity_time;
166   guint64 dropped_buffers;
167   guint64 avg_queue_size;
168   guint64 first_buffer_ts;
169   guint64 last_buffer_ts;
170 } GstMultiHandleClient;
171
172 // FIXME: remove cast ?
173 #define CLIENTS_LOCK_INIT(mhsink)       (g_rec_mutex_init(&(GST_MULTI_HANDLE_SINK(mhsink))->clientslock))
174 #define CLIENTS_LOCK_CLEAR(mhsink)      (g_rec_mutex_clear(&(GST_MULTI_HANDLE_SINK(mhsink))->clientslock))
175 #define CLIENTS_LOCK(mhsink)            (g_rec_mutex_lock(&(GST_MULTI_HANDLE_SINK(mhsink))->clientslock))
176 #define CLIENTS_UNLOCK(mhsink)          (g_rec_mutex_unlock(&(GST_MULTI_HANDLE_SINK(mhsink))->clientslock))
177
178 // FIXME: internalize in .c file ?
179 gint
180 find_syncframe (GstMultiHandleSink * sink, gint idx, gint direction);
181 #define find_next_syncframe(s,i)        find_syncframe(s,i,1)
182 #define find_prev_syncframe(s,i)        find_syncframe(s,i,-1)
183 gboolean is_sync_frame (GstMultiHandleSink * sink, GstBuffer * buffer);
184 gboolean gst_multi_handle_sink_stop (GstBaseSink * bsink);
185 gboolean gst_multi_handle_sink_start (GstBaseSink * bsink);
186 void gst_multi_handle_sink_setup_dscp (GstMultiHandleSink * mhsink);
187 gint gst_multi_handle_sink_setup_dscp_client (GstMultiHandleSink * sink, GstMultiHandleClient * client);
188 gint get_buffers_max (GstMultiHandleSink * sink, gint64 max);
189 gint
190 gst_multi_handle_sink_recover_client (GstMultiHandleSink * sink,
191     GstMultiHandleClient * client);
192 gint
193 gst_multi_handle_sink_new_client (GstMultiHandleSink * sink,
194     GstMultiHandleClient * client);
195 gboolean
196 find_limits (GstMultiHandleSink * sink,
197     gint * min_idx, gint bytes_min, gint buffers_min, gint64 time_min,
198     gint * max_idx, gint bytes_max, gint buffers_max, gint64 time_max);
199
200
201 /**
202  * GstMultiHandleSink:
203  *
204  * The multisocketsink object structure.
205  */
206 struct _GstMultiHandleSink {
207   GstBaseSink element;
208
209   /*< private >*/
210   guint64 bytes_to_serve; /* how much bytes we must serve */
211   guint64 bytes_served; /* how much bytes have we served */
212
213   GRecMutex clientslock;  /* lock to protect the clients list */
214   GList *clients;       /* list of clients we are serving */
215   guint clients_cookie; /* Cookie to detect changes to the clients list */
216
217   GHashTable *handle_hash;  /* index of handle -> GstMultiHandleClient */
218
219   GMainContext *main_context;
220   GCancellable *cancellable;
221
222   GSList *streamheader; /* GSList of GstBuffers to use as streamheader */
223   gboolean previous_buffer_in_caps;
224
225   gint qos_dscp;
226
227   GArray *bufqueue;     /* global queue of buffers */
228
229   gboolean running;     /* the thread state */
230   GThread *thread;      /* the sender thread */
231
232   /* these values are used to check if a client is reading fast
233    * enough and to control receovery */
234   GstFormat unit_format;/* the format of the units */
235   gint64 units_max;       /* max units to queue for a client */
236   gint64 units_soft_max;  /* max units a client can lag before recovery starts */
237   GstRecoverPolicy recover_policy;
238   GstClockTime timeout; /* max amount of nanoseconds to remain idle */
239
240   GstSyncMethod def_sync_method;    /* what method to use for connecting clients */
241   GstFormat     def_burst_format;
242   guint64       def_burst_value;
243
244   /* these values are used to control the amount of data
245    * kept in the queues. It allows clients to perform a burst
246    * on connect. */
247   gint   bytes_min;     /* min number of bytes to queue */
248   gint64 time_min;      /* min time to queue */
249   gint   buffers_min;   /* min number of buffers to queue */
250
251   gboolean resend_streamheader; /* resend streamheader if it changes */
252
253   /* stats */
254   gint buffers_queued;  /* number of queued buffers */
255   gint bytes_queued;    /* number of queued bytes */
256   gint time_queued;     /* number of queued time */
257
258   guint8 header_flags;
259 };
260
261 struct _GstMultiHandleSinkClass {
262   GstBaseSinkClass parent_class;
263
264   /* element methods */
265   void          (*add)          (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
266   void          (*add_full)     (GstMultiHandleSink *sink, GstMultiSinkHandle handle, GstSyncMethod sync,
267                                  GstFormat format, guint64 value, 
268                                  GstFormat max_format, guint64 max_value);
269   void          (*remove)       (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
270   void          (*remove_flush) (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
271   void          (*clear)        (GstMultiHandleSink *sink);
272   void          (*clear_post)   (GstMultiHandleSink *sink);
273   void          (*stop_pre)     (GstMultiHandleSink *sink);
274   void          (*stop_post)    (GstMultiHandleSink *sink);
275   gboolean      (*start_pre)    (GstMultiHandleSink *sink);
276   gpointer      (*thread)       (GstMultiHandleSink *sink);
277   void          (*queue_buffer) (GstMultiHandleSink *sink,
278                                  GstBuffer *buffer);
279   gboolean      (*client_queue_buffer)
280                                 (GstMultiHandleSink *sink,
281                                  GstMultiHandleClient *client,
282                                  GstBuffer *buffer);
283   int           (*client_get_fd)
284                                 (GstMultiHandleClient *client);
285   void          (*handle_debug) (GstMultiSinkHandle handle, gchar debug[30]);
286
287
288   GstStructure* (*get_stats)    (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
289   void          (*remove_client_link) (GstMultiHandleSink * sink, GList * link);
290
291
292   /* vtable */
293   gboolean (*init)   (GstMultiHandleSink *sink);
294   gboolean (*close)  (GstMultiHandleSink *sink);
295   void (*removed) (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
296
297   /* signals */
298   void (*client_added) (GstElement *element, GstMultiSinkHandle handle);
299   void (*client_removed) (GstElement *element, GstMultiSinkHandle handle, GstClientStatus status);
300   void (*client_socket_removed) (GstElement *element, GstMultiSinkHandle handle);
301 };
302
303 GType gst_multi_handle_sink_get_type (void);
304
305 void          gst_multi_handle_sink_add          (GstMultiHandleSink *sink, GSocket *socket);
306 void          gst_multi_handle_sink_add_full     (GstMultiHandleSink *sink, GSocket *socket, GstSyncMethod sync, 
307                                               GstFormat min_format, guint64 min_value,
308                                               GstFormat max_format, guint64 max_value);
309 void          gst_multi_handle_sink_remove       (GstMultiHandleSink *sink, GSocket *socket);
310 void          gst_multi_handle_sink_remove_flush (GstMultiHandleSink *sink, GSocket *socket);
311 GstStructure*  gst_multi_handle_sink_get_stats    (GstMultiHandleSink *sink, GSocket *socket);
312
313 void gst_multi_handle_sink_client_init (GstMultiHandleClient * client, GstSyncMethod sync_method);
314
315 // FIXME: make static again after refactoring
316 #define GST_TYPE_RECOVER_POLICY (gst_multi_handle_sink_recover_policy_get_type())
317 GType
318 gst_multi_handle_sink_recover_policy_get_type (void);
319 #define GST_TYPE_SYNC_METHOD (gst_multi_handle_sink_sync_method_get_type())
320 GType
321 gst_multi_handle_sink_sync_method_get_type (void);
322 #define GST_TYPE_CLIENT_STATUS (gst_multi_handle_sink_client_status_get_type())
323 GType
324 gst_multi_handle_sink_client_status_get_type (void);
325
326
327 G_END_DECLS
328
329 #endif /* __GST_MULTI_HANDLE_SINK_H__ */