jpeg_streamers.cpp
Go to the documentation of this file.
00001 #include "web_video_server/jpeg_streamers.h"
00002 #include "async_web_server_cpp/http_reply.hpp"
00003 
00004 namespace web_video_server
00005 {
00006 
00007 MjpegStreamer::MjpegStreamer(const async_web_server_cpp::HttpRequest &request,
00008                              async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle& nh) :
00009   ImageTransportImageStreamer(request, connection, nh), stream_(connection)
00010 {
00011   quality_ = request.get_query_param_value_or_default<int>("quality", 95);
00012   stream_.sendInitialHeader();
00013 }
00014 
00015 MjpegStreamer::~MjpegStreamer()
00016 {
00017   this->inactive_ = true;
00018   boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
00019 }
00020 
00021 void MjpegStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
00022 {
00023   std::vector<int> encode_params;
00024   encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
00025   encode_params.push_back(quality_);
00026 
00027   std::vector<uchar> encoded_buffer;
00028   cv::imencode(".jpeg", img, encoded_buffer, encode_params);
00029 
00030   stream_.sendPartAndClear(time, "image/jpeg", encoded_buffer);
00031 }
00032 
00033 boost::shared_ptr<ImageStreamer> MjpegStreamerType::create_streamer(const async_web_server_cpp::HttpRequest &request,
00034                                                                     async_web_server_cpp::HttpConnectionPtr connection,
00035                                                                     ros::NodeHandle& nh)
00036 {
00037   return boost::shared_ptr<ImageStreamer>(new MjpegStreamer(request, connection, nh));
00038 }
00039 
00040 std::string MjpegStreamerType::create_viewer(const async_web_server_cpp::HttpRequest &request)
00041 {
00042   std::stringstream ss;
00043   ss << "<img src=\"/stream?";
00044   ss << request.query;
00045   ss << "\"></img>";
00046   return ss.str();
00047 }
00048 
00049 JpegSnapshotStreamer::JpegSnapshotStreamer(const async_web_server_cpp::HttpRequest &request,
00050                                            async_web_server_cpp::HttpConnectionPtr connection,
00051                                            ros::NodeHandle& nh) :
00052     ImageTransportImageStreamer(request, connection, nh)
00053 {
00054   quality_ = request.get_query_param_value_or_default<int>("quality", 95);
00055 }
00056 
00057 JpegSnapshotStreamer::~JpegSnapshotStreamer()
00058 {
00059   this->inactive_ = true;
00060   boost::mutex::scoped_lock lock(send_mutex_); // protects sendImage.
00061 }
00062 
00063 void JpegSnapshotStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
00064 {
00065   std::vector<int> encode_params;
00066   encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
00067   encode_params.push_back(quality_);
00068 
00069   std::vector<uchar> encoded_buffer;
00070   cv::imencode(".jpeg", img, encoded_buffer, encode_params);
00071 
00072   char stamp[20];
00073   sprintf(stamp, "%.06lf", time.toSec());
00074   async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok)
00075       .header("Connection", "close")
00076       .header("Server", "web_video_server")
00077       .header("Cache-Control",
00078               "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, "
00079               "max-age=0")
00080       .header("X-Timestamp", stamp)
00081       .header("Pragma", "no-cache")
00082       .header("Content-type", "image/jpeg")
00083       .header("Access-Control-Allow-Origin", "*")
00084       .header("Content-Length",
00085               boost::lexical_cast<std::string>(encoded_buffer.size()))
00086       .write(connection_);
00087   connection_->write_and_clear(encoded_buffer);
00088   inactive_ = true;
00089 }
00090 
00091 }


web_video_server
Author(s): Mitchell Wills
autogenerated on Thu Jun 6 2019 18:02:48