-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.c
115 lines (91 loc) · 2.39 KB
/
server.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/time.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
#include "app.h"
#include "capture.h"
#include "vcompress.h"
#include "sender.h"
#include "utils.h"
/** webcam_server: 打开 /dev/video0, 获取图像, 压缩, 发送到 localhost:3020 端口
*
* 使用 320x240, fps=10
*/
#define VIDEO_WIDTH 320
#define VIDEO_HEIGHT 240
#ifdef HW_VC /* 软件编码fps=15,硬件编码fps=20 */
#define VIDEO_FPS 15.0
#else
#define VIDEO_FPS 8.0
#endif
#define TARGET_IP "192.168.1.5"
//#define TARGET_IP "127.0.0.1"
#define TARGET_PORT 3021
struct Ctx {
app_t a;
void *capture;
void *encoder;
void *sender;
tst_t t;
};
typedef struct Ctx Ctx;
struct app_timer {
struct timeval exp_tv, inter_tv; /* 超时时间,间隔时间 */
void (*efunc)(void*); /* 超时函数 */
void *arg; /* 超时函数参数 */
bool one_shot; /* one shot */
bool active;
};
void timer_handler(void *arg)
{
Ctx *c= (Ctx*)arg;
tst_start(c->t); //test
// 抓
Picture pic;
capture_get_picture(c->capture, &pic);
tst_print_and_start(c->t, "capture_get_picture"); //test
// 压
const void *outdata;
int outlen;
int rc = vc_compress(c->encoder, pic.data, pic.stride, &outdata, &outlen);
if (rc < 0) return;
tst_print_and_start(c->t, "vc_compress"); //test
// 发
sender_send(c->sender, outdata, outlen);
}
int main (int argc, char **argv)
{
Ctx c;
c.a = app_create(8);
c.capture = capture_open(argv[1], VIDEO_WIDTH, VIDEO_HEIGHT, PIX_FMT_YUV420P);
if (c.capture == NULL) {
fprintf(stderr, "ERR: %s\n", strerror(errno));
exit(-1);
}
c.encoder = vc_open(VIDEO_WIDTH, VIDEO_HEIGHT, VIDEO_FPS);
if (!c.encoder) {
fprintf(stderr, "ERR: can't open x264 encoder\n");
exit(-1);
}
c.sender = sender_open(TARGET_IP, TARGET_PORT);
if (!c.sender) {
fprintf(stderr, "ERR: can't open sender for %s:%d\n", TARGET_IP, TARGET_PORT);
exit(-1);
}
c.t = tst_create();
int fms = 1000 / VIDEO_FPS;
app_timer_t timer = app_timer_create(fms, false, timer_handler, &c);
app_add_timer(c.a, timer);
app_exec(c.a);
app_del_timer(c.a, timer);
app_timer_del(timer);
sender_close(c.sender);
vc_close(c.encoder);
capture_close(c.capture);
tst_free(c.t);
app_free(c.a);
return 0;
}