forked from ashwing920/SimpleRecorder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
144 lines (138 loc) · 3.38 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>
#include <time.h>
#include <pthread.h>
#include <dirent.h>
#include "picture_t.h"
#include "simplerecorder.h"
#include "log.h"
#define MAX_SIZE 1024*1024*10
static int recording;
static char osd_string[20];
static char mkv_filename[100];
static char dirname[20];
static char tempname[20];
static struct picture_t pic;
static void gen_osd_info()
{
time_t t = time(0);
strftime(osd_string, 20, "%Y-%m-%d %H:%M:%S",localtime(&t));
}
static void get_filename()
{
time_t t = time(0);
strcpy(mkv_filename,"./Record/");
strftime(dirname, 20, "%Y%m%d",localtime(&t));
strftime(tempname, 20, "%H%M%S",localtime(&t));
strcat(mkv_filename,dirname);
if(NULL==opendir(mkv_filename))
mkdir(mkv_filename,0775);
strcat(mkv_filename,"/");
strcat(mkv_filename,tempname);
strcat(mkv_filename,".mkv");
}
void stop_recording(int param)
{
recording = 0;
}
int main()
{
int i,FileSize;
struct timeval tpstart,tpend;
float timeuse;
struct encoded_pic_t encoded_pic,header_pic;
errno = 0;
if(NULL==opendir("./Record"))
mkdir("./Record",0775);
if(!camera_init(&pic))
goto error_cam;
if(!encoder_init(&pic)){
fprintf(stderr,"failed to initialize encoder\n");
goto error_encoder;
}
if(!preview_init(&pic))
goto error_preview;
get_filename();
printf("file:%s\n",mkv_filename);
if(!output_init(&pic,mkv_filename))
goto error_output;
if(!encoder_encode_headers(&encoded_pic))
goto error_output;
memcpy(&header_pic,&encoded_pic,sizeof(encoded_pic));
header_pic.buffer=malloc(encoded_pic.length);
memcpy(header_pic.buffer,encoded_pic.buffer,encoded_pic.length);
if(!output_write_headers(&header_pic))
goto error_output;
encoder_release(&encoded_pic);
if(!camera_on())
goto error_cam_on;
if(signal(SIGINT, stop_recording) == SIG_ERR){
fprintf(stderr,"signal() failed\n");
goto error_signal;
}
printf("Press ctrl-c to stop recording...\n");
recording = 1;
FileSize =0;
for(i=0; recording; i++){
if(!camera_get_frame(&pic))
break;
gen_osd_info();
osd_print(&pic, osd_string);
//if((i&7)==0) // i%8==0
gettimeofday(&tpstart,NULL);
preview_display(&pic);
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+
tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
//printf("usetime:%f\n",timeuse);
FileSize+=encoded_pic.length;
if(!encoder_encode_frame(&pic, &encoded_pic))
break;
applog_flush();
if ((FileSize>MAX_SIZE) && (encoded_pic.frame_type ==FRAME_TYPE_I)) {
output_close();
get_filename();
printf("file:%s\n",mkv_filename);
if(!output_init(&pic,mkv_filename))
goto error_output;
if(!output_write_headers(&header_pic))
goto error_output;
FileSize=0;
ResetTime(&pic,&encoded_pic);
if(!output_write_frame(&encoded_pic))
break;
encoder_release(&encoded_pic);
} else {
if(!output_write_frame(&encoded_pic))
break;
encoder_release(&encoded_pic);
}
}
printf("\n%d frames recorded\n", i);
error_signal:
printf("error signal\n");
camera_off();
error_cam_on:
printf("error cam\n");
output_close();
error_output:
printf("error output\n");
preview_close();
error_preview:
printf("error encode\n");
encoder_close();
error_encoder:
printf("error cam\n");
camera_close();
error_cam:
printf("error over\n");
return 0;
}