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.

1280 lines
49KB

  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, int line_num);
  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, int line_num)
  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, 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, 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, int line_num)
  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, line_num) < 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, line_num) < 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, int line_num, 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, 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, int line_num, 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, 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, int line_num)
  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, line_num)) < 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) {
  424. report_config_error(config->filename, line_num, AV_LOG_ERROR,
  425. &config->errors, "Option not found: %s\n", opt);
  426. if (!hinted && ctx->codec_id == AV_CODEC_ID_NONE) {
  427. hinted = 1;
  428. report_config_error(config->filename, line_num, AV_LOG_ERROR, NULL,
  429. "If '%s' is a codec private option, then prefix it with codec name, "
  430. "for example '%s:%s %s' or define codec earlier.\n",
  431. opt, avcodec_get_name(guessed_codec_id) ,opt, arg);
  432. }
  433. } else if ((ret = av_opt_set(ctx, option, arg, AV_OPT_SEARCH_CHILDREN)) < 0) {
  434. report_config_error(config->filename, line_num, AV_LOG_ERROR,
  435. &config->errors, "Invalid value for option %s (%s): %s\n", opt,
  436. arg, av_err2str(ret));
  437. } else if ((e = av_dict_get(*dict, option, NULL, 0))) {
  438. if ((o->type == AV_OPT_TYPE_FLAGS) && arg && (arg[0] == '+' || arg[0] == '-'))
  439. return av_dict_set(dict, option, arg, AV_DICT_APPEND);
  440. report_config_error(config->filename, line_num, AV_LOG_ERROR,
  441. &config->errors,
  442. "Redeclaring value of the option %s, previous value: %s\n",
  443. opt, e->value);
  444. } else if (av_dict_set(dict, option, arg, 0) < 0) {
  445. return AVERROR(ENOMEM);
  446. }
  447. return 0;
  448. }
  449. #define ERROR(...) report_config_error(config->filename, line_num, AV_LOG_ERROR, &config->errors, __VA_ARGS__)
  450. #define WARNING(...) report_config_error(config->filename, line_num, AV_LOG_WARNING, &config->warnings, __VA_ARGS__)
  451. static int ffserver_parse_config_global(FFServerConfig *config, const char *cmd,
  452. const char **p, int line_num)
  453. {
  454. int val;
  455. char arg[1024];
  456. if (!av_strcasecmp(cmd, "Port") || !av_strcasecmp(cmd, "HTTPPort")) {
  457. if (!av_strcasecmp(cmd, "Port"))
  458. WARNING("Port option is deprecated, use HTTPPort instead\n");
  459. ffserver_get_arg(arg, sizeof(arg), p);
  460. ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
  461. "Invalid port: %s\n", arg);
  462. if (val < 1024)
  463. WARNING("Trying to use IETF assigned system port: %d\n", val);
  464. config->http_addr.sin_port = htons(val);
  465. } else if (!av_strcasecmp(cmd, "HTTPBindAddress") || !av_strcasecmp(cmd, "BindAddress")) {
  466. if (!av_strcasecmp(cmd, "BindAddress"))
  467. WARNING("BindAddress option is deprecated, use HTTPBindAddress instead\n");
  468. ffserver_get_arg(arg, sizeof(arg), p);
  469. if (resolve_host(&config->http_addr.sin_addr, arg))
  470. ERROR("Invalid host/IP address: %s\n", arg);
  471. } else if (!av_strcasecmp(cmd, "NoDaemon")) {
  472. WARNING("NoDaemon option has no effect, you should remove it\n");
  473. } else if (!av_strcasecmp(cmd, "RTSPPort")) {
  474. ffserver_get_arg(arg, sizeof(arg), p);
  475. ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
  476. "Invalid port: %s\n", arg);
  477. config->rtsp_addr.sin_port = htons(val);
  478. } else if (!av_strcasecmp(cmd, "RTSPBindAddress")) {
  479. ffserver_get_arg(arg, sizeof(arg), p);
  480. if (resolve_host(&config->rtsp_addr.sin_addr, arg))
  481. ERROR("Invalid host/IP address: %s\n", arg);
  482. } else if (!av_strcasecmp(cmd, "MaxHTTPConnections")) {
  483. ffserver_get_arg(arg, sizeof(arg), p);
  484. ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
  485. "Invalid MaxHTTPConnections: %s\n", arg);
  486. config->nb_max_http_connections = val;
  487. if (config->nb_max_connections > config->nb_max_http_connections)
  488. ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
  489. config->nb_max_connections, config->nb_max_http_connections);
  490. } else if (!av_strcasecmp(cmd, "MaxClients")) {
  491. ffserver_get_arg(arg, sizeof(arg), p);
  492. ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
  493. "Invalid MaxClients: %s\n", arg);
  494. config->nb_max_connections = val;
  495. if (config->nb_max_connections > config->nb_max_http_connections)
  496. ERROR("Inconsistent configuration: MaxClients(%d) > MaxHTTPConnections(%d)\n",
  497. config->nb_max_connections, config->nb_max_http_connections);
  498. } else if (!av_strcasecmp(cmd, "MaxBandwidth")) {
  499. int64_t llval;
  500. char *tailp;
  501. ffserver_get_arg(arg, sizeof(arg), p);
  502. errno = 0;
  503. llval = strtoll(arg, &tailp, 10);
  504. if (llval < 10 || llval > 10000000 || tailp[0] || errno)
  505. ERROR("Invalid MaxBandwidth: %s\n", arg);
  506. else
  507. config->max_bandwidth = llval;
  508. } else if (!av_strcasecmp(cmd, "CustomLog")) {
  509. if (!config->debug)
  510. ffserver_get_arg(config->logfilename, sizeof(config->logfilename), p);
  511. } else if (!av_strcasecmp(cmd, "LoadModule")) {
  512. ERROR("Loadable modules are no longer supported\n");
  513. } else
  514. ERROR("Incorrect keyword: '%s'\n", cmd);
  515. return 0;
  516. }
  517. static int ffserver_parse_config_feed(FFServerConfig *config, const char *cmd, const char **p,
  518. int line_num, FFServerStream **pfeed)
  519. {
  520. FFServerStream *feed;
  521. char arg[1024];
  522. av_assert0(pfeed);
  523. feed = *pfeed;
  524. if (!av_strcasecmp(cmd, "<Feed")) {
  525. char *q;
  526. FFServerStream *s;
  527. feed = av_mallocz(sizeof(FFServerStream));
  528. if (!feed)
  529. return AVERROR(ENOMEM);
  530. ffserver_get_arg(feed->filename, sizeof(feed->filename), p);
  531. q = strrchr(feed->filename, '>');
  532. if (*q)
  533. *q = '\0';
  534. for (s = config->first_feed; s; s = s->next) {
  535. if (!strcmp(feed->filename, s->filename))
  536. ERROR("Feed '%s' already registered\n", s->filename);
  537. }
  538. feed->fmt = av_guess_format("ffm", NULL, NULL);
  539. /* default feed file */
  540. snprintf(feed->feed_filename, sizeof(feed->feed_filename),
  541. "/tmp/%s.ffm", feed->filename);
  542. feed->feed_max_size = 5 * 1024 * 1024;
  543. feed->is_feed = 1;
  544. feed->feed = feed; /* self feeding :-) */
  545. *pfeed = feed;
  546. return 0;
  547. }
  548. av_assert0(feed);
  549. if (!av_strcasecmp(cmd, "Launch")) {
  550. int i;
  551. feed->child_argv = av_mallocz(64 * sizeof(char *));
  552. if (!feed->child_argv)
  553. return AVERROR(ENOMEM);
  554. for (i = 0; i < 62; i++) {
  555. ffserver_get_arg(arg, sizeof(arg), p);
  556. if (!arg[0])
  557. break;
  558. feed->child_argv[i] = av_strdup(arg);
  559. if (!feed->child_argv[i])
  560. return AVERROR(ENOMEM);
  561. }
  562. feed->child_argv[i] =
  563. av_asprintf("http://%s:%d/%s",
  564. (config->http_addr.sin_addr.s_addr == INADDR_ANY) ? "127.0.0.1" :
  565. inet_ntoa(config->http_addr.sin_addr), ntohs(config->http_addr.sin_port),
  566. feed->filename);
  567. if (!feed->child_argv[i])
  568. return AVERROR(ENOMEM);
  569. } else if (!av_strcasecmp(cmd, "ACL")) {
  570. ffserver_parse_acl_row(NULL, feed, NULL, *p, config->filename,
  571. line_num);
  572. } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
  573. ffserver_get_arg(feed->feed_filename, sizeof(feed->feed_filename), p);
  574. feed->readonly = !av_strcasecmp(cmd, "ReadOnlyFile");
  575. } else if (!av_strcasecmp(cmd, "Truncate")) {
  576. ffserver_get_arg(arg, sizeof(arg), p);
  577. /* assume Truncate is true in case no argument is specified */
  578. if (!arg[0]) {
  579. feed->truncate = 1;
  580. } else {
  581. WARNING("Truncate N syntax in configuration file is deprecated, "
  582. "use Truncate alone with no arguments\n");
  583. feed->truncate = strtod(arg, NULL);
  584. }
  585. } else if (!av_strcasecmp(cmd, "FileMaxSize")) {
  586. char *p1;
  587. double fsize;
  588. ffserver_get_arg(arg, sizeof(arg), p);
  589. p1 = arg;
  590. fsize = strtod(p1, &p1);
  591. switch(av_toupper(*p1)) {
  592. case 'K':
  593. fsize *= 1024;
  594. break;
  595. case 'M':
  596. fsize *= 1024 * 1024;
  597. break;
  598. case 'G':
  599. fsize *= 1024 * 1024 * 1024;
  600. break;
  601. default:
  602. ERROR("Invalid file size: %s\n", arg);
  603. break;
  604. }
  605. feed->feed_max_size = (int64_t)fsize;
  606. if (feed->feed_max_size < FFM_PACKET_SIZE*4)
  607. ERROR("Feed max file size is too small, must be at least %d\n",
  608. FFM_PACKET_SIZE*4);
  609. } else if (!av_strcasecmp(cmd, "</Feed>")) {
  610. *pfeed = NULL;
  611. } else {
  612. ERROR("Invalid entry '%s' inside <Feed></Feed>\n", cmd);
  613. }
  614. return 0;
  615. }
  616. static void ffserver_apply_stream_config(AVCodecContext *enc, const AVDictionary *conf, AVDictionary **opts)
  617. {
  618. AVDictionaryEntry *e;
  619. /* Return values from ffserver_set_*_param are ignored.
  620. Values are initially parsed and checked before inserting to
  621. AVDictionary. */
  622. //video params
  623. if ((e = av_dict_get(conf, "VideoBitRateRangeMin", NULL, 0)))
  624. ffserver_set_int_param(&enc->rc_min_rate, e->value, 1000, INT_MIN,
  625. INT_MAX, NULL, 0, NULL);
  626. if ((e = av_dict_get(conf, "VideoBitRateRangeMax", NULL, 0)))
  627. ffserver_set_int_param(&enc->rc_max_rate, e->value, 1000, INT_MIN,
  628. INT_MAX, NULL, 0, NULL);
  629. if ((e = av_dict_get(conf, "Debug", NULL, 0)))
  630. ffserver_set_int_param(&enc->debug, e->value, 0, INT_MIN, INT_MAX,
  631. NULL, 0, NULL);
  632. if ((e = av_dict_get(conf, "Strict", NULL, 0)))
  633. ffserver_set_int_param(&enc->strict_std_compliance, e->value, 0,
  634. INT_MIN, INT_MAX, NULL, 0, NULL);
  635. if ((e = av_dict_get(conf, "VideoBufferSize", NULL, 0)))
  636. ffserver_set_int_param(&enc->rc_buffer_size, e->value, 8*1024,
  637. INT_MIN, INT_MAX, NULL, 0, NULL);
  638. if ((e = av_dict_get(conf, "VideoBitRateTolerance", NULL, 0)))
  639. ffserver_set_int_param(&enc->bit_rate_tolerance, e->value, 1000,
  640. INT_MIN, INT_MAX, NULL, 0, NULL);
  641. if ((e = av_dict_get(conf, "VideoBitRate", NULL, 0)))
  642. ffserver_set_int_param(&enc->bit_rate, e->value, 1000, INT_MIN,
  643. INT_MAX, NULL, 0, NULL);
  644. if ((e = av_dict_get(conf, "VideoSizeWidth", NULL, 0)))
  645. ffserver_set_int_param(&enc->width, e->value, 0, INT_MIN, INT_MAX,
  646. NULL, 0, NULL);
  647. if ((e = av_dict_get(conf, "VideoSizeHeight", NULL, 0)))
  648. ffserver_set_int_param(&enc->height, e->value, 0, INT_MIN, INT_MAX,
  649. NULL, 0, NULL);
  650. if ((e = av_dict_get(conf, "PixelFormat", NULL, 0))) {
  651. int val;
  652. ffserver_set_int_param(&val, e->value, 0, INT_MIN, INT_MAX, NULL, 0,
  653. NULL);
  654. enc->pix_fmt = val;
  655. }
  656. if ((e = av_dict_get(conf, "VideoGopSize", NULL, 0)))
  657. ffserver_set_int_param(&enc->gop_size, e->value, 0, INT_MIN, INT_MAX,
  658. NULL, 0, NULL);
  659. if ((e = av_dict_get(conf, "VideoFrameRateNum", NULL, 0)))
  660. ffserver_set_int_param(&enc->time_base.num, e->value, 0, INT_MIN,
  661. INT_MAX, NULL, 0, NULL);
  662. if ((e = av_dict_get(conf, "VideoFrameRateDen", NULL, 0)))
  663. ffserver_set_int_param(&enc->time_base.den, e->value, 0, INT_MIN,
  664. INT_MAX, NULL, 0, NULL);
  665. if ((e = av_dict_get(conf, "VideoQDiff", NULL, 0)))
  666. ffserver_set_int_param(&enc->max_qdiff, e->value, 0, INT_MIN, INT_MAX,
  667. NULL, 0, NULL);
  668. if ((e = av_dict_get(conf, "VideoQMax", NULL, 0)))
  669. ffserver_set_int_param(&enc->qmax, e->value, 0, INT_MIN, INT_MAX, NULL,
  670. 0, NULL);
  671. if ((e = av_dict_get(conf, "VideoQMin", NULL, 0)))
  672. ffserver_set_int_param(&enc->qmin, e->value, 0, INT_MIN, INT_MAX, NULL,
  673. 0, NULL);
  674. if ((e = av_dict_get(conf, "LumiMask", NULL, 0)))
  675. ffserver_set_float_param(&enc->lumi_masking, e->value, 0, -FLT_MAX,
  676. FLT_MAX, NULL, 0, NULL);
  677. if ((e = av_dict_get(conf, "DarkMask", NULL, 0)))
  678. ffserver_set_float_param(&enc->dark_masking, e->value, 0, -FLT_MAX,
  679. FLT_MAX, NULL, 0, NULL);
  680. if (av_dict_get(conf, "BitExact", NULL, 0))
  681. enc->flags |= CODEC_FLAG_BITEXACT;
  682. if (av_dict_get(conf, "DctFastint", NULL, 0))
  683. enc->dct_algo = FF_DCT_FASTINT;
  684. if (av_dict_get(conf, "IdctSimple", NULL, 0))
  685. enc->idct_algo = FF_IDCT_SIMPLE;
  686. if (av_dict_get(conf, "VideoHighQuality", NULL, 0))
  687. enc->mb_decision = FF_MB_DECISION_BITS;
  688. if ((e = av_dict_get(conf, "VideoTag", NULL, 0)))
  689. enc->codec_tag = MKTAG(e->value[0], e->value[1], e->value[2], e->value[3]);
  690. if ((e = av_dict_get(conf, "Qscale", NULL, 0))) {
  691. enc->flags |= CODEC_FLAG_QSCALE;
  692. ffserver_set_int_param(&enc->global_quality, e->value, FF_QP2LAMBDA,
  693. INT_MIN, INT_MAX, NULL, 0, NULL);
  694. }
  695. if (av_dict_get(conf, "Video4MotionVector", NULL, 0)) {
  696. enc->mb_decision = FF_MB_DECISION_BITS; //FIXME remove
  697. enc->flags |= CODEC_FLAG_4MV;
  698. }
  699. //audio params
  700. if ((e = av_dict_get(conf, "AudioChannels", NULL, 0)))
  701. ffserver_set_int_param(&enc->channels, e->value, 0, INT_MIN, INT_MAX,
  702. NULL, 0, NULL);
  703. if ((e = av_dict_get(conf, "AudioSampleRate", NULL, 0)))
  704. ffserver_set_int_param(&enc->sample_rate, e->value, 0, INT_MIN,
  705. INT_MAX, NULL, 0, NULL);
  706. if ((e = av_dict_get(conf, "AudioBitRate", NULL, 0)))
  707. ffserver_set_int_param(&enc->bit_rate, e->value, 0, INT_MIN, INT_MAX,
  708. NULL, 0, NULL);
  709. av_opt_set_dict2(enc->priv_data, opts, AV_OPT_SEARCH_CHILDREN);
  710. av_opt_set_dict2(enc, opts, AV_OPT_SEARCH_CHILDREN);
  711. if (av_dict_count(*opts))
  712. av_log(NULL, AV_LOG_ERROR, "Something went wrong, %d options not set!!!\n", av_dict_count(*opts));
  713. }
  714. static int ffserver_parse_config_stream(FFServerConfig *config, const char *cmd, const char **p,
  715. int line_num, FFServerStream **pstream)
  716. {
  717. char arg[1024], arg2[1024];
  718. FFServerStream *stream;
  719. int val;
  720. av_assert0(pstream);
  721. stream = *pstream;
  722. if (!av_strcasecmp(cmd, "<Stream")) {
  723. char *q;
  724. FFServerStream *s;
  725. stream = av_mallocz(sizeof(FFServerStream));
  726. if (!stream)
  727. return AVERROR(ENOMEM);
  728. config->dummy_actx = avcodec_alloc_context3(NULL);
  729. config->dummy_vctx = avcodec_alloc_context3(NULL);
  730. if (!config->dummy_vctx || !config->dummy_actx) {
  731. av_free(stream);
  732. avcodec_free_context(&config->dummy_vctx);
  733. avcodec_free_context(&config->dummy_actx);
  734. return AVERROR(ENOMEM);
  735. }
  736. config->dummy_actx->codec_type = AVMEDIA_TYPE_AUDIO;
  737. config->dummy_vctx->codec_type = AVMEDIA_TYPE_VIDEO;
  738. ffserver_get_arg(stream->filename, sizeof(stream->filename), p);
  739. q = strrchr(stream->filename, '>');
  740. if (q)
  741. *q = '\0';
  742. for (s = config->first_stream; s; s = s->next) {
  743. if (!strcmp(stream->filename, s->filename))
  744. ERROR("Stream '%s' already registered\n", s->filename);
  745. }
  746. stream->fmt = ffserver_guess_format(NULL, stream->filename, NULL);
  747. if (stream->fmt) {
  748. config->guessed_audio_codec_id = stream->fmt->audio_codec;
  749. config->guessed_video_codec_id = stream->fmt->video_codec;
  750. } else {
  751. config->guessed_audio_codec_id = AV_CODEC_ID_NONE;
  752. config->guessed_video_codec_id = AV_CODEC_ID_NONE;
  753. }
  754. *pstream = stream;
  755. return 0;
  756. }
  757. av_assert0(stream);
  758. if (!av_strcasecmp(cmd, "Feed")) {
  759. FFServerStream *sfeed;
  760. ffserver_get_arg(arg, sizeof(arg), p);
  761. sfeed = config->first_feed;
  762. while (sfeed) {
  763. if (!strcmp(sfeed->filename, arg))
  764. break;
  765. sfeed = sfeed->next_feed;
  766. }
  767. if (!sfeed)
  768. ERROR("Feed with name '%s' for stream '%s' is not defined\n", arg,
  769. stream->filename);
  770. else
  771. stream->feed = sfeed;
  772. } else if (!av_strcasecmp(cmd, "Format")) {
  773. ffserver_get_arg(arg, sizeof(arg), p);
  774. if (!strcmp(arg, "status")) {
  775. stream->stream_type = STREAM_TYPE_STATUS;
  776. stream->fmt = NULL;
  777. } else {
  778. stream->stream_type = STREAM_TYPE_LIVE;
  779. /* JPEG cannot be used here, so use single frame MJPEG */
  780. if (!strcmp(arg, "jpeg"))
  781. strcpy(arg, "mjpeg");
  782. stream->fmt = ffserver_guess_format(arg, NULL, NULL);
  783. if (!stream->fmt)
  784. ERROR("Unknown Format: %s\n", arg);
  785. }
  786. if (stream->fmt) {
  787. config->guessed_audio_codec_id = stream->fmt->audio_codec;
  788. config->guessed_video_codec_id = stream->fmt->video_codec;
  789. }
  790. } else if (!av_strcasecmp(cmd, "InputFormat")) {
  791. ffserver_get_arg(arg, sizeof(arg), p);
  792. stream->ifmt = av_find_input_format(arg);
  793. if (!stream->ifmt)
  794. ERROR("Unknown input format: %s\n", arg);
  795. } else if (!av_strcasecmp(cmd, "FaviconURL")) {
  796. if (stream->stream_type == STREAM_TYPE_STATUS)
  797. ffserver_get_arg(stream->feed_filename,
  798. sizeof(stream->feed_filename), p);
  799. else
  800. ERROR("FaviconURL only permitted for status streams\n");
  801. } else if (!av_strcasecmp(cmd, "Author") ||
  802. !av_strcasecmp(cmd, "Comment") ||
  803. !av_strcasecmp(cmd, "Copyright") ||
  804. !av_strcasecmp(cmd, "Title")) {
  805. char key[32];
  806. int i;
  807. ffserver_get_arg(arg, sizeof(arg), p);
  808. for (i = 0; i < strlen(cmd); i++)
  809. key[i] = av_tolower(cmd[i]);
  810. key[i] = 0;
  811. WARNING("'%s' option in configuration file is deprecated, "
  812. "use 'Metadata %s VALUE' instead\n", cmd, key);
  813. if (av_dict_set(&stream->metadata, key, arg, 0) < 0)
  814. goto nomem;
  815. } else if (!av_strcasecmp(cmd, "Metadata")) {
  816. ffserver_get_arg(arg, sizeof(arg), p);
  817. ffserver_get_arg(arg2, sizeof(arg2), p);
  818. if (av_dict_set(&stream->metadata, arg, arg2, 0) < 0)
  819. goto nomem;
  820. } else if (!av_strcasecmp(cmd, "Preroll")) {
  821. ffserver_get_arg(arg, sizeof(arg), p);
  822. stream->prebuffer = atof(arg) * 1000;
  823. } else if (!av_strcasecmp(cmd, "StartSendOnKey")) {
  824. stream->send_on_key = 1;
  825. } else if (!av_strcasecmp(cmd, "AudioCodec")) {
  826. ffserver_get_arg(arg, sizeof(arg), p);
  827. ffserver_set_codec(config->dummy_actx, arg, config, line_num);
  828. } else if (!av_strcasecmp(cmd, "VideoCodec")) {
  829. ffserver_get_arg(arg, sizeof(arg), p);
  830. ffserver_set_codec(config->dummy_vctx, arg, config, line_num);
  831. } else if (!av_strcasecmp(cmd, "MaxTime")) {
  832. ffserver_get_arg(arg, sizeof(arg), p);
  833. stream->max_time = atof(arg) * 1000;
  834. } else if (!av_strcasecmp(cmd, "AudioBitRate")) {
  835. float f;
  836. ffserver_get_arg(arg, sizeof(arg), p);
  837. ffserver_set_float_param(&f, arg, 1000, 0, FLT_MAX, config, line_num,
  838. "Invalid %s: %s\n", cmd, arg);
  839. if (av_dict_set_int(&config->audio_conf, cmd, lrintf(f), 0) < 0)
  840. goto nomem;
  841. } else if (!av_strcasecmp(cmd, "AudioChannels")) {
  842. ffserver_get_arg(arg, sizeof(arg), p);
  843. ffserver_set_int_param(NULL, arg, 0, 1, 8, config, line_num,
  844. "Invalid %s: %s, valid range is 1-8.", cmd, arg);
  845. if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
  846. goto nomem;
  847. } else if (!av_strcasecmp(cmd, "AudioSampleRate")) {
  848. ffserver_get_arg(arg, sizeof(arg), p);
  849. ffserver_set_int_param(NULL, arg, 0, 0, INT_MAX, config, line_num,
  850. "Invalid %s: %s", cmd, arg);
  851. if (av_dict_set(&config->audio_conf, cmd, arg, 0) < 0)
  852. goto nomem;
  853. } else if (!av_strcasecmp(cmd, "VideoBitRateRange")) {
  854. int minrate, maxrate;
  855. ffserver_get_arg(arg, sizeof(arg), p);
  856. if (sscanf(arg, "%d-%d", &minrate, &maxrate) == 2) {
  857. if (av_dict_set_int(&config->video_conf, "VideoBitRateRangeMin", minrate, 0) < 0 ||
  858. av_dict_set_int(&config->video_conf, "VideoBitRateRangeMax", maxrate, 0) < 0)
  859. goto nomem;
  860. } else
  861. ERROR("Incorrect format for VideoBitRateRange -- should be "
  862. "<min>-<max>: %s\n", arg);
  863. } else if (!av_strcasecmp(cmd, "Debug")) {
  864. ffserver_get_arg(arg, sizeof(arg), p);
  865. ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num,
  866. "Invalid %s: %s", cmd, arg);
  867. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  868. goto nomem;
  869. } else if (!av_strcasecmp(cmd, "Strict")) {
  870. ffserver_get_arg(arg, sizeof(arg), p);
  871. ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num,
  872. "Invalid %s: %s", cmd, arg);
  873. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  874. goto nomem;
  875. } else if (!av_strcasecmp(cmd, "VideoBufferSize")) {
  876. ffserver_get_arg(arg, sizeof(arg), p);
  877. ffserver_set_int_param(NULL, arg, 8*1024, 0, INT_MAX, config, line_num,
  878. "Invalid %s: %s", cmd, arg);
  879. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  880. goto nomem;
  881. } else if (!av_strcasecmp(cmd, "VideoBitRateTolerance")) {
  882. ffserver_get_arg(arg, sizeof(arg), p);
  883. ffserver_set_int_param(NULL, arg, 1000, INT_MIN, INT_MAX, config,
  884. line_num, "Invalid %s: %s", cmd, arg);
  885. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  886. goto nomem;
  887. } else if (!av_strcasecmp(cmd, "VideoBitRate")) {
  888. ffserver_get_arg(arg, sizeof(arg), p);
  889. ffserver_set_int_param(NULL, arg, 1000, 0, INT_MAX, config, line_num,
  890. "Invalid %s: %s", cmd, arg);
  891. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  892. goto nomem;
  893. } else if (!av_strcasecmp(cmd, "VideoSize")) {
  894. int ret, w, h;
  895. ffserver_get_arg(arg, sizeof(arg), p);
  896. ret = av_parse_video_size(&w, &h, arg);
  897. if (ret < 0)
  898. ERROR("Invalid video size '%s'\n", arg);
  899. else if ((w % 2) || (h % 2))
  900. WARNING("Image size is not a multiple of 2\n");
  901. if (av_dict_set_int(&config->video_conf, "VideoSizeWidth", w, 0) < 0 ||
  902. av_dict_set_int(&config->video_conf, "VideoSizeHeight", h, 0) < 0)
  903. goto nomem;
  904. } else if (!av_strcasecmp(cmd, "VideoFrameRate")) {
  905. AVRational frame_rate;
  906. ffserver_get_arg(arg, sizeof(arg), p);
  907. if (av_parse_video_rate(&frame_rate, arg) < 0) {
  908. ERROR("Incorrect frame rate: %s\n", arg);
  909. } else {
  910. if (av_dict_set_int(&config->video_conf, "VideoFrameRateNum", frame_rate.num, 0) < 0 ||
  911. av_dict_set_int(&config->video_conf, "VideoFrameRateDen", frame_rate.den, 0) < 0)
  912. goto nomem;
  913. }
  914. } else if (!av_strcasecmp(cmd, "PixelFormat")) {
  915. enum AVPixelFormat pix_fmt;
  916. ffserver_get_arg(arg, sizeof(arg), p);
  917. pix_fmt = av_get_pix_fmt(arg);
  918. if (pix_fmt == AV_PIX_FMT_NONE)
  919. ERROR("Unknown pixel format: %s\n", arg);
  920. if (av_dict_set_int(&config->video_conf, cmd, pix_fmt, 0) < 0)
  921. goto nomem;
  922. } else if (!av_strcasecmp(cmd, "VideoGopSize")) {
  923. ffserver_get_arg(arg, sizeof(arg), p);
  924. ffserver_set_int_param(NULL, arg, 0, INT_MIN, INT_MAX, config, line_num,
  925. "Invalid %s: %s", cmd, arg);
  926. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  927. goto nomem;
  928. } else if (!av_strcasecmp(cmd, "VideoIntraOnly")) {
  929. if (av_dict_set(&config->video_conf, "VideoGopSize", "1", 0) < 0)
  930. goto nomem;
  931. } else if (!av_strcasecmp(cmd, "VideoHighQuality")) {
  932. if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
  933. goto nomem;
  934. } else if (!av_strcasecmp(cmd, "Video4MotionVector")) {
  935. if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
  936. goto nomem;
  937. } else if (!av_strcasecmp(cmd, "AVOptionVideo") ||
  938. !av_strcasecmp(cmd, "AVOptionAudio")) {
  939. int ret;
  940. ffserver_get_arg(arg, sizeof(arg), p);
  941. ffserver_get_arg(arg2, sizeof(arg2), p);
  942. if (!av_strcasecmp(cmd, "AVOptionVideo"))
  943. ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_VIDEO_PARAM, config, line_num);
  944. else
  945. ret = ffserver_save_avoption(arg, arg2, AV_OPT_FLAG_AUDIO_PARAM, config, line_num);
  946. if (ret < 0)
  947. goto nomem;
  948. } else if (!av_strcasecmp(cmd, "AVPresetVideo") ||
  949. !av_strcasecmp(cmd, "AVPresetAudio")) {
  950. ffserver_get_arg(arg, sizeof(arg), p);
  951. if (!av_strcasecmp(cmd, "AVPresetVideo"))
  952. ffserver_opt_preset(arg, AV_OPT_FLAG_VIDEO_PARAM, config, line_num);
  953. else
  954. ffserver_opt_preset(arg, AV_OPT_FLAG_AUDIO_PARAM, config, line_num);
  955. } else if (!av_strcasecmp(cmd, "VideoTag")) {
  956. ffserver_get_arg(arg, sizeof(arg), p);
  957. if (strlen(arg) == 4) {
  958. if (av_dict_set(&config->video_conf, "VideoTag", arg, 0) < 0)
  959. goto nomem;
  960. }
  961. } else if (!av_strcasecmp(cmd, "BitExact")) {
  962. if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
  963. goto nomem;
  964. } else if (!av_strcasecmp(cmd, "DctFastint")) {
  965. if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
  966. goto nomem;
  967. } else if (!av_strcasecmp(cmd, "IdctSimple")) {
  968. if (av_dict_set(&config->video_conf, cmd, "", 0) < 0)
  969. goto nomem;
  970. } else if (!av_strcasecmp(cmd, "Qscale")) {
  971. ffserver_get_arg(arg, sizeof(arg), p);
  972. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  973. goto nomem;
  974. } else if (!av_strcasecmp(cmd, "VideoQDiff")) {
  975. ffserver_get_arg(arg, sizeof(arg), p);
  976. ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num,
  977. "%s out of range\n", cmd);
  978. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  979. goto nomem;
  980. } else if (!av_strcasecmp(cmd, "VideoQMax")) {
  981. ffserver_get_arg(arg, sizeof(arg), p);
  982. ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num,
  983. "%s out of range\n", cmd);
  984. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  985. goto nomem;
  986. } else if (!av_strcasecmp(cmd, "VideoQMin")) {
  987. ffserver_get_arg(arg, sizeof(arg), p);
  988. ffserver_set_int_param(NULL, arg, 0, 1, 31, config, line_num,
  989. "%s out of range\n", cmd);
  990. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  991. goto nomem;
  992. } else if (!av_strcasecmp(cmd, "LumiMask")) {
  993. ffserver_get_arg(arg, sizeof(arg), p);
  994. ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config,
  995. line_num, "Invalid %s: %s", cmd, arg);
  996. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  997. goto nomem;
  998. } else if (!av_strcasecmp(cmd, "DarkMask")) {
  999. ffserver_get_arg(arg, sizeof(arg), p);
  1000. ffserver_set_float_param(NULL, arg, 0, -FLT_MAX, FLT_MAX, config,
  1001. line_num, "Invalid %s: %s", cmd, arg);
  1002. if (av_dict_set(&config->video_conf, cmd, arg, 0) < 0)
  1003. goto nomem;
  1004. } else if (!av_strcasecmp(cmd, "NoVideo")) {
  1005. config->no_video = 1;
  1006. } else if (!av_strcasecmp(cmd, "NoAudio")) {
  1007. config->no_audio = 1;
  1008. } else if (!av_strcasecmp(cmd, "ACL")) {
  1009. ffserver_parse_acl_row(stream, NULL, NULL, *p, config->filename,
  1010. line_num);
  1011. } else if (!av_strcasecmp(cmd, "DynamicACL")) {
  1012. ffserver_get_arg(stream->dynamic_acl, sizeof(stream->dynamic_acl), p);
  1013. } else if (!av_strcasecmp(cmd, "RTSPOption")) {
  1014. ffserver_get_arg(arg, sizeof(arg), p);
  1015. av_freep(&stream->rtsp_option);
  1016. stream->rtsp_option = av_strdup(arg);
  1017. } else if (!av_strcasecmp(cmd, "MulticastAddress")) {
  1018. ffserver_get_arg(arg, sizeof(arg), p);
  1019. if (resolve_host(&stream->multicast_ip, arg))
  1020. ERROR("Invalid host/IP address: %s\n", arg);
  1021. stream->is_multicast = 1;
  1022. stream->loop = 1; /* default is looping */
  1023. } else if (!av_strcasecmp(cmd, "MulticastPort")) {
  1024. ffserver_get_arg(arg, sizeof(arg), p);
  1025. ffserver_set_int_param(&val, arg, 0, 1, 65535, config, line_num,
  1026. "Invalid MulticastPort: %s\n", arg);
  1027. stream->multicast_port = val;
  1028. } else if (!av_strcasecmp(cmd, "MulticastTTL")) {
  1029. ffserver_get_arg(arg, sizeof(arg), p);
  1030. ffserver_set_int_param(&val, arg, 0, INT_MIN, INT_MAX, config,
  1031. line_num, "Invalid MulticastTTL: %s\n", arg);
  1032. stream->multicast_ttl = val;
  1033. } else if (!av_strcasecmp(cmd, "NoLoop")) {
  1034. stream->loop = 0;
  1035. } else if (!av_strcasecmp(cmd, "</Stream>")) {
  1036. if (stream->feed && stream->fmt && strcmp(stream->fmt->name, "ffm")) {
  1037. if (config->dummy_actx->codec_id == AV_CODEC_ID_NONE)
  1038. config->dummy_actx->codec_id = config->guessed_audio_codec_id;
  1039. if (!config->no_audio && config->dummy_actx->codec_id != AV_CODEC_ID_NONE) {
  1040. AVCodecContext *audio_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_actx->codec_id));
  1041. ffserver_apply_stream_config(audio_enc, config->audio_conf, &config->audio_opts);
  1042. add_codec(stream, audio_enc);
  1043. }
  1044. if (config->dummy_vctx->codec_id == AV_CODEC_ID_NONE)
  1045. config->dummy_vctx->codec_id = config->guessed_video_codec_id;
  1046. if (!config->no_video && config->dummy_vctx->codec_id != AV_CODEC_ID_NONE) {
  1047. AVCodecContext *video_enc = avcodec_alloc_context3(avcodec_find_encoder(config->dummy_vctx->codec_id));
  1048. ffserver_apply_stream_config(video_enc, config->video_conf, &config->video_opts);
  1049. add_codec(stream, video_enc);
  1050. }
  1051. }
  1052. av_dict_free(&config->video_opts);
  1053. av_dict_free(&config->video_conf);
  1054. av_dict_free(&config->audio_opts);
  1055. av_dict_free(&config->audio_conf);
  1056. avcodec_free_context(&config->dummy_vctx);
  1057. avcodec_free_context(&config->dummy_actx);
  1058. *pstream = NULL;
  1059. } else if (!av_strcasecmp(cmd, "File") || !av_strcasecmp(cmd, "ReadOnlyFile")) {
  1060. ffserver_get_arg(stream->feed_filename, sizeof(stream->feed_filename),
  1061. p);
  1062. } else {
  1063. ERROR("Invalid entry '%s' inside <Stream></Stream>\n", cmd);
  1064. }
  1065. return 0;
  1066. nomem:
  1067. av_log(NULL, AV_LOG_ERROR, "Out of memory. Aborting.\n");
  1068. av_dict_free(&config->video_opts);
  1069. av_dict_free(&config->video_conf);
  1070. av_dict_free(&config->audio_opts);
  1071. av_dict_free(&config->audio_conf);
  1072. avcodec_free_context(&config->dummy_vctx);
  1073. avcodec_free_context(&config->dummy_actx);
  1074. return AVERROR(ENOMEM);
  1075. }
  1076. static int ffserver_parse_config_redirect(FFServerConfig *config, const char *cmd, const char **p,
  1077. int line_num, FFServerStream **predirect)
  1078. {
  1079. FFServerStream *redirect;
  1080. av_assert0(predirect);
  1081. redirect = *predirect;
  1082. if (!av_strcasecmp(cmd, "<Redirect")) {
  1083. char *q;
  1084. redirect = av_mallocz(sizeof(FFServerStream));
  1085. if (!redirect)
  1086. return AVERROR(ENOMEM);
  1087. ffserver_get_arg(redirect->filename, sizeof(redirect->filename), p);
  1088. q = strrchr(redirect->filename, '>');
  1089. if (*q)
  1090. *q = '\0';
  1091. redirect->stream_type = STREAM_TYPE_REDIRECT;
  1092. *predirect = redirect;
  1093. return 0;
  1094. }
  1095. av_assert0(redirect);
  1096. if (!av_strcasecmp(cmd, "URL")) {
  1097. ffserver_get_arg(redirect->feed_filename,
  1098. sizeof(redirect->feed_filename), p);
  1099. } else if (!av_strcasecmp(cmd, "</Redirect>")) {
  1100. if (!redirect->feed_filename[0])
  1101. ERROR("No URL found for <Redirect>\n");
  1102. *predirect = NULL;
  1103. } else {
  1104. ERROR("Invalid entry '%s' inside <Redirect></Redirect>\n", cmd);
  1105. }
  1106. return 0;
  1107. }
  1108. int ffserver_parse_ffconfig(const char *filename, FFServerConfig *config)
  1109. {
  1110. FILE *f;
  1111. char line[1024];
  1112. char cmd[64];
  1113. const char *p;
  1114. int line_num = 0;
  1115. FFServerStream **last_stream, *stream = NULL, *redirect = NULL;
  1116. FFServerStream **last_feed, *feed = NULL;
  1117. int ret = 0;
  1118. av_assert0(config);
  1119. f = fopen(filename, "r");
  1120. if (!f) {
  1121. ret = AVERROR(errno);
  1122. av_log(NULL, AV_LOG_ERROR,
  1123. "Could not open the configuration file '%s'\n", filename);
  1124. return ret;
  1125. }
  1126. config->first_stream = NULL;
  1127. last_stream = &config->first_stream;
  1128. config->first_feed = NULL;
  1129. last_feed = &config->first_feed;
  1130. config->errors = config->warnings = 0;
  1131. for(;;) {
  1132. if (fgets(line, sizeof(line), f) == NULL)
  1133. break;
  1134. line_num++;
  1135. p = line;
  1136. while (av_isspace(*p))
  1137. p++;
  1138. if (*p == '\0' || *p == '#')
  1139. continue;
  1140. ffserver_get_arg(cmd, sizeof(cmd), &p);
  1141. if (feed || !av_strcasecmp(cmd, "<Feed")) {
  1142. int opening = !av_strcasecmp(cmd, "<Feed");
  1143. if (opening && (stream || feed || redirect)) {
  1144. ERROR("Already in a tag\n");
  1145. } else {
  1146. if ((ret = ffserver_parse_config_feed(config, cmd, &p, line_num, &feed)) < 0)
  1147. break;
  1148. if (opening) {
  1149. /* add in stream list */
  1150. *last_stream = feed;
  1151. last_stream = &feed->next;
  1152. /* add in feed list */
  1153. *last_feed = feed;
  1154. last_feed = &feed->next_feed;
  1155. }
  1156. }
  1157. } else if (stream || !av_strcasecmp(cmd, "<Stream")) {
  1158. int opening = !av_strcasecmp(cmd, "<Stream");
  1159. if (opening && (stream || feed || redirect)) {
  1160. ERROR("Already in a tag\n");
  1161. } else {
  1162. if ((ret = ffserver_parse_config_stream(config, cmd, &p, line_num, &stream)) < 0)
  1163. break;
  1164. if (opening) {
  1165. /* add in stream list */
  1166. *last_stream = stream;
  1167. last_stream = &stream->next;
  1168. }
  1169. }
  1170. } else if (redirect || !av_strcasecmp(cmd, "<Redirect")) {
  1171. int opening = !av_strcasecmp(cmd, "<Redirect");
  1172. if (opening && (stream || feed || redirect))
  1173. ERROR("Already in a tag\n");
  1174. else {
  1175. if ((ret = ffserver_parse_config_redirect(config, cmd, &p, line_num, &redirect)) < 0)
  1176. break;
  1177. if (opening) {
  1178. /* add in stream list */
  1179. *last_stream = redirect;
  1180. last_stream = &redirect->next;
  1181. }
  1182. }
  1183. } else {
  1184. ffserver_parse_config_global(config, cmd, &p, line_num);
  1185. }
  1186. }
  1187. if (stream || feed || redirect)
  1188. ERROR("Not closed tag %s\n", stream ? "<Stream>" : (feed ? "<Feed>" : "<Redirect>"));
  1189. fclose(f);
  1190. if (ret < 0)
  1191. return ret;
  1192. if (config->errors)
  1193. return AVERROR(EINVAL);
  1194. else
  1195. return 0;
  1196. }
  1197. #undef ERROR
  1198. #undef WARNING