MythTV  master
v4l2encstreamhandler.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 #ifndef V4L2encStreamhandler_H
4 #define V4L2encStreamhandler_H
5 
6 #include <cstdint>
7 #include <string>
8 #include <vector>
9 
10 #include <QString>
11 #include <QAtomicInt>
12 #include <QMutex>
13 #include <QMap>
14 
15 #include "streamhandler.h"
16 #include "v4l2util.h"
17 
18 class DTVSignalMonitor;
19 class V4L2encChannel;
20 
22 {
23  friend class V4L2encRecorder;
24  friend class V4L2encSignalMonitor;
25 
26  enum constants {PACKET_SIZE = 188 * 32768};
27 
28  public:
29  static V4L2encStreamHandler *Get(const QString &devname, int audioinput,
30  int inputid);
31  static void Return(V4L2encStreamHandler * & ref, int inputid);
32 
33  public:
34  V4L2encStreamHandler(const QString & device, int audio_input, int inputid);
35  ~V4L2encStreamHandler(void) override;
36 
37  void run(void) override; // MThread
38 #if 0
39  void PriorityEvent(int fd) override; // DeviceReaderCB
40 #endif
41 
42  bool Configure(void);
43 
44  QString Driver(void) const { return m_v4l2.DriverName(); }
45  int GetStreamType(void);
46  bool IsOpen(void) const { return m_fd >= 0 && m_v4l2.IsOpen(); }
47 
48  bool HasTuner(void) const { return m_v4l2.HasTuner(); }
49  bool HasAudio(void) const { return m_v4l2.HasAudioSupport(); }
50  bool HasSlicedVBI(void) const { return m_v4l2.HasSlicedVBI(); }
51 
52  bool HasPictureAttributes(void) const { return m_hasPictureAttributes; }
53 // bool HasCap(uint32_t mask) const { return (mask & m_caps); }
54 
55  int AvailCount(void) const { return m_drb ? m_drb->GetUsed() : 0; }
56  bool StartEncoding(void);
57  bool StopEncoding(void);
58  void RestartEncoding(void);
59 
60  QString ErrorString(void) const { return m_error; }
61 
62  protected:
63  bool Status(bool &failed, bool &failing);
64 
65  static QString RequestDescription(int request);
66 
67  bool SetOption(const QString &opt, int value);
68  bool SetOption(const QString &opt, const QString &value);
69 
70  bool SetControl(int request, int value);
72  bool SetLanguageMode(void);
73  bool SetRecordingVolume(void);
74 
75  bool HasLock(void);
76  int GetSignalStrength(void);
77  bool GetResolution(int& width, int&height) const
78  { return m_v4l2.GetResolution(width, height); }
79 
80  void SetBitrate(int bitrate, int maxbitrate, int bitratemode,
81  const QString & reason);
82  bool SetBitrateForResolution(void);
83 
84  private:
85  bool Open(void);
86  void Close(void);
87 // int Read(unsigned char *buf, uint count);
88  bool ConfigureVBI(void);
89 
90  bool m_failing {false};
91  QString m_error;
92 
93  bool m_hasTuner {false};
94  bool m_hasPictureAttributes {false};
95 
96  // for implementing Get & Return
97  static QMutex s_handlers_lock;
98  static QMap<QString, V4L2encStreamHandler*> s_handlers;
99  static QMap<QString, uint> s_handlers_refcnt;
100 
101  int m_bufferSize {1000 * (int)TSPacket::kSize};
102 
103  // Encoding info
105  int m_streamType {-1};
106  int m_aspectRatio {-1};
107  int m_bitrateMode {V4L2_MPEG_VIDEO_BITRATE_MODE_VBR};
108  int m_bitrate {-1};
109  int m_maxBitrate {-1};
110  int m_audioCodec {-1};
111  int m_audioLayer {V4L2_MPEG_AUDIO_ENCODING_LAYER_1};
116  int m_audioVolume {-1};
117  int m_langMode {-1};
118  uint m_lowBitrateMode {V4L2_MPEG_VIDEO_BITRATE_MODE_VBR};
119  uint m_lowBitrate {UINT_MAX};
120  uint m_lowPeakBitrate {UINT_MAX};
121  uint m_mediumBitrateMode {V4L2_MPEG_VIDEO_BITRATE_MODE_VBR};
122  uint m_mediumBitrate {UINT_MAX};
124  uint m_highBitrateMode {V4L2_MPEG_VIDEO_BITRATE_MODE_VBR};
125  uint m_highBitrate {UINT_MAX};
127 
128  static const std::array<const int,14> kAudioRateL1;
129  static const std::array<const int,14> kAudioRateL2;
130  static const std::array<const int,14> kAudioRateL3;
131  static const std::array<const std::string,15> kStreamTypes;
132 
133  int m_fd {-1};
134  int m_audioInput {-1};
135 
136  uint m_width {UINT_MAX};
137  uint m_height {UINT_MAX};
138  uint m_signalStrength {UINT_MAX};
139 
142 
143  // VBI
144  QString m_vbiDevice;
145 
146  QAtomicInt m_streamingCnt {0};
147  QMutex m_streamLock;
148 
150 };
151 
152 #endif // V4L2encSTREAMHANDLER_H
V4L2encStreamHandler::run
void run(void) override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
Definition: v4l2encstreamhandler.cpp:167
V4L2encStreamHandler::m_audioBitrateL3
int m_audioBitrateL3
Definition: v4l2encstreamhandler.h:115
V4L2encStreamHandler::GetSignalStrength
int GetSignalStrength(void)
Definition: v4l2encstreamhandler.cpp:919
V4L2encStreamHandler::m_lowBitrate
uint m_lowBitrate
Definition: v4l2encstreamhandler.h:119
V4L2encStreamHandler::m_hasTuner
bool m_hasTuner
Definition: v4l2encstreamhandler.h:93
V4L2encStreamHandler::Return
static void Return(V4L2encStreamHandler *&ref, int inputid)
Definition: v4l2encstreamhandler.cpp:94
V4L2encStreamHandler::HasAudio
bool HasAudio(void) const
Definition: v4l2encstreamhandler.h:49
V4L2encStreamHandler::kAudioRateL2
static const std::array< const int, 14 > kAudioRateL2
Definition: v4l2encstreamhandler.h:129
V4L2util::GetResolution
bool GetResolution(int &width, int &height) const
Definition: v4l2util.cpp:657
V4L2encStreamHandler::SetRecordingVolume
bool SetRecordingVolume(void)
streamhandler.h
V4L2encStreamHandler::m_langMode
int m_langMode
0 is Main Lang; 1 is SAP Lang; 2 is Dual
Definition: v4l2encstreamhandler.h:117
V4L2encStreamHandler::m_audioVolume
int m_audioVolume
Definition: v4l2encstreamhandler.h:116
V4L2encStreamHandler
Definition: v4l2encstreamhandler.h:22
V4L2encStreamHandler::IsOpen
bool IsOpen(void) const
Definition: v4l2encstreamhandler.h:46
V4L2encStreamHandler::s_handlers_refcnt
static QMap< QString, uint > s_handlers_refcnt
Definition: v4l2encstreamhandler.h:99
StreamHandler
Definition: streamhandler.h:55
V4L2encStreamHandler::HasTuner
bool HasTuner(void) const
Definition: v4l2encstreamhandler.h:48
V4L2encStreamHandler::RestartEncoding
void RestartEncoding(void)
Definition: v4l2encstreamhandler.cpp:660
V4L2util::DriverName
QString DriverName(void) const
Definition: v4l2util.h:48
V4L2encStreamHandler::m_highBitrate
uint m_highBitrate
Definition: v4l2encstreamhandler.h:125
V4L2encStreamHandler::m_bitrate
int m_bitrate
Definition: v4l2encstreamhandler.h:108
V4L2encStreamHandler::m_streamLock
QMutex m_streamLock
Definition: v4l2encstreamhandler.h:147
V4L2encStreamHandler::HasLock
bool HasLock(void)
Definition: v4l2encstreamhandler.cpp:911
V4L2encStreamHandler::m_height
uint m_height
Definition: v4l2encstreamhandler.h:137
V4L2encStreamHandler::m_fd
int m_fd
Definition: v4l2encstreamhandler.h:133
V4L2util::HasTuner
bool HasTuner(void) const
Definition: v4l2util.cpp:692
V4L2encStreamHandler::m_audioLayer
int m_audioLayer
Definition: v4l2encstreamhandler.h:111
V4L2encStreamHandler::V4L2encStreamHandler
V4L2encStreamHandler(const QString &device, int audio_input, int inputid)
Definition: v4l2encstreamhandler.cpp:144
V4L2encStreamHandler::m_mediumBitrate
uint m_mediumBitrate
Definition: v4l2encstreamhandler.h:122
V4L2encStreamHandler::m_maxBitrate
int m_maxBitrate
Definition: v4l2encstreamhandler.h:109
V4L2encStreamHandler::m_bitrateMode
int m_bitrateMode
Definition: v4l2encstreamhandler.h:107
V4L2encStreamHandler::s_handlers_lock
static QMutex s_handlers_lock
Definition: v4l2encstreamhandler.h:97
V4L2encStreamHandler::Close
void Close(void)
Definition: v4l2encstreamhandler.cpp:467
DeviceReadBuffer
Buffers reads from device files.
Definition: DeviceReadBuffer.h:35
V4L2encStreamHandler::GetStreamType
int GetStreamType(void)
Definition: v4l2encstreamhandler.cpp:815
V4L2encStreamHandler::m_audioBitrateL1
int m_audioBitrateL1
Definition: v4l2encstreamhandler.h:113
V4L2encStreamHandler::StopEncoding
bool StopEncoding(void)
Definition: v4l2encstreamhandler.cpp:609
V4L2encStreamHandler::SetOption
bool SetOption(const QString &opt, int value)
Definition: v4l2encstreamhandler.cpp:704
V4L2encStreamHandler::kAudioRateL1
static const std::array< const int, 14 > kAudioRateL1
Definition: v4l2encstreamhandler.h:128
V4L2encStreamHandler::SetBitrateForResolution
bool SetBitrateForResolution(void)
Definition: v4l2encstreamhandler.cpp:962
V4L2encStreamHandler::m_highPeakBitrate
uint m_highPeakBitrate
Definition: v4l2encstreamhandler.h:126
StreamHandler::PriorityEvent
void PriorityEvent(int fd) override
Definition: streamhandler.h:95
V4L2encStreamHandler::m_audioBitrateL2
int m_audioBitrateL2
Definition: v4l2encstreamhandler.h:114
V4L2encStreamHandler::Open
bool Open(void)
Definition: v4l2encstreamhandler.cpp:339
V4L2encStreamHandler::m_signalStrength
uint m_signalStrength
Definition: v4l2encstreamhandler.h:138
V4L2encStreamHandler::m_audioInput
int m_audioInput
Definition: v4l2encstreamhandler.h:134
V4L2encSignalMonitor
Definition: v4l2encsignalmonitor.h:17
v4l2util.h
V4L2encStreamHandler::kStreamTypes
static const std::array< const std::string, 15 > kStreamTypes
Definition: v4l2encstreamhandler.h:131
V4L2util::HasSlicedVBI
bool HasSlicedVBI(void) const
Definition: v4l2util.cpp:115
V4L2encStreamHandler::SetBitrate
void SetBitrate(int bitrate, int maxbitrate, int bitratemode, const QString &reason)
Definition: v4l2encstreamhandler.cpp:933
V4L2encStreamHandler::m_audioCodec
int m_audioCodec
Definition: v4l2encstreamhandler.h:110
V4L2encStreamHandler::m_failing
bool m_failing
Definition: v4l2encstreamhandler.h:90
V4L2encStreamHandler::ErrorString
QString ErrorString(void) const
Definition: v4l2encstreamhandler.h:60
V4L2encStreamHandler::m_lowPeakBitrate
uint m_lowPeakBitrate
Definition: v4l2encstreamhandler.h:120
V4L2encStreamHandler::StartEncoding
bool StartEncoding(void)
Definition: v4l2encstreamhandler.cpp:500
V4L2encStreamHandler::m_streamType
int m_streamType
Definition: v4l2encstreamhandler.h:105
V4L2encStreamHandler::kAudioRateL3
static const std::array< const int, 14 > kAudioRateL3
Definition: v4l2encstreamhandler.h:130
V4L2encStreamHandler::m_aspectRatio
int m_aspectRatio
Definition: v4l2encstreamhandler.h:106
V4L2encStreamHandler::HasPictureAttributes
bool HasPictureAttributes(void) const
Definition: v4l2encstreamhandler.h:52
V4L2encStreamHandler::m_v4l2
V4L2util m_v4l2
Definition: v4l2encstreamhandler.h:140
V4L2encStreamHandler::s_handlers
static QMap< QString, V4L2encStreamHandler * > s_handlers
Definition: v4l2encstreamhandler.h:98
uint
unsigned int uint
Definition: compat.h:140
V4L2encStreamHandler::m_streamingCnt
QAtomicInt m_streamingCnt
Definition: v4l2encstreamhandler.h:146
V4L2encStreamHandler::m_audioSampleRate
int m_audioSampleRate
Definition: v4l2encstreamhandler.h:112
V4L2encStreamHandler::Driver
QString Driver(void) const
Definition: v4l2encstreamhandler.h:44
V4L2encStreamHandler::m_vbiDevice
QString m_vbiDevice
Definition: v4l2encstreamhandler.h:144
V4L2util::HasAudioSupport
bool HasAudioSupport(void) const
Definition: v4l2util.cpp:697
V4L2encStreamHandler::Status
bool Status(bool &failed, bool &failing)
Definition: v4l2encstreamhandler.cpp:137
V4L2encStreamHandler::SetControl
bool SetControl(int request, int value)
V4L2util::IsOpen
bool IsOpen(void) const
Definition: v4l2util.h:29
V4L2encStreamHandler::constants
constants
Definition: v4l2encstreamhandler.h:26
V4L2encStreamHandler::m_drb
DeviceReadBuffer * m_drb
Definition: v4l2encstreamhandler.h:141
V4L2encStreamHandler::m_mediumPeakBitrate
uint m_mediumPeakBitrate
Definition: v4l2encstreamhandler.h:123
V4L2encStreamHandler::GetResolution
bool GetResolution(int &width, int &height) const
Definition: v4l2encstreamhandler.h:77
V4L2encStreamHandler::SetVideoCaptureFormat
bool SetVideoCaptureFormat(void)
V4L2util
Definition: v4l2util.h:17
V4L2encStreamHandler::m_mediumBitrateMode
uint m_mediumBitrateMode
Definition: v4l2encstreamhandler.h:121
V4L2encStreamHandler::m_hasPictureAttributes
bool m_hasPictureAttributes
Definition: v4l2encstreamhandler.h:94
V4L2encStreamHandler::m_bufferSize
int m_bufferSize
Definition: v4l2encstreamhandler.h:101
V4L2encStreamHandler::HasSlicedVBI
bool HasSlicedVBI(void) const
Definition: v4l2encstreamhandler.h:50
V4L2encStreamHandler::RequestDescription
static QString RequestDescription(int request)
DTVSignalMonitor
This class is intended to detect the presence of needed tables.
Definition: dtvsignalmonitor.h:19
V4L2encStreamHandler::m_desiredStreamType
int m_desiredStreamType
Definition: v4l2encstreamhandler.h:104
V4L2encStreamHandler::~V4L2encStreamHandler
~V4L2encStreamHandler(void) override
Definition: v4l2encstreamhandler.cpp:161
V4L2encStreamHandler::m_pauseEncodingAllowed
bool m_pauseEncodingAllowed
Definition: v4l2encstreamhandler.h:149
V4L2encRecorder
This is a specialization of DTVRecorder used to handle streams from V4L2 recorders.
Definition: v4l2encrecorder.h:30
V4L2encStreamHandler::Get
static V4L2encStreamHandler * Get(const QString &devname, int audioinput, int inputid)
Definition: v4l2encstreamhandler.cpp:62
V4L2encStreamHandler::m_width
uint m_width
Definition: v4l2encstreamhandler.h:136
V4L2encStreamHandler::PACKET_SIZE
@ PACKET_SIZE
Definition: v4l2encstreamhandler.h:26
V4L2encStreamHandler::AvailCount
int AvailCount(void) const
Definition: v4l2encstreamhandler.h:55
V4L2encStreamHandler::m_error
QString m_error
Definition: v4l2encstreamhandler.h:91
V4L2encStreamHandler::SetLanguageMode
bool SetLanguageMode(void)
Set audio language mode.
Definition: v4l2encstreamhandler.cpp:676
V4L2encStreamHandler::m_highBitrateMode
uint m_highBitrateMode
Definition: v4l2encstreamhandler.h:124
V4L2encStreamHandler::ConfigureVBI
bool ConfigureVBI(void)
Definition: v4l2encstreamhandler.cpp:1031
V4L2encStreamHandler::Configure
bool Configure(void)
Definition: v4l2encstreamhandler.cpp:400
DeviceReadBuffer::GetUsed
uint GetUsed(void) const
Definition: DeviceReadBuffer.cpp:264
TSPacket::kSize
static constexpr unsigned int kSize
Definition: tspacket.h:223
V4L2encStreamHandler::m_lowBitrateMode
uint m_lowBitrateMode
Definition: v4l2encstreamhandler.h:118