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 
28 
29 extern "C" {
30 #include "libavformat/avformat.h"
31 #include "libavformat/url.h"
32 }
33 
35 class HLSStream;
36 class HLSSegment;
37 class StreamWorker;
38 class PlaylistWorker;
39 class HLSPlayback;
40 
41 using StreamsList = QList<HLSStream*>;
42 
44 {
45 public:
46  explicit HLSRingBuffer(const QString &lfilename);
47  HLSRingBuffer(const QString &lfilename, bool open);
48  ~HLSRingBuffer() override;
49 
50  bool IsOpen(void) const override; // RingBuffer
51  long long GetReadPosition(void) const override; // RingBuffer
52  bool OpenFile(const QString &lfilename,
53  std::chrono::milliseconds retry_ms = kDefaultOpenTimeout) override; // RingBuffer
54  bool IsStreamed(void) override { return false; } // RingBuffer
55  bool IsSeekingAllowed(void) override { return !m_error; } // RingBuffer
56  bool IsBookmarkAllowed(void) override { return true; } // RingBuffer
57  static bool IsHTTPLiveStreaming(QByteArray *s);
58  static bool TestForHTTPLiveStreaming(const QString &filename);
59  bool SaveToDisk(const QString &filename, int segstart = 0, int segend = -1);
60  int NumStreams(void) const;
61  void Interrupt(void);
62  void Continue(void);
63  int DurationForBytes(uint size);
64 
65 protected:
66  int SafeRead(void *data, uint sz) override; // RingBuffer
67  long long GetRealFileSizeInternal(void) const override; // RingBuffer
68  long long SeekInternal(long long pos, int whence) override; // RingBuffer
69 
70 private:
71  void FreeStreamsList(QList<HLSStream*> *streams) const;
72  HLSStream *GetStreamForSegment(int segnum) const;
73  HLSStream *GetStream(int wanted, const StreamsList *streams = nullptr) const;
74  HLSStream *GetFirstStream(const StreamsList *streams = nullptr) const;
75  HLSStream *GetLastStream(const StreamsList *streams = nullptr) const;
76  HLSStream *FindStream(const HLSStream *hls_new, const StreamsList *streams = nullptr) const;
77  HLSStream *GetCurrentStream(void) const;
78  static QString ParseAttributes(const QString &line, const char *attr);
79  static int ParseDecimalValue(const QString &line, int &target);
80  static int ParseSegmentInformation(const HLSStream *hls, const QString &line,
81  int &duration, QString &title);
82  static int ParseTargetDuration(HLSStream *hls, const QString &line);
83  HLSStream *ParseStreamInformation(const QString &line, const QString &uri) const;
84  static int ParseMediaSequence(HLSStream *hls, const QString &line);
85  int ParseKey(HLSStream *hls, const QString &line);
86  static int ParseProgramDateTime(HLSStream *hls, const QString &line);
87  static int ParseAllowCache(HLSStream *hls, const QString &line);
88  static int ParseVersion(const QString &line, int &version);
89  static int ParseEndList(HLSStream *hls);
90  static int ParseDiscontinuity(HLSStream *hls, const QString &line);
91  int ParseM3U8(const QByteArray *buffer, StreamsList *streams = nullptr);
92  int Prefetch(int count);
93  void SanityCheck(const HLSStream *hls) const;
94  HLSSegment *GetSegment(int segnum, std::chrono::milliseconds timeout = 1s);
95  int NumSegments(void) const;
96  int ChooseSegment(int stream) const;
97  int64_t SizeMedia(void) const;
98  void WaitUntilBuffered(void);
99  void SanitizeStreams(StreamsList *streams = nullptr);
100 
101  // private member variables
102  QString m_m3u8; // M3U8 url
103  QByteArray m_peeked;
104 
106 
107  /* state */
108  StreamsList m_streams; // bandwidth adaptation
109  mutable QMutex m_lock; // protect general class members
110  bool m_meta {false}; // meta playlist
111  bool m_error {false}; // parsing error
112 #ifdef USING_LIBCRYPTO
113  bool m_aesmsg {false}; // only print one time that the media is encrypted
114 #endif
115  int m_startup {0}; // starting segment (where seek start)
121  int64_t m_bitrate {0};
127  bool m_seektoend {false};
128 
129  friend class StreamWorker;
131  friend class PlaylistWorker;
133  FILE *m_fd {nullptr};
134  bool m_interrupted {false};
135  bool m_killed {false};
136 };
137 
138 #endif
HLSRingBuffer::m_error
bool m_error
Definition: httplivestreambuffer.h:111
HLSRingBuffer::ParseProgramDateTime
static int ParseProgramDateTime(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2019
HLSRingBuffer::m_meta
bool m_meta
Definition: httplivestreambuffer.h:110
hardwareprofile.smolt.timeout
float timeout
Definition: smolt.py:102
HLSRingBuffer::ChooseSegment
int ChooseSegment(int stream) const
Definition: httplivestreambuffer.cpp:2403
HLSRingBuffer::HLSRingBuffer
HLSRingBuffer(const QString &lfilename)
Definition: httplivestreambuffer.cpp:1542
HLSRingBuffer::IsBookmarkAllowed
bool IsBookmarkAllowed(void) override
Definition: httplivestreambuffer.h:56
HLSRingBuffer::FindStream
HLSStream * FindStream(const HLSStream *hls_new, const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1644
HLSRingBuffer::NumStreams
int NumStreams(void) const
Definition: httplivestreambuffer.cpp:2387
HLSRingBuffer::SizeMedia
int64_t SizeMedia(void) const
Definition: httplivestreambuffer.cpp:2640
HLSRingBuffer::NumSegments
int NumSegments(void) const
Definition: httplivestreambuffer.cpp:2392
HLSRingBuffer::TestForHTTPLiveStreaming
static bool TestForHTTPLiveStreaming(const QString &filename)
Definition: httplivestreambuffer.cpp:1715
HLSRingBuffer::ParseKey
int ParseKey(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:1934
HLSRingBuffer::GetFirstStream
HLSStream * GetFirstStream(const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1626
HLSRingBuffer::SafeRead
int SafeRead(void *data, uint sz) override
Definition: httplivestreambuffer.cpp:2683
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:2449
MythMediaBuffer
Definition: mythmediabuffer.h:50
StreamWorker
Definition: httplivestreambuffer.cpp:924
MythDownloadManager
Definition: mythdownloadmanager.h:48
HLSRingBuffer::m_interrupted
bool m_interrupted
Definition: httplivestreambuffer.h:134
HLSRingBuffer::m_playback
HLSPlayback * m_playback
Definition: httplivestreambuffer.h:105
HLSRingBuffer::ParseEndList
static int ParseEndList(HLSStream *hls)
Definition: httplivestreambuffer.cpp:2085
mythburn.FILE
int FILE
Definition: mythburn.py:139
HLSRingBuffer::m_bitrate
int64_t m_bitrate
assumed bitrate of playback used for the purpose of calculating length and seek position.
Definition: httplivestreambuffer.h:121
HLSRingBuffer::m_playlistworker
PlaylistWorker * m_playlistworker
Definition: httplivestreambuffer.h:132
HLSRingBuffer::IsHTTPLiveStreaming
static bool IsHTTPLiveStreaming(QByteArray *s)
Definition: httplivestreambuffer.cpp:1681
HLSRingBuffer::m_fd
FILE * m_fd
Definition: httplivestreambuffer.h:133
HLSRingBuffer::OpenFile
bool OpenFile(const QString &lfilename, std::chrono::milliseconds retry_ms=kDefaultOpenTimeout) override
Opens an HTTP Live Stream for reading.
Definition: httplivestreambuffer.cpp:2520
HLSRingBuffer::SeekInternal
long long SeekInternal(long long pos, int whence) override
Definition: httplivestreambuffer.cpp:2798
HLSSegment
Definition: httplivestreambuffer.cpp:120
HLSRingBuffer::m_peeked
QByteArray m_peeked
Definition: httplivestreambuffer.h:103
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:1772
PlaylistWorker
Definition: httplivestreambuffer.cpp:1248
HLSRingBuffer
Definition: httplivestreambuffer.h:43
HLSRingBuffer::GetSegment
HLSSegment * GetSegment(int segnum, std::chrono::milliseconds timeout=1s)
Retrieve segment [segnum] from any available streams.
Definition: httplivestreambuffer.cpp:2351
HLSRingBuffer::Prefetch
int Prefetch(int count)
Preferetch the first x segments of the stream.
Definition: httplivestreambuffer.cpp:2300
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:2655
HLSRingBuffer::ParseTargetDuration
static int ParseTargetDuration(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:1844
HLSRingBuffer::m_lock
QMutex m_lock
Definition: httplivestreambuffer.h:109
HLSRingBuffer::Continue
void Continue(void)
Definition: httplivestreambuffer.cpp:2975
HLSRingBuffer::GetStreamForSegment
HLSStream * GetStreamForSegment(int segnum) const
Definition: httplivestreambuffer.cpp:1602
HLSRingBuffer::SaveToDisk
bool SaveToDisk(const QString &filename, int segstart=0, int segend=-1)
Definition: httplivestreambuffer.cpp:2608
HLSRingBuffer::Interrupt
void Interrupt(void)
Definition: httplivestreambuffer.cpp:2966
HLSRingBuffer::m_m3u8
QString m_m3u8
Definition: httplivestreambuffer.h:102
HLSRingBuffer::ParseDiscontinuity
static int ParseDiscontinuity(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2097
uint
unsigned int uint
Definition: compat.h:81
HLSRingBuffer::ParseMediaSequence
static int ParseMediaSequence(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:1905
HLSRingBuffer::ParseSegmentInformation
static int ParseSegmentInformation(const HLSStream *hls, const QString &line, int &duration, QString &title)
Definition: httplivestreambuffer.cpp:1785
HLSRingBuffer::ParseAttributes
static QString ParseAttributes(const QString &line, const char *attr)
Definition: httplivestreambuffer.cpp:1747
HLSRingBuffer::m_streamworker
StreamWorker * m_streamworker
Definition: httplivestreambuffer.h:130
mythmediabuffer.h
HLSRingBuffer::GetStream
HLSStream * GetStream(int wanted, const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1612
HLSRingBuffer::m_startup
int m_startup
Definition: httplivestreambuffer.h:115
HLSRingBuffer::GetReadPosition
long long GetReadPosition(void) const override
Definition: httplivestreambuffer.cpp:2954
HLSPlayback
Definition: httplivestreambuffer.cpp:872
HLSRingBuffer::GetRealFileSizeInternal
long long GetRealFileSizeInternal(void) const override
Definition: httplivestreambuffer.cpp:2792
HLSRingBuffer::~HLSRingBuffer
~HLSRingBuffer() override
Definition: httplivestreambuffer.cpp:1561
mythcorecontext.h
HLSRingBuffer::IsSeekingAllowed
bool IsSeekingAllowed(void) override
Definition: httplivestreambuffer.h:55
HLSRingBuffer::ParseVersion
static int ParseVersion(const QString &line, int &version)
Definition: httplivestreambuffer.cpp:2054
HLSRingBuffer::ParseM3U8
int ParseM3U8(const QByteArray *buffer, StreamsList *streams=nullptr)
Definition: httplivestreambuffer.cpp:2104
StreamsList
QList< HLSStream * > StreamsList
Definition: httplivestreambuffer.h:41
HLSRingBuffer::FreeStreamsList
void FreeStreamsList(QList< HLSStream * > *streams) const
Definition: httplivestreambuffer.cpp:1588
HLSRingBuffer::m_streams
StreamsList m_streams
Definition: httplivestreambuffer.h:108
HLSRingBuffer::ParseStreamInformation
HLSStream * ParseStreamInformation(const QString &line, const QString &uri) const
Definition: httplivestreambuffer.cpp:1862
HLSRingBuffer::m_killed
bool m_killed
Definition: httplivestreambuffer.h:135
HLSRingBuffer::SanityCheck
void SanityCheck(const HLSStream *hls) const
Definition: httplivestreambuffer.cpp:2325
HLSStream
Definition: httplivestreambuffer.cpp:394
HLSRingBuffer::IsStreamed
bool IsStreamed(void) override
Definition: httplivestreambuffer.h:54
build_compdb.filename
filename
Definition: build_compdb.py:21
MythMediaBuffer::kDefaultOpenTimeout
static constexpr std::chrono::milliseconds kDefaultOpenTimeout
Definition: mythmediabuffer.h:62
HLSRingBuffer::GetCurrentStream
HLSStream * GetCurrentStream(void) const
return the stream we are currently streaming from
Definition: httplivestreambuffer.cpp:1672
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:127
HLSRingBuffer::GetLastStream
HLSStream * GetLastStream(const StreamsList *streams=nullptr) const
Definition: httplivestreambuffer.cpp:1631
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:2768
HLSRingBuffer::IsOpen
bool IsOpen(void) const override
Definition: httplivestreambuffer.cpp:2961
HLSRingBuffer::ParseAllowCache
static int ParseAllowCache(HLSStream *hls, const QString &line)
Definition: httplivestreambuffer.cpp:2030