Valkka  1.6.1
OpenSource Video Management
cachestream.h
Go to the documentation of this file.
1 #ifndef cachestream_HEADER_GUARD
2 #define cachestream_HEADER_GUARD
3 /*
4  * cachestream.h :
5  *
6  * (c) Copyright 2017-2024 Sampsa Riikonen
7  *
8  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
9  *
10  * This file is part of the Valkka library.
11  *
12  * Valkka is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License as
14  * published by the Free Software Foundation, either version 3 of the
15  * License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>
24  *
25  */
26 
36 #include "framefifo.h"
37 #include "filestream.h"
38 #include "Python.h"
39 
40 /* 02 03 04 05 06 07 08 09 10 11 12 13
41  * a a b A a b c C c c B A
42  *
43  * seek to 10 : <---- .. key frame for C, A found, but not for B .. if this is from blocks, ok
44  *
45  * better idea: start iterating from the very beginning, save (overwrite) frame pointers during progress
46  *
47  * Block request:
48  * when to request more blocks / frames ? .. when close enough to maxtime
49  *
50  *
51  * 1 2 3
52  * 2 3 4
53  *
54  */
55 
56 struct FileStreamContext { // <pyapi>
57  FileStreamContext(SlotNumber slot, FrameFilter *framefilter) : // <pyapi>
58  slot(slot), framefilter(framefilter) // <pyapi>
59  { // <pyapi>
60  } // <pyapi>
61  FileStreamContext() {} // <pyapi>
62  SlotNumber slot; // <pyapi>
63  FrameFilter *framefilter; // <pyapi>
64 }; // <pyapi>
65 
70 {
74  long int mstimestamp;
76  bool clear;
77 };
78 
82 enum class FileCacheSignal
83 {
84  none,
85  exit,
86  register_stream,
87  deregister_stream,
88  play_streams,
89  stop_streams,
90  clear,
91  seek_streams,
92  clear_streams,
93  report_cache
94 };
95 
101 {
102  FileCacheSignal signal;
103  FileCacheSignalPars pars;
104 };
105 
107 { // nothing here yet ..
108  int nada;
109 };
110 
116 {
117 
118 public:
119  FrameCache(const char *name, FrameCacheContext ctx = FrameCacheContext());
120  ~FrameCache();
121  ban_copy_ctor(FrameCache);
122  ban_copy_asm(FrameCache);
123 
124 protected:
125  std::string name;
127  long int mintime_;
128  long int maxtime_;
130 
131 protected:
132  Cache cache;
133  Cache::iterator state;
134 
135 protected: // mutex synchro
136  std::mutex mutex;
137  std::condition_variable condition;
138  std::condition_variable ready_condition;
139 
140 public: // getters
141  long int getMinTime_() { return this->mintime_; }
142  long int getMaxTime_() { return this->maxtime_; }
143  bool hasDeltaFrames() { return this->has_delta_frames; }
144 
145 protected:
146  void dump_();
147  void clear_();
148 
149 public:
150  virtual bool writeCopy(Frame *f, bool wait = false);
151  // virtual Frame* read(unsigned short int mstimeout=0); ///< Pop a frame from the end of the fifo and return the frame to the reservoir stack
152  void clear();
153  void dump();
154  bool isEmpty();
155  int seek(long int ms_streamtime_);
156  int keySeek(long int ms_streamtime_);
157  Frame *pullNextFrame();
158 };
159 
167 class CacheFrameFilter : public FrameFilter { // <pyapi>
168 
169 public: // <pyapi>
175  CacheFrameFilter(const char *name, FrameCache *framecache);
176 
177 protected:
178  FrameCache *framecache;
179 
180 protected:
181  void go(Frame *frame);
182 }; // <pyapi>
183 
206 class FileCacheThread : public AbstractFileThread { // <pyapi>
207 
208 public: // <pyapi>
209  FileCacheThread(const char *name); // <pyapi>
210  virtual ~FileCacheThread(); // <pyapi>
211 
212 protected: // internal framefilter chain
214  TypeFrameFilter typefilter;
215  SwitchFrameFilter switchfilter;
216  CacheFrameFilter cache_filter_1, cache_filter_2;
217 
218 protected:
219  FrameCache frame_cache_1;
220  FrameCache frame_cache_2;
223  void (*callback)(long int mstimestamp);
224  PyObject *pyfunc;
225  PyObject *pyfunc2;
227  Frame *next;
228  long int reftime;
229  long int walltime;
230  AbstractFileState state;
232 
233 protected: // Thread member redefinitions
234  std::deque<FileCacheSignalContext> signal_fifo;
235  std::vector<FrameFilter *> slots_;
236  // std::vector<SetupFrame*> setup_frames; ///< Slot number => SetupFrame mapping. Book-keeping of SetupFrames
237  std::vector<std::vector<SetupFrame *>> setup_frames;
238 
239 public: // redefined virtual functions
240  void run();
241  void preRun();
242  void postRun();
243  void sendSignal(FileCacheSignalContext signal_ctx);
244 
245 public: // internal
246  void switchCache();
247  void dumpPlayCache();
248  void dumpTmpCache();
249  void sendSetupFrames(SetupFrame *f);
250  void stopStreams(bool send_state = true);
251  void playStreams(bool send_state = true);
252  void setRefTimeAndStop(bool send_state = true);
253  void seekStreams(long int mstimestamp, bool clear, bool send_state = true);
254  void clear();
255 
256 private: // internal
257  void handleSignal(FileCacheSignalContext &signal_ctx);
258  void handleSignals();
259  int safeGetSlot(SlotNumber slot, FrameFilter *&ff);
260  void registerStream(FileStreamContext &ctx);
261  void deregisterStream(FileStreamContext &ctx);
262 
263 public: // API must be called before thread start
264  void setCallback(void func(long int));
265 
266 public: // API // <pyapi>
271  void setPyCallback(PyObject *pobj); // <pyapi>
272 
279  void setPyCallback2(PyObject *pobj); // <pyapi>
280 
285  void registerStreamCall(FileStreamContext &ctx); // <pyapi>
286  void deregisterStreamCall(FileStreamContext &ctx); // <pyapi>
287 
289  FrameFilter &getFrameFilter(); // <pyapi>
290  void requestStopCall(); // <pyapi>
291  void dumpCache(); // <pyapi>
292  void stopStreamsCall(); // <pyapi>
293  void playStreamsCall(); // <pyapi>
294  void clearCall(); // <pyapi>
295 
303  void seekStreamsCall(long int mstimestamp, bool clear = false); // <pyapi>
304 }; // <pyapi>
305 
306 #endif
FileCacheSignal
Signals for FileCacheThread.
Definition: cachestream.h:83
This class uses AbstractFileStream(s)
Definition: filestream.h:136
Passes frames to a FrameCache.
Definition: cachestream.h:167
CacheFrameFilter(const char *name, FrameCache *framecache)
Default constructor.
Definition: cachestream.cpp:37
void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: cachestream.cpp:40
Thread that caches frames and streams them into output at play speed.
Definition: cachestream.h:206
void clear()
Removes any reference time. Stops stream.
Definition: cachestream.cpp:543
void sendSetupFrames(SetupFrame *f)
Sends SetupFrame s to all active slots.
Definition: cachestream.cpp:436
void preRun()
Called before entering the main execution loop, but after creating the thread.
Definition: cachestream.cpp:804
SetupFrame state_setupframe
SetupFrame for sending the stream state (seek, play, etc.)
Definition: cachestream.h:231
long int reftime
walltime = frametime_ + reftime
Definition: cachestream.h:228
ForkFrameFilter fork
Write incoming frames here.
Definition: cachestream.h:213
void seekStreamsCall(long int mstimestamp, bool clear=false)
Seek.
Definition: cachestream.cpp:1026
FrameFilter & getFrameFilter()
Framefilter for writing frames to FileCacheThread.
Definition: cachestream.cpp:862
void seekStreams(long int mstimestamp, bool clear, bool send_state=true)
Sets target time. Sets FileCacheThread::next = NULL.
Definition: cachestream.cpp:502
FrameCache * play_cache
Points to the current play cache (default frame_cache_2)
Definition: cachestream.h:221
void requestStopCall()
API method: stops the thread.
Definition: cachestream.cpp:866
void setRefTimeAndStop(bool send_state=true)
Set reference time and set state to stop.
Definition: cachestream.cpp:488
std::deque< FileCacheSignalContext > signal_fifo
Redefinition of signal fifo.
Definition: cachestream.h:234
FrameCache * tmp_cache
Points to current cache receiving frames.
Definition: cachestream.h:222
long int target_mstimestamp_
We should be at this time instant (streamtime)
Definition: cachestream.h:226
PyObject * pyfunc2
Python callback that emits current loaded time limits.
Definition: cachestream.h:225
void registerStreamCall(FileStreamContext &ctx)
Pass frames downstream.
Definition: cachestream.cpp:931
void run()
Main execution loop is defined here.
Definition: cachestream.cpp:558
PyObject * pyfunc
Python callback that emits current time.
Definition: cachestream.h:224
void postRun()
Called after the main execution loop exits, but before joining the thread.
Definition: cachestream.cpp:807
void setPyCallback2(PyObject *pobj)
Define callback.
Definition: cachestream.cpp:349
std::vector< FrameFilter * > slots_
Slot number => output framefilter mapping.
Definition: cachestream.h:235
void setPyCallback(PyObject *pobj)
Define callback.
Definition: cachestream.cpp:330
std::vector< std::vector< SetupFrame * > > setup_frames
Slot number, subsession_index => SetupFrame mapping. Book-keeping of SetupFrames.
Definition: cachestream.h:237
Replicates frame flow to two filters Use this frame filter to create frame filter tree structures.
Definition: framefilter.h:141
FrameCache works like FrameFifo, but frames are not pre-reserved.
Definition: cachestream.h:116
int seek(long int ms_streamtime_)
Seek to a desider stream time. -1 = no frames at left, 1 = no frames at right, 0 = ok.
Definition: cachestream.cpp:144
std::condition_variable ready_condition
The Event/Flag for FrameFifo::ready_mutex.
Definition: cachestream.h:138
Frame * pullNextFrame()
Get the next frame. Returns NULL if no frame was available.
Definition: cachestream.cpp:283
bool has_delta_frames
Does the cache have streams with key-frame, delta-frame sequences.
Definition: cachestream.h:129
void clear()
Clear the cache.
Definition: cachestream.cpp:112
Cache::iterator state
The state of the queue.
Definition: cachestream.h:133
virtual bool writeCopy(Frame *f, bool wait=false)
Take a frame "ftmp" from the stack, copy contents of "f" into "ftmp" and insert "ftmp" into the begin...
Definition: cachestream.cpp:56
std::mutex mutex
The Lock.
Definition: cachestream.h:136
long int mintime_
smallest frame timestamp (frametime)
Definition: cachestream.h:127
void dump()
Dump frames in the cache.
Definition: cachestream.cpp:133
long int maxtime_
biggest frame timestamp (frametime)
Definition: cachestream.h:128
int keySeek(long int ms_streamtime_)
Seek to a desider stream time. -1 = no frames at left, 1 = no frames at right, 0 = ok....
Definition: cachestream.cpp:199
std::condition_variable condition
The Event/Flag.
Definition: cachestream.h:137
bool isEmpty()
Cache empty or not.
Definition: cachestream.cpp:138
FrameCacheContext ctx
Parameters defining the cache.
Definition: cachestream.h:126
Cache cache
The queue.
Definition: cachestream.h:132
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:44
Frame: An abstract queueable class.
Definition: frame.h:112
Setup frame.
Definition: frame.h:277
Passes frame to one of the two terminals.
Definition: framefilter.h:401
std::string name
Name of the thread.
Definition: thread.h:116
Passes through frames of certain type only.
Definition: framefilter.h:431
Thread safe system of fifo and a stack.
AbstractFileState
Describes the state of a stream.
Definition: threadsignal.h:43
Encapsulate data sent to FileCacheThread.
Definition: cachestream.h:101
Signal information for FileCacheThread.
Definition: cachestream.h:70
long int mstimestamp
Seek: use existing frames for seek or clear the state.
Definition: cachestream.h:74
FileStreamContext file_stream_ctx
< Identifies the stream
Definition: cachestream.h:72
Definition: cachestream.h:56
Definition: cachestream.h:107
@ none
undefined (initial value)
Definition: usbthread.h:143