Valkka  1.6.1
OpenSource Video Management
valkkafs.h
Go to the documentation of this file.
1 #ifndef valkkafs_HEADER_GUARD
2 #define valkkafs_HEADER_GUARD
3 /*
4  * valkkafs.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 "common.h"
37 #include "thread.h"
38 #include "framefilter.h"
39 #include "framefifo.h"
40 #include "rawrite.h"
41 #include "logging.h"
42 #include "Python.h"
43 
44 /*
45 #include "boost/python/numpy.hpp"
46 namespace p = boost::python;
47 namespace np = boost::python::numpy;
48 // https://www.boost.org/doc/libs/1_64_0/libs/python/doc/html/numpy/reference/ndarray.html
49 // https://github.com/ndarray/Boost.NumPy/blob/master/libs/numpy/example/simple.cpp
50 // let's not use boost..
51 */
52 
78 class ValkkaFS { // <pyapi>
79 
80 public: // <pyapi>
81 
95  ValkkaFS(const char *device_file, const char *block_file, std::size_t blocksize, std::size_t n_blocks, bool init=false); // <pyapi>
96  virtual ~ValkkaFS(); // <pyapi>
97 
98 protected:
99  std::string device_file;
100  std::string block_file;
101  std::size_t blocksize;
102  std::size_t n_blocks;
103  bool init;
104  std::size_t device_size;
105  std::vector<long int> tab;
106  std::fstream os;
107 
108  std::mutex mutex;
109  long int col_0;
110  long int col_1;
111  long int col_0_lu;
112  long int col_1_lu;
113  std::size_t current_row;
114  std::size_t prev_row;
116  PyObject *pyfunc;
117 
118 private:
119  void callPyFunc(std::string msg, bool use_gil=true);
120 
121 protected:
122  const static std::size_t n_cols = 2;
123 
124 protected:
125  std::size_t ind(std::size_t i, std::size_t j);
126 
127 public:
128  void setVal(std::size_t i, std::size_t j, long int val);
129  long int getVal(std::size_t i, std::size_t j);
130  std::size_t getBlockSeek(std::size_t n_block);
131  std::size_t getCurrentBlockSeek();
132 
133 public: // getters // <pyapi>
134  const std::size_t getBlockSize() {return this->blocksize;} // <pyapi>
135 
136 public: // <pyapi>
137  std::size_t get_n_blocks(); // <pyapi>
138  std::size_t get_n_cols(); // <pyapi>
139 
141  void dumpTable_();
143  void updateDumpTable_(std::size_t n_block);
144 
146  void dumpTable(); // <pyapi>
148  void readTable(); // <pyapi>
150  std::string getDevice(); // <pyapi>
152  std::size_t getDeviceSize(); // <pyapi>
154  void clearDevice(bool writethrough=false, bool verbose=false); // <pyapi>
156  void clearTable(); // <pyapi>
158  std::size_t maxFrameSize(); // <pyapi>
160  void reportTable(std::size_t from=0, std::size_t to=0, bool show_all=false); // <pyapi>
161 
166  void updateTable(bool disk_write=false); // <pyapi>
167 
173  void writeBlock(bool pycall=true, bool use_gil=true);
174 
176  void markFrame(long int mstimestamp);
177 
179  virtual void markKeyFrame(long int mstimestamp);
180 
182  void setCurrentBlock(std::size_t n_block); // <pyapi>
183 
185  void setBlockCallback(PyObject* pobj); // <pyapi>
186 
196  void setArrayCall(PyObject *pyobj); // <pyapi>
197 }; // <pyapi>
198 
199 
200 class ValkkaFS2 : public ValkkaFS { // <pyapi>
201 
202 public: // <pyapi>
209  ValkkaFS2(const char *device_file, const char *block_file, std::size_t blocksize, std::size_t n_blocks, bool init=false); // <pyapi>
210  virtual ~ValkkaFS2(); // <pyapi>
211 
213  virtual void markKeyFrame(long int mstimestamp);
214 }; // <pyapi>
215 
216 
222 class ValkkaFSTool { // <pyapi>
223 
224 public: // <pyapi>
225  ValkkaFSTool(ValkkaFS &valkkafs); // <pyapi>
226  ~ValkkaFSTool(); // <pyapi>
227 
228 protected:
229  // std::fstream is;
230  RawReader raw_reader;
231  ValkkaFS &valkkafs;
232 
233 public: // <pyapi>
234  void dumpBlock(std::size_t n_block); // <pyapi>
235 }; // <pyapi>
236 
237 
238 
246 class ValkkaFSWriterThread : public Thread { // <pyapi>
247 
248 public: // <pyapi>
249  ValkkaFSWriterThread(const char *name, ValkkaFS &valkkafs, FrameFifoContext fifo_ctx=FrameFifoContext(), bool o_direct = false); // <pyapi>
250  ~ValkkaFSWriterThread(); // <pyapi>
251 
252 protected:
253  ValkkaFS &valkkafs;
254  // std::fstream filestream;
255  RaWriter raw_writer;
256  std::map<SlotNumber, IdNumber> slot_to_id;
257  std::size_t bytecount;
258 
259 protected: // frame input
263 
264 protected: // Thread member redefinitions
265  std::deque<ValkkaFSWriterSignalContext> signal_fifo;
266 
267 public: // redefined virtual functions
268  void run();
269  void preRun();
270  void postRun();
271  void preJoin();
272  void postJoin();
273  void sendSignal(ValkkaFSWriterSignalContext signal_ctx);
274 
275 protected:
276  void handleSignal(ValkkaFSWriterSignalContext &signal_ctx);
277  void handleSignals();
278 
279 protected:
284  void saveCurrentBlock(bool pycall=true, bool use_gil=true);
285  void setSlotId(SlotNumber slot, IdNumber id);
286  void unSetSlotId(SlotNumber slot);
287  void clearSlotId();
288  void reportSlotId();
289  void seek(std::size_t n_block);
290 
291 // API
292 public: // <pyapi>
293  FifoFrameFilter &getFrameFilter(); // <pyapi>
294  FifoFrameFilter &getBlockingFrameFilter(); // <pyapi>
296  void setSlotIdCall(SlotNumber slot, IdNumber id); // <pyapi>
298  void unSetSlotIdCall(SlotNumber slot); // <pyapi>
300  void clearSlotIdCall(); // <pyapi>
302  void reportSlotIdCall(); // <pyapi>
304  void seekCall(std::size_t n_block); // <pyapi>
305  void requestStopCall(); // <pyapi>
306 }; // <pyapi>
307 
308 
309 #endif
Passes frames to a multiprocessing fifo.
Definition: framefilter.h:554
Passes frames to a FrameFifo.
Definition: framefilter.h:530
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
Definition: rawrite.h:67
Definition: rawrite.h:96
A class for multithreading with a signaling system.
Definition: thread.h:87
std::string name
Name of the thread.
Definition: thread.h:116
Definition: valkkafs.h:200
ValkkaFS2(const char *device_file, const char *block_file, std::size_t blocksize, std::size_t n_blocks, bool init=false)
Default Constructor.
Definition: valkkafs.cpp:442
virtual void markKeyFrame(long int mstimestamp)
Used by a writer class to inform that a key frame has been written.
Definition: valkkafs.cpp:449
Analyzer tool for ValkkaFS.
Definition: valkkafs.h:222
Writes frames to ValkkaFS.
Definition: valkkafs.h:246
void reportSlotIdCall()
Print slot, id number mappings.
Definition: valkkafs.cpp:824
void requestStopCall()
API method: stops the thread.
Definition: valkkafs.cpp:852
void saveCurrentBlock(bool pycall=true, bool use_gil=true)
Save the current block GIL must be obtained for calls that originate purely from the cpp side For cal...
Definition: valkkafs.cpp:630
void handleSignals()
Call ValkkaFSWriterThread::handleSignal for every signal in the signal_fifo.
Definition: valkkafs.cpp:717
void run()
Main execution loop is defined here.
Definition: valkkafs.cpp:530
void preJoin()
Called before the thread is joined.
Definition: valkkafs.cpp:670
std::deque< ValkkaFSWriterSignalContext > signal_fifo
Redefinition of signal fifo.
Definition: valkkafs.h:265
void handleSignal(ValkkaFSWriterSignalContext &signal_ctx)
Handle an individual signal. Signal can originate from the frame fifo or from the signal_fifo deque.
Definition: valkkafs.cpp:682
void setSlotIdCall(SlotNumber slot, IdNumber id)
Set a slot => id number mapping.
Definition: valkkafs.cpp:776
std::map< SlotNumber, IdNumber > slot_to_id
Map from slot numbers to ids.
Definition: valkkafs.h:256
void sendSignal(ValkkaFSWriterSignalContext signal_ctx)
Insert a signal into the signal_fifo.
Definition: valkkafs.cpp:712
FrameFifo infifo
Incoming frames are read from here.
Definition: valkkafs.h:260
void unSetSlotIdCall(SlotNumber slot)
Clear a slot => id number mapping.
Definition: valkkafs.cpp:794
void clearSlotIdCall()
Clear all slot => id number mappings.
Definition: valkkafs.cpp:810
void postRun()
Called after the main execution loop exits, but before joining the thread.
Definition: valkkafs.cpp:666
BlockingFifoFrameFilter infilter_block
Incoming frames can also be written here. If stack runs out of frames, writing will block.
Definition: valkkafs.h:262
void preRun()
Called before entering the main execution loop, but after creating the thread.
Definition: valkkafs.cpp:663
void seekCall(std::size_t n_block)
Seek to a certain block.
Definition: valkkafs.cpp:835
void postJoin()
Called after the thread has been joined.
Definition: valkkafs.cpp:678
FifoFrameFilter infilter
Write incoming frames here // TODO: add a chain of correcting FrameFilter(s)
Definition: valkkafs.h:261
Book-keeping for ValkkaFS.
Definition: valkkafs.h:78
std::size_t getDeviceSize()
returns device file size
Definition: valkkafs.cpp:326
PyObject * pyfunc
A python callback that's triggered when the blocktable should be read again.
Definition: valkkafs.h:116
void setCurrentBlock(std::size_t n_block)
Set block number that's being written.
Definition: valkkafs.cpp:368
std::vector< long int > tab
Blocktable.
Definition: valkkafs.h:105
std::size_t current_row
Row number (block) that's being written.
Definition: valkkafs.h:113
std::fstream os
Write handle to blocktable file.
Definition: valkkafs.h:106
long int col_1_lu
col_1 at previous blocktable update
Definition: valkkafs.h:112
void setArrayCall(PyObject *pyobj)
Copy blocktable to a given numpy array.
Definition: valkkafs.cpp:393
virtual void markKeyFrame(long int mstimestamp)
Used by a writer class to inform that a key frame has been written.
Definition: valkkafs.cpp:315
ValkkaFS(const char *device_file, const char *block_file, std::size_t blocksize, std::size_t n_blocks, bool init=false)
Default Constructor.
Definition: valkkafs.cpp:39
bool init
Clear the blocktable or not even if it exists.
Definition: valkkafs.h:103
void updateDumpTable_(std::size_t n_block)
dump single row of bloctable to disk.
Definition: valkkafs.cpp:105
void setBlockCallback(PyObject *pobj)
Set a python callable that's being triggered when a new block is written.
Definition: valkkafs.cpp:374
long int col_1
Current column 1 value (max anyframe timestamp)
Definition: valkkafs.h:110
std::size_t ind(std::size_t i, std::size_t j)
first index: block number (row), second index: column
Definition: valkkafs.cpp:121
void writeBlock(bool pycall=true, bool use_gil=true)
Used by a writer class to inform that a new block has been written.
Definition: valkkafs.cpp:262
void markFrame(long int mstimestamp)
Used by a writer class to inform that a non-key frame has been written.
Definition: valkkafs.cpp:310
long int col_0_lu
col_0 at previous blocktable update
Definition: valkkafs.h:111
void readTable()
read blocktable from disk
Definition: valkkafs.cpp:113
long int col_0
Current column 0 value (max keyframe timestamp)
Definition: valkkafs.h:109
std::size_t prev_row
Previous row number (block)
Definition: valkkafs.h:114
void reportTable(std::size_t from=0, std::size_t to=0, bool show_all=false)
print blocktable
Definition: valkkafs.cpp:156
void setVal(std::size_t i, std::size_t j, long int val)
set tab's value at block i, row j
Definition: valkkafs.cpp:131
std::size_t maxFrameSize()
returns maximum allowed frame size in bytes
Definition: valkkafs.cpp:151
void clearTable()
clears the blocktable and writes it to the disk
Definition: valkkafs.cpp:358
void updateTable(bool disk_write=false)
Tell ValkkaFS to update the blocktable, even if the block hasn't finished Typically used by an extern...
Definition: valkkafs.cpp:229
void dumpTable_()
dump blocktable to disk.
Definition: valkkafs.cpp:92
std::string getDevice()
returns device filename
Definition: valkkafs.cpp:322
void clearDevice(bool writethrough=false, bool verbose=false)
writes zero bytes to the device
Definition: valkkafs.cpp:330
void dumpTable()
dump blocktable to disk
Definition: valkkafs.cpp:100
List of common header files.
Thread safe system of fifo and a stack.
Definition of FrameFilter and derived classes for various purposes.
Logging utilities.
Write directly to files and devices with POSIX O_DIRECT.
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:56
Encapsulate data sent in the ValkkaFSWriterSignal.
Definition: threadsignal.h:185
Base class for multithreading.