data_source.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #ifndef MODULES_DATA_SOURCE_HPP_
  21. #define MODULES_DATA_SOURCE_HPP_
  22. /*!
  23. * @file data_source.hpp
  24. *
  25. * This file contains a declaration of the DataSourceParam and ESPacket struct, and the DataSource, FileHandler,
  26. * RtspHandler, ESMemHandler, ESJpegMemHandler and RawImgMemHandler class.
  27. */
  28. #include "opencv2/highgui/highgui.hpp"
  29. #include "opencv2/imgproc/imgproc.hpp"
  30. #if (CV_MAJOR_VERSION >= 3)
  31. #include "opencv2/imgcodecs/imgcodecs.hpp"
  32. #endif
  33. #include <memory>
  34. #include <string>
  35. #include <utility>
  36. #include "cnstream_frame.hpp"
  37. #include "cnstream_frame_va.hpp"
  38. #include "cnstream_pipeline.hpp"
  39. #include "cnstream_source.hpp"
  40. #include "data_source_param.hpp"
  41. namespace cnstream {
  42. /*!
  43. * @class DataSource
  44. *
  45. * @brief DataSource is a class to handle encoded input data.
  46. *
  47. * @note It is always the first module in a pipeline.
  48. */
  49. class DataSource : public SourceModule, public ModuleCreator<DataSource> {
  50. public:
  51. /*!
  52. * @brief Constructs a DataSource object.
  53. *
  54. * @param[in] moduleName The name of this module.
  55. *
  56. * @return No return value.
  57. */
  58. explicit DataSource(const std::string &moduleName);
  59. /*!
  60. * @brief Destructs a DataSource object.
  61. *
  62. * @return No return value.
  63. */
  64. ~DataSource();
  65. /*!
  66. * @brief Initializes the configuration of the DataSource module.
  67. *
  68. * This function will be called by the pipeline when the pipeline starts.
  69. *
  70. * @param[in] paramSet The module's parameter set to configure a DataSource module.
  71. *
  72. * @return Returns true if the parammeter set is supported and valid, othersize returns false.
  73. */
  74. bool Open(ModuleParamSet paramSet) override;
  75. /*!
  76. * @brief Frees the resources that the object may have acquired.
  77. *
  78. * This function will be called by the pipeline when the pipeline stops.
  79. *
  80. * @return No return value.
  81. */
  82. void Close() override;
  83. /*!
  84. * @brief Checks the parameter set for the DataSource module.
  85. *
  86. * @param[in] paramSet Parameters for this module.
  87. *
  88. * @return Returns true if all parameters are valid. Otherwise, returns false.
  89. */
  90. bool CheckParamSet(const ModuleParamSet &paramSet) const override;
  91. /*!
  92. * @brief Gets the parameters of the DataSource module.
  93. *
  94. * @return Returns the parameters of this module.
  95. *
  96. * @note This function should be called after ``Open`` function.
  97. */
  98. DataSourceParam GetSourceParam() const { return param_; }
  99. private:
  100. DataSourceParam param_;
  101. }; // class DataSource
  102. /*!
  103. * @struct ESPacket
  104. *
  105. * @brief The ESPacket is a structure describing the elementary stream data packet.
  106. */
  107. struct ESPacket {
  108. unsigned char *data = nullptr; /*!< The video data. */
  109. int size = 0; /*!< The size of the data. */
  110. uint64_t pts = 0; /*!< The presentation time stamp of the data. */
  111. uint32_t flags = 0; /*!< The flags of the data. */
  112. enum class FLAG{
  113. FLAG_KEY_FRAME = 0x01, /*!< The flag of key frame. */
  114. FLAG_EOS = 0x02, /*!< The flag of eos (the end of the stream) frame. */
  115. };
  116. }; // struct ESPacket
  117. class FileHandlerImpl;
  118. /*!
  119. * @struct MaximumVideoResolution
  120. *
  121. * @brief The MaximumVideoResolution (not supported on MLU220/MLU270) is a structure describing the maximum video
  122. * resolution parameters.
  123. *
  124. */
  125. struct MaximumVideoResolution {
  126. bool enable_variable_resolutions = false; /*!< Whether to enable variable resolutions. */
  127. uint32_t maximum_width; /*!< The maximum video width. */
  128. uint32_t maximum_height; /*!< The maximum video height. */
  129. }; // struct MaximumVideoResolution
  130. /*!
  131. * @class FileHandler
  132. *
  133. * @brief FileHandler is a class of source handler for video with format mp4, flv, matroska and USBCamera
  134. * ("/dev/videoxxx") .
  135. */
  136. class FileHandler : public SourceHandler {
  137. public:
  138. /*!
  139. * @brief Creates source handler.
  140. *
  141. * @param[in] module The data source module.
  142. * @param[in] stream_id The stream id of the stream.
  143. * @param[in] filename The filename of the stream.
  144. * @param[in] framerate Controls sending the frames of the stream with specific rate.
  145. * @param[in] loop Loops the stream.
  146. * @param[in] maximum_resolution The maximum video resolution for variable video resolutions.
  147. * See ``MaximumVideoResolution`` for detail.
  148. *
  149. * @return Returns source handler if it is created successfully, otherwise returns nullptr.
  150. */
  151. static std::shared_ptr<SourceHandler> Create(DataSource *module, const std::string &stream_id,
  152. const std::string &filename, int framerate, bool loop = false,
  153. const MaximumVideoResolution& maximum_resolution = {});
  154. /*!
  155. * @brief The destructor of FileHandler.
  156. *
  157. * @return No return value.
  158. */
  159. ~FileHandler();
  160. /*!
  161. * @brief Opens source handler.
  162. *
  163. * @return Returns true if the source handler is opened successfully, otherwise returns false.
  164. */
  165. bool Open() override;
  166. /*!
  167. * @brief Closes source handler.
  168. *
  169. * @return No return value
  170. */
  171. void Close() override;
  172. private:
  173. explicit FileHandler(DataSource *module, const std::string &stream_id, const std::string &filename, int framerate,
  174. bool loop, const MaximumVideoResolution& maximum_resolution);
  175. #ifdef UNIT_TEST
  176. public: // NOLINT
  177. #endif
  178. FileHandlerImpl *impl_ = nullptr;
  179. }; // class FileHandler
  180. class RtspHandlerImpl;
  181. /*!
  182. * @class RtspHandler
  183. *
  184. * @brief RtspHandler is a class of source handler for rtsp stream.
  185. */
  186. class RtspHandler : public SourceHandler {
  187. public:
  188. /*!
  189. * @brief Creates source handler.
  190. *
  191. * @param[in] module The data source module.
  192. * @param[in] stream_id The stream ID of the stream.
  193. * @param[in] url_name The url of the stream.
  194. * @param[in] use_ffmpeg Uses ffmpeg demuxer if it is true, otherwise uses live555 demuxer.
  195. * @param[in] reconnect It is valid when "use_ffmpeg" set false.
  196. * @param[in] maximum_resolution The maximum video resolution for variable video resolutions.
  197. * See ``MaximumVideoResolution`` for detail.
  198. *
  199. * @return Returns source handler if it is created successfully, otherwise returns nullptr.
  200. */
  201. static std::shared_ptr<SourceHandler> Create(DataSource *module, const std::string &stream_id,
  202. const std::string &url_name, bool use_ffmpeg = false,
  203. int reconnect = 10,
  204. const MaximumVideoResolution& maximum_resolution = {});
  205. /*!
  206. * @brief The destructor of RtspHandler.
  207. *
  208. * @return No return value.
  209. */
  210. ~RtspHandler();
  211. /*!
  212. * @brief Opens source handler.
  213. *
  214. * @return Returns true if the source handler is opened successfully, otherwise returns false.
  215. */
  216. bool Open() override;
  217. /*!
  218. * @brief Closes source handler.
  219. *
  220. * @return No return value.
  221. */
  222. void Close() override;
  223. private:
  224. explicit RtspHandler(DataSource *module, const std::string &stream_id, const std::string &url_name, bool use_ffmpeg,
  225. int reconnect, const MaximumVideoResolution& maximum_resolution);
  226. #ifdef UNIT_TEST
  227. public: // NOLINT
  228. #endif
  229. RtspHandlerImpl *impl_ = nullptr;
  230. }; // class RtspHandler
  231. class ESMemHandlerImpl;
  232. /*!
  233. * @class ESMemHandler
  234. *
  235. * @brief ESMemHandler is a class of source handler for H264/H265 bitstreams in memory (with prefix-start-code).
  236. */
  237. class ESMemHandler : public SourceHandler {
  238. public:
  239. /*!
  240. * @brief Creates source handler.
  241. *
  242. * @param[in] module The data source module.
  243. * @param[in] stream_id The stream id of the stream.
  244. * @param[in] maximum_resolution The maximum video resolution for variable video resolutions.
  245. * See ``MaximumVideoResolution`` for detail.
  246. *
  247. * @return Returns source handler if it is created successfully, otherwise returns nullptr.
  248. */
  249. static std::shared_ptr<SourceHandler> Create(DataSource *module, const std::string &stream_id,
  250. const MaximumVideoResolution& maximum_resolution = {});
  251. /*!
  252. * @brief The destructor of ESMemHandler.
  253. *
  254. * @return No return value.
  255. */
  256. ~ESMemHandler();
  257. /*!
  258. * @brief Opens source handler.
  259. *
  260. * @return Returns true if the source handler is opened successfully, otherwise returns false.
  261. */
  262. bool Open() override;
  263. /*!
  264. * @brief Closes source handler.
  265. *
  266. * @return No return value.
  267. */
  268. void Close() override;
  269. /*!
  270. * @enum DataType
  271. *
  272. * @brief Enumeration variables describing ES data type.
  273. */
  274. enum class DataType {
  275. INVALID, /*!< Invalid data type. */
  276. H264, /*!< The data type is H264. */
  277. H265 /*!< The data type is H265. */
  278. };
  279. /*!
  280. * @brief Sets data type.
  281. *
  282. * @param[in] type The data type.
  283. *
  284. * @return Returns 0 if data type is set successfully, otherwise returns -1.
  285. * @note This function must be called before ``Write`` function.
  286. */
  287. int SetDataType(DataType type);
  288. /*!
  289. * @brief Sends data in frame mode.
  290. *
  291. * @param[in] pkt The data packet
  292. *
  293. * @return Returns 0 if the data is written successfully.
  294. * Returns -1 if failed to write data. The possible reasons are the handler is closed,
  295. * the end of the stream is received, the data is nullptr and the data is invalid,
  296. * so that the video infomations can not be parsed from it.
  297. */
  298. int Write(ESPacket *pkt);
  299. /*!
  300. * @brief Sends data in chunk mode.
  301. *
  302. * @param[in] buf The data buffer
  303. * @param[in] len The length of the data
  304. *
  305. * @return Returns 0 if the data is written successfully.
  306. * Returns -1 if failed to write data. The possible reasons are the handler is closed, the end of the stream
  307. * is received and the data is invalid, so that the video infomations can not be parsed from it.
  308. */
  309. int Write(unsigned char *buf, int len);
  310. /*!
  311. * @brief Sends the end of the stream.
  312. *
  313. * The data remains in the parser will be dropped. Call this function, when the data of a stream is not completely
  314. * written and the stream needed to be removed.
  315. *
  316. * @return Returns 0 if the end of the stream is written successfully.
  317. * Returns -1 if failed to write data. The possible reason is the handler is closed.
  318. */
  319. int WriteEos();
  320. private:
  321. explicit ESMemHandler(DataSource *module, const std::string &stream_id,
  322. const MaximumVideoResolution& maximum_resolution);
  323. #ifdef UNIT_TEST
  324. public: // NOLINT
  325. #endif
  326. ESMemHandlerImpl *impl_ = nullptr;
  327. }; // class ESMemHandler
  328. class ESJpegMemHandlerImpl;
  329. /*!
  330. * @class ESJpegMemHandler
  331. *
  332. * @brief ESJpegMemHandler is a class of source handler for Jpeg bitstreams in memory.
  333. */
  334. class ESJpegMemHandler : public SourceHandler {
  335. public:
  336. /*!
  337. * @brief Creates source handler.
  338. *
  339. * @param[in] module The data source module.
  340. * @param[in] stream_id The stream id of the stream.
  341. * @param[in] max_width The maximum width of the image.
  342. * @param[in] max_height The maximum height of the image.
  343. *
  344. * @return Returns source handler if it is created successfully, otherwise returns nullptr.
  345. */
  346. static std::shared_ptr<SourceHandler> Create(DataSource *module, const std::string &stream_id,
  347. int max_width = 7680, int max_height = 4320/*Jpeg decoder maximum resolution 8K*/);
  348. /*!
  349. * @brief The destructor of ESJpegMemHandler.
  350. *
  351. * @return No return value.
  352. */
  353. ~ESJpegMemHandler();
  354. /*!
  355. * @brief Opens source handler.
  356. *
  357. * @return Returns true if the source handler is opened successfully, otherwise returns false.
  358. */
  359. bool Open() override;
  360. /*!
  361. * @brief Closes source handler.
  362. *
  363. * @return No return value.
  364. */
  365. void Close() override;
  366. /*!
  367. * @brief Sends data in frame mode.
  368. *
  369. * @param[in] pkt The data packet.
  370. *
  371. * @return Returns 0 if the data is written successfully.
  372. * Returns -1 if failed to write data. The possible reason is the handler is closed or the data is nullptr.
  373. */
  374. int Write(ESPacket *pkt);
  375. private:
  376. explicit ESJpegMemHandler(DataSource *module, const std::string &stream_id, int max_width, int max_height);
  377. #ifdef UNIT_TEST
  378. public: // NOLINT
  379. #endif
  380. ESJpegMemHandlerImpl *impl_ = nullptr;
  381. }; // class ESJpegMemHandler
  382. class RawImgMemHandlerImpl;
  383. /*!
  384. * @class RawImgMemHandler
  385. *
  386. * @brief RawImgMemHandler is a class of source handler for raw image data in memory.
  387. *
  388. * @note This handler will not send data to MLU decoder as the raw data has been decoded.
  389. */
  390. class RawImgMemHandler : public SourceHandler {
  391. public:
  392. /*!
  393. * @brief Creates source handler.
  394. *
  395. * @param[in] module The data source module.
  396. * @param[in] stream_id The stream id of the stream.
  397. *
  398. * @return Returns source handler if it is created successfully, otherwise returns nullptr.
  399. */
  400. static std::shared_ptr<SourceHandler> Create(DataSource *module, const std::string &stream_id);
  401. /*!
  402. * @brief The destructor of RawImgMemHandler.
  403. *
  404. * @return No return value.
  405. */
  406. ~RawImgMemHandler();
  407. /*!
  408. * @brief Opens source handler.
  409. *
  410. * @return Returns true if the source handler is opened successfully, otherwise returns false.
  411. */
  412. bool Open() override;
  413. /*!
  414. * @brief Closes source handler.
  415. *
  416. * @return No return value.
  417. */
  418. void Close() override;
  419. /*!
  420. * @brief Sends raw image with cv::Mat. Only BGR data with 8UC3 type is supported, and data is continuous.
  421. *
  422. * @param[in] mat_data The bgr24 format image data.
  423. * @param[in] pts The pts for mat_data, should be different for each image.
  424. *
  425. * @return Returns 0 if the data is written successfully.
  426. * Returns -1 if failed to write data. The possible reason is the end of the stream is received or failed to
  427. * process the data.
  428. * Returns -2 if the data is invalid.
  429. *
  430. * @note Sends nullptr after all data are sent.
  431. */
  432. int Write(const cv::Mat *mat_data, const uint64_t pts);
  433. /*!
  434. * @brief Sends raw image with image data and image infomation, support formats: bgr24, rgb24, nv21 and nv12.
  435. *
  436. * @param[in] data The data of the image, which is a continuous buffer.
  437. * @param[in] size The size of the data.
  438. * @param[in] pts The pts for raw image, should be different for each image.
  439. * @param[in] width The width of the image.
  440. * @param[in] height The height of the image.
  441. * @param[in] pixel_fmt The pixel format of the image. These formats are supported, bgr24, rgb24, nv21 and nv12.
  442. *
  443. * @return Returns 0 if the data is written successfully.
  444. * Returns -1 if failed to write data. The possible reason is the end of the stream is received or failed to
  445. * process the data.
  446. * Returns -2 if the data is invalid.
  447. *
  448. * @note Sends nullptr as data and passes 0 as size after all data are sent.
  449. */
  450. int Write(const uint8_t *data, const int size, const uint64_t pts, const int width = 0, const int height = 0,
  451. const CNDataFormat pixel_fmt = CNDataFormat::CN_INVALID);
  452. private:
  453. explicit RawImgMemHandler(DataSource *module, const std::string &stream_id);
  454. #ifdef UNIT_TEST
  455. public: // NOLINT
  456. #endif
  457. RawImgMemHandlerImpl *impl_ = nullptr;
  458. }; // class RawImgMemHandler
  459. } // namespace cnstream
  460. #endif // MODULES_DATA_SOURCE_HPP_