Logo
ApraPipes 1.0
Loading...
Searching...
No Matches
Mp4ReaderSource.h
1#pragma once
2
3#include "Module.h"
4#include <boost/filesystem.hpp>
8
10{
11public:
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;
87private:
89
90 template <class Archive>
91 void serialize(Archive& ar, const unsigned int version)
92 {
93 ar& boost::serialization::base_object<ModuleProps>(*this);
94 ar& videoPath;
95 ar& parseFS;
96 ar& skipDir;
101 ar& direction;
102 ar& readLoop;
103 ar& giveLiveTS;
104 }
105};
106
108{
109public:
111 virtual ~Mp4ReaderSource();
112 bool init();
113 bool term();
116 std::string getOpenVideoPath();
117 void setImageMetadata(std::string& pinId, framemetadata_sp& metadata);
118 std::string addOutPutPin(framemetadata_sp& metadata);
119 bool changePlayback(float speed, bool direction);
120 bool getVideoRangeFromCache(std::string videoPath, uint64_t& start_ts, uint64_t& end_ts);
121 bool randomSeek(uint64_t skipTS, bool forceReopen = false);
122 bool refreshCache();
123 std::map<std::string, std::pair<uint64_t, uint64_t>> getCacheSnapShot(); // to be used for debugging only
124 double getOpenVideoFPS();
126 int32_t getOpenVideoFrameCount();
127 void setPlaybackSpeed(float _playbckSpeed);
128 void getResolution(uint32_t& width, uint32_t& height)
129 {
130 width = mWidth;
131 height = mHeight;
132 }
133protected:
134 bool produce();
135 bool validateOutputPins();
136 bool handleCommand(Command::CommandType type, frame_sp& fame);
137 bool handlePropsChange(frame_sp& frame);
138 bool handlePausePlay(float speed, bool direction) override;
139private:
140 std::string h264ImagePinId;
141 std::string encodedImagePinId;
142 uint32_t mWidth = 0;
143 uint32_t mHeight = 0;
145 boost::shared_ptr<Mp4ReaderDetailAbs> mDetail;
147};
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:10
bool bFramesEnabled
Definition Mp4ReaderSource.h:82
std::string videoPath
Definition Mp4ReaderSource.h:77
bool readLoop
Definition Mp4ReaderSource.h:85
bool direction
Definition Mp4ReaderSource.h:81
Mp4ReaderSourceProps()
Definition Mp4ReaderSource.h:12
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
size_t biggerFrameSize
Definition Mp4ReaderSource.h:78
friend class boost::serialization::access
Definition Mp4ReaderSource.h:88
int parseFSTimeoutDuration
Definition Mp4ReaderSource.h:84
void serialize(Archive &ar, const unsigned int version)
Definition Mp4ReaderSource.h:91
Definition Mp4ReaderSource.h:108
void setPlaybackSpeed(float _playbckSpeed)
Definition Mp4ReaderSource.cpp:1706
bool changePlayback(float speed, bool direction)
Definition Mp4ReaderSource.cpp:1670
bool getVideoRangeFromCache(std::string videoPath, uint64_t &start_ts, uint64_t &end_ts)
Definition Mp4ReaderSource.cpp:1583
bool refreshCache()
Definition Mp4ReaderSource.cpp:1542
double getOpenVideoDurationInSecs()
Definition Mp4ReaderSource.cpp:1574
std::string getOpenVideoPath()
Definition Mp4ReaderSource.cpp:1547
Mp4ReaderSourceProps props
Definition Mp4ReaderSource.h:146
bool validateOutputPins()
Definition Mp4ReaderSource.cpp:1624
bool handlePropsChange(frame_sp &frame)
Definition Mp4ReaderSource.cpp:1656
bool init()
Definition Mp4ReaderSource.cpp:1483
boost::shared_ptr< Mp4ReaderDetailAbs > mDetail
Definition Mp4ReaderSource.h:145
bool handlePausePlay(float speed, bool direction) override
Definition Mp4ReaderSource.cpp:1692
bool randomSeek(uint64_t skipTS, bool forceReopen=false)
Definition Mp4ReaderSource.cpp:1700
std::string metadataFramePinId
Definition Mp4ReaderSource.h:144
uint32_t mHeight
Definition Mp4ReaderSource.h:143
void setProps(Mp4ReaderSourceProps &props)
Definition Mp4ReaderSource.cpp:1665
Mp4ReaderSourceProps getProps()
Definition Mp4ReaderSource.cpp:1651
bool handleCommand(Command::CommandType type, frame_sp &fame)
Definition Mp4ReaderSource.cpp:1676
std::map< std::string, std::pair< uint64_t, uint64_t > > getCacheSnapShot()
Definition Mp4ReaderSource.cpp:1537
std::string encodedImagePinId
Definition Mp4ReaderSource.h:141
void setImageMetadata(std::string &pinId, framemetadata_sp &metadata)
Definition Mp4ReaderSource.cpp:1530
bool term()
Definition Mp4ReaderSource.cpp:1588
Mp4ReaderSource(Mp4ReaderSourceProps _props)
Definition Mp4ReaderSource.cpp:1476
virtual ~Mp4ReaderSource()
Definition Mp4ReaderSource.cpp:1481
int32_t getOpenVideoFrameCount()
Definition Mp4ReaderSource.cpp:1556
bool produce()
Definition Mp4ReaderSource.cpp:1616
std::string h264ImagePinId
Definition Mp4ReaderSource.h:140
void getResolution(uint32_t &width, uint32_t &height)
Definition Mp4ReaderSource.h:128
double getOpenVideoFPS()
Definition Mp4ReaderSource.cpp:1565
std::string addOutPutPin(framemetadata_sp &metadata)
Definition Mp4ReaderSource.cpp:1595
uint32_t mWidth
Definition Mp4ReaderSource.h:142