nx_server_plugin_sdk 1.0 Server Plugin SDK

rpi_camera.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #ifndef _RPI_CAMERA_H_
4 #define _RPI_CAMERA_H_
5
6 #include <memory>
7 #include <mutex>
8
9 #include "rpi_omx.h"
10 #include "rw_lock.h"
11
13 namespace rpi_cam
14 {
16 typedef enum
17 {
18 Filter_None,
19 Filter_Noise,
20 Filter_Emboss,
21 Filter_Negative,
22 Filter_Sketch,
23 Filter_OilPaint,
24 Filter_Hatch,
25 Filter_Gpen,
26 Filter_Antialias,
27 Filter_DeRing,
28 Filter_Solarize
29 } ImageFilter;
30
32 struct CameraParameters
33 {
34 CameraParameters(rpi_omx::VideoFromat fmt = rpi_omx::VF_1280x960, ImageFilter filt = Filter_None)
35 : vfmt(fmt),
36 filter(filt),
37 sharpness(0),
38 contrast(0),
39 saturation(0),
40 brightness(50)
41 {}
42
43 rpi_omx::VideoFromat vfmt;
44 unsigned filter;
45 int sharpness;
46 int contrast;
47 int saturation;
48 int brightness;
49 };
50
52 struct EncoderParameters
53 {
54 EncoderParameters()
55 : vfmt(rpi_omx::VF_1280x960),
56 bitrateKbps(0)
57 {}
58
59 rpi_omx::VideoFromat vfmt;
60 unsigned bitrateKbps;
61 };
62
64 class RPiCamera
65 {
66 public:
67 typedef rw_lock::OneWriterManyReaders RWLock;
68
69 enum
70 {
71 FLAG_EOS = OMX_BUFFERFLAG_EOS,
72 FLAG_DECODEONLY = OMX_BUFFERFLAG_DECODEONLY,
73 FLAG_DATACORRUPT = OMX_BUFFERFLAG_DATACORRUPT,
74 FLAG_ENDOFFRAME = OMX_BUFFERFLAG_ENDOFFRAME,
75 FLAG_SYNCFRAME = OMX_BUFFERFLAG_SYNCFRAME,
76 FLAG_EXTRADATA = OMX_BUFFERFLAG_EXTRADATA,
77 FLAG_CODECCONFIG = OMX_BUFFERFLAG_CODECCONFIG
78 };
79
80 static constexpr unsigned STREAMS_NUM() { return 2; }
81 static constexpr unsigned BITRATE_0() { return 6000; }
82 static constexpr unsigned BITRATE_1() { return 500; }
83 static constexpr unsigned IFRAME_PERIOD() { return 5; }
84
85 static void init();
86 static void deinit();
87 static const rpi_omx::VideoFromat * getVideoFormats(unsigned& num, bool resized = false);
88
89 RPiCamera(const CameraParameters& camParams);
90 ~RPiCamera();
91
92 bool isOK() const;
93 bool read(unsigned streamNo, std::vector<uint8_t>& data, uint64_t& timeStamp, unsigned& flags);
94 void getEncoderConfig(unsigned streamNo, unsigned& width, unsigned& height, unsigned& fps, unsigned& bitrateKbps) const;
95 void configEncoder(unsigned streamNo, unsigned width, unsigned height, unsigned framerate, unsigned bitrateKbps);
96 void update(); // update if need it
97
98 private:
99 static std::recursive_mutex m_mutex; // locks entire class, not just an object
100 std::unique_ptr<rpi_omx::Camera> m_camera;
101 std::unique_ptr<rpi_omx::VideoSplitter> m_vsplitter;
102 std::unique_ptr<rpi_omx::Resizer> m_resizer;
103 std::unique_ptr<rpi_omx::Encoder> m_encoders[2];
104 CameraParameters m_camParams;
105 EncoderParameters m_encParams[2];
106 RWLock m_rw;
107 bool m_needUpdate;
108
109 static void updateRatio(rpi_omx::VideoFromat& fmt);
110 static const rpi_omx::VideoFromat& getResizedFormat(const rpi_omx::VideoFromat& f);
111
112 //
113
114 void prepare(const CameraParameters& camParams, const EncoderParameters& encParams0, const EncoderParameters& encParams1);
115 void release();
116 void resetErrors();
117
118 void switchState(OMX_STATETYPE state);
119
120 void configCamera(const CameraParameters& params);
121 void configEncoders(const EncoderParameters& params0, const EncoderParameters& params1);
122
123 void setupTunnels();
124 void teardownTunnels();
125
126 void enablePorts();
127 void disablePorts();
128
129 void allocateBuffers();
130 void returnBuffers();
131 void flushBuffers();
132 void freeBuffers();
133
134 void startCapture();
135 void stopCapture();
136
137 void dumpPorts();
138 };
139 }
140
141 #endif
rpi_cam::CameraParameters::filter
unsigned filter
Image filter.
Definition: rpi_camera.h:44
rpi_cam::Filter_Emboss
Emboss.
Definition: rpi_camera.h:20
rpi_cam::CameraParameters
Resolutions and common camera parameters.
Definition: rpi_camera.h:32
rpi_cam
Raspberry Pi camera unit and configuration.
Definition: camera_manager.cpp:21
rpi_cam::Filter_Hatch
Hatch.
Definition: rpi_camera.h:24
rpi_cam::Filter_DeRing
DeRing.
Definition: rpi_camera.h:27
rw_lock::OneWriterManyReaders
Definition: rw_lock.h:9
rpi_cam::Filter_None
No filter.
Definition: rpi_camera.h:18
rpi_cam::Filter_Sketch
Sketch.
Definition: rpi_camera.h:22
rpi_cam::Filter_OilPaint
OilPaint.
Definition: rpi_camera.h:23
rpi_cam::CameraParameters::contrast
int contrast
-100..100
Definition: rpi_camera.h:46
rpi_cam::CameraParameters::saturation
int saturation
-100..100
Definition: rpi_camera.h:47
rpi_cam::EncoderParameters
Encoder parameters.
Definition: rpi_camera.h:52
rpi_cam::ImageFilter
ImageFilter
Definition: rpi_camera.h:16
rpi_omx::VideoFromat
Definition: rpi_omx.h:75
rpi_cam::Filter_Noise
Remove noise.
Definition: rpi_camera.h:19
rpi_cam::Filter_Solarize
Solarize.
Definition: rpi_camera.h:28
rpi_cam::CameraParameters::brightness
int brightness
0..100
Definition: rpi_camera.h:48
rpi_cam::Filter_Gpen
Gpen.
Definition: rpi_camera.h:25
rpi_cam::CameraParameters::sharpness
int sharpness
-100..100
Definition: rpi_camera.h:45
rpi_cam::RPiCamera
Raspberry Pi camera unit.
Definition: rpi_camera.h:64
rpi_cam::Filter_Negative
Negative.
Definition: rpi_camera.h:21
rpi_cam::RPiCamera::read
bool read(unsigned streamNo, std::vector< uint8_t > &data, uint64_t &timeStamp, unsigned &flags)
Definition: rpi_camera.cpp:269
rpi_cam::Filter_Antialias
Antialias.
Definition: rpi_camera.h:26