ffserver_config.c 50 KB

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