You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1182 lines
44KB

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