Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
Mp4ReaderSource.h
1#pragma once
2
3#include "Module.h"
4#include "declarative/PropertyMacros.h"
5#include <boost/filesystem.hpp>
9
11{
12public:
16
17 Mp4ReaderSourceProps(std::string _videoPath, bool _parseFS, uint16_t _reInitInterval, bool _direction, bool _readLoop, bool _giveLiveTS, int _parseFSTimeoutDuration = 15, bool _bFramesEnabled = false) : ModuleProps()
18 {
19 /* About props:
20 - videoPath - Path of a video from where the reading will start.
21 - reInitInterval - Live Mode if reInitInterval > 0 i.e. reading a file as it gets written. We recheck the last file every reInitInterval time to see if it has been updated.
22 - direction - Playback direction (fwd/bwd).
23 - parseFS - Read the NVR format till infinity, if true. Else we read only one file.
24 - readLoop - Read a single video in loop. It can not be used in conjuction with live mode (reInitInterval > 0) or NVR mode (parseFS = true) mode.
25 - giveLiveTS - If enabled, gives live timestamps instead of recorded timestamps in the video files.
26 */
27
28 if (reInitInterval < 0)
29 {
30 auto errMsg = "incorrect prop reInitInterval <" + std::to_string(reInitInterval) + ">";
31 throw AIPException(AIP_FATAL, errMsg);
32 }
33
34 if (_readLoop && (_reInitInterval || _parseFS))
35 {
36 auto errMsg = "Incorrect parameters. Looping can not be coupled with retry feature or disk parsing. loop <" + std::to_string(_readLoop) +
37 "> reInitInterval <" + std::to_string(reInitInterval) + "> parseFS <" + std::to_string(_parseFS) + ">";
38 throw AIPException(AIP_FATAL, errMsg);
39 }
40 auto canonicalVideoPath = boost::filesystem::canonical(_videoPath);
41 videoPath = canonicalVideoPath.string();
42 parseFS = _parseFS;
43 bFramesEnabled = _bFramesEnabled;
44 direction = _direction;
45 giveLiveTS = _giveLiveTS;
46 if (_reInitInterval < 0)
47 {
48 throw AIPException(AIP_FATAL, "reInitInterval must be 0 or more seconds");
49 }
50 parseFSTimeoutDuration = _parseFSTimeoutDuration;
51 readLoop = _readLoop;
52 reInitInterval = _reInitInterval;
53
54 //If the input file path is the full video path then its root dir will be skipDir else if the input path is only root dir path then it is directly assigned to skipDir.
55 if (parseFS && boost::filesystem::path(videoPath).extension() == ".mp4")
56 {
57 skipDir = boost::filesystem::path(videoPath).parent_path().parent_path().parent_path().string();
58 }
59 else
60 {
61 skipDir = boost::filesystem::path(canonicalVideoPath).string();
62 }
63 }
64
65 void setMaxFrameSizes(size_t _maxImgFrameSize, size_t _maxMetadataFrameSize)
66 {
67 biggerFrameSize = _maxImgFrameSize;
68 biggerMetadataFrameSize = _maxMetadataFrameSize;
69 }
70
72 {
73 return ModuleProps::getSerializeSize() + sizeof(videoPath) + sizeof(parseFS) + sizeof(skipDir) + sizeof(direction) + sizeof(parseFSTimeoutDuration) + sizeof(biggerFrameSize) + sizeof(biggerMetadataFrameSize) + sizeof(bFramesEnabled);
74 }
75
76 std::string skipDir = "./data/Mp4_videos";
77 std::string videoPath = "";
78 size_t biggerFrameSize = 600000;
80 bool parseFS = true;
81 bool direction = true;
82 bool bFramesEnabled = false;
83 uint16_t reInitInterval = 0;
85 bool readLoop = false;
86 bool giveLiveTS = false;
87
88 // Declarative pipeline support: "h264" or "jpeg" to auto-create output pins
89 // If empty (default), output pins must be added manually via addOutPutPin()
90 std::string outputFormat = "";
91
92 // ============================================================
93 // Property Binding for Declarative Pipeline
94 // ============================================================
95 template<typename PropsT>
96 static void applyProperties(
97 PropsT& props,
98 const std::map<std::string, apra::ScalarPropertyValue>& values,
99 std::vector<std::string>& missingRequired
100 ) {
101 apra::applyProp(props.videoPath, "videoPath", values, true, missingRequired);
102 apra::applyProp(props.parseFS, "parseFS", values, false, missingRequired);
103 apra::applyProp(props.direction, "direction", values, false, missingRequired);
104 apra::applyProp(props.bFramesEnabled, "bFramesEnabled", values, false, missingRequired);
105 apra::applyProp(props.reInitInterval, "reInitInterval", values, false, missingRequired);
106 apra::applyProp(props.parseFSTimeoutDuration, "parseFSTimeoutDuration", values, false, missingRequired);
107 apra::applyProp(props.readLoop, "readLoop", values, false, missingRequired);
108 apra::applyProp(props.giveLiveTS, "giveLiveTS", values, false, missingRequired);
109 apra::applyProp(props.outputFormat, "outputFormat", values, false, missingRequired);
110 }
111
112 apra::ScalarPropertyValue getProperty(const std::string& propName) const {
113 if (propName == "videoPath") return videoPath;
114 if (propName == "parseFS") return parseFS;
115 if (propName == "direction") return direction;
116 if (propName == "bFramesEnabled") return bFramesEnabled;
117 if (propName == "reInitInterval") return static_cast<int64_t>(reInitInterval);
118 if (propName == "parseFSTimeoutDuration") return static_cast<int64_t>(parseFSTimeoutDuration);
119 if (propName == "readLoop") return readLoop;
120 if (propName == "giveLiveTS") return giveLiveTS;
121 if (propName == "outputFormat") return outputFormat;
122 throw std::runtime_error("Unknown property: " + propName);
123 }
124
125 bool setProperty(const std::string& propName, const apra::ScalarPropertyValue& value) {
126 // Most properties are static (can't change after init)
127 return false;
128 }
129
130 std::vector<std::string> dynamicPropertyNames() const {
131 return {}; // No dynamically changeable properties
132 }
133
134private:
136
137 template <class Archive>
138 void serialize(Archive& ar, const unsigned int version)
139 {
140 ar& boost::serialization::base_object<ModuleProps>(*this);
141 ar& videoPath;
142 ar& parseFS;
143 ar& skipDir;
144 ar& biggerFrameSize;
146 ar& bFramesEnabled;
148 ar& direction;
149 ar& readLoop;
150 ar& giveLiveTS;
151 }
152};
153
155{
156public:
158 virtual ~Mp4ReaderSource();
159 bool init();
160 bool term();
163 std::string getOpenVideoPath();
164 void setImageMetadata(std::string& pinId, framemetadata_sp& metadata);
165 std::string addOutPutPin(framemetadata_sp& metadata);
166 bool changePlayback(float speed, bool direction);
167 bool getVideoRangeFromCache(std::string videoPath, uint64_t& start_ts, uint64_t& end_ts);
168 bool randomSeek(uint64_t skipTS, bool forceReopen = false);
169 bool refreshCache();
170 std::map<std::string, std::pair<uint64_t, uint64_t>> getCacheSnapShot(); // to be used for debugging only
171 double getOpenVideoFPS();
173 int32_t getOpenVideoFrameCount();
174 void setPlaybackSpeed(float _playbckSpeed);
175 void getResolution(uint32_t& width, uint32_t& height)
176 {
177 width = mWidth;
178 height = mHeight;
179 }
180protected:
181 bool produce();
182 bool validateOutputPins();
183 bool handleCommand(Command::CommandType type, frame_sp& fame);
184 bool handlePropsChange(frame_sp& frame);
185 bool handlePausePlay(float speed, bool direction) override;
186private:
187 std::string h264ImagePinId;
188 std::string encodedImagePinId;
189 uint32_t mWidth = 0;
190 uint32_t mHeight = 0;
192 boost::shared_ptr<Mp4ReaderDetailAbs> mDetail;
194};
CommandType
Definition Command.h:9
Definition Module.h:33
virtual size_t getSerializeSize()
Definition Module.h:101
Definition Module.h:151
Definition Mp4ReaderSource.cpp:18
Definition Mp4ReaderSource.cpp:1148
Definition Mp4ReaderSource.cpp:1134
Definition Mp4ReaderSource.h:11
bool bFramesEnabled
Definition Mp4ReaderSource.h:82
std::string videoPath
Definition Mp4ReaderSource.h:77
bool readLoop
Definition Mp4ReaderSource.h:85
bool setProperty(const std::string &propName, const apra::ScalarPropertyValue &value)
Definition Mp4ReaderSource.h:125
bool direction
Definition Mp4ReaderSource.h:81
Mp4ReaderSourceProps()
Definition Mp4ReaderSource.h:13
size_t biggerMetadataFrameSize
Definition Mp4ReaderSource.h:79
bool giveLiveTS
Definition Mp4ReaderSource.h:86
std::string skipDir
Definition Mp4ReaderSource.h:76
uint16_t reInitInterval
Definition Mp4ReaderSource.h:83
bool parseFS
Definition Mp4ReaderSource.h:80
size_t getSerializeSize()
Definition Mp4ReaderSource.h:71
void setMaxFrameSizes(size_t _maxImgFrameSize, size_t _maxMetadataFrameSize)
Definition Mp4ReaderSource.h:65
Mp4ReaderSourceProps(std::string _videoPath, bool _parseFS, uint16_t _reInitInterval, bool _direction, bool _readLoop, bool _giveLiveTS, int _parseFSTimeoutDuration=15, bool _bFramesEnabled=false)
Definition Mp4ReaderSource.h:17
std::vector< std::string > dynamicPropertyNames() const
Definition Mp4ReaderSource.h:130
size_t biggerFrameSize
Definition Mp4ReaderSource.h:78
friend class boost::serialization::access
Definition Mp4ReaderSource.h:135
apra::ScalarPropertyValue getProperty(const std::string &propName) const
Definition Mp4ReaderSource.h:112
int parseFSTimeoutDuration
Definition Mp4ReaderSource.h:84
static void applyProperties(PropsT &props, const std::map< std::string, apra::ScalarPropertyValue > &values, std::vector< std::string > &missingRequired)
Definition Mp4ReaderSource.h:96
void serialize(Archive &ar, const unsigned int version)
Definition Mp4ReaderSource.h:138
std::string outputFormat
Definition Mp4ReaderSource.h:90
Definition Mp4ReaderSource.h:155
void setPlaybackSpeed(float _playbckSpeed)
Definition Mp4ReaderSource.cpp:1723
bool changePlayback(float speed, bool direction)
Definition Mp4ReaderSource.cpp:1687
bool getVideoRangeFromCache(std::string videoPath, uint64_t &start_ts, uint64_t &end_ts)
Definition Mp4ReaderSource.cpp:1600
bool refreshCache()
Definition Mp4ReaderSource.cpp:1559
double getOpenVideoDurationInSecs()
Definition Mp4ReaderSource.cpp:1591
std::string getOpenVideoPath()
Definition Mp4ReaderSource.cpp:1564
Mp4ReaderSourceProps props
Definition Mp4ReaderSource.h:193
bool validateOutputPins()
Definition Mp4ReaderSource.cpp:1641
bool handlePropsChange(frame_sp &frame)
Definition Mp4ReaderSource.cpp:1673
bool init()
Definition Mp4ReaderSource.cpp:1500
boost::shared_ptr< Mp4ReaderDetailAbs > mDetail
Definition Mp4ReaderSource.h:192
bool handlePausePlay(float speed, bool direction) override
Definition Mp4ReaderSource.cpp:1709
bool randomSeek(uint64_t skipTS, bool forceReopen=false)
Definition Mp4ReaderSource.cpp:1717
std::string metadataFramePinId
Definition Mp4ReaderSource.h:191
uint32_t mHeight
Definition Mp4ReaderSource.h:190
void setProps(Mp4ReaderSourceProps &props)
Definition Mp4ReaderSource.cpp:1682
Mp4ReaderSourceProps getProps()
Definition Mp4ReaderSource.cpp:1668
bool handleCommand(Command::CommandType type, frame_sp &fame)
Definition Mp4ReaderSource.cpp:1693
std::map< std::string, std::pair< uint64_t, uint64_t > > getCacheSnapShot()
Definition Mp4ReaderSource.cpp:1554
std::string encodedImagePinId
Definition Mp4ReaderSource.h:188
void setImageMetadata(std::string &pinId, framemetadata_sp &metadata)
Definition Mp4ReaderSource.cpp:1547
bool term()
Definition Mp4ReaderSource.cpp:1605
Mp4ReaderSource(Mp4ReaderSourceProps _props)
Definition Mp4ReaderSource.cpp:1476
virtual ~Mp4ReaderSource()
Definition Mp4ReaderSource.cpp:1498
int32_t getOpenVideoFrameCount()
Definition Mp4ReaderSource.cpp:1573
bool produce()
Definition Mp4ReaderSource.cpp:1633
std::string h264ImagePinId
Definition Mp4ReaderSource.h:187
void getResolution(uint32_t &width, uint32_t &height)
Definition Mp4ReaderSource.h:175
double getOpenVideoFPS()
Definition Mp4ReaderSource.cpp:1582
std::string addOutPutPin(framemetadata_sp &metadata)
Definition Mp4ReaderSource.cpp:1612
uint32_t mWidth
Definition Mp4ReaderSource.h:189
void applyProp(T &member, const char *propName, const std::map< std::string, ScalarPropertyValue > &values, bool isRequired, std::vector< std::string > &missingRequired)
Definition PropertyMacros.h:36
std::variant< int64_t, double, bool, std::string > ScalarPropertyValue
Definition ModuleRegistry.h:30