34e3abaf6a3e16e1c119d56866f2026848d8ba51
[platform/upstream/gstreamer.git] / plugins / elements / gstqueue2.h
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2003 Colin Walters <cwalters@gnome.org>
4  *                    2000,2005,2007 Wim Taymans <wim.taymans@gmail.com>
5  *                    2007 Thiago Sousa Santos <thiagoss@lcc.ufcg.edu.br>
6  *
7  * gstqueue2.h:
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  */
24 #ifndef __GST_QUEUE2_H__
25 #define __GST_QUEUE2_H__
26
27 #include <gst/gst.h>
28 #include <stdio.h>
29
30 G_BEGIN_DECLS
31
32 #define GST_TYPE_QUEUE2 \
33   (gst_queue2_get_type())
34 #define GST_QUEUE2(obj) \
35   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_QUEUE2,GstQueue2))
36 #define GST_QUEUE2_CLASS(klass) \
37   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_QUEUE2,GstQueue2Class))
38 #define GST_IS_QUEUE2(obj) \
39   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_QUEUE2))
40 #define GST_IS_QUEUE2_CLASS(klass) \
41   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_QUEUE2))
42 #define GST_QUEUE2_CAST(obj) \
43   ((GstQueue2 *)(obj))
44
45 typedef struct _GstQueue2 GstQueue2;
46 typedef struct _GstQueue2Size GstQueue2Size;
47 typedef struct _GstQueue2Class GstQueue2Class;
48 typedef struct _GstQueue2Range GstQueue2Range;
49
50 /* used to keep track of sizes (current and max) */
51 struct _GstQueue2Size
52 {
53   guint buffers;
54   guint bytes;
55   guint64 time;
56   guint64 rate_time;
57 };
58
59 struct _GstQueue2Range
60 {
61   GstQueue2Range *next;
62
63   guint64 offset;          /* offset of range start in source */
64   guint64 rb_offset;       /* offset of range start in ring buffer */
65   guint64 writing_pos;     /* writing position in source */
66   guint64 rb_writing_pos;  /* writing position in ring buffer */
67   guint64 reading_pos;     /* reading position in source */
68   guint64 max_reading_pos; /* latest requested offset in source */
69 };
70
71 struct _GstQueue2
72 {
73   GstElement element;
74
75   /*< private > */
76   GstPad *sinkpad;
77   GstPad *srcpad;
78
79   /* upstream size in bytes (if downstream is operating in pull mode) */
80   guint64 upstream_size;
81
82   /* segments to keep track of timestamps */
83   GstSegment sink_segment;
84   GstSegment src_segment;
85
86   /* Position of src/sink */
87   GstClockTime sinktime, srctime;
88   /* TRUE if either position needs to be recalculated */
89   gboolean sink_tainted, src_tainted;
90   /* Bitrates taken from tags */
91   guint sink_tags_bitrate;
92   guint src_tags_bitrate;
93
94   /* flowreturn when srcpad is paused */
95   GstFlowReturn srcresult;
96   GstFlowReturn sinkresult;
97   gboolean is_eos;
98   gboolean unexpected;
99
100   /* the queue of data we're keeping our hands on */
101   GQueue queue;
102
103   GCond query_handled;
104   gboolean last_query; /* result of last serialized query */
105   GstQuery *last_handled_query;
106
107   GstQueue2Size cur_level;       /* currently in the queue */
108   GstQueue2Size max_level;       /* max. amount of data allowed in the queue */
109   gboolean use_buffering;
110   gboolean use_tags_bitrate;
111   gboolean use_rate_estimate;
112   GstClockTime buffering_interval;
113
114   /* low/high watermarks for buffering */
115   gint low_watermark;
116   gint high_watermark;
117
118   /* current buffering state */
119   gboolean is_buffering;
120   gint buffering_percent;
121
122   /* for measuring input/output rates */
123   GTimer *in_timer;
124   gboolean in_timer_started;
125   gdouble last_update_in_rates_elapsed;
126   gdouble last_in_elapsed;
127   guint64 bytes_in;
128   gdouble byte_in_rate;
129   gdouble byte_in_period;
130
131   GTimer *out_timer;
132   gboolean out_timer_started;
133   gdouble last_out_elapsed;
134   guint64 bytes_out;
135   gdouble byte_out_rate;
136
137   GMutex qlock;                /* lock for queue (vs object lock) */
138   gboolean waiting_add;
139   GCond item_add;              /* signals buffers now available for reading */
140   gboolean waiting_del;
141   GCond item_del;              /* signals space now available for writing */
142
143   /* temp location stuff */
144   gchar *temp_template;
145   gboolean temp_location_set;
146   gchar *temp_location;
147   gboolean temp_remove;
148   FILE *temp_file;
149   /* list of downloaded areas and the current area */
150   GstQueue2Range *ranges;
151   GstQueue2Range *current;
152   /* we need this to send the first new segment event of the stream
153    * because we can't save it on the file */
154   gboolean segment_event_received;
155   GstEvent *starting_segment;
156   gboolean seeking;
157
158   GstEvent *stream_start_event;
159
160   guint64 ring_buffer_max_size;
161   guint8 * ring_buffer;
162
163   volatile gint downstream_may_block;
164
165   GstBufferingMode mode;
166   gint64 buffering_left;
167   gint avg_in;
168   gint avg_out;
169   gboolean percent_changed;
170   GMutex buffering_post_lock; /* assures only one posted at a time */
171 };
172
173 struct _GstQueue2Class
174 {
175   GstElementClass parent_class;
176 };
177
178 G_GNUC_INTERNAL GType gst_queue2_get_type (void);
179
180 G_END_DECLS
181
182 #endif /* __GST_QUEUE2_H__ */