From 0755df6f2d0e0ecdf689a35b0f11c07e35778024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Sowa?= Date: Tue, 19 Nov 2024 21:24:54 +0000 Subject: [PATCH] Don't use std::optional --- include/web_video_server/libav_streamer.hpp | 5 +++-- src/libav_streamer.cpp | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/web_video_server/libav_streamer.hpp b/include/web_video_server/libav_streamer.hpp index f267344..12914bb 100644 --- a/include/web_video_server/libav_streamer.hpp +++ b/include/web_video_server/libav_streamer.hpp @@ -42,8 +42,8 @@ extern "C" #include } +#include #include -#include #include #include "image_transport/image_transport.hpp" @@ -79,8 +79,9 @@ class LibavStreamer : public ImageTransportImageStreamer private: AVFrame * frame_; struct SwsContext * sws_context_; - std::optional first_image_timestamp_; std::mutex encode_mutex_; + bool first_image_received_; + std::chrono::steady_clock::time_point first_image_time_; std::string format_name_; std::string codec_name_; diff --git a/src/libav_streamer.cpp b/src/libav_streamer.cpp index d5c00b9..4814c4e 100644 --- a/src/libav_streamer.cpp +++ b/src/libav_streamer.cpp @@ -45,8 +45,8 @@ LibavStreamer::LibavStreamer( const std::string & content_type) : ImageTransportImageStreamer(request, connection, node), format_context_(0), codec_(0), codec_context_(0), video_stream_(0), frame_(0), sws_context_(0), - first_image_timestamp_(std::nullopt), format_name_(format_name), codec_name_(codec_name), - content_type_(content_type), opt_(0), io_buffer_(0) + first_image_received_(false), first_image_time_(), format_name_(format_name), + codec_name_(codec_name), content_type_(content_type), opt_(0), io_buffer_(0) { bitrate_ = request.get_query_param_value_or_default("bitrate", 100000); qmin_ = request.get_query_param_value_or_default("qmin", 10); @@ -220,8 +220,9 @@ void LibavStreamer::sendImage( const std::chrono::steady_clock::time_point & time) { std::scoped_lock lock(encode_mutex_); - if (!first_image_timestamp_.has_value()) { - first_image_timestamp_ = time; + if (!first_image_received_) { + first_image_received_ = true; + first_image_time_ = time; } AVPixelFormat input_coding_format = AV_PIX_FMT_BGR24; @@ -277,7 +278,7 @@ void LibavStreamer::sendImage( uint8_t * output_buf; double seconds = std::chrono::duration_cast>( - time - first_image_timestamp_.value()).count(); + time - first_image_time_).count(); // 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) {