jpeg_streamers.cpp
Go to the documentation of this file.
3 
4 namespace web_video_server
5 {
6 
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 
35  ros::NodeHandle& nh)
36 {
37  return boost::shared_ptr<ImageStreamer>(new MjpegStreamer(request, connection, nh));
38 }
39 
41 {
42  std::stringstream ss;
43  ss << "<img src=\"/stream?";
44  ss << request.query;
45  ss << "\"></img>";
46  return ss.str();
47 }
48 
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());
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()))
87  connection_->write_and_clear(encoded_buffer);
88  inactive_ = true;
89 }
90 
91 }
http_reply.hpp
web_video_server::JpegSnapshotStreamer::sendImage
virtual void sendImage(const cv::Mat &, const ros::Time &time)
Definition: jpeg_streamers.cpp:63
boost::shared_ptr< HttpConnection >
web_video_server::ImageStreamer::inactive_
bool inactive_
Definition: image_streamer.h:43
web_video_server::JpegSnapshotStreamer::JpegSnapshotStreamer
JpegSnapshotStreamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
Definition: jpeg_streamers.cpp:49
web_video_server::ImageTransportImageStreamer
Definition: image_streamer.h:49
web_video_server::MjpegStreamer::MjpegStreamer
MjpegStreamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
Definition: jpeg_streamers.cpp:7
web_video_server::JpegSnapshotStreamer::~JpegSnapshotStreamer
~JpegSnapshotStreamer()
Definition: jpeg_streamers.cpp:57
TimeBase< Time, Duration >::toSec
double toSec() const
web_video_server::MjpegStreamer
Definition: jpeg_streamers.h:13
web_video_server::MjpegStreamerType::create_viewer
std::string create_viewer(const async_web_server_cpp::HttpRequest &request)
Definition: jpeg_streamers.cpp:40
web_video_server::MultipartStream::sendInitialHeader
void sendInitialHeader()
Definition: multipart_stream.cpp:14
web_video_server::MjpegStreamerType::create_streamer
boost::shared_ptr< ImageStreamer > create_streamer(const async_web_server_cpp::HttpRequest &request, async_web_server_cpp::HttpConnectionPtr connection, ros::NodeHandle &nh)
Definition: jpeg_streamers.cpp:33
web_video_server::JpegSnapshotStreamer::quality_
int quality_
Definition: jpeg_streamers.h:46
async_web_server_cpp::HttpRequest
async_web_server_cpp::ReplyBuilder::header
ReplyBuilder & header(const HttpHeader &header)
async_web_server_cpp::HttpRequest::get_query_param_value_or_default
std::string get_query_param_value_or_default(const std::string &name, const std::string &default_value) const
web_video_server::MjpegStreamer::sendImage
virtual void sendImage(const cv::Mat &, const ros::Time &time)
Definition: jpeg_streamers.cpp:21
jpeg_streamers.h
web_video_server
Definition: h264_streamer.h:9
web_video_server::MultipartStream::sendPartAndClear
void sendPartAndClear(const ros::Time &time, const std::string &type, std::vector< unsigned char > &data)
Definition: multipart_stream.cpp:44
web_video_server::MjpegStreamer::~MjpegStreamer
~MjpegStreamer()
Definition: jpeg_streamers.cpp:15
web_video_server::MjpegStreamer::stream_
MultipartStream stream_
Definition: jpeg_streamers.h:23
web_video_server::ImageTransportImageStreamer::send_mutex_
boost::mutex send_mutex_
Definition: image_streamer.h:70
web_video_server::MjpegStreamer::quality_
int quality_
Definition: jpeg_streamers.h:24
ros::Time
async_web_server_cpp::HttpRequest::query
std::string query
async_web_server_cpp::HttpReply::builder
static ReplyBuilder builder(status_type status)
async_web_server_cpp::ReplyBuilder::write
void write(HttpConnectionPtr connection)
web_video_server::ImageStreamer::connection_
async_web_server_cpp::HttpConnectionPtr connection_
Definition: image_streamer.h:38
async_web_server_cpp::HttpReply::ok
ok
ros::NodeHandle


web_video_server
Author(s): Mitchell Wills
autogenerated on Wed Mar 2 2022 01:13:35