jpeg_streamers.cpp
Go to the documentation of this file.
2 #include "async_web_server_cpp/http_reply.hpp"
3 
4 namespace web_video_server
5 {
6 
7 MjpegStreamer::MjpegStreamer(const async_web_server_cpp::HttpRequest &request,
8  async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle& nh) :
9  ImageTransportImageStreamer(request, connection, nh), stream_(connection)
10 {
11  quality_ = request.get_query_param_value_or_default<int>("quality", 95);
13 }
14 
16 {
17  this->inactive_ = true;
18  boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
19 }
20 
21 void MjpegStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
22 {
23  std::vector<int> encode_params;
24  encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
25  encode_params.push_back(quality_);
26 
27  std::vector<uchar> encoded_buffer;
28  cv::imencode(".jpeg", img, encoded_buffer, encode_params);
29 
30  stream_.sendPartAndClear(time, "image/jpeg", encoded_buffer);
31 }
32 
33 boost::shared_ptr<ImageStreamer> MjpegStreamerType::create_streamer(const async_web_server_cpp::HttpRequest &request,
34  async_web_server_cpp::HttpConnectionPtr connection,
35  ros::NodeHandle& nh)
36 {
37  return boost::shared_ptr<ImageStreamer>(new MjpegStreamer(request, connection, nh));
38 }
39 
40 std::string MjpegStreamerType::create_viewer(const async_web_server_cpp::HttpRequest &request)
41 {
42  std::stringstream ss;
43  ss << "<img src=\"/stream?";
44  ss << request.query;
45  ss << "\"></img>";
46  return ss.str();
47 }
48 
49 JpegSnapshotStreamer::JpegSnapshotStreamer(const async_web_server_cpp::HttpRequest &request,
50  async_web_server_cpp::HttpConnectionPtr connection,
51  ros::NodeHandle& nh) :
52  ImageTransportImageStreamer(request, connection, nh)
53 {
54  quality_ = request.get_query_param_value_or_default<int>("quality", 95);
55 }
56 
58 {
59  this->inactive_ = true;
60  boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
61 }
62 
63 void JpegSnapshotStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
64 {
65  std::vector<int> encode_params;
66  encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
67  encode_params.push_back(quality_);
68 
69  std::vector<uchar> encoded_buffer;
70  cv::imencode(".jpeg", img, encoded_buffer, encode_params);
71 
72  char stamp[20];
73  sprintf(stamp, "%.06lf", time.toSec());
74  async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok)
75  .header("Connection", "close")
76  .header("Server", "web_video_server")
77  .header("Cache-Control",
78  "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, "
79  "max-age=0")
80  .header("X-Timestamp", stamp)
81  .header("Pragma", "no-cache")
82  .header("Content-type", "image/jpeg")
83  .header("Access-Control-Allow-Origin", "*")
84  .header("Content-Length",
85  boost::lexical_cast<std::string>(encoded_buffer.size()))
86  .write(connection_);
87  connection_->write_and_clear(encoded_buffer);
88  inactive_ = true;
89 }
90 
91 }
virtual void sendImage(const cv::Mat &, const ros::Time &time)
async_web_server_cpp::HttpConnectionPtr connection_
void sendPartAndClear(const ros::Time &time, const std::string &type, std::vector< unsigned char > &data)
boost::shared_ptr< ImageStreamer > create_streamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
JpegSnapshotStreamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
virtual void sendImage(const cv::Mat &, const ros::Time &time)
MjpegStreamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
std::string create_viewer(const async_web_server_cpp::HttpRequest &request)


web_video_server
Author(s): Mitchell Wills
autogenerated on Wed Jun 5 2019 20:38:04