hlsdemux: Fix accessing invalidated memory
[platform/upstream/gstreamer.git] / ext / hls / gsthlsdemux.h
1 /* GStreamer
2  * Copyright (C) 2010 Marc-Andre Lureau <marcandre.lureau@gmail.com>
3  * Copyright (C) 2010 Andoni Morales Alastruey <ylatuya@gmail.com>
4  *
5  * gsthlsdemux.h:
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., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23
24 #ifndef __GST_HLS_DEMUX_H__
25 #define __GST_HLS_DEMUX_H__
26
27 #include <gst/gst.h>
28 #include <gst/base/gstadapter.h>
29 #include "m3u8.h"
30 #include "gstfragmented.h"
31 #include <gst/uridownloader/gsturidownloader.h>
32 #if defined(HAVE_OPENSSL)
33 #include <openssl/evp.h>
34 #elif defined(HAVE_NETTLE)
35 #include <nettle/aes.h>
36 #include <nettle/cbc.h>
37 #else
38 #include <gcrypt.h>
39 #endif
40
41 G_BEGIN_DECLS
42 #define GST_TYPE_HLS_DEMUX \
43   (gst_hls_demux_get_type())
44 #define GST_HLS_DEMUX(obj) \
45   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_HLS_DEMUX,GstHLSDemux))
46 #define GST_HLS_DEMUX_CLASS(klass) \
47   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_HLS_DEMUX,GstHLSDemuxClass))
48 #define GST_IS_HLS_DEMUX(obj) \
49   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_HLS_DEMUX))
50 #define GST_IS_HLS_DEMUX_CLASS(klass) \
51   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_HLS_DEMUX))
52 #define GST_HLS_DEMUX_GET_CLASS(obj) \
53   (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_HLS_DEMUX,GstHLSDemuxClass))
54 #define GST_HLS_DEMUX_CAST(obj) \
55   ((GstHLSDemux *)obj)
56 typedef struct _GstHLSDemux GstHLSDemux;
57 typedef struct _GstHLSDemuxClass GstHLSDemuxClass;
58
59 /**
60  * GstHLSDemux:
61  *
62  * Opaque #GstHLSDemux data structure.
63  */
64 struct _GstHLSDemux
65 {
66   GstBin parent;
67
68   GstPad *sinkpad;
69   GstPad *srcpad;
70   gint srcpad_counter;
71
72   gboolean have_group_id;
73   guint group_id;
74
75   GstBuffer *playlist;
76   GstCaps *input_caps;
77   GstUriDownloader *downloader;
78   gchar *uri;                   /* Original playlist URI */
79   GstM3U8Client *client;        /* M3U8 client */
80   gboolean do_typefind;         /* Whether we need to typefind the next buffer */
81   gboolean new_playlist;        /* Whether a new playlist is about to start and pads should be switched */
82
83   /* Properties */
84   guint fragments_cache;        /* number of fragments needed to be cached to start playing */
85   gfloat bitrate_limit;         /* limit of the available bitrate to use */
86   guint connection_speed;       /* Network connection speed in kbps (0 = unknown) */
87
88   /* Streaming task */
89   GstTask *stream_task;
90   GRecMutex stream_lock;
91   gboolean stop_stream_task;
92   GMutex download_lock;         /* Used for protecting queue and the two conds */
93   GCond download_cond;          /* Signalled when something is added to the queue */
94   gboolean end_of_playlist;
95   gint download_failed_count;
96   gint64 next_download;
97
98   /* Updates task */
99   GstTask *updates_task;
100   GRecMutex updates_lock;
101   gint64 next_update;           /* Time of the next update */
102   gboolean stop_updates_task;
103   GMutex updates_timed_lock;
104   GCond updates_timed_cond;     /* Signalled when the playlist should be updated */
105
106   /* Position in the stream */
107   GstSegment segment;
108   gboolean need_segment;
109   gboolean discont;
110
111   /* Cache for the last key */
112   gchar *key_url;
113   GstFragment *key_fragment;
114
115   /* Current download rate (bps) */
116   gint current_download_rate;
117
118   /* fragment download tooling */
119   GstElement *src;
120   GstPad *src_srcpad; /* handy link to src's src pad */
121   GMutex fragment_download_lock;
122   gboolean download_finished;
123   GCond fragment_download_cond;
124   GstClockTime current_timestamp;
125   GstClockTime current_duration;
126   gboolean starting_fragment;
127   gboolean reset_crypto;
128   gint64 download_start_time;
129   gint64 download_total_time;
130   gint64 download_total_bytes;
131   GstFlowReturn last_ret;
132   GError *last_error;
133
134   /* decryption tooling */
135 #if defined(HAVE_OPENSSL)
136   EVP_CIPHER_CTX aes_ctx;
137 #elif defined(HAVE_NETTLE)
138   struct CBC_CTX (struct aes_ctx, AES_BLOCK_SIZE) aes_ctx;
139 #else
140   gcry_cipher_hd_t aes_ctx;
141 #endif
142   gchar *current_key;
143   guint8 *current_iv;
144   GstAdapter *adapter; /* used to accumulate 16 bytes multiple chunks */
145   GstBuffer *pending_buffer; /* decryption scenario:
146                               * the last buffer can only be pushed when
147                               * resized, so need to store and wait for
148                               * EOS to know it is the last */
149 };
150
151 struct _GstHLSDemuxClass
152 {
153   GstBinClass parent_class;
154 };
155
156 GType gst_hls_demux_get_type (void);
157
158 G_END_DECLS
159 #endif /* __GST_HLS_DEMUX_H__ */