22 #include "mjpeg_reply.h" 24 #include <core/exceptions/system.h> 25 #include <core/threading/mutex.h> 26 #include <core/threading/wait_condition.h> 27 #include <fvutils/compression/jpeg_compressor.h> 28 #include <fvcams/shmem.h> 58 add_header(
"Content-type",
"multipart/x-mixed-replace;boundary=MJPEG-next-frame");
59 stream_producer_ = stream_producer;
67 delete next_buffer_mutex_;
68 delete next_buffer_waitcond_;
80 next_buffer_mutex_->
lock();
81 next_buffer_ = buffer;
83 next_buffer_mutex_->
unlock();
89 if (buf_max_size == 0)
return 0;
94 next_buffer_mutex_->
lock();
95 while (! next_buffer_) {
96 next_buffer_waitcond_->
wait();
98 buffer_ = next_buffer_;
100 next_buffer_mutex_->
unlock();
103 if (asprintf(&header,
104 "--MJPEG-next-frame\r\n" 105 "Content-type: image/jpeg\r\n" 106 "Content-length: %zu\r\n" 107 "\r\n", buffer_->size()) == -1) {
110 size_t header_len = strlen(header);
111 memcpy(buffer, header, header_len);
112 buffer += header_len;
113 buf_max_size -= header_len;
114 written += header_len;
116 buffer_bytes_written_ = 0;
120 size_t remaining = buffer_->size() - buffer_bytes_written_;
121 if (remaining <= buf_max_size) {
122 memcpy(buffer, buffer_->data() + buffer_bytes_written_, remaining);
124 written += remaining;
126 memcpy(buffer, buffer_->data() + buffer_bytes_written_, buf_max_size);
127 buffer_bytes_written_ += buf_max_size;
128 written += buf_max_size;
Wait until a given condition holds.
virtual ~DynamicMJPEGStreamWebReply()
Destructor.
Fawkes library namespace.
void unlock()
Unlock the mutex.
void wake_all()
Wake up all waiting threads.
void add_header(std::string header, std::string content)
Add a HTTP header.
virtual void handle_buffer(RefPtr< WebviewJpegStreamProducer::Buffer > buffer)
Notification if a new buffer is available.
virtual size_t size()
Total size of the web reply.
void wait()
Wait for the condition forever.
RefPtr<> is a reference-counting shared smartpointer.
void remove_subscriber(Subscriber *subscriber)
Remove a subscriber.
virtual size_t next_chunk(size_t pos, char *buffer, size_t buf_max_size)
Get data of next chunk.
void lock()
Lock this mutex.
Mutex mutual exclusion lock.
void add_subscriber(Subscriber *subscriber)
Add a subscriber.