Merge branch 'move_subdir_rtsp-server' into tizen_gst_1.19.2_mono
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / 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  * Copyright (C) 2015 Tim-Philipp Müller <tim@centricular.com>
5  *
6  * gsthlsdemux.h:
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24
25 #ifndef __GST_HLS_DEMUX_H__
26 #define __GST_HLS_DEMUX_H__
27
28 #include <gst/gst.h>
29 #include "m3u8.h"
30 #include <gst/adaptivedemux/gstadaptivedemux.h>
31 #if defined(HAVE_OPENSSL)
32 #include <openssl/evp.h>
33 #elif defined(HAVE_NETTLE)
34 #include <nettle/aes.h>
35 #include <nettle/cbc.h>
36 #elif defined(HAVE_LIBGCRYPT)
37 #include <gcrypt.h>
38 #endif
39
40 G_BEGIN_DECLS
41
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
57 typedef struct _GstHLSDemux GstHLSDemux;
58 typedef struct _GstHLSDemuxClass GstHLSDemuxClass;
59 typedef struct _GstHLSDemuxStream GstHLSDemuxStream;
60 typedef struct _GstHLSTSReader GstHLSTSReader;
61
62 #define GST_HLS_DEMUX_STREAM_CAST(stream) ((GstHLSDemuxStream *)(stream))
63
64 typedef enum {
65   GST_HLS_TSREADER_NONE,
66   GST_HLS_TSREADER_MPEGTS,
67   GST_HLS_TSREADER_ID3
68 } GstHLSTSReaderType;
69
70 struct _GstHLSTSReader
71 {
72   GstHLSTSReaderType rtype;
73   gboolean have_id3;
74
75   gint packet_size;
76   gint pmt_pid;
77   gint pcr_pid;
78
79   GstClockTime last_pcr;
80   GstClockTime first_pcr;
81 };
82
83 struct _GstHLSDemuxStream
84 {
85   GstAdaptiveDemuxStream adaptive_demux_stream;
86
87   GstHLSTSReaderType stream_type;
88
89   GstM3U8 *playlist;
90   gboolean is_primary_playlist;
91
92   gboolean do_typefind;         /* Whether we need to typefind the next buffer */
93   GstBuffer *pending_typefind_buffer; /* for collecting data until typefind succeeds */
94
95   GstAdapter *pending_encrypted_data;  /* for chunking data into 16 byte multiples for decryption */
96   GstBuffer *pending_decrypted_buffer; /* last decrypted buffer for pkcs7 unpadding.
97                                           We only know that it is the last at EOS */
98   guint64 current_offset;              /* offset we're currently at */
99   gboolean reset_pts;
100 #ifdef TIZEN_FEATURE_HLSDEMUX_DISCONT
101   GstClockTime sequence_pos;
102   GstClockTime last_pcr;
103 #endif
104
105   /* decryption tooling */
106 #if defined(HAVE_OPENSSL)
107 # if OPENSSL_VERSION_NUMBER < 0x10100000L
108   EVP_CIPHER_CTX aes_ctx;
109 # else
110   EVP_CIPHER_CTX *aes_ctx;
111 # endif
112 #elif defined(HAVE_NETTLE)
113   struct CBC_CTX (struct aes128_ctx, AES_BLOCK_SIZE) aes_ctx;
114 #elif defined(HAVE_LIBGCRYPT)
115   gcry_cipher_hd_t aes_ctx;
116 #endif
117
118   gchar     *current_key;
119   guint8    *current_iv;
120
121   /* Accumulator for reading PAT/PMT/PCR from
122    * the stream so we can set timestamps/segments
123    * and switch cleanly */
124   GstBuffer *pending_pcr_buffer;
125 #ifdef TIZEN_FEATURE_HLSDEMUX_DISCONT_SEQUENCE
126   gint failed_count;
127 #endif
128   GstHLSTSReader tsreader;
129 };
130
131 typedef struct {
132   guint8 data[16];
133 } GstHLSKey;
134
135 /**
136  * GstHLSDemux:
137  *
138  * Opaque #GstHLSDemux data structure.
139  */
140 struct _GstHLSDemux
141 {
142   GstAdaptiveDemux parent;
143
144   gint srcpad_counter;
145
146   /* Decryption key cache: url => GstHLSKey */
147   GHashTable *keys;
148   GMutex      keys_lock;
149
150   /* FIXME: check locking, protected automatically by manifest_lock already? */
151   /* The master playlist with the available variant streams */
152   GstHLSMasterPlaylist *master;
153
154   GstHLSVariantStream  *current_variant;
155   /* The previous variant, used to transition streams over */
156   GstHLSVariantStream  *previous_variant;
157
158   gboolean streams_aware;
159 };
160
161 struct _GstHLSDemuxClass
162 {
163   GstAdaptiveDemuxClass parent_class;
164 };
165
166
167 void gst_hlsdemux_tsreader_init (GstHLSTSReader *r);
168 void gst_hlsdemux_tsreader_set_type (GstHLSTSReader *r, GstHLSTSReaderType rtype);
169
170 gboolean gst_hlsdemux_tsreader_find_pcrs (GstHLSTSReader *r, GstBuffer **buffer,
171     GstClockTime *first_pcr, GstClockTime *last_pcr, GstTagList **tags);
172
173 GType gst_hls_demux_get_type (void);
174
175 G_END_DECLS
176 #endif /* __GST_HLS_DEMUX_H__ */