test_display.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <gtest/gtest.h>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <memory>
  5. #include <string>
  6. #include <thread>
  7. #include <vector>
  8. #include "cnstream_frame_va.hpp"
  9. #include "displayer.hpp"
  10. namespace cnstream {
  11. static constexpr const char *gname = "display";
  12. TEST(Display, OpenClose) {
  13. Displayer module(gname);
  14. ModuleParamSet params;
  15. // empty param
  16. EXPECT_FALSE(module.Open(params));
  17. params["window-width"] = "1920";
  18. params["window-height"] = "1080";
  19. params["refresh-rate"] = "22";
  20. params["max-channels"] = "16";
  21. params["show"] = "false";
  22. params["full-screen"] = "false";
  23. EXPECT_TRUE(module.CheckParamSet(params));
  24. EXPECT_TRUE(module.Open(params));
  25. module.Close();
  26. // invalid window-width and window-height
  27. params["window-width"] = "-1920";
  28. params["window-height"] = "-1080";
  29. params["refresh-rate"] = "22";
  30. params["max-channels"] = "16";
  31. params["show"] = "false";
  32. EXPECT_FALSE(module.CheckParamSet(params));
  33. EXPECT_FALSE(module.Open(params));
  34. // invalid full-screen and show
  35. params["full-screen"] = "aaa";
  36. params["show"] = "bbb";
  37. EXPECT_FALSE(module.CheckParamSet(params));
  38. params["full-screen"] = "false";
  39. params["show"] = "false";
  40. params["window-width"] = "1920";
  41. params["window-height"] = "1080";
  42. params["refresh-rate"] = "22";
  43. params["max-channels"] = "16";
  44. params["show"] = "false";
  45. EXPECT_TRUE(module.CheckParamSet(params));
  46. EXPECT_TRUE(module.Open(params));
  47. module.Close();
  48. }
  49. TEST(Display, Process) {
  50. std::shared_ptr<Displayer> display = std::make_shared<Displayer>(gname);
  51. int width = 1920;
  52. int height = 1080;
  53. ModuleParamSet params;
  54. params["window-width"] = std::to_string(width);
  55. params["window-height"] = std::to_string(height);
  56. params["refresh-rate"] = "22";
  57. params["max-channels"] = "16";
  58. params["show"] = "false";
  59. ASSERT_TRUE(display->Open(params));
  60. cv::Mat img(height, width, CV_8UC3, cv::Scalar(0, 127, 0));
  61. auto data = cnstream::CNFrameInfo::Create(std::to_string(0));
  62. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  63. data->SetStreamIndex(0);
  64. frame->frame_id = 1;
  65. data->timestamp = 1000;
  66. frame->width = width;
  67. frame->height = height;
  68. void* ptr_cpu[1] = {img.data};
  69. frame->stride[0] = width;
  70. frame->ctx.dev_type = DevContext::DevType::CPU;
  71. frame->fmt = CNDataFormat::CN_PIXEL_FORMAT_BGR24;
  72. frame->CopyToSyncMem(ptr_cpu, false);
  73. data->collection.Add(kCNDataFrameTag, frame);
  74. EXPECT_EQ(display->Process(data), 0);
  75. auto thread_loop = [&display]() { display->GUILoop(nullptr); };
  76. std::thread thread_ = std::thread(thread_loop);
  77. std::this_thread::sleep_for(std::chrono::milliseconds(300));
  78. display->Close();
  79. if (thread_.joinable()) {
  80. thread_.join();
  81. }
  82. }
  83. } // namespace cnstream