rtsp.c 54 KB

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