• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gsthls.h"
31 #include <gst/adaptivedemux/gstadaptivedemux.h>
32 
33 #ifdef OHOS_EXT_FUNC
34 // ohos.ext.func.0013
35 #undef HAVE_OPENSSL
36 #undef HAVE_NETTLE
37 #undef HAVE_LIBGCRYPT
38 #endif
39 
40 #if defined(HAVE_OPENSSL)
41 #include <openssl/evp.h>
42 #elif defined(HAVE_NETTLE)
43 #include <nettle/aes.h>
44 #include <nettle/cbc.h>
45 #elif defined(HAVE_LIBGCRYPT)
46 #include <gcrypt.h>
47 #endif
48 
49 G_BEGIN_DECLS
50 
51 #define GST_TYPE_HLS_DEMUX \
52   (gst_hls_demux_get_type())
53 #define GST_HLS_DEMUX(obj) \
54   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_HLS_DEMUX,GstHLSDemux))
55 #define GST_HLS_DEMUX_CLASS(klass) \
56   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_HLS_DEMUX,GstHLSDemuxClass))
57 #define GST_IS_HLS_DEMUX(obj) \
58   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_HLS_DEMUX))
59 #define GST_IS_HLS_DEMUX_CLASS(klass) \
60   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_HLS_DEMUX))
61 #define GST_HLS_DEMUX_GET_CLASS(obj) \
62   (G_TYPE_INSTANCE_GET_CLASS ((obj),GST_TYPE_HLS_DEMUX,GstHLSDemuxClass))
63 #define GST_HLS_DEMUX_CAST(obj) \
64   ((GstHLSDemux *)obj)
65 
66 typedef struct _GstHLSDemux GstHLSDemux;
67 typedef struct _GstHLSDemuxClass GstHLSDemuxClass;
68 typedef struct _GstHLSDemuxStream GstHLSDemuxStream;
69 typedef struct _GstHLSTSReader GstHLSTSReader;
70 
71 #define GST_HLS_DEMUX_STREAM_CAST(stream) ((GstHLSDemuxStream *)(stream))
72 
73 typedef enum {
74   GST_HLS_TSREADER_NONE,
75   GST_HLS_TSREADER_MPEGTS,
76   GST_HLS_TSREADER_ID3
77 } GstHLSTSReaderType;
78 
79 struct _GstHLSTSReader
80 {
81   GstHLSTSReaderType rtype;
82   gboolean have_id3;
83 
84   gint packet_size;
85   gint pmt_pid;
86   gint pcr_pid;
87 
88   GstClockTime last_pcr;
89   GstClockTime first_pcr;
90 };
91 
92 struct _GstHLSDemuxStream
93 {
94   GstAdaptiveDemuxStream adaptive_demux_stream;
95 
96   GstHLSTSReaderType stream_type;
97 
98   GstM3U8 *playlist;
99   gboolean is_primary_playlist;
100 
101   gboolean do_typefind;         /* Whether we need to typefind the next buffer */
102   GstBuffer *pending_typefind_buffer; /* for collecting data until typefind succeeds */
103 
104   GstAdapter *pending_encrypted_data;  /* for chunking data into 16 byte multiples for decryption */
105   GstBuffer *pending_decrypted_buffer; /* last decrypted buffer for pkcs7 unpadding.
106                                           We only know that it is the last at EOS */
107   guint64 current_offset;              /* offset we're currently at */
108   gboolean reset_pts;
109 
110   /* decryption tooling */
111 #if defined(HAVE_OPENSSL)
112 # if OPENSSL_VERSION_NUMBER < 0x10100000L
113   EVP_CIPHER_CTX aes_ctx;
114 # else
115   EVP_CIPHER_CTX *aes_ctx;
116 # endif
117 #elif defined(HAVE_NETTLE)
118   struct CBC_CTX (struct aes_ctx, AES_BLOCK_SIZE) aes_ctx;
119 #elif defined(HAVE_LIBGCRYPT)
120   gcry_cipher_hd_t aes_ctx;
121 #endif
122 
123   gchar     *current_key;
124   guint8    *current_iv;
125 
126   /* Accumulator for reading PAT/PMT/PCR from
127    * the stream so we can set timestamps/segments
128    * and switch cleanly */
129   GstBuffer *pending_pcr_buffer;
130 
131   GstHLSTSReader tsreader;
132 };
133 
134 typedef struct {
135   guint8 data[16];
136 } GstHLSKey;
137 
138 /**
139  * GstHLSDemux:
140  *
141  * Opaque #GstHLSDemux data structure.
142  */
143 struct _GstHLSDemux
144 {
145   GstAdaptiveDemux parent;
146 
147   gint srcpad_counter;
148 
149   /* Decryption key cache: url => GstHLSKey */
150   GHashTable *keys;
151   GMutex      keys_lock;
152 
153   /* FIXME: check locking, protected automatically by manifest_lock already? */
154   /* The master playlist with the available variant streams */
155   GstHLSMasterPlaylist *master;
156 
157   GstHLSVariantStream  *current_variant;
158 };
159 
160 struct _GstHLSDemuxClass
161 {
162   GstAdaptiveDemuxClass parent_class;
163 };
164 
165 
166 void gst_hlsdemux_tsreader_init (GstHLSTSReader *r);
167 void gst_hlsdemux_tsreader_set_type (GstHLSTSReader *r, GstHLSTSReaderType rtype);
168 
169 gboolean gst_hlsdemux_tsreader_find_pcrs (GstHLSTSReader *r, GstBuffer **buffer,
170     GstClockTime *first_pcr, GstClockTime *last_pcr, GstTagList **tags);
171 
172 GType gst_hls_demux_get_type (void);
173 
174 G_END_DECLS
175 #endif /* __GST_HLS_DEMUX_H__ */
176