MYNT-EYE-S-SDK/src/internal/streams.cc

314 lines
10 KiB
C++
Raw Normal View History

2018-05-10 09:46:34 +03:00
// Copyright 2018 Slightech Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-04-08 07:23:33 +03:00
#include "internal/streams.h"
#include <glog/logging.h>
#include <algorithm>
2018-04-08 17:35:49 +03:00
#include <chrono>
2018-04-08 20:20:00 +03:00
#include <iomanip>
2018-04-11 12:08:24 +03:00
#include <stdexcept>
2018-04-08 20:20:00 +03:00
#include "internal/types.h"
2018-04-08 07:23:33 +03:00
MYNTEYE_BEGIN_NAMESPACE
2018-04-08 17:35:49 +03:00
namespace {
2018-04-09 04:24:08 +03:00
bool unpack_stereo_img_data(
2018-04-10 11:19:28 +03:00
const void *data, const StreamRequest &request, ImgData *img) {
CHECK_NOTNULL(img);
2018-04-08 20:20:00 +03:00
CHECK_EQ(request.format, Format::YUYV);
auto data_new = reinterpret_cast<const std::uint8_t *>(data);
std::size_t data_n =
request.width * request.height * bytes_per_pixel(request.format);
auto data_end = data_new + data_n;
std::size_t packet_n = sizeof(ImagePacket);
std::vector<std::uint8_t> packet(packet_n);
std::reverse_copy(data_end - packet_n, data_end, packet.begin());
ImagePacket img_packet(packet.data());
2018-04-13 05:20:02 +03:00
// LOG(INFO) << "ImagePacket: header=0x" << std::hex <<
2018-04-08 20:20:00 +03:00
// static_cast<int>(img_packet.header)
// << ", size=0x" << std::hex << static_cast<int>(img_packet.size)
// << ", frame_id="<< std::dec << img_packet.frame_id
// << ", timestamp="<< std::dec << img_packet.timestamp
// << ", exposure_time="<< std::dec << img_packet.exposure_time
// << ", checksum=0x" << std::hex << static_cast<int>(img_packet.checksum);
2018-07-08 13:49:09 +03:00
/*
2018-04-08 20:20:00 +03:00
if (img_packet.header != 0x3B) {
LOG(WARNING) << "Image packet header must be 0x3B, but 0x" << std::hex
<< std::uppercase << std::setw(2) << std::setfill('0')
<< static_cast<int>(img_packet.header) << " now";
2018-04-09 04:24:08 +03:00
return false;
2018-04-08 20:20:00 +03:00
}
std::uint8_t checksum = 0;
2018-04-09 15:51:13 +03:00
for (std::size_t i = 2, n = packet_n - 2; i <= n; i++) { // content: [2,9]
2018-04-08 20:20:00 +03:00
checksum = (checksum ^ packet[i]);
}
2018-04-17 09:46:11 +03:00
if (img_packet.checksum != checksum) {
2018-04-08 20:20:00 +03:00
LOG(WARNING) << "Image packet checksum should be 0x" << std::hex
<< std::uppercase << std::setw(2) << std::setfill('0')
2018-04-17 09:46:11 +03:00
<< static_cast<int>(img_packet.checksum) << ", but 0x"
<< std::setw(2) << std::setfill('0')
<< static_cast<int>(checksum) << " now";
2018-04-09 04:24:08 +03:00
return false;
2018-04-08 20:20:00 +03:00
}
2018-07-08 13:49:09 +03:00
*/
2018-04-10 11:19:28 +03:00
img->frame_id = img_packet.frame_id;
img->timestamp = img_packet.timestamp;
img->exposure_time = img_packet.exposure_time;
2018-04-09 04:24:08 +03:00
return true;
2018-04-08 17:35:49 +03:00
}
2018-04-09 04:24:08 +03:00
bool unpack_left_img_pixels(
2018-04-10 11:19:28 +03:00
const void *data, const StreamRequest &request, Streams::frame_t *frame) {
CHECK_NOTNULL(frame);
2018-04-08 17:35:49 +03:00
CHECK_EQ(request.format, Format::YUYV);
2018-07-08 13:49:09 +03:00
CHECK_EQ(frame->format(), Format::YUYV);
2018-04-08 17:35:49 +03:00
auto data_new = reinterpret_cast<const std::uint8_t *>(data);
2018-07-08 13:49:09 +03:00
std::size_t w = frame->width() * 2;
std::size_t h = frame->height();
for(std::size_t i = 0; i < h; i++) {
for(std::size_t j = 0; j < w; j++) {
frame->data()[i * w + j] = *(data_new + 2 * i * w + j);
}
2018-04-08 17:35:49 +03:00
}
2018-04-09 04:24:08 +03:00
return true;
2018-04-08 17:35:49 +03:00
}
2018-04-09 04:24:08 +03:00
bool unpack_right_img_pixels(
2018-04-10 11:19:28 +03:00
const void *data, const StreamRequest &request, Streams::frame_t *frame) {
CHECK_NOTNULL(frame);
2018-04-08 17:35:49 +03:00
CHECK_EQ(request.format, Format::YUYV);
2018-07-08 13:49:09 +03:00
CHECK_EQ(frame->format(), Format::YUYV);
2018-04-08 17:35:49 +03:00
auto data_new = reinterpret_cast<const std::uint8_t *>(data);
2018-07-08 13:49:09 +03:00
std::size_t w = frame->width() * 2;
std::size_t h = frame->height();
for(std::size_t i = 0; i < h; i++) {
for(std::size_t j = 0; j < w; j++) {
frame->data()[i * w + j] = *(data_new + (2 * i + 1) * w + j);
}
2018-04-08 17:35:49 +03:00
}
2018-04-09 04:24:08 +03:00
return true;
2018-04-08 17:35:49 +03:00
}
} // namespace
Streams::Streams(const std::vector<Stream> key_streams)
: key_streams_(key_streams),
stream_capabilities_(
2018-04-08 07:23:33 +03:00
{Capabilities::STEREO, Capabilities::COLOR, Capabilities::DEPTH,
Capabilities::POINTS, Capabilities::FISHEYE, Capabilities::INFRARED,
2018-04-08 17:35:49 +03:00
Capabilities::INFRARED2}),
unpack_img_data_map_(
{{Stream::LEFT, unpack_stereo_img_data},
{Stream::RIGHT, unpack_stereo_img_data}}),
unpack_img_pixels_map_(
{{Stream::LEFT, unpack_left_img_pixels},
{Stream::RIGHT, unpack_right_img_pixels}}) {
2018-04-08 07:23:33 +03:00
VLOG(2) << __func__;
}
Streams::~Streams() {
VLOG(2) << __func__;
}
void Streams::ConfigStream(
const Capabilities &capability, const StreamRequest &request) {
if (!IsStreamCapability(capability)) {
2018-05-30 07:31:54 +03:00
LOG(ERROR) << "Cannot config stream without stream capability";
return;
2018-04-08 07:23:33 +03:00
}
2018-04-08 17:35:49 +03:00
VLOG(2) << "Config stream request of " << capability << ", " << request;
2018-04-08 07:23:33 +03:00
stream_config_requests_[capability] = request;
}
2018-05-31 05:20:48 +03:00
bool Streams::PushStream(const Capabilities &capability, const void *data) {
2018-04-08 07:23:33 +03:00
if (!HasStreamConfigRequest(capability)) {
LOG(FATAL) << "Cannot push stream without stream config request";
}
2018-04-08 17:35:49 +03:00
std::unique_lock<std::mutex> lock(mtx_);
auto &&request = GetStreamConfigRequest(capability);
2018-05-31 05:20:48 +03:00
bool pushed = false;
2018-04-08 17:35:49 +03:00
switch (capability) {
case Capabilities::STEREO: {
2018-04-11 12:08:24 +03:00
// alloc left
2018-07-08 13:49:09 +03:00
AllocStreamData(Stream::LEFT, request, Format::YUYV);
2018-04-08 17:35:49 +03:00
auto &&left_data = stream_datas_map_[Stream::LEFT].back();
// unpack img data
2018-04-10 11:19:28 +03:00
if (unpack_img_data_map_[Stream::LEFT](
data, request, left_data.img.get())) {
2018-04-11 12:08:24 +03:00
// alloc right
2018-07-08 13:49:09 +03:00
AllocStreamData(Stream::RIGHT, request, Format::YUYV);
2018-04-11 12:08:24 +03:00
auto &&right_data = stream_datas_map_[Stream::RIGHT].back();
*right_data.img = *left_data.img;
// unpack frame
unpack_img_pixels_map_[Stream::LEFT](
data, request, left_data.frame.get());
unpack_img_pixels_map_[Stream::RIGHT](
data, request, right_data.frame.get());
2018-05-31 05:20:48 +03:00
pushed = true;
2018-04-09 04:24:08 +03:00
} else {
2018-04-11 12:08:24 +03:00
// discard left
DiscardStreamData(Stream::LEFT);
2018-04-09 04:24:08 +03:00
LOG(WARNING) << "Image packet is unaccepted, frame dropped";
2018-05-31 05:20:48 +03:00
pushed = false;
2018-04-09 04:24:08 +03:00
}
2018-04-08 17:35:49 +03:00
} break;
default:
LOG(FATAL) << "Not supported " << capability << " now";
}
if (HasKeyStreamDatas())
cv_.notify_one();
2018-05-31 05:20:48 +03:00
return pushed;
2018-04-08 17:35:49 +03:00
}
void Streams::WaitForStreams() {
std::unique_lock<std::mutex> lock(mtx_);
auto ready = std::bind(&Streams::HasKeyStreamDatas, this);
if (!ready() && !cv_.wait_for(lock, std::chrono::seconds(2), ready)) {
2018-05-30 07:31:54 +03:00
LOG(FATAL) << "Timeout waiting for key frames. Please use USB 3.0, and not "
"in virtual machine.";
2018-04-08 17:35:49 +03:00
}
}
2018-04-12 04:54:54 +03:00
void Streams::ConfigStreamLimits(
const Stream &stream, std::size_t max_data_size) {
CHECK_GT(max_data_size, 0);
stream_limits_map_[stream] = max_data_size;
}
std::size_t Streams::GetStreamDataMaxSize(const Stream &stream) const {
try {
return stream_limits_map_.at(stream);
} catch (const std::out_of_range &e) {
return 4; // default stream data max size
}
}
2018-04-08 17:35:49 +03:00
Streams::stream_datas_t Streams::GetStreamDatas(const Stream &stream) {
2018-04-08 20:20:00 +03:00
std::unique_lock<std::mutex> lock(mtx_);
2018-05-31 18:41:32 +03:00
if (!HasStreamDatas(stream)) {
2018-05-30 17:15:50 +03:00
LOG(WARNING) << "There are no stream datas of " << stream
<< ". Did you call WaitForStreams() before this?";
2018-04-08 17:35:49 +03:00
return {};
}
2018-05-31 18:41:32 +03:00
auto datas = stream_datas_map_.at(stream);
2018-04-08 17:35:49 +03:00
stream_datas_map_[stream].clear();
return datas;
}
Streams::stream_data_t Streams::GetLatestStreamData(const Stream &stream) {
2018-05-31 05:20:48 +03:00
std::unique_lock<std::mutex> lock(mtx_);
2018-05-31 18:41:32 +03:00
if (!HasStreamDatas(stream)) {
2018-05-31 05:20:48 +03:00
LOG(WARNING) << "There are no stream datas of " << stream
<< ". Did you call WaitForStreams() before this?";
return {};
}
2018-05-31 18:41:32 +03:00
auto data = stream_datas_map_.at(stream).back();
2018-05-31 05:20:48 +03:00
stream_datas_map_[stream].clear();
2018-05-31 18:41:32 +03:00
return data;
2018-04-08 17:35:49 +03:00
}
2018-04-11 12:08:24 +03:00
const Streams::stream_datas_t &Streams::stream_datas(const Stream &stream) {
2018-05-31 05:20:48 +03:00
std::unique_lock<std::mutex> lock(mtx_);
2018-04-11 12:08:24 +03:00
try {
return stream_datas_map_.at(stream);
} catch (const std::out_of_range &e) {
// Add empty vector of this stream key
stream_datas_map_[stream] = {};
return stream_datas_map_.at(stream);
}
2018-04-08 07:23:33 +03:00
}
bool Streams::IsStreamCapability(const Capabilities &capability) const {
return std::find(
stream_capabilities_.begin(), stream_capabilities_.end(),
capability) != stream_capabilities_.end();
}
bool Streams::HasStreamConfigRequest(const Capabilities &capability) const {
return stream_config_requests_.find(capability) !=
stream_config_requests_.end();
}
const StreamRequest &Streams::GetStreamConfigRequest(
const Capabilities &capability) const {
return stream_config_requests_.at(capability);
}
bool Streams::HasStreamDatas(const Stream &stream) const {
2018-05-31 18:41:32 +03:00
return stream_datas_map_.find(stream) != stream_datas_map_.end() &&
!stream_datas_map_.at(stream).empty();
2018-04-08 07:23:33 +03:00
}
2018-04-08 17:35:49 +03:00
void Streams::AllocStreamData(
const Stream &stream, const StreamRequest &request) {
AllocStreamData(stream, request, request.format);
}
void Streams::AllocStreamData(
const Stream &stream, const StreamRequest &request, const Format &format) {
stream_data_t data;
2018-06-01 10:09:03 +03:00
2018-05-31 18:41:32 +03:00
if (HasStreamDatas(stream)) {
// If cached equal to limits_max, drop the oldest one.
if (stream_datas_map_.at(stream).size() == GetStreamDataMaxSize(stream)) {
auto &&datas = stream_datas_map_[stream];
2018-06-01 10:09:03 +03:00
// reuse the dropped data
data.img = datas.front().img;
data.frame = datas.front().frame;
2018-05-31 18:41:32 +03:00
datas.erase(datas.begin());
VLOG(2) << "Stream data of " << stream << " is dropped as out of limits";
}
}
2018-06-01 10:09:03 +03:00
if (stream == Stream::LEFT || stream == Stream::RIGHT) {
if(!data.img) {
data.img = std::make_shared<ImgData>();
}
} else {
data.img = nullptr;
}
2018-05-31 18:41:32 +03:00
if (!data.frame) {
data.frame = std::make_shared<frame_t>(
2018-07-08 13:49:09 +03:00
request.width / 2, request.height, format, nullptr);
2018-04-08 17:35:49 +03:00
}
2018-05-31 18:41:32 +03:00
stream_datas_map_[stream].push_back(data);
2018-04-08 17:35:49 +03:00
}
2018-04-11 12:08:24 +03:00
void Streams::DiscardStreamData(const Stream &stream) {
// Must discard after alloc, otherwise at will out of range when no this key.
if (stream_datas_map_.at(stream).size() > 0) {
auto &&datas = stream_datas_map_[stream];
2018-05-31 18:41:32 +03:00
datas.pop_back();
2018-04-11 12:08:24 +03:00
} else {
VLOG(2) << "Stream data of " << stream << " is empty, could not discard";
}
}
2018-04-08 17:35:49 +03:00
bool Streams::HasKeyStreamDatas() const {
for (auto &&s : key_streams_) {
if (!HasStreamDatas(s))
return false;
}
return true;
2018-04-08 07:23:33 +03:00
}
MYNTEYE_END_NAMESPACE