Valkka  0.17.0
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  * Copyright 2017, 2018 Valkka Security Ltd. and 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 Affero 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 Affero General Public License for more details.
21  *
22  * You should have received a copy of the GNU Affero 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 
41 /* 02 03 04 05 06 07 08 09 10 11 12 13
42  * a a b A a b c C c c B A
43  *
44  * seek to 10 : <---- .. key frame for C, A found, but not for B .. if this is from blocks, ok
45  *
46  * better idea: start iterating from the very beginning, save (overwrite) frame pointers during progress
47  *
48  * Block request:
49  * when to request more blocks / frames ? .. when close enough to maxtime
50  *
51  *
52  * 1 2 3
53  * 2 3 4
54  *
55  */
56 
57 
58 
59 struct FileStreamContext { // <pyapi>
60  FileStreamContext(SlotNumber slot, FrameFilter *framefilter) : // <pyapi>
61  slot(slot), framefilter(framefilter) {} // <pyapi>
62  FileStreamContext() {} // <pyapi>
63  SlotNumber slot; // <pyapi>
64  FrameFilter *framefilter; // <pyapi>
65 }; // <pyapi>
66 
67 
75  long int mstimestamp;
77  bool clear;
78 };
79 
80 
84 enum class FileCacheSignal {
85  none,
86  exit,
87  register_stream,
88  deregister_stream,
89  play_streams,
90  stop_streams,
91  clear,
92  seek_streams,
93  clear_streams,
94  report_cache
95 };
96 
97 
104  FileCacheSignalPars pars;
105 };
106 
107 
108 
109 struct FrameCacheContext { // nothing here yet ..
110  int nada;
111 };
112 
113 
118 class FrameCache {
119 
120 public:
121  FrameCache(const char *name, FrameCacheContext ctx =FrameCacheContext());
122  ~FrameCache();
123  ban_copy_ctor(FrameCache);
124  ban_copy_asm(FrameCache);
125 
126 protected:
127  std::string name;
129  long int mintime_;
130  long int maxtime_;
132 
133 protected:
134  Cache cache;
135  Cache::iterator state;
136 
137 protected: // mutex synchro
138  std::mutex mutex;
139  std::condition_variable condition;
140  std::condition_variable ready_condition;
141 
142 public: // getters
143  long int getMinTime_() {return this->mintime_;}
144  long int getMaxTime_() {return this->maxtime_;}
145  bool hasDeltaFrames() {return this->has_delta_frames;}
146 
147 protected:
148  void dump_();
149  void clear_();
150 
151 public:
152  virtual bool writeCopy(Frame* f, bool wait=false);
153  // 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
154  void clear();
155  void dump();
156  bool isEmpty();
157  int seek(long int ms_streamtime_);
158  int keySeek(long int ms_streamtime_);
159  Frame *pullNextFrame();
160 };
161 
162 
163 
164 
172 class CacheFrameFilter : public FrameFilter { // <pyapi>
173 
174 public: // <pyapi>
180  CacheFrameFilter(const char* name, FrameCache* framecache);
181 
182 protected:
183  FrameCache* framecache;
184 
185 protected:
186  void go(Frame* frame);
187 }; // <pyapi>
188 
189 
212 class FileCacheThread : public AbstractFileThread { // <pyapi>
213 
214 public: // <pyapi>
215  FileCacheThread(const char *name); // <pyapi>
216  virtual ~FileCacheThread(); // <pyapi>
217 
218 protected: // internal framefilter chain
220  TypeFrameFilter typefilter;
221  SwitchFrameFilter switchfilter;
222  CacheFrameFilter cache_filter_1, cache_filter_2;
223 
224 protected:
225  FrameCache frame_cache_1;
226  FrameCache frame_cache_2;
229  void (*callback)(long int mstimestamp);
230  PyObject *pyfunc;
231  PyObject *pyfunc2;
233  Frame *next;
234  long int reftime;
235  long int walltime;
236  AbstractFileState state;
238 
239 protected: // Thread member redefinitions
240  std::deque<FileCacheSignalContext> signal_fifo;
241  std::vector<FrameFilter*> slots_;
242  // std::vector<SetupFrame*> setup_frames; ///< Slot number => SetupFrame mapping. Book-keeping of SetupFrames
243  std::vector<std::vector<SetupFrame*>> setup_frames;
244 
245 
246 public: // redefined virtual functions
247  void run();
248  void preRun();
249  void postRun();
250  void sendSignal(FileCacheSignalContext signal_ctx);
251 
252 public: // internal
253  void switchCache();
254  void dumpPlayCache();
255  void dumpTmpCache();
256  void sendSetupFrames(SetupFrame *f);
257  void stopStreams(bool send_state = true);
258  void playStreams(bool send_state = true);
259  void setRefTimeAndStop(bool send_state = true);
260  void seekStreams(long int mstimestamp, bool clear, bool send_state = true);
261  void clear();
262 
263 private: // internal
264  void handleSignal(FileCacheSignalContext &signal_ctx);
265  void handleSignals();
266  int safeGetSlot(SlotNumber slot, FrameFilter*& ff);
267  void registerStream (FileStreamContext &ctx);
268  void deregisterStream (FileStreamContext &ctx);
269 
270 public: // API must be called before thread start
271  void setCallback(void func(long int));
272 
273 public: // API // <pyapi>
278  void setPyCallback(PyObject* pobj); // <pyapi>
279 
286  void setPyCallback2(PyObject* pobj); // <pyapi>
287 
292  void registerStreamCall (FileStreamContext &ctx); // <pyapi>
293  void deregisterStreamCall (FileStreamContext &ctx); // <pyapi>
294 
296  FrameFilter &getFrameFilter(); // <pyapi>
297  void requestStopCall(); // <pyapi>
298  void dumpCache(); // <pyapi>
299  void stopStreamsCall(); // <pyapi>
300  void playStreamsCall(); // <pyapi>
301  void clearCall(); // <pyapi>
302 
310  void seekStreamsCall(long int mstimestamp, bool clear = false); // <pyapi>
311 }; // <pyapi>
312 
313 
314 
315 
316 #endif
std::vector< FrameFilter * > slots_
Slot number => output framefilter mapping.
Definition: cachestream.h:241
Signal information for FileCacheThread.
Definition: cachestream.h:71
signal to AVThread or OpenGLThread. Also custom signals to custom Threads
Thread safe system of fifo and a stack.
long int reftime
walltime = frametime_ + reftime
Definition: cachestream.h:234
std::deque< FileCacheSignalContext > signal_fifo
Redefinition of signal fifo.
Definition: cachestream.h:240
PyObject * pyfunc2
Python callback that emits current loaded time limits.
Definition: cachestream.h:231
bool has_delta_frames
Does the cache have streams with key-frame, delta-frame sequences.
Definition: cachestream.h:131
Definition: cachestream.h:59
FrameCacheContext ctx
Parameters defining the cache.
Definition: cachestream.h:128
undefined (initial value)
Definition: usbthread.h:143
Cache::iterator state
The state of the queue.
Definition: cachestream.h:135
PyObject * pyfunc
Python callback that emits current time.
Definition: cachestream.h:230
Thread that caches frames and streams them into output at play speed.
Definition: cachestream.h:212
std::condition_variable ready_condition
The Event/Flag for FrameFifo::ready_mutex.
Definition: cachestream.h:140
ForkFrameFilter fork
Write incoming frames here.
Definition: cachestream.h:219
Cache cache
The queue.
Definition: cachestream.h:134
Passes through frames of certain type only.
Definition: framefilter.h:454
FileStreamContext file_stream_ctx
< Identifies the stream
Definition: cachestream.h:73
long int mintime_
smallest frame timestamp (frametime)
Definition: cachestream.h:129
Passes frame to one of the two terminals.
Definition: framefilter.h:425
SetupFrame state_setupframe
SetupFrame for sending the stream state (seek, play, etc.)
Definition: cachestream.h:237
FrameCache * tmp_cache
Points to current cache receiving frames.
Definition: cachestream.h:228
std::condition_variable condition
The Event/Flag.
Definition: cachestream.h:139
std::vector< std::vector< SetupFrame * > > setup_frames
Slot number, subsession_index => SetupFrame mapping. Book-keeping of SetupFrames. ...
Definition: cachestream.h:243
long int mstimestamp
Seek: use existing frames for seek or clear the state.
Definition: cachestream.h:75
long int maxtime_
biggest frame timestamp (frametime)
Definition: cachestream.h:130
Setup frame.
Definition: frame.h:270
Encapsulate data sent to FileCacheThread.
Definition: cachestream.h:102
long int target_mstimestamp_
We should be at this time instant (streamtime)
Definition: cachestream.h:232
FileCacheSignal
Signals for FileCacheThread.
Definition: cachestream.h:84
Frame: An abstract queueable class.
Definition: frame.h:108
std::mutex mutex
The Lock.
Definition: cachestream.h:138
Passes frames to a FrameCache.
Definition: cachestream.h:172
FrameCache works like FrameFifo, but frames are not pre-reserved.
Definition: cachestream.h:118
This class uses AbstractFileStream(s)
Definition: filestream.h:136
AbstractFileState
Describes the state of a stream.
Definition: threadsignal.h:43
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:46
Replicates frame flow to two filters Use this frame filter to create frame filter tree structures...
Definition: framefilter.h:149
FrameCache * play_cache
Points to the current play cache (default frame_cache_2)
Definition: cachestream.h:227
Definition: cachestream.h:109