Newer
Older
#include <glog/logging.h>
#include <ftl/streamer.hpp>
#include <vector>
// #include <lz4.h>
using ftl::Streamer;
using ftl::net::Universe;
using cv::Mat;
using nlohmann::json;
using std::string;
using std::vector;
Streamer::Streamer(Universe &net, json &config) : net_(net), config_(config) {
uri_ = string("ftl://utu.fi/")+(string)config["name"]+string("/rgb-d");
net.createResource(uri_);
}
Streamer::~Streamer() {
}
void Streamer::send(const Mat &rgb, const Mat &depth) {
// Compress the rgb as jpeg.
vector<unsigned char> rgb_buf;
cv::imencode(".jpg", rgb, rgb_buf);

Nicolas Pope
committed
Mat d2;

Nicolas Pope
committed
vector<unsigned char> d_buf;

Nicolas Pope
committed
/*d_buf.resize(d2.step*d2.rows);
z_stream defstream;
defstream.zalloc = Z_NULL;
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;

Nicolas Pope
committed
defstream.avail_in = d2.step*d2.rows;
defstream.next_in = (Bytef *)d2.data; // input char array
defstream.avail_out = (uInt)d2.step*d2.rows; // size of output
defstream.next_out = (Bytef *)d_buf.data(); // output char array
deflateInit(&defstream, Z_DEFAULT_COMPRESSION);
deflate(&defstream, Z_FINISH);
deflateEnd(&defstream);

Nicolas Pope
committed
d2.copyTo(last);
d_buf.resize(defstream.total_out);*/
// LZ4 Version
// d_buf.resize(LZ4_compressBound(depth.step*depth.rows));
// int s = LZ4_compress_default((char*)depth.data, (char*)d_buf.data(), depth.step*depth.rows, d_buf.size());
// d_buf.resize(s);

Nicolas Pope
committed
cv::imencode(".png", d2, d_buf);

Nicolas Pope
committed
LOG(INFO) << "Depth Size = " << ((float)d_buf.size() / (1024.0f*1024.0f));
try {
net_.publish(uri_, rgb_buf, d_buf);
} catch (...) {
LOG(ERROR) << "Exception on net publish to " << uri_;
}