test_rtsp_sink.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*************************************************************************
  2. * Copyright (C) [2020] 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 <ifaddrs.h>
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include <libavcodec/avcodec.h>
  25. #include <libavformat/avformat.h>
  26. #include <libavutil/avutil.h>
  27. #ifdef __cplusplus
  28. }
  29. #endif
  30. #include <opencv2/highgui/highgui.hpp>
  31. #include <opencv2/imgproc/imgproc.hpp>
  32. #if (CV_MAJOR_VERSION >= 3)
  33. #include <opencv2/imgcodecs/imgcodecs.hpp>
  34. #endif
  35. #include <cstdlib>
  36. #include <ctime>
  37. #include <memory>
  38. #include <string>
  39. #include <utility>
  40. #include <vector>
  41. #include <future>
  42. #include "gtest/gtest.h"
  43. #include "cnstream_frame_va.hpp"
  44. #include "easyinfer/mlu_memory_op.h"
  45. #include "rtsp_sink.hpp"
  46. #include "rtsp_server.hpp"
  47. #include "test_base.hpp"
  48. namespace cnstream {
  49. static constexpr const char *gname = "rstp_sink";
  50. static constexpr int g_device_id = 0;
  51. static constexpr int g_dev_id = 0;
  52. static constexpr int g_width = 1280;
  53. static constexpr int g_height = 720;
  54. extern void TestAllCase(ModuleParamSet params, int frame_rate, bool tiler, int line);
  55. extern bool PullRtspStreamOpencv(int port = 9554);
  56. extern bool PullRtspStreamFFmpeg(int port = 9554);
  57. extern std::shared_ptr<CNFrameInfo> GenTestData(CNPixelFormat pix_fmt, int width, int height);
  58. TEST(RtspModule, OpenClose) {
  59. RtspSink module(gname);
  60. ModuleParamSet params;
  61. EXPECT_FALSE(module.Open(params));
  62. params["port"] = "9554";
  63. params["rtsp_over_http"] = "false";
  64. params["frame_rate"] = "25";
  65. params["bit_rate"] = "4000000";
  66. params["gop_size"] = "10";
  67. params["view_cols"] = "1";
  68. params["view_rows"] = "1";
  69. params["resample"] = "false";
  70. EXPECT_TRUE(module.Open(params));
  71. params["device_id"] = "0";
  72. params["encoder_type"] = "mlu";
  73. params["input_frame"] = "cpu";
  74. EXPECT_TRUE(module.Open(params));
  75. params["dst_width"] = "1280";
  76. params["dst_height"] = "720";
  77. EXPECT_TRUE(module.Open(params));
  78. params["input_frame"] = "mlu";
  79. params["encoder_type"] = "mlu";
  80. params["device_id"] = "-1";
  81. EXPECT_FALSE(module.Open(params));
  82. params["encoder_type"] = "abc";
  83. EXPECT_FALSE(module.Open(params));
  84. module.Close();
  85. }
  86. TEST(RtspModule, Process) {
  87. // create rtsp_sink
  88. std::shared_ptr<RtspSink> ptr = std::make_shared<RtspSink>(gname);
  89. ModuleParamSet params;
  90. int frame_rate = 25;
  91. std::string device_id = "-1";
  92. params["port"] = "9554";
  93. params["encoder_type"] = "cpu";
  94. params["input_frame"] = "cpu";
  95. params["device_id"] = device_id;
  96. int col = 3;
  97. int row = 2;
  98. params["view_cols"] = std::to_string(col);
  99. params["view_rows"] = std::to_string(row);
  100. params["frame_rate"] = std::to_string(frame_rate);
  101. TestAllCase(params, frame_rate, true, __LINE__);
  102. EXPECT_TRUE(ptr->Open(params));
  103. for (int i = 0; i < col * row; i++) {
  104. auto data = cnstream::CNFrameInfo::Create(std::to_string(i));
  105. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  106. data->collection.Add(kCNDataFrameTag, frame);
  107. EXPECT_EQ(ptr->Process(data), -1);
  108. data = cnstream::CNFrameInfo::Create(std::to_string(i), true);
  109. }
  110. auto fut = std::async(std::launch::async, PullRtspStreamFFmpeg, 9445);
  111. auto data = cnstream::CNFrameInfo::Create(std::to_string(col * row + 1));
  112. std::shared_ptr<CNDataFrame> frame(new (std::nothrow) CNDataFrame());
  113. data->collection.Add(kCNDataFrameTag, frame);
  114. EXPECT_EQ(ptr->Process(data), -1);
  115. data = cnstream::CNFrameInfo::Create(std::to_string(col * row + 1), true);
  116. fut.get();
  117. ptr->Close();
  118. }
  119. } // namespace cnstream