rtsp.c 59 KB

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