SLProject  4.3.020
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SENSSimulatedCamera Class Reference

#include <SENSSimulated.h>

Inheritance diagram for SENSSimulatedCamera:
[legend]

Public Member Functions

 ~SENSSimulatedCamera ()
 
const SENSCameraConfigstart (std::string deviceId, const SENSCameraStreamConfig &streamConfig, bool provideIntrinsics=true) override
 
void stop () override
 Stop a started camera device. More...
 
const SENSCapturePropscaptureProperties () override
 Get SENSCaptureProps which contains necessary information about all available camera devices and their capabilities. More...
 
- Public Member Functions inherited from SENSBaseCamera
SENSFrameBasePtr latestFrame () override
 Get the latest captured frame. If no frame was captured the frame will be empty (null). More...
 
const SENSCameraConfigconfig () const override
 defines how the camera was configured during start More...
 
void registerListener (SENSCameraListener *listener) override
 
void unregisterListener (SENSCameraListener *listener) override
 
bool started () const override
 
bool permissionGranted () const override
 
void setPermissionGranted () override
 
- Public Member Functions inherited from SENSCamera
virtual ~SENSCamera ()
 
- Public Member Functions inherited from SENSSimulated< int >
virtual ~SENSSimulated ()
 
- Public Member Functions inherited from SENSSimulatedBase
virtual ~SENSSimulatedBase ()
 

Private Member Functions

 SENSSimulatedCamera (StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, int >> &&data, std::string videoFileName, SENSCameraConfig cameraConfig, const SENSSimClock &clock)
 
void feedSensorData (const int counter) override
 feed new sensor data to sensor More...
 
void prepareSensorData (const int counter) override
 
void onLatencyProblem (int &counter) override
 when feeding camera frames from cv::videocapture updating the frame pos may take ages. So we skip one more frame and update the next frame counter More...
 

Private Attributes

std::string _videoFileName
 
cv::VideoCapture _cap
 
cv::Mat _preparedFrame
 
int _preparedFrameIndex = -1
 
cv::Mat _frame
 
std::mutex _frameMutex
 

Friends

class SENSSimulator
 

Additional Inherited Members

- Protected Types inherited from SENSSimulated< int >
using StartSimCB = std::function< void(void)>
 
using SensorSimStoppedCB = std::function< void(void)>
 
- Protected Member Functions inherited from SENSBaseCamera
void updateFrame (cv::Mat bgrImg, cv::Mat intrinsics, int width, int height, bool intrinsicsChanged)
 
void processStart ()
 call from start function to do startup preprocessing More...
 
- Protected Member Functions inherited from SENSSimulated< int >
 SENSSimulated (const std::string name, StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, int >> &&data, const SENSSimClock &clock)
 
void startSim ()
 start the sensor simulation thread More...
 
void stopSim ()
 stop the sensor simulation thread More...
 
bool getErrorMsg (std::string &msg) override
 get error msg (valid if function returns true) More...
 
bool isThreadRunning () const override
 indicates if simulation thread is running More...
 
- Protected Attributes inherited from SENSBaseCamera
SENSCaptureProps _captureProperties
 
std::atomic< bool > _started {false}
 flags if camera was started More...
 
SENSCameraConfig _config
 indicates what is currently running More...
 
std::atomic< bool > _permissionGranted {false}
 
std::vector< SENSCameraListener * > _listeners
 
std::mutex _listenerMutex
 
SENSFrameBasePtr _frame
 current frame More...
 
std::mutex _frameMutex
 
- Protected Attributes inherited from SENSSimulated< int >
std::vector< std::pair< SENSTimePt, int > > _data
 
SensorSimStoppedCB _sensorSimStoppedCB
 
StartSimCB _startSimCB
 
std::thread _thread
 
std::condition_variable _condVar
 
std::mutex _mutex
 
bool _stop
 
SENSTimePt _commonSimStartTimePt
 
bool _threadIsRunning
 
const SENSSimClock_clock
 
std::string _name
 
std::mutex _msgMutex
 
std::string _errorMsg
 

Detailed Description

SENSSimulatedCamera SENSSimulated implementation for Camera sensor simulation. Implements the SENSBaseCamera interface to make it a full camera sensor and the SENSSimulated interface for the sensor simulation backend.

Definition at line 158 of file SENSSimulated.h.

Constructor & Destructor Documentation

◆ ~SENSSimulatedCamera()

SENSSimulatedCamera::~SENSSimulatedCamera ( )

Definition at line 234 of file SENSSimulated.cpp.

235 {
236  stop();
237 }
void stop() override
Stop a started camera device.

◆ SENSSimulatedCamera()

SENSSimulatedCamera::SENSSimulatedCamera ( StartSimCB  startSimCB,
SensorSimStoppedCB  sensorSimStoppedCB,
std::vector< std::pair< SENSTimePt, int >> &&  data,
std::string  videoFileName,
SENSCameraConfig  cameraConfig,
const SENSSimClock clock 
)
private

Definition at line 292 of file SENSSimulated.cpp.

298  : SENSSimulated("camera", startSimCB, sensorSimStoppedCB, std::move(data), clock),
299  _videoFileName(videoFileName)
300 {
301  _config = cameraConfig;
302  _permissionGranted = true;
303 }
SENSCameraConfig _config
indicates what is currently running
Definition: SENSCamera.h:218
std::atomic< bool > _permissionGranted
Definition: SENSCamera.h:219
std::string _videoFileName
SENSSimulated(const std::string name, StartSimCB startSimCB, SensorSimStoppedCB sensorSimStoppedCB, std::vector< std::pair< SENSTimePt, int >> &&data, const SENSSimClock &clock)

Member Function Documentation

◆ captureProperties()

const SENSCaptureProps & SENSSimulatedCamera::captureProperties ( )
overridevirtual

Get SENSCaptureProps which contains necessary information about all available camera devices and their capabilities.

Implements SENSCamera.

Definition at line 280 of file SENSSimulated.cpp.

281 {
282  if (!_captureProperties.size())
283  {
286  _captureProperties.push_back(characteristics);
287  }
288 
289  return _captureProperties;
290 }
SENSCaptureProps _captureProperties
Definition: SENSCamera.h:216
SENSCameraFacing facing
camera facing
Definition: SENSCamera.h:133
SENSCameraStreamConfig streamConfig
currently selected stream config index (use it to look up original capture size)
Definition: SENSCamera.h:131
std::string deviceId
Definition: SENSCamera.h:130

◆ feedSensorData()

void SENSSimulatedCamera::feedSensorData ( const int  counter)
overrideprivatevirtual

feed new sensor data to sensor

Implements SENSSimulated< int >.

Definition at line 305 of file SENSSimulated.cpp.

306 {
307  HighResTimer t;
308  //prepare if not yet prepared (e.g. when while loop writes multiple lines)
309  if (_data[counter].second != _preparedFrameIndex)
310  {
311  SENS_DEBUG("preparing in feedSensorData");
312  prepareSensorData(counter);
313  }
314 
315  updateFrame(_preparedFrame, cv::Mat(), false, _preparedFrame.size().width, _preparedFrame.size().height);
316 
317  SENS_DEBUG("feedSensorData %lld us", t.elapsedTimeInMicroSec());
318 }
#define SENS_DEBUG(...)
Definition: SENS.h:60
High Resolution Timer class using C++11.
Definition: HighResTimer.h:31
int64_t elapsedTimeInMicroSec()
Definition: HighResTimer.h:39
void updateFrame(cv::Mat bgrImg, cv::Mat intrinsics, int width, int height, bool intrinsicsChanged)
Definition: SENSCamera.cpp:220
void prepareSensorData(const int counter) override
std::vector< std::pair< SENSTimePt, int > > _data
Definition: SENSSimulated.h:79

◆ onLatencyProblem()

void SENSSimulatedCamera::onLatencyProblem ( int &  counter)
overrideprivatevirtual

when feeding camera frames from cv::videocapture updating the frame pos may take ages. So we skip one more frame and update the next frame counter

Reimplemented from SENSSimulated< int >.

Definition at line 351 of file SENSSimulated.cpp.

352 {
353  //Setting the next frame position may take a lot of time.
354  //Thats why we need this special treatment for camera feed.
355 
356  //update the frame pos with current counter and measure needed time
357  SENSTimePt t0 = SENSClock::now();
358  prepareSensorData(counter);
359  SENSTimePt t1 = SENSClock::now();
360  SENSMicroseconds neededTime = std::chrono::duration_cast<SENSMicroseconds>(t1 - t0);
361 
362  //SENS_DEBUG("update CAP_PROP_POS_FRAMES %d: %lld", counter, t.elapsedTimeInMicroSec());
363  counter++;
364  SENSTimePt now = _clock.now();
365  while (counter < _data.size() - 1 && _data[counter].first < now)
366  {
367  //when we enter this loop the frame the neededTime for pos update took longer than the availableTime between too values.
368  //we measure how much longer and update the counter accordingly
369  SENSMicroseconds availableTime = std::chrono::duration_cast<SENSMicroseconds>(_data[counter + 1].first - _data[counter].first);
370  auto factor = neededTime.count() / availableTime.count();
371  factor++;
372  SENS_DEBUG("factor %lld neededTime %lld available time %lld", factor, neededTime.count(), availableTime.count());
373  counter += factor;
374 
375  if (counter < _data.size())
376  {
377  t0 = SENSClock::now();
378  prepareSensorData(counter);
379  t1 = SENSClock::now();
380  neededTime = std::chrono::duration_cast<SENSMicroseconds>(t1 - t0);
381  }
382 
383  now = _clock.now();
384  }
385 }
std::chrono::microseconds SENSMicroseconds
Definition: SENS.h:58
std::chrono::high_resolution_clock::time_point SENSTimePt
Definition: SENS.h:57
SENSTimePt now() const
get current simulation time (const function which should be thread save)
Definition: SENSSimClock.h:71
const SENSSimClock & _clock
Definition: SENSSimulated.h:94

◆ prepareSensorData()

void SENSSimulatedCamera::prepareSensorData ( const int  counter)
overrideprivatevirtual

prepare things that may take some time for the next writing of sensor data (e.g. for video reading we can already read the frame and do decoding and maybe it also blocks for some reasons..)

Reimplemented from SENSSimulated< int >.

Definition at line 320 of file SENSSimulated.cpp.

321 {
322  if (counter >= _data.size())
323  return;
324 
325  int frameIndex = _data[counter].second;
326  if (frameIndex == _preparedFrameIndex)
327  return;
328 
329  HighResTimer t;
330 
331  int nextFramePos = _cap.get(cv::CAP_PROP_POS_FRAMES);
332  if (nextFramePos != frameIndex)
333  {
334  SENS_DEBUG("updating frame pos");
335  _cap.set(cv::CAP_PROP_POS_FRAMES, frameIndex);
336  }
337 
338  cv::Mat frame;
339 
340  _cap.read(frame);
341 
342  {
343  _preparedFrameIndex = frameIndex;
344  std::lock_guard<std::mutex> lock(_frameMutex);
345  _preparedFrame = frame;
346  }
347 
348  SENS_DEBUG("prepared index %d: %lld", frameIndex, t.elapsedTimeInMicroSec());
349 }
std::mutex _frameMutex
cv::VideoCapture _cap

◆ start()

const SENSCameraConfig & SENSSimulatedCamera::start ( std::string  deviceId,
const SENSCameraStreamConfig streamConfig,
bool  provideIntrinsics = true 
)
overridevirtual

Call this function to configure the camera if you exactly know, what device you want to open and with which stream configuration. You can find out these properties by using SENSCaptureProps object (see getCaptureProperties())

Parameters
deviceIdcamera device id to start
streamConfigSENSCameraStreamConfig from camera device. This defines the size of the image. The image is converted to BGR and assigned to SENSFrame::imgBGR.
provideIntrinsicsspecifies if intrinsics estimation should be enabled. The estimated intrinsics are transferred with every SENSFrame as they may be different for every frame (e.g. on iOS). This value has different effects on different architectures.
Returns
the found configuration that is adjusted and used when SENSCamera::start() is called.

Implements SENSCamera.

Definition at line 239 of file SENSSimulated.cpp.

242 {
243  if (_started)
244  {
245  Utils::warnMsg("SENSWebCamera", "Call to start was ignored. Camera is currently running!", __LINE__, __FILE__);
246  return _config;
247  }
248 
249  if (!_cap.isOpened())
250  {
251  if (!_cap.open(_videoFileName))
252  throw SENSException(SENSType::CAM, "Could not open camera simulator for filename: " + _videoFileName, __LINE__, __FILE__);
253  }
254 
255  //retrieve all camera characteristics
256  if (_captureProperties.size() == 0)
258 
259  //CONFIG SHOULD HAVE BEEN LOADED FROM FILE
260 
261  processStart();
262 
263  //start the sensor simulation
264  startSim();
265 
266  _started = true;
267 
268  return _config;
269 }
std::atomic< bool > _started
flags if camera was started
Definition: SENSCamera.h:217
void processStart()
call from start function to do startup preprocessing
Definition: SENSCamera.cpp:294
const SENSCaptureProps & captureProperties() override
Get SENSCaptureProps which contains necessary information about all available camera devices and thei...
void startSim()
start the sensor simulation thread
void warnMsg(const char *tag, const char *msg, const int line, const char *file)
Platform independent warn message output.
Definition: Utils.cpp:1142

◆ stop()

void SENSSimulatedCamera::stop ( )
overridevirtual

Stop a started camera device.

Implements SENSCamera.

Definition at line 271 of file SENSSimulated.cpp.

272 {
273  if (_started)
274  {
275  stopSim();
276  _started = false;
277  }
278 }
void stopSim()
stop the sensor simulation thread

Friends And Related Function Documentation

◆ SENSSimulator

friend class SENSSimulator
friend

Definition at line 161 of file SENSSimulated.h.

Member Data Documentation

◆ _cap

cv::VideoCapture SENSSimulatedCamera::_cap
private

Definition at line 188 of file SENSSimulated.h.

◆ _frame

cv::Mat SENSSimulatedCamera::_frame
private

Definition at line 192 of file SENSSimulated.h.

◆ _frameMutex

std::mutex SENSSimulatedCamera::_frameMutex
private

Definition at line 193 of file SENSSimulated.h.

◆ _preparedFrame

cv::Mat SENSSimulatedCamera::_preparedFrame
private

Definition at line 190 of file SENSSimulated.h.

◆ _preparedFrameIndex

int SENSSimulatedCamera::_preparedFrameIndex = -1
private

Definition at line 191 of file SENSSimulated.h.

◆ _videoFileName

std::string SENSSimulatedCamera::_videoFileName
private

Definition at line 187 of file SENSSimulated.h.


The documentation for this class was generated from the following files: