cnstream_source.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_SOURCE_HPP_
  21. #define CNSTREAM_SOURCE_HPP_
  22. /**
  23. * @file cnstream_source.hpp
  24. *
  25. * This file contains a declaration of the Source Module class.
  26. */
  27. #include <atomic>
  28. #include <memory>
  29. #include <string>
  30. #include <unordered_map>
  31. #include <utility>
  32. #include <vector>
  33. #include "cnstream_common.hpp"
  34. #include "cnstream_module.hpp"
  35. namespace cnstream {
  36. class SourceHandler;
  37. /*!
  38. * @class SourceModule
  39. *
  40. * @brief SourceModule is the base class of source modules.
  41. */
  42. class SourceModule : public Module {
  43. public:
  44. /**
  45. * @brief Constructs a source module.
  46. *
  47. * @param[in] name The name of the source module.
  48. *
  49. * @return No return value.
  50. */
  51. explicit SourceModule(const std::string &name) : Module(name) { hasTransmit_.store(1); }
  52. /**
  53. * @brief Destructs a source module.
  54. *
  55. * @return No return value.
  56. */
  57. virtual ~SourceModule() { RemoveSources(); }
  58. /**
  59. * @brief Adds one stream to DataSource module. This function should be called after pipeline starts.
  60. *
  61. * @param[in] handler The source handler
  62. *
  63. * @retval Returns 0 for success, otherwise returns -1.
  64. */
  65. int AddSource(std::shared_ptr<SourceHandler> handler);
  66. /**
  67. * @brief Destructs a source module.
  68. *
  69. * @param[in] stream_id The stream identifier.
  70. *
  71. * @return Returns the handler of the stream.
  72. */
  73. std::shared_ptr<SourceHandler> GetSourceHandler(const std::string &stream_id);
  74. /**
  75. * @brief Removes one stream from ::DataSource module with given handler. This function should be called before
  76. * pipeline stops.
  77. *
  78. * @param[in] handler The handler of one stream.
  79. * @param[in] force The flag describing the removing behaviour.
  80. *
  81. * @retval 0: success (always success by now).
  82. *
  83. * @note If ``force`` sets to true, the stream will be removed immediately, otherwise the stream will be removed after
  84. * all cached frames are processed.
  85. */
  86. int RemoveSource(std::shared_ptr<SourceHandler> handler, bool force = false);
  87. /**
  88. * @brief Removes one stream from DataSource module with given the stream identification. This function should be
  89. * called before pipeline stops.
  90. *
  91. * @param[in] stream_id The stream identification.
  92. * @param[in] force The flag describing the removing behaviour.
  93. *
  94. * @retval 0: success (always success by now).
  95. *
  96. * @note If ``force`` sets to true, the stream will be removed immediately, otherwise the stream will be removed after
  97. * all cached frames are processed.
  98. */
  99. int RemoveSource(const std::string &stream_id, bool force = false);
  100. /**
  101. * @brief Removes all streams from DataSource module.
  102. *
  103. * @param[in] force The flag describing the removing behaviour.
  104. *
  105. * @retval 0: success (always success by now).
  106. *
  107. * @note If ``force`` sets to true, the stream will be removed immediately, otherwise the stream will be removed after
  108. * all cached frames are processed.
  109. */
  110. int RemoveSources(bool force = false);
  111. #ifdef UNIT_TEST
  112. public: // NOLINT
  113. #else
  114. protected: // NOLINT
  115. #endif
  116. friend class SourceHandler;
  117. /**
  118. * @brief Gets the stream index with the given stream identifier.
  119. *
  120. * @param[in] stream_id The stream identifier.
  121. *
  122. * @return Returns the stream index.
  123. */
  124. uint32_t GetStreamIndex(const std::string &stream_id);
  125. /**
  126. * @brief Gives back the stream index to pipeline.
  127. *
  128. * @param[in] stream_id The stream identifier.
  129. *
  130. * @return No return value.
  131. */
  132. void ReturnStreamIndex(const std::string &stream_id);
  133. /**
  134. * @brief Transmits data to next stage(s) of the pipeline.
  135. *
  136. * @param[in] data The data to be transmitted.
  137. *
  138. * @return Returns true if data is transmitted successfully, othersize returns false.
  139. */
  140. bool SendData(std::shared_ptr<CNFrameInfo> data);
  141. private:
  142. int Process(std::shared_ptr<CNFrameInfo> data) override {
  143. (void)data;
  144. LOGE(CORE) << "As a source module, Process() should not be invoked\n";
  145. return 0;
  146. }
  147. std::mutex mutex_;
  148. std::unordered_map<std::string /*stream_id*/, std::shared_ptr<SourceHandler>> source_map_;
  149. };
  150. /**
  151. * @class SourceHandler
  152. *
  153. * @brief SourceHandler is a class that handles various sources, such as RTSP and video file.
  154. */
  155. class SourceHandler : private NonCopyable {
  156. public:
  157. /**
  158. * @brief Constructs a source handler.
  159. *
  160. * @param[in] module The source module this handler belongs to.
  161. * @param[in] stream_id The name of the stream.
  162. *
  163. * @return No return value.
  164. */
  165. explicit SourceHandler(SourceModule *module, const std::string &stream_id) : module_(module), stream_id_(stream_id) {
  166. if (module_) {
  167. stream_index_ = module_->GetStreamIndex(stream_id_);
  168. }
  169. }
  170. /**
  171. * @brief Destructs a source module.
  172. *
  173. * @return No return value.
  174. */
  175. virtual ~SourceHandler() {
  176. if (module_) {
  177. module_->ReturnStreamIndex(stream_id_);
  178. }
  179. }
  180. /**
  181. * @brief Opens a decoder.
  182. *
  183. * @return Returns true if a decoder is opened successfully, otherwise returns false.
  184. */
  185. virtual bool Open() = 0;
  186. /**
  187. * @brief Closes a decoder.
  188. *
  189. * @return No return value.
  190. */
  191. virtual void Close() = 0;
  192. /**
  193. * @brief Gets the stream identification.
  194. *
  195. * @return Returns the name of stream.
  196. */
  197. std::string GetStreamId() const { return stream_id_; }
  198. /**
  199. * @brief Creates the context of ``CNFameInfo`` .
  200. *
  201. * @param[in] eos The flag marking the frame is end of stream.
  202. * @param[in] payload The payload of ``CNFameInfo``. It's useless now.
  203. *
  204. * @return Returns the context of ``CNFameInfo`` .
  205. */
  206. std::shared_ptr<CNFrameInfo> CreateFrameInfo(bool eos = false, std::shared_ptr<CNFrameInfo> payload = nullptr) {
  207. std::shared_ptr<CNFrameInfo> data = CNFrameInfo::Create(stream_id_, eos, payload);
  208. if (data) {
  209. data->SetStreamIndex(stream_index_);
  210. }
  211. return data;
  212. }
  213. /**
  214. * @brief Sends data to next module.
  215. *
  216. * @param[in] data The data need to be sent to next modules.
  217. *
  218. * @return Returns true if send data successfully, otherwise returns false.
  219. */
  220. bool SendData(std::shared_ptr<CNFrameInfo> data) {
  221. if (this->module_) {
  222. return this->module_->SendData(data);
  223. }
  224. return false;
  225. }
  226. protected:
  227. SourceModule *module_ = nullptr;
  228. mutable std::string stream_id_;
  229. uint32_t stream_index_ = INVALID_STREAM_IDX;
  230. };
  231. } // namespace cnstream
  232. #endif // CNSTREAM_SOURCE_HPP_