Commit | Line | Data |
---|---|---|
f6fa7814 DM |
1 | /* |
2 | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | |
3 | * | |
4 | * This file is part of FFmpeg. | |
5 | * | |
6 | * FFmpeg is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; either | |
9 | * version 2.1 of the License, or (at your option) any later version. | |
10 | * | |
11 | * FFmpeg is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with FFmpeg; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <float.h> | |
22 | #include "libavutil/opt.h" | |
23 | #include "libavutil/parseutils.h" | |
24 | #include "libavutil/avstring.h" | |
25 | #include "libavutil/pixdesc.h" | |
26 | #include "libavutil/avassert.h" | |
27 | ||
28 | // FIXME those are internal headers, ffserver _really_ shouldn't use them | |
29 | #include "libavformat/ffm.h" | |
30 | ||
31 | #include "cmdutils.h" | |
32 | #include "ffserver_config.h" | |
33 | ||
34 | #define MAX_CHILD_ARGS 64 | |
35 | ||
36 | static int ffserver_save_avoption(const char *opt, const char *arg, int type, | |
37 | FFServerConfig *config); | |
38 | static void vreport_config_error(const char *filename, int line_num, int log_level, | |
39 | int *errors, const char *fmt, va_list vl); | |
40 | static void report_config_error(const char *filename, int line_num, int log_level, | |
41 | int *errors, const char *fmt, ...); | |
42 | ||
43 | #define ERROR(...) report_config_error(config->filename, config->line_num,\ | |
44 | AV_LOG_ERROR, &config->errors, __VA_ARGS__) | |
45 | #define WARNING(...) report_config_error(config->filename, config->line_num,\ | |
46 | AV_LOG_WARNING, &config->warnings, __VA_ARGS__) | |
47 | ||
48 | /* FIXME: make ffserver work with IPv6 */ | |
49 | /* resolve host with also IP address parsing */ | |
50 | static int resolve_host(struct in_addr *sin_addr, const char *hostname) | |
51 | { | |
52 | ||
53 | if (!ff_inet_aton(hostname, sin_addr)) { | |
54 | #if HAVE_GETADDRINFO | |
55 | struct addrinfo *ai, *cur; | |
56 | struct addrinfo hints = { 0 }; | |
57 | hints.ai_family = AF_INET; | |
58 | if (getaddrinfo(hostname, NULL, &hints, &ai)) | |
59 | return -1; | |
60 | /* getaddrinfo returns a linked list of addrinfo structs. | |
61 | * Even if we set ai_family = AF_INET above, make sure | |
62 | * that the returned one actually is of the correct type. */ | |
63 | for (cur = ai; cur; cur = cur->ai_next) { | |
64 | if (cur->ai_family == AF_INET) { | |
65 | *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr; | |
66 | freeaddrinfo(ai); | |
67 | return 0; | |
68 | } | |
69 | } | |
70 | freeaddrinfo(ai); | |
71 | return -1; | |
72 | #else | |
73 | struct hostent *hp; | |
74 | hp = gethostbyname(hostname); | |
75 | if (!hp) | |
76 | return -1; | |
77 | memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); | |
78 | #endif | |
79 | } | |
80 | return 0; | |
81 | } | |
82 | ||
83 | void ffserver_get_arg(char *buf, int buf_size, const char **pp) | |
84 | { | |
85 | const char *p; | |
86 | char *q; | |
87 | int quote; | |
88 | ||
89 | p = *pp; | |
90 | while (av_isspace(*p)) p++; | |
91 | q = buf; | |
92 | quote = 0; | |
93 | if (*p == '\"' || *p == '\'') | |
94 | quote = *p++; | |
95 | for(;;) { | |
96 | if (quote) { | |
97 | if (*p == quote) | |
98 | break; | |
99 | } else { | |
100 | if (av_isspace(*p)) | |
101 | break; | |
102 | } | |
103 | if (*p == '\0') | |
104 | break; | |
105 | if ((q - buf) < buf_size - 1) | |
106 | *q++ = *p; | |
107 | p++; | |
108 | } | |
109 | *q = '\0'; | |
110 | if (quote && *p == quote) | |
111 | p++; | |
112 | *pp = p; | |
113 | } | |
114 | ||
115 | void ffserver_parse_acl_row(FFServerStream *stream, FFServerStream* feed, | |
116 | FFServerIPAddressACL *ext_acl, | |
117 | const char *p, const char *filename, int line_num) | |
118 | { | |
119 | char arg[1024]; | |
120 | FFServerIPAddressACL acl; | |
121 | int errors = 0; | |
122 | ||
123 | ffserver_get_arg(arg, sizeof(arg), &p); | |
124 | if (av_strcasecmp(arg, "allow") == 0) | |
125 | acl.action = IP_ALLOW; | |
126 | else if (av_strcasecmp(arg, "deny") == 0) | |
127 | acl.action = IP_DENY; | |
128 | else { | |
129 | fprintf(stderr, "%s:%d: ACL action '%s' is not ALLOW or DENY\n", | |
130 | filename, line_num, arg); | |
131 | errors++; | |
132 | } | |
133 | ||
134 | ffserver_get_arg(arg, sizeof(arg), &p); | |
135 | ||
136 | if (resolve_host(&acl.first, arg)) { | |
137 | fprintf(stderr, "%s:%d: ACL refers to invalid host or IP address '%s'\n", | |
138 | filename, line_num, arg); | |
139 | errors++; | |
140 | } else | |
141 | acl.last = acl.first; | |
142 | ||
143 | ffserver_get_arg(arg, sizeof(arg), &p); | |
144 | ||
145 | if (arg[0]) { | |
146 | if (resolve_host(&acl.last, arg)) { | |
147 | fprintf(stderr, | |
148 | "%s:%d: ACL refers to invalid host or IP address '%s'\n", | |
149 | filename, line_num, arg); | |
150 | errors++; | |
151 | } | |
152 | } | |
153 | ||
154 | if (!errors) { | |
155 | FFServerIPAddressACL *nacl = av_mallocz(sizeof(*nacl)); | |
156 | FFServerIPAddressACL **naclp = 0; | |
157 | ||
158 | acl.next = 0; | |
159 | *nacl = acl; | |
160 | ||
161 | if (stream) | |
162 | naclp = &stream->acl; | |
163 | else if (feed) | |
164 | naclp = &feed->acl; | |
165 | else if (ext_acl) | |
166 | naclp = &ext_acl; | |
167 | else { | |
168 | fprintf(stderr, "%s:%d: ACL found not in <stream> or <feed>\n", | |
169 | filename, line_num); | |
170 | errors++; | |
171 | } | |
172 | ||
173 | if (naclp) { | |
174 | while (*naclp) | |
175 | naclp = &(*naclp)->next; | |
176 | ||
177 | *naclp = nacl; | |
178 | } else | |
179 | av_free(nacl); | |
180 | } | |
181 | } | |
182 | ||
183 | /* add a codec and set the default parameters */ | |
184 | static void add_codec(FFServerStream *stream, AVCodecContext *av, | |
185 | FFServerConfig *config) | |
186 | { | |
187 | AVStream *st; | |
188 | AVDictionary **opts, *recommended = NULL; | |
189 | char *enc_config; | |
190 | ||
191 | if(stream->nb_streams >= FF_ARRAY_ELEMS(stream->streams)) | |
192 | return; | |
193 | ||
194 | opts = av->codec_type == AVMEDIA_TYPE_AUDIO ? | |
195 | &config->audio_opts : &config->video_opts; | |
196 | av_dict_copy(&recommended, *opts, 0); | |
197 | av_opt_set_dict2(av->priv_data, opts, AV_OPT_SEARCH_CHILDREN); | |
198 | av_opt_set_dict2(av, opts, AV_OPT_SEARCH_CHILDREN); | |
199 | if (av_dict_count(*opts)) | |
200 | av_log(NULL, AV_LOG_WARNING, | |
201 | "Something is wrong, %d options are not set!\n", av_dict_count(*opts)); | |
202 | ||
203 | if (config->stream_use_defaults) { | |
204 | //TODO: reident | |
205 | /* compute default parameters */ | |
206 | switch(av->codec_type) { | |
207 | case AVMEDIA_TYPE_AUDIO: | |
208 | if (!av_dict_get(recommended, "ab", NULL, 0)) { | |
209 | av->bit_rate = 64000; | |
210 | av_dict_set_int(&recommended, "ab", av->bit_rate, 0); | |
211 | WARNING("Setting default value for audio bit rate = %d. " | |
212 | "Use NoDefaults to disable it.\n", | |
213 | av->bit_rate); | |
214 | } | |
215 | if (!av_dict_get(recommended, "ar", NULL, 0)) { | |
216 | av->sample_rate = 22050; | |
217 | av_dict_set_int(&recommended, "ar", av->sample_rate, 0); | |
218 | WARNING("Setting default value for audio sample rate = %d. " | |
219 | "Use NoDefaults to disable it.\n", | |
220 | av->sample_rate); | |
221 | } | |
222 | if (!av_dict_get(recommended, "ac", NULL, 0)) { | |
223 | av->channels = 1; | |
224 | av_dict_set_int(&recommended, "ac", av->channels, 0); | |
225 | WARNING("Setting default value for audio channel count = %d. " | |
226 | "Use NoDefaults to disable it.\n", | |
227 | av->channels); | |
228 | } | |
229 | break; | |
230 | case AVMEDIA_TYPE_VIDEO: | |
231 | if (!av_dict_get(recommended, "b", NULL, 0)) { | |
232 | av->bit_rate = 64000; | |
233 | av_dict_set_int(&recommended, "b", av->bit_rate, 0); | |
234 | WARNING("Setting default value for video bit rate = %d. " | |
235 | "Use NoDefaults to disable it.\n", | |
236 | av->bit_rate); | |
237 | } | |
238 | if (!av_dict_get(recommended, "time_base", NULL, 0)){ | |
239 | av->time_base.den = 5; | |
240 | av->time_base.num = 1; | |
241 | av_dict_set(&recommended, "time_base", "1/5", 0); | |
242 | WARNING("Setting default value for video frame rate = %d. " | |
243 | "Use NoDefaults to disable it.\n", | |
244 | av->time_base.den); | |
245 | } | |
246 | if (!av_dict_get(recommended, "video_size", NULL, 0)) { | |
247 | av->width = 160; | |
248 | av->height = 128; | |
249 | av_dict_set(&recommended, "video_size", "160x128", 0); | |
250 | WARNING("Setting default value for video size = %dx%d. " | |
251 | "Use NoDefaults to disable it.\n", | |
252 | av->width, av->height); | |
253 | } | |
254 | /* Bitrate tolerance is less for streaming */ | |
255 | if (!av_dict_get(recommended, "bt", NULL, 0)) { | |
256 | av->bit_rate_tolerance = FFMAX(av->bit_rate / 4, | |
257 | (int64_t)av->bit_rate*av->time_base.num/av->time_base.den); | |
258 | av_dict_set_int(&recommended, "bt", av->bit_rate_tolerance, 0); | |
259 | WARNING("Setting default value for video bit rate tolerance = %d. " | |
260 | "Use NoDefaults to disable it.\n", | |
261 | av->bit_rate_tolerance); | |
262 | } | |
263 | ||
264 | if (!av_dict_get(recommended, "rc_eq", NULL, 0)) { | |
265 | av->rc_eq = av_strdup("tex^qComp"); | |
266 | av_dict_set(&recommended, "rc_eq", "tex^qComp", 0); | |
267 | WARNING("Setting default value for video rate control equation = %s. " | |
268 | "Use NoDefaults to disable it.\n", | |
269 | av->rc_eq); | |
270 | } | |
271 | if (!av_dict_get(recommended, "maxrate", NULL, 0)) { | |
272 | av->rc_max_rate = av->bit_rate * 2; | |
273 | av_dict_set_int(&recommended, "maxrate", av->rc_max_rate, 0); | |
274 | WARNING("Setting default value for video max rate = %d. " | |
275 | "Use NoDefaults to disable it.\n", | |
276 | av->rc_max_rate); | |
277 | } | |
278 | ||
279 | if (av->rc_max_rate && !av_dict_get(recommended, "bufsize", NULL, 0)) { | |
280 | av->rc_buffer_size = av->rc_max_rate; | |
281 | av_dict_set_int(&recommended, "bufsize", av->rc_buffer_size, 0); | |
282 | WARNING("Setting default value for video buffer size = %d. " | |
283 | "Use NoDefaults to disable it.\n", | |
284 | av->rc_buffer_size); | |
285 | } | |
286 | break; | |
287 | default: | |
288 | abort(); | |
289 | } | |
290 | } else { | |
291 | switch(av->codec_type) { | |
292 | case AVMEDIA_TYPE_AUDIO: | |
293 | if (av->bit_rate == 0) | |
294 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
295 | &config->errors, "audio bit rate is not set\n"); | |
296 | if (av->sample_rate == 0) | |
297 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
298 | &config->errors, "audio sample rate is not set\n"); | |
299 | break; | |
300 | case AVMEDIA_TYPE_VIDEO: | |
301 | if (av->width == 0 || av->height == 0) | |
302 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
303 | &config->errors, "video size is not set\n"); | |
304 | break; | |
305 | default: | |
306 | av_assert0(0); | |
307 | } | |
308 | } | |
309 | ||
310 | st = av_mallocz(sizeof(AVStream)); | |
311 | if (!st) | |
312 | return; | |
313 | av_dict_get_string(recommended, &enc_config, '=', ','); | |
314 | av_dict_free(&recommended); | |
315 | av_stream_set_recommended_encoder_configuration(st, enc_config); | |
316 | st->codec = av; | |
317 | stream->streams[stream->nb_streams++] = st; | |
318 | } | |
319 | ||
320 | static int ffserver_set_codec(AVCodecContext *ctx, const char *codec_name, FFServerConfig *config) | |
321 | { | |
322 | int ret; | |
323 | AVCodec *codec = avcodec_find_encoder_by_name(codec_name); | |
324 | if (!codec || codec->type != ctx->codec_type) { | |
325 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
326 | &config->errors, "Invalid codec name: %s\n", codec_name); | |
327 | return 0; | |
328 | } | |
329 | if (ctx->codec_id == AV_CODEC_ID_NONE && !ctx->priv_data) { | |
330 | if ((ret = avcodec_get_context_defaults3(ctx, codec)) < 0) | |
331 | return ret; | |
332 | ctx->codec = codec; | |
333 | } | |
334 | if (ctx->codec_id != codec->id) | |
335 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, &config->errors, | |
336 | "Inconsistent configuration: trying to set %s codec option, but %s codec is used previously\n", | |
337 | codec_name, avcodec_get_name(ctx->codec_id)); | |
338 | return 0; | |
339 | } | |
340 | ||
341 | static int ffserver_opt_preset(const char *arg, int type, FFServerConfig *config) | |
342 | { | |
343 | FILE *f=NULL; | |
344 | char filename[1000], tmp[1000], tmp2[1000], line[1000]; | |
345 | int ret = 0; | |
346 | AVCodecContext *avctx; | |
347 | const AVCodec *codec; | |
348 | ||
349 | switch(type) { | |
350 | case AV_OPT_FLAG_AUDIO_PARAM: | |
351 | avctx = config->dummy_actx; | |
352 | break; | |
353 | case AV_OPT_FLAG_VIDEO_PARAM: | |
354 | avctx = config->dummy_vctx; | |
355 | break; | |
356 | default: | |
357 | av_assert0(0); | |
358 | } | |
359 | codec = avcodec_find_encoder(avctx->codec_id); | |
360 | ||
361 | if (!(f = get_preset_file(filename, sizeof(filename), arg, 0, | |
362 | codec ? codec->name : NULL))) { | |
363 | av_log(NULL, AV_LOG_ERROR, "File for preset '%s' not found\n", arg); | |
364 | return AVERROR(EINVAL); | |
365 | } | |
366 | ||
367 | while(!feof(f)){ | |
368 | int e= fscanf(f, "%999[^\n]\n", line) - 1; | |
369 | if(line[0] == '#' && !e) | |
370 | continue; | |
371 | e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2; | |
372 | if(e){ | |
373 | av_log(NULL, AV_LOG_ERROR, "%s: Invalid syntax: '%s'\n", filename, line); | |
374 | ret = AVERROR(EINVAL); | |
375 | break; | |
376 | } | |
377 | if ((!strcmp(tmp, "acodec") && avctx->codec_type == AVMEDIA_TYPE_AUDIO) || | |
378 | !strcmp(tmp, "vcodec") && avctx->codec_type == AVMEDIA_TYPE_VIDEO) | |
379 | { | |
380 | if (ffserver_set_codec(avctx, tmp2, config) < 0) | |
381 | break; | |
382 | } else if (!strcmp(tmp, "scodec")) { | |
383 | av_log(NULL, AV_LOG_ERROR, "Subtitles preset found.\n"); | |
384 | ret = AVERROR(EINVAL); | |
385 | break; | |
386 | } else if (ffserver_save_avoption(tmp, tmp2, type, config) < 0) | |
387 | break; | |
388 | } | |
389 | ||
390 | fclose(f); | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
395 | static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename, const char *mime_type) | |
396 | { | |
397 | AVOutputFormat *fmt = av_guess_format(short_name, filename, mime_type); | |
398 | ||
399 | if (fmt) { | |
400 | AVOutputFormat *stream_fmt; | |
401 | char stream_format_name[64]; | |
402 | ||
403 | snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", | |
404 | fmt->name); | |
405 | stream_fmt = av_guess_format(stream_format_name, NULL, NULL); | |
406 | ||
407 | if (stream_fmt) | |
408 | fmt = stream_fmt; | |
409 | } | |
410 | ||
411 | return fmt; | |
412 | } | |
413 | ||
414 | static void vreport_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, va_list vl) | |
415 | { | |
416 | av_log(NULL, log_level, "%s:%d: ", filename, line_num); | |
417 | av_vlog(NULL, log_level, fmt, vl); | |
418 | if (errors) | |
419 | (*errors)++; | |
420 | } | |
421 | ||
422 | static void report_config_error(const char *filename, int line_num, int log_level, int *errors, const char *fmt, ...) | |
423 | { | |
424 | va_list vl; | |
425 | va_start(vl, fmt); | |
426 | vreport_config_error(filename, line_num, log_level, errors, fmt, vl); | |
427 | va_end(vl); | |
428 | } | |
429 | ||
430 | static int ffserver_set_int_param(int *dest, const char *value, int factor, int min, int max, | |
431 | FFServerConfig *config, const char *error_msg, ...) | |
432 | { | |
433 | int tmp; | |
434 | char *tailp; | |
435 | if (!value || !value[0]) | |
436 | goto error; | |
437 | errno = 0; | |
438 | tmp = strtol(value, &tailp, 0); | |
439 | if (tmp < min || tmp > max) | |
440 | goto error; | |
441 | if (factor) { | |
442 | if (FFABS(tmp) > INT_MAX / FFABS(factor)) | |
443 | goto error; | |
444 | tmp *= factor; | |
445 | } | |
446 | if (tailp[0] || errno) | |
447 | goto error; | |
448 | if (dest) | |
449 | *dest = tmp; | |
450 | return 0; | |
451 | error: | |
452 | if (config) { | |
453 | va_list vl; | |
454 | va_start(vl, error_msg); | |
455 | vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
456 | &config->errors, error_msg, vl); | |
457 | va_end(vl); | |
458 | } | |
459 | return AVERROR(EINVAL); | |
460 | } | |
461 | ||
462 | static int ffserver_set_float_param(float *dest, const char *value, float factor, float min, float max, | |
463 | FFServerConfig *config, const char *error_msg, ...) | |
464 | { | |
465 | double tmp; | |
466 | char *tailp; | |
467 | if (!value || !value[0]) | |
468 | goto error; | |
469 | errno = 0; | |
470 | tmp = strtod(value, &tailp); | |
471 | if (tmp < min || tmp > max) | |
472 | goto error; | |
473 | if (factor) | |
474 | tmp *= factor; | |
475 | if (tailp[0] || errno) | |
476 | goto error; | |
477 | if (dest) | |
478 | *dest = tmp; | |
479 | return 0; | |
480 | error: | |
481 | if (config) { | |
482 | va_list vl; | |
483 | va_start(vl, error_msg); | |
484 | vreport_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
485 | &config->errors, error_msg, vl); | |
486 | va_end(vl); | |
487 | } | |
488 | return AVERROR(EINVAL); | |
489 | } | |
490 | ||
491 | static int ffserver_save_avoption(const char *opt, const char *arg, int type, FFServerConfig *config) | |
492 | { | |
493 | static int hinted = 0; | |
494 | int ret = 0; | |
495 | AVDictionaryEntry *e; | |
496 | const AVOption *o = NULL; | |
497 | const char *option = NULL; | |
498 | const char *codec_name = NULL; | |
499 | char buff[1024]; | |
500 | AVCodecContext *ctx; | |
501 | AVDictionary **dict; | |
502 | enum AVCodecID guessed_codec_id; | |
503 | ||
504 | switch (type) { | |
505 | case AV_OPT_FLAG_VIDEO_PARAM: | |
506 | ctx = config->dummy_vctx; | |
507 | dict = &config->video_opts; | |
508 | guessed_codec_id = config->guessed_video_codec_id != AV_CODEC_ID_NONE ? | |
509 | config->guessed_video_codec_id : AV_CODEC_ID_H264; | |
510 | break; | |
511 | case AV_OPT_FLAG_AUDIO_PARAM: | |
512 | ctx = config->dummy_actx; | |
513 | dict = &config->audio_opts; | |
514 | guessed_codec_id = config->guessed_audio_codec_id != AV_CODEC_ID_NONE ? | |
515 | config->guessed_audio_codec_id : AV_CODEC_ID_AAC; | |
516 | break; | |
517 | default: | |
518 | av_assert0(0); | |
519 | } | |
520 | ||
521 | if (strchr(opt, ':')) { | |
522 | //explicit private option | |
523 | snprintf(buff, sizeof(buff), "%s", opt); | |
524 | codec_name = buff; | |
525 | option = strchr(buff, ':'); | |
526 | buff[option - buff] = '\0'; | |
527 | option++; | |
528 | if ((ret = ffserver_set_codec(ctx, codec_name, config)) < 0) | |
529 | return ret; | |
530 | if (!ctx->codec || !ctx->priv_data) | |
531 | return -1; | |
532 | } else { | |
533 | option = opt; | |
534 | } | |
535 | ||
536 | o = av_opt_find(ctx, option, NULL, type | AV_OPT_FLAG_ENCODING_PARAM, AV_OPT_SEARCH_CHILDREN); | |
537 | if (!o && (!strcmp(option, "time_base") || !strcmp(option, "pixel_format") || | |
538 | !strcmp(option, "video_size") || !strcmp(option, "codec_tag"))) | |
539 | o = av_opt_find(ctx, option, NULL, 0, 0); | |
540 | if (!o) { | |
541 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
542 | &config->errors, "Option not found: %s\n", opt); | |
543 | if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) { | |
544 | hinted = 1; | |
545 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, NULL, | |
546 | "If '%s' is a codec private option, then prefix it with codec name, " | |
547 | "for example '%s:%s %s' or define codec earlier.\n", | |
548 | opt, avcodec_get_name(guessed_codec_id) ,opt, arg); | |
549 | } | |
550 | } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) { | |
551 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
552 | &config->errors, "Invalid value for option %s (%s): %s\n", opt, | |
553 | arg, av_err2str(ret)); | |
554 | } else if ((e = av_dict_get(*dict, option, NULL, 0))) { | |
555 | if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-')) | |
556 | return av_dict_set(dict, option, arg, AV_DICT_APPEND); | |
557 | report_config_error(config->filename, config->line_num, AV_LOG_ERROR, | |
558 | &config->errors, | |
559 | "Redeclaring value of the option %s, previous value: %s\n", | |
560 | opt, e->value); | |
561 | } else if (av_dict_set(dict, option, arg, 0) < 0) { | |
562 | return AVERROR(ENOMEM); | |
563 | } | |
564 | return 0; | |
565 | } | |
566 | ||
567 | static int ffserver_save_avoption_int(const char *opt, int64_t arg, | |
568 | int type, FFServerConfig *config) | |
569 | { | |
570 | char buf[22]; | |
571 | snprintf(buf, sizeof(buf), "%"PRId64, arg); | |
572 | return ffserver_save_avoption(opt, buf, type, config); | |
573 | } | |
574 | ||
575 | static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd, | |
576 | const char **p) | |
577 | { | |
578 | int val; | |
579 | char arg[1024]; | |
580 | if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) { | |
581 | if (!av_strcasecmp(cmd, "Port")) | |
582 | WARNING("Port option is deprecated, use HTTPPort instead\n"); | |
583 | ffserver_get_arg(arg, sizeof(arg), p); | |
584 | ffserver_set_int_param(&val, arg, 0, 1, 65535, config, | |
585 | "Invalid port: %s\n", arg); | |
586 | if (val < 1024) | |
587 | WARNING("Trying to use IETF assigned system port: %d\n", val); | |
588 | config->http_addr.sin_port = htons(val); | |
589 | } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) { | |
590 | if (!av_strcasecmp(cmd, "BindAddress")) | |
591 | WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n"); | |
592 | ffserver_get_arg(arg, sizeof(arg), p); | |
593 | if (resolve_host(&config->http_addr.sin_addr, arg)) | |
594 | ERROR("Invalid host/IP address: %s\n", arg); | |
595 | } else if (!av_strcasecmp(cmd, "NoDaemon")) { | |
596 | WARNING("NoDaemon option has no effect, you should remove it\n"); | |
597 | } else if (!av_strcasecmp(cmd, "RTSPPort")) { | |
598 | ffserver_get_arg(arg, sizeof(arg), p); | |
599 | ffserver_set_int_param(&val, arg, 0, 1, 65535, config, | |
600 | "Invalid port: %s\n", arg); | |
601 | config->rtsp_addr.sin_port = htons(val); | |
602 | } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) { | |
603 | ffserver_get_arg(arg, sizeof(arg), p); | |
604 | if (resolve_host(&config->rtsp_addr.sin_addr, arg)) | |
605 | ERROR("Invalid host/IP address: %s\n", arg); | |
606 | } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) { | |
607 | ffserver_get_arg(arg, sizeof(arg), p); | |
608 | ffserver_set_int_param(&val, arg, 0, 1, 65535, config, | |
609 | "Invalid MaxHTTPConnections: %s\n", arg); | |
610 | config->nb_max_http_connections = val; | |
611 | if (config->nb_max_connections > config->nb_max_http_connections) | |
612 | ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n", | |
613 | config->nb_max_connections, config->nb_max_http_connections); | |
614 | } else if (!av_strcasecmp(cmd, "MaxClients")) { | |
615 | ffserver_get_arg(arg, sizeof(arg), p); | |
616 | ffserver_set_int_param(&val, arg, 0, 1, 65535, config, | |
617 | "Invalid MaxClients: %s\n", arg); | |
618 | config->nb_max_connections = val; | |
619 | if (config->nb_max_connections > config->nb_max_http_connections) | |
620 | ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n", | |
621 | config->nb_max_connections, config->nb_max_http_connections); | |
622 | } else if (!av_strcasecmp(cmd, "MaxBandwidth")) { | |
623 | int64_t llval; | |
624 | char *tailp; | |
625 | ffserver_get_arg(arg, sizeof(arg), p); | |
626 | errno = 0; | |
627 | llval = strtoll(arg, &tailp, 10); | |
628 | if (llval < 10 || llval > 10000000 || tailp[0] || errno) | |
629 | ERROR("Invalid MaxBandwidth: %s\n", arg); | |
630 | else | |
631 | config->max_bandwidth = llval; | |
632 | } else if (!av_strcasecmp(cmd, "CustomLog")) { | |
633 | if (!config->debug) | |
634 | ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p); | |
635 | } else if (!av_strcasecmp(cmd, "LoadModule")) { | |
636 | ERROR("Loadable modules are no longer supported\n"); | |
637 | } else if (!av_strcasecmp(cmd, "NoDefaults")) { | |
638 | config->use_defaults = 0; | |
639 | } else if (!av_strcasecmp(cmd, "UseDefaults")) { | |
640 | config->use_defaults = 1; | |
641 | } else | |
642 | ERROR("Incorrect keyword: '%s'\n", cmd); | |
643 | return 0; | |
644 | } | |
645 | ||
646 | static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p, | |
647 | FFServerStream **pfeed) | |
648 | { | |
649 | FFServerStream *feed; | |
650 | char arg[1024]; | |
651 | av_assert0(pfeed); | |
652 | feed = *pfeed; | |
653 | if (!av_strcasecmp(cmd, "<Feed")) { | |
654 | char *q; | |
655 | FFServerStream *s; | |
656 | feed = av_mallocz(sizeof(FFServerStream)); | |
657 | if (!feed) | |
658 | return AVERROR(ENOMEM); | |
659 | ffserver_get_arg(feed->filename, sizeof(feed->filename), p); | |
660 | q = strrchr(feed->filename, '>'); | |
661 | if (*q) | |
662 | *q = '\0'; | |
663 | ||
664 | for (s = config->first_feed; s; s = s->next) { | |
665 | if (!strcmp(feed->filename, s->filename)) | |
666 | ERROR("Feed '%s' already registered\n", s->filename); | |
667 | } | |
668 | ||
669 | feed->fmt = av_guess_format("ffm", NULL, NULL); | |
670 | /* default feed file */ | |
671 | snprintf(feed->feed_filename, sizeof(feed->feed_filename), | |
672 | "/tmp/%s.ffm", feed->filename); | |
673 | feed->feed_max_size = 5 * 1024 * 1024; | |
674 | feed->is_feed = 1; | |
675 | feed->feed = feed; /* self feeding :-) */ | |
676 | *pfeed = feed; | |
677 | return 0; | |
678 | } | |
679 | av_assert0(feed); | |
680 | if (!av_strcasecmp(cmd, "Launch")) { | |
681 | int i; | |
682 | ||
683 | feed->child_argv = av_mallocz_array(MAX_CHILD_ARGS, sizeof(char *)); | |
684 | if (!feed->child_argv) | |
685 | return AVERROR(ENOMEM); | |
686 | for (i = 0; i < MAX_CHILD_ARGS - 2; i++) { | |
687 | ffserver_get_arg(arg, sizeof(arg), p); | |
688 | if (!arg[0]) | |
689 | break; | |
690 | ||
691 | feed->child_argv[i] = av_strdup(arg); | |
692 | if (!feed->child_argv[i]) | |
693 | return AVERROR(ENOMEM); | |
694 | } | |
695 | ||
696 | feed->child_argv[i] = | |
697 | av_asprintf("http://%s:%d/%s", | |
698 | (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" : | |
699 | inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port), | |
700 | feed->filename); | |
701 | if (!feed->child_argv[i]) | |
702 | return AVERROR(ENOMEM); | |
703 | } else if (!av_strcasecmp(cmd, "ACL")) { | |
704 | ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename, | |
705 | config->line_num); | |
706 | } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) { | |
707 | ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p); | |
708 | feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile"); | |
709 | } else if (!av_strcasecmp(cmd, "Truncate")) { | |
710 | ffserver_get_arg(arg, sizeof(arg), p); | |
711 | /* assume Truncate is true in case no argument is specified */ | |
712 | if (!arg[0]) { | |
713 | feed->truncate = 1; | |
714 | } else { | |
715 | WARNING("Truncate N syntax in configuration file is deprecated, " | |
716 | "use Truncate alone with no arguments\n"); | |
717 | feed->truncate = strtod(arg, NULL); | |
718 | } | |
719 | } else if (!av_strcasecmp(cmd, "FileMaxSize")) { | |
720 | char *p1; | |
721 | double fsize; | |
722 | ||
723 | ffserver_get_arg(arg, sizeof(arg), p); | |
724 | p1 = arg; | |
725 | fsize = strtod(p1, &p1); | |
726 | switch(av_toupper(*p1)) { | |
727 | case 'K': | |
728 | fsize *= 1024; | |
729 | break; | |
730 | case 'M': | |
731 | fsize *= 1024 * 1024; | |
732 | break; | |
733 | case 'G': | |
734 | fsize *= 1024 * 1024 * 1024; | |
735 | break; | |
736 | default: | |
737 | ERROR("Invalid file size: %s\n", arg); | |
738 | break; | |
739 | } | |
740 | feed->feed_max_size = (int64_t)fsize; | |
741 | if (feed->feed_max_size < FFM_PACKET_SIZE*4) | |
742 | ERROR("Feed max file size is too small, must be at least %d\n", | |
743 | FFM_PACKET_SIZE*4); | |
744 | } else if (!av_strcasecmp(cmd, "</Feed>")) { | |
745 | *pfeed = NULL; | |
746 | } else { | |
747 | ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd); | |
748 | } | |
749 | return 0; | |
750 | } | |
751 | ||
752 | static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p, | |
753 | FFServerStream **pstream) | |
754 | { | |
755 | char arg[1024], arg2[1024]; | |
756 | FFServerStream *stream; | |
757 | int val; | |
758 | ||
759 | av_assert0(pstream); | |
760 | stream = *pstream; | |
761 | ||
762 | if (!av_strcasecmp(cmd, "<Stream")) { | |
763 | char *q; | |
764 | FFServerStream *s; | |
765 | stream = av_mallocz(sizeof(FFServerStream)); | |
766 | if (!stream) | |
767 | return AVERROR(ENOMEM); | |
768 | config->dummy_actx = avcodec_alloc_context3(NULL); | |
769 | config->dummy_vctx = avcodec_alloc_context3(NULL); | |
770 | if (!config->dummy_vctx || !config->dummy_actx) { | |
771 | av_free(stream); | |
772 | avcodec_free_context(&config->dummy_vctx); | |
773 | avcodec_free_context(&config->dummy_actx); | |
774 | return AVERROR(ENOMEM); | |
775 | } | |
776 | config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO; | |
777 | config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO; | |
778 | ffserver_get_arg(stream->filename, sizeof(stream->filename), p); | |
779 | q = strrchr(stream->filename, '>'); | |
780 | if (q) | |
781 | *q = '\0'; | |
782 | ||
783 | for (s = config->first_stream; s; s = s->next) { | |
784 | if (!strcmp(stream->filename, s->filename)) | |
785 | ERROR("Stream '%s' already registered\n", s->filename); | |
786 | } | |
787 | ||
788 | stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL); | |
789 | if (stream->fmt) { | |
790 | config->guessed_audio_codec_id = stream->fmt->audio_codec; | |
791 | config->guessed_video_codec_id = stream->fmt->video_codec; | |
792 | } else { | |
793 | config->guessed_audio_codec_id = AV_CODEC_ID_NONE; | |
794 | config->guessed_video_codec_id = AV_CODEC_ID_NONE; | |
795 | } | |
796 | config->stream_use_defaults = config->use_defaults; | |
797 | *pstream = stream; | |
798 | return 0; | |
799 | } | |
800 | av_assert0(stream); | |
801 | if (!av_strcasecmp(cmd, "Feed")) { | |
802 | FFServerStream *sfeed; | |
803 | ffserver_get_arg(arg, sizeof(arg), p); | |
804 | sfeed = config->first_feed; | |
805 | while (sfeed) { | |
806 | if (!strcmp(sfeed->filename, arg)) | |
807 | break; | |
808 | sfeed = sfeed->next_feed; | |
809 | } | |
810 | if (!sfeed) | |
811 | ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg, | |
812 | stream->filename); | |
813 | else | |
814 | stream->feed = sfeed; | |
815 | } else if (!av_strcasecmp(cmd, "Format")) { | |
816 | ffserver_get_arg(arg, sizeof(arg), p); | |
817 | if (!strcmp(arg, "status")) { | |
818 | stream->stream_type = STREAM_TYPE_STATUS; | |
819 | stream->fmt = NULL; | |
820 | } else { | |
821 | stream->stream_type = STREAM_TYPE_LIVE; | |
822 | /* JPEG cannot be used here, so use single frame MJPEG */ | |
823 | if (!strcmp(arg, "jpeg")) | |
824 | strcpy(arg, "mjpeg"); | |
825 | stream->fmt = ffserver_guess_format(arg, NULL, NULL); | |
826 | if (!stream->fmt) | |
827 | ERROR("Unknown Format: %s\n", arg); | |
828 | } | |
829 | if (stream->fmt) { | |
830 | config->guessed_audio_codec_id = stream->fmt->audio_codec; | |
831 | config->guessed_video_codec_id = stream->fmt->video_codec; | |
832 | } | |
833 | } else if (!av_strcasecmp(cmd, "InputFormat")) { | |
834 | ffserver_get_arg(arg, sizeof(arg), p); | |
835 | stream->ifmt = av_find_input_format(arg); | |
836 | if (!stream->ifmt) | |
837 | ERROR("Unknown input format: %s\n", arg); | |
838 | } else if (!av_strcasecmp(cmd, "FaviconURL")) { | |
839 | if (stream->stream_type == STREAM_TYPE_STATUS) | |
840 | ffserver_get_arg(stream->feed_filename, | |
841 | sizeof(stream->feed_filename), p); | |
842 | else | |
843 | ERROR("FaviconURL only permitted for status streams\n"); | |
844 | } else if (!av_strcasecmp(cmd, "Author") || | |
845 | !av_strcasecmp(cmd, "Comment") || | |
846 | !av_strcasecmp(cmd, "Copyright") || | |
847 | !av_strcasecmp(cmd, "Title")) { | |
848 | char key[32]; | |
849 | int i; | |
850 | ffserver_get_arg(arg, sizeof(arg), p); | |
851 | for (i = 0; i < strlen(cmd); i++) | |
852 | key[i] = av_tolower(cmd[i]); | |
853 | key[i] = 0; | |
854 | WARNING("'%s' option in configuration file is deprecated, " | |
855 | "use 'Metadata %s VALUE' instead\n", cmd, key); | |
856 | if (av_dict_set(&stream->metadata, key, arg, 0) < 0) | |
857 | goto nomem; | |
858 | } else if (!av_strcasecmp(cmd, "Metadata")) { | |
859 | ffserver_get_arg(arg, sizeof(arg), p); | |
860 | ffserver_get_arg(arg2, sizeof(arg2), p); | |
861 | if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0) | |
862 | goto nomem; | |
863 | } else if (!av_strcasecmp(cmd, "Preroll")) { | |
864 | ffserver_get_arg(arg, sizeof(arg), p); | |
865 | stream->prebuffer = atof(arg) * 1000; | |
866 | } else if (!av_strcasecmp(cmd, "StartSendOnKey")) { | |
867 | stream->send_on_key = 1; | |
868 | } else if (!av_strcasecmp(cmd, "AudioCodec")) { | |
869 | ffserver_get_arg(arg, sizeof(arg), p); | |
870 | ffserver_set_codec(config->dummy_actx, arg, config); | |
871 | } else if (!av_strcasecmp(cmd, "VideoCodec")) { | |
872 | ffserver_get_arg(arg, sizeof(arg), p); | |
873 | ffserver_set_codec(config->dummy_vctx, arg, config); | |
874 | } else if (!av_strcasecmp(cmd, "MaxTime")) { | |
875 | ffserver_get_arg(arg, sizeof(arg), p); | |
876 | stream->max_time = atof(arg) * 1000; | |
877 | } else if (!av_strcasecmp(cmd, "AudioBitRate")) { | |
878 | float f; | |
879 | ffserver_get_arg(arg, sizeof(arg), p); | |
880 | ffserver_set_float_param(&f, arg, 1000, -FLT_MAX, FLT_MAX, config, | |
881 | "Invalid %s: %s\n", cmd, arg); | |
882 | if (ffserver_save_avoption_int("ab", (int64_t)lrintf(f), AV_OPT_FLAG_AUDIO_PARAM, config) < 0) | |
883 | goto nomem; | |
884 | } else if (!av_strcasecmp(cmd, "AudioChannels")) { | |
885 | ffserver_get_arg(arg, sizeof(arg), p); | |
886 | if (ffserver_save_avoption("ac", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0) | |
887 | goto nomem; | |
888 | } else if (!av_strcasecmp(cmd, "AudioSampleRate")) { | |
889 | ffserver_get_arg(arg, sizeof(arg), p); | |
890 | if (ffserver_save_avoption("ar", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0) | |
891 | goto nomem; | |
892 | } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) { | |
893 | int minrate, maxrate; | |
894 | char *dash; | |
895 | ffserver_get_arg(arg, sizeof(arg), p); | |
896 | dash = strchr(arg, '-'); | |
897 | if (dash) { | |
898 | *dash = '\0'; | |
899 | dash++; | |
900 | if (ffserver_set_int_param(&minrate, arg, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0 && | |
901 | ffserver_set_int_param(&maxrate, dash, 1000, 0, INT_MAX, config, "Invalid %s: %s", cmd, arg) >= 0) { | |
902 | if (ffserver_save_avoption_int("minrate", minrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || | |
903 | ffserver_save_avoption_int("maxrate", maxrate, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
904 | goto nomem; | |
905 | } | |
906 | } else | |
907 | ERROR("Incorrect format for VideoBitRateRange -- should be " | |
908 | "<min>-<max>: %s\n", arg); | |
909 | } else if (!av_strcasecmp(cmd, "Debug")) { | |
910 | ffserver_get_arg(arg, sizeof(arg), p); | |
911 | if (ffserver_save_avoption("debug", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 || | |
912 | ffserver_save_avoption("debug", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
913 | goto nomem; | |
914 | } else if (!av_strcasecmp(cmd, "Strict")) { | |
915 | ffserver_get_arg(arg, sizeof(arg), p); | |
916 | if (ffserver_save_avoption("strict", arg, AV_OPT_FLAG_AUDIO_PARAM, config) < 0 || | |
917 | ffserver_save_avoption("strict", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
918 | goto nomem; | |
919 | } else if (!av_strcasecmp(cmd, "VideoBufferSize")) { | |
920 | ffserver_get_arg(arg, sizeof(arg), p); | |
921 | ffserver_set_int_param(&val, arg, 8*1024, 0, INT_MAX, config, | |
922 | "Invalid %s: %s", cmd, arg); | |
923 | if (ffserver_save_avoption_int("bufsize", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
924 | goto nomem; | |
925 | } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) { | |
926 | ffserver_get_arg(arg, sizeof(arg), p); | |
927 | ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config, | |
928 | "Invalid %s: %s", cmd, arg); | |
929 | if (ffserver_save_avoption_int("bt", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
930 | goto nomem; | |
931 | } else if (!av_strcasecmp(cmd, "VideoBitRate")) { | |
932 | ffserver_get_arg(arg, sizeof(arg), p); | |
933 | ffserver_set_int_param(&val, arg, 1000, INT_MIN, INT_MAX, config, | |
934 | "Invalid %s: %s", cmd, arg); | |
935 | if (ffserver_save_avoption_int("b", val, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
936 | goto nomem; | |
937 | } else if (!av_strcasecmp(cmd, "VideoSize")) { | |
938 | int ret, w, h; | |
939 | ffserver_get_arg(arg, sizeof(arg), p); | |
940 | ret = av_parse_video_size(&w, &h, arg); | |
941 | if (ret < 0) | |
942 | ERROR("Invalid video size '%s'\n", arg); | |
943 | else { | |
944 | if (w % 2 || h % 2) | |
945 | WARNING("Image size is not a multiple of 2\n"); | |
946 | if (ffserver_save_avoption("video_size", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
947 | goto nomem; | |
948 | } | |
949 | } else if (!av_strcasecmp(cmd, "VideoFrameRate")) { | |
950 | ffserver_get_arg(&arg[2], sizeof(arg) - 2, p); | |
951 | arg[0] = '1'; arg[1] = '/'; | |
952 | if (ffserver_save_avoption("time_base", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
953 | goto nomem; | |
954 | } else if (!av_strcasecmp(cmd, "PixelFormat")) { | |
955 | enum AVPixelFormat pix_fmt; | |
956 | ffserver_get_arg(arg, sizeof(arg), p); | |
957 | pix_fmt = av_get_pix_fmt(arg); | |
958 | if (pix_fmt == AV_PIX_FMT_NONE) | |
959 | ERROR("Unknown pixel format: %s\n", arg); | |
960 | else if (ffserver_save_avoption("pixel_format", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
961 | goto nomem; | |
962 | } else if (!av_strcasecmp(cmd, "VideoGopSize")) { | |
963 | ffserver_get_arg(arg, sizeof(arg), p); | |
964 | if (ffserver_save_avoption("g", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
965 | goto nomem; | |
966 | } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) { | |
967 | if (ffserver_save_avoption("g", "1", AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
968 | goto nomem; | |
969 | } else if (!av_strcasecmp(cmd, "VideoHighQuality")) { | |
970 | if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
971 | goto nomem; | |
972 | } else if (!av_strcasecmp(cmd, "Video4MotionVector")) { | |
973 | if (ffserver_save_avoption("mbd", "+bits", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || //FIXME remove | |
974 | ffserver_save_avoption("flags", "+mv4", AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
975 | goto nomem; | |
976 | } else if (!av_strcasecmp(cmd, "AVOptionVideo") || | |
977 | !av_strcasecmp(cmd, "AVOptionAudio")) { | |
978 | int ret; | |
979 | ffserver_get_arg(arg, sizeof(arg), p); | |
980 | ffserver_get_arg(arg2, sizeof(arg2), p); | |
981 | if (!av_strcasecmp(cmd, "AVOptionVideo")) | |
982 | ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config); | |
983 | else | |
984 | ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config); | |
985 | if (ret < 0) | |
986 | goto nomem; | |
987 | } else if (!av_strcasecmp(cmd, "AVPresetVideo") || | |
988 | !av_strcasecmp(cmd, "AVPresetAudio")) { | |
989 | ffserver_get_arg(arg, sizeof(arg), p); | |
990 | if (!av_strcasecmp(cmd, "AVPresetVideo")) | |
991 | ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config); | |
992 | else | |
993 | ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config); | |
994 | } else if (!av_strcasecmp(cmd, "VideoTag")) { | |
995 | ffserver_get_arg(arg, sizeof(arg), p); | |
996 | if (strlen(arg) == 4 && | |
997 | ffserver_save_avoption_int("codec_tag", MKTAG(arg[0], arg[1], arg[2], arg[3]), | |
998 | AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
999 | goto nomem; | |
1000 | } else if (!av_strcasecmp(cmd, "BitExact")) { | |
1001 | if (ffserver_save_avoption("flags", "+bitexact", AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1002 | goto nomem; | |
1003 | } else if (!av_strcasecmp(cmd, "DctFastint")) { | |
1004 | if (ffserver_save_avoption("dct", "fastint", AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1005 | goto nomem; | |
1006 | } else if (!av_strcasecmp(cmd, "IdctSimple")) { | |
1007 | if (ffserver_save_avoption("idct", "simple", AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1008 | goto nomem; | |
1009 | } else if (!av_strcasecmp(cmd, "Qscale")) { | |
1010 | ffserver_get_arg(arg, sizeof(arg), p); | |
1011 | ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config, | |
1012 | "Invalid Qscale: %s\n", arg); | |
1013 | if (ffserver_save_avoption("flags", "+qscale", AV_OPT_FLAG_VIDEO_PARAM, config) < 0 || | |
1014 | ffserver_save_avoption_int("global_quality", FF_QP2LAMBDA * val, | |
1015 | AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1016 | goto nomem; | |
1017 | } else if (!av_strcasecmp(cmd, "VideoQDiff")) { | |
1018 | ffserver_get_arg(arg, sizeof(arg), p); | |
1019 | if (ffserver_save_avoption("qdiff", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1020 | goto nomem; | |
1021 | } else if (!av_strcasecmp(cmd, "VideoQMax")) { | |
1022 | ffserver_get_arg(arg, sizeof(arg), p); | |
1023 | if (ffserver_save_avoption("qmax", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1024 | goto nomem; | |
1025 | } else if (!av_strcasecmp(cmd, "VideoQMin")) { | |
1026 | ffserver_get_arg(arg, sizeof(arg), p); | |
1027 | if (ffserver_save_avoption("qmin", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1028 | goto nomem; | |
1029 | } else if (!av_strcasecmp(cmd, "LumiMask")) { | |
1030 | ffserver_get_arg(arg, sizeof(arg), p); | |
1031 | if (ffserver_save_avoption("lumi_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1032 | goto nomem; | |
1033 | } else if (!av_strcasecmp(cmd, "DarkMask")) { | |
1034 | ffserver_get_arg(arg, sizeof(arg), p); | |
1035 | if (ffserver_save_avoption("dark_mask", arg, AV_OPT_FLAG_VIDEO_PARAM, config) < 0) | |
1036 | goto nomem; | |
1037 | } else if (!av_strcasecmp(cmd, "NoVideo")) { | |
1038 | config->no_video = 1; | |
1039 | } else if (!av_strcasecmp(cmd, "NoAudio")) { | |
1040 | config->no_audio = 1; | |
1041 | } else if (!av_strcasecmp(cmd, "ACL")) { | |
1042 | ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename, | |
1043 | config->line_num); | |
1044 | } else if (!av_strcasecmp(cmd, "DynamicACL")) { | |
1045 | ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p); | |
1046 | } else if (!av_strcasecmp(cmd, "RTSPOption")) { | |
1047 | ffserver_get_arg(arg, sizeof(arg), p); | |
1048 | av_freep(&stream->rtsp_option); | |
1049 | stream->rtsp_option = av_strdup(arg); | |
1050 | } else if (!av_strcasecmp(cmd, "MulticastAddress")) { | |
1051 | ffserver_get_arg(arg, sizeof(arg), p); | |
1052 | if (resolve_host(&stream->multicast_ip, arg)) | |
1053 | ERROR("Invalid host/IP address: %s\n", arg); | |
1054 | stream->is_multicast = 1; | |
1055 | stream->loop = 1; /* default is looping */ | |
1056 | } else if (!av_strcasecmp(cmd, "MulticastPort")) { | |
1057 | ffserver_get_arg(arg, sizeof(arg), p); | |
1058 | ffserver_set_int_param(&val, arg, 0, 1, 65535, config, | |
1059 | "Invalid MulticastPort: %s\n", arg); | |
1060 | stream->multicast_port = val; | |
1061 | } else if (!av_strcasecmp(cmd, "MulticastTTL")) { | |
1062 | ffserver_get_arg(arg, sizeof(arg), p); | |
1063 | ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config, | |
1064 | "Invalid MulticastTTL: %s\n", arg); | |
1065 | stream->multicast_ttl = val; | |
1066 | } else if (!av_strcasecmp(cmd, "NoLoop")) { | |
1067 | stream->loop = 0; | |
1068 | } else if (!av_strcasecmp(cmd, "</Stream>")) { | |
1069 | config->stream_use_defaults &= 1; | |
1070 | if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) { | |
1071 | if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE) | |
1072 | config->dummy_actx->codec_id = config->guessed_audio_codec_id; | |
1073 | if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) { | |
1074 | AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id)); | |
1075 | add_codec(stream, audio_enc, config); | |
1076 | } | |
1077 | if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE) | |
1078 | config->dummy_vctx->codec_id = config->guessed_video_codec_id; | |
1079 | if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) { | |
1080 | AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id)); | |
1081 | add_codec(stream, video_enc, config); | |
1082 | } | |
1083 | } | |
1084 | av_dict_free(&config->video_opts); | |
1085 | av_dict_free(&config->audio_opts); | |
1086 | avcodec_free_context(&config->dummy_vctx); | |
1087 | avcodec_free_context(&config->dummy_actx); | |
1088 | *pstream = NULL; | |
1089 | } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) { | |
1090 | ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename), | |
1091 | p); | |
1092 | } else if (!av_strcasecmp(cmd, "UseDefaults")) { | |
1093 | if (config->stream_use_defaults > 1) | |
1094 | WARNING("Multiple UseDefaults/NoDefaults entries.\n"); | |
1095 | config->stream_use_defaults = 3; | |
1096 | } else if (!av_strcasecmp(cmd, "NoDefaults")) { | |
1097 | if (config->stream_use_defaults > 1) | |
1098 | WARNING("Multiple UseDefaults/NoDefaults entries.\n"); | |
1099 | config->stream_use_defaults = 2; | |
1100 | } else { | |
1101 | ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd); | |
1102 | } | |
1103 | return 0; | |
1104 | nomem: | |
1105 | av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n"); | |
1106 | av_dict_free(&config->video_opts); | |
1107 | av_dict_free(&config->audio_opts); | |
1108 | avcodec_free_context(&config->dummy_vctx); | |
1109 | avcodec_free_context(&config->dummy_actx); | |
1110 | return AVERROR(ENOMEM); | |
1111 | } | |
1112 | ||
1113 | static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p, | |
1114 | FFServerStream **predirect) | |
1115 | { | |
1116 | FFServerStream *redirect; | |
1117 | av_assert0(predirect); | |
1118 | redirect = *predirect; | |
1119 | ||
1120 | if (!av_strcasecmp(cmd, "<Redirect")) { | |
1121 | char *q; | |
1122 | redirect = av_mallocz(sizeof(FFServerStream)); | |
1123 | if (!redirect) | |
1124 | return AVERROR(ENOMEM); | |
1125 | ||
1126 | ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p); | |
1127 | q = strrchr(redirect->filename, '>'); | |
1128 | if (*q) | |
1129 | *q = '\0'; | |
1130 | redirect->stream_type = STREAM_TYPE_REDIRECT; | |
1131 | *predirect = redirect; | |
1132 | return 0; | |
1133 | } | |
1134 | av_assert0(redirect); | |
1135 | if (!av_strcasecmp(cmd, "URL")) { | |
1136 | ffserver_get_arg(redirect->feed_filename, | |
1137 | sizeof(redirect->feed_filename), p); | |
1138 | } else if (!av_strcasecmp(cmd, "</Redirect>")) { | |
1139 | if (!redirect->feed_filename[0]) | |
1140 | ERROR("No URL found for <Redirect>\n"); | |
1141 | *predirect = NULL; | |
1142 | } else { | |
1143 | ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd); | |
1144 | } | |
1145 | return 0; | |
1146 | } | |
1147 | ||
1148 | int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config) | |
1149 | { | |
1150 | FILE *f; | |
1151 | char line[1024]; | |
1152 | char cmd[64]; | |
1153 | const char *p; | |
1154 | FFServerStream **last_stream, *stream = NULL, *redirect = NULL; | |
1155 | FFServerStream **last_feed, *feed = NULL; | |
1156 | int ret = 0; | |
1157 | ||
1158 | av_assert0(config); | |
1159 | ||
1160 | config->line_num = 0; | |
1161 | f = fopen(filename, "r"); | |
1162 | if (!f) { | |
1163 | ret = AVERROR(errno); | |
1164 | av_log(NULL, AV_LOG_ERROR, | |
1165 | "Could not open the configuration file '%s'\n", filename); | |
1166 | return ret; | |
1167 | } | |
1168 | ||
1169 | config->first_stream = NULL; | |
1170 | last_stream = &config->first_stream; | |
1171 | config->first_feed = NULL; | |
1172 | last_feed = &config->first_feed; | |
1173 | config->errors = config->warnings = 0; | |
1174 | ||
1175 | for(;;) { | |
1176 | if (fgets(line, sizeof(line), f) == NULL) | |
1177 | break; | |
1178 | config->line_num++; | |
1179 | p = line; | |
1180 | while (av_isspace(*p)) | |
1181 | p++; | |
1182 | if (*p == '\0' || *p == '#') | |
1183 | continue; | |
1184 | ||
1185 | ffserver_get_arg(cmd, sizeof(cmd), &p); | |
1186 | ||
1187 | if (feed || !av_strcasecmp(cmd, "<Feed")) { | |
1188 | int opening = !av_strcasecmp(cmd, "<Feed"); | |
1189 | if (opening && (stream || feed || redirect)) { | |
1190 | ERROR("Already in a tag\n"); | |
1191 | } else { | |
1192 | if ((ret = ffserver_parse_config_feed(config, cmd, &p, &feed)) < 0) | |
1193 | break; | |
1194 | if (opening) { | |
1195 | /* add in stream list */ | |
1196 | *last_stream = feed; | |
1197 | last_stream = &feed->next; | |
1198 | /* add in feed list */ | |
1199 | *last_feed = feed; | |
1200 | last_feed = &feed->next_feed; | |
1201 | } | |
1202 | } | |
1203 | } else if (stream || !av_strcasecmp(cmd, "<Stream")) { | |
1204 | int opening = !av_strcasecmp(cmd, "<Stream"); | |
1205 | if (opening && (stream || feed || redirect)) { | |
1206 | ERROR("Already in a tag\n"); | |
1207 | } else { | |
1208 | if ((ret = ffserver_parse_config_stream(config, cmd, &p, &stream)) < 0) | |
1209 | break; | |
1210 | if (opening) { | |
1211 | /* add in stream list */ | |
1212 | *last_stream = stream; | |
1213 | last_stream = &stream->next; | |
1214 | } | |
1215 | } | |
1216 | } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) { | |
1217 | int opening = !av_strcasecmp(cmd, "<Redirect"); | |
1218 | if (opening && (stream || feed || redirect)) | |
1219 | ERROR("Already in a tag\n"); | |
1220 | else { | |
1221 | if ((ret = ffserver_parse_config_redirect(config, cmd, &p, &redirect)) < 0) | |
1222 | break; | |
1223 | if (opening) { | |
1224 | /* add in stream list */ | |
1225 | *last_stream = redirect; | |
1226 | last_stream = &redirect->next; | |
1227 | } | |
1228 | } | |
1229 | } else { | |
1230 | ffserver_parse_config_global(config, cmd, &p); | |
1231 | } | |
1232 | } | |
1233 | if (stream || feed || redirect) | |
1234 | ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>")); | |
1235 | ||
1236 | fclose(f); | |
1237 | if (ret < 0) | |
1238 | return ret; | |
1239 | if (config->errors) | |
1240 | return AVERROR(EINVAL); | |
1241 | else | |
1242 | return 0; | |
1243 | } | |
1244 | ||
1245 | #undef ERROR | |
1246 | #undef WARNING | |
1247 | ||
1248 | void ffserver_free_child_args(void *argsp) | |
1249 | { | |
1250 | int i; | |
1251 | char **args; | |
1252 | if (!argsp) | |
1253 | return; | |
1254 | args = *(char ***)argsp; | |
1255 | if (!args) | |
1256 | return; | |
1257 | for (i = 0; i < MAX_CHILD_ARGS; i++) | |
1258 | av_free(args[i]); | |
1259 | av_freep(argsp); | |
1260 | } |