Valkka  0.17.0
OpenSource Video Management
framefilter.h
Go to the documentation of this file.
1 #ifndef framefilter_HEADER_GUARD
2 #define framefilter_HEADER_GUARD
3 /*
4  * framefilter.h : Definition of FrameFilter and derived classes for various purposes
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 
37 #include "frame.h"
38 #include "framefifo.h"
39 
40 
46 class FrameFilter { // <pyapi>
47 
48 public: // <pyapi>
55  FrameFilter(const char* name, FrameFilter* next=NULL); // don't include into the python api (this class is abstract)
56  virtual ~FrameFilter();
57 
58 protected:
59  std::string name;
61 
62 protected: // <pyapi>
63  // does the filtering
64  virtual void go(Frame* frame) = 0;
65 
66 public: // API
69  virtual void run(Frame* frame);
70 }; // <pyapi>
71 
72 
76 class DummyFrameFilter : public FrameFilter { // <pyapi>
77 
78 public: // <pyapi>
79  DummyFrameFilter(const char* name, bool verbose=true, FrameFilter* next=NULL); // <pyapi>
80 
81 protected:
82  bool verbose;
83 
84 protected:
85  void go(Frame* frame);
86 
87 }; // <pyapi>
88 
89 
93 class InfoFrameFilter : public FrameFilter { // <pyapi>
94 
95 public: // <pyapi>
96  InfoFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
97 
98 protected:
99  void go(Frame* frame);
100 }; // <pyapi>
101 
102 
106 class BriefInfoFrameFilter : public FrameFilter { // <pyapi>
107 
108 public: // <pyapi>
109  BriefInfoFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
110 
111 protected:
112  void go(Frame* frame);
113 }; // <pyapi>
114 
115 
125 class ThreadSafeFrameFilter : public FrameFilter { // <pyapi>
126 
127 private:
128  std::mutex mutex;
129 
130 public: // <pyapi>
131  ThreadSafeFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
132 
133 protected:
134  void go(Frame* frame);
135 
136 public:
137  void run(Frame* frame);
138 }; // <pyapi>
139 
140 
141 
142 
143 
144 
149 class ForkFrameFilter : public FrameFilter { // <pyapi>
150 
151 public: // <pyapi>
157  ForkFrameFilter(const char* name, FrameFilter* next=NULL, FrameFilter* next2=NULL); // <pyapi>
158 
159 protected:
160  FrameFilter* next2;
161 
162 protected:
163  void go(Frame* frame);
164 
165 public:
166  void run(Frame* frame);
167 }; // <pyapi>
168 
169 
174 class ForkFrameFilter3 : public FrameFilter { // <pyapi>
175 
176 public: // <pyapi>
183  ForkFrameFilter3(const char* name, FrameFilter* next=NULL, FrameFilter* next2=NULL, FrameFilter* next3=NULL); // <pyapi>
184 
185 protected:
186  FrameFilter* next2;
187  FrameFilter* next3;
188 
189 protected:
190  void go(Frame* frame);
191 
192 public:
193  void run(Frame* frame);
194 }; // <pyapi>
195 
196 
203 class ForkFrameFilterN : public FrameFilter { // <pyapi>
204 
205 public: // <pyapi>
211  ForkFrameFilterN(const char* name); // <pyapi>
214  virtual ~ForkFrameFilterN(); // <pyapi>
215 
216 protected:
217  std::mutex mutex;
218  std::map<std::string,FrameFilter*> framefilters;
219 
220 protected:
221  void go(Frame* frame);
222 
223 public:
224  void run(Frame* frame);
225 
226 public: // <pyapi>
233  bool connect (const char* tag, FrameFilter* filter); // <pyapi>
239  bool disconnect (const char* tag); // <pyapi>
240 }; // <pyapi>
241 
242 
243 
244 
248 class SlotFrameFilter : public FrameFilter { // <pyapi>
249 
250 public: // <pyapi>
251  SlotFrameFilter(const char* name, SlotNumber n_slot, FrameFilter* next=NULL); // <pyapi>
252 
253 protected:
254  unsigned n_slot;
255 
256 protected:
257  void go(Frame* frame);
258 
259 }; // <pyapi>
260 
261 
266 class PassSlotFrameFilter : public FrameFilter { // <pyapi>
267 
268 public: // <pyapi>
269  PassSlotFrameFilter(const char* name, SlotNumber n_slot, FrameFilter* next = NULL); // <pyapi>
270 
271 protected:
272  unsigned n_slot;
273 
274 protected:
275  void go(Frame* frame);
276 
277 public:
278  void run(Frame* frame);
279 }; // <pyapi>
280 
281 
285 class DumpFrameFilter : public FrameFilter { // <pyapi>
286 
287 public: // <pyapi>
288  DumpFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
289 
290 protected:
291  int count;
292 
293 protected:
294  void go(Frame* frame);
295 }; // <pyapi>
296 
297 
302 class CountFrameFilter : public FrameFilter { // <pyapi>
303 
304 public: // <pyapi>
305  CountFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
306 
307 protected:
308  int count;
309 
310 protected:
311  void go(Frame* frame);
312 }; // <pyapi>
313 
314 
315 
319 class TimestampFrameFilter : public FrameFilter { // <pyapi>
320 
321 public: // <pyapi>
322  TimestampFrameFilter(const char* name, FrameFilter* next=NULL, long int msdiff_max=TIMESTAMP_CORRECT_TRESHOLD); // <pyapi>
323 
324 protected:
325  long int mstime_delta;
326  long int msdiff_max;
327 
328 protected:
329  void go(Frame* frame);
330 }; // <pyapi>
331 
332 
333 
337 class TimestampFrameFilter2 : public FrameFilter { // <pyapi>
338 
339 public: // <pyapi>
340  TimestampFrameFilter2(const char* name, FrameFilter* next=NULL, long int msdiff_max=TIMESTAMP_CORRECT_TRESHOLD); // <pyapi>
341 
342 protected:
343  long int mstime_delta;
344  long int msdiff_max;
345  long int savedtimestamp;
346 
347 protected:
348  void go(Frame* frame);
349 }; // <pyapi>
350 
351 
355 class DummyTimestampFrameFilter : public FrameFilter { // <pyapi>
356 
357 public: // <pyapi>
358  DummyTimestampFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
359 
360 protected:
361  void go(Frame* frame);
362 }; // <pyapi>
363 
364 
371 class RepeatH264ParsFrameFilter : public FrameFilter { // <pyapi>
372 
373 public: // <pyapi>
374  RepeatH264ParsFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
375 
376 protected:
377  BasicFrame sps, pps;
378  int phase;
379 
380 protected:
381  void go(Frame* frame);
382 
383 public:
384  void run(Frame* frame);
385 
386 }; // <pyapi>
387 
388 
397 class GateFrameFilter : public FrameFilter { // <pyapi>
398 
399 public: // <pyapi>
400  GateFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
401 
402 protected:
403  bool on;
404  bool config_frames;
405  std::mutex mutex;
406 
407 protected:
408  void go(Frame* frame);
409 
410 public:
411  void run(Frame* frame);
412 
413 public: // <pyapi>
414  void set(); // <pyapi>
415  void unSet(); // <pyapi>
416  void passConfigFrames(); // <pyapi>
417  void noConfigFrames(); // <pyapi>
418 }; // <pyapi>
419 
420 
425 class SwitchFrameFilter : public FrameFilter { // <pyapi>
426 
427 public: // <pyapi>
428  SwitchFrameFilter(const char* name, FrameFilter* next1=NULL, FrameFilter* next2=NULL); // <pyapi>
429 
430 protected:
431  FrameFilter* next1;
432  FrameFilter* next2;
433 
434 protected:
435  int index;
436  std::mutex mutex;
437 
438 protected:
439  void go(Frame* frame);
440 
441 public:
442  void run(Frame* frame);
443 
444 public:
445  void set1(); // <pyapi>
446  void set2(); // <pyapi>
447 }; // <pyapi>
448 
454 class TypeFrameFilter : public FrameFilter {
455 
456 public:
457  TypeFrameFilter(const char* name, FrameClass frameclass, FrameFilter* next=NULL);
458 
459 protected:
460  FrameClass frameclass;
461 
462 protected:
463  void go(Frame* frame);
464  void run(Frame* frame);
465 };
466 
467 
468 
474 class CachingGateFrameFilter : public FrameFilter { // <pyapi>
475 
476 public: // <pyapi>
477  CachingGateFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
478 
479 protected:
480  bool on;
481  std::mutex mutex;
482  SetupFrame setupframe; // TODO: shouldn't we have array of setupframes here?
483  bool got_setup;
484 
485 protected:
486  void go(Frame* frame);
487 
488 public:
489  void run(Frame* frame);
490 
491 
492 public: // <pyapi>
493  void set(); // <pyapi>
494  void unSet(); // <pyapi>
495 }; // <pyapi>
496 
497 
498 
507 class SetSlotFrameFilter : public FrameFilter { // <pyapi>
508 
509 public: // <pyapi>
510  SetSlotFrameFilter(const char* name, FrameFilter* next=NULL); // <pyapi>
511 
512 protected:
513  SlotNumber n_slot;
514  std::mutex mutex;
515 
516 protected:
517  void go(Frame* frame);
518 
519 public: // <pyapi>
520  void setSlot(SlotNumber n=0); // <pyapi>
521 
522 }; // <pyapi>
523 
524 
535 class TimeIntervalFrameFilter : public FrameFilter { // <pyapi>
536 
537 public: // <pyapi>
538  TimeIntervalFrameFilter(const char* name, long int mstimedelta, FrameFilter* next=NULL); // <pyapi>
539 
540 protected:
541  long int mstimedelta;
542  long int prevmstimestamp;
543 
544 protected:
545  void go(Frame* frame);
546 
547 public:
548  void run(Frame* frame);
549 
550 }; // <pyapi>
551 
552 
560 class FifoFrameFilter : public FrameFilter { // <pyapi>
561 
562 public: // <pyapi>
568  FifoFrameFilter(const char* name, FrameFifo* framefifo);
569 
570 protected:
571  FrameFifo* framefifo;
572 
573 protected:
574  void go(Frame* frame);
575 }; // <pyapi>
576 
577 
585 class BlockingFifoFrameFilter : public FrameFilter { // <pyapi>
586 
587 public: // <pyapi>
588  BlockingFifoFrameFilter(const char* name, FrameFifo* framefifo);
589 
590 protected:
591  FrameFifo* framefifo;
592 
593 protected:
594  void go(Frame* frame);
595 }; // <pyapi>
596 
597 
598 
608 class SwScaleFrameFilter : public FrameFilter { // <pyapi>
609 
610 public: // <pyapi>
611  SwScaleFrameFilter(const char* name, int target_width, int target_height, FrameFilter* next=NULL);
612  ~SwScaleFrameFilter();
613 
614 protected: // initialized at constructor
617  int width;
618  int height;
619  AVRGBFrame outputframe;
620  SwsContext *sws_ctx;
621 
622 protected:
623  void go(Frame* frame);
624 
625 public:
626  void run(Frame* frame);
627 }; // <pyapi>
628 
629 
630 
631 
632 #endif
virtual ~FrameFilter()
Virtual destructor // <pyapi>
Definition: framefilter.cpp:42
Custom payload Frame.
Definition: frame.h:160
Changes the slot number of the Frame.
Definition: framefilter.h:507
Thread safe system of fifo and a stack.
virtual void go(Frame *frame)=0
Does the actual filtering/modification to the Frame. Define in subclass.
A "hello world" demo class: prints its own name if verbose is set to true.
Definition: framefilter.h:76
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:72
For H264, some cameras don&#39;t send sps and pps packets again before every keyframe.
Definition: framefilter.h:371
int target_height
target frame height
Definition: framefilter.h:616
Corrects erroneous timestamps (while preserving timestamp distances).
Definition: framefilter.h:337
Passes frames to a multiprocessing fifo.
Definition: framefilter.h:585
FrameFilter * next
The next frame filter in the chain to be applied.
Definition: framefilter.h:60
SwsContext * sws_ctx
FFmpeg scaling context structure.
Definition: framefilter.h:620
Interpolate from YUV bitmap to RGB.
Definition: framefilter.h:608
turn decoding on
Frame classes.
FrameFilter s that are fed from various different threads, should be protected with this...
Definition: framefilter.h:125
Passes through frames of certain type only.
Definition: framefilter.h:454
FrameFilter(const char *name, FrameFilter *next=NULL)
Default constructor.
Definition: framefilter.cpp:39
Replicates frame flow to arbitrary number of outputs.
Definition: framefilter.h:203
Passes frame to one of the two terminals.
Definition: framefilter.h:425
virtual void run(Frame *frame)
Calls this->go(Frame* frame) and then calls the this->next->run(Frame* frame) (if this->next != NULL)...
Definition: framefilter.cpp:45
Sets the frame slot value.
Definition: framefilter.h:248
Dumps each received packet to a file: use with care! For debugging purposes only. ...
Definition: framefilter.h:285
FrameClass
Enumeration of Frame classes used by Valkka.
Definition: frame.h:51
Replicates frame flow to three filters Use this frame filter to create frame filter tree structures...
Definition: framefilter.h:174
Passes through frames with a certain slot number only.
Definition: framefilter.h:266
Counts frames passed through this filter.
Definition: framefilter.h:302
When turned on, passes frames.
Definition: framefilter.h:397
Substitute timestamps with the time they arrive to the client.
Definition: framefilter.h:355
int target_width
target frame width
Definition: framefilter.h:615
Caches SetupFrame s.
Definition: framefilter.h:474
Setup frame.
Definition: frame.h:270
Decoded YUV frame in a non-planar format (thus "NP")
Definition: frame.h:430
Corrects erroneous timestamps (while preserving timestamp distances).
Definition: framefilter.h:319
Frame: An abstract queueable class.
Definition: frame.h:108
Passes frames to a FrameFifo.
Definition: framefilter.h:560
std::map< std::string, FrameFilter * > framefilters
nametag to connecting FrameFilter mapping
Definition: framefilter.h:218
Dump the beginning of Frame&#39;s payload into stdout.
Definition: framefilter.h:93
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:46
Pass frames, but not all of them - only on regular intervals.
Definition: framefilter.h:535
Replicates frame flow to two filters Use this frame filter to create frame filter tree structures...
Definition: framefilter.h:149
Dump the beginning of Frame&#39;s payload into stdout in a one-liner.
Definition: framefilter.h:106