-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScreenRecorder.cpp
More file actions
1153 lines (924 loc) · 39.1 KB
/
ScreenRecorder.cpp
File metadata and controls
1153 lines (924 loc) · 39.1 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by enrico on 11/08/21.
//
#include "ScreenRecorder.h"
using namespace std;
//Show Dshow Device
//used on Windows to select the audio device
void show_dshow_device() {
AVFormatContext* pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options, "list_devices", "true", 0);
AVInputFormat* iformat = av_find_input_format("dshow");
printf("========Device Info=============\n");
avformat_open_input(&pFormatCtx, "video=dummy", iformat, &options);
printf("================================\n");
}
//Show AVFoundation Device
void show_avfoundation_device() {
AVFormatContext* pFormatCtx = avformat_alloc_context();
AVDictionary* options = NULL;
av_dict_set(&options, "list_devices", "true", 0);
AVInputFormat* iformat = av_find_input_format("avfoundation");
printf("==AVFoundation Device Info===\n");
avformat_open_input(&pFormatCtx, "", iformat, &options);
printf("=============================\n");
}
//get dimensions of main desktop
void getScreenResolution(int& width, int& height) {
#if defined __APPLE__
Display* disp = XOpenDisplay(NULL);
Screen* scrn = DefaultScreenOfDisplay(disp);
width = scrn->width;
height = scrn->height;
#endif
#if defined _WIN32
width = (int)GetSystemMetrics(SM_CXSCREEN);
height = (int)GetSystemMetrics(SM_CYSCREEN);
#endif
#if defined linux
Display* disp = XOpenDisplay(NULL);
Screen* scrn = DefaultScreenOfDisplay(disp);
width = scrn->width;
height = scrn->height;
#endif
if(width%2 != 0) width-=1;
if(height%2 != 0) height-=1;
}
ScreenRecorder::ScreenRecorder() : pauseCapture(false), stopCapture(false), started(false), activeMenu(true), recordAudio(false), disabledMenu(false) {
avcodec_register_all();
avdevice_register_all();
#if defined _WIN32
::SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
deviceName = "";
#endif
getScreenResolution(width, height);
screen_height = height;
screen_width = width;
cout << "Main desktop: " << width << "x" << height << endl;
dir_path = ".";
x_offset = 0;
y_offset = 0;
}
ScreenRecorder::~ScreenRecorder() {
if (started) {
t_video.join();
if (recordAudio)
t_audio.join();
//complete write on file
value = av_write_trailer(outAVFormatContext);
if (value < 0) {
cerr << "Error in writing av trailer" << endl;
exit(-1);
}
//close input device for audio
avformat_close_input(&inAudioFormatContext);
if (inAudioFormatContext == nullptr) {
cout << "inAudioFormatContext close successfully" << endl;
}
else {
cerr << "Error: unable to close the inAudioFormatContext" << endl;
exit(-1);
}
avformat_free_context(inAudioFormatContext);
if (inAudioFormatContext == nullptr) {
cout << "AudioFormat freed successfully" << endl;
}
else {
cerr << "Error: unable to free AudioFormatContext" << endl;
exit(-1);
}
//close input device for video
avformat_close_input(&pAVFormatContext);
if (pAVFormatContext == nullptr) {
cout << "File close successfully" << endl;
}
else {
cerr << "Error: unable to close the file" << endl;
exit(-1);
}
avformat_free_context(pAVFormatContext);
if (pAVFormatContext == nullptr) {
cout << "VideoFormat freed successfully" << endl;
}
else {
cerr << "Error: unable to free VideoFormatContext" << endl;
exit(-1);
}
}
}
//permits to stop recording
void ScreenRecorder::stopCommand() {
unique_lock<mutex> ul(mu);
stopCapture = true;
if (pauseCapture)
pauseCapture = false;
cv.notify_all();
}
//permits to pause recording in order to be resumed later
void ScreenRecorder::pauseCommand() {
unique_lock<mutex> ul(mu);
if (!pauseCapture)
pauseCapture = true;
}
//permits to resume paused recording
void ScreenRecorder::resumeCommand() {
unique_lock<mutex> ul(mu);
if (pauseCapture) {
pauseCapture = false;
cv.notify_all();
}
}
//read if recording is started or not
bool ScreenRecorder::getStarted() {
return started;
}
//read if menu is active or not (thread safe)
bool ScreenRecorder::getActiveMenu() {
std::lock_guard<std::mutex> lg(mu);
return activeMenu;
}
//comunicate that the menu has been activated (thread safe)
void ScreenRecorder::setActiveMenu(bool val) {
std::lock_guard<std::mutex> lg(mu);
activeMenu = val;
}
//read if menu is disabled
bool ScreenRecorder::getDisabledMenu() {
std::lock_guard<std::mutex> lg(mu);
return disabledMenu;
}
//read if audio is enabled r not
bool ScreenRecorder::getRecordAudio() {
return recordAudio;
};
//permits to enable audio recording
void ScreenRecorder::setRecordAudio(bool val) {
recordAudio = val;
}
//permits to set output directory (defuault: .)
void ScreenRecorder::setOutputDir(const char* dir) {
dir_path = dir;
}
//overide screen dimension to record (default->desktop dimension)
void ScreenRecorder::setScreenDimension(int width, int height) {
if(width % 2 != 0)
width -= 1;
if(height % 2 !=0)
height -= 1;
this->width = width;
this->height = height;
cout << "Screen dimension setted correctly" << endl;
}
//override screen offset (default->x=0, y=0)
void ScreenRecorder::setScreenOffset(int x_offset, int y_offset) {
this->x_offset = x_offset;
this->y_offset = y_offset;
cout << "Screen offset setted correctly" << endl;
}
//initialize the output file
int ScreenRecorder::initOutputFile() throw() {
value = 0;
outputFile = const_cast<char*>("output.mp4");
#if defined _WIN32
string completePath = string(dir_path) + string("\\") + string(outputFile);
#else
string completePath = string(dir_path) + string("/") + string(outputFile);
#endif
outAVFormatContext = nullptr;
outputAVFormat = av_guess_format(nullptr, "output.mp4", nullptr);
if (outputAVFormat == nullptr) {
throw error("Error in guessing the video format, try with correct format");
}
//avformat_alloc_output_context2(&outAVFormatContext, outputAVFormat, nullptr, outputFile); //for just video, we used this
avformat_alloc_output_context2(&outAVFormatContext, outputAVFormat, outputAVFormat->name, const_cast<char*>(completePath.c_str()));
if (outAVFormatContext == nullptr) {
throw error("Error in allocating outAVFormatContext");
}
/*===========================================================================*/
try{
this->generateVideoStream();
if (recordAudio) this->generateAudioStream();
}
catch(exception e){
throw e;
}
//create an empty video file
if (!(outAVFormatContext->flags & AVFMT_NOFILE)) {
if (avio_open2(&outAVFormatContext->pb, const_cast<char*>(completePath.c_str()), AVIO_FLAG_WRITE, nullptr, &videoOptions) < 0) {
throw error("Error in creating the video file");
}
}
if (outAVFormatContext->nb_streams == 0) {
throw error("Error in creating the video file");
}
value = avformat_write_header(outAVFormatContext, &videoOptions);
if (value < 0) {
throw error("Error in writing the header context" );
}
return 0;
}
//start recording (audio is optional)
//open devices ans initialize the output file
void ScreenRecorder::startRecording() throw() {
if ((width + x_offset) > screen_width || (height + y_offset) > screen_height) {
cout << "Diemensions of screen section setted are too large for the screen.";
setActiveMenu(true);
return;
}
try{
if (recordAudio) openAudioDevice();
openVideoDevice();
initOutputFile();
}
catch(error e){
throw e;
}
//detect possible exception during lauch threads
try{
started = true;
t_video = std::move(std::thread{ [this]() {
this->captureVideoFrames();
}
});
if (recordAudio) {
t_audio = std::move(std::thread{ [this]() {
this->captureAudio();
}
});
}
}
catch(exception e){
started = false;
throw e;
}
}
/*==================================== VIDEO ==============================*/
//open input video device
int ScreenRecorder::openVideoDevice() throw() {
value = 0;
videoOptions = nullptr;
pAVFormatContext = nullptr;
pAVFormatContext = avformat_alloc_context();
string dimension = to_string(width) + "x" + to_string(height);
av_dict_set(&videoOptions, "video_size", dimension.c_str(), 0); //option to set the dimension of the screen section to record
value = av_dict_set(&videoOptions, "framerate", "25", 0);
if (value < 0) {
throw error("Error in setting dictionary value (setting framerate)");
}
value = av_dict_set(&videoOptions, "preset", "ultrafast", 0);
if (value < 0) {
throw error("Error in setting dictionary value (setting preset value)");
}
#ifdef _WIN32
pAVInputFormat = av_find_input_format("gdigrab");
//Set some options
//grabbing frame rate
//The distance from the left edge of the screen or desktop
av_dict_set(&videoOptions, "offset_x", to_string(x_offset).c_str(), 0);
//The distance from the top edge of the screen or desktop
av_dict_set(&videoOptions, "offset_y", to_string(y_offset).c_str(), 0);
//Video frame size. The default is to capture the full screen
//av_dict_set(&options,"video_size","640x480",0);
if (avformat_open_input(&pAVFormatContext, "desktop", pAVInputFormat, &videoOptions) != 0) {
cerr << "Couldn't open input stream" << endl;
exit(-1);
}
#elif defined linux
//permits to set the capturing from screen
//Set some options
//grabbing frame rate
//av_dict_set(&options,"framerate","5",0);
//Make the grabbed area follow the mouse
//av_dict_set(&options,"follow_mouse","centered",0);
//Video frame size. The default is to capture the full screen
//av_dict_set(&opt, "offset_x", "20", 0);
//av_dict_set(&opt, "offset_y", "20", 0);
//AVDictionary* opt = nullptr;
//int offset_x = 0, offset_y = 0;
string url = ":0.0+" + to_string(x_offset) + "," + to_string(y_offset); //custom string to set the start point of the screen section
pAVInputFormat = av_find_input_format("x11grab");
value = avformat_open_input(&pAVFormatContext, url.c_str(), pAVInputFormat, &videoOptions);
if (value != 0) {
throw error("Error in opening input device");
}
#else
show_avfoundation_device();
//The distance from the left edge of the screen or desktop
value = av_dict_set(&videoOptions, "vf", ("crop=" + to_string(width) + ":" + to_string(height) + ":" + to_string(x_offset) + ":" +
to_string(y_offset)).c_str(), 0);
if (value < 0) {
throw error("Error in setting crop");
}
value = av_dict_set(&videoOptions, "pixel_format", "yuv420p", 0);
if (value < 0) {
throw error("Error in setting pixel format");
}
pAVInputFormat = av_find_input_format("avfoundation");
if (avformat_open_input(&pAVFormatContext, "1:none", pAVInputFormat, &videoOptions) != 0) {
throw error("Error in opening input device");
}
#endif
//get video stream infos from context
value = avformat_find_stream_info(pAVFormatContext, nullptr);
if (value < 0) {
throw error("Error in retrieving the stream info");
}
VideoStreamIndx = -1;
for (int i = 0; i < pAVFormatContext->nb_streams; i++) {
if (pAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
VideoStreamIndx = i;
break;
}
}
if (VideoStreamIndx == -1) {
throw error("Error: unable to find video stream index");
}
pAVCodecContext = pAVFormatContext->streams[VideoStreamIndx]->codec;
pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id/*params->codec_id*/);
if (pAVCodec == nullptr) {
throw error("Error: unable to find decoder video");
}
return 0;
}
//find information for video stream
//called by initOutputFile()
void ScreenRecorder::generateVideoStream() throw() {
outVideoCodec = avcodec_find_encoder(AV_CODEC_ID_H264); //AV_CODEC_ID_MPEG4
if (outVideoCodec == nullptr) {
throw error("Error in finding the AVCodec, try again with the correct codec");
}
//Generate video stream
videoSt = avformat_new_stream(outAVFormatContext, outVideoCodec);
if (videoSt == nullptr) {
throw error("Error in creating AVFormatStream");
}
//set properties of the video file (stream)
outVideoCodecContext = videoSt->codec;
outVideoCodecContext->codec_id = AV_CODEC_ID_H264;// AV_CODEC_ID_MPEG4; // AV_CODEC_ID_H264 // AV_CODEC_ID_MPEG1VIDEO
outVideoCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
outVideoCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
outVideoCodecContext->bit_rate = /*10000000;*/ 2500000;
outVideoCodecContext->width = width; //dimension of the output video file
outVideoCodecContext->height = height;
outVideoCodecContext->gop_size = 15; // 3
//outVideoCodecContext->global_quality = 500; //for AV_CODEC_ID_MPEG4
outVideoCodecContext->max_b_frames = 2;
outVideoCodecContext->time_base.num = 1;
outVideoCodecContext->time_base.den = 25; // 15fps
outVideoCodecContext->bit_rate_tolerance = 400000;
if (outVideoCodecContext->codec_id == AV_CODEC_ID_H264) {
av_opt_set(outVideoCodecContext, "preset", "ultrafast", 0);
av_opt_set(outVideoCodecContext, "cabac", "1", 0);
av_opt_set(outVideoCodecContext, "ref", "3", 0);
av_opt_set(outVideoCodecContext, "deblock", "1:0:0", 0);
av_opt_set(outVideoCodecContext, "analyse", "0x3:0x113", 0);
av_opt_set(outVideoCodecContext, "subme", "7", 0);
av_opt_set(outVideoCodecContext, "chroma_qp_offset", "4", 0);
av_opt_set(outVideoCodecContext, "rc", "crf", 0);
av_opt_set(outVideoCodecContext, "rc_lookahead", "40", 0);
av_opt_set(outVideoCodecContext, "crf", "10.0", 0);
}
if (outAVFormatContext->oformat->flags & AVFMT_GLOBALHEADER) {
outVideoCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
value = avcodec_open2(outVideoCodecContext, outVideoCodec, nullptr);
if (value < 0) {
throw error("Error in opening the AVCodec");
}
outVideoStreamIndex = -1;
for (int i = 0; i < outAVFormatContext->nb_streams; i++) {
if (outAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN) {
outVideoStreamIndex = i;
}
}
if (outVideoStreamIndex < 0) {
throw error("Error: cannot find a free stream index for video output");
}
avcodec_parameters_from_context(outAVFormatContext->streams[outVideoStreamIndex]->codecpar, outVideoCodecContext);
}
//executed in a second thread to record video
int ScreenRecorder::captureVideoFrames() {
int64_t pts = 0;
int flag;
int frameFinished = 0;
bool endPause = false;
int numPause = 0;
int64_t numFrame = 0;
int frameIndex = 0;
value = 0;
pAVPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
if (pAVPacket == nullptr) {
cerr << "Error in allocating AVPacket" << endl;
exit(-1);
//throw error("Error in allocating AVPacket");
}
pAVFrame = av_frame_alloc();
if (pAVFrame == nullptr) {
cerr << "Error: unable to alloc the AVFrame resources" << endl;
exit(-1);
//throw error("Error: unable to alloc the AVFrame resources");
}
outFrame = av_frame_alloc();
if (outFrame == nullptr) {
cerr << "Error: unable to alloc the AVFrame resources for out frame" << endl;
exit(-1);
//throw error("Error: unable to alloc the AVFrame resources for out frame");
}
int videoOutBuffSize;
int nBytes = av_image_get_buffer_size(outVideoCodecContext->pix_fmt, outVideoCodecContext->width, outVideoCodecContext->height, 32);
uint8_t* videoOutBuff = (uint8_t*)av_malloc(nBytes);
if (videoOutBuff == nullptr) {
cerr << "Error: unable to allocate memory" << endl;
exit(-1);
//throw error("Error: unable to allocate memory");
}
value = av_image_fill_arrays(outFrame->data, outFrame->linesize, videoOutBuff, AV_PIX_FMT_YUV420P, outVideoCodecContext->width, outVideoCodecContext->height, 1);
if (value < 0) {
cerr << "Error in filling image array" << endl;
}
SwsContext* swsCtx_;
if (avcodec_open2(pAVCodecContext, pAVCodec, nullptr) < 0) {
cerr << "Could not open codec" << endl;
exit(-1);
}
swsCtx_ = sws_getContext(pAVCodecContext->width, pAVCodecContext->height, pAVCodecContext->pix_fmt, outVideoCodecContext->width, outVideoCodecContext->height, outVideoCodecContext->pix_fmt, SWS_BICUBIC,
nullptr, nullptr, nullptr);
cout << "pVCodec Context width: height " << pAVCodecContext->width << " " << pAVCodecContext->height << endl;
cout << "outVideoCodecContext width: height " << outVideoCodecContext->width << " " << outVideoCodecContext->height << endl;
AVPacket outPacket;
int gotPicture;
double pauseTime, startSeconds, precSeconds=0;
time_t startPause, stopPause;
time_t startTime;
time(&startTime);
startSeconds = difftime(startTime, 0);
cout << endl;
while (true) {
unique_lock<mutex> ul(mu);
if (pauseCapture) {
#if defined linux
avformat_close_input(&pAVFormatContext);
if (pAVFormatContext != nullptr) {
cerr << "Error: unable to close the pAVFormatContext (before pause)" << endl;
exit(-1);
}
#endif
endPause = true;
time(&startPause);
numPause++;
}
cv.wait(ul, [this]() { return !pauseCapture; }); //pause capture (not busy waiting)
if (endPause) {
#if defined linux
string url = ":0.0+" + to_string(x_offset) + "," + to_string(y_offset); //custom string to set the start point of the screen section
string dimension = to_string(width) + "x" + to_string(height);
av_dict_set(&videoOptions, "video_size", dimension.c_str(), 0);
value = avformat_open_input(&pAVFormatContext, url.c_str(), pAVInputFormat, &videoOptions);
if (value != 0) {
cerr << "Error in opening input device (video after pause)" << endl;
exit(-1);
}
value = avformat_find_stream_info(pAVFormatContext, nullptr);
if (value < 0) {
cerr << "Error in retrieving the stream info" << endl;
exit(-1);
}
VideoStreamIndx = -1;
for (int i = 0; i < pAVFormatContext->nb_streams; i++) {
if (pAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
VideoStreamIndx = i;
break;
}
}
if (VideoStreamIndx == -1) {
cerr << "Error: unable to find video stream index" << endl;
exit(-2);
}
pAVCodecContext = pAVFormatContext->streams[VideoStreamIndx]->codec;
pAVCodec = avcodec_find_decoder(pAVCodecContext->codec_id/*params->codec_id*/);
if (pAVCodec == nullptr) {
cerr << "Error: unable to find decoder video" << endl;
exit(-1);
}
if (avcodec_open2(pAVCodecContext, pAVCodec, nullptr) < 0) {
cerr << "Could not open codec" << endl;
exit(-1);
}
swsCtx_ = sws_getContext(pAVCodecContext->width, pAVCodecContext->height, pAVCodecContext->pix_fmt, outVideoCodecContext->width, outVideoCodecContext->height, outVideoCodecContext->pix_fmt, SWS_BICUBIC,
nullptr, nullptr, nullptr);
#endif
endPause = false;
time(&stopPause);
pauseTime = difftime(stopPause, startPause);
startSeconds += pauseTime;
}
if (stopCapture) { //check if the capture has to stop
break;
}
ul.unlock();
if (av_read_frame(pAVFormatContext, pAVPacket) >= 0 && pAVPacket->stream_index == VideoStreamIndx) {
av_packet_rescale_ts(pAVPacket, pAVFormatContext->streams[VideoStreamIndx]->time_base, pAVCodecContext->time_base);
value = avcodec_decode_video2(pAVCodecContext, pAVFrame, &frameFinished, pAVPacket);
if (value < 0) {
cout << "Unable to decode video" << endl;
}
pAVFrame->pts = numFrame;
if (frameFinished) { //frame successfully decoded
numFrame++;
av_init_packet(&outPacket);
outPacket.data = nullptr;
outPacket.size = 0;
if (outAVFormatContext->streams[outVideoStreamIndex]->start_time <= 0) {
outAVFormatContext->streams[outVideoStreamIndex]->start_time = pAVFrame->pts;
}
//disable warning on the console
outFrame->width = outVideoCodecContext->width;
outFrame->height = outVideoCodecContext->height;
outFrame->format = outVideoCodecContext->pix_fmt;
outFrame->pts = pAVFrame->pts;
sws_scale(swsCtx_, pAVFrame->data, pAVFrame->linesize, 0, pAVCodecContext->height, outFrame->data, outFrame->linesize);
avcodec_encode_video2(outVideoCodecContext, &outPacket, outFrame, &gotPicture);
if (gotPicture) {
av_packet_rescale_ts(&outPacket, outVideoCodecContext->time_base, outAVFormatContext->streams[outVideoStreamIndex]->time_base);
time_t timer;
double seconds;
mu.lock();
if (!activeMenu) {
disabledMenu = false;
time(&timer);
seconds = difftime(timer, 0) - startSeconds;
int h = (int)(seconds / 3600);
int m = (int)(seconds / 60) % 60;
int s = (int)(seconds) % 60;
if ((seconds - precSeconds) >= 1) {
std::cout << "\r" << std::setw(2) << std::setfill('0') << h << ':'
<< std::setw(2) << std::setfill('0') << m << ':'
<< std::setw(2) << std::setfill('0') << s << std::flush;
precSeconds = seconds;
}
}
else {
disabledMenu = true;
}
mu.unlock();
write_lock.lock();
if (av_write_frame(outAVFormatContext, &outPacket) != 0) {
cerr << "Error in writing video frame" << endl;
}
write_lock.unlock();
av_packet_unref(&outPacket);
}
gotPicture = 0;
av_packet_unref(&outPacket);
av_free_packet(pAVPacket); //avoid memory saturation
}
frameFinished = 0;
}
}
av_free(videoOutBuff);
return 0;
}
/*========================================== AUDIO ============================*/
//open audio device
int ScreenRecorder::openAudioDevice() throw() {
audioOptions = nullptr;
inAudioFormatContext = nullptr;
inAudioFormatContext = avformat_alloc_context();
value = av_dict_set(&audioOptions, "sample_rate", "44100", 0);
if (value < 0) {
throw error("Error: cannot set audio sample rate");
}
value = av_dict_set(&audioOptions, "async", "25", 0);
if (value < 0) {
throw error("Error: cannot set audio sample rate");
}
#if defined linux
audioInputFormat = av_find_input_format("alsa");
value = avformat_open_input(&inAudioFormatContext, "hw:0", audioInputFormat, &audioOptions);
if (value != 0) {
throw error("Error in opening input device (audio)");
}
#endif
#if defined _WIN32
show_dshow_device();
cout << "\nPlease select the audio device among those listed before: ";
getchar();
getline(cin, deviceName);
deviceName = "audio=" + deviceName;
audioInputFormat = av_find_input_format("dshow");
value = avformat_open_input(&inAudioFormatContext, deviceName.c_str(), audioInputFormat, &audioOptions);
if (value != 0) {
throw error("Error in opening input device (audio)");
}
#endif
#if defined __APPLE__
audioInputFormat = av_find_input_format("avfoundation");
value = avformat_open_input(&inAudioFormatContext, "none:0", audioInputFormat, &audioOptions);
#endif
value = avformat_find_stream_info(inAudioFormatContext, nullptr);
if (value != 0) {
throw error("Error: cannot find the audio stream information");
}
audioStreamIndx = -1;
for (int i = 0; i < inAudioFormatContext->nb_streams; i++) {
if (inAudioFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStreamIndx = i;
break;
}
}
if (audioStreamIndx == -1) {
throw error("Error: unable to find audio stream index");
}
}
//find information for audio stream
//called by initOutputFile()
void ScreenRecorder::generateAudioStream() throw(){
AVCodecParameters* params = inAudioFormatContext->streams[audioStreamIndx]->codecpar;
inAudioCodec = avcodec_find_decoder(params->codec_id);
if (inAudioCodec == nullptr) {
throw error("Error: cannot find the audio decoder");
}
inAudioCodecContext = avcodec_alloc_context3(inAudioCodec);
if (avcodec_parameters_to_context(inAudioCodecContext, params) < 0) {
throw error("Cannot create codec context for audio input");
}
value = avcodec_open2(inAudioCodecContext, inAudioCodec, nullptr);
if (value < 0) {
throw error("Error: cannot open the input audio codec");
}
//Generate audio stream
outAudioCodecContext = nullptr;
outAudioCodec = nullptr;
int i;
AVStream* audio_st = avformat_new_stream(outAVFormatContext, nullptr);
if (audio_st == nullptr) {
throw error("Error: cannot create audio stream");
}
outAudioCodec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (outAudioCodec == nullptr) {
throw error("Error: cannot find requested encoder");
}
outAudioCodecContext = avcodec_alloc_context3(outAudioCodec);
if (outAudioCodecContext == nullptr) {
throw error("Error: cannot create related VideoCodecContext");
}
if ((outAudioCodec)->supported_samplerates) {
outAudioCodecContext->sample_rate = (outAudioCodec)->supported_samplerates[0];
for (i = 0; (outAudioCodec)->supported_samplerates[i]; i++) {
if ((outAudioCodec)->supported_samplerates[i] == inAudioCodecContext->sample_rate)
outAudioCodecContext->sample_rate = inAudioCodecContext->sample_rate;
}
}
outAudioCodecContext->codec_id = AV_CODEC_ID_AAC;
outAudioCodecContext->sample_fmt = (outAudioCodec)->sample_fmts ? (outAudioCodec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
outAudioCodecContext->channels = inAudioCodecContext->channels;
outAudioCodecContext->channel_layout = av_get_default_channel_layout(outAudioCodecContext->channels);
outAudioCodecContext->bit_rate = 96000;
outAudioCodecContext->time_base = { 1, inAudioCodecContext->sample_rate };
outAudioCodecContext->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
if ((outAVFormatContext)->oformat->flags & AVFMT_GLOBALHEADER) {
outAudioCodecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if (avcodec_open2(outAudioCodecContext, outAudioCodec, nullptr) < 0) {
throw error("error in opening the avcodec");
}
//find a free stream index
outAudioStreamIndex = -1;
for (i = 0; i < outAVFormatContext->nb_streams; i++) {
if (outAVFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_UNKNOWN) {
outAudioStreamIndex = i;
}
}
if (outAudioStreamIndex < 0) {
throw error("Error: cannot find a free stream for audio on the output");
}
avcodec_parameters_from_context(outAVFormatContext->streams[outAudioStreamIndex]->codecpar, outAudioCodecContext);
}
int ScreenRecorder::init_fifo()
{
/* Create the FIFO buffer based on the specified output sample format. */
if (!(fifo = av_audio_fifo_alloc(outAudioCodecContext->sample_fmt,
outAudioCodecContext->channels, 1))) {
fprintf(stderr, "Could not allocate FIFO\n");
return AVERROR(ENOMEM);
}
return 0;
}
int ScreenRecorder::add_samples_to_fifo(uint8_t** converted_input_samples, const int frame_size) {
int error;
/* Make the FIFO as large as it needs to be to hold both,
* the old and the new samples. */
if ((error = av_audio_fifo_realloc(fifo, av_audio_fifo_size(fifo) + frame_size)) < 0) {
fprintf(stderr, "Could not reallocate FIFO\n");
return error;
}
/* Store the new samples in the FIFO buffer. */
if (av_audio_fifo_write(fifo, (void**)converted_input_samples, frame_size) < frame_size) {
fprintf(stderr, "Could not write data to FIFO\n");
return AVERROR_EXIT;
}
return 0;
}
int ScreenRecorder::initConvertedSamples(uint8_t*** converted_input_samples, AVCodecContext* output_codec_context, int frame_size) {
int _error;
/* Allocate as many pointers as there are audio channels.
* Each pointer will later point to the audio samples of the corresponding
* channels (although it may be NULL for interleaved formats).
*/
if (!(*converted_input_samples = (uint8_t**)calloc(output_codec_context->channels,
sizeof(**converted_input_samples)))) {
fprintf(stderr, "Could not allocate converted input sample pointers\n");
return AVERROR(ENOMEM);
}
/* Allocate memory for the samples of all channels in one consecutive
* block for convenience. */
if (av_samples_alloc(*converted_input_samples, nullptr,
output_codec_context->channels,
frame_size,
output_codec_context->sample_fmt, 0) < 0) {
throw error("Errorr: could not allocate memeory for samples in all channels (audio)");
}
return 0;
}
//executed in a second thread to record audio
void ScreenRecorder::captureAudio() {
int ret;
AVPacket* inPacket, * outPacket;
AVFrame* rawFrame, * scaledFrame;
uint8_t** resampledData;
bool endPause = false;
init_fifo();
//allocate space for a packet
inPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
if (!inPacket) {
cerr << "Cannot allocate an AVPacket for encoded video" << endl;
exit(1);
//throw error("Cannot allocate an AVPacket for encoded video");
}
av_init_packet(inPacket);
//allocate space for a packet
rawFrame = av_frame_alloc();
if (!rawFrame) {
cerr << "Cannot allocate an AVPacket for encoded video" << endl;
exit(1);
//throw error("Cannot allocate an AVPacket for encoded video");
}
scaledFrame = av_frame_alloc();
if (!scaledFrame) {
cerr << "Cannot allocate an AVPacket for encoded video" << endl;
exit(1);
//throw error("Cannot allocate an AVPacket for encoded video");
}
outPacket = (AVPacket*)av_malloc(sizeof(AVPacket));
if (!outPacket) {
cerr << "Cannot allocate an AVPacket for encoded video" << endl;
exit(1);
//throw error("Cannot allocate an AVPacket for encoded video");
}
//init the resampler
SwrContext* resampleContext = nullptr;
resampleContext = swr_alloc_set_opts(resampleContext,
av_get_default_channel_layout(outAudioCodecContext->channels),
outAudioCodecContext->sample_fmt,
outAudioCodecContext->sample_rate,
av_get_default_channel_layout(inAudioCodecContext->channels),
inAudioCodecContext->sample_fmt,
inAudioCodecContext->sample_rate,
0,
nullptr);
if (!resampleContext) {
cerr << "Cannot allocate the resample context" << endl;
exit(1);
//throw error("Cannot allocate the resample context");
}
if ((swr_init(resampleContext)) < 0) {