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