@@ -44,9 +44,9 @@ LibavStreamer::LibavStreamer(
4444 const std::string & format_name, const std::string & codec_name,
4545 const std::string & content_type)
4646: ImageTransportImageStreamer(request, connection, node), format_context_(0 ), codec_(0 ),
47- codec_context_ (0 ), video_stream_(0 ), frame_(0 ), sws_context_(0 ), first_image_timestamp_( 0 ),
48- format_name_(format_name ), codec_name_(codec_name ), content_type_(content_type), opt_( 0 ),
49- io_buffer_(0 )
47+ codec_context_ (0 ), video_stream_(0 ), frame_(0 ), sws_context_(0 ),
48+ first_image_received_( false ), first_image_time_( ), format_name_(format_name ),
49+ codec_name_(codec_name), content_type_(content_type), opt_( 0 ), io_buffer_(0 )
5050{
5151 bitrate_ = request.get_query_param_value_or_default <int >(" bitrate" , 100000 );
5252 qmin_ = request.get_query_param_value_or_default <int >(" qmin" , 10 );
@@ -215,11 +215,14 @@ void LibavStreamer::initializeEncoder()
215215{
216216}
217217
218- void LibavStreamer::sendImage (const cv::Mat & img, const rclcpp::Time & time)
218+ void LibavStreamer::sendImage (
219+ const cv::Mat & img,
220+ const std::chrono::steady_clock::time_point & time)
219221{
220222 std::scoped_lock lock (encode_mutex_);
221- if (0 == first_image_timestamp_.nanoseconds ()) {
222- first_image_timestamp_ = time;
223+ if (!first_image_received_) {
224+ first_image_received_ = true ;
225+ first_image_time_ = time;
223226 }
224227
225228 AVPixelFormat input_coding_format = AV_PIX_FMT_BGR24;
@@ -274,7 +277,8 @@ void LibavStreamer::sendImage(const cv::Mat & img, const rclcpp::Time & time)
274277 std::size_t size;
275278 uint8_t * output_buf;
276279
277- double seconds = (time - first_image_timestamp_).seconds ();
280+ double seconds = std::chrono::duration_cast<std::chrono::duration<double >>(
281+ time - first_image_time_).count ();
278282 // Encode video at 1/0.95 to minimize delay
279283 pkt->pts = (int64_t )(seconds / av_q2d (video_stream_->time_base ) * 0.95 );
280284 if (pkt->pts <= 0 ) {
0 commit comments