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.

1134 lines
43KB

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