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  std::chrono::seconds TargetDuration(void) const { return m_targetDuration; }
35  void SetTargetDuration(std::chrono::seconds 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  std::chrono::seconds 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 QByteArray& 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  QByteArray 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  std::chrono::seconds m_targetDuration {-1s}; // maximum duration per segment
83  uint64_t m_curByteRate {0};
84  uint64_t m_bitrate; // bitrate of stream content (bits per second)
85  std::chrono::seconds m_duration {0s}; // duration of the stream
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  QByteArray 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
HLSRecStream::~HLSRecStream
~HLSRecStream(void)
Definition: HLSStream.cpp:21
HLSRecStream::Version
int Version(void) const
Definition: HLSStream.h:32
HLSRecStream::Cache
bool Cache(void) const
Definition: HLSStream.h:41
HLSRecStream::Duration
std::chrono::seconds Duration(void) const
Definition: HLSStream.cpp:146
HLSRecStream::NumReleasedSegments
uint NumReleasedSegments(void) const
HLSRecStream::RetryCount
int RetryCount(void) const
Definition: HLSStream.h:56
HLSRecStream::HLSRecStream
HLSRecStream(int seq, uint64_t bitrate, QString m3u8_url, QString segment_base_url)
Definition: HLSStream.cpp:12
HLSRecStream::SetVersion
void SetVersion(int x)
Definition: HLSStream.h:33
HLSRecStream::operator<
bool operator<(const HLSRecStream &b) const
Definition: HLSStream.cpp:152
HLSRecStream::IsGreater
static bool IsGreater(const HLSRecStream *s1, const HLSRecStream *s2)
HLSRecStream::m_m3u8Url
QString m_m3u8Url
Definition: HLSStream.h:91
mythsingledownload.h
HLSRecStream::Good
void Good(void)
Definition: HLSStream.h:54
HLSRecStream::m_duration
std::chrono::seconds m_duration
Definition: HLSStream.h:85
HLSRecStream::Read
int Read(uint8_t *buffer, int len)
HLSRecStream
Definition: HLSStream.h:16
HLSRecStream::m_live
bool m_live
Definition: HLSStream.h:86
HLSRecStream::SetLive
void SetLive(bool x)
Definition: HLSStream.h:44
HLSRecStream::NumTotalSegments
uint NumTotalSegments(void) const
HLSRecStream::SegmentBaseUrl
QString SegmentBaseUrl(void) const
Definition: HLSStream.h:46
HLSRecStream::m_targetDuration
std::chrono::seconds m_targetDuration
Definition: HLSStream.h:82
HLSRecStream::Retrying
void Retrying(void)
Definition: HLSStream.h:55
HLSRecStream::SetBitrate
void SetBitrate(uint64_t bitrate)
Definition: HLSStream.h:38
HLSRecStream::TargetDuration
std::chrono::seconds TargetDuration(void) const
Definition: HLSStream.h:34
HLSRecStream::AverageBandwidth
uint64_t AverageBandwidth(void) const
Definition: HLSStream.h:36
HLSRecStream::m_segmentBaseUrl
QString m_segmentBaseUrl
Definition: HLSStream.h:92
HLSRecStream::toString
QString toString(void) const
Definition: HLSStream.cpp:33
uint
unsigned int uint
Definition: compat.h:140
HLSRecStream::m_bitrate
uint64_t m_bitrate
Definition: HLSStream.h:84
HLSRecStream::m_retries
int m_retries
Definition: HLSStream.h:95
HLSRecStream::m_bandwidthSegs
QQueue< int64_t > m_bandwidthSegs
Definition: HLSStream.h:89
HLSReader
Definition: HLSReader.h:34
MythSingleDownload
Definition: mythsingledownload.h:25
HLSRecStream::SetTargetDuration
void SetTargetDuration(std::chrono::seconds x)
Definition: HLSStream.h:35
HLSRecStream::m_id
int m_id
Definition: HLSStream.h:80
HLSRecStream::Live
bool Live(void) const
Definition: HLSStream.h:43
HLSRecStream::Bitrate
uint64_t Bitrate(void) const
Definition: HLSStream.h:37
HLSRecStream::m_version
int m_version
Definition: HLSStream.h:81
HLSRecStream::m_lock
QMutex m_lock
Definition: HLSStream.h:93
HLSRecStream::operator>
bool operator>(const HLSRecStream &b) const
Definition: HLSStream.cpp:157
HLSRecStream::m_sumBandwidth
double m_sumBandwidth
Definition: HLSStream.h:88
HLSRecStream::SetCache
void SetCache(bool x)
Definition: HLSStream.h:42
HLSRecStream::SetSegmentBaseUrl
void SetSegmentBaseUrl(const QString &n)
Definition: HLSStream.h:47
HLSSegment.h
HLSRecStream::SetCurrentByteRate
void SetCurrentByteRate(uint64_t byterate)
Definition: HLSStream.h:40
HLSRecStream::Id
int Id(void) const
Definition: HLSStream.h:31
HLSRecStream::m_bandwidth
int64_t m_bandwidth
Definition: HLSStream.h:87
HLSRecStream::CurrentByteRate
uint64_t CurrentByteRate(void) const
Definition: HLSStream.h:39
HLSRecStream::m_cache
bool m_cache
Definition: HLSStream.h:94
HLSRecStream::NumCachedSegments
uint NumCachedSegments(void) const
HLSRecStream::M3U8Url
QString M3U8Url(void) const
Definition: HLSStream.h:45
HLSRecStream::m_curByteRate
uint64_t m_curByteRate
Definition: HLSStream.h:83