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.

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