Valkka  0.17.0
OpenSource Video Management
muxer.h
Go to the documentation of this file.
1 #ifndef muxer_HEADER_GUARD
2 #define muxer_HEADER_GUARD
3 /*
4  * muxer.h : FFmpeg muxers, implemented as Valkka framefilters
5  *
6  * Copyright 2017, 2018, 2019 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 
42 #endif
43 
44 
45 #include "constant.h"
46 #include "framefilter.h"
47 
48 
49 class MuxFrameFilter : public FrameFilter { // <pyapi>
50 
51 public: // <pyapi>
52  MuxFrameFilter(const char* name, FrameFilter *next = NULL); // <pyapi>
53  virtual ~MuxFrameFilter(); // <pyapi>
54 
55 protected:
56  bool active;
57  bool ready;
58  bool initialized;
59  long int mstimestamp0;
60  long int zerotime;
61  bool zerotimeset;
62  uint32_t missing, ccf;
63  std::string format_name;
64 
65 protected: //libav stuff
66  AVFormatContext *av_format_ctx;
67  AVIOContext *avio_ctx;
68  uint8_t *avio_ctx_buffer;
69  AVRational timebase;
70  std::vector<AVCodecContext*> codec_contexes;
71  std::vector<AVStream*> streams;
72  AVFormatContext *av_format_context;
73  AVPacket *avpkt;
74  AVDictionary *av_dict;
75 
76 
77  static const size_t avio_ctx_buffer_size = 4096;
78 
79 protected: //mutex stuff
80  std::mutex mutex;
81  std::condition_variable condition;
82 
83 protected: //frames
84  std::vector<SetupFrame> setupframes;
85 
86 public:
87  // MuxFrame internal_frame; ///< outgoing muxed frame // TODO
88  BasicFrame internal_frame;
89 
90 protected:
91  virtual void defineMux() = 0;
92  virtual void go(Frame* frame);
93  void initMux();
94  void closeMux();
95  void deActivate_();
96 
97 public: // API calls // <pyapi>
98  // setFileName(const char* fname); ///< Sets the output filename // <pyapi>
99  void activate(long int zerotime=0);
100  void deActivate();
101 
102 public:
103  static int write_packet(void *opaque, uint8_t *buf, int buf_size);
104  static int read_packet(void *opaque, uint8_t *buf, int buf_size) {return 0;} // dummy function
105  static int64_t seek(void *opaque, int64_t offset, int whence) {return 0;} // dummy function
106 }; // <pyapi>
107 
108 
109 
110 class FragMP4MuxFrameFilter : public MuxFrameFilter { // <pyapi>
111 
112 public: // <pyapi>
113  FragMP4MuxFrameFilter(const char* name, FrameFilter *next = NULL); // <pyapi>
114  virtual ~FragMP4MuxFrameFilter(); // <pyapi>
115 
116 public:
117  // MP4MuxFrame internal_frame; // TODO
118  BasicFrame internal_frame;
119 
120 protected:
121  virtual void defineMux();
122 }; // <pyapi>
123 
124 
125 
126 
127 
virtual void go(Frame *frame)
Does the actual filtering/modification to the Frame. Define in subclass.
Definition: muxer.cpp:229
void initMux()
Open file, reserve codec_contexes, streams, write preamble, set initialized=true if success...
Definition: muxer.cpp:79
Custom payload Frame.
Definition: frame.h:160
Constant/default values, version numbers.
std::mutex mutex
Mutex protecting the "active" boolean.
Definition: muxer.h:80
bool ready
Got enough setup frames.
Definition: muxer.h:57
void deActivate()
Stop streaming // <pyapi>
Definition: muxer.cpp:418
bool active
Writing to file has been requested (but not necessarily achieved..)
Definition: muxer.h:56
FrameFilter * next
The next frame filter in the chain to be applied.
Definition: framefilter.h:60
std::vector< SetupFrame > setupframes
deep copies of the arrived setup frames
Definition: muxer.h:84
void closeMux()
Close file, dealloc codec_contexes, streams.
Definition: muxer.cpp:192
Definition: muxer.h:110
long int mstimestamp0
Time of activation (i.e. when the recording started)
Definition: muxer.h:59
Definition of FrameFilter and derived classes for various purposes.
long int zerotime
Start time set explicitly by the user.
Definition: muxer.h:60
void activate(long int zerotime=0)
Request streaming to asap (when config frames have arrived) // <pyapi>
Definition: muxer.cpp:407
Frame: An abstract queueable class.
Definition: frame.h:108
bool initialized
File was opened ok : codec_contexes, streams and av_format_context reserved (should be freed at some ...
Definition: muxer.h:58
The mother class of all frame filters! FrameFilters are used to create "filter chains".
Definition: framefilter.h:46
std::condition_variable condition
Condition variable for the mutex.
Definition: muxer.h:81
Definition: muxer.h:49
virtual void defineMux()=0
Define container format (format_name) & muxing parameters (av_dict). Define in child classes...