multipart_stream.cpp
Go to the documentation of this file.
3 
4 namespace web_video_server
5 {
6 
9  const std::string& boundry,
10  std::size_t max_queue_size)
11  : connection_(connection), boundry_(boundry), max_queue_size_(max_queue_size)
12 {}
13 
16  "Server", "web_video_server").header("Cache-Control",
17  "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0").header(
18  "Pragma", "no-cache").header("Content-type", "multipart/x-mixed-replace;boundary="+boundry_).header(
19  "Access-Control-Allow-Origin", "*").write(connection_);
20  connection_->write("--"+boundry_+"\r\n");
21 }
22 
23 void MultipartStream::sendPartHeader(const ros::Time &time, const std::string& type, size_t payload_size) {
24  char stamp[20];
25  sprintf(stamp, "%.06lf", time.toSec());
27  new std::vector<async_web_server_cpp::HttpHeader>());
28  headers->push_back(async_web_server_cpp::HttpHeader("Content-type", type));
29  headers->push_back(async_web_server_cpp::HttpHeader("X-Timestamp", stamp));
30  headers->push_back(
31  async_web_server_cpp::HttpHeader("Content-Length", boost::lexical_cast<std::string>(payload_size)));
33 }
34 
36  boost::shared_ptr<std::string> str(new std::string("\r\n--"+boundry_+"\r\n"));
37  PendingFooter pf;
38  pf.timestamp = time;
39  pf.contents = str;
40  connection_->write(boost::asio::buffer(*str), str);
41  if (max_queue_size_ > 0) pending_footers_.push(pf);
42 }
43 
44 void MultipartStream::sendPartAndClear(const ros::Time &time, const std::string& type,
45  std::vector<unsigned char> &data) {
46  if (!isBusy())
47  {
48  sendPartHeader(time, type, data.size());
49  connection_->write_and_clear(data);
50  sendPartFooter(time);
51  }
52 }
53 
54 void MultipartStream::sendPart(const ros::Time &time, const std::string& type,
55  const boost::asio::const_buffer &buffer,
57  if (!isBusy())
58  {
59  sendPartHeader(time, type, boost::asio::buffer_size(buffer));
60  connection_->write(buffer, resource);
61  sendPartFooter(time);
62  }
63 }
64 
66  ros::Time currentTime = ros::Time::now();
67  while (!pending_footers_.empty())
68  {
69  if (pending_footers_.front().contents.expired()) {
70  pending_footers_.pop();
71  } else {
72  ros::Time footerTime = pending_footers_.front().timestamp;
73  if ((currentTime - footerTime).toSec() > 0.5) {
74  pending_footers_.pop();
75  } else {
76  break;
77  }
78  }
79  }
80  return !(max_queue_size_ == 0 || pending_footers_.size() < max_queue_size_);
81 }
82 
83 }
http_reply.hpp
boost::shared_ptr< HttpConnection >
async_web_server_cpp::HttpHeader
web_video_server::MultipartStream::sendPart
void sendPart(const ros::Time &time, const std::string &type, const boost::asio::const_buffer &buffer, async_web_server_cpp::HttpConnection::ResourcePtr resource)
Definition: multipart_stream.cpp:54
TimeBase< Time, Duration >::toSec
double toSec() const
web_video_server::MultipartStream::sendPartHeader
void sendPartHeader(const ros::Time &time, const std::string &type, size_t payload_size)
Definition: multipart_stream.cpp:23
web_video_server::MultipartStream::connection_
async_web_server_cpp::HttpConnectionPtr connection_
Definition: multipart_stream.h:35
web_video_server::MultipartStream::sendInitialHeader
void sendInitialHeader()
Definition: multipart_stream.cpp:14
async_web_server_cpp::ReplyBuilder::header
ReplyBuilder & header(const HttpHeader &header)
web_video_server::PendingFooter::timestamp
ros::Time timestamp
Definition: multipart_stream.h:13
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::PendingFooter::contents
boost::weak_ptr< std::string > contents
Definition: multipart_stream.h:14
web_video_server::MultipartStream::pending_footers_
std::queue< PendingFooter > pending_footers_
Definition: multipart_stream.h:37
web_video_server::MultipartStream::MultipartStream
MultipartStream(async_web_server_cpp::HttpConnectionPtr &connection, const std::string &boundry="boundarydonotcross", std::size_t max_queue_size=1)
Definition: multipart_stream.cpp:7
ros::Time
web_video_server::MultipartStream::sendPartFooter
void sendPartFooter(const ros::Time &time)
Definition: multipart_stream.cpp:35
async_web_server_cpp::HttpReply::builder
static ReplyBuilder builder(status_type status)
web_video_server::MultipartStream::boundry_
std::string boundry_
Definition: multipart_stream.h:36
async_web_server_cpp::ReplyBuilder::write
void write(HttpConnectionPtr connection)
async_web_server_cpp::HttpReply::to_buffers
static std::vector< boost::asio::const_buffer > to_buffers(const std::vector< HttpHeader > &headers)
web_video_server::MultipartStream::isBusy
bool isBusy()
Definition: multipart_stream.cpp:65
web_video_server::MultipartStream::max_queue_size_
const std::size_t max_queue_size_
Definition: multipart_stream.h:34
multipart_stream.h
web_video_server::PendingFooter
Definition: multipart_stream.h:12
async_web_server_cpp::HttpReply::ok
ok
ros::Time::now
static Time now()


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