Valkka  1.6.1
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  * (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 #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:
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();
168 }; // <pyapi>
169 
170 
171 
172 
173 
Passes frames to a multiprocessing fifo.
Definition: framefilter.h:554
FrameFifo using file descriptors.
Definition: framefifo.h:132
File Descriptor Writer Thread.
Definition: fdwritethread.h:124
std::list< FDWrite * > fd_writes
For iterating over the FDWrite entries.
Definition: fdwritethread.h:140
void preRun()
Called before entering the main execution loop, but after creating the thread.
Definition: fdwritethread.cpp:222
void requestStopCall()
API method: stops the thread.
Definition: fdwritethread.cpp:377
FDWriteThread(const char *name, FrameFifoContext fifo_ctx=FrameFifoContext())
Default constructor.
Definition: fdwritethread.cpp:51
std::vector< FDWrite * > slots_
For fast, pointer-arithmetic-based indexing of the slots.
Definition: fdwritethread.h:139
void preJoin()
Called before the thread is joined.
Definition: fdwritethread.cpp:228
void handleSignal(const FDWriteSignalContext &signal_ctx)
Handle an individual signal.
Definition: fdwritethread.cpp:235
BlockingFifoFrameFilter infilter_block
Incoming frames can also be written here. If stack runs out of frames, writing will block.
Definition: fdwritethread.h:138
FifoFrameFilter & getFrameFilter()
API method: get filter for sending frames // <pyapi>
Definition: fdwritethread.cpp:398
FDFrameFifo infifo
Incoming frames (also signal frames) are read from here.
Definition: fdwritethread.h:136
fd_set read_fds
File descriptor sets used by select.
Definition: fdwritethread.h:141
void run()
Main execution loop is defined here.
Definition: fdwritethread.cpp:130
void registerStreamCall(const FDWriteContext &ctx)
API method: registers a stream // <pyapi>
Definition: fdwritethread.cpp:337
void deregisterStreamCall(const FDWriteContext &ctx)
API method: de-registers a stream // <pyapi>
Definition: fdwritethread.cpp:357
int nfds
Max file descriptor number.
Definition: fdwritethread.h:142
FifoFrameFilter infilter
Write incoming frames here // TODO: add a chain of correcting FrameFilter(s)
Definition: fdwritethread.h:137
void postJoin()
Called after the thread has been joined.
Definition: fdwritethread.cpp:231
void postRun()
Called after the main execution loop exits, but before joining the thread.
Definition: fdwritethread.cpp:225
Definition: fdwritethread.h:66
FDWrite(FrameFifo &fifo, const FDWriteContext &ctx)
Default constructor.
Definition: fdwritethread.cpp:39
const FDWriteContext & ctx
Identifies the connection type, stream address, etc.
Definition: fdwritethread.h:73
virtual ~FDWrite()
Default virtual destructor.
Definition: fdwritethread.cpp:43
FrameFifo & fifo
Outgoing Frames are finally recycled here.
Definition: fdwritethread.h:74
Passes frames to a FrameFifo.
Definition: framefilter.h:530
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
A class for multithreading with a signaling system.
Definition: thread.h:87
std::string name
Name of the thread.
Definition: thread.h:116
Thread safe system of fifo and a stack.
Definition of FrameFilter and derived classes for various purposes.
Describes an outgoing file descriptor connection.
Definition: fdwritethread.h:49
SlotNumber slot
A unique stream slot that identifies this stream // <pyapi>
Definition: fdwritethread.h:53
int fd
file descriptor // <pyapi>
Definition: fdwritethread.h:52
Encapsulate data sent to FDWriteThread with a SignalFrame.
Definition: fdwritethread.h:107
Information sent with a signal to FDWriteThread.
Definition: fdwritethread.h:86
FDWriteContext fd_write_ctx
< Identifies the stream
Definition: fdwritethread.h:88
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:56
Base class for multithreading.
@ none
undefined (initial value)
Definition: usbthread.h:143