22 #include "image-rest-api.h"
24 #include "jpeg_stream_producer.h"
25 #include "mjpeg_reply.h"
27 #include <fvutils/ipc/shm_image.h>
28 #include <webview/rest_api_manager.h>
31 using namespace firevision;
53 WebRequest::METHOD_GET,
"/?", std::bind(&ImageRestApi::cb_list_images,
this));
56 std::bind(&ImageRestApi::cb_get_image,
this, std::placeholders::_1));
65 for (
auto &s : streams_) {
77 ImageRestApi::cb_list_images()
81 std::list<SharedMemoryImageBufferMetaData> meta_data = SharedMemoryImageBuffer::list_meta_data();
83 for (
auto &m : meta_data) {
99 std::shared_ptr<fawkes::WebviewJpegStreamProducer>
100 ImageRestApi::get_stream(
const std::string &image_id)
102 if (streams_.find(image_id) == streams_.end()) {
104 std::string cfg_prefix =
"/webview/images/" + image_id +
"/";
105 unsigned int quality = 80;
110 quality =
config->
get_uint(
"/webview/images/default/jpeg-quality");
123 quality =
config->
get_uint((cfg_prefix +
"jpeg-quality").c_str());
135 auto stream = std::make_shared<WebviewJpegStreamProducer>(image_id, quality, fps, vflip);
139 streams_[image_id] = stream;
142 "Failed to open buffer '%s',"
143 " exception follows",
150 return streams_[image_id];
153 std::unique_ptr<WebReply>
156 std::string image = params.
path_arg(
"id");
158 std::string::size_type last_dot = image.rfind(
".");
159 if (last_dot == std::string::npos) {
160 return std::make_unique<StaticWebReply>(WebReply::HTTP_NOT_FOUND,
"Invalid stream ID");
162 std::string image_id = image.substr(0, last_dot);
163 std::string image_type = image.substr(last_dot + 1);
165 std::shared_ptr<WebviewJpegStreamProducer> stream = get_stream(image_id);
167 return std::make_unique<StaticWebReply>(WebReply::HTTP_NOT_FOUND,
"Stream not found");
170 if (image_type ==
"jpeg" || image_type ==
"jpg") {
174 std::string body((
char *)buf->
data(), buf->
size());
175 auto reply = std::make_unique<StaticWebReply>(WebReply::HTTP_OK, body);
176 reply->add_header(
"Content-type",
"image/jpeg");
177 reply->set_caching(
false);
179 }
else if (image_type ==
"mjpeg" || image_type ==
"mjpg") {
180 return std::make_unique<DynamicMJPEGStreamWebReply>(stream);
182 return std::make_unique<StaticWebReply>(WebReply::HTTP_NOT_FOUND,
"Unknown image format");