rtsp.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769
  1. /*
  2. * RTSP/SDP client
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /* needed by inet_aton() */
  22. #define _SVID_SOURCE
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "avformat.h"
  26. #include <sys/time.h>
  27. #if HAVE_SYS_SELECT_H
  28. #include <sys/select.h>
  29. #endif
  30. #include <strings.h>
  31. #include "network.h"
  32. #include "rtsp.h"
  33. #include "rtpdec.h"
  34. #include "rdt.h"
  35. #include "rtp_asf.h"
  36. //#define DEBUG
  37. //#define DEBUG_RTP_TCP
  38. static int rtsp_read_play(AVFormatContext *s);
  39. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  40. int rtsp_default_protocols = (1 << RTSP_LOWER_TRANSPORT_UDP);
  41. #endif
  42. static int rtsp_probe(AVProbeData *p)
  43. {
  44. if (av_strstart(p->filename, "rtsp:", NULL))
  45. return AVPROBE_SCORE_MAX;
  46. return 0;
  47. }
  48. #define SPACE_CHARS " \t\r\n"
  49. /* we use memchr() instead of strchr() here because strchr() will return
  50. * the terminating '\0' of SPACE_CHARS instead of NULL if c is '\0'. */
  51. #define redir_isspace(c) memchr(SPACE_CHARS, c, 4)
  52. static void skip_spaces(const char **pp)
  53. {
  54. const char *p;
  55. p = *pp;
  56. while (redir_isspace(*p))
  57. p++;
  58. *pp = p;
  59. }
  60. static void get_word_until_chars(char *buf, int buf_size,
  61. const char *sep, const char **pp)
  62. {
  63. const char *p;
  64. char *q;
  65. p = *pp;
  66. skip_spaces(&p);
  67. q = buf;
  68. while (!strchr(sep, *p) && *p != '\0') {
  69. if ((q - buf) < buf_size - 1)
  70. *q++ = *p;
  71. p++;
  72. }
  73. if (buf_size > 0)
  74. *q = '\0';
  75. *pp = p;
  76. }
  77. static void get_word_sep(char *buf, int buf_size, const char *sep,
  78. const char **pp)
  79. {
  80. if (**pp == '/') (*pp)++;
  81. get_word_until_chars(buf, buf_size, sep, pp);
  82. }
  83. static void get_word(char *buf, int buf_size, const char **pp)
  84. {
  85. get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
  86. }
  87. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
  88. params>] */
  89. static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st, int payload_type, const char *p)
  90. {
  91. char buf[256];
  92. int i;
  93. AVCodec *c;
  94. const char *c_name;
  95. /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
  96. see if we can handle this kind of payload */
  97. get_word_sep(buf, sizeof(buf), "/", &p);
  98. if (payload_type >= RTP_PT_PRIVATE) {
  99. RTPDynamicProtocolHandler *handler= RTPFirstDynamicPayloadHandler;
  100. while(handler) {
  101. if (!strcasecmp(buf, handler->enc_name) && (codec->codec_type == handler->codec_type)) {
  102. codec->codec_id = handler->codec_id;
  103. rtsp_st->dynamic_handler= handler;
  104. if(handler->open) {
  105. rtsp_st->dynamic_protocol_context= handler->open();
  106. }
  107. break;
  108. }
  109. handler= handler->next;
  110. }
  111. } else {
  112. /* We are in a standard case ( from http://www.iana.org/assignments/rtp-parameters) */
  113. /* search into AVRtpPayloadTypes[] */
  114. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  115. }
  116. c = avcodec_find_decoder(codec->codec_id);
  117. if (c && c->name)
  118. c_name = c->name;
  119. else
  120. c_name = (char *)NULL;
  121. if (c_name) {
  122. get_word_sep(buf, sizeof(buf), "/", &p);
  123. i = atoi(buf);
  124. switch (codec->codec_type) {
  125. case CODEC_TYPE_AUDIO:
  126. av_log(codec, AV_LOG_DEBUG, " audio codec set to : %s\n", c_name);
  127. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  128. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  129. if (i > 0) {
  130. codec->sample_rate = i;
  131. get_word_sep(buf, sizeof(buf), "/", &p);
  132. i = atoi(buf);
  133. if (i > 0)
  134. codec->channels = i;
  135. // TODO: there is a bug here; if it is a mono stream, and less than 22000Hz, faad upconverts to stereo and twice the
  136. // frequency. No problem, but the sample rate is being set here by the sdp line. Upcoming patch forthcoming. (rdm)
  137. }
  138. av_log(codec, AV_LOG_DEBUG, " audio samplerate set to : %i\n", codec->sample_rate);
  139. av_log(codec, AV_LOG_DEBUG, " audio channels set to : %i\n", codec->channels);
  140. break;
  141. case CODEC_TYPE_VIDEO:
  142. av_log(codec, AV_LOG_DEBUG, " video codec set to : %s\n", c_name);
  143. break;
  144. default:
  145. break;
  146. }
  147. return 0;
  148. }
  149. return -1;
  150. }
  151. /* return the length and optionnaly the data */
  152. static int hex_to_data(uint8_t *data, const char *p)
  153. {
  154. int c, len, v;
  155. len = 0;
  156. v = 1;
  157. for(;;) {
  158. skip_spaces(&p);
  159. if (*p == '\0')
  160. break;
  161. c = toupper((unsigned char)*p++);
  162. if (c >= '0' && c <= '9')
  163. c = c - '0';
  164. else if (c >= 'A' && c <= 'F')
  165. c = c - 'A' + 10;
  166. else
  167. break;
  168. v = (v << 4) | c;
  169. if (v & 0x100) {
  170. if (data)
  171. data[len] = v;
  172. len++;
  173. v = 1;
  174. }
  175. }
  176. return len;
  177. }
  178. static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value)
  179. {
  180. switch (codec->codec_id) {
  181. case CODEC_ID_MPEG4:
  182. case CODEC_ID_AAC:
  183. if (!strcmp(attr, "config")) {
  184. /* decode the hexa encoded parameter */
  185. int len = hex_to_data(NULL, value);
  186. if (codec->extradata)
  187. av_free(codec->extradata);
  188. codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  189. if (!codec->extradata)
  190. return;
  191. codec->extradata_size = len;
  192. hex_to_data(codec->extradata, value);
  193. }
  194. break;
  195. default:
  196. break;
  197. }
  198. return;
  199. }
  200. typedef struct {
  201. const char *str;
  202. uint16_t type;
  203. uint32_t offset;
  204. } AttrNameMap;
  205. /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
  206. #define ATTR_NAME_TYPE_INT 0
  207. #define ATTR_NAME_TYPE_STR 1
  208. static const AttrNameMap attr_names[]=
  209. {
  210. {"SizeLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, sizelength)},
  211. {"IndexLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, indexlength)},
  212. {"IndexDeltaLength", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, indexdeltalength)},
  213. {"profile-level-id", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, profile_level_id)},
  214. {"StreamType", ATTR_NAME_TYPE_INT, offsetof(RTPPayloadData, streamtype)},
  215. {"mode", ATTR_NAME_TYPE_STR, offsetof(RTPPayloadData, mode)},
  216. {NULL, -1, -1},
  217. };
  218. /** parse the attribute line from the fmtp a line of an sdp resonse. This is broken out as a function
  219. * because it is used in rtp_h264.c, which is forthcoming.
  220. */
  221. int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
  222. {
  223. skip_spaces(p);
  224. if(**p) {
  225. get_word_sep(attr, attr_size, "=", p);
  226. if (**p == '=')
  227. (*p)++;
  228. get_word_sep(value, value_size, ";", p);
  229. if (**p == ';')
  230. (*p)++;
  231. return 1;
  232. }
  233. return 0;
  234. }
  235. /* parse a SDP line and save stream attributes */
  236. static void sdp_parse_fmtp(AVStream *st, const char *p)
  237. {
  238. char attr[256];
  239. char value[4096];
  240. int i;
  241. RTSPStream *rtsp_st = st->priv_data;
  242. AVCodecContext *codec = st->codec;
  243. RTPPayloadData *rtp_payload_data = &rtsp_st->rtp_payload_data;
  244. /* loop on each attribute */
  245. while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
  246. {
  247. /* grab the codec extra_data from the config parameter of the fmtp line */
  248. sdp_parse_fmtp_config(codec, attr, value);
  249. /* Looking for a known attribute */
  250. for (i = 0; attr_names[i].str; ++i) {
  251. if (!strcasecmp(attr, attr_names[i].str)) {
  252. if (attr_names[i].type == ATTR_NAME_TYPE_INT)
  253. *(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
  254. else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  255. *(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
  256. }
  257. }
  258. }
  259. }
  260. /** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
  261. * and end time.
  262. * Used for seeking in the rtp stream.
  263. */
  264. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  265. {
  266. char buf[256];
  267. skip_spaces(&p);
  268. if (!av_stristart(p, "npt=", &p))
  269. return;
  270. *start = AV_NOPTS_VALUE;
  271. *end = AV_NOPTS_VALUE;
  272. get_word_sep(buf, sizeof(buf), "-", &p);
  273. *start = parse_date(buf, 1);
  274. if (*p == '-') {
  275. p++;
  276. get_word_sep(buf, sizeof(buf), "-", &p);
  277. *end = parse_date(buf, 1);
  278. }
  279. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  280. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  281. }
  282. typedef struct SDPParseState {
  283. /* SDP only */
  284. struct in_addr default_ip;
  285. int default_ttl;
  286. int skip_media; ///< set if an unknown m= line occurs
  287. } SDPParseState;
  288. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  289. int letter, const char *buf)
  290. {
  291. RTSPState *rt = s->priv_data;
  292. char buf1[64], st_type[64];
  293. const char *p;
  294. enum CodecType codec_type;
  295. int payload_type, i;
  296. AVStream *st;
  297. RTSPStream *rtsp_st;
  298. struct in_addr sdp_ip;
  299. int ttl;
  300. #ifdef DEBUG
  301. printf("sdp: %c='%s'\n", letter, buf);
  302. #endif
  303. p = buf;
  304. if (s1->skip_media && letter != 'm')
  305. return;
  306. switch(letter) {
  307. case 'c':
  308. get_word(buf1, sizeof(buf1), &p);
  309. if (strcmp(buf1, "IN") != 0)
  310. return;
  311. get_word(buf1, sizeof(buf1), &p);
  312. if (strcmp(buf1, "IP4") != 0)
  313. return;
  314. get_word_sep(buf1, sizeof(buf1), "/", &p);
  315. if (inet_aton(buf1, &sdp_ip) == 0)
  316. return;
  317. ttl = 16;
  318. if (*p == '/') {
  319. p++;
  320. get_word_sep(buf1, sizeof(buf1), "/", &p);
  321. ttl = atoi(buf1);
  322. }
  323. if (s->nb_streams == 0) {
  324. s1->default_ip = sdp_ip;
  325. s1->default_ttl = ttl;
  326. } else {
  327. st = s->streams[s->nb_streams - 1];
  328. rtsp_st = st->priv_data;
  329. rtsp_st->sdp_ip = sdp_ip;
  330. rtsp_st->sdp_ttl = ttl;
  331. }
  332. break;
  333. case 's':
  334. av_metadata_set(&s->metadata, "title", p);
  335. break;
  336. case 'i':
  337. if (s->nb_streams == 0) {
  338. av_metadata_set(&s->metadata, "comment", p);
  339. break;
  340. }
  341. break;
  342. case 'm':
  343. /* new stream */
  344. s1->skip_media = 0;
  345. get_word(st_type, sizeof(st_type), &p);
  346. if (!strcmp(st_type, "audio")) {
  347. codec_type = CODEC_TYPE_AUDIO;
  348. } else if (!strcmp(st_type, "video")) {
  349. codec_type = CODEC_TYPE_VIDEO;
  350. } else if (!strcmp(st_type, "application")) {
  351. codec_type = CODEC_TYPE_DATA;
  352. } else {
  353. s1->skip_media = 1;
  354. return;
  355. }
  356. rtsp_st = av_mallocz(sizeof(RTSPStream));
  357. if (!rtsp_st)
  358. return;
  359. rtsp_st->stream_index = -1;
  360. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  361. rtsp_st->sdp_ip = s1->default_ip;
  362. rtsp_st->sdp_ttl = s1->default_ttl;
  363. get_word(buf1, sizeof(buf1), &p); /* port */
  364. rtsp_st->sdp_port = atoi(buf1);
  365. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  366. /* XXX: handle list of formats */
  367. get_word(buf1, sizeof(buf1), &p); /* format list */
  368. rtsp_st->sdp_payload_type = atoi(buf1);
  369. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  370. /* no corresponding stream */
  371. } else {
  372. st = av_new_stream(s, 0);
  373. if (!st)
  374. return;
  375. st->priv_data = rtsp_st;
  376. rtsp_st->stream_index = st->index;
  377. st->codec->codec_type = codec_type;
  378. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  379. /* if standard payload type, we can find the codec right now */
  380. ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  381. }
  382. }
  383. /* put a default control url */
  384. av_strlcpy(rtsp_st->control_url, s->filename, sizeof(rtsp_st->control_url));
  385. break;
  386. case 'a':
  387. if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
  388. char proto[32];
  389. /* get the control url */
  390. st = s->streams[s->nb_streams - 1];
  391. rtsp_st = st->priv_data;
  392. /* XXX: may need to add full url resolution */
  393. url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
  394. if (proto[0] == '\0') {
  395. /* relative control URL */
  396. av_strlcat(rtsp_st->control_url, "/", sizeof(rtsp_st->control_url));
  397. av_strlcat(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  398. } else {
  399. av_strlcpy(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  400. }
  401. } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
  402. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  403. get_word(buf1, sizeof(buf1), &p);
  404. payload_type = atoi(buf1);
  405. st = s->streams[s->nb_streams - 1];
  406. rtsp_st = st->priv_data;
  407. sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
  408. } else if (av_strstart(p, "fmtp:", &p)) {
  409. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  410. get_word(buf1, sizeof(buf1), &p);
  411. payload_type = atoi(buf1);
  412. for(i = 0; i < s->nb_streams;i++) {
  413. st = s->streams[i];
  414. rtsp_st = st->priv_data;
  415. if (rtsp_st->sdp_payload_type == payload_type) {
  416. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  417. if(!rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf)) {
  418. sdp_parse_fmtp(st, p);
  419. }
  420. } else {
  421. sdp_parse_fmtp(st, p);
  422. }
  423. }
  424. }
  425. } else if(av_strstart(p, "framesize:", &p)) {
  426. // let dynamic protocol handlers have a stab at the line.
  427. get_word(buf1, sizeof(buf1), &p);
  428. payload_type = atoi(buf1);
  429. for(i = 0; i < s->nb_streams;i++) {
  430. st = s->streams[i];
  431. rtsp_st = st->priv_data;
  432. if (rtsp_st->sdp_payload_type == payload_type) {
  433. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  434. rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf);
  435. }
  436. }
  437. }
  438. } else if(av_strstart(p, "range:", &p)) {
  439. int64_t start, end;
  440. // this is so that seeking on a streamed file can work.
  441. rtsp_parse_range_npt(p, &start, &end);
  442. s->start_time= start;
  443. s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
  444. } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
  445. if (atoi(p) == 1)
  446. rt->transport = RTSP_TRANSPORT_RDT;
  447. } else {
  448. if (rt->server_type == RTSP_SERVER_WMS)
  449. ff_wms_parse_sdp_a_line(s, p);
  450. if (s->nb_streams > 0) {
  451. if (rt->server_type == RTSP_SERVER_REAL)
  452. ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
  453. rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
  454. if (rtsp_st->dynamic_handler &&
  455. rtsp_st->dynamic_handler->parse_sdp_a_line)
  456. rtsp_st->dynamic_handler->parse_sdp_a_line(s, s->nb_streams - 1,
  457. rtsp_st->dynamic_protocol_context, buf);
  458. }
  459. }
  460. break;
  461. }
  462. }
  463. static int sdp_parse(AVFormatContext *s, const char *content)
  464. {
  465. const char *p;
  466. int letter;
  467. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  468. * contain long SDP lines containing complete ASF Headers (several
  469. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  470. * "rulebooks" describing their properties. Therefore, the SDP line
  471. * buffer is large. */
  472. char buf[8192], *q;
  473. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  474. memset(s1, 0, sizeof(SDPParseState));
  475. p = content;
  476. for(;;) {
  477. skip_spaces(&p);
  478. letter = *p;
  479. if (letter == '\0')
  480. break;
  481. p++;
  482. if (*p != '=')
  483. goto next_line;
  484. p++;
  485. /* get the content */
  486. q = buf;
  487. while (*p != '\n' && *p != '\r' && *p != '\0') {
  488. if ((q - buf) < sizeof(buf) - 1)
  489. *q++ = *p;
  490. p++;
  491. }
  492. *q = '\0';
  493. sdp_parse_line(s, s1, letter, buf);
  494. next_line:
  495. while (*p != '\n' && *p != '\0')
  496. p++;
  497. if (*p == '\n')
  498. p++;
  499. }
  500. return 0;
  501. }
  502. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  503. {
  504. const char *p;
  505. int v;
  506. p = *pp;
  507. skip_spaces(&p);
  508. v = strtol(p, (char **)&p, 10);
  509. if (*p == '-') {
  510. p++;
  511. *min_ptr = v;
  512. v = strtol(p, (char **)&p, 10);
  513. *max_ptr = v;
  514. } else {
  515. *min_ptr = v;
  516. *max_ptr = v;
  517. }
  518. *pp = p;
  519. }
  520. /* XXX: only one transport specification is parsed */
  521. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  522. {
  523. char transport_protocol[16];
  524. char profile[16];
  525. char lower_transport[16];
  526. char parameter[16];
  527. RTSPTransportField *th;
  528. char buf[256];
  529. reply->nb_transports = 0;
  530. for(;;) {
  531. skip_spaces(&p);
  532. if (*p == '\0')
  533. break;
  534. th = &reply->transports[reply->nb_transports];
  535. get_word_sep(transport_protocol, sizeof(transport_protocol),
  536. "/", &p);
  537. if (!strcasecmp (transport_protocol, "rtp")) {
  538. get_word_sep(profile, sizeof(profile), "/;,", &p);
  539. lower_transport[0] = '\0';
  540. /* rtp/avp/<protocol> */
  541. if (*p == '/') {
  542. get_word_sep(lower_transport, sizeof(lower_transport),
  543. ";,", &p);
  544. }
  545. th->transport = RTSP_TRANSPORT_RTP;
  546. } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
  547. !strcasecmp (transport_protocol, "x-real-rdt")) {
  548. /* x-pn-tng/<protocol> */
  549. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  550. profile[0] = '\0';
  551. th->transport = RTSP_TRANSPORT_RDT;
  552. }
  553. if (!strcasecmp(lower_transport, "TCP"))
  554. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  555. else
  556. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  557. if (*p == ';')
  558. p++;
  559. /* get each parameter */
  560. while (*p != '\0' && *p != ',') {
  561. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  562. if (!strcmp(parameter, "port")) {
  563. if (*p == '=') {
  564. p++;
  565. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  566. }
  567. } else if (!strcmp(parameter, "client_port")) {
  568. if (*p == '=') {
  569. p++;
  570. rtsp_parse_range(&th->client_port_min,
  571. &th->client_port_max, &p);
  572. }
  573. } else if (!strcmp(parameter, "server_port")) {
  574. if (*p == '=') {
  575. p++;
  576. rtsp_parse_range(&th->server_port_min,
  577. &th->server_port_max, &p);
  578. }
  579. } else if (!strcmp(parameter, "interleaved")) {
  580. if (*p == '=') {
  581. p++;
  582. rtsp_parse_range(&th->interleaved_min,
  583. &th->interleaved_max, &p);
  584. }
  585. } else if (!strcmp(parameter, "multicast")) {
  586. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  587. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  588. } else if (!strcmp(parameter, "ttl")) {
  589. if (*p == '=') {
  590. p++;
  591. th->ttl = strtol(p, (char **)&p, 10);
  592. }
  593. } else if (!strcmp(parameter, "destination")) {
  594. struct in_addr ipaddr;
  595. if (*p == '=') {
  596. p++;
  597. get_word_sep(buf, sizeof(buf), ";,", &p);
  598. if (inet_aton(buf, &ipaddr))
  599. th->destination = ntohl(ipaddr.s_addr);
  600. }
  601. }
  602. while (*p != ';' && *p != '\0' && *p != ',')
  603. p++;
  604. if (*p == ';')
  605. p++;
  606. }
  607. if (*p == ',')
  608. p++;
  609. reply->nb_transports++;
  610. }
  611. }
  612. void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf)
  613. {
  614. const char *p;
  615. /* NOTE: we do case independent match for broken servers */
  616. p = buf;
  617. if (av_stristart(p, "Session:", &p)) {
  618. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  619. } else if (av_stristart(p, "Content-Length:", &p)) {
  620. reply->content_length = strtol(p, NULL, 10);
  621. } else if (av_stristart(p, "Transport:", &p)) {
  622. rtsp_parse_transport(reply, p);
  623. } else if (av_stristart(p, "CSeq:", &p)) {
  624. reply->seq = strtol(p, NULL, 10);
  625. } else if (av_stristart(p, "Range:", &p)) {
  626. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  627. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  628. skip_spaces(&p);
  629. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  630. } else if (av_stristart(p, "Server:", &p)) {
  631. skip_spaces(&p);
  632. av_strlcpy(reply->server, p, sizeof(reply->server));
  633. }
  634. }
  635. static int url_readbuf(URLContext *h, unsigned char *buf, int size)
  636. {
  637. int ret, len;
  638. len = 0;
  639. while (len < size) {
  640. ret = url_read(h, buf+len, size-len);
  641. if (ret < 1)
  642. return ret;
  643. len += ret;
  644. }
  645. return len;
  646. }
  647. /* skip a RTP/TCP interleaved packet */
  648. static void rtsp_skip_packet(AVFormatContext *s)
  649. {
  650. RTSPState *rt = s->priv_data;
  651. int ret, len, len1;
  652. uint8_t buf[1024];
  653. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  654. if (ret != 3)
  655. return;
  656. len = AV_RB16(buf + 1);
  657. #ifdef DEBUG
  658. printf("skipping RTP packet len=%d\n", len);
  659. #endif
  660. /* skip payload */
  661. while (len > 0) {
  662. len1 = len;
  663. if (len1 > sizeof(buf))
  664. len1 = sizeof(buf);
  665. ret = url_readbuf(rt->rtsp_hd, buf, len1);
  666. if (ret != len1)
  667. return;
  668. len -= len1;
  669. }
  670. }
  671. /**
  672. * Read a RTSP message from the server, or prepare to read data
  673. * packets if we're reading data interleaved over the TCP/RTSP
  674. * connection as well.
  675. *
  676. * @param s RTSP demuxer context
  677. * @param reply pointer where the RTSP message header will be stored
  678. * @param content_ptr pointer where the RTSP message body, if any, will
  679. * be stored (length is in \p reply)
  680. * @param return_on_interleaved_data whether the function may return if we
  681. * encounter a data marker ('$'), which precedes data
  682. * packets over interleaved TCP/RTSP connections. If this
  683. * is set, this function will return 1 after encountering
  684. * a '$'. If it is not set, the function will skip any
  685. * data packets (if they are encountered), until a reply
  686. * has been fully parsed. If no more data is available
  687. * without parsing a reply, it will return an error.
  688. *
  689. * @returns 1 if a data packets is ready to be received, -1 on error,
  690. * and 0 on success.
  691. */
  692. static int
  693. rtsp_read_reply (AVFormatContext *s, RTSPMessageHeader *reply,
  694. unsigned char **content_ptr, int return_on_interleaved_data)
  695. {
  696. RTSPState *rt = s->priv_data;
  697. char buf[4096], buf1[1024], *q;
  698. unsigned char ch;
  699. const char *p;
  700. int ret, content_length, line_count = 0;
  701. unsigned char *content = NULL;
  702. memset(reply, 0, sizeof(*reply));
  703. /* parse reply (XXX: use buffers) */
  704. rt->last_reply[0] = '\0';
  705. for(;;) {
  706. q = buf;
  707. for(;;) {
  708. ret = url_readbuf(rt->rtsp_hd, &ch, 1);
  709. #ifdef DEBUG_RTP_TCP
  710. printf("ret=%d c=%02x [%c]\n", ret, ch, ch);
  711. #endif
  712. if (ret != 1)
  713. return -1;
  714. if (ch == '\n')
  715. break;
  716. if (ch == '$') {
  717. /* XXX: only parse it if first char on line ? */
  718. if (return_on_interleaved_data) {
  719. return 1;
  720. } else
  721. rtsp_skip_packet(s);
  722. } else if (ch != '\r') {
  723. if ((q - buf) < sizeof(buf) - 1)
  724. *q++ = ch;
  725. }
  726. }
  727. *q = '\0';
  728. #ifdef DEBUG
  729. printf("line='%s'\n", buf);
  730. #endif
  731. /* test if last line */
  732. if (buf[0] == '\0')
  733. break;
  734. p = buf;
  735. if (line_count == 0) {
  736. /* get reply code */
  737. get_word(buf1, sizeof(buf1), &p);
  738. get_word(buf1, sizeof(buf1), &p);
  739. reply->status_code = atoi(buf1);
  740. } else {
  741. rtsp_parse_line(reply, p);
  742. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  743. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  744. }
  745. line_count++;
  746. }
  747. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  748. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  749. content_length = reply->content_length;
  750. if (content_length > 0) {
  751. /* leave some room for a trailing '\0' (useful for simple parsing) */
  752. content = av_malloc(content_length + 1);
  753. (void)url_readbuf(rt->rtsp_hd, content, content_length);
  754. content[content_length] = '\0';
  755. }
  756. if (content_ptr)
  757. *content_ptr = content;
  758. else
  759. av_free(content);
  760. return 0;
  761. }
  762. static void rtsp_send_cmd(AVFormatContext *s,
  763. const char *cmd, RTSPMessageHeader *reply,
  764. unsigned char **content_ptr)
  765. {
  766. RTSPState *rt = s->priv_data;
  767. char buf[4096], buf1[1024];
  768. rt->seq++;
  769. av_strlcpy(buf, cmd, sizeof(buf));
  770. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  771. av_strlcat(buf, buf1, sizeof(buf));
  772. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  773. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  774. av_strlcat(buf, buf1, sizeof(buf));
  775. }
  776. av_strlcat(buf, "\r\n", sizeof(buf));
  777. #ifdef DEBUG
  778. printf("Sending:\n%s--\n", buf);
  779. #endif
  780. url_write(rt->rtsp_hd, buf, strlen(buf));
  781. rtsp_read_reply(s, reply, content_ptr, 0);
  782. }
  783. /* close and free RTSP streams */
  784. static void rtsp_close_streams(RTSPState *rt)
  785. {
  786. int i;
  787. RTSPStream *rtsp_st;
  788. for(i=0;i<rt->nb_rtsp_streams;i++) {
  789. rtsp_st = rt->rtsp_streams[i];
  790. if (rtsp_st) {
  791. if (rtsp_st->transport_priv) {
  792. if (rt->transport == RTSP_TRANSPORT_RDT)
  793. ff_rdt_parse_close(rtsp_st->transport_priv);
  794. else
  795. rtp_parse_close(rtsp_st->transport_priv);
  796. }
  797. if (rtsp_st->rtp_handle)
  798. url_close(rtsp_st->rtp_handle);
  799. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  800. rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
  801. }
  802. }
  803. av_free(rt->rtsp_streams);
  804. if (rt->asf_ctx) {
  805. av_close_input_stream (rt->asf_ctx);
  806. rt->asf_ctx = NULL;
  807. }
  808. }
  809. static int
  810. rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  811. {
  812. RTSPState *rt = s->priv_data;
  813. AVStream *st = NULL;
  814. /* open the RTP context */
  815. if (rtsp_st->stream_index >= 0)
  816. st = s->streams[rtsp_st->stream_index];
  817. if (!st)
  818. s->ctx_flags |= AVFMTCTX_NOHEADER;
  819. if (rt->transport == RTSP_TRANSPORT_RDT)
  820. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  821. rtsp_st->dynamic_protocol_context,
  822. rtsp_st->dynamic_handler);
  823. else
  824. rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
  825. rtsp_st->sdp_payload_type,
  826. &rtsp_st->rtp_payload_data);
  827. if (!rtsp_st->transport_priv) {
  828. return AVERROR(ENOMEM);
  829. } else if (rt->transport != RTSP_TRANSPORT_RDT) {
  830. if(rtsp_st->dynamic_handler) {
  831. rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  832. rtsp_st->dynamic_protocol_context,
  833. rtsp_st->dynamic_handler);
  834. }
  835. }
  836. return 0;
  837. }
  838. /**
  839. * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
  840. */
  841. static int
  842. make_setup_request (AVFormatContext *s, const char *host, int port,
  843. int lower_transport, const char *real_challenge)
  844. {
  845. RTSPState *rt = s->priv_data;
  846. int rtx, j, i, err, interleave = 0;
  847. RTSPStream *rtsp_st;
  848. RTSPMessageHeader reply1, *reply = &reply1;
  849. char cmd[2048];
  850. const char *trans_pref;
  851. if (rt->transport == RTSP_TRANSPORT_RDT)
  852. trans_pref = "x-pn-tng";
  853. else
  854. trans_pref = "RTP/AVP";
  855. /* for each stream, make the setup request */
  856. /* XXX: we assume the same server is used for the control of each
  857. RTSP stream */
  858. for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  859. char transport[2048];
  860. /**
  861. * WMS serves all UDP data over a single connection, the RTX, which
  862. * isn't necessarily the first in the SDP but has to be the first
  863. * to be set up, else the second/third SETUP will fail with a 461.
  864. */
  865. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  866. rt->server_type == RTSP_SERVER_WMS) {
  867. if (i == 0) {
  868. /* rtx first */
  869. for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
  870. int len = strlen(rt->rtsp_streams[rtx]->control_url);
  871. if (len >= 4 &&
  872. !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4, "/rtx"))
  873. break;
  874. }
  875. if (rtx == rt->nb_rtsp_streams)
  876. return -1; /* no RTX found */
  877. rtsp_st = rt->rtsp_streams[rtx];
  878. } else
  879. rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
  880. } else
  881. rtsp_st = rt->rtsp_streams[i];
  882. /* RTP/UDP */
  883. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  884. char buf[256];
  885. if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
  886. port = reply->transports[0].client_port_min;
  887. goto have_port;
  888. }
  889. /* first try in specified port range */
  890. if (RTSP_RTP_PORT_MIN != 0) {
  891. while(j <= RTSP_RTP_PORT_MAX) {
  892. snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
  893. j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
  894. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
  895. goto rtp_opened;
  896. }
  897. }
  898. }
  899. /* then try on any port
  900. ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  901. ** err = AVERROR_INVALIDDATA;
  902. ** goto fail;
  903. ** }
  904. */
  905. rtp_opened:
  906. port = rtp_get_local_port(rtsp_st->rtp_handle);
  907. have_port:
  908. snprintf(transport, sizeof(transport) - 1,
  909. "%s/UDP;", trans_pref);
  910. if (rt->server_type != RTSP_SERVER_REAL)
  911. av_strlcat(transport, "unicast;", sizeof(transport));
  912. av_strlcatf(transport, sizeof(transport),
  913. "client_port=%d", port);
  914. if (rt->transport == RTSP_TRANSPORT_RTP &&
  915. !(rt->server_type == RTSP_SERVER_WMS && i > 0))
  916. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  917. }
  918. /* RTP/TCP */
  919. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  920. /** For WMS streams, the application streams are only used for
  921. * UDP. When trying to set it up for TCP streams, the server
  922. * will return an error. Therefore, we skip those streams. */
  923. if (rt->server_type == RTSP_SERVER_WMS &&
  924. s->streams[rtsp_st->stream_index]->codec->codec_type == CODEC_TYPE_DATA)
  925. continue;
  926. snprintf(transport, sizeof(transport) - 1,
  927. "%s/TCP;", trans_pref);
  928. if (rt->server_type == RTSP_SERVER_WMS)
  929. av_strlcat(transport, "unicast;", sizeof(transport));
  930. av_strlcatf(transport, sizeof(transport),
  931. "interleaved=%d-%d",
  932. interleave, interleave + 1);
  933. interleave += 2;
  934. }
  935. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  936. snprintf(transport, sizeof(transport) - 1,
  937. "%s/UDP;multicast", trans_pref);
  938. }
  939. if (rt->server_type == RTSP_SERVER_REAL ||
  940. rt->server_type == RTSP_SERVER_WMS)
  941. av_strlcat(transport, ";mode=play", sizeof(transport));
  942. snprintf(cmd, sizeof(cmd),
  943. "SETUP %s RTSP/1.0\r\n"
  944. "Transport: %s\r\n",
  945. rtsp_st->control_url, transport);
  946. if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
  947. char real_res[41], real_csum[9];
  948. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  949. real_challenge);
  950. av_strlcatf(cmd, sizeof(cmd),
  951. "If-Match: %s\r\n"
  952. "RealChallenge2: %s, sd=%s\r\n",
  953. rt->session_id, real_res, real_csum);
  954. }
  955. rtsp_send_cmd(s, cmd, reply, NULL);
  956. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  957. err = 1;
  958. goto fail;
  959. } else if (reply->status_code != RTSP_STATUS_OK ||
  960. reply->nb_transports != 1) {
  961. err = AVERROR_INVALIDDATA;
  962. goto fail;
  963. }
  964. /* XXX: same protocol for all streams is required */
  965. if (i > 0) {
  966. if (reply->transports[0].lower_transport != rt->lower_transport ||
  967. reply->transports[0].transport != rt->transport) {
  968. err = AVERROR_INVALIDDATA;
  969. goto fail;
  970. }
  971. } else {
  972. rt->lower_transport = reply->transports[0].lower_transport;
  973. rt->transport = reply->transports[0].transport;
  974. }
  975. /* close RTP connection if not choosen */
  976. if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
  977. (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
  978. url_close(rtsp_st->rtp_handle);
  979. rtsp_st->rtp_handle = NULL;
  980. }
  981. switch(reply->transports[0].lower_transport) {
  982. case RTSP_LOWER_TRANSPORT_TCP:
  983. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  984. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  985. break;
  986. case RTSP_LOWER_TRANSPORT_UDP:
  987. {
  988. char url[1024];
  989. /* XXX: also use address if specified */
  990. snprintf(url, sizeof(url), "rtp://%s:%d",
  991. host, reply->transports[0].server_port_min);
  992. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
  993. rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  994. err = AVERROR_INVALIDDATA;
  995. goto fail;
  996. }
  997. }
  998. break;
  999. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1000. {
  1001. char url[1024];
  1002. struct in_addr in;
  1003. in.s_addr = htonl(reply->transports[0].destination);
  1004. snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
  1005. inet_ntoa(in),
  1006. reply->transports[0].port_min,
  1007. reply->transports[0].ttl);
  1008. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1009. err = AVERROR_INVALIDDATA;
  1010. goto fail;
  1011. }
  1012. }
  1013. break;
  1014. }
  1015. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1016. goto fail;
  1017. }
  1018. if (rt->server_type == RTSP_SERVER_REAL)
  1019. rt->need_subscription = 1;
  1020. return 0;
  1021. fail:
  1022. for (i=0; i<rt->nb_rtsp_streams; i++) {
  1023. if (rt->rtsp_streams[i]->rtp_handle) {
  1024. url_close(rt->rtsp_streams[i]->rtp_handle);
  1025. rt->rtsp_streams[i]->rtp_handle = NULL;
  1026. }
  1027. }
  1028. return err;
  1029. }
  1030. static int rtsp_read_header(AVFormatContext *s,
  1031. AVFormatParameters *ap)
  1032. {
  1033. RTSPState *rt = s->priv_data;
  1034. char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
  1035. URLContext *rtsp_hd;
  1036. int port, ret, err;
  1037. RTSPMessageHeader reply1, *reply = &reply1;
  1038. unsigned char *content = NULL;
  1039. int lower_transport_mask = 0;
  1040. char real_challenge[64];
  1041. /* extract hostname and port */
  1042. url_split(NULL, 0, NULL, 0,
  1043. host, sizeof(host), &port, path, sizeof(path), s->filename);
  1044. if (port < 0)
  1045. port = RTSP_DEFAULT_PORT;
  1046. /* search for options */
  1047. option_list = strchr(path, '?');
  1048. if (option_list) {
  1049. /* remove the options from the path */
  1050. *option_list++ = 0;
  1051. while(option_list) {
  1052. /* move the option pointer */
  1053. option = option_list;
  1054. option_list = strchr(option_list, '&');
  1055. if (option_list)
  1056. *(option_list++) = 0;
  1057. /* handle the options */
  1058. if (strcmp(option, "udp") == 0)
  1059. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
  1060. else if (strcmp(option, "multicast") == 0)
  1061. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
  1062. else if (strcmp(option, "tcp") == 0)
  1063. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
  1064. }
  1065. }
  1066. if (!lower_transport_mask)
  1067. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1068. /* open the tcp connexion */
  1069. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  1070. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  1071. return AVERROR(EIO);
  1072. rt->rtsp_hd = rtsp_hd;
  1073. rt->seq = 0;
  1074. /* request options supported by the server; this also detects server type */
  1075. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1076. snprintf(cmd, sizeof(cmd),
  1077. "OPTIONS %s RTSP/1.0\r\n", s->filename);
  1078. if (rt->server_type == RTSP_SERVER_REAL)
  1079. av_strlcat(cmd,
  1080. /**
  1081. * The following entries are required for proper
  1082. * streaming from a Realmedia server. They are
  1083. * interdependent in some way although we currently
  1084. * don't quite understand how. Values were copied
  1085. * from mplayer SVN r23589.
  1086. * @param CompanyID is a 16-byte ID in base64
  1087. * @param ClientChallenge is a 16-byte ID in hex
  1088. */
  1089. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1090. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1091. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1092. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1093. sizeof(cmd));
  1094. rtsp_send_cmd(s, cmd, reply, NULL);
  1095. if (reply->status_code != RTSP_STATUS_OK) {
  1096. err = AVERROR_INVALIDDATA;
  1097. goto fail;
  1098. }
  1099. /* detect server type if not standard-compliant RTP */
  1100. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1101. rt->server_type = RTSP_SERVER_REAL;
  1102. continue;
  1103. } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
  1104. rt->server_type = RTSP_SERVER_WMS;
  1105. } else if (rt->server_type == RTSP_SERVER_REAL) {
  1106. strcpy(real_challenge, reply->real_challenge);
  1107. }
  1108. break;
  1109. }
  1110. /* describe the stream */
  1111. snprintf(cmd, sizeof(cmd),
  1112. "DESCRIBE %s RTSP/1.0\r\n"
  1113. "Accept: application/sdp\r\n",
  1114. s->filename);
  1115. if (rt->server_type == RTSP_SERVER_REAL) {
  1116. /**
  1117. * The Require: attribute is needed for proper streaming from
  1118. * Realmedia servers.
  1119. */
  1120. av_strlcat(cmd,
  1121. "Require: com.real.retain-entity-for-setup\r\n",
  1122. sizeof(cmd));
  1123. }
  1124. rtsp_send_cmd(s, cmd, reply, &content);
  1125. if (!content) {
  1126. err = AVERROR_INVALIDDATA;
  1127. goto fail;
  1128. }
  1129. if (reply->status_code != RTSP_STATUS_OK) {
  1130. err = AVERROR_INVALIDDATA;
  1131. goto fail;
  1132. }
  1133. /* now we got the SDP description, we parse it */
  1134. ret = sdp_parse(s, (const char *)content);
  1135. av_freep(&content);
  1136. if (ret < 0) {
  1137. err = AVERROR_INVALIDDATA;
  1138. goto fail;
  1139. }
  1140. do {
  1141. int lower_transport = ff_log2_tab[lower_transport_mask & ~(lower_transport_mask - 1)];
  1142. err = make_setup_request(s, host, port, lower_transport,
  1143. rt->server_type == RTSP_SERVER_REAL ?
  1144. real_challenge : NULL);
  1145. if (err < 0)
  1146. goto fail;
  1147. lower_transport_mask &= ~(1 << lower_transport);
  1148. if (lower_transport_mask == 0 && err == 1) {
  1149. err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
  1150. goto fail;
  1151. }
  1152. } while (err);
  1153. rt->state = RTSP_STATE_IDLE;
  1154. rt->seek_timestamp = 0; /* default is to start stream at position
  1155. zero */
  1156. if (ap->initial_pause) {
  1157. /* do not start immediately */
  1158. } else {
  1159. if (rtsp_read_play(s) < 0) {
  1160. err = AVERROR_INVALIDDATA;
  1161. goto fail;
  1162. }
  1163. }
  1164. return 0;
  1165. fail:
  1166. rtsp_close_streams(rt);
  1167. av_freep(&content);
  1168. url_close(rt->rtsp_hd);
  1169. return err;
  1170. }
  1171. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1172. uint8_t *buf, int buf_size)
  1173. {
  1174. RTSPState *rt = s->priv_data;
  1175. int id, len, i, ret;
  1176. RTSPStream *rtsp_st;
  1177. #ifdef DEBUG_RTP_TCP
  1178. printf("tcp_read_packet:\n");
  1179. #endif
  1180. redo:
  1181. for(;;) {
  1182. RTSPMessageHeader reply;
  1183. ret = rtsp_read_reply(s, &reply, NULL, 1);
  1184. if (ret == -1)
  1185. return -1;
  1186. if (ret == 1) /* received '$' */
  1187. break;
  1188. /* XXX: parse message */
  1189. }
  1190. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  1191. if (ret != 3)
  1192. return -1;
  1193. id = buf[0];
  1194. len = AV_RB16(buf + 1);
  1195. #ifdef DEBUG_RTP_TCP
  1196. printf("id=%d len=%d\n", id, len);
  1197. #endif
  1198. if (len > buf_size || len < 12)
  1199. goto redo;
  1200. /* get the data */
  1201. ret = url_readbuf(rt->rtsp_hd, buf, len);
  1202. if (ret != len)
  1203. return -1;
  1204. if (rt->transport == RTSP_TRANSPORT_RDT &&
  1205. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  1206. return -1;
  1207. /* find the matching stream */
  1208. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1209. rtsp_st = rt->rtsp_streams[i];
  1210. if (id >= rtsp_st->interleaved_min &&
  1211. id <= rtsp_st->interleaved_max)
  1212. goto found;
  1213. }
  1214. goto redo;
  1215. found:
  1216. *prtsp_st = rtsp_st;
  1217. return len;
  1218. }
  1219. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1220. uint8_t *buf, int buf_size)
  1221. {
  1222. RTSPState *rt = s->priv_data;
  1223. RTSPStream *rtsp_st;
  1224. fd_set rfds;
  1225. int fd, fd_max, n, i, ret, tcp_fd;
  1226. struct timeval tv;
  1227. for(;;) {
  1228. if (url_interrupt_cb())
  1229. return AVERROR(EINTR);
  1230. FD_ZERO(&rfds);
  1231. tcp_fd = fd_max = url_get_file_handle(rt->rtsp_hd);
  1232. FD_SET(tcp_fd, &rfds);
  1233. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1234. rtsp_st = rt->rtsp_streams[i];
  1235. if (rtsp_st->rtp_handle) {
  1236. /* currently, we cannot probe RTCP handle because of
  1237. * blocking restrictions */
  1238. fd = url_get_file_handle(rtsp_st->rtp_handle);
  1239. if (fd > fd_max)
  1240. fd_max = fd;
  1241. FD_SET(fd, &rfds);
  1242. }
  1243. }
  1244. tv.tv_sec = 0;
  1245. tv.tv_usec = 100 * 1000;
  1246. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1247. if (n > 0) {
  1248. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1249. rtsp_st = rt->rtsp_streams[i];
  1250. if (rtsp_st->rtp_handle) {
  1251. fd = url_get_file_handle(rtsp_st->rtp_handle);
  1252. if (FD_ISSET(fd, &rfds)) {
  1253. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1254. if (ret > 0) {
  1255. *prtsp_st = rtsp_st;
  1256. return ret;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. if (FD_ISSET(tcp_fd, &rfds)) {
  1262. RTSPMessageHeader reply;
  1263. rtsp_read_reply(s, &reply, NULL, 0);
  1264. /* XXX: parse message */
  1265. }
  1266. }
  1267. }
  1268. }
  1269. static int rtsp_read_packet(AVFormatContext *s,
  1270. AVPacket *pkt)
  1271. {
  1272. RTSPState *rt = s->priv_data;
  1273. RTSPStream *rtsp_st;
  1274. int ret, len;
  1275. uint8_t buf[10 * RTP_MAX_PACKET_LENGTH];
  1276. if (rt->server_type == RTSP_SERVER_REAL) {
  1277. int i;
  1278. RTSPMessageHeader reply1, *reply = &reply1;
  1279. enum AVDiscard cache[MAX_STREAMS];
  1280. char cmd[1024];
  1281. for (i = 0; i < s->nb_streams; i++)
  1282. cache[i] = s->streams[i]->discard;
  1283. if (!rt->need_subscription) {
  1284. if (memcmp (cache, rt->real_setup_cache,
  1285. sizeof(enum AVDiscard) * s->nb_streams)) {
  1286. av_strlcatf(cmd, sizeof(cmd),
  1287. "SET_PARAMETER %s RTSP/1.0\r\n"
  1288. "Unsubscribe: %s\r\n",
  1289. s->filename, rt->last_subscription);
  1290. rtsp_send_cmd(s, cmd, reply, NULL);
  1291. if (reply->status_code != RTSP_STATUS_OK)
  1292. return AVERROR_INVALIDDATA;
  1293. rt->need_subscription = 1;
  1294. }
  1295. }
  1296. if (rt->need_subscription) {
  1297. int r, rule_nr, first = 1;
  1298. memcpy(rt->real_setup_cache, cache,
  1299. sizeof(enum AVDiscard) * s->nb_streams);
  1300. rt->last_subscription[0] = 0;
  1301. snprintf(cmd, sizeof(cmd),
  1302. "SET_PARAMETER %s RTSP/1.0\r\n"
  1303. "Subscribe: ",
  1304. s->filename);
  1305. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1306. rule_nr = 0;
  1307. for (r = 0; r < s->nb_streams; r++) {
  1308. if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
  1309. if (s->streams[r]->discard != AVDISCARD_ALL) {
  1310. if (!first)
  1311. av_strlcat(rt->last_subscription, ",",
  1312. sizeof(rt->last_subscription));
  1313. ff_rdt_subscribe_rule(
  1314. rt->last_subscription,
  1315. sizeof(rt->last_subscription), i, rule_nr);
  1316. first = 0;
  1317. }
  1318. rule_nr++;
  1319. }
  1320. }
  1321. }
  1322. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  1323. rtsp_send_cmd(s, cmd, reply, NULL);
  1324. if (reply->status_code != RTSP_STATUS_OK)
  1325. return AVERROR_INVALIDDATA;
  1326. rt->need_subscription = 0;
  1327. if (rt->state == RTSP_STATE_PLAYING)
  1328. rtsp_read_play (s);
  1329. }
  1330. }
  1331. /* get next frames from the same RTP packet */
  1332. if (rt->cur_transport_priv) {
  1333. if (rt->transport == RTSP_TRANSPORT_RDT)
  1334. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1335. else
  1336. ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1337. if (ret == 0) {
  1338. rt->cur_transport_priv = NULL;
  1339. return 0;
  1340. } else if (ret == 1) {
  1341. return 0;
  1342. } else {
  1343. rt->cur_transport_priv = NULL;
  1344. }
  1345. }
  1346. /* read next RTP packet */
  1347. redo:
  1348. switch(rt->lower_transport) {
  1349. default:
  1350. case RTSP_LOWER_TRANSPORT_TCP:
  1351. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1352. break;
  1353. case RTSP_LOWER_TRANSPORT_UDP:
  1354. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1355. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1356. if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1357. rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
  1358. break;
  1359. }
  1360. if (len < 0)
  1361. return len;
  1362. if (rt->transport == RTSP_TRANSPORT_RDT)
  1363. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
  1364. else
  1365. ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
  1366. if (ret < 0)
  1367. goto redo;
  1368. if (ret == 1) {
  1369. /* more packets may follow, so we save the RTP context */
  1370. rt->cur_transport_priv = rtsp_st->transport_priv;
  1371. }
  1372. return 0;
  1373. }
  1374. static int rtsp_read_play(AVFormatContext *s)
  1375. {
  1376. RTSPState *rt = s->priv_data;
  1377. RTSPMessageHeader reply1, *reply = &reply1;
  1378. char cmd[1024];
  1379. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1380. if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1381. if (rt->state == RTSP_STATE_PAUSED) {
  1382. snprintf(cmd, sizeof(cmd),
  1383. "PLAY %s RTSP/1.0\r\n",
  1384. s->filename);
  1385. } else {
  1386. snprintf(cmd, sizeof(cmd),
  1387. "PLAY %s RTSP/1.0\r\n"
  1388. "Range: npt=%0.3f-\r\n",
  1389. s->filename,
  1390. (double)rt->seek_timestamp / AV_TIME_BASE);
  1391. }
  1392. rtsp_send_cmd(s, cmd, reply, NULL);
  1393. if (reply->status_code != RTSP_STATUS_OK) {
  1394. return -1;
  1395. }
  1396. }
  1397. rt->state = RTSP_STATE_PLAYING;
  1398. return 0;
  1399. }
  1400. /* pause the stream */
  1401. static int rtsp_read_pause(AVFormatContext *s)
  1402. {
  1403. RTSPState *rt = s->priv_data;
  1404. RTSPMessageHeader reply1, *reply = &reply1;
  1405. char cmd[1024];
  1406. rt = s->priv_data;
  1407. if (rt->state != RTSP_STATE_PLAYING)
  1408. return 0;
  1409. else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1410. snprintf(cmd, sizeof(cmd),
  1411. "PAUSE %s RTSP/1.0\r\n",
  1412. s->filename);
  1413. rtsp_send_cmd(s, cmd, reply, NULL);
  1414. if (reply->status_code != RTSP_STATUS_OK) {
  1415. return -1;
  1416. }
  1417. }
  1418. rt->state = RTSP_STATE_PAUSED;
  1419. return 0;
  1420. }
  1421. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1422. int64_t timestamp, int flags)
  1423. {
  1424. RTSPState *rt = s->priv_data;
  1425. rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
  1426. switch(rt->state) {
  1427. default:
  1428. case RTSP_STATE_IDLE:
  1429. break;
  1430. case RTSP_STATE_PLAYING:
  1431. if (rtsp_read_play(s) != 0)
  1432. return -1;
  1433. break;
  1434. case RTSP_STATE_PAUSED:
  1435. rt->state = RTSP_STATE_IDLE;
  1436. break;
  1437. }
  1438. return 0;
  1439. }
  1440. static int rtsp_read_close(AVFormatContext *s)
  1441. {
  1442. RTSPState *rt = s->priv_data;
  1443. RTSPMessageHeader reply1, *reply = &reply1;
  1444. char cmd[1024];
  1445. #if 0
  1446. /* NOTE: it is valid to flush the buffer here */
  1447. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  1448. url_fclose(&rt->rtsp_gb);
  1449. }
  1450. #endif
  1451. snprintf(cmd, sizeof(cmd),
  1452. "TEARDOWN %s RTSP/1.0\r\n",
  1453. s->filename);
  1454. rtsp_send_cmd(s, cmd, reply, NULL);
  1455. rtsp_close_streams(rt);
  1456. url_close(rt->rtsp_hd);
  1457. return 0;
  1458. }
  1459. #if CONFIG_RTSP_DEMUXER
  1460. AVInputFormat rtsp_demuxer = {
  1461. "rtsp",
  1462. NULL_IF_CONFIG_SMALL("RTSP input format"),
  1463. sizeof(RTSPState),
  1464. rtsp_probe,
  1465. rtsp_read_header,
  1466. rtsp_read_packet,
  1467. rtsp_read_close,
  1468. rtsp_read_seek,
  1469. .flags = AVFMT_NOFILE,
  1470. .read_play = rtsp_read_play,
  1471. .read_pause = rtsp_read_pause,
  1472. };
  1473. #endif
  1474. static int sdp_probe(AVProbeData *p1)
  1475. {
  1476. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1477. /* we look for a line beginning "c=IN IP4" */
  1478. while (p < p_end && *p != '\0') {
  1479. if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
  1480. return AVPROBE_SCORE_MAX / 2;
  1481. while(p < p_end - 1 && *p != '\n') p++;
  1482. if (++p >= p_end)
  1483. break;
  1484. if (*p == '\r')
  1485. p++;
  1486. }
  1487. return 0;
  1488. }
  1489. #define SDP_MAX_SIZE 8192
  1490. static int sdp_read_header(AVFormatContext *s,
  1491. AVFormatParameters *ap)
  1492. {
  1493. RTSPState *rt = s->priv_data;
  1494. RTSPStream *rtsp_st;
  1495. int size, i, err;
  1496. char *content;
  1497. char url[1024];
  1498. /* read the whole sdp file */
  1499. /* XXX: better loading */
  1500. content = av_malloc(SDP_MAX_SIZE);
  1501. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1502. if (size <= 0) {
  1503. av_free(content);
  1504. return AVERROR_INVALIDDATA;
  1505. }
  1506. content[size] ='\0';
  1507. sdp_parse(s, content);
  1508. av_free(content);
  1509. /* open each RTP stream */
  1510. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1511. rtsp_st = rt->rtsp_streams[i];
  1512. snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
  1513. inet_ntoa(rtsp_st->sdp_ip),
  1514. rtsp_st->sdp_port,
  1515. rtsp_st->sdp_port,
  1516. rtsp_st->sdp_ttl);
  1517. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1518. err = AVERROR_INVALIDDATA;
  1519. goto fail;
  1520. }
  1521. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1522. goto fail;
  1523. }
  1524. return 0;
  1525. fail:
  1526. rtsp_close_streams(rt);
  1527. return err;
  1528. }
  1529. static int sdp_read_packet(AVFormatContext *s,
  1530. AVPacket *pkt)
  1531. {
  1532. return rtsp_read_packet(s, pkt);
  1533. }
  1534. static int sdp_read_close(AVFormatContext *s)
  1535. {
  1536. RTSPState *rt = s->priv_data;
  1537. rtsp_close_streams(rt);
  1538. return 0;
  1539. }
  1540. #if CONFIG_SDP_DEMUXER
  1541. AVInputFormat sdp_demuxer = {
  1542. "sdp",
  1543. NULL_IF_CONFIG_SMALL("SDP"),
  1544. sizeof(RTSPState),
  1545. sdp_probe,
  1546. sdp_read_header,
  1547. sdp_read_packet,
  1548. sdp_read_close,
  1549. };
  1550. #endif
  1551. #if CONFIG_REDIR_DEMUXER
  1552. /* dummy redirector format (used directly in av_open_input_file now) */
  1553. static int redir_probe(AVProbeData *pd)
  1554. {
  1555. const char *p;
  1556. p = pd->buf;
  1557. skip_spaces(&p);
  1558. if (av_strstart(p, "http://", NULL) ||
  1559. av_strstart(p, "rtsp://", NULL))
  1560. return AVPROBE_SCORE_MAX;
  1561. return 0;
  1562. }
  1563. static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1564. {
  1565. char buf[4096], *q;
  1566. int c;
  1567. AVFormatContext *ic = NULL;
  1568. ByteIOContext *f = s->pb;
  1569. /* parse each URL and try to open it */
  1570. c = url_fgetc(f);
  1571. while (c != URL_EOF) {
  1572. /* skip spaces */
  1573. for(;;) {
  1574. if (!redir_isspace(c))
  1575. break;
  1576. c = url_fgetc(f);
  1577. }
  1578. if (c == URL_EOF)
  1579. break;
  1580. /* record url */
  1581. q = buf;
  1582. for(;;) {
  1583. if (c == URL_EOF || redir_isspace(c))
  1584. break;
  1585. if ((q - buf) < sizeof(buf) - 1)
  1586. *q++ = c;
  1587. c = url_fgetc(f);
  1588. }
  1589. *q = '\0';
  1590. //printf("URL='%s'\n", buf);
  1591. /* try to open the media file */
  1592. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1593. break;
  1594. }
  1595. if (!ic)
  1596. return AVERROR(EIO);
  1597. *s = *ic;
  1598. url_fclose(f);
  1599. return 0;
  1600. }
  1601. AVInputFormat redir_demuxer = {
  1602. "redir",
  1603. NULL_IF_CONFIG_SMALL("Redirector format"),
  1604. 0,
  1605. redir_probe,
  1606. redir_read_header,
  1607. NULL,
  1608. NULL,
  1609. };
  1610. #endif