ros_compressed_streamer.cpp
Go to the documentation of this file.
2 
3 namespace web_video_server
4 {
5 
8  ImageStreamer(request, connection, nh), stream_(connection)
9 {
11 }
12 
14 {
15  this->inactive_ = true;
16  boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
17 }
18 
20  std::string compressed_topic = topic_ + "/compressed";
21  image_sub_ = nh_.subscribe(compressed_topic, 1, &RosCompressedStreamer::imageCallback, this);
22 }
23 
25 {
26  if (inactive_ || (last_msg == 0))
27  return;
28 
29  if ( last_frame + ros::Duration(max_age) < ros::Time::now() ) {
30  boost::mutex::scoped_lock lock(send_mutex_);
31  sendImage(last_msg, ros::Time::now() ); // don't update last_frame, it may remain an old value.
32  }
33 }
34 
35 void RosCompressedStreamer::sendImage(const sensor_msgs::CompressedImageConstPtr &msg,
36  const ros::Time &time) {
37  try {
38  std::string content_type;
39  if(msg->format.find("jpeg") != std::string::npos) {
40  content_type = "image/jpeg";
41  }
42  else if(msg->format.find("png") != std::string::npos) {
43  content_type = "image/png";
44  }
45  else {
46  ROS_WARN_STREAM("Unknown ROS compressed image format: " << msg->format);
47  return;
48  }
49 
50  stream_.sendPart(time, content_type, boost::asio::buffer(msg->data), msg);
51  }
52  catch (boost::system::system_error &e)
53  {
54  // happens when client disconnects
55  ROS_DEBUG("system_error exception: %s", e.what());
56  inactive_ = true;
57  return;
58  }
59  catch (std::exception &e)
60  {
61  ROS_ERROR_THROTTLE(30, "exception: %s", e.what());
62  inactive_ = true;
63  return;
64  }
65  catch (...)
66  {
67  ROS_ERROR_THROTTLE(30, "exception");
68  inactive_ = true;
69  return;
70  }
71 }
72 
73 
74 void RosCompressedStreamer::imageCallback(const sensor_msgs::CompressedImageConstPtr &msg) {
75  boost::mutex::scoped_lock lock(send_mutex_); // protects last_msg and last_frame
76  last_msg = msg;
77  last_frame = ros::Time(msg->header.stamp.sec, msg->header.stamp.nsec);
79 }
80 
81 
84  ros::NodeHandle& nh)
85 {
86  return boost::shared_ptr<ImageStreamer>(new RosCompressedStreamer(request, connection, nh));
87 }
88 
90 {
91  std::stringstream ss;
92  ss << "<img src=\"/stream?";
93  ss << request.query;
94  ss << "\"></img>";
95  return ss.str();
96 }
97 
98 
99 }
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
boost::shared_ptr< ImageStreamer > create_streamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
void sendPart(const ros::Time &time, const std::string &type, const boost::asio::const_buffer &buffer, async_web_server_cpp::HttpConnection::ResourcePtr resource)
#define ROS_ERROR_THROTTLE(period,...)
std::string create_viewer(const async_web_server_cpp::HttpRequest &request)
#define ROS_WARN_STREAM(args)
virtual void sendImage(const sensor_msgs::CompressedImageConstPtr &msg, const ros::Time &time)
static Time now()
sensor_msgs::CompressedImageConstPtr last_msg
RosCompressedStreamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
void imageCallback(const sensor_msgs::CompressedImageConstPtr &msg)
#define ROS_DEBUG(...)


web_video_server
Author(s): Mitchell Wills
autogenerated on Tue Mar 1 2022 00:04:38