ffserver_config.c 50 KB

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