multifdsink: put back multifdsink before refactoring
[platform/upstream/gstreamer.git] / gst / tcp / gstmultifdsink.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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21
22 #ifndef __GST_MULTI_FD_SINK_H__
23 #define __GST_MULTI_FD_SINK_H__
24
25 #include <gst/gst.h>
26 #include <gst/base/gstbasesink.h>
27
28 G_BEGIN_DECLS
29
30 #define GST_TYPE_MULTI_FD_SINK \
31   (gst_multi_fd_sink_get_type())
32 #define GST_MULTI_FD_SINK(obj) \
33   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MULTI_FD_SINK,GstMultiFdSink))
34 #define GST_MULTI_FD_SINK_CLASS(klass) \
35   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MULTI_FD_SINK,GstMultiFdSinkClass))
36 #define GST_IS_MULTI_FD_SINK(obj) \
37   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MULTI_FD_SINK))
38 #define GST_IS_MULTI_FD_SINK_CLASS(klass) \
39   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MULTI_FD_SINK))
40 #define GST_MULTI_FD_SINK_GET_CLASS(klass) \
41   (G_TYPE_INSTANCE_GET_CLASS ((klass), GST_TYPE_MULTI_FD_SINK, GstMultiFdSinkClass))
42
43
44 typedef struct _GstMultiFdSink GstMultiFdSink;
45 typedef struct _GstMultiFdSinkClass GstMultiFdSinkClass;
46
47 typedef enum {
48   GST_MULTI_FD_SINK_OPEN             = (GST_ELEMENT_FLAG_LAST << 0),
49
50   GST_MULTI_FD_SINK_FLAG_LAST        = (GST_ELEMENT_FLAG_LAST << 2)
51 } GstMultiFdSinkFlags;
52
53 /**
54  * GstRecoverPolicy:
55  * @GST_RECOVER_POLICY_NONE             : no recovering is done
56  * @GST_RECOVER_POLICY_RESYNC_LATEST    : client is moved to last buffer
57  * @GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT: client is moved to the soft limit
58  * @GST_RECOVER_POLICY_RESYNC_KEYFRAME  : client is moved to latest keyframe
59  *
60  * Possible values for the recovery procedure to use when a client consumes
61  * data too slow and has a backlag of more that soft-limit buffers.
62  */
63 typedef enum
64 {
65   GST_RECOVER_POLICY_NONE,
66   GST_RECOVER_POLICY_RESYNC_LATEST,
67   GST_RECOVER_POLICY_RESYNC_SOFT_LIMIT,
68   GST_RECOVER_POLICY_RESYNC_KEYFRAME
69 } GstRecoverPolicy;
70
71 /**
72  * GstSyncMethod:
73  * @GST_SYNC_METHOD_LATEST              : client receives most recent buffer
74  * @GST_SYNC_METHOD_NEXT_KEYFRAME       : client receives next keyframe
75  * @GST_SYNC_METHOD_LATEST_KEYFRAME     : client receives latest keyframe (burst)
76  * @GST_SYNC_METHOD_BURST               : client receives specific amount of data
77  * @GST_SYNC_METHOD_BURST_KEYFRAME      : client receives specific amount of data 
78  *                                        starting from latest keyframe
79  * @GST_SYNC_METHOD_BURST_WITH_KEYFRAME : client receives specific amount of data from
80  *                                        a keyframe, or if there is not enough data after
81  *                                        the keyframe, starting before the keyframe
82  *
83  * This enum defines the selection of the first buffer that is sent
84  * to a new client.
85  */
86 typedef enum
87 {
88   GST_SYNC_METHOD_LATEST,
89   GST_SYNC_METHOD_NEXT_KEYFRAME,
90   GST_SYNC_METHOD_LATEST_KEYFRAME,
91   GST_SYNC_METHOD_BURST,
92   GST_SYNC_METHOD_BURST_KEYFRAME,
93   GST_SYNC_METHOD_BURST_WITH_KEYFRAME
94 } GstSyncMethod;
95
96 /**
97  * GstTCPUnitType:
98  * @GST_TCP_UNIT_TYPE_UNDEFINED: undefined
99  * @GST_TCP_UNIT_TYPE_BUFFERS  : buffers
100  * @GST_TCP_UNIT_TYPE_TIME     : timeunits (in nanoseconds)
101  * @GST_TCP_UNIT_TYPE_BYTES    : bytes
102  *
103  * The units used to specify limits.
104  */
105 typedef enum
106 {
107   GST_TCP_UNIT_TYPE_UNDEFINED,
108   GST_TCP_UNIT_TYPE_BUFFERS,
109   GST_TCP_UNIT_TYPE_TIME,
110   GST_TCP_UNIT_TYPE_BYTES
111 } GstTCPUnitType;
112
113 /**
114  * GstClientStatus:
115  * @GST_CLIENT_STATUS_OK       : client is ok
116  * @GST_CLIENT_STATUS_CLOSED   : client closed the socket
117  * @GST_CLIENT_STATUS_REMOVED  : client is removed
118  * @GST_CLIENT_STATUS_SLOW     : client is too slow
119  * @GST_CLIENT_STATUS_ERROR    : client is in error
120  * @GST_CLIENT_STATUS_DUPLICATE: same client added twice
121  * @GST_CLIENT_STATUS_FLUSHING : client is flushing out the remaining buffers.
122  *
123  * This specifies the reason why a client was removed from
124  * multifdsink and is received in the "client-removed" signal.
125  */
126 typedef enum
127 {
128   GST_CLIENT_STATUS_OK          = 0,
129   GST_CLIENT_STATUS_CLOSED      = 1,
130   GST_CLIENT_STATUS_REMOVED     = 2,
131   GST_CLIENT_STATUS_SLOW        = 3,
132   GST_CLIENT_STATUS_ERROR       = 4,
133   GST_CLIENT_STATUS_DUPLICATE   = 5,
134   GST_CLIENT_STATUS_FLUSHING    = 6
135 } GstClientStatus;
136
137 /* structure for a client
138  */
139 typedef struct {
140   GstPollFD fd;
141
142   gint bufpos;                  /* position of this client in the global queue */
143   gint flushcount;              /* the remaining number of buffers to flush out or -1 if the 
144                                    client is not flushing. */
145
146   GstClientStatus status;
147   gboolean is_socket;
148
149   GSList *sending;              /* the buffers we need to send */
150   gint bufoffset;               /* offset in the first buffer */
151
152   gboolean discont;
153
154   gboolean caps_sent;
155   gboolean new_connection;
156
157   gboolean currently_removing;
158
159   /* method to sync client when connecting */
160   GstSyncMethod sync_method;
161   GstTCPUnitType   burst_min_unit;
162   guint64       burst_min_value;
163   GstTCPUnitType   burst_max_unit;
164   guint64       burst_max_value;
165
166   GstCaps *caps;                /* caps of last queued buffer */
167
168   /* stats */
169   guint64 bytes_sent;
170   guint64 connect_time;
171   guint64 disconnect_time;
172   guint64 last_activity_time;
173   guint64 dropped_buffers;
174   guint64 avg_queue_size;
175   guint64 first_buffer_ts;
176   guint64 last_buffer_ts;
177 } GstTCPClient;
178
179 #define CLIENTS_LOCK_INIT(fdsink)       (g_rec_mutex_init(&fdsink->clientslock))
180 #define CLIENTS_LOCK_CLEAR(fdsink)      (g_rec_mutex_clear(&fdsink->clientslock))
181 #define CLIENTS_LOCK(fdsink)            (g_rec_mutex_lock(&fdsink->clientslock))
182 #define CLIENTS_UNLOCK(fdsink)          (g_rec_mutex_unlock(&fdsink->clientslock))
183
184 /**
185  * GstMultiFdSink:
186  *
187  * The multifdsink object structure.
188  */
189 struct _GstMultiFdSink {
190   GstBaseSink element;
191
192   /*< private >*/
193   guint64 bytes_to_serve; /* how much bytes we must serve */
194   guint64 bytes_served; /* how much bytes have we served */
195
196   GRecMutex clientslock;  /* lock to protect the clients list */
197   GList *clients;       /* list of clients we are serving */
198   GHashTable *fd_hash;  /* index on fd to client */
199   guint clients_cookie; /* Cookie to detect changes to the clients list */
200
201   gint mode;
202   GstPoll *fdset;
203
204   GSList *streamheader; /* GSList of GstBuffers to use as streamheader */
205   gboolean previous_buffer_in_caps;
206
207   guint mtu;
208   gint qos_dscp;
209   gboolean handle_read;
210
211   GArray *bufqueue;     /* global queue of buffers */
212
213   gboolean running;     /* the thread state */
214   GThread *thread;      /* the sender thread */
215
216   /* these values are used to check if a client is reading fast
217    * enough and to control receovery */
218   GstTCPUnitType unit_type;/* the type of the units */
219   gint64 units_max;       /* max units to queue for a client */
220   gint64 units_soft_max;  /* max units a client can lag before recovery starts */
221   GstRecoverPolicy recover_policy;
222   GstClockTime timeout; /* max amount of nanoseconds to remain idle */
223
224   GstSyncMethod def_sync_method;    /* what method to use for connecting clients */
225   GstTCPUnitType   def_burst_unit;
226   guint64       def_burst_value;
227
228   /* these values are used to control the amount of data
229    * kept in the queues. It allows clients to perform a burst
230    * on connect. */
231   gint   bytes_min;     /* min number of bytes to queue */
232   gint64 time_min;      /* min time to queue */
233   gint   buffers_min;   /* min number of buffers to queue */
234
235   gboolean resend_streamheader; /* resend streamheader if it changes */
236
237   /* stats */
238   gint buffers_queued;  /* number of queued buffers */
239   gint bytes_queued;    /* number of queued bytes */
240   gint time_queued;     /* number of queued time */
241
242   guint8 header_flags;
243 };
244
245 struct _GstMultiFdSinkClass {
246   GstBaseSinkClass parent_class;
247
248   /* element methods */
249   void          (*add)          (GstMultiFdSink *sink, int fd);
250   void          (*add_full)     (GstMultiFdSink *sink, int fd, GstSyncMethod sync,
251                                  GstTCPUnitType format, guint64 value, 
252                                  GstTCPUnitType max_unit, guint64 max_value);
253   void          (*remove)       (GstMultiFdSink *sink, int fd);
254   void          (*remove_flush) (GstMultiFdSink *sink, int fd);
255   void          (*clear)        (GstMultiFdSink *sink);
256   GValueArray*  (*get_stats)    (GstMultiFdSink *sink, int fd);
257
258   /* vtable */
259   gboolean (*init)   (GstMultiFdSink *sink);
260   gboolean (*wait)   (GstMultiFdSink *sink, GstPoll *set);
261   gboolean (*close)  (GstMultiFdSink *sink);
262   void (*removed) (GstMultiFdSink *sink, int fd);
263
264   /* signals */
265   void (*client_added) (GstElement *element, gint fd);
266   void (*client_removed) (GstElement *element, gint fd, GstClientStatus status);
267   void (*client_fd_removed) (GstElement *element, gint fd);
268 };
269
270 GType gst_multi_fd_sink_get_type (void);
271
272 void          gst_multi_fd_sink_add          (GstMultiFdSink *sink, int fd);
273 void          gst_multi_fd_sink_add_full     (GstMultiFdSink *sink, int fd, GstSyncMethod sync, 
274                                               GstTCPUnitType min_unit, guint64 min_value,
275                                               GstTCPUnitType max_unit, guint64 max_value);
276 void          gst_multi_fd_sink_remove       (GstMultiFdSink *sink, int fd);
277 void          gst_multi_fd_sink_remove_flush (GstMultiFdSink *sink, int fd);
278 void          gst_multi_fd_sink_clear        (GstMultiFdSink *sink);
279 GValueArray*  gst_multi_fd_sink_get_stats    (GstMultiFdSink *sink, int fd);
280
281 G_END_DECLS
282
283 #endif /* __GST_MULTI_FD_SINK_H__ */