Improve lock stream datas

This commit is contained in:
John Zhao 2018-05-31 10:20:48 +08:00
parent b96d7a1782
commit 425b181216
4 changed files with 23 additions and 17 deletions

View File

@ -337,14 +337,12 @@ void Device::WaitForStreams() {
std::vector<device::StreamData> Device::GetStreamDatas(const Stream &stream) {
CHECK(video_streaming_);
CHECK_NOTNULL(streams_);
std::lock_guard<std::mutex> _(mtx_streams_);
return streams_->GetStreamDatas(stream);
}
device::StreamData Device::GetLatestStreamData(const Stream &stream) {
CHECK(video_streaming_);
CHECK_NOTNULL(streams_);
std::lock_guard<std::mutex> _(mtx_streams_);
return streams_->GetLatestStreamData(stream);
}
@ -410,17 +408,15 @@ void Device::StartVideoStreaming() {
--drop_count;
return;
}
std::lock_guard<std::mutex> _(mtx_streams_);
streams_->PushStream(Capabilities::STEREO, data);
if (HasStreamCallback(Stream::LEFT)) {
auto &&stream_datas = streams_->stream_datas(Stream::LEFT);
if (stream_datas.size() > 0) {
if (streams_->PushStream(Capabilities::STEREO, data)) {
if (HasStreamCallback(Stream::LEFT)) {
auto &&stream_datas = streams_->stream_datas(Stream::LEFT);
// if (stream_datas.size() > 0) {}
stream_callbacks_.at(Stream::LEFT)(stream_datas.back());
}
}
if (HasStreamCallback(Stream::RIGHT)) {
auto &&stream_datas = streams_->stream_datas(Stream::RIGHT);
if (stream_datas.size() > 0) {
if (HasStreamCallback(Stream::RIGHT)) {
auto &&stream_datas = streams_->stream_datas(Stream::RIGHT);
// if (stream_datas.size() > 0) {}
stream_callbacks_.at(Stream::RIGHT)(stream_datas.back());
}
}

View File

@ -18,7 +18,6 @@
#include <limits>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
@ -261,8 +260,6 @@ class MYNTEYE_API Device {
std::map<Capabilities, StreamRequest> stream_config_requests_;
std::mutex mtx_streams_;
std::shared_ptr<Channels> channels_;
std::shared_ptr<Motions> motions_;

View File

@ -132,12 +132,13 @@ void Streams::ConfigStream(
stream_config_requests_[capability] = request;
}
void Streams::PushStream(const Capabilities &capability, const void *data) {
bool Streams::PushStream(const Capabilities &capability, const void *data) {
if (!HasStreamConfigRequest(capability)) {
LOG(FATAL) << "Cannot push stream without stream config request";
}
std::unique_lock<std::mutex> lock(mtx_);
auto &&request = GetStreamConfigRequest(capability);
bool pushed = false;
switch (capability) {
case Capabilities::STEREO: {
// alloc left
@ -155,10 +156,12 @@ void Streams::PushStream(const Capabilities &capability, const void *data) {
data, request, left_data.frame.get());
unpack_img_pixels_map_[Stream::RIGHT](
data, request, right_data.frame.get());
pushed = true;
} else {
// discard left
DiscardStreamData(Stream::LEFT);
LOG(WARNING) << "Image packet is unaccepted, frame dropped";
pushed = false;
}
} break;
default:
@ -166,6 +169,7 @@ void Streams::PushStream(const Capabilities &capability, const void *data) {
}
if (HasKeyStreamDatas())
cv_.notify_one();
return pushed;
}
void Streams::WaitForStreams() {
@ -204,10 +208,19 @@ Streams::stream_datas_t Streams::GetStreamDatas(const Stream &stream) {
}
Streams::stream_data_t Streams::GetLatestStreamData(const Stream &stream) {
return GetStreamDatas(stream).back();
std::unique_lock<std::mutex> lock(mtx_);
if (!HasStreamDatas(stream) || stream_datas_map_.at(stream).empty()) {
LOG(WARNING) << "There are no stream datas of " << stream
<< ". Did you call WaitForStreams() before this?";
return {};
}
stream_datas_t datas = stream_datas_map_.at(stream);
stream_datas_map_[stream].clear();
return datas.back();
}
const Streams::stream_datas_t &Streams::stream_datas(const Stream &stream) {
std::unique_lock<std::mutex> lock(mtx_);
try {
return stream_datas_map_.at(stream);
} catch (const std::out_of_range &e) {

View File

@ -44,7 +44,7 @@ class Streams {
void ConfigStream(
const Capabilities &capability, const StreamRequest &request);
void PushStream(const Capabilities &capability, const void *data);
bool PushStream(const Capabilities &capability, const void *data);
void WaitForStreams();