forked from beihuijie/carrier-rtsp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_and_encoding.cpp
executable file
·198 lines (163 loc) · 3.78 KB
/
capture_and_encoding.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
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
#include <string.h>
#include <errno.h>
#include <imp/imp_isp.h>
#include <imp/imp_system.h>
#include <imp/imp_log.h>
#include <imp/imp_framesource.h>
#include <imp/imp_encoder.h>
#include "imp-common.h"
#include "capture_and_encoding.h"
#define TAG "capture_and_encoding"
extern struct chn_conf chn[];
int destory()
{
int ret, i;
/* Exit sequence as follow */
/* Step.a Stream Off */
ret = sample_framesource_streamoff();
if (ret < 0) {
IMP_LOG_ERR(TAG, "FrameSource StreamOff failed\n");
return -1;
}
/* Step.b UnBind */
for (i = 0; i < FS_CHN_NUM; i++) {
if (chn[i].enable) {
ret = IMP_System_UnBind(&chn[i].framesource_chn, &chn[i].imp_encoder);
if (ret < 0) {
IMP_LOG_ERR(TAG, "UnBind FrameSource channel%d and Encoder failed\n",i);
return -1;
}
}
}
/* Step.c Encoder exit */
ret = sample_encoder_exit();
if (ret < 0) {
IMP_LOG_ERR(TAG, "Encoder exit failed\n");
return -1;
}
/* Step.d FrameSource exit */
ret = sample_framesource_exit();
if (ret < 0) {
IMP_LOG_ERR(TAG, "FrameSource exit failed\n");
return -1;
}
/* Step.e System exit */
ret = sample_system_exit();
if (ret < 0) {
IMP_LOG_ERR(TAG, "sample_system_exit() failed\n");
return -1;
}
ret = IMP_Encoder_StopRecvPic(0);
if (ret < 0) {
IMP_LOG_ERR(TAG, "IMP_Encoder_StopRecvPic() failed\n");
return -1;
}
return 0;
}
static int save_stream(int fd, IMPEncoderStream *stream)
{
unsigned int ret;
int i, nr_pack = stream->packCount;
for (i = 0; i < nr_pack; i++) {
ret = write(fd, (void *)stream->pack[i].virAddr,
stream->pack[i].length);
if (ret != stream->pack[i].length){
IMP_LOG_ERR(TAG,"stream write error:%s\n", strerror(errno));
return -1;
}
}
return 0;
}
static int get_h264_stream(int fd, int chn)
{
int ret;
/* Polling H264 Stream, set timeout as 1000msec */
ret = IMP_Encoder_PollingStream(chn, 100);
if (ret < 0) {
IMP_LOG_ERR(TAG, "Polling stream timeout\n");
}
IMPEncoderStream stream;
/* Get H264 Stream */
ret = IMP_Encoder_GetStream(chn, &stream, 1);
if (ret < 0) {
IMP_LOG_ERR(TAG, "IMP_Encoder_GetStream() failed\n");
return -1;
}
ret = save_stream(fd, &stream);
if (ret < 0) {
close(fd);
return ret;
}
IMP_Encoder_ReleaseStream(chn, &stream);
return 0;
}
void *get_stream(int fd, int chn)
{
int ret;
ret = IMP_Encoder_StartRecvPic(chn);
if (ret < 0){
IMP_LOG_ERR(TAG, "IMP_Encoder_StartRecvPic(%d) failed\n", 1);
return NULL;
}
ret = get_h264_stream(fd, chn);
if (ret < 0) {
IMP_LOG_ERR(TAG, "Get H264 stream failed\n");
return NULL;
}
/* ret = IMP_Encoder_StopRecvPic(chn);
if (ret < 0) {
IMP_LOG_ERR(TAG, "IMP_Encoder_StopRecvPic() failed\n");
return NULL;
}
*/
return 0;
}
int capture_and_encoding()
{
int ret = 0;
int i = 0;
printf(">>>>>caputre_and_encoding start\n");
ret = sample_system_init();
if (ret < 0) {
IMP_LOG_ERR(TAG, "IMP_System_Init() failed\n");
return -1;
}
/* Step.2 FrameSource init */
ret = sample_framesource_init();
if (ret < 0) {
IMP_LOG_ERR(TAG, "FrameSource init failed\n");
return -1;
}
for (i = 0; i < FS_CHN_NUM; i++) {
if (chn[i].enable) {
ret = IMP_Encoder_CreateGroup(chn[i].index);
if (ret < 0) {
IMP_LOG_ERR(TAG, "IMP_Encoder_CreateGroup(%d) error !\n", i);
return -1;
}
}
}
/* Step.3 Encoder init */
ret = sample_encoder_init();
if (ret < 0) {
IMP_LOG_ERR(TAG, "Encoder init failed\n");
return -1;
}
/* Step.4 Bind */
for (i = 0; i < FS_CHN_NUM; i++) {
if (chn[i].enable) {
ret = IMP_System_Bind(&chn[i].framesource_chn, &chn[i].imp_encoder);
if (ret < 0) {
IMP_LOG_ERR(TAG, "Bind FrameSource channel%d and Encoder failed\n",i);
return -1;
}
}
}
/* Step.5 Stream On */
ret = sample_framesource_streamon();
if (ret < 0) {
IMP_LOG_ERR(TAG, "ImpStreamOn failed\n");
return -1;
}
return 0;
}