rtsp.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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. #include "avformat.h"
  22. #include <sys/time.h>
  23. #include <unistd.h> /* for select() prototype */
  24. #include "network.h"
  25. #include "avstring.h"
  26. #include "rtsp.h"
  27. #include "rtp_internal.h"
  28. //#define DEBUG
  29. //#define DEBUG_RTP_TCP
  30. enum RTSPClientState {
  31. RTSP_STATE_IDLE,
  32. RTSP_STATE_PLAYING,
  33. RTSP_STATE_PAUSED,
  34. };
  35. typedef struct RTSPState {
  36. URLContext *rtsp_hd; /* RTSP TCP connexion handle */
  37. int nb_rtsp_streams;
  38. struct RTSPStream **rtsp_streams;
  39. enum RTSPClientState state;
  40. int64_t seek_timestamp;
  41. /* XXX: currently we use unbuffered input */
  42. // ByteIOContext rtsp_gb;
  43. int seq; /* RTSP command sequence number */
  44. char session_id[512];
  45. enum RTSPProtocol protocol;
  46. char last_reply[2048]; /* XXX: allocate ? */
  47. RTPDemuxContext *cur_rtp;
  48. } RTSPState;
  49. typedef struct RTSPStream {
  50. URLContext *rtp_handle; /* RTP stream handle */
  51. RTPDemuxContext *rtp_ctx; /* RTP parse context */
  52. int stream_index; /* corresponding stream index, if any. -1 if none (MPEG2TS case) */
  53. int interleaved_min, interleaved_max; /* interleave ids, if TCP transport */
  54. char control_url[1024]; /* url for this stream (from SDP) */
  55. int sdp_port; /* port (from SDP content - not used in RTSP) */
  56. struct in_addr sdp_ip; /* IP address (from SDP content - not used in RTSP) */
  57. int sdp_ttl; /* IP TTL (from SDP content - not used in RTSP) */
  58. int sdp_payload_type; /* payload type - only used in SDP */
  59. rtp_payload_data_t rtp_payload_data; /* rtp payload parsing infos from SDP */
  60. RTPDynamicProtocolHandler *dynamic_handler; ///< Only valid if it's a dynamic protocol. (This is the handler structure)
  61. void *dynamic_protocol_context; ///< Only valid if it's a dynamic protocol. (This is any private data associated with the dynamic protocol)
  62. } RTSPStream;
  63. static int rtsp_read_play(AVFormatContext *s);
  64. /* XXX: currently, the only way to change the protocols consists in
  65. changing this variable */
  66. #if LIBAVFORMAT_VERSION_INT < (53 << 16)
  67. int rtsp_default_protocols = (1 << RTSP_PROTOCOL_RTP_UDP);
  68. #endif
  69. static int rtsp_probe(AVProbeData *p)
  70. {
  71. if (av_strstart(p->filename, "rtsp:", NULL))
  72. return AVPROBE_SCORE_MAX;
  73. return 0;
  74. }
  75. static int redir_isspace(int c)
  76. {
  77. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  78. }
  79. static void skip_spaces(const char **pp)
  80. {
  81. const char *p;
  82. p = *pp;
  83. while (redir_isspace(*p))
  84. p++;
  85. *pp = p;
  86. }
  87. static void get_word_sep(char *buf, int buf_size, const char *sep,
  88. const char **pp)
  89. {
  90. const char *p;
  91. char *q;
  92. p = *pp;
  93. if (*p == '/')
  94. p++;
  95. skip_spaces(&p);
  96. q = buf;
  97. while (!strchr(sep, *p) && *p != '\0') {
  98. if ((q - buf) < buf_size - 1)
  99. *q++ = *p;
  100. p++;
  101. }
  102. if (buf_size > 0)
  103. *q = '\0';
  104. *pp = p;
  105. }
  106. static void get_word(char *buf, int buf_size, const char **pp)
  107. {
  108. const char *p;
  109. char *q;
  110. p = *pp;
  111. skip_spaces(&p);
  112. q = buf;
  113. while (!redir_isspace(*p) && *p != '\0') {
  114. if ((q - buf) < buf_size - 1)
  115. *q++ = *p;
  116. p++;
  117. }
  118. if (buf_size > 0)
  119. *q = '\0';
  120. *pp = p;
  121. }
  122. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other
  123. params>] */
  124. static int sdp_parse_rtpmap(AVCodecContext *codec, RTSPStream *rtsp_st, int payload_type, const char *p)
  125. {
  126. char buf[256];
  127. int i;
  128. AVCodec *c;
  129. const char *c_name;
  130. /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
  131. see if we can handle this kind of payload */
  132. get_word_sep(buf, sizeof(buf), "/", &p);
  133. if (payload_type >= RTP_PT_PRIVATE) {
  134. RTPDynamicProtocolHandler *handler= RTPFirstDynamicPayloadHandler;
  135. while(handler) {
  136. if (!strcmp(buf, handler->enc_name) && (codec->codec_type == handler->codec_type)) {
  137. codec->codec_id = handler->codec_id;
  138. rtsp_st->dynamic_handler= handler;
  139. if(handler->open) {
  140. rtsp_st->dynamic_protocol_context= handler->open();
  141. }
  142. break;
  143. }
  144. handler= handler->next;
  145. }
  146. } else {
  147. /* We are in a standard case ( from http://www.iana.org/assignments/rtp-parameters) */
  148. /* search into AVRtpPayloadTypes[] */
  149. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  150. }
  151. c = avcodec_find_decoder(codec->codec_id);
  152. if (c && c->name)
  153. c_name = c->name;
  154. else
  155. c_name = (char *)NULL;
  156. if (c_name) {
  157. get_word_sep(buf, sizeof(buf), "/", &p);
  158. i = atoi(buf);
  159. switch (codec->codec_type) {
  160. case CODEC_TYPE_AUDIO:
  161. av_log(codec, AV_LOG_DEBUG, " audio codec set to : %s\n", c_name);
  162. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  163. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  164. if (i > 0) {
  165. codec->sample_rate = i;
  166. get_word_sep(buf, sizeof(buf), "/", &p);
  167. i = atoi(buf);
  168. if (i > 0)
  169. codec->channels = i;
  170. // TODO: there is a bug here; if it is a mono stream, and less than 22000Hz, faad upconverts to stereo and twice the
  171. // frequency. No problem, but the sample rate is being set here by the sdp line. Upcoming patch forthcoming. (rdm)
  172. }
  173. av_log(codec, AV_LOG_DEBUG, " audio samplerate set to : %i\n", codec->sample_rate);
  174. av_log(codec, AV_LOG_DEBUG, " audio channels set to : %i\n", codec->channels);
  175. break;
  176. case CODEC_TYPE_VIDEO:
  177. av_log(codec, AV_LOG_DEBUG, " video codec set to : %s\n", c_name);
  178. break;
  179. default:
  180. break;
  181. }
  182. return 0;
  183. }
  184. return -1;
  185. }
  186. /* return the length and optionnaly the data */
  187. static int hex_to_data(uint8_t *data, const char *p)
  188. {
  189. int c, len, v;
  190. len = 0;
  191. v = 1;
  192. for(;;) {
  193. skip_spaces(&p);
  194. if (p == '\0')
  195. break;
  196. c = toupper((unsigned char)*p++);
  197. if (c >= '0' && c <= '9')
  198. c = c - '0';
  199. else if (c >= 'A' && c <= 'F')
  200. c = c - 'A' + 10;
  201. else
  202. break;
  203. v = (v << 4) | c;
  204. if (v & 0x100) {
  205. if (data)
  206. data[len] = v;
  207. len++;
  208. v = 1;
  209. }
  210. }
  211. return len;
  212. }
  213. static void sdp_parse_fmtp_config(AVCodecContext *codec, char *attr, char *value)
  214. {
  215. switch (codec->codec_id) {
  216. case CODEC_ID_MPEG4:
  217. case CODEC_ID_AAC:
  218. if (!strcmp(attr, "config")) {
  219. /* decode the hexa encoded parameter */
  220. int len = hex_to_data(NULL, value);
  221. codec->extradata = av_mallocz(len + FF_INPUT_BUFFER_PADDING_SIZE);
  222. if (!codec->extradata)
  223. return;
  224. codec->extradata_size = len;
  225. hex_to_data(codec->extradata, value);
  226. }
  227. break;
  228. default:
  229. break;
  230. }
  231. return;
  232. }
  233. typedef struct attrname_map
  234. {
  235. const char *str;
  236. uint16_t type;
  237. uint32_t offset;
  238. } attrname_map_t;
  239. /* All known fmtp parmeters and the corresping RTPAttrTypeEnum */
  240. #define ATTR_NAME_TYPE_INT 0
  241. #define ATTR_NAME_TYPE_STR 1
  242. static attrname_map_t attr_names[]=
  243. {
  244. {"SizeLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, sizelength)},
  245. {"IndexLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexlength)},
  246. {"IndexDeltaLength", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, indexdeltalength)},
  247. {"profile-level-id", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, profile_level_id)},
  248. {"StreamType", ATTR_NAME_TYPE_INT, offsetof(rtp_payload_data_t, streamtype)},
  249. {"mode", ATTR_NAME_TYPE_STR, offsetof(rtp_payload_data_t, mode)},
  250. {NULL, -1, -1},
  251. };
  252. /** parse the attribute line from the fmtp a line of an sdp resonse. This is broken out as a function
  253. * because it is used in rtp_h264.c, which is forthcoming.
  254. */
  255. int rtsp_next_attr_and_value(const char **p, char *attr, int attr_size, char *value, int value_size)
  256. {
  257. skip_spaces(p);
  258. if(**p)
  259. {
  260. get_word_sep(attr, attr_size, "=", p);
  261. if (**p == '=')
  262. (*p)++;
  263. get_word_sep(value, value_size, ";", p);
  264. if (**p == ';')
  265. (*p)++;
  266. return 1;
  267. }
  268. return 0;
  269. }
  270. /* parse a SDP line and save stream attributes */
  271. static void sdp_parse_fmtp(AVStream *st, const char *p)
  272. {
  273. char attr[256];
  274. char value[4096];
  275. int i;
  276. RTSPStream *rtsp_st = st->priv_data;
  277. AVCodecContext *codec = st->codec;
  278. rtp_payload_data_t *rtp_payload_data = &rtsp_st->rtp_payload_data;
  279. /* loop on each attribute */
  280. while(rtsp_next_attr_and_value(&p, attr, sizeof(attr), value, sizeof(value)))
  281. {
  282. /* grab the codec extra_data from the config parameter of the fmtp line */
  283. sdp_parse_fmtp_config(codec, attr, value);
  284. /* Looking for a known attribute */
  285. for (i = 0; attr_names[i].str; ++i) {
  286. if (!strcasecmp(attr, attr_names[i].str)) {
  287. if (attr_names[i].type == ATTR_NAME_TYPE_INT)
  288. *(int *)((char *)rtp_payload_data + attr_names[i].offset) = atoi(value);
  289. else if (attr_names[i].type == ATTR_NAME_TYPE_STR)
  290. *(char **)((char *)rtp_payload_data + attr_names[i].offset) = av_strdup(value);
  291. }
  292. }
  293. }
  294. }
  295. /** Parse a string \p in the form of Range:npt=xx-xx, and determine the start
  296. * and end time.
  297. * Used for seeking in the rtp stream.
  298. */
  299. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  300. {
  301. char buf[256];
  302. skip_spaces(&p);
  303. if (!av_stristart(p, "npt=", &p))
  304. return;
  305. *start = AV_NOPTS_VALUE;
  306. *end = AV_NOPTS_VALUE;
  307. get_word_sep(buf, sizeof(buf), "-", &p);
  308. *start = parse_date(buf, 1);
  309. if (*p == '-') {
  310. p++;
  311. get_word_sep(buf, sizeof(buf), "-", &p);
  312. *end = parse_date(buf, 1);
  313. }
  314. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  315. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  316. }
  317. typedef struct SDPParseState {
  318. /* SDP only */
  319. struct in_addr default_ip;
  320. int default_ttl;
  321. } SDPParseState;
  322. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  323. int letter, const char *buf)
  324. {
  325. RTSPState *rt = s->priv_data;
  326. char buf1[64], st_type[64];
  327. const char *p;
  328. int codec_type, payload_type, i;
  329. AVStream *st;
  330. RTSPStream *rtsp_st;
  331. struct in_addr sdp_ip;
  332. int ttl;
  333. #ifdef DEBUG
  334. printf("sdp: %c='%s'\n", letter, buf);
  335. #endif
  336. p = buf;
  337. switch(letter) {
  338. case 'c':
  339. get_word(buf1, sizeof(buf1), &p);
  340. if (strcmp(buf1, "IN") != 0)
  341. return;
  342. get_word(buf1, sizeof(buf1), &p);
  343. if (strcmp(buf1, "IP4") != 0)
  344. return;
  345. get_word_sep(buf1, sizeof(buf1), "/", &p);
  346. if (inet_aton(buf1, &sdp_ip) == 0)
  347. return;
  348. ttl = 16;
  349. if (*p == '/') {
  350. p++;
  351. get_word_sep(buf1, sizeof(buf1), "/", &p);
  352. ttl = atoi(buf1);
  353. }
  354. if (s->nb_streams == 0) {
  355. s1->default_ip = sdp_ip;
  356. s1->default_ttl = ttl;
  357. } else {
  358. st = s->streams[s->nb_streams - 1];
  359. rtsp_st = st->priv_data;
  360. rtsp_st->sdp_ip = sdp_ip;
  361. rtsp_st->sdp_ttl = ttl;
  362. }
  363. break;
  364. case 's':
  365. av_strlcpy(s->title, p, sizeof(s->title));
  366. break;
  367. case 'i':
  368. if (s->nb_streams == 0) {
  369. av_strlcpy(s->comment, p, sizeof(s->comment));
  370. break;
  371. }
  372. break;
  373. case 'm':
  374. /* new stream */
  375. get_word(st_type, sizeof(st_type), &p);
  376. if (!strcmp(st_type, "audio")) {
  377. codec_type = CODEC_TYPE_AUDIO;
  378. } else if (!strcmp(st_type, "video")) {
  379. codec_type = CODEC_TYPE_VIDEO;
  380. } else {
  381. return;
  382. }
  383. rtsp_st = av_mallocz(sizeof(RTSPStream));
  384. if (!rtsp_st)
  385. return;
  386. rtsp_st->stream_index = -1;
  387. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  388. rtsp_st->sdp_ip = s1->default_ip;
  389. rtsp_st->sdp_ttl = s1->default_ttl;
  390. get_word(buf1, sizeof(buf1), &p); /* port */
  391. rtsp_st->sdp_port = atoi(buf1);
  392. get_word(buf1, sizeof(buf1), &p); /* protocol (ignored) */
  393. /* XXX: handle list of formats */
  394. get_word(buf1, sizeof(buf1), &p); /* format list */
  395. rtsp_st->sdp_payload_type = atoi(buf1);
  396. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  397. /* no corresponding stream */
  398. } else {
  399. st = av_new_stream(s, 0);
  400. if (!st)
  401. return;
  402. st->priv_data = rtsp_st;
  403. rtsp_st->stream_index = st->index;
  404. st->codec->codec_type = codec_type;
  405. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  406. /* if standard payload type, we can find the codec right now */
  407. rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  408. }
  409. }
  410. /* put a default control url */
  411. av_strlcpy(rtsp_st->control_url, s->filename, sizeof(rtsp_st->control_url));
  412. break;
  413. case 'a':
  414. if (av_strstart(p, "control:", &p) && s->nb_streams > 0) {
  415. char proto[32];
  416. /* get the control url */
  417. st = s->streams[s->nb_streams - 1];
  418. rtsp_st = st->priv_data;
  419. /* XXX: may need to add full url resolution */
  420. url_split(proto, sizeof(proto), NULL, 0, NULL, 0, NULL, NULL, 0, p);
  421. if (proto[0] == '\0') {
  422. /* relative control URL */
  423. av_strlcat(rtsp_st->control_url, "/", sizeof(rtsp_st->control_url));
  424. av_strlcat(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  425. } else {
  426. av_strlcpy(rtsp_st->control_url, p, sizeof(rtsp_st->control_url));
  427. }
  428. } else if (av_strstart(p, "rtpmap:", &p)) {
  429. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  430. get_word(buf1, sizeof(buf1), &p);
  431. payload_type = atoi(buf1);
  432. for(i = 0; i < s->nb_streams;i++) {
  433. st = s->streams[i];
  434. rtsp_st = st->priv_data;
  435. if (rtsp_st->sdp_payload_type == payload_type) {
  436. sdp_parse_rtpmap(st->codec, rtsp_st, payload_type, p);
  437. }
  438. }
  439. } else if (av_strstart(p, "fmtp:", &p)) {
  440. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  441. get_word(buf1, sizeof(buf1), &p);
  442. payload_type = atoi(buf1);
  443. for(i = 0; i < s->nb_streams;i++) {
  444. st = s->streams[i];
  445. rtsp_st = st->priv_data;
  446. if (rtsp_st->sdp_payload_type == payload_type) {
  447. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  448. if(!rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf)) {
  449. sdp_parse_fmtp(st, p);
  450. }
  451. } else {
  452. sdp_parse_fmtp(st, p);
  453. }
  454. }
  455. }
  456. } else if(av_strstart(p, "framesize:", &p)) {
  457. // let dynamic protocol handlers have a stab at the line.
  458. get_word(buf1, sizeof(buf1), &p);
  459. payload_type = atoi(buf1);
  460. for(i = 0; i < s->nb_streams;i++) {
  461. st = s->streams[i];
  462. rtsp_st = st->priv_data;
  463. if (rtsp_st->sdp_payload_type == payload_type) {
  464. if(rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->parse_sdp_a_line) {
  465. rtsp_st->dynamic_handler->parse_sdp_a_line(st, rtsp_st->dynamic_protocol_context, buf);
  466. }
  467. }
  468. }
  469. } else if(av_strstart(p, "range:", &p)) {
  470. int64_t start, end;
  471. // this is so that seeking on a streamed file can work.
  472. rtsp_parse_range_npt(p, &start, &end);
  473. s->start_time= start;
  474. s->duration= (end==AV_NOPTS_VALUE)?AV_NOPTS_VALUE:end-start; // AV_NOPTS_VALUE means live broadcast (and can't seek)
  475. }
  476. break;
  477. }
  478. }
  479. static int sdp_parse(AVFormatContext *s, const char *content)
  480. {
  481. const char *p;
  482. int letter;
  483. char buf[1024], *q;
  484. SDPParseState sdp_parse_state, *s1 = &sdp_parse_state;
  485. memset(s1, 0, sizeof(SDPParseState));
  486. p = content;
  487. for(;;) {
  488. skip_spaces(&p);
  489. letter = *p;
  490. if (letter == '\0')
  491. break;
  492. p++;
  493. if (*p != '=')
  494. goto next_line;
  495. p++;
  496. /* get the content */
  497. q = buf;
  498. while (*p != '\n' && *p != '\r' && *p != '\0') {
  499. if ((q - buf) < sizeof(buf) - 1)
  500. *q++ = *p;
  501. p++;
  502. }
  503. *q = '\0';
  504. sdp_parse_line(s, s1, letter, buf);
  505. next_line:
  506. while (*p != '\n' && *p != '\0')
  507. p++;
  508. if (*p == '\n')
  509. p++;
  510. }
  511. return 0;
  512. }
  513. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  514. {
  515. const char *p;
  516. int v;
  517. p = *pp;
  518. skip_spaces(&p);
  519. v = strtol(p, (char **)&p, 10);
  520. if (*p == '-') {
  521. p++;
  522. *min_ptr = v;
  523. v = strtol(p, (char **)&p, 10);
  524. *max_ptr = v;
  525. } else {
  526. *min_ptr = v;
  527. *max_ptr = v;
  528. }
  529. *pp = p;
  530. }
  531. /* XXX: only one transport specification is parsed */
  532. static void rtsp_parse_transport(RTSPHeader *reply, const char *p)
  533. {
  534. char transport_protocol[16];
  535. char profile[16];
  536. char lower_transport[16];
  537. char parameter[16];
  538. RTSPTransportField *th;
  539. char buf[256];
  540. reply->nb_transports = 0;
  541. for(;;) {
  542. skip_spaces(&p);
  543. if (*p == '\0')
  544. break;
  545. th = &reply->transports[reply->nb_transports];
  546. get_word_sep(transport_protocol, sizeof(transport_protocol),
  547. "/", &p);
  548. if (*p == '/')
  549. p++;
  550. if (!strcasecmp (transport_protocol, "rtp")) {
  551. get_word_sep(profile, sizeof(profile), "/;,", &p);
  552. lower_transport[0] = '\0';
  553. /* rtp/avp/<protocol> */
  554. if (*p == '/') {
  555. p++;
  556. get_word_sep(lower_transport, sizeof(lower_transport),
  557. ";,", &p);
  558. }
  559. } else if (!strcasecmp (transport_protocol, "x-pn-tng")) {
  560. /* x-pn-tng/<protocol> */
  561. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  562. profile[0] = '\0';
  563. }
  564. if (!strcasecmp(lower_transport, "TCP"))
  565. th->protocol = RTSP_PROTOCOL_RTP_TCP;
  566. else
  567. th->protocol = RTSP_PROTOCOL_RTP_UDP;
  568. if (*p == ';')
  569. p++;
  570. /* get each parameter */
  571. while (*p != '\0' && *p != ',') {
  572. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  573. if (!strcmp(parameter, "port")) {
  574. if (*p == '=') {
  575. p++;
  576. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  577. }
  578. } else if (!strcmp(parameter, "client_port")) {
  579. if (*p == '=') {
  580. p++;
  581. rtsp_parse_range(&th->client_port_min,
  582. &th->client_port_max, &p);
  583. }
  584. } else if (!strcmp(parameter, "server_port")) {
  585. if (*p == '=') {
  586. p++;
  587. rtsp_parse_range(&th->server_port_min,
  588. &th->server_port_max, &p);
  589. }
  590. } else if (!strcmp(parameter, "interleaved")) {
  591. if (*p == '=') {
  592. p++;
  593. rtsp_parse_range(&th->interleaved_min,
  594. &th->interleaved_max, &p);
  595. }
  596. } else if (!strcmp(parameter, "multicast")) {
  597. if (th->protocol == RTSP_PROTOCOL_RTP_UDP)
  598. th->protocol = RTSP_PROTOCOL_RTP_UDP_MULTICAST;
  599. } else if (!strcmp(parameter, "ttl")) {
  600. if (*p == '=') {
  601. p++;
  602. th->ttl = strtol(p, (char **)&p, 10);
  603. }
  604. } else if (!strcmp(parameter, "destination")) {
  605. struct in_addr ipaddr;
  606. if (*p == '=') {
  607. p++;
  608. get_word_sep(buf, sizeof(buf), ";,", &p);
  609. if (inet_aton(buf, &ipaddr))
  610. th->destination = ntohl(ipaddr.s_addr);
  611. }
  612. }
  613. while (*p != ';' && *p != '\0' && *p != ',')
  614. p++;
  615. if (*p == ';')
  616. p++;
  617. }
  618. if (*p == ',')
  619. p++;
  620. reply->nb_transports++;
  621. }
  622. }
  623. void rtsp_parse_line(RTSPHeader *reply, const char *buf)
  624. {
  625. const char *p;
  626. /* NOTE: we do case independent match for broken servers */
  627. p = buf;
  628. if (av_stristart(p, "Session:", &p)) {
  629. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  630. } else if (av_stristart(p, "Content-Length:", &p)) {
  631. reply->content_length = strtol(p, NULL, 10);
  632. } else if (av_stristart(p, "Transport:", &p)) {
  633. rtsp_parse_transport(reply, p);
  634. } else if (av_stristart(p, "CSeq:", &p)) {
  635. reply->seq = strtol(p, NULL, 10);
  636. } else if (av_stristart(p, "Range:", &p)) {
  637. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  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, RTSPHeader *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(RTSPHeader));
  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->rtp_ctx)
  761. rtp_parse_close(rtsp_st->rtp_ctx);
  762. if (rtsp_st->rtp_handle)
  763. url_close(rtsp_st->rtp_handle);
  764. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  765. rtsp_st->dynamic_handler->close(rtsp_st->dynamic_protocol_context);
  766. }
  767. av_free(rtsp_st);
  768. }
  769. av_free(rt->rtsp_streams);
  770. }
  771. static int rtsp_read_header(AVFormatContext *s,
  772. AVFormatParameters *ap)
  773. {
  774. RTSPState *rt = s->priv_data;
  775. char host[1024], path[1024], tcpname[1024], cmd[2048], *option_list, *option;
  776. URLContext *rtsp_hd;
  777. int port, i, j, ret, err;
  778. RTSPHeader reply1, *reply = &reply1;
  779. unsigned char *content = NULL;
  780. RTSPStream *rtsp_st;
  781. int protocol_mask = 0;
  782. AVStream *st;
  783. /* extract hostname and port */
  784. url_split(NULL, 0, NULL, 0,
  785. host, sizeof(host), &port, path, sizeof(path), s->filename);
  786. if (port < 0)
  787. port = RTSP_DEFAULT_PORT;
  788. /* search for options */
  789. option_list = strchr(path, '?');
  790. if (option_list) {
  791. /* remove the options from the path */
  792. *option_list++ = 0;
  793. while(option_list) {
  794. /* move the option pointer */
  795. option = option_list;
  796. option_list = strchr(option_list, '&');
  797. if (option_list)
  798. *(option_list++) = 0;
  799. /* handle the options */
  800. if (strcmp(option, "udp") == 0)
  801. protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP);
  802. else if (strcmp(option, "multicast") == 0)
  803. protocol_mask = (1<< RTSP_PROTOCOL_RTP_UDP_MULTICAST);
  804. else if (strcmp(option, "tcp") == 0)
  805. protocol_mask = (1<< RTSP_PROTOCOL_RTP_TCP);
  806. }
  807. }
  808. if (!protocol_mask)
  809. protocol_mask = rtsp_default_protocols;
  810. /* open the tcp connexion */
  811. snprintf(tcpname, sizeof(tcpname), "tcp://%s:%d", host, port);
  812. if (url_open(&rtsp_hd, tcpname, URL_RDWR) < 0)
  813. return AVERROR(EIO);
  814. rt->rtsp_hd = rtsp_hd;
  815. rt->seq = 0;
  816. /* describe the stream */
  817. snprintf(cmd, sizeof(cmd),
  818. "DESCRIBE %s RTSP/1.0\r\n"
  819. "Accept: application/sdp\r\n",
  820. s->filename);
  821. rtsp_send_cmd(s, cmd, reply, &content);
  822. if (!content) {
  823. err = AVERROR_INVALIDDATA;
  824. goto fail;
  825. }
  826. if (reply->status_code != RTSP_STATUS_OK) {
  827. err = AVERROR_INVALIDDATA;
  828. goto fail;
  829. }
  830. /* now we got the SDP description, we parse it */
  831. ret = sdp_parse(s, (const char *)content);
  832. av_freep(&content);
  833. if (ret < 0) {
  834. err = AVERROR_INVALIDDATA;
  835. goto fail;
  836. }
  837. /* for each stream, make the setup request */
  838. /* XXX: we assume the same server is used for the control of each
  839. RTSP stream */
  840. for(j = RTSP_RTP_PORT_MIN, i = 0; i < rt->nb_rtsp_streams; ++i) {
  841. char transport[2048];
  842. rtsp_st = rt->rtsp_streams[i];
  843. /* compute available transports */
  844. transport[0] = '\0';
  845. /* RTP/UDP */
  846. if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP)) {
  847. char buf[256];
  848. /* first try in specified port range */
  849. if (RTSP_RTP_PORT_MIN != 0) {
  850. while(j <= RTSP_RTP_PORT_MAX) {
  851. snprintf(buf, sizeof(buf), "rtp://%s?localport=%d", host, j);
  852. j += 2; /* we will use two port by rtp stream (rtp and rtcp) */
  853. if (url_open(&rtsp_st->rtp_handle, buf, URL_RDWR) == 0) {
  854. goto rtp_opened;
  855. }
  856. }
  857. }
  858. /* then try on any port
  859. ** if (url_open(&rtsp_st->rtp_handle, "rtp://", URL_RDONLY) < 0) {
  860. ** err = AVERROR_INVALIDDATA;
  861. ** goto fail;
  862. ** }
  863. */
  864. rtp_opened:
  865. port = rtp_get_local_port(rtsp_st->rtp_handle);
  866. if (transport[0] != '\0')
  867. av_strlcat(transport, ",", sizeof(transport));
  868. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  869. "RTP/AVP/UDP;unicast;client_port=%d-%d",
  870. port, port + 1);
  871. }
  872. /* RTP/TCP */
  873. else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_TCP)) {
  874. if (transport[0] != '\0')
  875. av_strlcat(transport, ",", sizeof(transport));
  876. snprintf(transport + strlen(transport), sizeof(transport) - strlen(transport) - 1,
  877. "RTP/AVP/TCP");
  878. }
  879. else if (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP_MULTICAST)) {
  880. if (transport[0] != '\0')
  881. av_strlcat(transport, ",", sizeof(transport));
  882. snprintf(transport + strlen(transport),
  883. sizeof(transport) - strlen(transport) - 1,
  884. "RTP/AVP/UDP;multicast");
  885. }
  886. snprintf(cmd, sizeof(cmd),
  887. "SETUP %s RTSP/1.0\r\n"
  888. "Transport: %s\r\n",
  889. rtsp_st->control_url, transport);
  890. rtsp_send_cmd(s, cmd, reply, NULL);
  891. 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].protocol != rt->protocol) {
  899. err = AVERROR_INVALIDDATA;
  900. goto fail;
  901. }
  902. } else {
  903. rt->protocol = reply->transports[0].protocol;
  904. }
  905. /* close RTP connection if not choosen */
  906. if (reply->transports[0].protocol != RTSP_PROTOCOL_RTP_UDP &&
  907. (protocol_mask & (1 << RTSP_PROTOCOL_RTP_UDP))) {
  908. url_close(rtsp_st->rtp_handle);
  909. rtsp_st->rtp_handle = NULL;
  910. }
  911. switch(reply->transports[0].protocol) {
  912. case RTSP_PROTOCOL_RTP_TCP:
  913. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  914. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  915. break;
  916. case RTSP_PROTOCOL_RTP_UDP:
  917. {
  918. char url[1024];
  919. /* XXX: also use address if specified */
  920. snprintf(url, sizeof(url), "rtp://%s:%d",
  921. host, reply->transports[0].server_port_min);
  922. if (rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  923. err = AVERROR_INVALIDDATA;
  924. goto fail;
  925. }
  926. }
  927. break;
  928. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  929. {
  930. char url[1024];
  931. struct in_addr in;
  932. in.s_addr = htonl(reply->transports[0].destination);
  933. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  934. inet_ntoa(in),
  935. reply->transports[0].port_min,
  936. reply->transports[0].ttl);
  937. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  938. err = AVERROR_INVALIDDATA;
  939. goto fail;
  940. }
  941. }
  942. break;
  943. }
  944. /* open the RTP context */
  945. st = NULL;
  946. if (rtsp_st->stream_index >= 0)
  947. st = s->streams[rtsp_st->stream_index];
  948. if (!st)
  949. s->ctx_flags |= AVFMTCTX_NOHEADER;
  950. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  951. if (!rtsp_st->rtp_ctx) {
  952. err = AVERROR(ENOMEM);
  953. goto fail;
  954. } else {
  955. if(rtsp_st->dynamic_handler) {
  956. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  957. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  958. }
  959. }
  960. }
  961. rt->state = RTSP_STATE_IDLE;
  962. rt->seek_timestamp = 0; /* default is to start stream at position
  963. zero */
  964. if (ap->initial_pause) {
  965. /* do not start immediately */
  966. } else {
  967. if (rtsp_read_play(s) < 0) {
  968. err = AVERROR_INVALIDDATA;
  969. goto fail;
  970. }
  971. }
  972. return 0;
  973. fail:
  974. rtsp_close_streams(rt);
  975. av_freep(&content);
  976. url_close(rt->rtsp_hd);
  977. return err;
  978. }
  979. static int tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  980. uint8_t *buf, int buf_size)
  981. {
  982. RTSPState *rt = s->priv_data;
  983. int id, len, i, ret;
  984. RTSPStream *rtsp_st;
  985. #ifdef DEBUG_RTP_TCP
  986. printf("tcp_read_packet:\n");
  987. #endif
  988. redo:
  989. for(;;) {
  990. ret = url_readbuf(rt->rtsp_hd, buf, 1);
  991. #ifdef DEBUG_RTP_TCP
  992. printf("ret=%d c=%02x [%c]\n", ret, buf[0], buf[0]);
  993. #endif
  994. if (ret != 1)
  995. return -1;
  996. if (buf[0] == '$')
  997. break;
  998. }
  999. ret = url_readbuf(rt->rtsp_hd, buf, 3);
  1000. if (ret != 3)
  1001. return -1;
  1002. id = buf[0];
  1003. len = AV_RB16(buf + 1);
  1004. #ifdef DEBUG_RTP_TCP
  1005. printf("id=%d len=%d\n", id, len);
  1006. #endif
  1007. if (len > buf_size || len < 12)
  1008. goto redo;
  1009. /* get the data */
  1010. ret = url_readbuf(rt->rtsp_hd, buf, len);
  1011. if (ret != len)
  1012. return -1;
  1013. /* find the matching stream */
  1014. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1015. rtsp_st = rt->rtsp_streams[i];
  1016. if (id >= rtsp_st->interleaved_min &&
  1017. id <= rtsp_st->interleaved_max)
  1018. goto found;
  1019. }
  1020. goto redo;
  1021. found:
  1022. *prtsp_st = rtsp_st;
  1023. return len;
  1024. }
  1025. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1026. uint8_t *buf, int buf_size)
  1027. {
  1028. RTSPState *rt = s->priv_data;
  1029. RTSPStream *rtsp_st;
  1030. fd_set rfds;
  1031. int fd1, fd2, fd_max, n, i, ret;
  1032. struct timeval tv;
  1033. for(;;) {
  1034. if (url_interrupt_cb())
  1035. return AVERROR(EINTR);
  1036. FD_ZERO(&rfds);
  1037. fd_max = -1;
  1038. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1039. rtsp_st = rt->rtsp_streams[i];
  1040. /* currently, we cannot probe RTCP handle because of blocking restrictions */
  1041. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1042. if (fd1 > fd_max)
  1043. fd_max = fd1;
  1044. FD_SET(fd1, &rfds);
  1045. }
  1046. tv.tv_sec = 0;
  1047. tv.tv_usec = 100 * 1000;
  1048. n = select(fd_max + 1, &rfds, NULL, NULL, &tv);
  1049. if (n > 0) {
  1050. for(i = 0; i < rt->nb_rtsp_streams; i++) {
  1051. rtsp_st = rt->rtsp_streams[i];
  1052. rtp_get_file_handles(rtsp_st->rtp_handle, &fd1, &fd2);
  1053. if (FD_ISSET(fd1, &rfds)) {
  1054. ret = url_read(rtsp_st->rtp_handle, buf, buf_size);
  1055. if (ret > 0) {
  1056. *prtsp_st = rtsp_st;
  1057. return ret;
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. static int rtsp_read_packet(AVFormatContext *s,
  1065. AVPacket *pkt)
  1066. {
  1067. RTSPState *rt = s->priv_data;
  1068. RTSPStream *rtsp_st;
  1069. int ret, len;
  1070. uint8_t buf[RTP_MAX_PACKET_LENGTH];
  1071. /* get next frames from the same RTP packet */
  1072. if (rt->cur_rtp) {
  1073. ret = rtp_parse_packet(rt->cur_rtp, pkt, NULL, 0);
  1074. if (ret == 0) {
  1075. rt->cur_rtp = NULL;
  1076. return 0;
  1077. } else if (ret == 1) {
  1078. return 0;
  1079. } else {
  1080. rt->cur_rtp = NULL;
  1081. }
  1082. }
  1083. /* read next RTP packet */
  1084. redo:
  1085. switch(rt->protocol) {
  1086. default:
  1087. case RTSP_PROTOCOL_RTP_TCP:
  1088. len = tcp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1089. break;
  1090. case RTSP_PROTOCOL_RTP_UDP:
  1091. case RTSP_PROTOCOL_RTP_UDP_MULTICAST:
  1092. len = udp_read_packet(s, &rtsp_st, buf, sizeof(buf));
  1093. if (len >=0 && rtsp_st->rtp_ctx)
  1094. rtp_check_and_send_back_rr(rtsp_st->rtp_ctx, len);
  1095. break;
  1096. }
  1097. if (len < 0)
  1098. return len;
  1099. ret = rtp_parse_packet(rtsp_st->rtp_ctx, pkt, buf, len);
  1100. if (ret < 0)
  1101. goto redo;
  1102. if (ret == 1) {
  1103. /* more packets may follow, so we save the RTP context */
  1104. rt->cur_rtp = rtsp_st->rtp_ctx;
  1105. }
  1106. return 0;
  1107. }
  1108. static int rtsp_read_play(AVFormatContext *s)
  1109. {
  1110. RTSPState *rt = s->priv_data;
  1111. RTSPHeader reply1, *reply = &reply1;
  1112. char cmd[1024];
  1113. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  1114. if (rt->state == RTSP_STATE_PAUSED) {
  1115. snprintf(cmd, sizeof(cmd),
  1116. "PLAY %s RTSP/1.0\r\n",
  1117. s->filename);
  1118. } else {
  1119. snprintf(cmd, sizeof(cmd),
  1120. "PLAY %s RTSP/1.0\r\n"
  1121. "Range: npt=%0.3f-\r\n",
  1122. s->filename,
  1123. (double)rt->seek_timestamp / AV_TIME_BASE);
  1124. }
  1125. rtsp_send_cmd(s, cmd, reply, NULL);
  1126. if (reply->status_code != RTSP_STATUS_OK) {
  1127. return -1;
  1128. } else {
  1129. rt->state = RTSP_STATE_PLAYING;
  1130. return 0;
  1131. }
  1132. }
  1133. /* pause the stream */
  1134. static int rtsp_read_pause(AVFormatContext *s)
  1135. {
  1136. RTSPState *rt = s->priv_data;
  1137. RTSPHeader reply1, *reply = &reply1;
  1138. char cmd[1024];
  1139. rt = s->priv_data;
  1140. if (rt->state != RTSP_STATE_PLAYING)
  1141. return 0;
  1142. snprintf(cmd, sizeof(cmd),
  1143. "PAUSE %s RTSP/1.0\r\n",
  1144. s->filename);
  1145. rtsp_send_cmd(s, cmd, reply, NULL);
  1146. if (reply->status_code != RTSP_STATUS_OK) {
  1147. return -1;
  1148. } else {
  1149. rt->state = RTSP_STATE_PAUSED;
  1150. return 0;
  1151. }
  1152. }
  1153. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  1154. int64_t timestamp, int flags)
  1155. {
  1156. RTSPState *rt = s->priv_data;
  1157. rt->seek_timestamp = av_rescale_q(timestamp, s->streams[stream_index]->time_base, AV_TIME_BASE_Q);
  1158. switch(rt->state) {
  1159. default:
  1160. case RTSP_STATE_IDLE:
  1161. break;
  1162. case RTSP_STATE_PLAYING:
  1163. if (rtsp_read_play(s) != 0)
  1164. return -1;
  1165. break;
  1166. case RTSP_STATE_PAUSED:
  1167. rt->state = RTSP_STATE_IDLE;
  1168. break;
  1169. }
  1170. return 0;
  1171. }
  1172. static int rtsp_read_close(AVFormatContext *s)
  1173. {
  1174. RTSPState *rt = s->priv_data;
  1175. RTSPHeader reply1, *reply = &reply1;
  1176. char cmd[1024];
  1177. #if 0
  1178. /* NOTE: it is valid to flush the buffer here */
  1179. if (rt->protocol == RTSP_PROTOCOL_RTP_TCP) {
  1180. url_fclose(&rt->rtsp_gb);
  1181. }
  1182. #endif
  1183. snprintf(cmd, sizeof(cmd),
  1184. "TEARDOWN %s RTSP/1.0\r\n",
  1185. s->filename);
  1186. rtsp_send_cmd(s, cmd, reply, NULL);
  1187. rtsp_close_streams(rt);
  1188. url_close(rt->rtsp_hd);
  1189. return 0;
  1190. }
  1191. #ifdef CONFIG_RTSP_DEMUXER
  1192. AVInputFormat rtsp_demuxer = {
  1193. "rtsp",
  1194. "RTSP input format",
  1195. sizeof(RTSPState),
  1196. rtsp_probe,
  1197. rtsp_read_header,
  1198. rtsp_read_packet,
  1199. rtsp_read_close,
  1200. rtsp_read_seek,
  1201. .flags = AVFMT_NOFILE,
  1202. .read_play = rtsp_read_play,
  1203. .read_pause = rtsp_read_pause,
  1204. };
  1205. #endif
  1206. static int sdp_probe(AVProbeData *p1)
  1207. {
  1208. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1209. /* we look for a line beginning "c=IN IP4" */
  1210. while (p < p_end && *p != '\0') {
  1211. if (p + sizeof("c=IN IP4") - 1 < p_end && av_strstart(p, "c=IN IP4", NULL))
  1212. return AVPROBE_SCORE_MAX / 2;
  1213. while(p < p_end - 1 && *p != '\n') p++;
  1214. if (++p >= p_end)
  1215. break;
  1216. if (*p == '\r')
  1217. p++;
  1218. }
  1219. return 0;
  1220. }
  1221. #define SDP_MAX_SIZE 8192
  1222. static int sdp_read_header(AVFormatContext *s,
  1223. AVFormatParameters *ap)
  1224. {
  1225. RTSPState *rt = s->priv_data;
  1226. RTSPStream *rtsp_st;
  1227. int size, i, err;
  1228. char *content;
  1229. char url[1024];
  1230. AVStream *st;
  1231. /* read the whole sdp file */
  1232. /* XXX: better loading */
  1233. content = av_malloc(SDP_MAX_SIZE);
  1234. size = get_buffer(s->pb, content, SDP_MAX_SIZE - 1);
  1235. if (size <= 0) {
  1236. av_free(content);
  1237. return AVERROR_INVALIDDATA;
  1238. }
  1239. content[size] ='\0';
  1240. sdp_parse(s, content);
  1241. av_free(content);
  1242. /* open each RTP stream */
  1243. for(i=0;i<rt->nb_rtsp_streams;i++) {
  1244. rtsp_st = rt->rtsp_streams[i];
  1245. snprintf(url, sizeof(url), "rtp://%s:%d?multicast=1&ttl=%d",
  1246. inet_ntoa(rtsp_st->sdp_ip),
  1247. rtsp_st->sdp_port,
  1248. rtsp_st->sdp_ttl);
  1249. if (url_open(&rtsp_st->rtp_handle, url, URL_RDWR) < 0) {
  1250. err = AVERROR_INVALIDDATA;
  1251. goto fail;
  1252. }
  1253. /* open the RTP context */
  1254. st = NULL;
  1255. if (rtsp_st->stream_index >= 0)
  1256. st = s->streams[rtsp_st->stream_index];
  1257. if (!st)
  1258. s->ctx_flags |= AVFMTCTX_NOHEADER;
  1259. rtsp_st->rtp_ctx = rtp_parse_open(s, st, rtsp_st->rtp_handle, rtsp_st->sdp_payload_type, &rtsp_st->rtp_payload_data);
  1260. if (!rtsp_st->rtp_ctx) {
  1261. err = AVERROR(ENOMEM);
  1262. goto fail;
  1263. } else {
  1264. if(rtsp_st->dynamic_handler) {
  1265. rtsp_st->rtp_ctx->dynamic_protocol_context= rtsp_st->dynamic_protocol_context;
  1266. rtsp_st->rtp_ctx->parse_packet= rtsp_st->dynamic_handler->parse_packet;
  1267. }
  1268. }
  1269. }
  1270. return 0;
  1271. fail:
  1272. rtsp_close_streams(rt);
  1273. return err;
  1274. }
  1275. static int sdp_read_packet(AVFormatContext *s,
  1276. AVPacket *pkt)
  1277. {
  1278. return rtsp_read_packet(s, pkt);
  1279. }
  1280. static int sdp_read_close(AVFormatContext *s)
  1281. {
  1282. RTSPState *rt = s->priv_data;
  1283. rtsp_close_streams(rt);
  1284. return 0;
  1285. }
  1286. #ifdef CONFIG_SDP_DEMUXER
  1287. AVInputFormat sdp_demuxer = {
  1288. "sdp",
  1289. "SDP",
  1290. sizeof(RTSPState),
  1291. sdp_probe,
  1292. sdp_read_header,
  1293. sdp_read_packet,
  1294. sdp_read_close,
  1295. };
  1296. #endif
  1297. #ifdef CONFIG_REDIR_DEMUXER
  1298. /* dummy redirector format (used directly in av_open_input_file now) */
  1299. static int redir_probe(AVProbeData *pd)
  1300. {
  1301. const char *p;
  1302. p = pd->buf;
  1303. while (redir_isspace(*p))
  1304. p++;
  1305. if (av_strstart(p, "http://", NULL) ||
  1306. av_strstart(p, "rtsp://", NULL))
  1307. return AVPROBE_SCORE_MAX;
  1308. return 0;
  1309. }
  1310. static int redir_read_header(AVFormatContext *s, AVFormatParameters *ap)
  1311. {
  1312. char buf[4096], *q;
  1313. int c;
  1314. AVFormatContext *ic = NULL;
  1315. ByteIOContext *f = s->pb;
  1316. /* parse each URL and try to open it */
  1317. c = url_fgetc(f);
  1318. while (c != URL_EOF) {
  1319. /* skip spaces */
  1320. for(;;) {
  1321. if (!redir_isspace(c))
  1322. break;
  1323. c = url_fgetc(f);
  1324. }
  1325. if (c == URL_EOF)
  1326. break;
  1327. /* record url */
  1328. q = buf;
  1329. for(;;) {
  1330. if (c == URL_EOF || redir_isspace(c))
  1331. break;
  1332. if ((q - buf) < sizeof(buf) - 1)
  1333. *q++ = c;
  1334. c = url_fgetc(f);
  1335. }
  1336. *q = '\0';
  1337. //printf("URL='%s'\n", buf);
  1338. /* try to open the media file */
  1339. if (av_open_input_file(&ic, buf, NULL, 0, NULL) == 0)
  1340. break;
  1341. }
  1342. if (!ic)
  1343. return AVERROR(EIO);
  1344. *s = *ic;
  1345. url_fclose(f);
  1346. return 0;
  1347. }
  1348. AVInputFormat redir_demuxer = {
  1349. "redir",
  1350. "Redirector format",
  1351. 0,
  1352. redir_probe,
  1353. redir_read_header,
  1354. NULL,
  1355. NULL,
  1356. };
  1357. #endif