1 //
2 // ZoneMinder Camera Class Interface, $Date$, $Revision$
3 // Copyright (C) 2001-2008 Philip Coombes
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 //
19 
20 #ifndef ZM_CAMERA_H
21 #define ZM_CAMERA_H
22 
23 #include "zm_image.h"
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 
27 #include <memory>
28 
29 class Monitor;
30 class ZMPacket;
31 
32 //
33 // Abstract base class for cameras. This is intended just to express
34 // common attributes
35 //
36 class Camera {
37 protected:
38   typedef enum { LOCAL_SRC, REMOTE_SRC, FILE_SRC, FFMPEG_SRC, LIBVLC_SRC, CURL_SRC, VNC_SRC } SourceType;
39 
40   const Monitor *monitor;
41   SourceType    type;
42   unsigned int  width;
43   unsigned int  linesize;
44   unsigned int  height;
45   unsigned int  colours;
46   unsigned int  subpixelorder;
47   unsigned int  pixels;
48   unsigned long long imagesize;
49   int           brightness;
50   int           hue;
51   int           colour;
52   int           contrast;
53   bool          capture;
54   bool          record_audio;
55   int                 mVideoStreamId;
56   int                 mAudioStreamId;
57   AVCodecContext      *mVideoCodecContext;
58   AVCodecContext      *mAudioCodecContext;
59   AVStream *mVideoStream;
60   AVStream *mAudioStream;
61   AVFormatContext *mFormatContext; // One for video, one for audio
62   AVFormatContext *mSecondFormatContext; // One for video, one for audio
63   int64_t     mFirstVideoPTS;
64   int64_t     mFirstAudioPTS;
65   int64_t     mLastVideoPTS;
66   int64_t     mLastAudioPTS;
67   unsigned int  bytes;
68 
69 public:
70   Camera(
71       const Monitor* monitor,
72       SourceType p_type,
73       unsigned int p_width,
74       unsigned int p_height,
75       int p_colours,
76       int p_subpixelorder,
77       int p_brightness,
78       int p_contrast,
79       int p_hue,
80       int p_colour,
81       bool p_capture,
82       bool p_record_audio
83       );
84   virtual ~Camera();
85 
Type()86   SourceType Type() const { return type; }
IsLocal()87   bool IsLocal() const { return type == LOCAL_SRC; }
IsRemote()88   bool IsRemote() const { return type == REMOTE_SRC; }
IsFile()89   bool IsFile() const { return type == FILE_SRC; }
IsFfmpeg()90   bool IsFfmpeg() const { return type == FFMPEG_SRC; }
IsLibvlc()91   bool IsLibvlc() const { return type == LIBVLC_SRC; }
IscURL()92   bool IscURL() const { return type == CURL_SRC; }
IsVNC()93   bool IsVNC() const { return type == VNC_SRC; }
Width()94   unsigned int Width() const { return width; }
LineSize()95   unsigned int LineSize() const { return linesize; }
Height()96   unsigned int Height() const { return height; }
Colours()97   unsigned int Colours() const { return colours; }
SubpixelOrder()98   unsigned int SubpixelOrder() const { return subpixelorder; }
Pixels()99   unsigned int Pixels() const { return pixels; }
ImageSize()100   unsigned long long ImageSize() const { return imagesize; }
Bytes()101   unsigned int Bytes() const { return bytes; };
getFrequency()102   int getFrequency() {
103 #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
104     return mAudioStream ? mAudioStream->codecpar->sample_rate : -1;
105 #else
106     return mAudioStream ? mAudioStream->codec->sample_rate : -1;
107 #endif
108   }
getChannels()109   int getChannels() {
110 #if LIBAVCODEC_VERSION_CHECK(57, 64, 0, 64, 0)
111     return mAudioStream ? mAudioStream->codecpar->channels : -1;
112 #else
113     return mAudioStream ? mAudioStream->codec->channels : -1;
114 #endif
115   }
116 
117   virtual int Brightness( int/*p_brightness*/=-1 ) { return -1; }
118   virtual int Hue( int/*p_hue*/=-1 ) { return -1; }
119   virtual int Colour( int/*p_colour*/=-1 ) { return -1; }
120   virtual int Contrast( int/*p_contrast*/=-1 ) { return -1; }
121 
CanCapture()122   bool CanCapture() const { return capture; }
123 
SupportsNativeVideo()124   bool SupportsNativeVideo() const {
125     return (type == FFMPEG_SRC);
126     //return (type == FFMPEG_SRC )||(type == REMOTE_SRC);
127   }
128 
129   virtual AVStream      *getVideoStream();
getAudioStream()130   virtual AVStream      *getAudioStream() { return mAudioStream; };
getVideoCodecContext()131   virtual AVCodecContext     *getVideoCodecContext() { return mVideoCodecContext; };
getAudioCodecContext()132   virtual AVCodecContext     *getAudioCodecContext() { return mAudioCodecContext; };
getVideoStreamId()133   int            getVideoStreamId() { return mVideoStreamId; };
getAudioStreamId()134   int            getAudioStreamId() { return mAudioStreamId; };
135 
PrimeCapture()136   virtual int PrimeCapture() { return 0; }
137   virtual int PreCapture() = 0;
138   virtual int Capture(std::shared_ptr<ZMPacket> &p) = 0;
139   virtual int PostCapture() = 0;
140   virtual int Close() = 0;
141 };
142 
143 #endif // ZM_CAMERA_H
144