Skip to content
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 usage of deprecated libavcodec functions #150

Merged
merged 9 commits into from
Oct 2, 2024
Merged

Conversation

bjsowa
Copy link
Collaborator

@bjsowa bjsowa commented Sep 30, 2024

Fixes web_video_server for Jazzy and Rolling distributions.

I also dropped backward compatibility with old ffmpeg libraries to make the code easier to maintain.

@bjsowa
Copy link
Collaborator Author

bjsowa commented Sep 30, 2024

@sea-bass This seems to work on Humble and Ubuntu 22.04 but Segfaults on Rolling on Ubuntu 24.04 when trying to use vp8, vp9 or h264 compression (mjpeg streaming works). Backtrace shows the seg fault occurs in avformat_write_header function. I want to fix this issue before merging this PR. Any help would be apprieciated.

@bjsowa
Copy link
Collaborator Author

bjsowa commented Oct 1, 2024

I think I've got this finally working. Checked on Humble, Iron, Jazzy and Rolling. I can't get H264 to work (it never worked for me) but all of the other stream types (mjpeg, png, ros_compressed, vp8, vp9) are working.

@bjsowa bjsowa requested a review from sea-bass October 1, 2024 15:03
@bjsowa bjsowa marked this pull request as ready for review October 1, 2024 15:03
@bjsowa
Copy link
Collaborator Author

bjsowa commented Oct 1, 2024

av_lockmgr_register has been deprecated and does nothing since ffmpeg 4.0 (Ubuntu 20.04 uses ffmpeg 4.2.7) so I think we can safely remove it.

@bjsowa bjsowa requested a review from EzraBrooks October 1, 2024 16:03
Copy link

@EzraBrooks EzraBrooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this contribution! You got to it before I was able to get my project's Jazzy upgrade underway :)

I will largely defer to @sea-bass here because I am not as well-versed in C++ as he is, but the changes seem sensical to me.

Copy link
Contributor

@sea-bass sea-bass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to ask to update any READMEs/documentation to mention that h264 is removed... but that would assume the existence of that 😆

Changes LGTM, and only have minor optional suggestions. Thank you for this!

Comment on lines +226 to +246
RCLCPP_DEBUG_STREAM(nh_->get_logger(), "avcodec_send_frame() encoder flushed\n");
}
#else
pkt.data = NULL; // packet data will be allocated by the encoder
pkt.size = 0;
if (avcodec_send_frame(codec_context_, frame_) < 0)
else if (ret == AVERROR(EAGAIN))
{
RCLCPP_DEBUG_STREAM(nh_->get_logger(), "avcodec_send_frame() need output read out\n");
}
if (ret < 0)
{
throw std::runtime_error("Error encoding video frame");
}
if (avcodec_receive_packet(codec_context_, &pkt) < 0)

ret = avcodec_receive_packet(codec_context_, pkt);
bool got_packet = pkt->size > 0;
if (ret == AVERROR_EOF)
{
RCLCPP_DEBUG_STREAM(nh_->get_logger(), "avcodec_receive_packet() encoder flushed\n");
}
else if (ret == AVERROR(EAGAIN))
{
throw std::runtime_error("Error retrieving encoded packet");
RCLCPP_DEBUG_STREAM(nh_->get_logger(), "avcodec_receive_packet() needs more input\n");
got_packet = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to keep these debug logs before merging, or did you intend to remove them?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug logs were backported from #103 . I don't see any reason to remove them.

Comment on lines 255 to +256
// Encode video at 1/0.95 to minimize delay
pkt.pts = (int64_t)(seconds / av_q2d(video_stream_->time_base) * 0.95);
if (pkt.pts <= 0)
pkt.pts = 1;
pkt.dts = AV_NOPTS_VALUE;
pkt->pts = (int64_t)(seconds / av_q2d(video_stream_->time_base) * 0.95);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This magic number of 0.95 would be nice to have as an actual constant defined in an anonymous namespace at the top of this file.

Copy link
Collaborator Author

@bjsowa bjsowa Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is out of scope for this PR. Instead of making it a constant I might add URL-controlled parameter for this (e.g. playback_speed) in the future.

@bjsowa bjsowa merged commit 1f6a5b7 into ros2 Oct 2, 2024
8 checks passed
@bjsowa bjsowa deleted the fix-deprecated-avcodec branch October 2, 2024 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants