Valkka  0.17.0
OpenSource Video Management
avfilethread.h
Go to the documentation of this file.
1 #ifndef avfilethread_HEADER_GUARD
2 #define avfilethread_HEADER_GUARD
3 
4 /*
5  * avfilethread.h : A Thread handling files and sending frames to fifo
6  *
7  * Copyright 2017, 2018 Valkka Security Ltd. and Sampsa Riikonen.
8  *
9  * Authors: Sampsa Riikonen <sampsa.riikonen@iki.fi>
10  *
11  * This file is part of the Valkka library.
12  *
13  * Valkka is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU Affero General Public License as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Affero General Public License for more details.
22  *
23  * You should have received a copy of the GNU Affero General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>
25  *
26  */
27 
28 // TODO: implement this as a subclass of AbstractFileThread
29 // TODO: rename FileThread to AVFileThread
30 
41 #include "frame.h"
42 #include "thread.h"
43 #include "framefilter.h"
44 #include "logging.h"
45 #include "tools.h"
46 
51 enum class FileState { // <pyapi>
52  none, // <pyapi>
53  error, // <pyapi>
54  seek, // in the middle of a seek // <pyapi>
55  stop, // stream stopped // <pyapi>
56  play // stream is playing // <pyapi>
57 }; // <pyapi>
58 
59 
68 struct FileContext { // <pyapi>
69  FileContext(std::string filename, SlotNumber slot, FrameFilter* framefilter, long int st=0) : // <pyapi>
70  filename(filename), slot(slot), framefilter(framefilter), seektime_(st), // <pyapi>
71  duration(0), mstimestamp(0), status(FileState::none) // <pyapi>
72  {}
73  FileContext() : filename(""), slot(0), framefilter(NULL), seektime_(0), // <pyapi>
74  duration(0), mstimestamp(0), status(FileState::none) // <pyapi>
75  {}
76  std::string filename;
77  SlotNumber slot;
79  long int seektime_;
80  long int duration;
81  long int mstimestamp;
83 }; // <pyapi>
84 
85 
86 // TODO: still ugly memory leaks with the bitstream filter. Use this class to test things.
88  public:
93  TestFileStream(const char* filename);
94  ~TestFileStream();
95 
96 public:
97  AVFormatContext *input_context;
98 
99 public:
100  AVPacket *avpkt;
101  AVBitStreamFilterContext *annexb;
102  void pull();
103 };
104 
105 
120 class FileStream {
121 
122 public:
127  FileStream(FileContext &ctx);
128  ~FileStream();
129 
130 public:
132  AVFormatContext *input_context;
133  std::vector<AVBitStreamFilterContext*> filters;
134 
135 public:
138  long int duration;
139  long int reftime;
143  AVPacket *avpkt;
144 
145 public: // getters
146  SlotNumber getSlot() {return ctx.slot;}
147 
148 public:
149  void setRefMstime(long int ms_streamtime_);
150  void seek(long int ms_streamtime_);
151  void play();
152  void stop();
153  long int update(long int mstimestamp);
154  long int pullNextFrame();
155 };
156 
157 
163 enum class FileSignal {
164  none,
165  exit,
166  open_stream,
167  close_stream,
168  seek_stream,
169  play_stream,
170  stop_stream,
171  get_state
172 };
173 
174 
181 };
182 
183 
196 class FileThread : public Thread { // <pyapi>
197 
198 public: // <pyapi>
203  FileThread(const char* name, FrameFifoContext fifo_ctx=FrameFifoContext()); // <pyapi>
205  ~FileThread(); // <pyapi>
206 
207 protected: // frame input // TODO: implement writing to files with FileThread
210 
211 protected: // redefinitions
212  std::deque<FileSignalContext> signal_fifo;
213 
214 protected:
215  std::vector<FileStream*> slots_;
216  // TODO: all slots should be done in the future with std::map<SlotNumber,FileStream*>
217  std::list<SlotNumber> active_slots;
218  bool loop;
219  // std::list<FileStream*> streamlist; // TODO: a better event loop: FileStream s that have frames to be presented are queued here
220 
221 /* // some misc. ideas ..
222 protected:
223  int count_streams_seeking; ///< number of stream seeking at the moment
224  std::condition_variable seek_condition; ///< notified when all streams have stopped seeking
225 */
226 
227 
228 public: // redefined virtual functions
229  void run();
230  void preRun();
231  void postRun();
232  void sendSignal(FileSignalContext signal_ctx);
233  void sendSignalAndWait(FileSignalContext signal_ctx);
234 
235 protected:
236  void handleSignals();
237 
238 private: // internal
239  int safeGetSlot (SlotNumber slot, FileStream*& stream);
240  void openFileStream (FileContext &file_ctx);
241  void closeFileStream (FileContext &file_ctx);
242  void seekFileStream (FileContext &file_ctx);
243  void playFileStream (FileContext &file_ctx);
244  void stopFileStream (FileContext &file_ctx);
245 
246 public: // *** C & Python API *** .. these routines go through the convar/mutex locking // <pyapi>
247  void closeFileStreamCall (FileContext &file_ctx);
248  void openFileStreamCall (FileContext &file_ctx);
249  void seekFileStreamCall (FileContext &file_ctx);
250  void playFileStreamCall (FileContext &file_ctx);
251  void stopFileStreamCall (FileContext &file_ctx);
252  void requestStopCall();
253  FifoFrameFilter &getFrameFilter();
254 }; // <pyapi>
255 
256 #endif
AVPacket * avpkt
Data for the next frame in ffmpeg AVPacket format.
Definition: avfilethread.h:143
long int seektime_
incoming: used by signal seek_stream // <pyapi>
Definition: avfilethread.h:79
FrameFilter * framefilter
incoming: the frames are feeded into this FrameFilter // <pyapi>
Definition: avfilethread.h:78
long int duration
outgoing: duration of the stream // <pyapi>
Definition: avfilethread.h:80
std::string filename
incoming: the filename // <pyapi>
Definition: avfilethread.h:76
FrameFifo infifo
A FrameFifo for incoming frames.
Definition: avfilethread.h:208
std::deque< FileSignalContext > signal_fifo
Redefinition of signal fifo (Thread::signal_fifo is now hidden from usage)
Definition: avfilethread.h:212
This class in analogous to LiveThread, but it handles files instead of live streams.
Definition: avfilethread.h:196
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
FileState
Describes the state of a FileStream.
Definition: avfilethread.h:51
FileState status
outgoing: status of the file // <pyapi>
Definition: avfilethread.h:82
Custom payload Frame.
Definition: frame.h:160
Identifies the information the signals FileSignal carry.
Definition: avfilethread.h:178
long int mstimestamp
outgoing: current position of the stream (stream time) // <pyapi>
Definition: avfilethread.h:81
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:72
SetupFrame setupframe
Setup frame written to the filterchain.
Definition: avfilethread.h:136
long int frame_mstimestamp_
Timestamp of previous frame sent, -1 means there was no previous frame (underscore means stream time)...
Definition: avfilethread.h:141
FifoFrameFilter infilter
A FrameFilter for writing incoming frames.
Definition: avfilethread.h:209
Logging utilities.
FileSignal
Characteristic signals for the FileThread.
Definition: avfilethread.h:163
undefined (initial value)
Definition: usbthread.h:143
SlotNumber slot
incoming: a unique stream slot that identifies this stream // <pyapi>
Definition: avfilethread.h:77
Base class for multithreading.
BasicFrame out_frame
This frame is written to the filterchain (i.e. to FileStream::ctx and there to FileContext::framefilt...
Definition: avfilethread.h:137
This class in analogous to the Connection class in live streams.
Definition: avfilethread.h:120
FileContext()
Dummy constructor. Set values by manipulating members // <pyapi>
Definition: avfilethread.h:73
Frame classes.
long int reftime
Relation between the stream time and wallclock time. See Presention timing and playing.
Definition: avfilethread.h:139
FileState state
Decribes the FileStream state: errors, stopped, playing, etc.
Definition: avfilethread.h:142
bool loop
Controls the execution of the main loop.
Definition: avfilethread.h:218
std::list< SlotNumber > active_slots
Slots that are activated.
Definition: avfilethread.h:217
FileContext & ctx
FileContext describing this stream.
Definition: avfilethread.h:131
long int target_mstimestamp_
Where the stream would like to be (underscore means stream time)
Definition: avfilethread.h:140
Definition of FrameFilter and derived classes for various purposes.
query information about the stream
FileContext(std::string filename, SlotNumber slot, FrameFilter *framefilter, long int st=0)
Default constructor // <pyapi>
Definition: avfilethread.h:69
Setup frame.
Definition: frame.h:270
A class for multithreading with a signaling system.
Definition: thread.h:90
Passes frames to a FrameFifo.
Definition: framefilter.h:560
Auxiliary routines.
long int duration
Duration of the stream.
Definition: avfilethread.h:138
Definition: avfilethread.h:87
This class descibes the origin and state of a FileStream.
Definition: avfilethread.h:68
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:46
FileContext * file_context
pointer, cause we have return values
Definition: avfilethread.h:180
AVPacket * avpkt
Data for the next frame in ffmpeg AVPacket format.
Definition: avfilethread.h:100
std::vector< FileStream * > slots_
Slots: a vector of FileStream instances.
Definition: avfilethread.h:215