-
Notifications
You must be signed in to change notification settings - Fork 9
/
input-rtp.c
347 lines (294 loc) · 8.16 KB
/
input-rtp.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) 2004 Nathan Lutchansky <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#ifdef HAVE_ASM_TYPES_H
#include <asm/types.h>
#endif
#ifdef HAVE_LINUX_COMPILER_H
#include <linux/compiler.h>
#endif
#include <linux/videodev2.h>
#ifdef HAVE_GO7007_H
#include <linux/go7007.h>
#endif
#include <event.h>
#include <log.h>
#include <frame.h>
#include <stream.h>
#include <inputs.h>
#include <conf_parse.h>
#include <ortp/ortp.h>
#define INPUTTYPE_WEBCAM 1
#define INPUTTYPE_NTSC 2
#define INPUTTYPE_PAL 3
struct rtp_spook_input {
struct stream *output;
struct frame_exchanger *ex;
char device[256];
int format;
int bitrate;
int width;
int height;
int fincr;
int fbase;
int inputport;
int inputtype;
int fps;
int fd;
void *bufaddr[2];
pthread_t thread;
int running;
int localport;
RtpSession *session;
};
static void *capture_loop( void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
struct frame *f;
int status;
unsigned char buffer[38400];
int have_more, err, stream_received=0;
uint32_t ts=0;
do
{
have_more=1;
while (have_more)
{
err=rtp_session_recv_with_ts(conf->session,buffer,sizeof(buffer),ts,&have_more);
if (err>0)
{
stream_received=1;
}
if ((stream_received) && (err>0))
{
if( ( f = get_next_frame( conf->ex, 0 ) ) )
{
f->length = err;
f->format = conf->format;
f->width = conf->width;
f->height = conf->height;
f->key = 1;
memcpy( f->d, buffer, err);
deliver_frame( conf->ex, f );
printf("\r\nDeliver packet with length %d", err);
}
else
{
spook_log( SL_WARN, "video: dropping frame" );
}
}
}
ts+=160;
//ortp_message("Receiving packet.");
}while(1);
return NULL;
}
static void get_back_frame1( struct frame *f, void *d )
{
struct frame *newf;
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
newf = new_frame();
if(newf)
{
exchange_frame( conf->ex, newf );
deliver_frame_to_stream( f, conf->output );
}
else
{
//printf("\r\ndrop frame!");
unref_frame(f);
}
}
static void ssrc_cb(RtpSession *session)
{
printf("hey, the ssrc has changed !\n");
}
static int rtp_setup( struct rtp_spook_input *conf )
{
// ??RTP??
conf->session=rtp_session_new(RTP_SESSION_RECVONLY);
rtp_session_set_scheduling_mode(conf->session,1);
rtp_session_set_blocking_mode(conf->session,1);
rtp_session_set_local_addr(conf->session,"0.0.0.0", conf->localport);
rtp_session_set_connected_mode(conf->session,TRUE);
rtp_session_set_symmetric_rtp(conf->session,TRUE);
rtp_session_enable_adaptive_jitter_compensation(conf->session,TRUE);
rtp_session_set_jitter_compensation(conf->session,40);
rtp_session_set_payload_type(conf->session,0);
rtp_session_signal_connect(conf->session,"ssrc_changed",(RtpCallback)ssrc_cb,0);
rtp_session_signal_connect(conf->session,"ssrc_changed",(RtpCallback)rtp_session_reset,0);
return 0;
}
static void get_framerate( struct stream *s, int *fincr, int *fbase )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)s->private;
*fincr = conf->fincr;
*fbase = conf->fbase;
}
static void set_running( struct stream *s, int running )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)s->private;
conf->running = running;
}
/************************ CONFIGURATION DIRECTIVES ************************/
static void *start_block(void)
{
struct rtp_spook_input *conf;
conf = (struct rtp_spook_input *)malloc( sizeof( struct rtp_spook_input ) );
conf->output = NULL;
conf->device[0] = 0;
conf->format = FORMAT_RAW_UYVY;
conf->bitrate = 0;
conf->width = 0;
conf->height = 0;
conf->inputport = -1;
conf->inputtype = 0;
conf->fps = -1;
conf->fd = -1;
conf->running = 0;
conf->localport = 0;
conf->session = NULL;
return conf;
}
static int end_block( void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
int i;
if( ! conf->output )
{
spook_log( SL_ERR, "rtpi: missing output stream name" );
return -1;
}
if( conf->fps < 0 )
{
spook_log( SL_ERR,
"v4l: framerate not specified for webcam" );
return -1;
} else if( conf->fps > 0 )
{
conf->fincr = 1;
conf->fbase = conf->fps;
}
else
{
spook_log( SL_INFO, "v4l: must figure out framerate" );
return -1;
}
if( rtp_setup( conf ) < 0 ) return -1;
conf->ex = new_exchanger( conf->fps, get_back_frame1, conf );
for( i = 0; i < conf->fps; ++i )
exchange_frame( conf->ex, new_frame() );
pthread_create( &conf->thread, NULL, capture_loop, conf );
return 0;
}
static int set_device( int num_tokens, struct token *tokens, void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
strcpy( conf->device, tokens[1].v.str );
return 0;
}
static int set_output( int num_tokens, struct token *tokens, void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
conf->output = new_stream( tokens[1].v.str, conf->format, conf );
if( ! conf->output )
{
spook_log( SL_ERR, "rtpi: unable to create stream \"%s\"",
tokens[1].v.str );
return -1;
}
conf->output->get_framerate = get_framerate;
conf->output->set_running = set_running;
return 0;
}
static int set_format( int num_tokens, struct token *tokens, void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
if( conf->output )
{
spook_log( SL_ERR, "rtpi: output format must be specified "
"before output stream name" );
return -1;
}
if( ! strcasecmp( tokens[1].v.str, "pcm" ) )
conf->format = FORMAT_PCM;
else if( ! strcasecmp( tokens[1].v.str, "mpeg4" ) )
conf->format = FORMAT_MPEG4;
else if( ! strcasecmp( tokens[1].v.str, "mjpeg" ) )
conf->format = FORMAT_JPEG;
else
{
spook_log( SL_ERR, "rtpi: format \"%s\" is unsupported; try "
"PCM, MJPEG, or MPEG4", tokens[1].v.str );
return -1;
}
return 0;
}
static int set_framerate_num( int num_tokens, struct token *tokens, void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
if( conf->fps >= 0 )
{
spook_log( SL_ERR, "v4l: frame rate has already been set!" );
return -1;
}
conf->fps = tokens[1].v.num;
return 0;
}
static int set_framesize( int num_tokens, struct token *tokens, void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
conf->width = tokens[1].v.num;
conf->height = tokens[2].v.num;
return 0;
}
static int set_port( int num_tokens, struct token *tokens, void *d )
{
struct rtp_spook_input *conf = (struct rtp_spook_input *)d;
conf->localport = tokens[1].v.num;
return 0;
}
static struct statement config_statements[] = {
/* directive name, process function, min args, max args, arg types */
{ "device", set_device, 1, 1, { TOKEN_STR } },
{ "output", set_output, 1, 1, { TOKEN_STR } },
{ "format", set_format, 1, 1, { TOKEN_STR } },
{ "framerate", set_framerate_num, 1, 1, { TOKEN_NUM } },
{ "framesize", set_framesize, 2, 2, { TOKEN_NUM, TOKEN_NUM } },
{ "port", set_port, 1, 1, {TOKEN_NUM}},
/* empty terminator -- do not remove */
{ NULL, NULL, 0, 0, {} }
};
void rtp_init(void)
{
ortp_init();
ortp_scheduler_init();
ortp_set_log_level_mask(ORTP_ERROR);
register_config_context( "input", "rtp", start_block, end_block,
config_statements );
}