Valkka  0.17.0
OpenSource Video Management
fdwritethread.h
1 #ifndef fdwriterthread_HEADER_GUARD
2 #define fdwriterthread_HEADER_GUARD
3 /*
4  * fdwriterthread.h : A general thread that write frames into something described by a file descriptor
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 #endif
37 
38 #include <sys/select.h>
39 #include "thread.h"
40 #include "framefilter.h"
41 #include "framefifo.h"
42 
43 
44 // (1) A data structure for describing the outgoing connection
45 
49 struct FDWriteContext { // <pyapi>
50  FDWriteContext() {} // <pyapi>
51  FDWriteContext(int fd, SlotNumber slot) : fd(fd), slot(slot) {} // <pyapi>
52  int fd;
53  SlotNumber slot;
54 }; // <pyapi>
55 
56 inline std::ostream& operator<< (std::ostream& os, const FDWriteContext& ctx) {
57  // https://stackoverflow.com/questions/4571611/making-operator-virtual
58  os << "<FDWriteContext : slot = " << ctx.slot << " / fd = " << ctx.fd << " >";
59  return os;
60 }
61 
62 
63 
64 // (2) A class for handling the outgoing connection. Used internally by the FDWriteThread.
65 
66 class FDWrite {
67 
68 public:
69  FDWrite(FrameFifo& fifo, const FDWriteContext& ctx);
70  virtual ~FDWrite();
71 
72 public: // init'd at constructor time
75  std::deque<Frame*> internal_fifo;
76 
77 };
78 
79 
80 
81 // (3) Define the communication with the Thread
82 
89 };
90 
91 
95 enum class FDWriteSignal {
96  none,
97  exit,
98  register_stream,
99  deregister_stream
100 };
101 
102 
108  FDWriteSignal signal;
109  FDWriteSignalPars pars;
110 };
111 
112 
113 
114 
115 // (4) The Thread class
116 
124 class FDWriteThread : public Thread { // <pyapi>
125 
126 public: // <pyapi>
132  FDWriteThread(const char* name, FrameFifoContext fifo_ctx = FrameFifoContext()); // <pyapi>
133  virtual ~FDWriteThread(); // <pyapi>
134 
135 protected: // frame input
139  std::vector<FDWrite*> slots_;
140  std::list<FDWrite*> fd_writes;
141  fd_set write_fds, read_fds;
142  int nfds;
143  struct timeval timeout;
144 
145 
146 public: // redefined virtual functions
147  void run();
148  void preRun();
149  void postRun();
150  void preJoin();
151  void postJoin();
152 
153 protected:
154  void handleSignal(const FDWriteSignalContext &signal_ctx);
155 
156 private: // internal
157  void setMaxFD ();
158  int safeGetSlot (const SlotNumber slot, FDWrite*& fd_write);
159  void registerStream (const FDWriteContext &ctx);
160  void deregisterStream (const FDWriteContext &ctx);
161 
162 public: // *** C & Python API *** .. these routines go through the condvar/mutex locking // <pyapi>
163  // inbound streams
164  void registerStreamCall (const FDWriteContext &ctx);
165  void deregisterStreamCall (const FDWriteContext &ctx);
166  void requestStopCall();
167  FifoFrameFilter &getFrameFilter();
168 }; // <pyapi>
169 
170 
171 
172 
173 
int nfds
Max file descriptor number.
Definition: fdwritethread.h:142
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:45
signal to AVThread or OpenGLThread. Also custom signals to custom Threads
FDFrameFifo infifo
Incoming frames (also signal frames) are read from here.
Definition: fdwritethread.h:136
FrameFifo & fifo
Outgoing Frames are finally recycled here.
Definition: fdwritethread.h:74
Thread safe system of fifo and a stack.
SlotNumber slot
A unique stream slot that identifies this stream // <pyapi>
Definition: fdwritethread.h:53
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:72
Information sent with a signal to FDWriteThread.
Definition: fdwritethread.h:86
Describes an outgoing file descriptor connection.
Definition: fdwritethread.h:49
undefined (initial value)
Definition: usbthread.h:143
int fd
file descriptor // <pyapi>
Definition: fdwritethread.h:52
Base class for multithreading.
Passes frames to a multiprocessing fifo.
Definition: framefilter.h:585
BlockingFifoFrameFilter infilter_block
Incoming frames can also be written here. If stack runs out of frames, writing will block...
Definition: fdwritethread.h:138
Definition: fdwritethread.h:66
FrameFifo using file descriptors.
Definition: framefifo.h:121
File Descriptor Writer Thread.
Definition: fdwritethread.h:124
const FDWriteContext & ctx
Identifies the connection type, stream address, etc.
Definition: fdwritethread.h:73
Definition of FrameFilter and derived classes for various purposes.
fd_set read_fds
File descriptor sets used by select.
Definition: fdwritethread.h:141
A class for multithreading with a signaling system.
Definition: thread.h:90
FifoFrameFilter infilter
Write incoming frames here // TODO: add a chain of correcting FrameFilter(s)
Definition: fdwritethread.h:137
Passes frames to a FrameFifo.
Definition: framefilter.h:560
Encapsulate data sent to FDWriteThread with a SignalFrame.
Definition: fdwritethread.h:107
FDWriteContext fd_write_ctx
< Identifies the stream
Definition: fdwritethread.h:88
std::list< FDWrite * > fd_writes
For iterating over the FDWrite entries.
Definition: fdwritethread.h:140
std::vector< FDWrite * > slots_
For fast, pointer-arithmetic-based indexing of the slots.
Definition: fdwritethread.h:139