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 <QWaitCondition>
11#include <QString>
12#include <QMutex>
13#include <QMap>
14#include <QRecursiveMutex>
15#include <QSet>
16
17// MythTV headers
18#include "libmythbase/mthread.h"
20
21#include "DeviceReadBuffer.h" // for ReaderPausedCB
22#include "mpeg/mpegstreamdata.h" // for PIDPriority
23
25
26//#define DEBUG_PID_FILTERS
27
29{
30 public:
31 PIDInfo() = default;
32 explicit PIDInfo(uint pid) : m_pid(pid) {}
33 PIDInfo(uint pid, uint stream_type, int pes_type) :
34 m_pid(pid), m_streamType(stream_type), m_pesType(pes_type) {;}
35 virtual ~PIDInfo() {;}
36
37 virtual bool Open(const QString &/*dev*/, bool /*use_section_reader*/)
38 { return false; }
39 virtual bool Close(const QString &/*dev*/) { return false; }
40 bool IsOpen(void) const { return m_filterFd >= 0; }
41
42 uint m_pid { UINT_MAX };
43 int m_filterFd { -1 };
45 int m_pesType { -1 };
46};
47// Please do not change this to hash or other unordered container.
48// HDHRStreamHandler::UpdateFilters() relies on the forward
49// iterator returning these in order of ascending pid number.
50using PIDInfoMap = QMap<uint,PIDInfo*>;
51
52// locking order
53// _pid_lock -> _listener_lock
54// _add_rm_lock -> _listener_lock
55// -> _start_stop_lock
56
57class StreamHandler : protected MThread, public DeviceReaderCB
58{
59 public:
60 virtual void AddListener(MPEGStreamData *data,
61 bool allow_section_reader = false,
62 bool needs_buffering = false,
63 const QString& output_file= QString());
64 virtual void RemoveListener(MPEGStreamData *data);
65 bool IsRunning(void) const;
66 bool HasError(void) const { return m_bError; }
67
69 virtual bool AddNamedOutputFile(const QString &filename);
71 virtual void RemoveNamedOutputFile(const QString &filename);
72
73 protected:
74 explicit StreamHandler(QString device, int inputid)
75 : MThread("StreamHandler"), m_device(std::move(device)), m_inputId(inputid) {}
76 ~StreamHandler() override;
77
78 void Start(void);
79 void Stop(void);
80
81 void SetRunning(bool running,
82 bool using_buffering,
83 bool using_section_reader);
84
86 bool RemovePIDFilter(uint pid);
87 bool RemoveAllPIDFilters(void);
88
89 void UpdateListeningForEIT(void);
91 virtual bool UpdateFilters(void) { return true; }
92 virtual void CycleFiltersByPriority() {}
93
95
96 // DeviceReaderCB
97 void ReaderPaused(int fd) override { (void) fd; } // DeviceReaderCB
98 void PriorityEvent(int fd) override { (void) fd; } // DeviceReaderCB
99
100 virtual PIDInfo *CreatePIDInfo(uint pid, uint stream_type, int pes_type)
101 { return new PIDInfo(pid, stream_type, pes_type); }
102
103 protected:
105 void WriteMPTS(const unsigned char * buffer, uint len);
109 virtual void SetRunningDesired(bool desired);
110
111 protected:
112 QString m_device;
114 bool m_needsBuffering {false};
116
118
119 mutable QMutex m_startStopLock;
120 volatile bool m_runningDesired {false};
121
122 // not to be confused with the other four header files that define
123 // m_error to be a QString. Somehow v4l2encstreamhandler.cpp
124 // blends these into a single class.
125 volatile bool m_bError {false};
126 bool m_running {false};
127 bool m_restarting {false};
128 bool m_usingBuffering {false};
130 QWaitCondition m_runningStateChanged;
131
132 mutable QRecursiveMutex m_pidLock;
133 std::vector<uint> m_eitPids;
136 bool m_filtersChanged {false};
138
140 QSet<QString> m_mptsFiles;
143
144 using StreamDataList = QHash<MPEGStreamData*,QString>;
145 mutable QRecursiveMutex m_listenerLock;
147};
148
149#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:43
virtual bool Open(const QString &, bool)
Definition: streamhandler.h:37
PIDInfo(uint pid, uint stream_type, int pes_type)
Definition: streamhandler.h:33
virtual bool Close(const QString &)
Definition: streamhandler.h:39
PIDInfo()=default
int m_pesType
PESStreamID.
Definition: streamhandler.h:45
uint m_pid
Definition: streamhandler.h:42
uint m_streamType
StreamID.
Definition: streamhandler.h:44
bool IsOpen(void) const
Definition: streamhandler.h:40
virtual ~PIDInfo()
Definition: streamhandler.h:35
PIDInfo(uint pid)
Definition: streamhandler.h:32
QRecursiveMutex m_pidLock
StreamHandler(QString device, int inputid)
Definition: streamhandler.h:74
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:66
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:92
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:98
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:97
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:91
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: freesurround.h:24
PIDPriority
dictionary info
Definition: azlyrics.py:7
STL namespace.
QMap< uint, PIDInfo * > PIDInfoMap
Definition: streamhandler.h:50