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.

1223 lines
46KB

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