ffserver_config.c 50 KB

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