cnstream_frame.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 CNSTREAM_FRAME_HPP_
  21. #define CNSTREAM_FRAME_HPP_
  22. #include <memory>
  23. #include <string>
  24. #include <unordered_map>
  25. #include <vector>
  26. #include "cnstream_collection.hpp"
  27. #include "cnstream_common.hpp"
  28. #include "util/cnstream_any.hpp"
  29. /**
  30. * @file cnstream_frame.hpp
  31. *
  32. * This file contains a declaration of the CNFrameInfo class.
  33. */
  34. namespace cnstream {
  35. class Module;
  36. class Pipeline;
  37. /**
  38. * @enum CNFrameFlag
  39. *
  40. * @brief Enumeration variables describing the mask of CNDataFrame.
  41. */
  42. enum class CNFrameFlag {
  43. CN_FRAME_FLAG_EOS = 1 << 0, /*!< This enumeration indicates the end of data stream. */
  44. CN_FRAME_FLAG_INVALID = 1 << 1, /*!< This enumeration indicates an invalid frame. */
  45. CN_FRAME_FLAG_REMOVED = 1 << 2 /*!< This enumeration indicates that the stream has been removed. */
  46. };
  47. /**
  48. * @class CNFrameInfo
  49. *
  50. * @brief CNFrameInfo is a class holding the information of a frame.
  51. *
  52. */
  53. class CNFrameInfo : private NonCopyable {
  54. public:
  55. /**
  56. * @brief Creates a CNFrameInfo instance.
  57. *
  58. * @param[in] stream_id The data stream alias. Identifies which data stream the frame data comes from.
  59. * @param[in] eos Whether this is the end of the stream. This parameter is set to false by default to
  60. * create a CNFrameInfo instance. If you set this parameter to true,
  61. * CNDataFrame::flags will be set to ``CN_FRAME_FLAG_EOS``. Then, the modules
  62. * do not have permission to process this frame. This frame should be handed over to
  63. * the pipeline for processing.
  64. *
  65. * @return Returns ``shared_ptr`` of ``CNFrameInfo`` if this function has run successfully. Otherwise, returns NULL.
  66. */
  67. static std::shared_ptr<CNFrameInfo> Create(const std::string& stream_id, bool eos = false,
  68. std::shared_ptr<CNFrameInfo> payload = nullptr);
  69. CNS_IGNORE_DEPRECATED_PUSH
  70. private:
  71. CNFrameInfo() = default;
  72. public:
  73. /**
  74. * @brief Destructs CNFrameInfo object.
  75. *
  76. * @return No return value.
  77. */
  78. ~CNFrameInfo();
  79. CNS_IGNORE_DEPRECATED_POP
  80. /**
  81. * @brief Checks whether DataFrame is end of stream (EOS) or not.
  82. *
  83. * @return Returns true if the frame is EOS. Returns false if the frame is not EOS.
  84. */
  85. bool IsEos() { return (flags & static_cast<size_t>(cnstream::CNFrameFlag::CN_FRAME_FLAG_EOS)) ? true : false; }
  86. /**
  87. * @brief Checks whether DataFrame is removed or not.
  88. *
  89. * @return Returns true if the frame is EOS. Returns false if the frame is not EOS.
  90. */
  91. bool IsRemoved() {
  92. return (flags & static_cast<size_t>(cnstream::CNFrameFlag::CN_FRAME_FLAG_REMOVED)) ? true : false;
  93. }
  94. /**
  95. * @brief Checks if DataFrame is valid or not.
  96. *
  97. *
  98. *
  99. * @return Returns true if frame is invalid, otherwise returns false.
  100. */
  101. bool IsInvalid() {
  102. return (flags & static_cast<size_t>(cnstream::CNFrameFlag::CN_FRAME_FLAG_INVALID)) ? true : false;
  103. }
  104. /**
  105. * @brief Sets index (usually the index is a number) to identify stream.
  106. *
  107. * @param[in] index Number to identify stream.
  108. *
  109. * @return No return value.
  110. *
  111. * @note This is only used for distributing each stream data to the appropriate thread.
  112. * We do not recommend SDK users to use this API because it will be removed later.
  113. */
  114. void SetStreamIndex(uint32_t index) { channel_idx = index; }
  115. /**
  116. * @brief Gets index number which identifies stream.
  117. *
  118. *
  119. *
  120. * @return Index number.
  121. *
  122. * @note This is only used for distributing each stream data to the appropriate thread.
  123. * We do not recommend SDK users to use this API because it will be removed later.
  124. */
  125. uint32_t GetStreamIndex() const { return channel_idx; }
  126. std::string stream_id; /*!< The data stream aliases where this frame is located to. */
  127. int64_t timestamp = -1; /*!< The time stamp of this frame. */
  128. size_t flags = 0; /*!< The mask for this frame, ``CNFrameFlag``. */
  129. std::string videoPath; // < video path
  130. // user-defined DataFrame,InferResult etc...
  131. CNS_DEPRECATED std::unordered_map<int, any> datas; /*!< (Deprecated) Uses CNFrameInfo::collection instead. */
  132. CNS_DEPRECATED std::mutex datas_lock_; /*!< (Deprecated) Uses CNFrameInfo::collection instead. */
  133. Collection collection; /*!< Stored structured data. */
  134. std::shared_ptr<cnstream::CNFrameInfo> payload = nullptr; /*!< CNFrameInfo instance of parent pipeline. */
  135. private:
  136. /**
  137. * The below methods and members are used by the framework.
  138. */
  139. friend class Pipeline;
  140. mutable uint32_t channel_idx = INVALID_STREAM_IDX; ///< The index of the channel, stream_index
  141. void SetModulesMask(uint64_t mask);
  142. uint64_t GetModulesMask();
  143. uint64_t MarkPassed(Module* current); // return changed mask
  144. std::mutex mask_lock_;
  145. /* Identifies which modules have processed this data */
  146. uint64_t modules_mask_ = 0;
  147. };
  148. /*!
  149. * Defines an alias for the std::shared_ptr<CNFrameInfo>. CNFrameInfoPtr now denotes a shared pointer of frame
  150. * information.
  151. */
  152. using CNFrameInfoPtr = std::shared_ptr<CNFrameInfo>;
  153. } // namespace cnstream
  154. #endif // CNSTREAM_FRAME_HPP_