Skip to content

Commit

Permalink
* optimize rtmp test and rtsp test
Browse files Browse the repository at this point in the history
  • Loading branch information
lxowalle committed Aug 12, 2024
1 parent a1d032a commit 48085da
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
8 changes: 8 additions & 0 deletions components/vision/port/maixcam/maix_rtmp_maixcam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions components/vision/port/maixcam/maix_rtsp_maixcam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion examples/rtmp_demo/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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

Expand Down
22 changes: 21 additions & 1 deletion examples/rtsp_demo/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <width> <height> <format> <fps> <buff_num>");
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();
Expand Down

0 comments on commit 48085da

Please sign in to comment.