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 void MjpegStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
00016 {
00017 std::vector<int> encode_params;
00018 encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
00019 encode_params.push_back(quality_);
00020
00021 std::vector<uchar> encoded_buffer;
00022 cv::imencode(".jpeg", img, encoded_buffer, encode_params);
00023
00024 stream_.sendPartAndClear(time, "image/jpeg", encoded_buffer);
00025 }
00026
00027 boost::shared_ptr<ImageStreamer> MjpegStreamerType::create_streamer(const async_web_server_cpp::HttpRequest &request,
00028 async_web_server_cpp::HttpConnectionPtr connection,
00029 ros::NodeHandle& nh)
00030 {
00031 return boost::shared_ptr<ImageStreamer>(new MjpegStreamer(request, connection, nh));
00032 }
00033
00034 std::string MjpegStreamerType::create_viewer(const async_web_server_cpp::HttpRequest &request)
00035 {
00036 std::stringstream ss;
00037 ss << "<img src=\"/stream?";
00038 ss << request.query;
00039 ss << "\"></img>";
00040 return ss.str();
00041 }
00042
00043 JpegSnapshotStreamer::JpegSnapshotStreamer(const async_web_server_cpp::HttpRequest &request,
00044 async_web_server_cpp::HttpConnectionPtr connection,
00045 ros::NodeHandle& nh) :
00046 ImageTransportImageStreamer(request, connection, nh)
00047 {
00048 quality_ = request.get_query_param_value_or_default<int>("quality", 95);
00049 }
00050
00051 void JpegSnapshotStreamer::sendImage(const cv::Mat &img, const ros::Time &time)
00052 {
00053 std::vector<int> encode_params;
00054 encode_params.push_back(CV_IMWRITE_JPEG_QUALITY);
00055 encode_params.push_back(quality_);
00056
00057 std::vector<uchar> encoded_buffer;
00058 cv::imencode(".jpeg", img, encoded_buffer, encode_params);
00059
00060 char stamp[20];
00061 sprintf(stamp, "%.06lf", time.toSec());
00062 async_web_server_cpp::HttpReply::builder(async_web_server_cpp::HttpReply::ok).header("Connection", "close").header(
00063 "Server", "web_video_server").header("Cache-Control",
00064 "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0").header(
00065 "X-Timestamp", stamp).header("Pragma", "no-cache").header("Content-type", "image/jpeg").header(
00066 "Access-Control-Allow-Origin", "*").header("Content-Length",
00067 boost::lexical_cast<std::string>(encoded_buffer.size())).write(
00068 connection_);
00069 connection_->write_and_clear(encoded_buffer);
00070 inactive_ = true;
00071 }
00072
00073 }