diff --git a/README.md b/README.md index 58a2cea7..cfb156b8 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ ros2 run usb_cam usb_cam_node_exe --ros-args --params-file /path/to/colcon_ws/sr # launch the usb_cam executable that loads parameters from the same `usb_cam/config/params.yaml` file as above # along with an additional image viewer node -ros2 launch usb_cam demo_launch.py +ros2 launch usb_cam camera.launch.py ``` ## Launching Multiple usb_cam's diff --git a/include/usb_cam/formats/mjpeg.hpp b/include/usb_cam/formats/mjpeg.hpp index 64c22369..86c0308a 100644 --- a/include/usb_cam/formats/mjpeg.hpp +++ b/include/usb_cam/formats/mjpeg.hpp @@ -75,7 +75,6 @@ class MJPEG2RGB : public pixel_format_base m_avframe_device(av_frame_alloc()), m_avframe_rgb(av_frame_alloc()), m_avoptions(NULL), - m_avpacket(av_packet_alloc()), m_averror_str(reinterpret_cast(malloc(AV_ERROR_MAX_STRING_SIZE))) { if (!m_avcodec) { @@ -85,9 +84,6 @@ class MJPEG2RGB : public pixel_format_base if (!m_avparser) { throw std::runtime_error("Could not find MJPEG parser"); } - if (!m_avpacket) { - throw std::runtime_error("Could not allocate AVPacket"); - } m_avcodec_context = avcodec_alloc_context3(m_avcodec); @@ -145,6 +141,12 @@ class MJPEG2RGB : public pixel_format_base ~MJPEG2RGB() { + if (m_averror_str) { + free(m_averror_str); + } + if (m_avoptions) { + free(m_avoptions); + } if (m_avcodec_context) { avcodec_close(m_avcodec_context); avcodec_free_context(&m_avcodec_context); @@ -155,10 +157,6 @@ class MJPEG2RGB : public pixel_format_base if (m_avframe_rgb) { av_frame_free(&m_avframe_rgb); } - if (m_avpacket) { - av_packet_unref(m_avpacket); - av_packet_free(&m_avpacket); - } if (m_avparser) { av_parser_close(m_avparser); } @@ -174,24 +172,20 @@ class MJPEG2RGB : public pixel_format_base // clear the picture memset(dest, 0, m_avframe_device_size); - #if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100) - // deprecated: https://github.com/FFmpeg/FFmpeg/commit/f7db77bd8785d1715d3e7ed7e69bd1cc991f2d07 - av_init_packet(m_avpacket); - #endif - - av_packet_from_data( - m_avpacket, - const_cast(reinterpret_cast(src)), - bytes_used); + auto avpacket = av_packet_alloc(); + av_new_packet(avpacket, bytes_used); + memcpy(avpacket->data, src, bytes_used); // Pass src MJPEG image to decoder - m_result = avcodec_send_packet(m_avcodec_context, m_avpacket); + m_result = avcodec_send_packet(m_avcodec_context, avpacket); + + av_packet_free(&avpacket); - av_packet_unref(m_avpacket); // If result is not 0, report what went wrong if (m_result != 0) { std::cerr << "Failed to send AVPacket to decode: "; print_av_error_string(m_result); + return; } m_result = avcodec_receive_frame(m_avcodec_context, m_avframe_device); @@ -201,6 +195,7 @@ class MJPEG2RGB : public pixel_format_base } else if (m_result < 0) { std::cerr << "Failed to recieve decoded frame from codec: "; print_av_error_string(m_result); + return; } sws_scale( @@ -228,7 +223,6 @@ class MJPEG2RGB : public pixel_format_base AVFrame * m_avframe_device; AVFrame * m_avframe_rgb; AVDictionary * m_avoptions; - AVPacket * m_avpacket; SwsContext * m_sws_context; size_t m_avframe_device_size; size_t m_avframe_rgb_size; diff --git a/src/usb_cam_node.cpp b/src/usb_cam_node.cpp index e46064b0..585888d3 100644 --- a/src/usb_cam_node.cpp +++ b/src/usb_cam_node.cpp @@ -362,13 +362,10 @@ void UsbCamNode::update() { if (m_camera->is_capturing()) { // If the camera exposure longer higher than the framerate period - // then that caps the framerate. - // auto t0 = now(); + // then that caps the framerate if (!take_and_send_image()) { RCLCPP_WARN_ONCE(this->get_logger(), "USB camera did not respond in time."); } - // auto diff = now() - t0; - // INFO(diff.nanoseconds() / 1e6 << " " << int(t0.nanoseconds() / 1e9)); } } } // namespace usb_cam