Valkka  0.17.0
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, 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 "common.h"
37 #include "frame.h"
38 #include "logging.h"
39 #include "macro.h"
40 
45 struct FrameFifoContext { // <pyapi>
46  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>
47  FrameFifoContext(int n_basic, int n_avpkt, int n_avframe, int n_yuvpbo, int n_setup, int n_signal, bool flush_when_full) : // <pyapi>
48  n_basic(n_basic), n_avpkt(n_avpkt), n_avframe(n_avframe), n_yuvpbo(n_yuvpbo), n_setup(n_setup), n_signal(n_signal), n_marker(n_signal), flush_when_full(flush_when_full) {} // <pyapi>
49  FrameFifoContext(int n_signal) : // <pyapi>
50  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>
51  int n_basic;
52  int n_avpkt;
53  int n_avframe;
54  int n_yuvpbo;
55  int n_setup;
56  int n_signal;
57  int n_marker;
59 }; // <pyapi>
60 
61 
72 class FrameFifo {
73 
74 public:
75  FrameFifo(const char *name, FrameFifoContext ctx =FrameFifoContext());
76  virtual ~FrameFifo();
77  ban_copy_ctor(FrameFifo);
78  ban_copy_asm(FrameFifo);
79 
80 protected:
81  std::string name;
83 
84 protected: // reservoir, stack & fifo queue
85  std::map<FrameClass,Reservoir> reservoirs;
86  std::map<FrameClass,Stack> stacks;
87  Fifo fifo;
88 
89 protected: // mutex synchro
90  std::mutex mutex;
91  std::condition_variable condition;
92  std::condition_variable ready_condition;
93 
94 protected:
95  virtual void recycle_(Frame* f);
96  virtual void recycleAll_();
97 
98 public:
99  Reservoir &getReservoir(FrameClass cl) {return this->reservoirs[cl];}
100 
101 public:
102  virtual bool writeCopy(Frame* f, bool wait=false);
103  virtual Frame* read(unsigned short int mstimeout=0);
104  virtual void recycle(Frame* f);
105  virtual void recycleAll();
106  virtual void dumpStacks();
107  virtual void dumpFifo();
108  virtual void diagnosis();
109  bool isEmpty();
110 };
111 
112 
121 class FDFrameFifo : public FrameFifo {
122 
123 public:
124  FDFrameFifo(const char *name, FrameFifoContext ctx =FrameFifoContext());
125  virtual ~FDFrameFifo();
126  //ban_copy_ctor(FDFrameFifo);
127  //ban_copy_asm(FDFrameFifo);
128 
129 private:
130  int fd;
131 
132 public:
133  virtual bool writeCopy(Frame* f, bool wait=false);
134 
135  virtual Frame* read(unsigned short int mstimeout=0);
136 
137 public:
138  const int getFD() {return this->fd;}
139 };
140 
141 
142 
143 #endif
144 
std::condition_variable ready_condition
The Event/Flag for FrameFifo::ready_mutex.
Definition: framefifo.h:92
int n_yuvpbo
data at yuvpbo struct // <pyapi>
Definition: framefifo.h:54
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:45
int n_basic
data at payload // <pyapi>
Definition: framefifo.h:51
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:72
Logging utilities.
std::map< FrameClass, Reservoir > reservoirs
The actual frames.
Definition: framefifo.h:85
int n_avframe
data at ffmpeg av_frame and ffmpeg av_codec_context // <pyapi>
Definition: framefifo.h:53
int n_setup
setup data // <pyapi>
Definition: framefifo.h:55
Frame classes.
FrameFifoContext ctx
Parameters defining the stack and overflow behaviour.
Definition: framefifo.h:82
int n_marker
marks start/end of frame emission. defaults to n_signal // <pyapi>
Definition: framefifo.h:57
std::condition_variable condition
The Event/Flag.
Definition: framefifo.h:91
FrameFifo using file descriptors.
Definition: framefifo.h:121
Fifo fifo
The fifo queue.
Definition: framefifo.h:87
std::mutex mutex
The Lock.
Definition: framefifo.h:90
FrameClass
Enumeration of Frame classes used by Valkka.
Definition: frame.h:51
bool flush_when_full
Flush when filled // <pyapi>
Definition: framefifo.h:58
Reservoir & getReservoir(FrameClass cl)
Get the reservoir .. in the case you want to manipulate the frames.
Definition: framefifo.h:99
std::map< FrameClass, Stack > stacks
Pointers to the actual frames, sorted by FrameClass.
Definition: framefifo.h:86
Frame: An abstract queueable class.
Definition: frame.h:108
List of common header files.
int n_signal
signal to AVThread or OpenGLThread // <pyapi>
Definition: framefifo.h:56
int n_avpkt
data at ffmpeg avpkt // <pyapi>
Definition: framefifo.h:52