-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstate_machine.cpp
126 lines (122 loc) · 3 KB
/
state_machine.cpp
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
#include "stdint.h"
#include "state_machine.h"
#include "camera_capture.h"
#include "yuyv2yuv.h"
#include "yuv2x264.h"
#include "transfer.h"
#include "debug.h"
#include "config.h"
static int STATIC_STATE = INIT_CAMERA;
int fd;
uint8_t *yuyv_img, *yuv_img;
void to_state(int state)
{
STATIC_STATE = state;
}
void do_state()
{
int ret;
int i;
struct timeval tv;
int r;
switch (STATIC_STATE)
{
case INIT_CAMERA:
{
debug_print("INIT_CAMERA state, next to READ_CAMERA state.\n");
/* init camera.*/
yuyv_img = (uint8_t *)malloc(sizeof(uint8_t) * YUYV_SIZE);
yuv_img = (uint8_t *)malloc(sizeof(uint8_t) * YUV_SIZE);
fd = open_camera();
init_camera(fd);
start_capture(fd);
/* init x264*/
ret = init_param_for_x264();
if (ret < 0)
{
debug_print("init param for x264 has caused fault.");
to_state(ERROR_STATE);
}
/* init jrtp*/
ret = init_param_for_jrtplib();
if (ret < 0)
{
debug_print("init param for jrtp has caused fault.");
to_state(ERROR_STATE);
}
to_state(READ_CAMERA);
break;
}
case READ_CAMERA:
{
debug_print("READ_CAMERA state, next to TRAN_FORMAT state.");
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r)
{
if (EINTR == errno)
{
to_state(READ_CAMERA);
}
debug_print("Fail to select");
exit(EXIT_FAILURE);
}
if (0 == r)
{
debug_print("select Timeout");
exit(-1);
}
read_frame(fd, yuyv_img);
to_state(TRAN_FORMAT);
break;
}
case TRAN_FORMAT:
{
debug_print("TRAN_FORMAT state, next to SEND_PACKET state.");
/**/
yuyv_2_yuv(yuyv_img, yuv_img);
/**/
ret = encode_one_frame(yuv_img);
if (ret < 0)
{
debug_print("encode one frame has caused fault.");
to_state(ERROR_STATE);
}
to_state(SEND_PACKET);
break;
}
case SEND_PACKET:
{
debug_print("SEND_PACKET state, next to READ_CAMERA state.");
for (i = 0; i < i_nal; ++i)
{
debug_print("transfer %dth nalu size = %d", i, nal[i].i_payload);
ret = transfer_one_nalu_by_rtp(nal[i].p_payload, nal[i].i_payload, BYTES_PER_SECOND/FPS);
if (ret < 0)
{
to_state(ERROR_STATE);
}
}
to_state(READ_CAMERA);
break;
}
case ERROR_STATE:
{
close_camera_device(fd);
close_yuv2x264();
close_transfer();
//to_state(INIT_CAMERA);
break;
}
default:
{
debug_print("default state , next to ERROR_STATE state.");
to_state(ERROR_STATE);
break;
}
}
}