test_handler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <gtest/gtest.h>
  21. #include <chrono>
  22. #include <memory>
  23. #include <string>
  24. #include <thread>
  25. #include <vector>
  26. #include "cnstream_source.hpp"
  27. #include "data_handler_file.hpp"
  28. #include "data_handler_mem.hpp"
  29. #include "data_handler_rtsp.hpp"
  30. #include "data_source.hpp"
  31. #include "test_base.hpp"
  32. namespace cnstream {
  33. static constexpr const char *gname = "source";
  34. static constexpr const char *gmp4_path = "../../modules/unitest/source/data/img.mp4";
  35. // device = 0 represents mlu, device = 1 represents cpu
  36. #if 0
  37. static void OpenHandler(std::shared_ptr<DataHandler> handler, int device) {
  38. DataSource *source = dynamic_cast<DataSource*>(handler->module_);
  39. handler->param_ = source->GetSourceParam();
  40. if (device == 0) {
  41. handler->dev_ctx_.dev_type = DevContext::MLU;
  42. handler->dev_ctx_.dev_id = 0;
  43. } else {
  44. handler->dev_ctx_.dev_type = DevContext::CPU;
  45. handler->dev_ctx_.dev_id = -1;
  46. }
  47. handler->dev_ctx_.ddr_channel = handler->stream_index_ % 4;
  48. }
  49. #endif
  50. class SourceHandlerTest : public SourceHandler {
  51. public:
  52. explicit SourceHandlerTest(DataSource *module, const std::string &stream_id) : SourceHandler(module, stream_id) {}
  53. ~SourceHandlerTest() {}
  54. bool Open() override { return true; }
  55. void Close() override {}
  56. };
  57. TEST(SourceHandler, Construct) {
  58. DataSource src(gname);
  59. auto handler = std::make_shared<SourceHandlerTest>(&src, std::to_string(0));
  60. EXPECT_TRUE(handler != nullptr);
  61. }
  62. TEST(SourceHandler, GetStreamId) {
  63. DataSource src(gname);
  64. auto handler = std::make_shared<SourceHandlerTest>(&src, std::to_string(123));
  65. ASSERT_TRUE(handler != nullptr);
  66. EXPECT_TRUE(handler->GetStreamId() == std::to_string(123));
  67. auto handler_2 = std::make_shared<SourceHandlerTest>(&src, std::to_string(2));
  68. ASSERT_TRUE(handler_2 != nullptr);
  69. EXPECT_TRUE(handler_2->GetStreamId() == std::to_string(2));
  70. auto handler_3 = std::make_shared<SourceHandlerTest>(&src, std::to_string(100));
  71. ASSERT_TRUE(handler_3 != nullptr);
  72. EXPECT_TRUE(handler_3->GetStreamId() == std::to_string(100));
  73. }
  74. TEST(DataHandlerFile, OpenClose) {
  75. std::string mp4_path = GetExePath() + gmp4_path;
  76. DataSource *psrc = nullptr;
  77. auto handler_wrong = FileHandler::Create(psrc, std::to_string(0), mp4_path, 30, false);
  78. ASSERT_TRUE(handler_wrong == nullptr);
  79. DataSource src(gname);
  80. auto handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, false);
  81. ModuleParamSet param;
  82. param["output_type"] = "mlu";
  83. param["decoder_type"] = "mlu";
  84. param["device_id"] = "0";
  85. EXPECT_TRUE(src.Open(param));
  86. EXPECT_TRUE(handler->Open());
  87. EXPECT_EQ(handler->GetStreamId(), "0");
  88. handler->Close();
  89. param["output_type"] = "cpu";
  90. param["decoder_type"] = "cpu";
  91. param["device_id"] = "-1";
  92. src.Open(param);
  93. EXPECT_TRUE(handler->Open());
  94. EXPECT_EQ(handler->GetStreamId(), "0");
  95. handler->Close();
  96. }
  97. TEST(DataHandlerFile, PrepareResources) {
  98. DataSource src(gname);
  99. std::string h264_path = GetExePath() + "../../modules/unitest/source/data/img.h264";
  100. std::string flv_path = GetExePath() + "../../modules/unitest/source/data/img.flv";
  101. std::string mkv_path = GetExePath() + "../../modules/unitest/source/data/img.mkv";
  102. std::string mp4_path = GetExePath() + "../../modules/unitest/source/data/img.mp4";
  103. std::string h265_path = GetExePath() + "../../modules/unitest/source/data/265.mp4";
  104. std::string car_path = GetExePath() + "../../modules/unitest/source/data/cars_short.mp4";
  105. // H264
  106. auto handler = FileHandler::Create(&src, std::to_string(0), h264_path, 30, false);
  107. auto file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  108. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  109. file_handler->impl_->ClearResources();
  110. // flv
  111. handler = FileHandler::Create(&src, std::to_string(0), flv_path, 30, false);
  112. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  113. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  114. file_handler->impl_->ClearResources();
  115. // mkv
  116. handler = FileHandler::Create(&src, std::to_string(0), mkv_path, 30, false);
  117. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  118. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  119. file_handler->impl_->ClearResources();
  120. // mp4
  121. handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, false);
  122. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  123. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  124. file_handler->impl_->ClearResources();
  125. // H265
  126. handler = FileHandler::Create(&src, std::to_string(0), h265_path, 30, false);
  127. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  128. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  129. file_handler->impl_->ClearResources();
  130. ModuleParamSet param;
  131. param["output_type"] = "mlu";
  132. param["decoder_type"] = "mlu";
  133. param["device_id"] = "0";
  134. // mlu decoder
  135. EXPECT_TRUE(src.Open(param));
  136. EXPECT_TRUE(file_handler->Open());
  137. file_handler->Close();
  138. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  139. file_handler->impl_->ClearResources();
  140. // cpu decoder
  141. param["decoder_type"] = "cpu";
  142. param["output_type"] = "cpu";
  143. param["device_id"] = "-1";
  144. EXPECT_TRUE(src.Open(param));
  145. auto cpu_handler = FileHandler::Create(&src, std::to_string(0), car_path, 30, false);
  146. auto cpu_file_handler = std::dynamic_pointer_cast<FileHandler>(cpu_handler);
  147. EXPECT_TRUE(cpu_file_handler->Open());
  148. cpu_file_handler->Close();
  149. EXPECT_TRUE(cpu_file_handler->impl_->PrepareResources());
  150. cpu_file_handler->impl_->ClearResources();
  151. }
  152. TEST(DataHandlerFile, ProcessMlu) {
  153. DataSource src(gname);
  154. std::string h264_path = GetExePath() + "../../modules/unitest/source/data/img.h264";
  155. // std::string flv_path = GetExePath() + "../../modules/unitest/source/data/img.flv";
  156. std::string mkv_path = GetExePath() + "../../modules/unitest/source/data/img.mkv";
  157. std::string mp4_path = GetExePath() + "../../modules/unitest/source/data/img.mp4";
  158. std::string hevc_path = GetExePath() + "../../modules/unitest/source/data/img.hevc";
  159. std::string car_path = GetExePath() + "../../modules/unitest/source/data/cars_short.mp4";
  160. // H264
  161. auto handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, false);
  162. auto file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  163. ModuleParamSet param;
  164. param["output_type"] = "mlu";
  165. param["decoder_type"] = "mlu";
  166. param["device_id"] = "0";
  167. EXPECT_TRUE(src.Open(param));
  168. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  169. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  170. // img.mp4 has 5 frames
  171. for (uint32_t i = 0; i < 5; i++) {
  172. EXPECT_TRUE(file_handler->impl_->Process());
  173. }
  174. // loop is set to false, send eos and return false
  175. EXPECT_FALSE(file_handler->impl_->Process());
  176. file_handler->impl_->ClearResources();
  177. // set loop to true
  178. handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, true);
  179. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  180. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  181. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  182. uint32_t loop = 10;
  183. while (loop--) {
  184. // img.mp4 has 5 frames
  185. for (uint32_t i = 0; i < 5; i++) {
  186. EXPECT_TRUE(file_handler->impl_->Process());
  187. }
  188. // loop is set to true, do not send eos and return true
  189. EXPECT_TRUE(file_handler->impl_->Process());
  190. }
  191. file_handler->impl_->ClearResources();
  192. // reuse codec buffer
  193. param["reuse_cndec_buf"] = "true";
  194. handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, false);
  195. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  196. EXPECT_TRUE(src.Open(param));
  197. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  198. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  199. EXPECT_TRUE(file_handler->impl_->Process());
  200. file_handler->impl_->ClearResources();
  201. // h264
  202. handler = FileHandler::Create(&src, std::to_string(0), h264_path, 30, false);
  203. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  204. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  205. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  206. EXPECT_TRUE(file_handler->impl_->Process());
  207. file_handler->impl_->ClearResources();
  208. // mkv
  209. handler = FileHandler::Create(&src, std::to_string(0), mkv_path, 30, false);
  210. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  211. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  212. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  213. EXPECT_TRUE(file_handler->impl_->Process());
  214. file_handler->impl_->ClearResources();
  215. // mp4
  216. handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, false);
  217. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  218. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  219. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  220. EXPECT_TRUE(file_handler->impl_->Process());
  221. file_handler->impl_->ClearResources();
  222. // HEVC
  223. handler = FileHandler::Create(&src, std::to_string(0), hevc_path, 30, false);
  224. file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  225. file_handler->impl_->SetDecodeParam(src.GetSourceParam());
  226. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  227. EXPECT_TRUE(file_handler->impl_->Process());
  228. file_handler->impl_->ClearResources();
  229. }
  230. TEST(DataHandlerFile, ProcessCpu) {
  231. DataSource src(gname);
  232. std::string mp4_path = GetExePath() + "../../modules/unitest/source/data/cars_short.mp4";
  233. // H264
  234. auto handler = FileHandler::Create(&src, std::to_string(0), mp4_path, 30, false);
  235. auto file_handler = std::dynamic_pointer_cast<FileHandler>(handler);
  236. ModuleParamSet param;
  237. param["output_type"] = "cpu";
  238. param["decoder_type"] = "cpu";
  239. EXPECT_TRUE(src.Open(param));
  240. EXPECT_TRUE(file_handler->Open());
  241. file_handler->Close();
  242. EXPECT_TRUE(file_handler->impl_->PrepareResources());
  243. // cars.mp4 has 11 frames
  244. for (uint32_t i = 0; i < 11; i++) {
  245. EXPECT_TRUE(file_handler->impl_->Process());
  246. }
  247. // loop is set to false, send eos and return false
  248. EXPECT_FALSE(file_handler->impl_->Process());
  249. file_handler->impl_->ClearResources();
  250. }
  251. } // namespace cnstream