data_handler_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import os, sys
  2. import numpy as np
  3. sys.path.append(os.path.split(os.path.realpath(__file__))[0] + "/../lib")
  4. from cnstream import *
  5. from cnstream_cpptest import *
  6. import time
  7. import cv2
  8. g_cur_dir = os.path.dirname(os.path.realpath(__file__))
  9. class TestSourceHandler():
  10. def test_data_source(self):
  11. data_source = DataSource("test_source")
  12. params = {"output_type" : "abc", "decoder_type" : "mlu"}
  13. assert False == data_source.check_param_set(params)
  14. params = {"output_type" : "cpu", "device_id" : "0", "decoder_type" : "mlu"}
  15. assert True == data_source.open(params)
  16. assert OutputType.output_cpu == data_source.get_source_param().output_type
  17. assert DecoderType.decoder_mlu == data_source.get_source_param().decoder_type
  18. def test_file_handler(self):
  19. data_source = DataSource("test_source")
  20. params = {"output_type" : "mlu", "device_id" : "0", "decoder_type" : "mlu"}
  21. assert data_source.open(params)
  22. cpp_test_helper = CppDataHanlderTestHelper()
  23. cpp_test_helper.set_observer(data_source)
  24. mp4_path = "../../modules/unitest/source/data/img.mp4"
  25. file_handler = FileHandler(data_source, "stream_id_0", mp4_path, 30)
  26. assert file_handler.open()
  27. assert "stream_id_0" == file_handler.get_stream_id()
  28. data = file_handler.create_frame_info()
  29. assert file_handler.send_data(data)
  30. file_handler.close()
  31. assert 0 == data_source.add_source(file_handler)
  32. assert file_handler == data_source.get_source_handler("stream_id_0")
  33. time.sleep(3)
  34. assert 7 == cpp_test_helper.get_count("stream_id_0")
  35. assert 0 == data_source.remove_source("stream_id_0")
  36. assert None == data_source.get_source_handler("stream_id_0")
  37. def test_rtsp_handler(self):
  38. data_source = DataSource("test_source")
  39. params = {"output_type" : "mlu", "device_id" : "0", "decoder_type" : "mlu"}
  40. assert data_source.open(params)
  41. cpp_test_helper = CppDataHanlderTestHelper()
  42. cpp_test_helper.set_observer(data_source)
  43. url = "rtsp://admin:abcd@123@192.168.80.47:554/h264/ch1/main/av_stream"
  44. rtsp_handler = RtspHandler(data_source, "stream_id_1", url)
  45. assert "stream_id_1" == rtsp_handler.get_stream_id()
  46. assert rtsp_handler.open()
  47. time.sleep(1)
  48. data = rtsp_handler.create_frame_info()
  49. assert rtsp_handler.send_data(data)
  50. rtsp_handler.close()
  51. assert 0 == data_source.add_source(rtsp_handler)
  52. time.sleep(1)
  53. assert rtsp_handler == data_source.get_source_handler("stream_id_1")
  54. assert 0 == data_source.remove_source("stream_id_1")
  55. assert None == data_source.get_source_handler("stream_id_1")
  56. def test_raw_img_mem_handler(self):
  57. data_source = DataSource("test_source")
  58. params = {"output_type" : "mlu", "device_id" : "0", "decoder_type" : "mlu"}
  59. assert data_source.open(params)
  60. cpp_test_helper = CppDataHanlderTestHelper()
  61. cpp_test_helper.set_observer(data_source)
  62. file_handler = RawImgMemHandler(data_source, "stream_id_0")
  63. assert file_handler.open()
  64. assert "stream_id_0" == file_handler.get_stream_id()
  65. assert 0 == data_source.add_source(file_handler)
  66. assert file_handler == data_source.get_source_handler("stream_id_0")
  67. # bgr24/rgb24 height = 720; width = 1280; channel = 3
  68. data = np.ones([720, 1280, 3], dtype=np.uint8)
  69. assert 0 == file_handler.write(data, 0, CNDataFormat.CN_PIXEL_FORMAT_BGR24)
  70. # yuv nv12/nv21 height = 720; width = 1280; channel = 1
  71. data_nv21 = np.ones([720 * 3 // 2, 1280], dtype=np.uint8)
  72. assert 0 == file_handler.write(data_nv21, 0, CNDataFormat.CN_PIXEL_FORMAT_YUV420_NV21)
  73. # read cv mat
  74. img_path = g_cur_dir + "/data/test_img_0.jpg"
  75. img = cv2.imread(img_path)
  76. assert 0 == file_handler.write(img, 0)
  77. assert 3 == cpp_test_helper.get_count("stream_id_0")
  78. file_handler.close()
  79. data_source.close()