From 48085dae8092ef43958d6017d49920bf1f709247 Mon Sep 17 00:00:00 2001 From: lxowalle Date: Mon, 12 Aug 2024 14:36:12 +0800 Subject: [PATCH] * optimize rtmp test and rtsp test --- .../vision/port/maixcam/maix_rtmp_maixcam.cpp | 8 +++++++ .../vision/port/maixcam/maix_rtsp_maixcam.cpp | 7 ++++++ examples/rtmp_demo/main/src/main.cpp | 4 +++- examples/rtsp_demo/main/src/main.cpp | 22 ++++++++++++++++++- 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/components/vision/port/maixcam/maix_rtmp_maixcam.cpp b/components/vision/port/maixcam/maix_rtmp_maixcam.cpp index 25c51f98..97a363be 100644 --- a/components/vision/port/maixcam/maix_rtmp_maixcam.cpp +++ b/components/vision/port/maixcam/maix_rtmp_maixcam.cpp @@ -310,6 +310,9 @@ namespace maix::rtmp { uint32_t timestamp = 0; uint64_t curr_ms = time::ticks_ms(); uint64_t last_ms = curr_ms; + int fps = camera->fps(); + uint64_t cam_last_read_us = time::time_us(); + uint64_t cam_wait_us = 1000000 / fps; bool first_frame = 1; @@ -383,6 +386,11 @@ namespace maix::rtmp { break; } + while (time::ticks_us() - cam_last_read_us < cam_wait_us) { + time::sleep_us(50); + } + cam_last_read_us = time::ticks_us(); + curr_ms = time::ticks_ms(); timestamp = curr_ms - last_ms; diff --git a/components/vision/port/maixcam/maix_rtsp_maixcam.cpp b/components/vision/port/maixcam/maix_rtsp_maixcam.cpp index 77593347..e766bbde 100644 --- a/components/vision/port/maixcam/maix_rtsp_maixcam.cpp +++ b/components/vision/port/maixcam/maix_rtsp_maixcam.cpp @@ -167,6 +167,9 @@ namespace maix::rtsp void *data; int data_size, width, height, format; int vi_ch = 0, enc_ch = 1; + int fps = rtsp->to_camera()->fps(); + uint64_t wait_us = 1000000 / fps; + uint64_t last_us = time::time_us(); while (rtsp->rtsp_is_start()) { rtsp->update_timestamp(); uint64_t timestamp = rtsp->get_timestamp(); @@ -205,7 +208,11 @@ namespace maix::rtsp if (mmf_vi_frame_pop(vi_ch, &data, &data_size, &width, &height, &format)) { continue; } + while (time::ticks_us() - last_us < wait_us) { + time::sleep_us(50); + } + last_us = time::ticks_us(); if (mmf_enc_h265_push(enc_ch, (uint8_t *)data, width, height, format)) { log::warn("mmf_enc_h265_push failed\n"); continue; diff --git a/examples/rtmp_demo/main/src/main.cpp b/examples/rtmp_demo/main/src/main.cpp index 6bba9b12..f5ea4a36 100644 --- a/examples/rtmp_demo/main/src/main.cpp +++ b/examples/rtmp_demo/main/src/main.cpp @@ -76,7 +76,7 @@ int _main(int argc, char* argv[]) std::string stream = argv[5]; int bitrate = 1000 * 1000; if (argc > 6) bitrate = atoi(argv[6]); - printf("push rtmp://%s:%d/%s/%s!\r\n", &host[0], port, &app[0], &stream[0]); + camera::Camera cam = camera::Camera(1280, 720, image::Format::FMT_YVU420SP); display::Display disp = display::Display(); @@ -85,7 +85,9 @@ int _main(int argc, char* argv[]) rtmp.bind_camera(&cam); log::info("start\r\n"); + log::info("rtmp://%s:%d/%s/%s", &host[0], port, &app[0], &stream[0]); rtmp.start(); + while (!app::need_exit()) { // image::Image *img = rtmp.capture(); // not support now diff --git a/examples/rtsp_demo/main/src/main.cpp b/examples/rtsp_demo/main/src/main.cpp index 517661af..7ff8dd68 100644 --- a/examples/rtsp_demo/main/src/main.cpp +++ b/examples/rtsp_demo/main/src/main.cpp @@ -19,7 +19,27 @@ using namespace maix; int _main(int argc, char* argv[]) { int cnt = 0; - camera::Camera cam = camera::Camera(640, 480, image::Format::FMT_YVU420SP); + int cam_w = -1; + int cam_h = -1; + image::Format cam_fmt = image::Format::FMT_YVU420SP; + int cam_fps = -1; + int cam_buffer_num = 3; + if (argc > 1) { + if (!strcmp(argv[1], "-h")) { + log::info("./camera_display "); + log::info("example: ./camera_display 640 480 0 60 2"); + exit(0); + } else { + cam_w = atoi(argv[1]); + } + } + if (argc > 2) cam_h = atoi(argv[2]); + if (argc > 3) cam_fmt = (image::Format)atoi(argv[3]); + if (argc > 4) cam_fps = atoi(argv[4]); + if (argc > 5) cam_buffer_num = atoi(argv[5]); + log::info("Camera width:%d height:%d format:%s fps:%d buffer_num:%d", cam_w, cam_h, image::fmt_names[cam_fmt].c_str(), cam_fps, cam_buffer_num); + + camera::Camera cam = camera::Camera(cam_w, cam_h, cam_fmt, "", cam_fps, cam_buffer_num); camera::Camera *cam2 = cam.add_channel(640, 480); display::Display disp = display::Display(); rtsp::Rtsp rtsp = rtsp::Rtsp();