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