Valkka  1.6.1
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  * (c) Copyright 2017-2024 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 Lesser 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 Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser 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>
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:
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 
179  FileSignal signal;
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 condvar/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();
254 }; // <pyapi>
255 
256 #endif
FileSignal
Characteristic signals for the FileThread.
Definition: avfilethread.h:163
@ get_state
query information about the stream
Custom payload Frame.
Definition: frame.h:166
Passes frames to a FrameFifo.
Definition: framefilter.h:530
This class in analogous to the Connection class in live streams.
Definition: avfilethread.h:120
void seek(long int ms_streamtime_)
Seek to a desider stream time.
Definition: avfilethread.cpp:246
AVPacket * avpkt
Data for the next frame in ffmpeg AVPacket format.
Definition: avfilethread.h:143
void stop()
Stop playing the strem.
Definition: avfilethread.cpp:308
~FileStream()
Default destructor.
Definition: avfilethread.cpp:223
FileState state
Decribes the FileStream state: errors, stopped, playing, etc.
Definition: avfilethread.h:142
BasicFrame out_frame
This frame is written to the filterchain (i.e. to FileStream::ctx and there to FileContext::framefilt...
Definition: avfilethread.h:137
long int reftime
Relation between the stream time and wallclock time. See Presention timing and playing.
Definition: avfilethread.h:139
FileContext & ctx
FileContext describing this stream.
Definition: avfilethread.h:131
SetupFrame setupframe
Setup frame written to the filterchain.
Definition: avfilethread.h:136
FileStream(FileContext &ctx)
Default constructor.
Definition: avfilethread.cpp:151
void setRefMstime(long int ms_streamtime_)
Creates a correspondence with the current wallclock time and a desider stream time,...
Definition: avfilethread.cpp:241
long int frame_mstimestamp_
Timestamp of previous frame sent, -1 means there was no previous frame (underscore means stream time)
Definition: avfilethread.h:141
long int pullNextFrame()
Tries to achieve FileStream::target_mstimestamp_ . Sends frames whose timestamps are less than that t...
Definition: avfilethread.cpp:336
long int update(long int mstimestamp)
Tries to achieve mstimestamp: calculates FileStream::target_mstimestamp_ and calls pullNextFrame....
Definition: avfilethread.cpp:313
long int duration
Duration of the stream.
Definition: avfilethread.h:138
long int target_mstimestamp_
Where the stream would like to be (underscore means stream time)
Definition: avfilethread.h:140
void play()
Start playing the stream.
Definition: avfilethread.cpp:302
This class in analogous to LiveThread, but it handles files instead of live streams.
Definition: avfilethread.h:196
void closeFileStreamCall(FileContext &file_ctx)
API method: registers a stream // <pyapi>
Definition: avfilethread.cpp:768
void postRun()
Called after the main execution loop exits, but before joining the thread.
Definition: avfilethread.cpp:473
std::vector< FileStream * > slots_
Slots: a vector of FileStream instances.
Definition: avfilethread.h:215
~FileThread()
Default destructor.
Definition: avfilethread.cpp:451
std::deque< FileSignalContext > signal_fifo
Redefinition of signal fifo (Thread::signal_fifo is now hidden from usage)
Definition: avfilethread.h:212
bool loop
Controls the execution of the main loop.
Definition: avfilethread.h:218
void run()
Main execution loop is defined here.
Definition: avfilethread.cpp:545
std::list< SlotNumber > active_slots
Slots that are activated.
Definition: avfilethread.h:217
void playFileStreamCall(FileContext &file_ctx)
API method: starts playing the stream and feeding frames // <pyapi>
Definition: avfilethread.cpp:781
FileThread(const char *name, FrameFifoContext fifo_ctx=FrameFifoContext())
Default constructor.
Definition: avfilethread.cpp:446
FifoFrameFilter infilter
A FrameFilter for writing incoming frames.
Definition: avfilethread.h:209
FifoFrameFilter & getFrameFilter()
API method: get filter for sending frames with live555 // <pyapi>
Definition: avfilethread.cpp:813
FrameFifo infifo
A FrameFifo for incoming frames.
Definition: avfilethread.h:208
void openFileStreamCall(FileContext &file_ctx)
API method: de-registers a stream // <pyapi>
Definition: avfilethread.cpp:749
void stopFileStreamCall(FileContext &file_ctx)
API method: stops playing the stream and feeding frames // <pyapi>
Definition: avfilethread.cpp:787
void preRun()
Called before entering the main execution loop, but after creating the thread.
Definition: avfilethread.cpp:472
void seekFileStreamCall(FileContext &file_ctx)
API method: seek to a certain point // <pyapi>
Definition: avfilethread.cpp:774
void requestStopCall()
API method: Like Thread::stopCall() but does not block // <pyapi>
Definition: avfilethread.cpp:793
A thread-safe combination of a fifo (first-in-first-out) queue and an associated stack.
Definition: framefifo.h:83
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:44
Setup frame.
Definition: frame.h:277
Definition: avfilethread.h:87
void pull()
Definition: avfilethread.cpp:102
~TestFileStream()
Default destructor.
Definition: avfilethread.cpp:86
TestFileStream(const char *filename)
Default constructor.
Definition: avfilethread.cpp:36
AVPacket * avpkt
Data for the next frame in ffmpeg AVPacket format.
Definition: avfilethread.h:100
A class for multithreading with a signaling system.
Definition: thread.h:87
std::string name
Name of the thread.
Definition: thread.h:116
Frame classes.
Definition of FrameFilter and derived classes for various purposes.
FileState
Describes the state of a FileStream.
Definition: avfilethread.h:51
Logging utilities.
This class descibes the origin and state of a FileStream.
Definition: avfilethread.h:68
FrameFilter * framefilter
incoming: the frames are feeded into this FrameFilter // <pyapi>
Definition: avfilethread.h:78
long int seektime_
incoming: used by signal seek_stream // <pyapi>
Definition: avfilethread.h:79
FileState status
outgoing: status of the file // <pyapi>
Definition: avfilethread.h:82
FileContext()
Dummy constructor. Set values by manipulating members // <pyapi>
Definition: avfilethread.h:73
long int mstimestamp
outgoing: current position of the stream (stream time) // <pyapi>
Definition: avfilethread.h:81
std::string filename
incoming: the filename // <pyapi>
Definition: avfilethread.h:76
FileContext(std::string filename, SlotNumber slot, FrameFilter *framefilter, long int st=0)
Default constructor // <pyapi>
Definition: avfilethread.h:69
long int duration
outgoing: duration of the stream // <pyapi>
Definition: avfilethread.h:80
SlotNumber slot
incoming: a unique stream slot that identifies this stream // <pyapi>
Definition: avfilethread.h:77
Identifies the information the signals FileSignal carry.
Definition: avfilethread.h:178
FileContext * file_context
pointer, cause we have return values
Definition: avfilethread.h:180
Describes the stack structure and fifo behaviour for a FrameFifo.
Definition: framefifo.h:56
Base class for multithreading.
Auxiliary routines.
@ none
undefined (initial value)
Definition: usbthread.h:143