summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2020-03-29 15:36:16 +0200
committerStanislaw Halik <sthalik@misaki.pl>2020-03-29 15:36:43 +0200
commit3d20cc94beaa114a4ae8ab63e042ca907471d819 (patch)
tree222861f373c4f231719f883b17c81fafb1c5f847
parentfafe2e19d9f411f892fb9a162f03e6a6ce381f5a (diff)
video/ps3eye: sleep the right amount
-rw-r--r--video-ps3eye/module.cpp36
-rw-r--r--video-ps3eye/module.hpp4
2 files changed, 25 insertions, 15 deletions
diff --git a/video-ps3eye/module.cpp b/video-ps3eye/module.cpp
index db52f8ae..caed2fd7 100644
--- a/video-ps3eye/module.cpp
+++ b/video-ps3eye/module.cpp
@@ -158,6 +158,8 @@ bool ps3eye_camera::start(info& args)
ptr.in.gain = (uint8_t)s.gain;
ptr.in.exposure = (uint8_t)s.exposure;
+ sleep_ms = (int)std::ceil(1000./std::max(1, (int)ptr.in.framerate));
+
wrapper.start();
constexpr int sleep_ms = 10, max_sleeps = 5000/sleep_ms;
@@ -189,34 +191,40 @@ ok:
std::tuple<const frame&, bool> ps3eye_camera::get_frame()
{
- volatile auto& ptr = *(ps3eye::shm*)shm.ptr();
- constexpr int sleep_ms = 2;
- constexpr int max_sleeps = 2000/sleep_ms;
+ auto volatile* ptr = (ps3eye::shm*)shm.ptr();
- if (!open)
- goto fail;
+ if (shm.success() && open)
+ {
+ int elapsed = std::min((int)std::ceil(t.elapsed_ms()), 100);
+ portable::sleep(sleep_ms - elapsed);
- for (int i = 0; i < max_sleeps; i++)
+ if (unsigned tc = ptr->out.timecode; tc != timecode)
+ {
+ timecode = tc;
+ goto ok;
+ }
+ }
+
+ for (int i = 0; i < 100; i++)
{
- unsigned new_timecode = ptr.out.timecode;
- if (timecode != new_timecode)
+ if (unsigned tc = ptr->out.timecode; tc != timecode)
{
- timecode = new_timecode;
+ timecode = tc;
goto ok;
}
- portable::sleep(sleep_ms);
+ portable::sleep(1);
}
-fail:
stop();
return { fr, false };
- static_assert(offsetof(decltype(ptr.out), data_640x480) == offsetof(decltype(ptr.out), data_320x240));
+ static_assert(offsetof(decltype(ptr->out), data_640x480) == offsetof(decltype(ptr->out), data_320x240));
ok:
- memcpy(data, (unsigned char*)ptr.out.data_640x480,sizeof(ptr.out.data_640x480));
+ t.start();
+ memcpy(data, (unsigned char*)ptr->out.data_640x480,sizeof(ptr->out.data_640x480));
fr.data = data;
- return { fr, true};
+ return { fr, true };
}
bool ps3eye_camera::show_dialog()
diff --git a/video-ps3eye/module.hpp b/video-ps3eye/module.hpp
index 9f7aaec1..56ffe5df 100644
--- a/video-ps3eye/module.hpp
+++ b/video-ps3eye/module.hpp
@@ -5,6 +5,7 @@
#include "compat/shm.h"
#include "options/options.hpp"
#include "compat/macros1.h"
+#include "compat/timer.hpp"
#include "ui_dialog.h"
#include <QDialog>
@@ -52,8 +53,9 @@ struct ps3eye_camera final : video::impl::camera
shm_wrapper shm { "ps3eye-driver-shm", nullptr, sizeof(ps3eye::shm) };
settings s;
frame fr;
+ Timer t;
unsigned char data[640 * 480 * 3] = {};
- int framerate = 30;
+ int framerate = 30, sleep_ms = 1;
bool open = false;
unsigned timecode = 0;