-
Notifications
You must be signed in to change notification settings - Fork 597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix raw_mjpeg function and shrink the compressed image msg size #336
base: ros2
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -171,7 +171,7 @@ void UsbCamNode::init() | |||||||
|
||||||||
// if pixel format is equal to 'mjpeg', i.e. raw mjpeg stream, initialize compressed image message | ||||||||
// and publisher | ||||||||
if (m_parameters.pixel_format_name == "mjpeg") { | ||||||||
if (m_parameters.pixel_format_name == "raw_mjpeg") { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hilookas We should support both mjpeg and raw_mjpeg for compressed image streams I think, no?
Suggested change
|
||||||||
m_compressed_img_msg.reset(new sensor_msgs::msg::CompressedImage()); | ||||||||
m_compressed_img_msg->header.frame_id = m_parameters.frame_id; | ||||||||
m_compressed_image_publisher = | ||||||||
|
@@ -391,6 +391,8 @@ bool UsbCamNode::take_and_send_image_mjpeg() | |||||||
|
||||||||
// grab the image, pass image msg buffer to fill | ||||||||
m_camera->get_image(reinterpret_cast<char *>(&m_compressed_img_msg->data[0])); | ||||||||
m_compressed_img_msg->data.resize(m_camera->get_image_size_in_bytes()); | ||||||||
m_camera->reset_image_size_in_bytes(); // reset for next grab | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hilookas I dont think this does what you think it does 😅 the |
||||||||
|
||||||||
auto stamp = m_camera->get_image_timestamp(); | ||||||||
m_compressed_img_msg->header.stamp.sec = stamp.tv_sec; | ||||||||
|
@@ -423,7 +425,7 @@ void UsbCamNode::update() | |||||||
// If the camera exposure longer higher than the framerate period | ||||||||
// then that caps the framerate. | ||||||||
// auto t0 = now(); | ||||||||
bool isSuccessful = (m_parameters.pixel_format_name == "mjpeg") ? | ||||||||
bool isSuccessful = (m_parameters.pixel_format_name == "raw_mjpeg") ? | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hilookas should probably support both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To my understand, mode mjpeg and mode raw_mjpeg is different. mjpeg mode should decode the mjpeg image from driver, and send it to m_image topic, which will be compressed again to /compressed by image_transport raw_mjpeg should send jpeg encoded image from driver and send it directly to /compressed, m_image topic should keep empty preventing cpu-consuming jpeg decoding. This mode should be optimized to send compressed image only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And infact there is no "mjpeg" mode right now for ros2 |
||||||||
take_and_send_image_mjpeg() : | ||||||||
take_and_send_image(); | ||||||||
if (!isSuccessful) { | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,7 +82,8 @@ void UsbCam::process_image(const char * src, char * & dest, const int & bytes_us | |
// TODO(flynneva): could we skip the copy here somehow? | ||
// If no conversion required, just copy the image from V4L2 buffer | ||
if (m_image.pixel_format->requires_conversion() == false) { | ||
memcpy(dest, src, m_image.size_in_bytes); | ||
m_image.size_in_bytes = bytes_used; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hilookas why are you setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's because the length of the mjpeg encoded image fetched from v4l2 driver is changed between different frames. We need record current length of the frame, and during the topic sending process, we need use the corrent length of the frame. Incorrect length will cause the memory leaking issue. Hence, memcpy should use bytes_used (which is from v4l2 driver) rather than m_image.size_in_bytes |
||
memcpy(dest, src, bytes_used); | ||
} else { | ||
m_image.pixel_format->convert(src, dest, bytes_used); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hilookas this method should be added to the
image_t
structure, where inside it should set either the height or bytes_per_line variable to 0 and callset_size_in_bytes()
.