tizen 2.3.1 release
[framework/multimedia/gst-plugins-ext0.10.git] / hlsdemux2 / src / gsthlsdemux2.h
1 /*
2  * hlsdemux2
3  *
4  * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, Naveen Cherukuri <naveen.ch@samsung.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  *
26  * Alternatively, the contents of this file may be used under the
27  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28  * which case the following provisions apply instead of the ones
29  * mentioned above:
30  *
31  * This library is free software; you can redistribute it and/or
32  * modify it under the terms of the GNU Library General Public
33  * License as published by the Free Software Foundation; either
34  * version 2 of the License, or (at your option) any later version.
35  *
36  * This library is distributed in the hope that it will be useful,
37  * but WITHOUT ANY WARRANTY; without even the implied warranty of
38  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
39  * Library General Public License for more details.
40  *
41  * You should have received a copy of the GNU Library General Public
42  * License along with this library; if not, write to the
43  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
44  * Boston, MA 02111-1307, USA.
45  */
46
47
48 #ifndef __GST_HLSDEMUX2_H__
49 #define __GST_HLSDEMUX2_H__
50
51 #include <gst/gst.h>
52 #include <gst/base/gstadapter.h>
53 #include <gst/app/gstappsrc.h>
54 #include <gst/app/gstappsink.h>
55 #include "m3u8.h"
56
57 G_BEGIN_DECLS
58 #define GST_TYPE_HLSDEMUX2  (gst_hlsdemux2_get_type())
59 #define GST_HLSDEMUX2(obj)  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_HLSDEMUX2, GstHLSDemux2))
60 #define GST_HLSDEMUX2_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_HLSDEMUX2,GstHLSDemux2Class))
61 #define GST_IS_HLSDEMUX2(obj)  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_HLSDEMUX2))
62 #define GST_IS_HLSDEMUX2_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_HLSDEMUX2))
63
64 typedef struct _GstHLSDemux2 GstHLSDemux2;
65 typedef struct _GstHLSDemux2Class GstHLSDemux2Class;
66 typedef struct _GstHLSDemux2Stream GstHLSDemux2Stream;
67 typedef struct _GstHLSDemux2PvtStream GstHLSDemux2PvtStream;
68
69 #define GST_HLS_DEMUX_AES_BLOCK_SIZE 16
70
71 #define ON_ERROR_REQUEST_AGAIN // Orange server giving soup errors. so, request for fail count times
72
73 #define HLSDEMUX2_CHUNK_TIME_DURATION (0.5 * GST_SECOND)
74 #define HLSDEMUX2_NUM_CHUNK_DOWNLOADS_FAST_SWITCH 4
75 #define HLSDEMUX2_MAX_N_PAST_FRAG_DOWNLOADRATES 3
76
77 #define HLSDEMUX2_FAST_CHECK_CRITICAL_TIME_FACTOR 1.2
78 #define HLSDEMUX2_FAST_CHECK_WARNING_TIME_FACTOR 1
79
80 #ifdef USING_AVG_PAST_DOWNLOADRATES
81 #define HLSDEMUX2_MAX_N_PAST_DOWNLOADRATES 3
82 #endif
83
84
85 #define PLAYLIST_REFRESH_TIMEOUT (20 * GST_SECOND)
86
87 #define HLSDEMUX2_OVERHEAD (1 * GST_SECOND)
88
89 #define LATEST_AV_SYNC
90
91 typedef enum
92 {
93   HLSDEMUX2_STREAM_VIDEO = 0,
94   HLSDEMUX2_STREAM_AUDIO,
95   HLSDEMUX2_STREAM_TEXT,
96   HLSDEMUX2_STREAM_PRIVATE,
97   HLSDEMUX2_STREAM_NUM,
98 }HLSDEMUX2_STREAM_TYPE;
99
100 typedef enum
101 {
102   HLSDEMUX2_DOWNWARD_RECOVERY = 0,
103   HLSDEMUX2_UPWARD_RECOVERY,
104   HLSDEMUX2_NO_RECOVERY,
105 }HLSDEMUX2_RECOVERY_MODE;
106
107 typedef enum
108 {
109   HLSDEMUX2_MULTI_VARIANT = 0,
110   HLSDEMUX2_SINGLE_VARIANT
111 }HLSDEMUX2_STREAM_CONFIGURATION;
112
113 typedef struct
114 {
115   GstBin *sinkbin;
116   GstElement *parser; /*only for .aac stream */
117   GstElement *queue;
118   GstElement *sink;
119 }HLSDemux2SinkBin;
120
121 typedef struct
122 {
123   GstElement *pipe;
124   GstElement *urisrc;
125   GstElement *typefind;
126   GstElement *queue;
127   GstElement *demuxer;
128   GList *sinkbins;
129
130   guint64 content_size;
131   gboolean applied_fast_switch;
132
133   gchar *remaining_data;
134   guint remaining_size;
135
136   GMutex *lock;
137   GCond *cond;
138   gboolean is_encrypted;
139
140   gulong src_bprobe;
141   gboolean get_next_frag; /* on error like NOT_FOUND, request next fragment */
142
143   guint cur_stream_cnt;
144   gboolean first_buffer;
145   gboolean force_timestamps; /* useful in .aac files playback */
146   gboolean error_rcvd;
147   gboolean find_mediaseq;
148   GstClockTime seeked_pos;
149   GstClockTime cur_running_dur;
150
151   /* for fragment download rate calculation */
152   guint64 download_rate;
153   GstClockTime download_start_ts;
154   GstClockTime download_stop_ts;
155   guint64 src_downloaded_size;
156   guint64 queue_downloaded_size;
157   gint ndownloaded; /* number of fragments downloaded */
158   GArray *avg_chunk_drates; /* chunk download rates array */
159   GArray *avg_frag_drates; /* fragment download rates array */
160   guint64 chunk_downloaded_size;
161   GstClockTime chunk_start_ts;
162 }HLSDemux2FragDownloader;
163
164 /* playlist downloader */
165 typedef struct
166 {
167   GstElement *pipe;
168   GstBus *bus;
169   GstElement *urisrc;
170   GstElement *sink;
171   GMutex *lock;
172   GCond *cond;
173
174   GstBuffer *playlist;
175   HLSDEMUX2_RECOVERY_MODE recovery_mode;
176   GstClockTime download_start_ts;
177 }HLSDemux2PLDownloader;
178
179 /* Key downloader */
180 typedef struct
181 {
182   GstElement *pipe;
183   GstBus *bus;
184   GstElement *urisrc;
185   GstElement *sink;
186
187   GMutex *lock;
188   GCond *cond;
189
190   GstBuffer *key;
191   gchar *prev_key_uri;
192 }HLSDemux2KeyDownloader;
193
194 /**
195  * GstHLSDemux2:
196  *
197  * Opaque #GstHLSDemux2 data structure.
198  */
199 struct _GstHLSDemux2
200 {
201   GstElement parent;
202
203   GstPad *sinkpad;
204
205   /* M3U8 client */
206   GstM3U8Client *client;
207
208   gboolean is_live;
209
210   HLSDemux2FragDownloader *fdownloader; /* fragment downloader */
211   HLSDemux2PLDownloader *pldownloader; /* playlist downloader */
212   HLSDemux2KeyDownloader *kdownloader; /* key downloader */
213
214   /* properties */
215   gint blocksize;
216
217   /* master playlist got from upstream */
218   GstBuffer *playlist;
219
220   /* fragment download task */
221   GstTask *download_task;
222   GStaticRecMutex download_lock;
223
224   /* Playlist updates task for live session */
225   GStaticRecMutex updates_lock;
226   GTimeVal next_update;         /* Time of the next update */
227
228   /* playlist update condition */
229   GCond *pl_update_cond;
230   GMutex *pl_update_lock;
231
232   /* array of pointers to each stream's list */
233   GstHLSDemux2Stream *streams[HLSDEMUX2_STREAM_NUM];
234   gint active_stream_cnt;
235
236   gint percent; /* buffering percentage posting */
237   GMutex *buffering_lock;
238   gboolean is_buffering;
239   GThread *buffering_posting_thread;
240   GMutex *post_msg_lock;
241   GCond *post_msg_start;
242   GCond *post_msg_exit;
243
244   /* HTTP request cookies. */
245   gchar **lastCookie;
246   gchar **playlistCookie;
247   gchar **keyCookie;
248   gchar **fragCookie;
249   gchar *lastDomain;
250   gchar *playlistDomain;
251   gchar *keyDomain;
252   gchar *fragDomain;
253
254   gchar *user_agent;
255
256   /* Properties */
257   gfloat bitrate_switch_tol;    /* tolerance with respect to the fragment duration to switch the bitarate*/
258
259   gboolean cancelled;
260   gboolean end_of_playlist;
261
262   guint64 total_cache_duration;
263   guint64 target_duration;
264
265 #ifdef ON_ERROR_REQUEST_AGAIN
266    /* requesting the same uron error */
267   gchar *playlist_uri;
268   gchar *frag_uri;  /* NOT supporting YET for fragment download errors*/
269   gchar *key_uri;
270 #endif
271
272   int soup_request_fail_cnt;
273   /* can be removed [mainly for debugging & testing] */
274   gboolean force_lower_bitrate;
275
276   GstClockTime cfrag_dur;
277   gboolean error_posted;
278   gboolean flushing;
279   GstClockTime ns_start;
280   GstClockTime cur_audio_fts;
281
282   GstHLSDemux2PvtStream * private_stream;
283   gboolean has_image_buffer;
284   HLSDEMUX2_STREAM_CONFIGURATION stream_config;
285   GstBuffer *prev_image_buffer;
286   GstBuffer *prev_video_buffer;
287 };
288
289 struct _GstHLSDemux2Class
290 {
291   GstElementClass parent_class;
292 };
293
294 GType gst_hlsdemux2_get_type (void);
295
296 G_END_DECLS
297 #endif /* __GST_HLSDEMUX2_H__ */