AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
agxData/Track.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXDATA_TRACK_H
18#define AGXDATA_TRACK_H
19
20#include <agx/Component.h>
21#include <agx/Clock.h>
22#include <agxData/Frame.h>
23#include <agxData/FrameIO.h>
24#include <agx/List.h>
25#include <agx/Thread.h>
26
27#include <atomic>
28#include <condition_variable>
29#include <mutex>
30
31
32namespace agxData
33{
36 {
37 public:
39
43
44 public:
48
49 public:
51
52 public:
53 Track(const agx::Name& name = "Track");
54 Track(FrameReader *input, FrameWriter *output);
55
59 void setInput(FrameReader *input);
60
64 FrameReader *getInput();
65 const FrameReader *getInput() const;
66
70 void setOutput(FrameWriter *output);
71
72
78 void start();
79
83 void stop();
84
88 // const FrameWriterRefVector& getOutputs() const;
89 FrameWriter *getOutput();
90 const FrameWriter *getOutput() const;
91
96
101
105 void setMaxNumBytes(agx::UInt numBytes);
106
111
116
121
125 agx::Real getDataRate() const;
126
132
133
139
143 agx::UInt getAccumulatedNumBytes() const;
144
148 agx::UInt getAccumulatedNumFrames() const;
149
154 void flush(bool force = false);
155
164 bool isQueueEmpty() const;
165
169 bool isEOF() const;
170
174 bool isInputEOF() const;
175
179 bool isRunning() const;
180
184 void setEnableLooping(bool flag);
185
189 bool getEnableLooping() const;
190
194 void setExpandFrames(bool flag);
195
199 bool getExpandFrames() const;
200
208 bool jumpToFrame(agx::UInt frameIndex);
210
214 bool hasJumped() const;
215
220
226
230 agx::UInt getFrameStride() const;
231
232
235
238
242 void loadCache(Track *other);
243
244
245 // Should only be called by the input
246 void pushFrame(Frame *frame);
247
248 // Should only be called by the output.
249 FrameRef popFrame(bool blocking = true);
250
251 protected:
252 virtual ~Track();
253
254 private:
255 // friend class Thread;
256
257 // The mutex must be held when calling this method.
258 FrameRef _popFrame();
259 void finalizePop(Frame *frame);
260 void flush(bool force, bool needLock);
261
262 void signalEOF();
263 void resetEOF();
264 void updateDataRate(Frame *frame);
265
266 // Waits on the hasFrame condition variable until the frame buffer has
267 // frames or the condition has been forced open.
268 //
269 // \return True if there is a frame in the queue and the condition
270 // variable hasn't been forced open. False if the condition variable
271 // has been forced open.
272 bool waitForFrame();
273
274 // Waits on the hasFreeSlots condition variable until the frame buffer has
275 // free slots of the condition has been forced open.
276 //
277 // \return True if there is a free slot in the queue and the condition
278 // variable hasn't been force open. False if the condition variable has been
279 // forced open.
280 bool waitForFreeSlot();
281
282 void clearQueue();
283 void _setFrameStride(agx::UInt stride);
284
285 private:
287 typedef std::pair<FrameRef, FrameList::iterator> FrameEntry;
289
290
291 class FrameCache
292 {
293 public:
294 FrameCache();
295 FrameCache(const FrameCache& other);
296
297 FrameCache& operator= (const FrameCache& other);
298
299 void addFrame(Frame *frame);
300 Frame *getFrame(agx::UInt frameIndex);
301 bool evictFrame();
302 void evictFrame(Frame *frame);
303
304 agx::Real getHitRate() const;
305 void clear();
306
307 bool isFull() const;
308
309 agx::UInt getNumFrames() const;
310 agx::UInt getNumBytes() const;
311
312 void setMaxNumBytes(agx::UInt numBytes);
313
314 agx::UInt getMaxNumBytes() const;
315
316 private:
317 FrameList m_frameList;
318 FrameTable m_frameTable;
319
320 agx::UInt m_numRequests;
321 agx::UInt m_numHits;
322 agx::UInt m_numBytes;
323 agx::UInt m_maxNumBytes;
324 };
325
326
327 FrameCache m_frameCache;
328 FrameList m_frameQueue;
329
330 FrameReaderRef m_input;
331 FrameWriterRef m_output;
332
333 ExceptionHandler m_exceptionHandler;
334
335 mutable std::mutex m_mutex;
336 std::mutex m_outputVectorMutex;
337 std::mutex m_jumpMutex; // Locked when jumping between frames is forbidden.
338
339 std::condition_variable_any m_hasFramesCondition;
340 std::atomic<bool> m_hasFramesForceOpen;
341 std::condition_variable_any m_hasFreeSlotsCondition;
342 std::atomic<bool> m_hasFreeSlotsForceOpen;
343
344 InputThreadRef m_inputThread;
345 OutputThreadRef m_outputThread;
346
347 agx::UInt m_numBytes;
348 agx::UInt m_accumulatedNumBytes;
349 agx::UInt m_accumulatedNumFrames;
350 bool m_inputEOF;
351 bool m_enableLooping;
352 bool m_hasJumped;
353 bool m_isRunning;
354 agx::RealModeEnum m_realMode;
355 agx::Real m_dataRate;
356 agx::UInt m_dataRateCounter;
357 agx::Timer m_dataRateTimer;
358 agx::UInt m_frameStride;
359 agx::Real m_activeTimeStart;
360 agx::Real m_activeTimeEnd;
361
362 ThreadRef m_exceptionThread;
363
364 bool m_expandFrames;
365 FrameRef m_expandedFrame;
366 };
367
368
370 {
371 public:
372 Thread(Track *track, agx::Callback entryPoint, const agx::Name& = "Thread");
373
377 void start(bool activate = true);
378
382 void stop();
383
387 bool isRunning() const;
388
395 void activate();
396
401
405 bool isActive() const;
406
407 Track *getTrack() { return m_track; }
408
409 protected:
410 virtual ~Thread();
411
412 private:
413 virtual void run(); // Do not override this
414
415 private:
416 Track *m_track;
417 agx::Callback m_callback;
418 std::atomic<bool> m_running;
419 std::atomic<bool> m_active;
420 agx::Block m_startBlock;
421 agx::Block m_activationBlock;
422 };
423
425 {
426 public:
428
429 protected:
430 virtual ~InputThread();
431
432 private:
433 void execute();
434 // Frame *readFrame();
435 };
436
438 {
439 public:
441
442 protected:
443 virtual ~OutputThread();
444
445
446 private:
447 void execute();
448 };
449
450
451 /* Implementation */
452
454 AGX_FORCE_INLINE const FrameReader *Track::getInput() const { return m_input; }
455 // AGX_FORCE_INLINE const FrameWriterRefVector& Track::getOutputs() const { return m_outputs; }
457 AGX_FORCE_INLINE const FrameWriter *Track::getOutput() const { return m_output; }
458
459 AGX_FORCE_INLINE bool Track::isInputEOF() const { return m_inputEOF; }
460 AGX_FORCE_INLINE bool Track::isEOF() const { return m_output->isEOF(); }
461 AGX_FORCE_INLINE bool Track::getEnableLooping() const { return m_enableLooping; }
462
463 AGX_FORCE_INLINE agx::UInt Track::getFrameStride() const { return m_frameStride; }
464 AGX_FORCE_INLINE bool Track::hasJumped() const { return m_hasJumped; }
465
466
467 AGX_FORCE_INLINE agx::UInt Track::getAccumulatedNumBytes() const { return m_accumulatedNumBytes; }
468 AGX_FORCE_INLINE agx::UInt Track::getAccumulatedNumFrames() const { return m_accumulatedNumFrames; }
469
471 {
472 std::lock_guard<std::mutex> lock(m_mutex);
473 return m_dataRate;
474 }
475
476 AGX_FORCE_INLINE bool Track::Thread::isRunning() const { return m_running; }
477 AGX_FORCE_INLINE bool Track::Thread::isActive() const { return m_active; }
478
479}
480
481
482#endif /* AGXDATA_TRACK_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXCORE_EXPORT
The FrameReader reads frames from an external source.
Definition: FrameIO.h:127
The FrameWriter writes frames to an external target.
Definition: FrameIO.h:169
Thread(Track *track, agx::Callback entryPoint, const agx::Name &="Thread")
void deactivate()
Deactivate the thread.
void start(bool activate=true)
Start the thread, and optionally activate it.
void stop()
Stop the thread.
void activate()
Activate the thread.
void loadCache(Track *other)
Load cached frames from another track.
agx::Real getActiveTimeRangeStart() const
agx::RealModeEnum getRealMode() const
agx::UInt getFrameStride() const
Track(const agx::Name &name="Track")
void pushFrame(Frame *frame)
agx::UInt getNumCachedBytes() const
void setOutput(FrameWriter *output)
Specify the output stream.
bool isInputEOF() const
agx::UInt getAccumulatedNumFrames() const
void flush(bool force=false)
Flush all frames to the registered outputs.
FrameRef popFrame(bool blocking=true)
static agx::Model * ClassModel()
bool jumpToTime(agx::Real time)
bool getExpandFrames() const
FrameWriter * getOutput()
bool isQueueEmpty() const
Determine if the track is empty.
agx::Real setFrameFrequency(agx::Real frequency)
Set the frame stride using a frequency, which is converted to a stride.
void setFrameStride(agx::UInt stride)
Set frame stride.
void start()
Start the track, producing frames from the input and consuming frames at the outputs.
bool jumpToFrame(agx::UInt frameIndex)
Select which frame that should be returned by the next call to 'readFrame'.
agx::Real getDataRate() const
agx::UInt getNumCachedFrames() const
agx::UInt getNumQueuedFrames() const
void setInput(FrameReader *input)
Set the input stream.
bool hasJumped() const
FrameEvent pushFrameEvent
Definition: agxData/Track.h:46
agx::Real getActiveTimeRangeEnd() const
bool isEOF() const
void setEnableLooping(bool flag)
Set to true to enable looping from the input.
void stop()
Stop frame production.
agx::Event1< Frame * > FrameEvent
Definition: agxData/Track.h:45
agx::UInt getNumQueuedBytes() const
FrameEvent popFrameEvent
Definition: agxData/Track.h:47
void setActiveTimeRangeStart(agx::Real time)
void setActiveTimeRangeEnd(agx::Real time)
agx::UInt getAccumulatedNumBytes() const
FrameReader * getInput()
virtual ~Track()
agx::UInt getMaxNumBytes() const
void setRealMode(agx::RealModeEnum mode)
Set the real mode.
agx::Callback2< Track *, std::exception & > ExceptionHandler
Definition: agxData/Track.h:50
void setExceptionHandler(const ExceptionHandler &handler)
Specify an exception handler.
void setExpandFrames(bool flag)
Set to true to expand all partial frames to keyframes.
Track(FrameReader *input, FrameWriter *output)
const ExceptionHandler & getExceptionHandler() const
bool isRunning() const
void setMaxNumBytes(agx::UInt numBytes)
Set the maximum number of bytes in the frame cache.
bool getEnableLooping() const
Basic wrapper class aroud std::thread.
Definition: Thread.h:83
Block synchronization primitive.
Templated callback with two arguments.
Definition: Callback.h:140
Generalized callback, using std::function.
Definition: Callback.h:54
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:39
An event with one argument.
Definition: Event.h:103
Inheritance with partial specialization due to bug with ref_ptr containers.
A model is an abstract representation of the class of an agx::Object.
Definition: Model.h:41
Representation of a name string.
Definition: Name.h:33
agx::Object is a refcounted object with a name.
Definition: Object.h:59
The Timer class permits timing execution speed with the same refinement as the built in hardware cloc...
Definition: Timer.h:62
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contains classes for low level data storage for AGX.
Definition: Container.h:23
RealModeEnum
The data type for basic floating point representation.
Definition: Real.h:35
uint64_t UInt
Definition: Integer.h:27
double Real
Definition: Real.h:41