Merge branch 'master' into 0.11
[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   gpointer pointer;
127   int fd;
128   GSocket *socket;
129 } GstMultiSinkHandle;
130
131 /* structure for a client
132  */
133 typedef struct {
134   GstMultiSinkHandle handle;
135
136   gchar debug[30];              /* a debug string used in debug calls to
137                                    identify the client */
138   gint bufpos;                  /* position of this client in the global queue */
139   gint flushcount;              /* the remaining number of buffers to flush out or -1 if the 
140                                    client is not flushing. */
141
142   GstClientStatus status;
143
144   GSList *sending;              /* the buffers we need to send */
145   gint bufoffset;               /* offset in the first buffer */
146
147   gboolean discont;
148
149   gboolean new_connection;
150   gboolean currently_removing;
151
152
153   /* method to sync client when connecting */
154   GstSyncMethod sync_method;
155   GstFormat     burst_min_format;
156   guint64       burst_min_value;
157   GstFormat     burst_max_format;
158   guint64       burst_max_value;
159
160   GstCaps *caps;                /* caps of last queued buffer */
161
162   /* stats */
163   guint64 bytes_sent;
164   guint64 connect_time;
165   guint64 disconnect_time;
166   guint64 last_activity_time;
167   guint64 dropped_buffers;
168   guint64 avg_queue_size;
169   guint64 first_buffer_ts;
170   guint64 last_buffer_ts;
171 } GstMultiHandleClient;
172
173 #define CLIENTS_LOCK_INIT(mhsink)       (g_rec_mutex_init(&(mhsink)->clientslock))
174 #define CLIENTS_LOCK_CLEAR(mhsink)      (g_rec_mutex_clear(&(mhsink)->clientslock))
175 #define CLIENTS_LOCK(mhsink)            (g_rec_mutex_lock(&(mhsink)->clientslock))
176 #define CLIENTS_UNLOCK(mhsink)          (g_rec_mutex_unlock(&(mhsink)->clientslock))
177
178 gint gst_multi_handle_sink_setup_dscp_client (GstMultiHandleSink * sink, GstMultiHandleClient * client);
179 gint
180 gst_multi_handle_sink_new_client_position (GstMultiHandleSink * sink,
181     GstMultiHandleClient * client);
182
183 /**
184  * GstMultiHandleSink:
185  *
186  * The multisocketsink object structure.
187  */
188 struct _GstMultiHandleSink {
189   GstBaseSink element;
190
191   /*< private >*/
192   guint64 bytes_to_serve; /* how much bytes we must serve */
193   guint64 bytes_served; /* how much bytes have we served */
194
195   GRecMutex clientslock;  /* lock to protect the clients list */
196   GList *clients;       /* list of clients we are serving */
197   guint clients_cookie; /* Cookie to detect changes to the clients list */
198
199   GHashTable *handle_hash;  /* index of handle -> GstMultiHandleClient */
200
201   GMainContext *main_context;
202   GCancellable *cancellable;
203
204   GSList *streamheader; /* GSList of GstBuffers to use as streamheader */
205   gboolean previous_buffer_in_caps;
206
207   gint qos_dscp;
208
209   GArray *bufqueue;     /* global queue of buffers */
210
211   gboolean running;     /* the thread state */
212   GThread *thread;      /* the sender thread */
213
214   /* these values are used to check if a client is reading fast
215    * enough and to control receovery */
216   GstFormat unit_format;/* the format of the units */
217   gint64 units_max;       /* max units to queue for a client */
218   gint64 units_soft_max;  /* max units a client can lag before recovery starts */
219   GstRecoverPolicy recover_policy;
220   GstClockTime timeout; /* max amount of nanoseconds to remain idle */
221
222   GstSyncMethod def_sync_method;    /* what method to use for connecting clients */
223   GstFormat     def_burst_format;
224   guint64       def_burst_value;
225
226   /* these values are used to control the amount of data
227    * kept in the queues. It allows clients to perform a burst
228    * on connect. */
229   gint   bytes_min;     /* min number of bytes to queue */
230   gint64 time_min;      /* min time to queue */
231   gint   buffers_min;   /* min number of buffers to queue */
232
233   gboolean resend_streamheader; /* resend streamheader if it changes */
234
235   /* stats */
236   gint buffers_queued;  /* number of queued buffers */
237   gint bytes_queued;    /* number of queued bytes */
238   gint time_queued;     /* number of queued time */
239 };
240
241 /* MultiHandleSink signals implemented in base class and declared
242  * in subclass
243  */
244 enum
245 {
246   /* signals */
247   GST_MULTI_HANDLE_SIGNAL_CLIENT_ADDED,
248   GST_MULTI_HANDLE_SIGNAL_CLIENT_REMOVED,
249   GST_MULTI_HANDLE_SIGNAL_CLIENT_HANDLE_REMOVED,
250
251   GST_MULTI_HANDLE_LAST_SIGNAL
252 };
253
254
255 struct _GstMultiHandleSinkClass {
256   GstBaseSinkClass parent_class;
257
258   /* element methods */
259   void          (*add)          (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
260   void          (*add_full)     (GstMultiHandleSink *sink, GstMultiSinkHandle handle, GstSyncMethod sync,
261                                  GstFormat format, guint64 value, 
262                                  GstFormat max_format, guint64 max_value);
263   void          (*remove)       (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
264   void          (*remove_flush) (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
265
266   void          (*clear)        (GstMultiHandleSink *sink);
267   void          (*stop_pre)     (GstMultiHandleSink *sink);
268   void          (*stop_post)    (GstMultiHandleSink *sink);
269   gboolean      (*start_pre)    (GstMultiHandleSink *sink);
270   gpointer      (*thread)       (GstMultiHandleSink *sink);
271   /* called by subclass when it has a new buffer to queue for a client */
272   gboolean      (*client_queue_buffer)
273                                 (GstMultiHandleSink *sink,
274                                  GstMultiHandleClient *client,
275                                  GstBuffer *buffer);
276   int           (*client_get_fd)
277                                 (GstMultiHandleClient *client);
278   void          (*client_free)
279                                 (GstMultiHandleClient *client);
280   void          (*handle_debug) (GstMultiSinkHandle handle, gchar debug[30]);
281   gpointer      (*handle_hash_key)  (GstMultiSinkHandle handle);
282   /* called when the client hash/list has been changed */
283   void          (*hash_changed)  (GstMultiHandleSink *mhsink);
284   void          (*hash_adding)   (GstMultiHandleSink *mhsink, GstMultiHandleClient *client);
285   void          (*hash_removing) (GstMultiHandleSink *mhsink, GstMultiHandleClient *client);
286   GstMultiHandleClient* (*new_client) (GstMultiHandleSink *mhsink, GstMultiSinkHandle handle, GstSyncMethod sync_method);
287
288
289   GstStructure* (*get_stats)    (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
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_handle_removed) (GstElement *element, GstMultiSinkHandle handle);
301
302   guint signals[GST_MULTI_HANDLE_LAST_SIGNAL];
303 };
304
305 GType gst_multi_handle_sink_get_type (void);
306
307 void          gst_multi_handle_sink_add          (GstMultiHandleSink *sink, GstMultiSinkHandle);
308 void          gst_multi_handle_sink_add_full     (GstMultiHandleSink *sink, GstMultiSinkHandle, GstSyncMethod sync, 
309                                               GstFormat min_format, guint64 min_value,
310                                               GstFormat max_format, guint64 max_value);
311 void          gst_multi_handle_sink_remove       (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
312 void          gst_multi_handle_sink_remove_flush (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
313 GstStructure*  gst_multi_handle_sink_get_stats    (GstMultiHandleSink *sink, GstMultiSinkHandle handle);
314 void gst_multi_handle_sink_remove_client_link (GstMultiHandleSink * sink,
315     GList * link);
316
317 void gst_multi_handle_sink_client_init (GstMultiHandleClient * client, GstSyncMethod sync_method);
318
319 #define GST_TYPE_RECOVER_POLICY (gst_multi_handle_sink_recover_policy_get_type())
320 GType gst_multi_handle_sink_recover_policy_get_type (void);
321 #define GST_TYPE_SYNC_METHOD (gst_multi_handle_sink_sync_method_get_type())
322 GType gst_multi_handle_sink_sync_method_get_type (void);
323 #define GST_TYPE_CLIENT_STATUS (gst_multi_handle_sink_client_status_get_type())
324 GType gst_multi_handle_sink_client_status_get_type (void);
325
326
327 G_END_DECLS
328
329 #endif /* __GST_MULTI_HANDLE_SINK_H__ */