rtsp.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  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 {
  359. s1->skip_media = 1;
  360. return;
  361. }
  362. rtsp_st = av_mallocz(sizeof(RTSPStream));
  363. if (!rtsp_st)
  364. return;
  365. rtsp_st->stream_index = -1;
  366. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  367. rtsp_st->sdp_ip = s1->default_ip;
  368. rtsp_st->sdp_ttl = s1->default_ttl;
  369. get_word(buf1, sizeof(buf1), &p); /* port */
  370. rtsp_st->sdp_port = atoi(buf1);
  371. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  372. /* XXX: handle list of formats */
  373. get_word(buf1, sizeof(buf1), &p); /* format list */
  374. rtsp_st->sdp_payload_type = atoi(buf1);
  375. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  376. /* no corresponding stream */
  377. } else {
  378. st = av_new_stream(s, 0);
  379. if (!st)
  380. return;
  381. st->priv_data = rtsp_st;
  382. rtsp_st->stream_index = st->index;
  383. st->codec->codec_type = codec_type;
  384. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  385. /* if standard payload type, we can find the codec right now */
  386. ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  387. }
  388. }
  389. /* put a default control url */
  390. av_strlcpy(rtsp_st->control_url, s->filename, sizeof(rtsp_st->control_url));
  391. break;
  392. case 'a':
  393. if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
  394. char proto[32];
  395. /* get the control url */
  396. st = s->streams[s->nb_streams - 1];
  397. rtsp_st = st->priv_data;
  398. /* XXX: may need to add full url resolution */
  399. url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
  400. if (proto[0] == '\0') {
  401. /* relative control URL */
  402. av_strlcat(rtsp_st->control_url, "/", sizeof(rtsp_st->control_url));
  403. av_strlcat(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  404. } else {
  405. av_strlcpy(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  406. }
  407. } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
  408. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  409. get_word(buf1, sizeof(buf1), &p);
  410. payload_type = atoi(buf1);
  411. st = s->streams[s->nb_streams - 1];
  412. rtsp_st = st->priv_data;
  413. sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
  414. } else if (av_strstart(p, "fmtp:", &p)) {
  415. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  416. get_word(buf1, sizeof(buf1), &p);
  417. payload_type = atoi(buf1);
  418. for(i = 0; i < s->nb_streams;i++) {
  419. st = s->streams[i];
  420. rtsp_st = st->priv_data;
  421. if (rtsp_st->sdp_payload_type == payload_type) {
  422. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  423. if(!rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf)) {
  424. sdp_parse_fmtp(st, p);
  425. }
  426. } else {
  427. sdp_parse_fmtp(st, p);
  428. }
  429. }
  430. }
  431. } else if(av_strstart(p, "framesize:", &p)) {
  432. // let dynamic protocol handlers have a stab at the line.
  433. get_word(buf1, sizeof(buf1), &p);
  434. payload_type = atoi(buf1);
  435. for(i = 0; i < s->nb_streams;i++) {
  436. st = s->streams[i];
  437. rtsp_st = st->priv_data;
  438. if (rtsp_st->sdp_payload_type == payload_type) {
  439. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  440. rtsp_st->dynamic_handler->parse_sdp_a_line(s, i, rtsp_st->dynamic_protocol_context, buf);
  441. }
  442. }
  443. }
  444. } else if(av_strstart(p, "range:", &p)) {
  445. int64_t start, end;
  446. // this is so that seeking on a streamed file can work.
  447. rtsp_parse_range_npt(p, &start, &end);
  448. s->start_time= start;
  449. s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
  450. } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
  451. if (atoi(p) == 1)
  452. rt->transport = RTSP_TRANSPORT_RDT;
  453. } else if (s->nb_streams > 0) {
  454. if (rt->server_type == RTSP_SERVER_REAL)
  455. ff_real_parse_sdp_a_line(s, s->nb_streams - 1, p);
  456. rtsp_st = s->streams[s->nb_streams - 1]->priv_data;
  457. if (rtsp_st->dynamic_handler &&
  458. rtsp_st->dynamic_handler->parse_sdp_a_line)
  459. rtsp_st->dynamic_handler->parse_sdp_a_line(s, s->nb_streams - 1,
  460. rtsp_st->dynamic_protocol_context, buf);
  461. }
  462. break;
  463. }
  464. }
  465. static int sdp_parse(AVFormatContext *s, const char *content)
  466. {
  467. const char *p;
  468. int letter;
  469. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  470. * contain long SDP lines containing complete ASF Headers (several
  471. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  472. * "rulebooks" describing their properties. Therefore, the SDP line
  473. * buffer is large. */
  474. char buf[8192], *q;
  475. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  476. memset(s1, 0, sizeof(SDPParseState));
  477. p = content;
  478. for(;;) {
  479. skip_spaces(&p);
  480. letter = *p;
  481. if (letter == '\0')
  482. break;
  483. p++;
  484. if (*p != '=')
  485. goto next_line;
  486. p++;
  487. /* get the content */
  488. q = buf;
  489. while (*p != '\n' && *p != '\r' && *p != '\0') {
  490. if ((q - buf) < sizeof(buf) - 1)
  491. *q++ = *p;
  492. p++;
  493. }
  494. *q = '\0';
  495. sdp_parse_line(s, s1, letter, buf);
  496. next_line:
  497. while (*p != '\n' && *p != '\0')
  498. p++;
  499. if (*p == '\n')
  500. p++;
  501. }
  502. return 0;
  503. }
  504. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  505. {
  506. const char *p;
  507. int v;
  508. p = *pp;
  509. skip_spaces(&p);
  510. v = strtol(p, (char **)&p, 10);
  511. if (*p == '-') {
  512. p++;
  513. *min_ptr = v;
  514. v = strtol(p, (char **)&p, 10);
  515. *max_ptr = v;
  516. } else {
  517. *min_ptr = v;
  518. *max_ptr = v;
  519. }
  520. *pp = p;
  521. }
  522. /* XXX: only one transport specification is parsed */
  523. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  524. {
  525. char transport_protocol[16];
  526. char profile[16];
  527. char lower_transport[16];
  528. char parameter[16];
  529. RTSPTransportField *th;
  530. char buf[256];
  531. reply->nb_transports = 0;
  532. for(;;) {
  533. skip_spaces(&p);
  534. if (*p == '\0')
  535. break;
  536. th = &reply->transports[reply->nb_transports];
  537. get_word_sep(transport_protocol, sizeof(transport_protocol),
  538. "/", &p);
  539. if (*p == '/')
  540. p++;
  541. if (!strcasecmp (transport_protocol, "rtp")) {
  542. get_word_sep(profile, sizeof(profile), "/;,", &p);
  543. lower_transport[0] = '\0';
  544. /* rtp/avp/<protocol> */
  545. if (*p == '/') {
  546. p++;
  547. get_word_sep(lower_transport, sizeof(lower_transport),
  548. ";,", &p);
  549. }
  550. th->transport = RTSP_TRANSPORT_RTP;
  551. } else if (!strcasecmp (transport_protocol, "x-pn-tng") ||
  552. !strcasecmp (transport_protocol, "x-real-rdt")) {
  553. /* x-pn-tng/<protocol> */
  554. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  555. profile[0] = '\0';
  556. th->transport = RTSP_TRANSPORT_RDT;
  557. }
  558. if (!strcasecmp(lower_transport, "TCP"))
  559. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  560. else
  561. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  562. if (*p == ';')
  563. p++;
  564. /* get each parameter */
  565. while (*p != '\0' && *p != ',') {
  566. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  567. if (!strcmp(parameter, "port")) {
  568. if (*p == '=') {
  569. p++;
  570. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  571. }
  572. } else if (!strcmp(parameter, "client_port")) {
  573. if (*p == '=') {
  574. p++;
  575. rtsp_parse_range(&th->client_port_min,
  576. &th->client_port_max, &p);
  577. }
  578. } else if (!strcmp(parameter, "server_port")) {
  579. if (*p == '=') {
  580. p++;
  581. rtsp_parse_range(&th->server_port_min,
  582. &th->server_port_max, &p);
  583. }
  584. } else if (!strcmp(parameter, "interleaved")) {
  585. if (*p == '=') {
  586. p++;
  587. rtsp_parse_range(&th->interleaved_min,
  588. &th->interleaved_max, &p);
  589. }
  590. } else if (!strcmp(parameter, "multicast")) {
  591. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  592. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  593. } else if (!strcmp(parameter, "ttl")) {
  594. if (*p == '=') {
  595. p++;
  596. th->ttl = strtol(p, (char **)&p, 10);
  597. }
  598. } else if (!strcmp(parameter, "destination")) {
  599. struct in_addr ipaddr;
  600. if (*p == '=') {
  601. p++;
  602. get_word_sep(buf, sizeof(buf), ";,", &p);
  603. if (inet_aton(buf, &ipaddr))
  604. th->destination = ntohl(ipaddr.s_addr);
  605. }
  606. }
  607. while (*p != ';' && *p != '\0' && *p != ',')
  608. p++;
  609. if (*p == ';')
  610. p++;
  611. }
  612. if (*p == ',')
  613. p++;
  614. reply->nb_transports++;
  615. }
  616. }
  617. void rtsp_parse_line(RTSPMessageHeader *reply, const char *buf)
  618. {
  619. const char *p;
  620. /* NOTE: we do case independent match for broken servers */
  621. p = buf;
  622. if (av_stristart(p, "Session:", &p)) {
  623. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  624. } else if (av_stristart(p, "Content-Length:", &p)) {
  625. reply->content_length = strtol(p, NULL, 10);
  626. } else if (av_stristart(p, "Transport:", &p)) {
  627. rtsp_parse_transport(reply, p);
  628. } else if (av_stristart(p, "CSeq:", &p)) {
  629. reply->seq = strtol(p, NULL, 10);
  630. } else if (av_stristart(p, "Range:", &p)) {
  631. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  632. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  633. skip_spaces(&p);
  634. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  635. } else if (av_stristart(p, "Server:", &p)) {
  636. skip_spaces(&p);
  637. av_strlcpy(reply->server, p, sizeof(reply->server));
  638. }
  639. }
  640. static int url_readbuf(URLContext *h, unsigned char *buf, int size)
  641. {
  642. int ret, len;
  643. len = 0;
  644. while (len < size) {
  645. ret = url_read(h, buf+len, size-len);
  646. if (ret < 1)
  647. return ret;
  648. len += ret;
  649. }
  650. return len;
  651. }
  652. /* skip a RTP/TCP interleaved packet */
  653. static void rtsp_skip_packet(AVFormatContext *s)
  654. {
  655. RTSPState *rt = s->priv_data;
  656. int ret, len, len1;
  657. uint8_t buf[1024];
  658. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  659. if (ret != 3)
  660. return;
  661. len = AV_RB16(buf + 1);
  662. #ifdef DEBUG
  663. printf("skipping RTP packet len=%d\n", len);
  664. #endif
  665. /* skip payload */
  666. while (len > 0) {
  667. len1 = len;
  668. if (len1 > sizeof(buf))
  669. len1 = sizeof(buf);
  670. ret = url_readbuf(rt->rtsp_hd, buf, len1);
  671. if (ret != len1)
  672. return;
  673. len -= len1;
  674. }
  675. }
  676. static void rtsp_send_cmd(AVFormatContext *s,
  677. const char *cmd, RTSPMessageHeader *reply,
  678. unsigned char **content_ptr)
  679. {
  680. RTSPState *rt = s->priv_data;
  681. char buf[4096], buf1[1024], *q;
  682. unsigned char ch;
  683. const char *p;
  684. int content_length, line_count;
  685. unsigned char *content = NULL;
  686. memset(reply, 0, sizeof(*reply));
  687. rt->seq++;
  688. av_strlcpy(buf, cmd, sizeof(buf));
  689. snprintf(buf1, sizeof(buf1), "CSeq: %d\r\n", rt->seq);
  690. av_strlcat(buf, buf1, sizeof(buf));
  691. if (rt->session_id[0] != '\0' && !strstr(cmd, "\nIf-Match:")) {
  692. snprintf(buf1, sizeof(buf1), "Session: %s\r\n", rt->session_id);
  693. av_strlcat(buf, buf1, sizeof(buf));
  694. }
  695. av_strlcat(buf, "\r\n", sizeof(buf));
  696. #ifdef DEBUG
  697. printf("Sending:\n%s--\n", buf);
  698. #endif
  699. url_write(rt->rtsp_hd, buf, strlen(buf));
  700. /* parse reply (XXX: use buffers) */
  701. line_count = 0;
  702. rt->last_reply[0] = '\0';
  703. for(;;) {
  704. q = buf;
  705. for(;;) {
  706. if (url_readbuf(rt->rtsp_hd, &ch, 1) != 1)
  707. break;
  708. if (ch == '\n')
  709. break;
  710. if (ch == '$') {
  711. /* XXX: only parse it if first char on line ? */
  712. rtsp_skip_packet(s);
  713. } else if (ch != '\r') {
  714. if ((q - buf) < sizeof(buf) - 1)
  715. *q++ = ch;
  716. }
  717. }
  718. *q = '\0';
  719. #ifdef DEBUG
  720. printf("line='%s'\n", buf);
  721. #endif
  722. /* test if last line */
  723. if (buf[0] == '\0')
  724. break;
  725. p = buf;
  726. if (line_count == 0) {
  727. /* get reply code */
  728. get_word(buf1, sizeof(buf1), &p);
  729. get_word(buf1, sizeof(buf1), &p);
  730. reply->status_code = atoi(buf1);
  731. } else {
  732. rtsp_parse_line(reply, p);
  733. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  734. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  735. }
  736. line_count++;
  737. }
  738. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0')
  739. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  740. content_length = reply->content_length;
  741. if (content_length > 0) {
  742. /* leave some room for a trailing '\0' (useful for simple parsing) */
  743. content = av_malloc(content_length + 1);
  744. (void)url_readbuf(rt->rtsp_hd, content, content_length);
  745. content[content_length] = '\0';
  746. }
  747. if (content_ptr)
  748. *content_ptr = content;
  749. else
  750. av_free(content);
  751. }
  752. /* close and free RTSP streams */
  753. static void rtsp_close_streams(RTSPState *rt)
  754. {
  755. int i;
  756. RTSPStream *rtsp_st;
  757. for(i=0;i<rt->nb_rtsp_streams;i++) {
  758. rtsp_st = rt->rtsp_streams[i];
  759. if (rtsp_st) {
  760. if (rtsp_st->transport_priv) {
  761. if (rt->transport == RTSP_TRANSPORT_RDT)
  762. ff_rdt_parse_close(rtsp_st->transport_priv);
  763. else
  764. rtp_parse_close(rtsp_st->transport_priv);
  765. }
  766. if (rtsp_st->rtp_handle)
  767. url_close(rtsp_st->rtp_handle);
  768. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  769. rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
  770. }
  771. }
  772. av_free(rt->rtsp_streams);
  773. }
  774. static int
  775. rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  776. {
  777. RTSPState *rt = s->priv_data;
  778. AVStream *st = NULL;
  779. /* open the RTP context */
  780. if (rtsp_st->stream_index >= 0)
  781. st = s->streams[rtsp_st->stream_index];
  782. if (!st)
  783. s->ctx_flags |= AVFMTCTX_NOHEADER;
  784. if (rt->transport == RTSP_TRANSPORT_RDT)
  785. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  786. rtsp_st->dynamic_protocol_context,
  787. rtsp_st->dynamic_handler);
  788. else
  789. rtsp_st->transport_priv = rtp_parse_open(s, st, rtsp_st->rtp_handle,
  790. rtsp_st->sdp_payload_type,
  791. &rtsp_st->rtp_payload_data);
  792. if (!rtsp_st->transport_priv) {
  793. return AVERROR(ENOMEM);
  794. } else if (rt->transport != RTSP_TRANSPORT_RDT) {
  795. if(rtsp_st->dynamic_handler) {
  796. rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  797. rtsp_st->dynamic_protocol_context,
  798. rtsp_st->dynamic_handler);
  799. }
  800. }
  801. return 0;
  802. }
  803. /**
  804. * @returns 0 on success, <0 on error, 1 if protocol is unavailable.
  805. */
  806. static int
  807. make_setup_request (AVFormatContext *s, const char *host, int port,
  808. int lower_transport, const char *real_challenge)
  809. {
  810. RTSPState *rt = s->priv_data;
  811. int j, i, err, interleave = 0;
  812. RTSPStream *rtsp_st;
  813. RTSPMessageHeader reply1, *reply = &reply1;
  814. char cmd[2048];
  815. const char *trans_pref;
  816. if (rt->transport == RTSP_TRANSPORT_RDT)
  817. trans_pref = "x-pn-tng";
  818. else
  819. trans_pref = "RTP/AVP";
  820. /* for each stream, make the setup request */
  821. /* XXX: we assume the same server is used for the control of each
  822. RTSP stream */
  823. for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  824. char transport[2048];
  825. rtsp_st = rt->rtsp_streams[i];
  826. /* RTP/UDP */
  827. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  828. char buf[256];
  829. /* first try in specified port range */
  830. if (RTSP_RTP_PORT_MIN != 0) {
  831. while(j <= RTSP_RTP_PORT_MAX) {
  832. snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
  833. j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
  834. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
  835. goto rtp_opened;
  836. }
  837. }
  838. }
  839. /* then try on any port
  840. ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  841. ** err = AVERROR_INVALIDDATA;
  842. ** goto fail;
  843. ** }
  844. */
  845. rtp_opened:
  846. port = rtp_get_local_port(rtsp_st->rtp_handle);
  847. snprintf(transport, sizeof(transport) - 1,
  848. "%s/UDP;", trans_pref);
  849. if (rt->server_type != RTSP_SERVER_REAL)
  850. av_strlcat(transport, "unicast;", sizeof(transport));
  851. av_strlcatf(transport, sizeof(transport),
  852. "client_port=%d", port);
  853. if (rt->transport == RTSP_TRANSPORT_RTP)
  854. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  855. }
  856. /* RTP/TCP */
  857. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  858. snprintf(transport, sizeof(transport) - 1,
  859. "%s/TCP;", trans_pref);
  860. if (rt->server_type == RTSP_SERVER_WMS)
  861. av_strlcat(transport, "unicast;", sizeof(transport));
  862. av_strlcatf(transport, sizeof(transport),
  863. "interleaved=%d-%d",
  864. interleave, interleave + 1);
  865. interleave += 2;
  866. }
  867. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  868. snprintf(transport, sizeof(transport) - 1,
  869. "%s/UDP;multicast", trans_pref);
  870. }
  871. if (rt->server_type == RTSP_SERVER_REAL ||
  872. rt->server_type == RTSP_SERVER_WMS)
  873. av_strlcat(transport, ";mode=play", sizeof(transport));
  874. snprintf(cmd, sizeof(cmd),
  875. "SETUP %s RTSP/1.0\r\n"
  876. "Transport: %s\r\n",
  877. rtsp_st->control_url, transport);
  878. if (i == 0 && rt->server_type == RTSP_SERVER_REAL) {
  879. char real_res[41], real_csum[9];
  880. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  881. real_challenge);
  882. av_strlcatf(cmd, sizeof(cmd),
  883. "If-Match: %s\r\n"
  884. "RealChallenge2: %s, sd=%s\r\n",
  885. rt->session_id, real_res, real_csum);
  886. }
  887. rtsp_send_cmd(s, cmd, reply, NULL);
  888. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  889. err = 1;
  890. goto fail;
  891. } else if (reply->status_code != RTSP_STATUS_OK ||
  892. reply->nb_transports != 1) {
  893. err = AVERROR_INVALIDDATA;
  894. goto fail;
  895. }
  896. /* XXX: same protocol for all streams is required */
  897. if (i > 0) {
  898. if (reply->transports[0].lower_transport != rt->lower_transport ||
  899. reply->transports[0].transport != rt->transport) {
  900. err = AVERROR_INVALIDDATA;
  901. goto fail;
  902. }
  903. } else {
  904. rt->lower_transport = reply->transports[0].lower_transport;
  905. rt->transport = reply->transports[0].transport;
  906. }
  907. /* close RTP connection if not choosen */
  908. if (reply->transports[0].lower_transport != RTSP_LOWER_TRANSPORT_UDP &&
  909. (lower_transport == RTSP_LOWER_TRANSPORT_UDP)) {
  910. url_close(rtsp_st->rtp_handle);
  911. rtsp_st->rtp_handle = NULL;
  912. }
  913. switch(reply->transports[0].lower_transport) {
  914. case RTSP_LOWER_TRANSPORT_TCP:
  915. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  916. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  917. break;
  918. case RTSP_LOWER_TRANSPORT_UDP:
  919. {
  920. char url[1024];
  921. /* XXX: also use address if specified */
  922. snprintf(url, sizeof(url), "rtp://%s:%d",
  923. host, reply->transports[0].server_port_min);
  924. if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  925. err = AVERROR_INVALIDDATA;
  926. goto fail;
  927. }
  928. }
  929. break;
  930. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  931. {
  932. char url[1024];
  933. struct in_addr in;
  934. in.s_addr = htonl(reply->transports[0].destination);
  935. snprintf(url, sizeof(url), "rtp://%s:%d?ttl=%d",
  936. inet_ntoa(in),
  937. reply->transports[0].port_min,
  938. reply->transports[0].ttl);
  939. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  940. err = AVERROR_INVALIDDATA;
  941. goto fail;
  942. }
  943. }
  944. break;
  945. }
  946. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  947. goto fail;
  948. }
  949. if (rt->server_type == RTSP_SERVER_REAL)
  950. rt->need_subscription = 1;
  951. return 0;
  952. fail:
  953. for (i=0; i<rt->nb_rtsp_streams; i++) {
  954. if (rt->rtsp_streams[i]->rtp_handle) {
  955. url_close(rt->rtsp_streams[i]->rtp_handle);
  956. rt->rtsp_streams[i]->rtp_handle = NULL;
  957. }
  958. }
  959. return err;
  960. }
  961. static int rtsp_read_header(AVFormatContext *s,
  962. AVFormatParameters *ap)
  963. {
  964. RTSPState *rt = s->priv_data;
  965. char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
  966. URLContext *rtsp_hd;
  967. int port, ret, err;
  968. RTSPMessageHeader reply1, *reply = &reply1;
  969. unsigned char *content = NULL;
  970. int lower_transport_mask = 0;
  971. char real_challenge[64];
  972. /* extract hostname and port */
  973. url_split(NULL, 0, NULL, 0,
  974. host, sizeof(host), &port, path, sizeof(path), s->filename);
  975. if (port < 0)
  976. port = RTSP_DEFAULT_PORT;
  977. /* search for options */
  978. option_list = strchr(path, '?');
  979. if (option_list) {
  980. /* remove the options from the path */
  981. *option_list++ = 0;
  982. while(option_list) {
  983. /* move the option pointer */
  984. option = option_list;
  985. option_list = strchr(option_list, '&');
  986. if (option_list)
  987. *(option_list++) = 0;
  988. /* handle the options */
  989. if (strcmp(option, "udp") == 0)
  990. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP);
  991. else if (strcmp(option, "multicast") == 0)
  992. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_UDP_MULTICAST);
  993. else if (strcmp(option, "tcp") == 0)
  994. lower_transport_mask = (1<< RTSP_LOWER_TRANSPORT_TCP);
  995. }
  996. }
  997. if (!lower_transport_mask)
  998. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  999. /* open the tcp connexion */
  1000. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  1001. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  1002. return AVERROR(EIO);
  1003. rt->rtsp_hd = rtsp_hd;
  1004. rt->seq = 0;
  1005. /* request options supported by the server; this also detects server type */
  1006. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1007. snprintf(cmd, sizeof(cmd),
  1008. "OPTIONS %s RTSP/1.0\r\n", s->filename);
  1009. if (rt->server_type == RTSP_SERVER_REAL)
  1010. av_strlcat(cmd,
  1011. /**
  1012. * The following entries are required for proper
  1013. * streaming from a Realmedia server. They are
  1014. * interdependent in some way although we currently
  1015. * don't quite understand how. Values were copied
  1016. * from mplayer SVN r23589.
  1017. * @param CompanyID is a 16-byte ID in base64
  1018. * @param ClientChallenge is a 16-byte ID in hex
  1019. */
  1020. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1021. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1022. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1023. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1024. sizeof(cmd));
  1025. rtsp_send_cmd(s, cmd, reply, NULL);
  1026. if (reply->status_code != RTSP_STATUS_OK) {
  1027. err = AVERROR_INVALIDDATA;
  1028. goto fail;
  1029. }
  1030. /* detect server type if not standard-compliant RTP */
  1031. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1032. rt->server_type = RTSP_SERVER_REAL;
  1033. continue;
  1034. } else if (!strncasecmp(reply->server, "WMServer/", 9)) {
  1035. rt->server_type = RTSP_SERVER_WMS;
  1036. } else if (rt->server_type == RTSP_SERVER_REAL) {
  1037. strcpy(real_challenge, reply->real_challenge);
  1038. }
  1039. break;
  1040. }
  1041. /* describe the stream */
  1042. snprintf(cmd, sizeof(cmd),
  1043. "DESCRIBE %s RTSP/1.0\r\n"
  1044. "Accept: application/sdp\r\n",
  1045. s->filename);
  1046. if (rt->server_type == RTSP_SERVER_REAL) {
  1047. /**
  1048. * The Require: attribute is needed for proper streaming from
  1049. * Realmedia servers.
  1050. */
  1051. av_strlcat(cmd,
  1052. "Require: com.real.retain-entity-for-setup\r\n",
  1053. sizeof(cmd));
  1054. }
  1055. rtsp_send_cmd(s, cmd, reply, &content);
  1056. if (!content) {
  1057. err = AVERROR_INVALIDDATA;
  1058. goto fail;
  1059. }
  1060. if (reply->status_code != RTSP_STATUS_OK) {
  1061. err = AVERROR_INVALIDDATA;
  1062. goto fail;
  1063. }
  1064. /* now we got the SDP description, we parse it */
  1065. ret = sdp_parse(s, (const char *)content);
  1066. av_freep(&content);
  1067. if (ret < 0) {
  1068. err = AVERROR_INVALIDDATA;
  1069. goto fail;
  1070. }
  1071. do {
  1072. int lower_transport = ff_log2_tab[lower_transport_mask & ~(lower_transport_mask - 1)];
  1073. err = make_setup_request(s, host, port, lower_transport,
  1074. rt->server_type == RTSP_SERVER_REAL ?
  1075. real_challenge : NULL);
  1076. if (err < 0)
  1077. goto fail;
  1078. lower_transport_mask &= ~(1 << lower_transport);
  1079. if (lower_transport_mask == 0 && err == 1) {
  1080. err = AVERROR(FF_NETERROR(EPROTONOSUPPORT));
  1081. goto fail;
  1082. }
  1083. } while (err);
  1084. rt->state = RTSP_STATE_IDLE;
  1085. rt->seek_timestamp = 0; /* default is to start stream at position
  1086. zero */
  1087. if (ap->initial_pause) {
  1088. /* do not start immediately */
  1089. } else {
  1090. if (rtsp_read_play(s) < 0) {
  1091. err = AVERROR_INVALIDDATA;
  1092. goto fail;
  1093. }
  1094. }
  1095. return 0;
  1096. fail:
  1097. rtsp_close_streams(rt);
  1098. av_freep(&content);
  1099. url_close(rt->rtsp_hd);
  1100. return err;
  1101. }
  1102. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1103. uint8_t *buf, int buf_size)
  1104. {
  1105. RTSPState *rt = s->priv_data;
  1106. int id, len, i, ret;
  1107. RTSPStream *rtsp_st;
  1108. #ifdef DEBUG_RTP_TCP
  1109. printf("tcp_read_packet:\n");
  1110. #endif
  1111. redo:
  1112. for(;;) {
  1113. ret = url_readbuf(rt->rtsp_hd, buf, 1);
  1114. #ifdef DEBUG_RTP_TCP
  1115. printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
  1116. #endif
  1117. if (ret != 1)
  1118. return -1;
  1119. if (buf[0] == '$')
  1120. break;
  1121. }
  1122. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  1123. if (ret != 3)
  1124. return -1;
  1125. id = buf[0];
  1126. len = AV_RB16(buf + 1);
  1127. #ifdef DEBUG_RTP_TCP
  1128. printf("id=%d len=%d\n", id, len);
  1129. #endif
  1130. if (len > buf_size || len < 12)
  1131. goto redo;
  1132. /* get the data */
  1133. ret = url_readbuf(rt->rtsp_hd, buf, len);
  1134. if (ret != len)
  1135. return -1;
  1136. if (rt->transport == RTSP_TRANSPORT_RDT &&
  1137. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  1138. return -1;
  1139. /* find the matching stream */
  1140. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1141. rtsp_st = rt->rtsp_streams[i];
  1142. if (id >= rtsp_st->interleaved_min &&
  1143. id <= rtsp_st->interleaved_max)
  1144. goto found;
  1145. }
  1146. goto redo;
  1147. found:
  1148. *prtsp_st = rtsp_st;
  1149. return len;
  1150. }
  1151. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1152. uint8_t *buf, int buf_size)
  1153. {
  1154. RTSPState *rt = s->priv_data;
  1155. RTSPStream *rtsp_st;
  1156. fd_set rfds;
  1157. int fd1, fd2, fd_max, n, i, ret;
  1158. struct timeval tv;
  1159. for(;;) {
  1160. if (url_interrupt_cb())
  1161. return AVERROR(EINTR);
  1162. FD_ZERO(&rfds);
  1163. fd_max = -1;
  1164. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1165. rtsp_st = rt->rtsp_streams[i];
  1166. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  1167. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1168. if (fd1 > fd_max)
  1169. fd_max = fd1;
  1170. FD_SET(fd1, &rfds);
  1171. }
  1172. tv.tv_sec = 0;
  1173. tv.tv_usec = 100 * 1000;
  1174. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1175. if (n > 0) {
  1176. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1177. rtsp_st = rt->rtsp_streams[i];
  1178. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1179. if (FD_ISSET(fd1, &rfds)) {
  1180. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1181. if (ret > 0) {
  1182. *prtsp_st = rtsp_st;
  1183. return ret;
  1184. }
  1185. }
  1186. }
  1187. }
  1188. }
  1189. }
  1190. static int rtsp_read_packet(AVFormatContext *s,
  1191. AVPacket *pkt)
  1192. {
  1193. RTSPState *rt = s->priv_data;
  1194. RTSPStream *rtsp_st;
  1195. int ret, len;
  1196. uint8_t buf[10 * RTP_MAX_PACKET_LENGTH];
  1197. if (rt->server_type == RTSP_SERVER_REAL) {
  1198. int i;
  1199. RTSPMessageHeader reply1, *reply = &reply1;
  1200. enum AVDiscard cache[MAX_STREAMS];
  1201. char cmd[1024];
  1202. for (i = 0; i < s->nb_streams; i++)
  1203. cache[i] = s->streams[i]->discard;
  1204. if (!rt->need_subscription) {
  1205. if (memcmp (cache, rt->real_setup_cache,
  1206. sizeof(enum AVDiscard) * s->nb_streams)) {
  1207. av_strlcatf(cmd, sizeof(cmd),
  1208. "SET_PARAMETER %s RTSP/1.0\r\n"
  1209. "Unsubscribe: %s\r\n",
  1210. s->filename, rt->last_subscription);
  1211. rtsp_send_cmd(s, cmd, reply, NULL);
  1212. if (reply->status_code != RTSP_STATUS_OK)
  1213. return AVERROR_INVALIDDATA;
  1214. rt->need_subscription = 1;
  1215. }
  1216. }
  1217. if (rt->need_subscription) {
  1218. int r, rule_nr, first = 1;
  1219. memcpy(rt->real_setup_cache, cache,
  1220. sizeof(enum AVDiscard) * s->nb_streams);
  1221. rt->last_subscription[0] = 0;
  1222. snprintf(cmd, sizeof(cmd),
  1223. "SET_PARAMETER %s RTSP/1.0\r\n"
  1224. "Subscribe: ",
  1225. s->filename);
  1226. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1227. rule_nr = 0;
  1228. for (r = 0; r < s->nb_streams; r++) {
  1229. if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
  1230. if (s->streams[r]->discard != AVDISCARD_ALL) {
  1231. if (!first)
  1232. av_strlcat(rt->last_subscription, ",",
  1233. sizeof(rt->last_subscription));
  1234. ff_rdt_subscribe_rule(
  1235. rt->last_subscription,
  1236. sizeof(rt->last_subscription), i, rule_nr);
  1237. first = 0;
  1238. }
  1239. rule_nr++;
  1240. }
  1241. }
  1242. }
  1243. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  1244. rtsp_send_cmd(s, cmd, reply, NULL);
  1245. if (reply->status_code != RTSP_STATUS_OK)
  1246. return AVERROR_INVALIDDATA;
  1247. rt->need_subscription = 0;
  1248. if (rt->state == RTSP_STATE_PLAYING)
  1249. rtsp_read_play (s);
  1250. }
  1251. }
  1252. /* get next frames from the same RTP packet */
  1253. if (rt->cur_transport_priv) {
  1254. if (rt->transport == RTSP_TRANSPORT_RDT)
  1255. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1256. else
  1257. ret = rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1258. if (ret == 0) {
  1259. rt->cur_transport_priv = NULL;
  1260. return 0;
  1261. } else if (ret == 1) {
  1262. return 0;
  1263. } else {
  1264. rt->cur_transport_priv = NULL;
  1265. }
  1266. }
  1267. /* read next RTP packet */
  1268. redo:
  1269. switch(rt->lower_transport) {
  1270. default:
  1271. case RTSP_LOWER_TRANSPORT_TCP:
  1272. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1273. break;
  1274. case RTSP_LOWER_TRANSPORT_UDP:
  1275. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1276. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1277. if (len >=0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1278. rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
  1279. break;
  1280. }
  1281. if (len < 0)
  1282. return len;
  1283. if (rt->transport == RTSP_TRANSPORT_RDT)
  1284. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
  1285. else
  1286. ret = rtp_parse_packet(rtsp_st->transport_priv, pkt, buf, len);
  1287. if (ret < 0)
  1288. goto redo;
  1289. if (ret == 1) {
  1290. /* more packets may follow, so we save the RTP context */
  1291. rt->cur_transport_priv = rtsp_st->transport_priv;
  1292. }
  1293. return 0;
  1294. }
  1295. static int rtsp_read_play(AVFormatContext *s)
  1296. {
  1297. RTSPState *rt = s->priv_data;
  1298. RTSPMessageHeader reply1, *reply = &reply1;
  1299. char cmd[1024];
  1300. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1301. if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1302. if (rt->state == RTSP_STATE_PAUSED) {
  1303. snprintf(cmd, sizeof(cmd),
  1304. "PLAY %s RTSP/1.0\r\n",
  1305. s->filename);
  1306. } else {
  1307. snprintf(cmd, sizeof(cmd),
  1308. "PLAY %s RTSP/1.0\r\n"
  1309. "Range: npt=%0.3f-\r\n",
  1310. s->filename,
  1311. (double)rt->seek_timestamp / AV_TIME_BASE);
  1312. }
  1313. rtsp_send_cmd(s, cmd, reply, NULL);
  1314. if (reply->status_code != RTSP_STATUS_OK) {
  1315. return -1;
  1316. }
  1317. }
  1318. rt->state = RTSP_STATE_PLAYING;
  1319. return 0;
  1320. }
  1321. /* pause the stream */
  1322. static int rtsp_read_pause(AVFormatContext *s)
  1323. {
  1324. RTSPState *rt = s->priv_data;
  1325. RTSPMessageHeader reply1, *reply = &reply1;
  1326. char cmd[1024];
  1327. rt = s->priv_data;
  1328. if (rt->state != RTSP_STATE_PLAYING)
  1329. return 0;
  1330. else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  1331. snprintf(cmd, sizeof(cmd),
  1332. "PAUSE %s RTSP/1.0\r\n",
  1333. s->filename);
  1334. rtsp_send_cmd(s, cmd, reply, NULL);
  1335. if (reply->status_code != RTSP_STATUS_OK) {
  1336. return -1;
  1337. }
  1338. }
  1339. rt->state = RTSP_STATE_PAUSED;
  1340. return 0;
  1341. }
  1342. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1343. int64_t timestamp, int flags)
  1344. {
  1345. RTSPState *rt = s->priv_data;
  1346. rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
  1347. switch(rt->state) {
  1348. default:
  1349. case RTSP_STATE_IDLE:
  1350. break;
  1351. case RTSP_STATE_PLAYING:
  1352. if (rtsp_read_play(s) != 0)
  1353. return -1;
  1354. break;
  1355. case RTSP_STATE_PAUSED:
  1356. rt->state = RTSP_STATE_IDLE;
  1357. break;
  1358. }
  1359. return 0;
  1360. }
  1361. static int rtsp_read_close(AVFormatContext *s)
  1362. {
  1363. RTSPState *rt = s->priv_data;
  1364. RTSPMessageHeader reply1, *reply = &reply1;
  1365. char cmd[1024];
  1366. #if 0
  1367. /* NOTE: it is valid to flush the buffer here */
  1368. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  1369. url_fclose(&rt->rtsp_gb);
  1370. }
  1371. #endif
  1372. snprintf(cmd, sizeof(cmd),
  1373. "TEARDOWN %s RTSP/1.0\r\n",
  1374. s->filename);
  1375. rtsp_send_cmd(s, cmd, reply, NULL);
  1376. rtsp_close_streams(rt);
  1377. url_close(rt->rtsp_hd);
  1378. return 0;
  1379. }
  1380. #if CONFIG_RTSP_DEMUXER
  1381. AVInputFormat rtsp_demuxer = {
  1382. "rtsp",
  1383. NULL_IF_CONFIG_SMALL("RTSP input format"),
  1384. sizeof(RTSPState),
  1385. rtsp_probe,
  1386. rtsp_read_header,
  1387. rtsp_read_packet,
  1388. rtsp_read_close,
  1389. rtsp_read_seek,
  1390. .flags = AVFMT_NOFILE,
  1391. .read_play = rtsp_read_play,
  1392. .read_pause = rtsp_read_pause,
  1393. };
  1394. #endif
  1395. static int sdp_probe(AVProbeData *p1)
  1396. {
  1397. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1398. /* we look for a line beginning "c=IN IP4" */
  1399. while (p < p_end && *p != '\0') {
  1400. if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
  1401. return AVPROBE_SCORE_MAX / 2;
  1402. while(p < p_end - 1 && *p != '\n') p++;
  1403. if (++p >= p_end)
  1404. break;
  1405. if (*p == '\r')
  1406. p++;
  1407. }
  1408. return 0;
  1409. }
  1410. #define SDP_MAX_SIZE 8192
  1411. static int sdp_read_header(AVFormatContext *s,
  1412. AVFormatParameters *ap)
  1413. {
  1414. RTSPState *rt = s->priv_data;
  1415. RTSPStream *rtsp_st;
  1416. int size, i, err;
  1417. char *content;
  1418. char url[1024];
  1419. /* read the whole sdp file */
  1420. /* XXX: better loading */
  1421. content = av_malloc(SDP_MAX_SIZE);
  1422. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1423. if (size <= 0) {
  1424. av_free(content);
  1425. return AVERROR_INVALIDDATA;
  1426. }
  1427. content[size] ='\0';
  1428. sdp_parse(s, content);
  1429. av_free(content);
  1430. /* open each RTP stream */
  1431. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1432. rtsp_st = rt->rtsp_streams[i];
  1433. snprintf(url, sizeof(url), "rtp://%s:%d?localport=%d&ttl=%d",
  1434. inet_ntoa(rtsp_st->sdp_ip),
  1435. rtsp_st->sdp_port,
  1436. rtsp_st->sdp_port,
  1437. rtsp_st->sdp_ttl);
  1438. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1439. err = AVERROR_INVALIDDATA;
  1440. goto fail;
  1441. }
  1442. if ((err = rtsp_open_transport_ctx(s, rtsp_st)))
  1443. goto fail;
  1444. }
  1445. return 0;
  1446. fail:
  1447. rtsp_close_streams(rt);
  1448. return err;
  1449. }
  1450. static int sdp_read_packet(AVFormatContext *s,
  1451. AVPacket *pkt)
  1452. {
  1453. return rtsp_read_packet(s, pkt);
  1454. }
  1455. static int sdp_read_close(AVFormatContext *s)
  1456. {
  1457. RTSPState *rt = s->priv_data;
  1458. rtsp_close_streams(rt);
  1459. return 0;
  1460. }
  1461. #if CONFIG_SDP_DEMUXER
  1462. AVInputFormat sdp_demuxer = {
  1463. "sdp",
  1464. NULL_IF_CONFIG_SMALL("SDP"),
  1465. sizeof(RTSPState),
  1466. sdp_probe,
  1467. sdp_read_header,
  1468. sdp_read_packet,
  1469. sdp_read_close,
  1470. };
  1471. #endif
  1472. #if CONFIG_REDIR_DEMUXER
  1473. /* dummy redirector format (used directly in av_open_input_file now) */
  1474. static int redir_probe(AVProbeData *pd)
  1475. {
  1476. const char *p;
  1477. p = pd->buf;
  1478. while (redir_isspace(*p))
  1479. p++;
  1480. if (av_strstart(p, "http://", NULL) ||
  1481. av_strstart(p, "rtsp://", NULL))
  1482. return AVPROBE_SCORE_MAX;
  1483. return 0;
  1484. }
  1485. static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1486. {
  1487. char buf[4096], *q;
  1488. int c;
  1489. AVFormatContext *ic = NULL;
  1490. ByteIOContext *f = s->pb;
  1491. /* parse each URL and try to open it */
  1492. c = url_fgetc(f);
  1493. while (c != URL_EOF) {
  1494. /* skip spaces */
  1495. for(;;) {
  1496. if (!redir_isspace(c))
  1497. break;
  1498. c = url_fgetc(f);
  1499. }
  1500. if (c == URL_EOF)
  1501. break;
  1502. /* record url */
  1503. q = buf;
  1504. for(;;) {
  1505. if (c == URL_EOF || redir_isspace(c))
  1506. break;
  1507. if ((q - buf) < sizeof(buf) - 1)
  1508. *q++ = c;
  1509. c = url_fgetc(f);
  1510. }
  1511. *q = '\0';
  1512. //printf("URL='%s'\n", buf);
  1513. /* try to open the media file */
  1514. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1515. break;
  1516. }
  1517. if (!ic)
  1518. return AVERROR(EIO);
  1519. *s = *ic;
  1520. url_fclose(f);
  1521. return 0;
  1522. }
  1523. AVInputFormat redir_demuxer = {
  1524. "redir",
  1525. NULL_IF_CONFIG_SMALL("Redirector format"),
  1526. 0,
  1527. redir_probe,
  1528. redir_read_header,
  1529. NULL,
  1530. NULL,
  1531. };
  1532. #endif