MythTV  master
httplivestreambuffer.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * httplivestreambuffer.cpp
3  * MythTV
4  *
5  * Created by Jean-Yves Avenard on 6/05/12.
6  * Copyright (c) 2012 Bubblestuff Pty Ltd. All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program 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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22 
23 #ifndef MythXCode_hlsbuffer_h
24 #define MythXCode_hlsbuffer_h
25 
27 
29 class HLSStream;
30 class HLSSegment;
31 class StreamWorker;
32 class PlaylistWorker;
33 class HLSPlayback;
34 
35 using StreamsList = QList<HLSStream*>;
36 
38 {
39 public:
40  explicit HLSRingBuffer(const QString &lfilename);
41  HLSRingBuffer(const QString &lfilename, bool open);
42  ~HLSRingBuffer() override;
43 
44  bool IsOpen(void) const override; // RingBuffer
45  long long GetReadPosition(void) const override; // RingBuffer
46  bool OpenFile(const QString &lfilename,
47  std::chrono::milliseconds retry_ms = kDefaultOpenTimeout) override; // RingBuffer
48  bool IsStreamed(void) override { return false; } // RingBuffer
49  bool IsSeekingAllowed(void) override { return !m_error; } // RingBuffer
50  bool IsBookmarkAllowed(void) override { return true; } // RingBuffer
51  static bool IsHTTPLiveStreaming(QByteArray *s);
52  static bool TestForHTTPLiveStreaming(const QString &filename);
53  bool SaveToDisk(const QString &filename, int segstart = 0, int segend = -1);
54  int NumStreams(void) const;
55  void Interrupt(void);
56  void Continue(void);
57  int DurationForBytes(uint size);
58 
59 protected:
60  int SafeRead(void *data, uint sz) override; // RingBuffer
61  long long GetRealFileSizeInternal(void) const override; // RingBuffer
62  long long SeekInternal(long long pos, int whence) override; // RingBuffer
63 
64 private:
65  void FreeStreamsList(QList<HLSStream*> *streams) const;
66  HLSStream *GetStreamForSegment(int segnum) const;
67  HLSStream *GetStream(int wanted, const StreamsList *streams = nullptr) const;
68  HLSStream *GetFirstStream(const StreamsList *streams = nullptr) const;
69  HLSStream *GetLastStream(const StreamsList *streams = nullptr) const;
70  HLSStream *FindStream(const HLSStream *hls_new, const StreamsList *streams = nullptr) const;
71  HLSStream *GetCurrentStream(void) const;
72  static QString ParseAttributes(const QString &line, const char *attr);
73  static int ParseDecimalValue(const QString &line, int &target);
74  static int ParseSegmentInformation(const HLSStream *hls, const QString &line,
75  int &duration, QString &title);
76  static int ParseTargetDuration(HLSStream *hls, const QString &line);
77  HLSStream *ParseStreamInformation(const QString &line, const QString &uri) const;
78  static int ParseMediaSequence(HLSStream *hls, const QString &line);
79  int ParseKey(HLSStream *hls, const QString &line);
80  static int ParseProgramDateTime(HLSStream *hls, const QString &line);
81  static int ParseAllowCache(HLSStream *hls, const QString &line);
82  static int ParseVersion(const QString &line, int &version);
83  static int ParseEndList(HLSStream *hls);
84  static int ParseDiscontinuity(HLSStream *hls, const QString &line);
85  int ParseM3U8(const QByteArray *buffer, StreamsList *streams = nullptr);
86  int Prefetch(int count);
87  void SanityCheck(const HLSStream *hls) const;
88  HLSSegment *GetSegment(int segnum, std::chrono::milliseconds timeout = 1s);
89  int NumSegments(void) const;
90  int ChooseSegment(int stream) const;
91  int64_t SizeMedia(void) const;
92  void WaitUntilBuffered(void);
93  void SanitizeStreams(StreamsList *streams = nullptr);
94 
95  // private member variables
96  QString m_m3u8; // M3U8 url
97  QByteArray m_peeked;
98 
99  HLSPlayback *m_playback {nullptr};
100 
101  /* state */
102  StreamsList m_streams; // bandwidth adaptation
103  mutable QMutex m_lock; // protect general class members
104  bool m_meta {false}; // meta playlist
105  bool m_error {false}; // parsing error
106 #ifdef USING_LIBCRYPTO
107  bool m_aesmsg {false}; // only print one time that the media is encrypted
108 #endif
109  int m_startup {0}; // starting segment (where seek start)
115  int64_t m_bitrate {0};
121  bool m_seektoend {false};
122 
123  friend class StreamWorker;
125  friend class PlaylistWorker;
127  FILE *m_fd {nullptr};
128  bool m_interrupted {false};
129  bool m_killed {false};
130 };
131 
132 #endif
HLSRingBuffer::m_error
bool m_error
Definition: httplivestreambuffer.h:105
HLSRingBuffer::ParseProgramDateTime
static int ParseProgramDateTime(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2096
HLSRingBuffer::m_meta
bool m_meta
Definition: httplivestreambuffer.h:104
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:101
HLSRingBuffer::ChooseSegment
int ChooseSegment(int stream) const
Definition: httplivestreambuffer.cpp:2494
HLSRingBuffer::HLSRingBuffer
HLSRingBuffer(const QString &lfilename)
Definition: httplivestreambuffer.cpp:1617
HLSRingBuffer::IsBookmarkAllowed
bool IsBookmarkAllowed(void) override
Definition: httplivestreambuffer.h:50
HLSRingBuffer::FindStream
HLSStream * FindStream(const HLSStream *hls_new, const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1719
HLSRingBuffer::NumStreams
int NumStreams(void) const
Definition: httplivestreambuffer.cpp:2478
HLSRingBuffer::SizeMedia
int64_t SizeMedia(void) const
Definition: httplivestreambuffer.cpp:2731
HLSRingBuffer::NumSegments
int NumSegments(void) const
Definition: httplivestreambuffer.cpp:2483
HLSRingBuffer::TestForHTTPLiveStreaming
static bool TestForHTTPLiveStreaming(const QString &filename)
Definition: httplivestreambuffer.cpp:1790
HLSRingBuffer::ParseKey
int ParseKey(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2011
HLSRingBuffer::GetFirstStream
HLSStream * GetFirstStream(const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1701
HLSRingBuffer::SafeRead
int SafeRead(void *data, uint sz) override
Definition: httplivestreambuffer.cpp:2777
HLSRingBuffer::SanitizeStreams
void SanitizeStreams(StreamsList *streams=nullptr)
Streams may not be all starting at the same sequence number, so attempt to align their starting seque...
Definition: httplivestreambuffer.cpp:2540
MythMediaBuffer
Definition: mythmediabuffer.h:59
StreamWorker
Definition: httplivestreambuffer.cpp:991
MythDownloadManager
Definition: mythdownloadmanager.h:48
HLSRingBuffer::m_interrupted
bool m_interrupted
Definition: httplivestreambuffer.h:128
HLSRingBuffer::m_playback
HLSPlayback * m_playback
Definition: httplivestreambuffer.h:99
HLSRingBuffer::ParseEndList
static int ParseEndList(HLSStream *hls)
Definition: httplivestreambuffer.cpp:2162
mythburn.FILE
int FILE
Definition: mythburn.py:138
HLSRingBuffer::m_bitrate
int64_t m_bitrate
assumed bitrate of playback used for the purpose of calculating length and seek position.
Definition: httplivestreambuffer.h:115
HLSRingBuffer::m_playlistworker
PlaylistWorker * m_playlistworker
Definition: httplivestreambuffer.h:126
HLSRingBuffer::IsHTTPLiveStreaming
static bool IsHTTPLiveStreaming(QByteArray *s)
Definition: httplivestreambuffer.cpp:1756
HLSRingBuffer::m_fd
FILE * m_fd
Definition: httplivestreambuffer.h:127
HLSRingBuffer::OpenFile
bool OpenFile(const QString &lfilename, std::chrono::milliseconds retry_ms=kDefaultOpenTimeout) override
Opens an HTTP Live Stream for reading.
Definition: httplivestreambuffer.cpp:2611
HLSRingBuffer::SeekInternal
long long SeekInternal(long long pos, int whence) override
Definition: httplivestreambuffer.cpp:2892
HLSSegment
Definition: httplivestreambuffer.cpp:129
HLSRingBuffer::m_peeked
QByteArray m_peeked
Definition: httplivestreambuffer.h:97
HLSRingBuffer::ParseDecimalValue
static int ParseDecimalValue(const QString &line, int &target)
Return the decimal argument in a line of type: blah:<decimal> presence of value <decimal> is compulso...
Definition: httplivestreambuffer.cpp:1847
PlaylistWorker
Definition: httplivestreambuffer.cpp:1322
HLSRingBuffer
Definition: httplivestreambuffer.h:37
HLSRingBuffer::GetSegment
HLSSegment * GetSegment(int segnum, std::chrono::milliseconds timeout=1s)
Retrieve segment [segnum] from any available streams.
Definition: httplivestreambuffer.cpp:2442
HLSRingBuffer::Prefetch
int Prefetch(int count)
Preferetch the first x segments of the stream.
Definition: httplivestreambuffer.cpp:2391
HLSRingBuffer::WaitUntilBuffered
void WaitUntilBuffered(void)
Wait until we have enough segments buffered to allow smooth playback Do not wait if VOD and at end of...
Definition: httplivestreambuffer.cpp:2748
HLSRingBuffer::ParseTargetDuration
static int ParseTargetDuration(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:1921
HLSRingBuffer::m_lock
QMutex m_lock
Definition: httplivestreambuffer.h:103
HLSRingBuffer::Continue
void Continue(void)
Definition: httplivestreambuffer.cpp:3069
HLSRingBuffer::GetStreamForSegment
HLSStream * GetStreamForSegment(int segnum) const
Definition: httplivestreambuffer.cpp:1677
HLSRingBuffer::SaveToDisk
bool SaveToDisk(const QString &filename, int segstart=0, int segend=-1)
Definition: httplivestreambuffer.cpp:2699
HLSRingBuffer::Interrupt
void Interrupt(void)
Definition: httplivestreambuffer.cpp:3060
HLSRingBuffer::m_m3u8
QString m_m3u8
Definition: httplivestreambuffer.h:96
HLSRingBuffer::ParseDiscontinuity
static int ParseDiscontinuity(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2174
HLSRingBuffer::ParseMediaSequence
static int ParseMediaSequence(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:1982
HLSRingBuffer::ParseSegmentInformation
static int ParseSegmentInformation(const HLSStream *hls, const QString &line, int &duration, QString &title)
Definition: httplivestreambuffer.cpp:1862
HLSRingBuffer::ParseAttributes
static QString ParseAttributes(const QString &line, const char *attr)
Definition: httplivestreambuffer.cpp:1822
HLSRingBuffer::m_streamworker
StreamWorker * m_streamworker
Definition: httplivestreambuffer.h:124
mythmediabuffer.h
HLSRingBuffer::GetStream
HLSStream * GetStream(int wanted, const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1687
HLSRingBuffer::m_startup
int m_startup
Definition: httplivestreambuffer.h:109
HLSRingBuffer::GetReadPosition
long long GetReadPosition(void) const override
Definition: httplivestreambuffer.cpp:3048
HLSPlayback
Definition: httplivestreambuffer.cpp:939
HLSRingBuffer::GetRealFileSizeInternal
long long GetRealFileSizeInternal(void) const override
Definition: httplivestreambuffer.cpp:2886
HLSRingBuffer::~HLSRingBuffer
~HLSRingBuffer() override
Definition: httplivestreambuffer.cpp:1636
HLSRingBuffer::IsSeekingAllowed
bool IsSeekingAllowed(void) override
Definition: httplivestreambuffer.h:49
HLSRingBuffer::ParseVersion
static int ParseVersion(const QString &line, int &version)
Definition: httplivestreambuffer.cpp:2131
HLSRingBuffer::ParseM3U8
int ParseM3U8(const QByteArray *buffer, StreamsList *streams=nullptr)
Definition: httplivestreambuffer.cpp:2181
StreamsList
QList< HLSStream * > StreamsList
Definition: httplivestreambuffer.h:35
HLSRingBuffer::FreeStreamsList
void FreeStreamsList(QList< HLSStream * > *streams) const
Definition: httplivestreambuffer.cpp:1663
HLSRingBuffer::m_streams
StreamsList m_streams
Definition: httplivestreambuffer.h:102
HLSRingBuffer::ParseStreamInformation
HLSStream * ParseStreamInformation(const QString &line, const QString &uri) const
Definition: httplivestreambuffer.cpp:1939
HLSRingBuffer::m_killed
bool m_killed
Definition: httplivestreambuffer.h:129
HLSRingBuffer::SanityCheck
void SanityCheck(const HLSStream *hls) const
Definition: httplivestreambuffer.cpp:2416
HLSStream
Definition: httplivestreambuffer.cpp:461
HLSRingBuffer::IsStreamed
bool IsStreamed(void) override
Definition: httplivestreambuffer.h:48
build_compdb.filename
filename
Definition: build_compdb.py:21
MythMediaBuffer::kDefaultOpenTimeout
static constexpr std::chrono::milliseconds kDefaultOpenTimeout
Definition: mythmediabuffer.h:71
HLSRingBuffer::GetCurrentStream
HLSStream * GetCurrentStream(void) const
return the stream we are currently streaming from
Definition: httplivestreambuffer.cpp:1747
nv_python_libs.bbciplayer.bbciplayer_api.version
string version
Definition: bbciplayer_api.py:77
HLSRingBuffer::m_seektoend
bool m_seektoend
FFmpeg seek to the end of the stream in order to determine the length of the video.
Definition: httplivestreambuffer.h:121
HLSRingBuffer::GetLastStream
HLSStream * GetLastStream(const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1706
HLSRingBuffer::DurationForBytes
int DurationForBytes(uint size)
returns an estimated duration in ms for size amount of data returns 0 if we can't estimate the durati...
Definition: httplivestreambuffer.cpp:2862
uint
unsigned int uint
Definition: freesurround.h:24
HLSRingBuffer::IsOpen
bool IsOpen(void) const override
Definition: httplivestreambuffer.cpp:3055
HLSRingBuffer::ParseAllowCache
static int ParseAllowCache(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2107