Valkka  0.7.1
OpenSource Video Management
filethread.h
Go to the documentation of this file.
1 #ifndef filethread_HEADER_GUARD
2 #define filethread_HEADER_GUARD
3 
4 /*
5  * filethread.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 
38 #include "frame.h"
39 #include "thread.h"
40 #include "framefilter.h"
41 #include "logging.h"
42 #include "tools.h"
43 
48 enum class FileState { // <pyapi>
49  none, // <pyapi>
50  error, // <pyapi>
51  seek, // in the middle of a seek // <pyapi>
52  stop, // stream stopped // <pyapi>
53  play // stream is playing // <pyapi>
54 }; // <pyapi>
55 
56 
65 struct FileContext { // <pyapi>
66  FileContext(std::string filename, SlotNumber slot, FrameFilter* framefilter, long int st=0) : // <pyapi>
67  filename(filename), slot(slot), framefilter(framefilter), seektime_(st), // <pyapi>
68  duration(0), mstimestamp(0), status(FileState::none) // <pyapi>
69  {}
70  FileContext() : filename(""), slot(0), framefilter(NULL), seektime_(0), // <pyapi>
71  duration(0), mstimestamp(0), status(FileState::none) // <pyapi>
72  {}
73  std::string filename;
74  SlotNumber slot;
76  long int seektime_;
77  long int duration;
78  long int mstimestamp;
80 }; // <pyapi>
81 
82 
83 // TODO: still ugly memory leaks with the bitstream filter. Use this class to test things.
85  public:
90  TestFileStream(const char* filename);
91  ~TestFileStream();
92 
93 public:
94  AVFormatContext *input_context;
95 
96 public:
97  AVPacket *avpkt;
98  AVBitStreamFilterContext *annexb;
99  void pull();
100 };
101 
102 
117 class FileStream {
118 
119 public:
124  FileStream(FileContext &ctx);
125  ~FileStream();
126 
127 public:
129  AVFormatContext *input_context;
130  std::vector<AVBitStreamFilterContext*> filters;
131 
132 public:
135  long int duration;
136  long int reftime;
140  AVPacket *avpkt;
141 
142 public: // getters
143  SlotNumber getSlot() {return ctx.slot;}
144 
145 public:
146  void setRefMstime(long int ms_streamtime_);
147  void seek(long int ms_streamtime_);
148  void play();
149  void stop();
150  long int update(long int mstimestamp);
151  long int pullNextFrame();
152 };
153 
154 
160 enum class FileSignal {
161  none,
162  exit,
163  open_stream,
164  close_stream,
165  seek_stream,
166  play_stream,
167  stop_stream,
168  get_state
169 };
170 
171 
178 };
179 
180 
193 class FileThread : public Thread { // <pyapi>
194 
195 public: // <pyapi>
202  FileThread(const char* name, FrameFifoContext fifo_ctx=FrameFifoContext()); // <pyapi>
204  ~FileThread(); // <pyapi>
205 
206 protected: // frame input // TODO: implement writing to files with FileThread
209 
210 protected: // redefinitions
211  std::deque<FileSignalContext> signal_fifo;
212 
213 protected:
214  std::vector<FileStream*> slots_;
215  // TODO: all slots should be done in the future with std::map<SlotNumber,FileStream*>
216  std::list<SlotNumber> active_slots;
217  bool loop;
218  // std::list<FileStream*> streamlist; // TODO: a better event loop: FileStream s that have frames to be presented are queued here
219 
220 /* // some misc. ideas ..
221 protected:
222  int count_streams_seeking; ///< number of stream seeking at the moment
223  std::condition_variable seek_condition; ///< notified when all streams have stopped seeking
224 */
225 
226 
227 public: // redefined virtual functions
228  void run();
229  void preRun();
230  void postRun();
231  void sendSignal(FileSignalContext signal_ctx);
232  void sendSignalAndWait(FileSignalContext signal_ctx);
233 
234 protected:
235  void handleSignals();
236 
237 private: // internal
238  int safeGetSlot (SlotNumber slot, FileStream*& stream);
239  void openFileStream (FileContext &file_ctx);
240  void closeFileStream (FileContext &file_ctx);
241  void seekFileStream (FileContext &file_ctx);
242  void playFileStream (FileContext &file_ctx);
243  void stopFileStream (FileContext &file_ctx);
244 
245 public: // *** C & Python API *** .. these routines go through the convar/mutex locking // <pyapi>
246  void closeFileStreamCall (FileContext &file_ctx);
247  void openFileStreamCall (FileContext &file_ctx);
248  void seekFileStreamCall (FileContext &file_ctx);
249  void playFileStreamCall (FileContext &file_ctx);
250  void stopFileStreamCall (FileContext &file_ctx);
251  void requestStopCall();
252  FifoFrameFilter &getFrameFilter();
253 }; // <pyapi>
254 
255 #endif
AVPacket * avpkt
Data for the next frame in ffmpeg AVPacket format.
Definition: filethread.h:140
long int seektime_
incoming: used by signal seek_stream // <pyapi>
Definition: filethread.h:76
FrameFilter * framefilter
incoming: the frames are feeded into this FrameFilter // <pyapi>
Definition: filethread.h:75
long int duration
outgoing: duration of the stream // <pyapi>
Definition: filethread.h:77
std::string filename
incoming: the filename // <pyapi>
Definition: filethread.h:73
FrameFifo infifo
A FrameFifo for incoming frames.
Definition: filethread.h:207
std::deque< FileSignalContext > signal_fifo
Redefinition of signal fifo (Thread::signal_fifo is now hidden from usage)
Definition: filethread.h:211
This class in analogous to LiveThread, but it handles files instead of live streams.
Definition: filethread.h:193
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:45
signal to AVThread or OpenGLThread
FileState
Describes the state of a FileStream.
Definition: filethread.h:48
FileState status
outgoing: status of the file // <pyapi>
Definition: filethread.h:79
Custom payload Frame.
Definition: frame.h:141
Identifies the information the signals FileSignal carry.
Definition: filethread.h:175
long int mstimestamp
outgoing: current position of the stream (stream time) // <pyapi>
Definition: filethread.h:78
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:69
SetupFrame setupframe
Setup frame written to the filterchain.
Definition: filethread.h:133
long int frame_mstimestamp_
Timestamp of previous frame sent, -1 means there was no previous frame (underscore means stream time)...
Definition: filethread.h:138
FifoFrameFilter infilter
A FrameFilter for writing incoming frames.
Definition: filethread.h:208
Logging utilities.
query information about the stream
SlotNumber slot
incoming: a unique stream slot that identifies this stream // <pyapi>
Definition: filethread.h:74
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: filethread.h:134
This class in analogous to the Connection class in live streams.
Definition: filethread.h:117
FileContext()
Dummy constructor. Set values by manipulating members // <pyapi>
Definition: filethread.h:70
Frame classes.
long int reftime
Relation between the stream time and wallclock time. See Presention timing and playing.
Definition: filethread.h:136
FileState state
Decribes the FileStream state: errors, stopped, playing, etc.
Definition: filethread.h:139
bool loop
Controls the execution of the main loop.
Definition: filethread.h:217
std::list< SlotNumber > active_slots
Slots that are activated.
Definition: filethread.h:216
FileContext & ctx
FileContext describing this stream.
Definition: filethread.h:128
FileSignal
Characteristic signals for the FileThread.
Definition: filethread.h:160
long int target_mstimestamp_
Where the stream would like to be (underscore means stream time)
Definition: filethread.h:137
Definition of FrameFilter and derived classes for various purposes.
FileContext(std::string filename, SlotNumber slot, FrameFilter *framefilter, long int st=0)
Default constructor // <pyapi>
Definition: filethread.h:66
Setup frame for decoders.
Definition: frame.h:191
A class for multithreading with a signaling system.
Definition: thread.h:89
Passes frames to a multiprocessing fifo.
Definition: framefilter.h:452
Auxiliary routines.
long int duration
Duration of the stream.
Definition: filethread.h:135
Definition: filethread.h:84
This class descibes the origin and state of a FileStream.
Definition: filethread.h:65
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: filethread.h:177
AVPacket * avpkt
Data for the next frame in ffmpeg AVPacket format.
Definition: filethread.h:97
std::vector< FileStream * > slots_
Slots: a vector of FileStream instances.
Definition: filethread.h:214