Torc  0.1
torccamera.h
Go to the documentation of this file.
1 #ifndef TORCCAMERA_H
2 #define TORCCAMERA_H
3 
4 // Qt
5 #include <QObject>
6 
7 // Torc
8 #include "torcmaths.h"
10 #include "ffmpeg/torcmuxer.h"
11 
12 // FFmpeg
13 #include <libavcodec/avcodec.h>
14 
15 // NB these are also enforced in the XSD
16 #define VIDEO_WIDTH_MIN 640
17 #define VIDEO_WIDTH_MAX 1920
18 #define VIDEO_HEIGHT_MIN 480
19 #define VIDEO_HEIGHT_MAX 1080
20 #define VIDEO_FRAMERATE_MIN 1
21 #define VIDEO_FRAMERATE_MAX 60
22 #define VIDEO_BITRATE_MIN 128000
23 #define VIDEO_BITRATE_MAX 25000000
24 #define VIDEO_SEGMENT_TARGET 2 // 2 second segments
25 #define VIDEO_GOPDURA_TARGET 1 // with IDR every second
26 #define VIDEO_SEGMENT_NUMBER 10 // 10 segments for a total of 20 buffered seconds
27 #define VIDEO_SEGMENT_MAX 20
28 #define VIDEO_TIMEBASE 90000
29 #define VIDEO_DRIFT_SHORT 60 // short term drift average
30 #define VIDEO_DRIFT_LONG (60*5) // long term drift average
31 #define VIDEO_H264_PROFILE FF_PROFILE_H264_MAIN
32 
34 {
35  public:
36  TorcCameraParams(void);
37  explicit TorcCameraParams(const QVariantMap &Details);
38  ~TorcCameraParams() = default;
39  TorcCameraParams(const TorcCameraParams &Other) = default;
42  bool operator == (const TorcCameraParams &Other) const;
43 
44  bool IsVideo (void) const;
45  bool IsStill (void) const;
46  bool IsCompatible (const TorcCameraParams &Other) const;
47 
48  bool m_valid;
49  int m_width;
50  int m_height;
51  int m_stride;
54  int m_bitrate;
57  int m_gopSize;
58  QString m_videoCodec;
59  QString m_contentDir;
60 };
61 
62 Q_DECLARE_METATYPE(TorcCameraParams)
63 
64 class TorcCameraDevice : public QObject
65 {
66  Q_OBJECT
67 
68  public:
69  explicit TorcCameraDevice(const TorcCameraParams &Params);
70  virtual ~TorcCameraDevice();
71 
72  virtual bool Setup (void);
73  virtual bool Start (void) = 0;
74  virtual bool Stop (void) = 0;
75  QByteArray GetSegment (int Segment);
76  QByteArray GetInitSegment (void);
77 
78  public slots:
79  virtual void TakeStills (uint Count);
80  virtual void StreamVideo (bool Video) = 0;
81 
82  signals:
83  void WritingStarted (void);
84  void WritingStopped (void);
85  void SegmentRemoved (int Segment);
86  void InitSegmentReady(void);
87  void SegmentReady (int Segment);
88  void SetErrored (bool Errored);
89  void StillReady (const QString &File);
90  void ParametersChanged (TorcCameraParams &Params);
91 
92  protected:
93  // streaming
94  void TrackDrift (void);
95  // stills
96  virtual void StartStill (void) = 0;
97  virtual bool EnableStills (uint Count);
98  void SaveStill (void);
99  void SaveStillBuffer (quint32 Length, uint8_t* Data);
100  void ClearStillsBuffers (void);
101 
103 
104  // video/streaming
107  quint64 m_frameCount;
109  AVPacket *m_bufferedPacket;
111  QReadWriteLock m_ringBufferLock;
116 
117  // stills
120  QList<QPair<quint32, uint8_t*> > m_stillsBuffers;
121 
122  private:
123  Q_DISABLE_COPY(TorcCameraDevice)
124 };
125 
127 {
128  public:
130  virtual ~TorcCameraFactory() = default;
131 
132  static TorcCameraDevice* GetCamera (const QString &Type, const TorcCameraParams &Params);
133  static TorcCameraFactory* GetTorcCameraFactory (void);
134  TorcCameraFactory* NextFactory (void) const;
135  virtual bool CanHandle (const QString &Type, const TorcCameraParams &Params) = 0;
136  virtual TorcCameraDevice* Create (const QString &Type, const TorcCameraParams &Params) = 0;
137  virtual QString GetCameraName (void) = 0;
138 
139  protected:
142 
143  private:
144  Q_DISABLE_COPY(TorcCameraFactory)
145 };
146 
147 #endif // TORCCAMERA_H
TorcAverage< double > m_shortAverage
Definition: torccamera.h:114
QString m_videoCodec
Definition: torccamera.h:58
TorcCameraParams(void)
Definition: torccamera.cpp:31
TorcCameraParams m_params
Definition: torccamera.h:102
~TorcCameraParams()=default
QReadWriteLock m_ringBufferLock
Definition: torccamera.h:111
TorcAverage< double > m_longAverage
Definition: torccamera.h:115
QString m_contentDir
Definition: torccamera.h:59
AVPacket * m_bufferedPacket
Definition: torccamera.h:109
bool IsVideo(void) const
Definition: torccamera.cpp:234
quint64 m_frameCount
Definition: torccamera.h:107
bool m_haveInitSegment
Definition: torccamera.h:108
quint64 m_referenceTime
Definition: torccamera.h:112
bool IsCompatible(const TorcCameraParams &Other) const
Definition: torccamera.cpp:244
static TorcCameraFactory * gTorcCameraFactory
Definition: torccamera.h:140
TorcCameraParams & operator=(const TorcCameraParams &Other)
Definition: torccamera.cpp:152
TorcCameraFactory * nextTorcCameraFactory
Definition: torccamera.h:141
bool operator==(const TorcCameraParams &Other) const
Definition: torccamera.cpp:172
QList< QPair< quint32, uint8_t * > > m_stillsBuffers
Definition: torccamera.h:120
TorcCameraParams Combine(const TorcCameraParams &Add)
Definition: torccamera.cpp:189
bool IsStill(void) const
Definition: torccamera.cpp:239
TorcMuxer * m_muxer
Definition: torccamera.h:105
TorcSegmentedRingBuffer * m_ringBuffer
Definition: torccamera.h:110