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") {
171 std::shared_ptr<WebviewJpegStreamProducer::Buffer> buf = stream->wait_for_next_frame();
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");
ImageInfo representation for JSON transfer.
void set_colorspace(const std::string &colorspace)
Set colorspace value.
void set_kind(const std::string &kind)
Set kind value.
void set_frame(const std::string &frame)
Set frame value.
void set_height(const int64_t &height)
Set height value.
void set_width(const int64_t &width)
Set width value.
void set_mem_size(const int64_t &mem_size)
Set mem_size value.
void set_id(const std::string &id)
Set id value.
void set_apiVersion(const std::string &apiVersion)
Set apiVersion value.
static std::string api_version()
Get version of implemented API.
virtual void loop()
Code to execute in the thread.
virtual void init()
Initialize the thread.
~ImageRestApi()
Destructor.
ImageRestApi()
Constructor.
virtual void finalize()
Finalize the thread.
Container to return array via REST.
void push_back(M &m)
Add item at the back of the container.
Configuration * config
This is the Configuration member used to access the configuration.
virtual unsigned int get_uint(const char *path)=0
Get value from configuration which is of type unsigned int.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
Base class for exceptions in Fawkes.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Logger * logger
This is the Logger member used to access the logger.
virtual void add(ThreadList &tl)=0
Add multiple threads.
virtual void remove(ThreadList &tl)=0
Remove multiple threads.
ThreadCollector * thread_collector
Thread collector.
Thread class encapsulation of pthreads.
WebviewRestApiManager * webview_rest_api_manager
Webview REST API manager.
void unregister_api(WebviewRestApi *api)
Remove a request processor.
void register_api(WebviewRestApi *api)
Add a REST API.
Webview REST API component.
void add_handler(WebRequest::Method method, std::string path, Handler handler)
Add handler function.
REST parameters to pass to handlers.
std::string path_arg(const std::string &what)
Get a path argument.
Fawkes library namespace.