test_handler_mem.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "data_handler_mem.hpp"
  27. #include "data_source.hpp"
  28. #include "test_base.hpp"
  29. #define TestSize 10
  30. namespace cnstream {
  31. static constexpr const char *gname = "source";
  32. static constexpr const char *gh264_path = "../../modules/unitest/source/data/raw.h264";
  33. static void ResetParam(ModuleParamSet &param) { // NOLINT
  34. param["output_type"] = "mlu";
  35. param["device_id"] = "0";
  36. param["interval"] = "1";
  37. param["decoder_type"] = "mlu";
  38. param["input_buf_number"] = "32"; // Max is 32 due to Codec's limitation
  39. param["output_buf_number"] = "32"; // Max is 32 due to Codec's limitation
  40. }
  41. class EsMemObserver : public IModuleObserver {
  42. public:
  43. int GetCnt() {
  44. return count;
  45. }
  46. void Wait() {
  47. while (!get_eos) {
  48. std::chrono::seconds sec(1);
  49. std::this_thread::sleep_for(sec);
  50. }
  51. }
  52. void Reset() {
  53. get_eos.store(false);
  54. count.store(0);
  55. }
  56. private:
  57. void notify(std::shared_ptr<CNFrameInfo> data) override {
  58. if (!data->IsEos()) {
  59. count++;
  60. } else {
  61. get_eos = true;
  62. }
  63. }
  64. std::atomic<int> count{0};
  65. std::atomic<bool> get_eos{false};
  66. };
  67. TEST(DataHandlerMem, Write) {
  68. DataSource src(gname);
  69. EsMemObserver observer;
  70. src.SetObserver(&observer);
  71. ModuleParamSet param;
  72. ResetParam(param);
  73. ASSERT_TRUE(src.CheckParamSet(param));
  74. ASSERT_TRUE(src.Open(param));
  75. std::string video_path = GetExePath() + gh264_path;
  76. FILE *fp = fopen(video_path.c_str(), "rb");
  77. ASSERT_TRUE(fp != nullptr);
  78. unsigned char buf[4096];
  79. {
  80. auto handler = ESMemHandler::Create(&src, std::to_string(0));
  81. ASSERT_TRUE(handler != nullptr);
  82. EXPECT_TRUE(handler->GetStreamId() == std::to_string(0));
  83. auto memHandler = std::dynamic_pointer_cast<cnstream::ESMemHandler>(handler);
  84. EXPECT_EQ(memHandler->SetDataType(ESMemHandler::DataType::H264), 0);
  85. EXPECT_EQ(memHandler->Open(), true);
  86. while (!feof(fp)) {
  87. int size = fread(buf, 1, 4096, fp);
  88. EXPECT_EQ(memHandler->Write(buf, size), 0);
  89. }
  90. EXPECT_EQ(memHandler->Write(nullptr, 0), 0);
  91. observer.Wait();
  92. memHandler->Close();
  93. int frame_cnt = 5;
  94. EXPECT_EQ(observer.GetCnt(), frame_cnt);
  95. }
  96. observer.Reset();
  97. fseek(fp, 0, SEEK_SET);
  98. {
  99. auto handler = ESMemHandler::Create(&src, std::to_string(0));
  100. ASSERT_TRUE(handler != nullptr);
  101. EXPECT_TRUE(handler->GetStreamId() == std::to_string(0));
  102. auto memHandler = std::dynamic_pointer_cast<cnstream::ESMemHandler>(handler);
  103. EXPECT_EQ(memHandler->SetDataType(ESMemHandler::DataType::H264), 0);
  104. EXPECT_EQ(memHandler->Open(), true);
  105. for (uint32_t i = 0; i < 4; i++) {
  106. int size = fread(buf, 1, 4096, fp);
  107. EXPECT_EQ(memHandler->Write(buf, size), 0);
  108. }
  109. EXPECT_EQ(memHandler->WriteEos(), 0);
  110. observer.Wait();
  111. memHandler->Close();
  112. int frame_cnt = 2;
  113. EXPECT_EQ(observer.GetCnt(), frame_cnt);
  114. }
  115. fclose(fp);
  116. src.Close();
  117. }
  118. TEST(DataHandlerMem, WriteEosOnly) {
  119. DataSource src(gname);
  120. EsMemObserver observer;
  121. src.SetObserver(&observer);
  122. ModuleParamSet param;
  123. ResetParam(param);
  124. ASSERT_TRUE(src.CheckParamSet(param));
  125. ASSERT_TRUE(src.Open(param));
  126. {
  127. auto handler = ESMemHandler::Create(&src, std::to_string(0));
  128. ASSERT_TRUE(handler != nullptr);
  129. EXPECT_TRUE(handler->GetStreamId() == std::to_string(0));
  130. auto memHandler = std::dynamic_pointer_cast<cnstream::ESMemHandler>(handler);
  131. EXPECT_EQ(memHandler->SetDataType(ESMemHandler::DataType::H264), 0);
  132. EXPECT_EQ(memHandler->Open(), true);
  133. EXPECT_EQ(memHandler->WriteEos(), 0);
  134. observer.Wait();
  135. memHandler->Close();
  136. EXPECT_EQ(observer.GetCnt(), 0);
  137. }
  138. observer.Reset();
  139. {
  140. auto handler = ESMemHandler::Create(&src, std::to_string(0));
  141. ASSERT_TRUE(handler != nullptr);
  142. EXPECT_TRUE(handler->GetStreamId() == std::to_string(0));
  143. auto memHandler = std::dynamic_pointer_cast<cnstream::ESMemHandler>(handler);
  144. EXPECT_EQ(memHandler->SetDataType(ESMemHandler::DataType::H264), 0);
  145. EXPECT_EQ(memHandler->Open(), true);
  146. EXPECT_EQ(memHandler->Write(nullptr, 0), 0);
  147. observer.Wait();
  148. memHandler->Close();
  149. EXPECT_EQ(observer.GetCnt(), 0);
  150. }
  151. src.Close();
  152. }
  153. } // namespace cnstream