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  * (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 "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);
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 using file descriptors.
Definition: framefifo.h:132
virtual ~FDFrameFifo()
Default virtual dtor
Definition: framefifo.cpp:308
FDFrameFifo(const char *name, FrameFifoContext ctx=FrameFifoContext())
Default ctor
Definition: framefifo.cpp:299
virtual Frame * read(unsigned short int mstimeout=0)
Pop a frame from the end of the fifo when available.
Definition: framefifo.cpp:394
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
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
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
virtual void recycleAll()
Recycle all frames from fifo back to stack (make a "flush")
Definition: framefifo.cpp:242
virtual void recycle(Frame *f)
Like FrameFifo::recycle_ but with mutex protection.
Definition: framefifo.cpp:225
std::condition_variable ready_condition
The Event/Flag for FrameFifo::ready_mutex.
Definition: framefifo.h:103
virtual void recycleAll_()
Recycle all frames back to the stack.
Definition: framefifo.cpp:232
bool isEmpty()
Tell if fifo is empty.
Definition: framefifo.cpp:294
virtual void recycle_(Frame *f)
Return Frame f back into the stack. Update target_size if necessary.
Definition: framefifo.cpp:218
FrameFifo(const char *name, FrameFifoContext ctx=FrameFifoContext())
Default ctor
Definition: framefifo.cpp:37
Fifo fifo
The fifo queue.
Definition: framefifo.h:98
virtual void dumpStacks()
Dump frames in the stacks.
Definition: framefifo.cpp:248
std::mutex mutex
The Lock.
Definition: framefifo.h:101
virtual void diagnosis()
Print a resumen of fifo and stack usage.
Definition: framefifo.cpp:278
std::map< FrameClass, Reservoir > reservoirs
The actual frames.
Definition: framefifo.h:96
virtual ~FrameFifo()
Default virtual dtor
Definition: framefifo.cpp:75
virtual Frame * read(unsigned short int mstimeout=0)
Pop a frame from the end of the fifo when available.
Definition: framefifo.cpp:171
FrameFifoContext ctx
Parameters defining the stack and overflow behaviour.
Definition: framefifo.h:93
std::condition_variable condition
The Event/Flag.
Definition: framefifo.h:102
std::map< FrameClass, Stack > stacks
Pointers to the actual frames, sorted by FrameClass.
Definition: framefifo.h:97
Reservoir & getReservoir(FrameClass cl)
Get the reservoir .. in the case you want to manipulate the frames.
Definition: framefifo.h:110
virtual void dumpFifo()
Dump frames in the fifo.
Definition: framefifo.cpp:266
Frame: An abstract queueable class.
Definition: frame.h:112
List of common header files.
Frame classes.
FrameClass
Enumeration of Frame classes used by Valkka.
Definition: frame.h:53
Logging utilities.
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:56
int n_avpkt
data at ffmpeg avpkt // <pyapi>
Definition: framefifo.h:63
int n_setup
setup data // <pyapi>
Definition: framefifo.h:66
int n_basic
data at payload // <pyapi>
Definition: framefifo.h:62
int n_avframe
data at ffmpeg av_frame and ffmpeg av_codec_context // <pyapi>
Definition: framefifo.h:64
int n_signal
signal to AVThread or OpenGLThread // <pyapi>
Definition: framefifo.h:67
bool flush_when_full
Flush when filled // <pyapi>
Definition: framefifo.h:69
int n_marker
marks start/end of frame emission. defaults to n_signal // <pyapi>
Definition: framefifo.h:68
int n_yuvpbo
data at yuvpbo struct // <pyapi>
Definition: framefifo.h:65