Valkka  1.6.1
OpenSource Video Management
framefifo.h
Go to the documentation of this file.
1 #ifndef framefifo_HEADER_GUARD
2 #define framefifo_HEADER_GUARD
3 /*
4  * framefifo.h : Thread safe system of fifo and a stack
5  *
6  * Copyright 2017-2020 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 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 "frame.h"
38 #include "logging.h"
39 #include "macro.h"
40 
56 struct FrameFifoContext { // <pyapi>
57  FrameFifoContext() : n_basic(50), n_avpkt(0), n_avframe(0), n_yuvpbo(0), n_setup(20), n_signal(20), n_marker(20), flush_when_full(DEFAULT_FRAMEFIFO_FLUSH_WHEN_FULL) {} // <pyapi>
58  FrameFifoContext(int n_basic, int n_avpkt, int n_avframe, int n_yuvpbo, int n_setup, int n_signal, bool flush_when_full) : // <pyapi>
60  FrameFifoContext(int n_signal) : // <pyapi>
61  n_basic(0), n_avpkt(0), n_avframe(0), n_yuvpbo(0), n_setup(0), n_signal(n_signal), n_marker(n_signal), flush_when_full(DEFAULT_FRAMEFIFO_FLUSH_WHEN_FULL) {} // <pyapi>
62  int n_basic;
63  int n_avpkt;
64  int n_avframe;
65  int n_yuvpbo;
66  int n_setup;
67  int n_signal;
68  int n_marker;
70 }; // <pyapi>
71 
72 
83 class FrameFifo {
84 
85 public:
86  FrameFifo(const char *name, FrameFifoContext ctx =FrameFifoContext());
87  virtual ~FrameFifo();
88  ban_copy_ctor(FrameFifo);
89  ban_copy_asm(FrameFifo);
90 
91 protected:
92  std::string name;
94 
95 protected: // reservoir, stack & fifo queue
96  std::map<FrameClass,Reservoir> reservoirs;
97  std::map<FrameClass,Stack> stacks;
98  Fifo fifo;
99 
100 protected: // mutex synchro
101  std::mutex mutex;
102  std::condition_variable condition;
103  std::condition_variable ready_condition;
104 
105 protected:
106  virtual void recycle_(Frame* f);
107  virtual void recycleAll_();
108 
109 public:
110  Reservoir &getReservoir(FrameClass cl) {return this->reservoirs[cl];}
111 
112 public:
113  virtual bool writeCopy(Frame* f, bool wait=false);
114  virtual Frame* read(unsigned short int mstimeout=0);
115  virtual void recycle(Frame* f);
116  virtual void recycleAll();
117  virtual void dumpStacks();
118  virtual void dumpFifo();
119  virtual void diagnosis();
120  bool isEmpty();
121 };
122 
123 
132 class FDFrameFifo : public FrameFifo {
133 
134 public:
135  FDFrameFifo(const char *name, FrameFifoContext ctx =FrameFifoContext());
136  virtual ~FDFrameFifo();
137  //ban_copy_ctor(FDFrameFifo);
138  //ban_copy_asm(FDFrameFifo);
139 
140 private:
141  int fd;
142 
143 public:
144  virtual bool writeCopy(Frame* f, bool wait=false);
145 
146  virtual Frame* read(unsigned short int mstimeout=0);
147 
148 public:
149  const int getFD() {return this->fd;}
150 };
151 
152 
153 
154 #endif
155 
FrameFifo::recycle
virtual void recycle(Frame *f)
Like FrameFifo::recycle_ but with mutex protection.
Definition: framefifo.cpp:225
FrameFifoContext::n_basic
int n_basic
data at payload // <pyapi>
Definition: framefifo.h:62
FrameFifo::FrameFifo
FrameFifo(const char *name, FrameFifoContext ctx=FrameFifoContext())
Default ctor
Definition: framefifo.cpp:37
FDFrameFifo
FrameFifo using file descriptors.
Definition: framefifo.h:132
FDFrameFifo::writeCopy
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: framefifo.cpp:313
FrameFifo::dumpStacks
virtual void dumpStacks()
Dump frames in the stacks.
Definition: framefifo.cpp:248
Frame
Frame: An abstract queueable class.
Definition: frame.h:111
FrameFifo::condition
std::condition_variable condition
The Event/Flag.
Definition: framefifo.h:102
FDFrameFifo::FDFrameFifo
FDFrameFifo(const char *name, FrameFifoContext ctx=FrameFifoContext())
Default ctor
Definition: framefifo.cpp:299
FrameFifoContext::n_marker
int n_marker
marks start/end of frame emission. defaults to n_signal // <pyapi>
Definition: framefifo.h:68
FDFrameFifo::read
virtual Frame * read(unsigned short int mstimeout=0)
Pop a frame from the end of the fifo when available.
Definition: framefifo.cpp:394
FrameFifoContext::n_avframe
int n_avframe
data at ffmpeg av_frame and ffmpeg av_codec_context // <pyapi>
Definition: framefifo.h:64
FrameFifo
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
FrameFifoContext::n_setup
int n_setup
setup data // <pyapi>
Definition: framefifo.h:66
FrameFifo::reservoirs
std::map< FrameClass, Reservoir > reservoirs
The actual frames.
Definition: framefifo.h:96
FrameFifo::isEmpty
bool isEmpty()
Tell if fifo is empty.
Definition: framefifo.cpp:294
FrameClass
FrameClass
Enumeration of Frame classes used by Valkka.
Definition: frame.h:52
FDFrameFifo::~FDFrameFifo
virtual ~FDFrameFifo()
Default virtual dtor
Definition: framefifo.cpp:308
FrameFifo::~FrameFifo
virtual ~FrameFifo()
Default virtual dtor
Definition: framefifo.cpp:75
FrameFifo::read
virtual Frame * read(unsigned short int mstimeout=0)
Pop a frame from the end of the fifo when available.
Definition: framefifo.cpp:171
FrameFifo::diagnosis
virtual void diagnosis()
Print a resumen of fifo and stack usage.
Definition: framefifo.cpp:278
FrameFifoContext::flush_when_full
bool flush_when_full
Flush when filled // <pyapi>
Definition: framefifo.h:69
frame.h
Frame classes.
FrameFifo::dumpFifo
virtual void dumpFifo()
Dump frames in the fifo.
Definition: framefifo.cpp:266
FrameFifo::ready_condition
std::condition_variable ready_condition
The Event/Flag for FrameFifo::ready_mutex.
Definition: framefifo.h:103
FrameFifo::stacks
std::map< FrameClass, Stack > stacks
Pointers to the actual frames, sorted by FrameClass.
Definition: framefifo.h:97
common.h
List of common header files.
macro.h
FrameFifo::recycle_
virtual void recycle_(Frame *f)
Return Frame f back into the stack. Update target_size if necessary.
Definition: framefifo.cpp:218
FrameFifoContext::n_signal
int n_signal
signal to AVThread or OpenGLThread // <pyapi>
Definition: framefifo.h:67
logging.h
Logging utilities.
FrameFifo::writeCopy
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: framefifo.cpp:100
FrameFifo::mutex
std::mutex mutex
The Lock.
Definition: framefifo.h:101
FrameFifo::ctx
FrameFifoContext ctx
Parameters defining the stack and overflow behaviour.
Definition: framefifo.h:93
FrameFifoContext::n_avpkt
int n_avpkt
data at ffmpeg avpkt // <pyapi>
Definition: framefifo.h:63
FrameFifo::recycleAll
virtual void recycleAll()
Recycle all frames from fifo back to stack (make a "flush")
Definition: framefifo.cpp:242
FrameFifo::fifo
Fifo fifo
The fifo queue.
Definition: framefifo.h:98
FrameFifoContext::n_yuvpbo
int n_yuvpbo
data at yuvpbo struct // <pyapi>
Definition: framefifo.h:65
FrameFifoContext
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:56
FrameFifo::recycleAll_
virtual void recycleAll_()
Recycle all frames back to the stack.
Definition: framefifo.cpp:232