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

View File

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

View File

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

View File

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