Skip to content

Commit 51cb5f1

Browse files
authored
Use chrono steady clock for frame timing (backport #173) (#174)
* Use chrono steady_clock for frame timing * Increase cmake minimum version
1 parent 70aa532 commit 51cb5f1

15 files changed

+94
-65
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 2.8.3)
1+
cmake_minimum_required(VERSION 3.10.2)
22
project(web_video_server)
33

44
## Find catkin macros and libraries

include/web_video_server/image_streamer.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef IMAGE_STREAMER_H_
22
#define IMAGE_STREAMER_H_
33

4+
#include <chrono>
5+
46
#include <ros/ros.h>
57
#include <image_transport/image_transport.h>
68
#include <opencv2/opencv.hpp>
@@ -29,7 +31,7 @@ class ImageStreamer
2931
/**
3032
* Restreams the last received image frame if older than max_age.
3133
*/
32-
virtual void restreamFrame(double max_age) = 0;
34+
virtual void restreamFrame(std::chrono::duration<double> max_age) = 0;
3335

3436
std::string getTopic()
3537
{
@@ -56,8 +58,8 @@ class ImageTransportImageStreamer : public ImageStreamer
5658

5759
protected:
5860
virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg);
59-
virtual void sendImage(const cv::Mat &, const ros::Time &time) = 0;
60-
virtual void restreamFrame(double max_age);
61+
virtual void sendImage(const cv::Mat &, const std::chrono::steady_clock::time_point &time) = 0;
62+
virtual void restreamFrame(std::chrono::duration<double> max_age);
6163
virtual void initialize(const cv::Mat &);
6264

6365
image_transport::Subscriber image_sub_;
@@ -66,7 +68,7 @@ class ImageTransportImageStreamer : public ImageStreamer
6668
bool invert_;
6769
std::string default_transport_;
6870

69-
ros::Time last_frame;
71+
std::chrono::steady_clock::time_point last_frame_;
7072
cv::Mat output_size_image;
7173
boost::mutex send_mutex_;
7274

include/web_video_server/jpeg_streamers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class MjpegStreamer : public ImageTransportImageStreamer
1818
ros::NodeHandle& nh);
1919
~MjpegStreamer();
2020
protected:
21-
virtual void sendImage(const cv::Mat &, const ros::Time &time);
21+
virtual void sendImage(const cv::Mat &, const std::chrono::steady_clock::time_point &time);
2222

2323
private:
2424
MultipartStream stream_;
@@ -41,7 +41,7 @@ class JpegSnapshotStreamer : public ImageTransportImageStreamer
4141
async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle& nh);
4242
~JpegSnapshotStreamer();
4343
protected:
44-
virtual void sendImage(const cv::Mat &, const ros::Time &time);
44+
virtual void sendImage(const cv::Mat &, const std::chrono::steady_clock::time_point &time);
4545

4646
private:
4747
int quality_;

include/web_video_server/libav_streamer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef LIBAV_STREAMERS_H_
22
#define LIBAV_STREAMERS_H_
33

4+
#include <chrono>
5+
46
#include <image_transport/image_transport.h>
57
#include "web_video_server/image_streamer.h"
68
#include "async_web_server_cpp/http_request.hpp"
@@ -32,7 +34,7 @@ class LibavStreamer : public ImageTransportImageStreamer
3234

3335
protected:
3436
virtual void initializeEncoder();
35-
virtual void sendImage(const cv::Mat&, const ros::Time& time);
37+
virtual void sendImage(const cv::Mat&, const std::chrono::steady_clock::time_point& time);
3638
virtual void initialize(const cv::Mat&);
3739
AVOutputFormat* output_format_;
3840
AVFormatContext* format_context_;
@@ -45,8 +47,9 @@ class LibavStreamer : public ImageTransportImageStreamer
4547
private:
4648
AVFrame* frame_;
4749
struct SwsContext* sws_context_;
48-
ros::Time first_image_timestamp_;
4950
boost::mutex encode_mutex_;
51+
bool first_image_received_;
52+
std::chrono::steady_clock::time_point first_image_time_;
5053

5154
std::string format_name_;
5255
std::string codec_name_;

include/web_video_server/multipart_stream.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace web_video_server
1010
{
1111

1212
struct PendingFooter {
13-
ros::Time timestamp;
13+
std::chrono::steady_clock::time_point timestamp;
1414
boost::weak_ptr<std::string> contents;
1515
};
1616

@@ -21,10 +21,10 @@ class MultipartStream {
2121
std::size_t max_queue_size=1);
2222

2323
void sendInitialHeader();
24-
void sendPartHeader(const ros::Time &time, const std::string& type, size_t payload_size);
25-
void sendPartFooter(const ros::Time &time);
26-
void sendPartAndClear(const ros::Time &time, const std::string& type, std::vector<unsigned char> &data);
27-
void sendPart(const ros::Time &time, const std::string& type, const boost::asio::const_buffer &buffer,
24+
void sendPartHeader(const std::chrono::steady_clock::time_point &time, const std::string& type, size_t payload_size);
25+
void sendPartFooter(const std::chrono::steady_clock::time_point &time);
26+
void sendPartAndClear(const std::chrono::steady_clock::time_point &time, const std::string& type, std::vector<unsigned char> &data);
27+
void sendPart(const std::chrono::steady_clock::time_point &time, const std::string& type, const boost::asio::const_buffer &buffer,
2828
async_web_server_cpp::HttpConnection::ResourcePtr resource);
2929

3030
private:

include/web_video_server/png_streamers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class PngStreamer : public ImageTransportImageStreamer
1818
ros::NodeHandle& nh);
1919
~PngStreamer();
2020
protected:
21-
virtual void sendImage(const cv::Mat &, const ros::Time &time);
21+
virtual void sendImage(const cv::Mat &, const std::chrono::steady_clock::time_point &time);
2222
virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg);
2323

2424
private:
@@ -42,7 +42,7 @@ class PngSnapshotStreamer : public ImageTransportImageStreamer
4242
async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle& nh);
4343
~PngSnapshotStreamer();
4444
protected:
45-
virtual void sendImage(const cv::Mat &, const ros::Time &time);
45+
virtual void sendImage(const cv::Mat &, const std::chrono::steady_clock::time_point &time);
4646
virtual cv::Mat decodeImage(const sensor_msgs::ImageConstPtr& msg);
4747

4848
private:

include/web_video_server/ros_compressed_streamer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ class RosCompressedStreamer : public ImageStreamer
1818
~RosCompressedStreamer();
1919

2020
virtual void start();
21-
virtual void restreamFrame(double max_age);
21+
virtual void restreamFrame(std::chrono::duration<double> max_age);
2222

2323
protected:
24-
virtual void sendImage(const sensor_msgs::CompressedImageConstPtr &msg, const ros::Time &time);
24+
virtual void sendImage(const sensor_msgs::CompressedImageConstPtr &msg, const std::chrono::steady_clock::time_point &time);
2525

2626
private:
2727
void imageCallback(const sensor_msgs::CompressedImageConstPtr &msg);
2828
MultipartStream stream_;
2929
ros::Subscriber image_sub_;
30-
ros::Time last_frame;
30+
std::chrono::steady_clock::time_point last_frame_;
3131
sensor_msgs::CompressedImageConstPtr last_msg;
3232
boost::mutex send_mutex_;
3333
};

include/web_video_server/web_video_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class WebVideoServer
5050
async_web_server_cpp::HttpConnectionPtr connection, const char* begin, const char* end);
5151

5252
private:
53-
void restreamFrames(double max_age);
53+
void restreamFrames(std::chrono::duration<double> max_age);
5454
void cleanup_inactive_streams();
5555

5656
ros::NodeHandle nh_;

src/image_streamer.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ void ImageTransportImageStreamer::initialize(const cv::Mat &)
5050
{
5151
}
5252

53-
void ImageTransportImageStreamer::restreamFrame(double max_age)
53+
void ImageTransportImageStreamer::restreamFrame(std::chrono::duration<double> max_age)
5454
{
5555
if (inactive_ || !initialized_ )
5656
return;
5757
try {
58-
if ( last_frame + ros::Duration(max_age) < ros::Time::now() ) {
58+
if (last_frame_ + max_age < std::chrono::steady_clock::now()) {
5959
boost::mutex::scoped_lock lock(send_mutex_);
60-
sendImage(output_size_image, ros::Time::now() ); // don't update last_frame, it may remain an old value.
60+
// don't update last_frame, it may remain an old value.
61+
sendImage(output_size_image, std::chrono::steady_clock::now());
6162
}
6263
}
6364
catch (boost::system::system_error &e)
@@ -148,8 +149,8 @@ void ImageTransportImageStreamer::imageCallback(const sensor_msgs::ImageConstPtr
148149
initialized_ = true;
149150
}
150151

151-
last_frame = ros::Time::now();
152-
sendImage(output_size_image, msg->header.stamp);
152+
last_frame_ = std::chrono::steady_clock::now();
153+
sendImage(output_size_image, last_frame_);
153154
}
154155
catch (cv_bridge::Exception &e)
155156
{

src/jpeg_streamers.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ MjpegStreamer::~MjpegStreamer()
1818
boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
1919
}
2020

21-
void MjpegStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
21+
void MjpegStreamer::sendImage(
22+
const cv::Mat & img,
23+
const std::chrono::steady_clock::time_point & time)
2224
{
2325
std::vector<int> encode_params;
2426
#if CV_VERSION_MAJOR >= 3
@@ -64,7 +66,9 @@ JpegSnapshotStreamer::~JpegSnapshotStreamer()
6466
boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
6567
}
6668

67-
void JpegSnapshotStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
69+
void JpegSnapshotStreamer::sendImage(
70+
const cv::Mat & img,
71+
const std::chrono::steady_clock::time_point & time)
6872
{
6973
std::vector<int> encode_params;
7074
#if CV_VERSION_MAJOR >= 3
@@ -78,7 +82,8 @@ void JpegSnapshotStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
7882
cv::imencode(".jpeg", img, encoded_buffer, encode_params);
7983

8084
char stamp[20];
81-
sprintf(stamp, "%.06lf", time.toSec());
85+
snprintf(stamp, sizeof(stamp), "%.06lf",
86+
std::chrono::duration_cast<std::chrono::duration<double>>(time.time_since_epoch()).count());
8287
async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok)
8388
.header("Connection", "close")
8489
.header("Server", "web_video_server")

0 commit comments

Comments
 (0)