forked from francescolonardo/ScreenRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenRecorder.h
177 lines (154 loc) · 4.4 KB
/
ScreenRecorder.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "CommandLineInterface.h"
#if defined(__linux__)
#include <X11/Xlib.h>
#elif defined(_WIN32) || defined(__CYGWIN__)
#include <wtypes.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <CoreGraphics/CoreGraphics.h>
#endif
#include <string>
#include <queue>
#include <set>
#include <fstream>
#include <thread>
#include <mutex>
#include <condition_variable>
#define STATELESS 0
#define RECORDING 1
#define PAUSED 2
#define STOPPED 3
#define __STDC_CONSTANT_MACROS
// FFMPEG LIBRARIES
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavcodec/avfft.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavutil/opt.h"
#include "libavutil/common.h"
#include "libavutil/channel_layout.h"
#include "libavutil/imgutils.h"
#include "libavutil/mathematics.h"
#include "libavutil/samplefmt.h"
#include "libavutil/time.h"
#include "libavutil/opt.h"
#include "libavutil/pixdesc.h"
#include "libavutil/file.h"
#include "libavutil/log.h"
#include "libavutil/error.h"
#include "libavutil/audio_fifo.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
}
using namespace std;
class ScreenRecorder
{
private:
// *** CLI object
CommandLineInterface cli{};
// executable's parameters
string area_size, area_offsets;
string area_width, area_height, area_x_offset, area_y_offset;
string video_fps;
bool audio_flag;
string out_filename;
// rec_status change management
uint8_t rec_status;
mutex rec_status_mtx;
// av queues management
queue<AVPacket *> vin_packets_q;
mutex vin_packets_q_mtx;
condition_variable vin_packets_q_cv;
queue<AVPacket *> ain_packets_q;
mutex ain_packets_q_mtx;
condition_variable ain_packets_q_cv;
// av_frame(...) mutual exclusion
mutex av_write_frame_mtx;
// captured/elaborated packets mutual esclusion
uint64_t v_packets_captured, v_packets_elaborated; // counters
mutex v_packets_captured_mtx;
mutex v_packets_elaborated_mtx;
// errors' management
ofstream log_file; // logger()
char err_buf[32]; // debugger()
// globals' definition
char tmp_str[100];
int value; // value returned by ffmpeg's functions
// threads' pointers
unique_ptr<thread> capture_video_thrd_ptr;
unique_ptr<thread> elaborate_video_thrd_ptr;
unique_ptr<thread> capture_audio_thrd_ptr;
unique_ptr<thread> elaborate_audio_thrd_ptr;
unique_ptr<thread> change_rec_status_thrd_ptr;
// video
AVInputFormat *vin_format;
AVFormatContext *vin_format_context;
AVStream *vin_stream;
AVRational vin_fps;
int vin_stream_idx;
int vout_stream_idx;
AVCodecContext *vin_codec_context;
AVOutputFormat *out_format; // extra
AVFormatContext *out_format_context; // extra
AVStream *vout_stream;
AVCodecContext *vout_codec_context;
AVPacket *vin_packet;
AVFrame *vin_frame;
SwsContext *rescaler_context;
AVFrame *vout_frame;
AVPacket *vout_packet;
#if defined(__APPLE__) && defined(__MACH__)
AVFrame *vout_frame_filtered;
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
string filter_descr;
#endif
// audio
AVInputFormat *ain_format;
AVFormatContext *ain_format_context;
AVStream *ain_stream;
int ain_stream_idx;
int aout_stream_idx;
AVCodecContext *ain_codec_context;
AVStream *aout_stream;
AVCodecContext *aout_codec_context;
AVPacket *ain_packet;
AVFrame *ain_frame;
SwrContext *resampler_context;
AVAudioFifo *a_fifo;
AVFrame *aout_frame;
AVPacket *aout_packet;
string getCurrentTimestamp();
void logFileError(string str);
void debugThrowError(string str, int level, int errnum);
string getCurrentTimeRecorded(unsigned int packets_counter, unsigned int video_fps);
void changeRecordingStatus();
void openInputDeviceVideo();
void openInputDeviceAudio();
void prepareDecoderVideo();
void prepareDecoderAudio();
void prepareEncoderVideo();
void prepareEncoderAudio();
void prepareCaptureVideo();
void prepareCaptureAudio();
#if defined(__APPLE__) && defined(__MACH__)
void prepareFilterVideo();
#endif
void prepareOutputFile();
void capturePacketsVideo();
void elaboratePacketsVideo();
void capturePacketsAudio();
void elaboratePacketsAudio();
void deallocateResourcesVideo();
void deallocateResourcesAudio();
public:
ScreenRecorder(string area_size, string area_offsets, string video_fps, bool audio_flag, string out_filename);
~ScreenRecorder();
void record();
};