-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtelepi.c
More file actions
204 lines (153 loc) · 4.76 KB
/
telepi.c
File metadata and controls
204 lines (153 loc) · 4.76 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
/******************************************************************************
* TelePi - remote streaming for your Raspberry Pi
******************************************************************************
* Initial preview release
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "bcm_host.h"
#include "ilclient.h"
#include "telepi.h"
#define NUMFRAMES 300000
#define WIDTH 640
#define PITCH ((WIDTH+31)&~31)
#define HEIGHT 480
#define HEIGHT16 ((HEIGHT+15)&~15)
#define SIZE ((WIDTH * HEIGHT16 * 3)/2)
DISPMANX_DISPLAY_HANDLE_T display;
DISPMANX_RESOURCE_HANDLE_T resource;
DISPMANX_MODEINFO_T info;
VC_RECT_T rect;
uint32_t screen = 0;
uint32_t vc_image_ptr;
COMPONENT_T *video_encode = NULL;
OMX_BUFFERHEADERTYPE *buf;
OMX_BUFFERHEADERTYPE *out;
/*
* simulate grabbing a picture from some device
*/
int take_snapshot(void *buffer, OMX_U32 * filledLen)
{
VC_IMAGE_TRANSFORM_T transform = 0;
int ret;
ret = vc_dispmanx_snapshot(display, resource, transform);
assert(ret == 0);
ret = vc_dispmanx_resource_read_data(resource, &rect, buffer, info.width * 3);
assert(ret == 0);
*filledLen = info.width * info.height * 3;
return 1;
}
static int
video_encode_test(char *outputfilename)
{
int status = 0;
int framenumber = 0;
FILE *outf;
int ret;
OMX_ERRORTYPE r;
VC_IMAGE_TYPE_T type = VC_IMAGE_BGR888;
fprintf(stderr, "Open display[%i]...\n", screen );
display = vc_dispmanx_display_open( screen );
ret = vc_dispmanx_display_get_info(display, &info);
assert(ret == 0);
fprintf(stderr, "Display is %d x %d\n", info.width, info.height );
resource = vc_dispmanx_resource_create( type,
info.width,
info.height,
&vc_image_ptr );
fprintf(stderr, "VC image ptr: 0x%X\n", vc_image_ptr);
ret = vc_dispmanx_rect_set(&rect, 0, 0, info.width, info.height);
assert(ret == 0);
encode_init(&video_encode);
encode_config_format(video_encode, info.width, info.height, 30);
encode_config_encoding(video_encode, OMX_VIDEO_CodingAVC);
encode_config_bitrate(video_encode, 16000000 /* 8388608 */);
//encode_config_low_latency(video_encode, true);
encode_config_activate(video_encode);
if (outputfilename[0] == '-')
{
outf = stdout;
}
else
{
outf = fopen(outputfilename, "wb");
}
if (outf == NULL)
{
fprintf(stderr, "Failed to open '%s' for writing video\n", outputfilename);
exit(1);
}
fprintf(stderr, "looping for buffers...\n");
do
{
buf = ilclient_get_input_buffer(video_encode, 200, 1);
if (buf != NULL)
{
/* fill it */
take_snapshot(buf->pBuffer, &buf->nFilledLen);
framenumber++;
if (OMX_EmptyThisBuffer(ILC_GET_HANDLE(video_encode), buf) != OMX_ErrorNone)
{
fprintf(stderr, "Error emptying buffer!\n");
}
out = ilclient_get_output_buffer(video_encode, 201, 1);
r = OMX_FillThisBuffer(ILC_GET_HANDLE(video_encode), out);
if (r != OMX_ErrorNone)
{
fprintf(stderr, "Error filling buffer: %x\n", r);
}
if (out != NULL)
{
/*
if (out->nFlags & OMX_BUFFERFLAG_CODECCONFIG)
{
int i;
for (i = 0; i < out->nFilledLen; i++)
printf("%x ", out->pBuffer[i]);
printf("\n");
}
*/
r = fwrite(out->pBuffer, 1, out->nFilledLen, outf);
if (r != out->nFilledLen)
{
fprintf(stderr, "fwrite: Error emptying buffer: %d!\n", r);
}
else
{
//fprintf(stderr, "Writing frame %d/%d\n", framenumber, NUMFRAMES);
}
fflush(outf);
out->nFilledLen = 0;
}
else
{
fprintf(stderr, "Not getting it :(\n");
}
}
else
{
fprintf(stderr, "Doh, no buffers for me!\n");
}
} while (framenumber < NUMFRAMES);
fclose(outf);
fprintf(stderr, "Teardown.\n");
encode_deinit(video_encode);
return status;
}
int
main(int argc, char **argv)
{
fprintf(stderr, "TelePi - Raspberry Pi remote viewer\n\n");
if (argc < 2) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "Record to file: %s <filename>\n", argv[0]);
fprintf(stderr, "Output to stdout: %s <filename>\n", argv[0]);
fprintf(stderr, "\nStream to a remote computer:\n");
fprintf(stderr, "%s - | netcat <remote_ip> 5001\n\n", argv[0]);
exit(1);
}
bcm_host_init();
return video_encode_test(argv[1]);
}