rtsp.c 61 KB

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