MythTV master
HLSReader.h
Go to the documentation of this file.
1#ifndef HLS_READER_H
2#define HLS_READER_H
3
4#include <QObject>
5#include <QString>
6#include <QUrl>
7#include <QTextStream>
8
9/*
10 Use MythSingleDownload.
11
12 MythDownloadManager leaks memory and QNetworkAccessManager can only
13 handle six simultaneous downloads. Each HLS stream can be
14 downloading the playlist and the segments at the same time, so the
15 limit of six could impact performance if recording more than three
16 HLS channels.
17*/
18
19#ifdef HLS_USE_MYTHDOWNLOADMANAGER
21#else
23#endif
24
25#include "libmythtv/mythtvexp.h"
26
27#include "HLSSegment.h"
28#include "HLSStream.h"
29#include "HLSStreamWorker.h"
30#include "HLSPlaylistWorker.h"
31
32
34{
35 friend class HLSStreamWorker;
36 friend class HLSPlaylistWorker;
37
38 public:
39 using StreamContainer = QMap<QString, HLSRecStream* >;
40 using SegmentContainer = QVector<HLSRecSegment>;
41
42 HLSReader(int inputId) { m_inputId = inputId; };
43 ~HLSReader(void);
44
45 bool Open(const QString & m3u, int bitrate_index = 0);
46 void Close(bool quiet = false);
47 qint64 Read(uint8_t* buffer, qint64 len);
48 void Throttle(bool val);
49 bool IsThrottled(void) const { return m_throttle; }
50 bool IsOpen(const QString& url) const
51 { return m_curstream && m_m3u8 == url; }
52 bool FatalError(void) const { return m_fatal; }
53
54 bool LoadMetaPlaylists(MythSingleDownload& downloader);
55 void ResetStream(void)
56 { QMutexLocker lock(&m_streamLock); m_curstream = nullptr; }
57 void ResetSequence(void) { m_curSeq = -1; }
58
59 QString StreamURL(void) const
60 { return QString("%1").arg(m_curstream ? m_curstream->M3U8Url() : ""); }
61
62#ifdef HLS_USE_MYTHDOWNLOADMANAGER // MythDownloadManager leaks memory
63 static bool DownloadURL(const QString &url, QByteArray *buffer);
64#endif
65 static void CancelURL(const QString &url);
66 static void CancelURL(const QStringList &urls);
67
68 static bool IsValidPlaylist(QTextStream & text);
69
70 protected:
71 void Cancel(bool quiet = false);
72 bool LoadSegments(MythSingleDownload& downloader);
73 uint PercentBuffered(void) const;
74 std::chrono::seconds TargetDuration(void) const
75 { return (m_curstream ? m_curstream->TargetDuration() : 0s); }
76
77 void AllowPlaylistSwitch(void) { m_bandwidthCheck = true; }
78
79 void PlaylistGood(void);
80 void PlaylistRetrying(void);
81 int PlaylistRetryCount(void) const;
82
83 private:
84 bool ParseM3U8(const QByteArray & buffer, HLSRecStream* stream = nullptr);
85 void DecreaseBitrate(int progid);
86 void IncreaseBitrate(int progid);
87
88 // Downloading
89 int DownloadSegmentData(MythSingleDownload& downloader, HLSRecStream* hls,
90 HLSRecSegment& segment, int playlist_size);
91
92 // Debug
93 void EnableDebugging(void);
94
95 private:
96 QString m_m3u8;
100 HLSRecStream *m_curstream {nullptr};
101 int64_t m_curSeq {-1};
102 int m_bitrateIndex {0};
103
104 bool m_fatal {false};
105 bool m_cancel {false};
106 bool m_throttle {true};
107 // Only print one time that the media is encrypted
108 bool m_aesMsg {false};
109
110 HLSPlaylistWorker *m_playlistWorker {nullptr};
111 HLSStreamWorker *m_streamWorker {nullptr};
112
113 int m_playlistSize {0};
114 bool m_bandwidthCheck {false};
115 uint m_prebufferCnt {10};
116
117 QMutex m_seqLock;
118
119 mutable QMutex m_streamLock;
120
121 mutable QMutex m_workerLock;
122
124 QWaitCondition m_throttleCond;
125
126 bool m_debug {false};
127 int m_debugCnt {0};
128
129 // Downloading
130 int m_slowCnt {0};
131 QByteArray m_buffer;
132 QMutex m_bufLock;
133
134 // Log message
135 int m_inputId {0};
136};
137
138#endif // HLS_READER_H
void AllowPlaylistSwitch(void)
Definition: HLSReader.h:77
QWaitCondition m_throttleCond
Definition: HLSReader.h:124
QMutex m_bufLock
Definition: HLSReader.h:132
StreamContainer m_streams
Definition: HLSReader.h:98
QMutex m_seqLock
Definition: HLSReader.h:117
QMutex m_throttleLock
Definition: HLSReader.h:123
bool IsThrottled(void) const
Definition: HLSReader.h:49
QVector< HLSRecSegment > SegmentContainer
Definition: HLSReader.h:40
static void CancelURL(const QStringList &urls)
bool IsOpen(const QString &url) const
Definition: HLSReader.h:50
QMutex m_workerLock
Definition: HLSReader.h:121
QByteArray m_buffer
Definition: HLSReader.h:131
static void CancelURL(const QString &url)
SegmentContainer m_segments
Definition: HLSReader.h:99
std::chrono::seconds TargetDuration(void) const
Definition: HLSReader.h:74
HLSReader(int inputId)
Definition: HLSReader.h:42
void ResetStream(void)
Definition: HLSReader.h:55
QString StreamURL(void) const
Definition: HLSReader.h:59
QMap< QString, HLSRecStream * > StreamContainer
Definition: HLSReader.h:39
QMutex m_streamLock
Definition: HLSReader.h:119
void ResetSequence(void)
Definition: HLSReader.h:57
QString m_segmentBase
Definition: HLSReader.h:97
bool FatalError(void) const
Definition: HLSReader.h:52
QString m_m3u8
Definition: HLSReader.h:96
unsigned int uint
Definition: freesurround.h:24
int quiet
#define MTV_PUBLIC
Definition: mythtvexp.h:15