MythTV master
streamhandler.h
Go to the documentation of this file.
1// -*- Mode: c++ -*-
2
3#ifndef STREAM_HANDLER_H
4#define STREAM_HANDLER_H
5
6#include <utility>
7#include <vector>
8
9// Qt headers
10#include <QChar> // Fix Qt6 GCC SFINAE warning
11#include <QWaitCondition>
12#include <QString>
13#include <QMutex>
14#include <QMap>
15#include <QRecursiveMutex>
16#include <QSet>
17
18// MythTV headers
19#include "libmythbase/mthread.h"
21
22#include "DeviceReadBuffer.h" // for ReaderPausedCB
23#include "mpeg/mpegstreamdata.h" // for PIDPriority
24
26
27//#define DEBUG_PID_FILTERS
28
30{
31 public:
32 PIDInfo() = default;
33 explicit PIDInfo(uint pid) : m_pid(pid) {}
34 PIDInfo(uint pid, uint stream_type, int pes_type) :
35 m_pid(pid), m_streamType(stream_type), m_pesType(pes_type) {;}
36 virtual ~PIDInfo() {;}
37
38 virtual bool Open(const QString &/*dev*/, bool /*use_section_reader*/)
39 { return false; }
40 virtual bool Close(const QString &/*dev*/) { return false; }
41 bool IsOpen(void) const { return m_filterFd >= 0; }
42
43 uint m_pid { UINT_MAX };
44 int m_filterFd { -1 };
46 int m_pesType { -1 };
47};
48// Please do not change this to hash or other unordered container.
49// HDHRStreamHandler::UpdateFilters() relies on the forward
50// iterator returning these in order of ascending pid number.
51using PIDInfoMap = QMap<uint,PIDInfo*>;
52
53// locking order
54// _pid_lock -> _listener_lock
55// _add_rm_lock -> _listener_lock
56// -> _start_stop_lock
57
58class StreamHandler : protected MThread, public DeviceReaderCB
59{
60 public:
61 virtual void AddListener(MPEGStreamData *data,
62 bool allow_section_reader = false,
63 bool needs_buffering = false,
64 const QString& output_file= QString());
65 virtual void RemoveListener(MPEGStreamData *data);
66 bool IsRunning(void) const;
67 bool HasError(void) const { return m_bError; }
68
70 virtual bool AddNamedOutputFile(const QString &filename);
72 virtual void RemoveNamedOutputFile(const QString &filename);
73
74 // DeviceReaderCB
75 void ReaderPaused(int fd) override { (void) fd; } // DeviceReaderCB
76 void PriorityEvent(int fd) override { (void) fd; } // DeviceReaderCB
77
78 protected:
79 explicit StreamHandler(QString device, int inputid)
80 : MThread("StreamHandler"), m_device(std::move(device)), m_inputId(inputid) {}
81 ~StreamHandler() override;
82
83 void Start(void);
84 void Stop(void);
85
86 void SetRunning(bool running,
87 bool using_buffering,
88 bool using_section_reader);
89
91 bool RemovePIDFilter(uint pid);
92 bool RemoveAllPIDFilters(void);
93
94 void UpdateListeningForEIT(void);
96 virtual bool UpdateFilters(void) { return true; }
97 virtual void CycleFiltersByPriority() {}
98
100
101 virtual PIDInfo *CreatePIDInfo(uint pid, uint stream_type, int pes_type)
102 { return new PIDInfo(pid, stream_type, pes_type); }
103
104 protected:
106 void WriteMPTS(const unsigned char * buffer, uint len);
110 virtual void SetRunningDesired(bool desired);
111
112 protected:
113 QString m_device;
115 bool m_needsBuffering {false};
117
119
120 mutable QMutex m_startStopLock;
121 volatile bool m_runningDesired {false};
122
123 // not to be confused with the other four header files that define
124 // m_error to be a QString. Somehow v4l2encstreamhandler.cpp
125 // blends these into a single class.
126 volatile bool m_bError {false};
127 bool m_running {false};
128 bool m_restarting {false};
129 bool m_usingBuffering {false};
131 QWaitCondition m_runningStateChanged;
132
133 mutable QRecursiveMutex m_pidLock;
134 std::vector<uint> m_eitPids;
137 bool m_filtersChanged {false};
139
141 QSet<QString> m_mptsFiles;
144
145 using StreamDataList = QHash<MPEGStreamData*,QString>;
146 mutable QRecursiveMutex m_listenerLock;
148};
149
150#endif // STREAM_HANDLER_H
Encapsulates data about MPEG stream and emits events for each table.
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:14
int m_filterFd
Input filter file descriptor.
Definition: streamhandler.h:44
virtual bool Open(const QString &, bool)
Definition: streamhandler.h:38
PIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:34
virtual bool Close(const QString &)
Definition: streamhandler.h:40
PIDInfo()=default
int m_pesType
PESStreamID.
Definition: streamhandler.h:46
uint m_pid
Definition: streamhandler.h:43
uint m_streamType
StreamID.
Definition: streamhandler.h:45
bool IsOpen(void) const
Definition: streamhandler.h:41
virtual ~PIDInfo()
Definition: streamhandler.h:36
PIDInfo(uint pid)
Definition: streamhandler.h:33
QRecursiveMutex m_pidLock
StreamHandler(QString device, int inputid)
Definition: streamhandler.h:79
bool AddPIDFilter(PIDInfo *info)
virtual bool AddNamedOutputFile(const QString &filename)
Called with _listener_lock locked just after adding new output file.
bool HasError(void) const
Definition: streamhandler.h:67
StreamDataList m_streamDataList
bool RemovePIDFilter(uint pid)
QString m_mptsBaseFile
MythTimer m_cycleTimer
void WriteMPTS(const unsigned char *buffer, uint len)
Write out a copy of the raw MPTS.
virtual void RemoveNamedOutputFile(const QString &filename)
Called with _listener_lock locked just before removing old output file.
void Start(void)
virtual void CycleFiltersByPriority()
Definition: streamhandler.h:97
virtual void SetRunningDesired(bool desired)
At minimum this sets _running_desired, this may also send signals to anything that might be blocking ...
QHash< MPEGStreamData *, QString > StreamDataList
void PriorityEvent(int fd) override
Definition: streamhandler.h:76
void UpdateListeningForEIT(void)
QString m_device
PIDInfoMap m_pidInfo
ThreadedFileWriter * m_mptsTfw
volatile bool m_runningDesired
volatile bool m_bError
bool RemoveAllPIDFilters(void)
void ReaderPaused(int fd) override
Definition: streamhandler.h:75
void SetRunning(bool running, bool using_buffering, bool using_section_reader)
QMutex m_startStopLock
bool m_usingSectionReader
bool UpdateFiltersFromStreamData(void)
QMutex m_addRmLock
bool IsRunning(void) const
std::vector< uint > m_eitPids
PIDPriority GetPIDPriority(uint pid) const
QSet< QString > m_mptsFiles
~StreamHandler() override
void Stop(void)
QWaitCondition m_runningStateChanged
virtual void RemoveListener(MPEGStreamData *data)
virtual void AddListener(MPEGStreamData *data, bool allow_section_reader=false, bool needs_buffering=false, const QString &output_file=QString())
bool m_allowSectionReader
virtual bool UpdateFilters(void)
Definition: streamhandler.h:96
virtual PIDInfo * CreatePIDInfo(uint pid, uint stream_type, int pes_type)
QRecursiveMutex m_listenerLock
This class supports the writing of recordings to disk.
unsigned int uint
Definition: compat.h:60
PIDPriority
dictionary info
Definition: azlyrics.py:7
QMap< uint, PIDInfo * > PIDInfoMap
Definition: streamhandler.h:51