MythTV  master
recorderbase.h
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 #ifndef RECORDERBASE_H_
3 #define RECORDERBASE_H_
4 
5 #include <cstdint>
6 
7 #include <QWaitCondition>
8 #include <QAtomicInt>
9 #include <QDateTime>
10 #include <QRunnable>
11 #include <QString>
12 #include <QMutex>
13 #include <QMap>
14 
15 #include "recordingquality.h"
16 #include "programtypes.h" // for MarkTypes, frm_pos_map_t
17 #include "mythtimer.h"
18 #include "mythtvexp.h"
19 #include "recordingfile.h"
20 
21 extern "C"
22 {
23 #include "libavcodec/avcodec.h" // for Video/Audio codec enums
24 }
25 
26 class FireWireDBOptions;
27 class GeneralDBOptions;
28 class RecordingProfile;
29 class RecordingInfo;
30 class DVBDBOptions;
31 class RecorderBase;
32 class ChannelBase;
33 class RingBuffer;
34 class TVRec;
35 
36 class FrameRate
37 {
38 public:
39  FrameRate(uint n, uint d=1) : num(n), den(d) {}
40  double toDouble(void) const { return num / (double)den; }
41  bool isNonzero(void) const { return num; }
42  uint getNum(void) const { return num; }
43  uint getDen(void) const { return den; }
44  QString toString(void) const { return QString("%1/%2").arg(num).arg(den); }
45  bool operator==(const FrameRate &other) {
46  return num == other.num && den == other.den;
47  }
48  bool operator!=(const FrameRate &other) { return !(*this == other); }
49 private:
52 };
53 
66 class MTV_PUBLIC RecorderBase : public QRunnable
67 {
68  friend class Transcode; // for access to SetIntOption(), SetStrOption()
69 
70  public:
71  explicit RecorderBase(TVRec *rec);
72  virtual ~RecorderBase();
73 
75  void SetFrameRate(double rate)
76  {
77  video_frame_rate = rate;
78  ntsc_framerate = (29.96 <= rate && 29.98 >= rate);
79  m_frameRate = FrameRate((rate * 100) + 0.5, 100);
80  }
81 
91  void SetRecording(const RecordingInfo *pginfo);
92 
99  void SetRingBuffer(RingBuffer *rbuf);
100 
107  virtual void SetOption(const QString &opt, const QString &value);
108 
113  virtual void SetOption(const QString &opt, int value);
114 
121  virtual void SetVideoFilters(QString &filters) = 0;
122 
129  virtual void SetOptionsFromProfile(RecordingProfile *profile,
130  const QString &videodev,
131  const QString &audiodev,
132  const QString &vbidev) = 0;
133 
144  void SetNextRecording(const RecordingInfo*, RingBuffer*);
145 
149  virtual void Initialize(void) = 0;
150 
155  void run(void) override = 0; // QRunnable
156 
162  virtual void Reset(void) = 0;
163 
165  virtual bool IsErrored(void) = 0;
166 
172  virtual long long GetFramesWritten(void) = 0;
173 
182  virtual int GetVideoFd(void) = 0;
183 
198  long long GetKeyframePosition(long long desired) const;
199  bool GetKeyframePositions(
200  long long start, long long end, frm_pos_map_t&) const;
201  bool GetKeyframeDurations(
202  long long start, long long end, frm_pos_map_t&) const;
203 
204  virtual void StopRecording(void);
205  virtual bool IsRecording(void);
206  virtual bool IsRecordingRequested(void);
207 
209  virtual RecordingQuality *GetRecordingQuality(const RecordingInfo*) const;
210 
211  // pausing interface
212  virtual void Pause(bool clear = true);
213  virtual void Unpause(void);
214  virtual bool IsPaused(bool holding_lock = false) const;
215  virtual bool WaitForPause(int timeout = 1000);
216 
219  double GetFrameRate(void) const { return m_frameRate.toDouble(); }
220 
223  virtual void CheckForRingBufferSwitch(void);
224 
227  void SavePositionMap(bool force = false, bool finished = false);
228 
229  enum AspectRatio {
230  ASPECT_UNKNOWN = 0x00,
231  ASPECT_1_1 = 0x01,
232  ASPECT_4_3 = 0x02,
233  ASPECT_16_9 = 0x03,
234  ASPECT_2_21_1 = 0x04,
235  ASPECT_CUSTOM = 0x05,
236  };
237 
238  static RecorderBase *CreateRecorder(
239  TVRec *tvrec,
240  ChannelBase *channel,
241  const RecordingProfile &profile,
242  const GeneralDBOptions &genOpt);
243 
244  protected:
248  void SetIntOption(RecordingProfile *profile, const QString &name);
252  void SetStrOption(RecordingProfile *profile, const QString &name);
253  virtual bool PauseAndWait(int timeout = 100);
254 
255  virtual void ResetForNewFile(void) = 0;
256  virtual void SetRecordingStatus(RecStatus::Type status,
257  const QString& file, int line);
258  virtual void ClearStatistics(void);
259  virtual void FinishRecording(void);
260  virtual void StartNewFile(void) { }
261 
264  void SetPositionMapType(MarkTypes type) { positionMapType = type; }
265 
268  void AspectChange(uint ratio, long long frame);
269 
272  void ResolutionChange(uint width, uint height, long long frame);
273 
276  void FrameRateChange(uint framerate, long long frame);
277 
280  void VideoCodecChange(AVCodecID vCodec);
281 
284  void AudioCodecChange(AVCodecID aCodec);
285 
288  void SetDuration(uint64_t duration);
289 
292  void SetTotalFrames(uint64_t total_frames);
293 
294  void TryWriteProgStartMark(const frm_pos_map_t &durationDeltaCopy);
295 
299 
303  QString videocodec;
304  QString videodevice;
305 
306  bool ntsc;
309 
310  uint m_videoAspect; // AspectRatio (1 = 4:3, 2 = 16:9
311 
315 
317 
318  // For handling pausing + stop recording
319  mutable QMutex pauseLock; // also used for request_recording and recording
321  bool paused;
322  QWaitCondition pauseWait;
323  QWaitCondition unpauseWait;
327  bool recording;
328  QWaitCondition recordingWait;
329 
330 
331  // For RingBuffer switching
336 
337  // Seektable support
339  mutable QMutex positionMapLock;
345 
346  // ProgStart mark support
348  long long lastSavedKeyframe;
349  long long lastSavedDuration;
350 
351  // Statistics
352  // Note: Once we enter RecorderBase::run(), only that thread can
353  // update these values safely. These values are read in that thread
354  // without locking and updated with the lock held. Outside that
355  // thread these values are only read, and only with the lock held.
356  mutable QMutex statisticsLock;
357  QAtomicInt timeOfFirstDataIsSet; // doesn't need locking
358  QDateTime timeOfFirstData;
359  QAtomicInt timeOfLatestDataCount; // doesn't need locking
360  QAtomicInt timeOfLatestDataPacketInterval; // doesn't need locking
361  QDateTime timeOfLatestData;
366 };
367 
368 #endif
369 
370 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MythTimer ringBufferCheckTimer
Definition: recorderbase.h:335
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
RingBuffer * ringBuffer
Definition: recorderbase.h:297
QWaitCondition unpauseWait
Definition: recorderbase.h:323
RecordingInfo * curRecording
Definition: recorderbase.h:316
qint64 estimatedProgStartMS
Definition: recorderbase.h:347
QMutex pauseLock
Definition: recorderbase.h:319
QDateTime timeOfFirstData
Definition: recorderbase.h:358
QString toString(void) const
Definition: recorderbase.h:44
frm_pos_map_t durationMap
Definition: recorderbase.h:342
MythTimer timeOfLatestDataTimer
Definition: recorderbase.h:362
QWaitCondition pauseWait
Definition: recorderbase.h:322
virtual void StartNewFile(void)
Definition: recorderbase.h:260
enum AVContainerFormats AVContainer
Holds information on a TV Program one might wish to record.
Definition: recordinginfo.h:34
RingBuffer * nextRingBuffer
Definition: recorderbase.h:333
bool operator!=(const FrameRate &other)
Definition: recorderbase.h:48
QWaitCondition recordingWait
Definition: recorderbase.h:328
QString videocodec
Definition: recorderbase.h:303
uint getNum(void) const
Definition: recorderbase.h:42
unsigned int uint
Definition: compat.h:140
QAtomicInt timeOfFirstDataIsSet
Definition: recorderbase.h:357
QAtomicInt timeOfLatestDataCount
Definition: recorderbase.h:359
AVCodecID m_primaryAudioCodec
Definition: recorderbase.h:302
QMutex positionMapLock
Definition: recorderbase.h:339
MarkTypes
Definition: programtypes.h:48
double toDouble(void) const
Definition: recorderbase.h:40
static int run(MythMediaDevice *dev=nullptr, bool startRandomShow=false)
bool operator==(const FrameRate &other)
Definition: recorderbase.h:45
bool ntsc_framerate
Definition: recorderbase.h:307
QDateTime timeOfLatestData
Definition: recorderbase.h:361
AVContainer m_containerFormat
Definition: recorderbase.h:300
TVRec * tvrec
Definition: recorderbase.h:296
void SetFrameRate(double rate)
Sets the video frame rate.
Definition: recorderbase.h:75
#define MTV_PUBLIC
Definition: mythtvexp.h:15
bool recording
True while recording is actually being performed.
Definition: recorderbase.h:327
RecordingGaps recordingGaps
Definition: recorderbase.h:363
This is the coordinating class of the Recorder Subsystem.
Definition: tv_rec.h:150
bool request_recording
True if API call has requested a recording be [re]started.
Definition: recorderbase.h:325
static void clear(SettingsMap &cache, SettingsMap &overrides, const QString &myKey)
Definition: mythdb.cpp:830
void SetPositionMapType(MarkTypes type)
Set seektable type.
Definition: recorderbase.h:264
double video_frame_rate
Definition: recorderbase.h:308
static const uint16_t * d
FrameRate(uint n, uint d=1)
Definition: recorderbase.h:39
QMutex statisticsLock
Definition: recorderbase.h:356
MarkTypes positionMapType
Definition: recorderbase.h:338
const char * name
Definition: ParseText.cpp:339
This is the abstract base class for supporting recorder hardware.
Definition: recorderbase.h:66
bool isNonzero(void) const
Definition: recorderbase.h:41
long long lastSavedDuration
Definition: recorderbase.h:349
RecordingInfo * nextRecording
Definition: recorderbase.h:334
AVCodecID m_primaryVideoCodec
Definition: recorderbase.h:301
FrameRate m_frameRate
Definition: recorderbase.h:314
Abstract class providing a generic interface to tuning hardware.
Definition: channelbase.h:31
uint m_videoAspect
Definition: recorderbase.h:310
static const uint kTimeOfLatestDataIntervalTarget
timeOfLatest update interval target in milliseconds.
Definition: recorderbase.h:365
QAtomicInt timeOfLatestDataPacketInterval
Definition: recorderbase.h:360
frm_pos_map_t positionMap
Definition: recorderbase.h:340
QMutex nextRingBufferLock
Definition: recorderbase.h:332
QList< RecordingGap > RecordingGaps
Implements a file/stream reader/writer.
MythTimer positionMapTimer
Definition: recorderbase.h:344
QMap< long long, long long > frm_pos_map_t
Frame # -> File offset map.
Definition: programtypes.h:46
frm_pos_map_t positionMapDelta
Definition: recorderbase.h:341
frm_pos_map_t durationMapDelta
Definition: recorderbase.h:343
QString videodevice
Definition: recorderbase.h:304
double GetFrameRate(void) const
Returns the latest frame rate.
Definition: recorderbase.h:219
uint getDen(void) const
Definition: recorderbase.h:43
long long lastSavedKeyframe
Definition: recorderbase.h:348
bool request_pause
Definition: recorderbase.h:320
uint m_videoHeight
Definition: recorderbase.h:312