MythTV  master
HLSStream.h
Go to the documentation of this file.
1 #ifndef HLS_STREAM_
2 #define HLS_STREAM_
3 
4 #ifdef USING_LIBCRYPTO
5 // encryption related stuff
6 #include <openssl/aes.h>
7 #define AES_BLOCK_SIZE 16 // HLS only support AES-128
8 #endif // USING_LIBCRYPTO
9 
10 #include <QMap>
11 #include <QQueue>
12 
13 #include "mythsingledownload.h"
14 #include "HLSSegment.h"
15 
17 {
18  friend class HLSReader;
19 
20  public:
21 #ifdef USING_LIBCRYPTO
22  using AESKeyMap = QMap<QString, AES_KEY* >;
23 #endif
24 
25  HLSRecStream(int seq, uint64_t bitrate, QString m3u8_url, QString segment_base_url);
26  ~HLSRecStream(void);
27 
28  int Read(uint8_t* buffer, int len);
29 
30  QString toString(void) const;
31  int Id(void) const { return m_id; }
32  int Version(void) const { return m_version; }
33  void SetVersion(int x) { m_version = x; }
34  int TargetDuration(void) const { return m_targetDuration; }
35  void SetTargetDuration(int x) { m_targetDuration = x; }
36  uint64_t AverageBandwidth(void) const { return static_cast<uint64_t>(m_bandwidth); }
37  uint64_t Bitrate(void) const { return m_bitrate; }
38  void SetBitrate(uint64_t bitrate) { m_bitrate = bitrate; }
39  uint64_t CurrentByteRate(void) const { return m_curByteRate; }
40  void SetCurrentByteRate(uint64_t byterate) { m_curByteRate = byterate; }
41  bool Cache(void) const { return m_cache; }
42  void SetCache(bool x) { m_cache = x; }
43  bool Live(void) const { return m_live; }
44  void SetLive(bool x) { m_live = x; }
45  QString M3U8Url(void) const { return m_m3u8Url; }
46  QString SegmentBaseUrl(void) const { return m_segmentBaseUrl; }
47  void SetSegmentBaseUrl(const QString &n) { m_segmentBaseUrl = n; }
48 
49  uint Duration(void) const;
50  uint NumCachedSegments(void) const;
51  uint NumReleasedSegments(void) const;
52  uint NumTotalSegments(void) const;
53 
54  void Good(void) { m_retries = 0; }
55  void Retrying(void) { ++m_retries; }
56  int RetryCount(void) const { return m_retries; }
57 
58  static bool IsGreater(const HLSRecStream *s1, const HLSRecStream *s2);
59  bool operator<(const HLSRecStream &b) const;
60  bool operator>(const HLSRecStream &b) const;
61 
62 #ifdef USING_LIBCRYPTO
63  protected:
64  bool DownloadKey(MythSingleDownload& downloader,
65  const QString& keypath, AES_KEY* aeskey);
66  bool DecodeData(MythSingleDownload& downloader,
67  const uint8_t *IV, const QString& keypath,
68  QByteArray& data, int64_t sequence);
69  bool SetAESIV(QString line);
70  bool IVLoaded(void) const { return m_ivLoaded; }
71 
72  uint8_t *AESIV(void) { return m_aesIV; }
73  void SetKeyPath(const QString& x) { m_keypath = x; }
74 #endif // USING_LIBCRYPTO
75 
76  protected:
77  void AverageBandwidth(int64_t bandwidth);
78 
79  private:
80  int m_id; // program id
81  int m_version {1}; // protocol version should be 1
82  int m_targetDuration {-1}; // maximum duration per segment (s)
83  uint64_t m_curByteRate {0};
84  uint64_t m_bitrate; // bitrate of stream content (bits per second)
85  int64_t m_duration {0LL}; // duration of the stream in seconds
86  bool m_live {true};
87  int64_t m_bandwidth {0}; // measured average download bandwidth (bits/second)
88  double m_sumBandwidth {0.0};
89  QQueue<int64_t> m_bandwidthSegs;
90 
91  QString m_m3u8Url ; // uri to m3u8
92  QString m_segmentBaseUrl; // uri to base for relative segments (m3u8 redirect target)
93  mutable QMutex m_lock;
94  bool m_cache {false}; // allow caching
95  int m_retries {0};
96 
97 #ifdef USING_LIBCRYPTO
98  private:
99  QString m_keypath; // URL path of the encrypted key
100  bool m_ivLoaded {false};
101  uint8_t m_aesIV[AES_BLOCK_SIZE] {0};// IV used when decypher the block
102  AESKeyMap m_aesKeys; // AES-128 keys by path
103 #endif // USING_LIBCRYPTO
104 };
105 
106 #endif // HLS_STREAM_H
int Version(void) const
Definition: HLSStream.h:32
bool operator<(const HLSRecStream &b) const
Definition: HLSStream.cpp:154
QString M3U8Url(void) const
Definition: HLSStream.h:45
uint NumReleasedSegments(void) const
void SetCurrentByteRate(uint64_t byterate)
Definition: HLSStream.h:40
double m_sumBandwidth
Definition: HLSStream.h:88
bool operator>(const HLSRecStream &b) const
Definition: HLSStream.cpp:159
int RetryCount(void) const
Definition: HLSStream.h:56
int TargetDuration(void) const
Definition: HLSStream.h:34
bool Cache(void) const
Definition: HLSStream.h:41
void SetBitrate(uint64_t bitrate)
Definition: HLSStream.h:38
QString SegmentBaseUrl(void) const
Definition: HLSStream.h:46
int64_t m_bandwidth
Definition: HLSStream.h:87
void SetVersion(int x)
Definition: HLSStream.h:33
QString m_segmentBaseUrl
Definition: HLSStream.h:92
int Read(uint8_t *buffer, int len)
bool m_live
Definition: HLSStream.h:86
int64_t m_duration
Definition: HLSStream.h:85
static bool IsGreater(const HLSRecStream *s1, const HLSRecStream *s2)
QQueue< int64_t > m_bandwidthSegs
Definition: HLSStream.h:89
int m_version
Definition: HLSStream.h:81
void SetTargetDuration(int x)
Definition: HLSStream.h:35
uint64_t Bitrate(void) const
Definition: HLSStream.h:37
void Good(void)
Definition: HLSStream.h:54
uint NumTotalSegments(void) const
QMutex m_lock
Definition: HLSStream.h:93
uint64_t m_bitrate
Definition: HLSStream.h:84
uint Duration(void) const
Definition: HLSStream.cpp:148
unsigned int uint
Definition: compat.h:140
bool Live(void) const
Definition: HLSStream.h:43
int Id(void) const
Definition: HLSStream.h:31
~HLSRecStream(void)
Definition: HLSStream.cpp:21
int m_targetDuration
Definition: HLSStream.h:82
uint64_t AverageBandwidth(void) const
Definition: HLSStream.h:36
void SetLive(bool x)
Definition: HLSStream.h:44
void Retrying(void)
Definition: HLSStream.h:55
int m_retries
Definition: HLSStream.h:95
uint64_t m_curByteRate
Definition: HLSStream.h:83
HLSRecStream(int seq, uint64_t bitrate, QString m3u8_url, QString segment_base_url)
Definition: HLSStream.cpp:12
void SetSegmentBaseUrl(const QString &n)
Definition: HLSStream.h:47
QString toString(void) const
Definition: HLSStream.cpp:33
QString m_m3u8Url
Definition: HLSStream.h:91
bool m_cache
Definition: HLSStream.h:94
void SetCache(bool x)
Definition: HLSStream.h:42
uint64_t CurrentByteRate(void) const
Definition: HLSStream.h:39
uint NumCachedSegments(void) const