rtsp.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143
  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 "libavutil/avassert.h"
  22. #include "libavutil/base64.h"
  23. #include "libavutil/avstring.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "libavutil/mathematics.h"
  26. #include "libavutil/parseutils.h"
  27. #include "libavutil/random_seed.h"
  28. #include "libavutil/dict.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/time.h"
  31. #include "avformat.h"
  32. #include "avio_internal.h"
  33. #if HAVE_POLL_H
  34. #include <poll.h>
  35. #endif
  36. #include "internal.h"
  37. #include "network.h"
  38. #include "os_support.h"
  39. #include "http.h"
  40. #include "rtsp.h"
  41. #include "rtpdec.h"
  42. #include "rdt.h"
  43. #include "rtpdec_formats.h"
  44. #include "rtpenc_chain.h"
  45. #include "url.h"
  46. #include "rtpenc.h"
  47. #include "mpegts.h"
  48. //#define DEBUG
  49. /* Timeout values for socket poll, in ms,
  50. * and read_packet(), in seconds */
  51. #define POLL_TIMEOUT_MS 100
  52. #define READ_PACKET_TIMEOUT_S 10
  53. #define MAX_TIMEOUTS READ_PACKET_TIMEOUT_S * 1000 / POLL_TIMEOUT_MS
  54. #define SDP_MAX_SIZE 16384
  55. #define RECVBUF_SIZE 10 * RTP_MAX_PACKET_LENGTH
  56. #define DEFAULT_REORDERING_DELAY 100000
  57. #define OFFSET(x) offsetof(RTSPState, x)
  58. #define DEC AV_OPT_FLAG_DECODING_PARAM
  59. #define ENC AV_OPT_FLAG_ENCODING_PARAM
  60. #define RTSP_FLAG_OPTS(name, longname) \
  61. { name, longname, OFFSET(rtsp_flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC, "rtsp_flags" }, \
  62. { "filter_src", "Only receive packets from the negotiated peer IP", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_FILTER_SRC}, 0, 0, DEC, "rtsp_flags" }, \
  63. { "listen", "Wait for incoming connections", 0, AV_OPT_TYPE_CONST, {.i64 = RTSP_FLAG_LISTEN}, 0, 0, DEC, "rtsp_flags" }
  64. #define RTSP_MEDIATYPE_OPTS(name, longname) \
  65. { name, longname, OFFSET(media_type_mask), AV_OPT_TYPE_FLAGS, { .i64 = (1 << (AVMEDIA_TYPE_DATA+1)) - 1 }, INT_MIN, INT_MAX, DEC, "allowed_media_types" }, \
  66. { "video", "Video", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_VIDEO}, 0, 0, DEC, "allowed_media_types" }, \
  67. { "audio", "Audio", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_AUDIO}, 0, 0, DEC, "allowed_media_types" }, \
  68. { "data", "Data", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << AVMEDIA_TYPE_DATA}, 0, 0, DEC, "allowed_media_types" }
  69. const AVOption ff_rtsp_options[] = {
  70. { "initial_pause", "Don't start playing the stream immediately", OFFSET(initial_pause), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DEC },
  71. FF_RTP_FLAG_OPTS(RTSPState, rtp_muxer_flags),
  72. { "rtsp_transport", "RTSP transport protocols", OFFSET(lower_transport_mask), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, DEC|ENC, "rtsp_transport" }, \
  73. { "udp", "UDP", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_UDP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
  74. { "tcp", "TCP", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_TCP}, 0, 0, DEC|ENC, "rtsp_transport" }, \
  75. { "udp_multicast", "UDP multicast", 0, AV_OPT_TYPE_CONST, {.i64 = 1 << RTSP_LOWER_TRANSPORT_UDP_MULTICAST}, 0, 0, DEC, "rtsp_transport" },
  76. { "http", "HTTP tunneling", 0, AV_OPT_TYPE_CONST, {.i64 = (1 << RTSP_LOWER_TRANSPORT_HTTP)}, 0, 0, DEC, "rtsp_transport" },
  77. RTSP_FLAG_OPTS("rtsp_flags", "RTSP flags"),
  78. RTSP_MEDIATYPE_OPTS("allowed_media_types", "Media types to accept from the server"),
  79. { "min_port", "Minimum local UDP port", OFFSET(rtp_port_min), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MIN}, 0, 65535, DEC|ENC },
  80. { "max_port", "Maximum local UDP port", OFFSET(rtp_port_max), AV_OPT_TYPE_INT, {.i64 = RTSP_RTP_PORT_MAX}, 0, 65535, DEC|ENC },
  81. { "timeout", "Maximum timeout (in seconds) to wait for incoming connections. -1 is infinite. Implies flag listen", OFFSET(initial_timeout), AV_OPT_TYPE_INT, {.i64 = -1}, INT_MIN, INT_MAX, DEC },
  82. { NULL },
  83. };
  84. static const AVOption sdp_options[] = {
  85. RTSP_FLAG_OPTS("sdp_flags", "SDP flags"),
  86. RTSP_MEDIATYPE_OPTS("allowed_media_types", "Media types to accept from the server"),
  87. { NULL },
  88. };
  89. static const AVOption rtp_options[] = {
  90. RTSP_FLAG_OPTS("rtp_flags", "RTP flags"),
  91. { NULL },
  92. };
  93. static void get_word_until_chars(char *buf, int buf_size,
  94. const char *sep, const char **pp)
  95. {
  96. const char *p;
  97. char *q;
  98. p = *pp;
  99. p += strspn(p, SPACE_CHARS);
  100. q = buf;
  101. while (!strchr(sep, *p) && *p != '\0') {
  102. if ((q - buf) < buf_size - 1)
  103. *q++ = *p;
  104. p++;
  105. }
  106. if (buf_size > 0)
  107. *q = '\0';
  108. *pp = p;
  109. }
  110. static void get_word_sep(char *buf, int buf_size, const char *sep,
  111. const char **pp)
  112. {
  113. if (**pp == '/') (*pp)++;
  114. get_word_until_chars(buf, buf_size, sep, pp);
  115. }
  116. static void get_word(char *buf, int buf_size, const char **pp)
  117. {
  118. get_word_until_chars(buf, buf_size, SPACE_CHARS, pp);
  119. }
  120. /** Parse a string p in the form of Range:npt=xx-xx, and determine the start
  121. * and end time.
  122. * Used for seeking in the rtp stream.
  123. */
  124. static void rtsp_parse_range_npt(const char *p, int64_t *start, int64_t *end)
  125. {
  126. char buf[256];
  127. p += strspn(p, SPACE_CHARS);
  128. if (!av_stristart(p, "npt=", &p))
  129. return;
  130. *start = AV_NOPTS_VALUE;
  131. *end = AV_NOPTS_VALUE;
  132. get_word_sep(buf, sizeof(buf), "-", &p);
  133. av_parse_time(start, buf, 1);
  134. if (*p == '-') {
  135. p++;
  136. get_word_sep(buf, sizeof(buf), "-", &p);
  137. av_parse_time(end, buf, 1);
  138. }
  139. // av_log(NULL, AV_LOG_DEBUG, "Range Start: %lld\n", *start);
  140. // av_log(NULL, AV_LOG_DEBUG, "Range End: %lld\n", *end);
  141. }
  142. static int get_sockaddr(const char *buf, struct sockaddr_storage *sock)
  143. {
  144. struct addrinfo hints = { 0 }, *ai = NULL;
  145. hints.ai_flags = AI_NUMERICHOST;
  146. if (getaddrinfo(buf, NULL, &hints, &ai))
  147. return -1;
  148. memcpy(sock, ai->ai_addr, FFMIN(sizeof(*sock), ai->ai_addrlen));
  149. freeaddrinfo(ai);
  150. return 0;
  151. }
  152. #if CONFIG_RTPDEC
  153. static void init_rtp_handler(RTPDynamicProtocolHandler *handler,
  154. RTSPStream *rtsp_st, AVCodecContext *codec)
  155. {
  156. if (!handler)
  157. return;
  158. codec->codec_id = handler->codec_id;
  159. rtsp_st->dynamic_handler = handler;
  160. if (handler->alloc) {
  161. rtsp_st->dynamic_protocol_context = handler->alloc();
  162. if (!rtsp_st->dynamic_protocol_context)
  163. rtsp_st->dynamic_handler = NULL;
  164. }
  165. }
  166. /* parse the rtpmap description: <codec_name>/<clock_rate>[/<other params>] */
  167. static int sdp_parse_rtpmap(AVFormatContext *s,
  168. AVStream *st, RTSPStream *rtsp_st,
  169. int payload_type, const char *p)
  170. {
  171. AVCodecContext *codec = st->codec;
  172. char buf[256];
  173. int i;
  174. AVCodec *c;
  175. const char *c_name;
  176. /* Loop into AVRtpDynamicPayloadTypes[] and AVRtpPayloadTypes[] and
  177. * see if we can handle this kind of payload.
  178. * The space should normally not be there but some Real streams or
  179. * particular servers ("RealServer Version 6.1.3.970", see issue 1658)
  180. * have a trailing space. */
  181. get_word_sep(buf, sizeof(buf), "/ ", &p);
  182. if (payload_type < RTP_PT_PRIVATE) {
  183. /* We are in a standard case
  184. * (from http://www.iana.org/assignments/rtp-parameters). */
  185. /* search into AVRtpPayloadTypes[] */
  186. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  187. }
  188. if (codec->codec_id == AV_CODEC_ID_NONE) {
  189. RTPDynamicProtocolHandler *handler =
  190. ff_rtp_handler_find_by_name(buf, codec->codec_type);
  191. init_rtp_handler(handler, rtsp_st, codec);
  192. /* If no dynamic handler was found, check with the list of standard
  193. * allocated types, if such a stream for some reason happens to
  194. * use a private payload type. This isn't handled in rtpdec.c, since
  195. * the format name from the rtpmap line never is passed into rtpdec. */
  196. if (!rtsp_st->dynamic_handler)
  197. codec->codec_id = ff_rtp_codec_id(buf, codec->codec_type);
  198. }
  199. c = avcodec_find_decoder(codec->codec_id);
  200. if (c && c->name)
  201. c_name = c->name;
  202. else
  203. c_name = "(null)";
  204. get_word_sep(buf, sizeof(buf), "/", &p);
  205. i = atoi(buf);
  206. switch (codec->codec_type) {
  207. case AVMEDIA_TYPE_AUDIO:
  208. av_log(s, AV_LOG_DEBUG, "audio codec set to: %s\n", c_name);
  209. codec->sample_rate = RTSP_DEFAULT_AUDIO_SAMPLERATE;
  210. codec->channels = RTSP_DEFAULT_NB_AUDIO_CHANNELS;
  211. if (i > 0) {
  212. codec->sample_rate = i;
  213. avpriv_set_pts_info(st, 32, 1, codec->sample_rate);
  214. get_word_sep(buf, sizeof(buf), "/", &p);
  215. i = atoi(buf);
  216. if (i > 0)
  217. codec->channels = i;
  218. // TODO: there is a bug here; if it is a mono stream, and
  219. // less than 22000Hz, faad upconverts to stereo and twice
  220. // the frequency. No problem, but the sample rate is being
  221. // set here by the sdp line. Patch on its way. (rdm)
  222. }
  223. av_log(s, AV_LOG_DEBUG, "audio samplerate set to: %i\n",
  224. codec->sample_rate);
  225. av_log(s, AV_LOG_DEBUG, "audio channels set to: %i\n",
  226. codec->channels);
  227. break;
  228. case AVMEDIA_TYPE_VIDEO:
  229. av_log(s, AV_LOG_DEBUG, "video codec set to: %s\n", c_name);
  230. if (i > 0)
  231. avpriv_set_pts_info(st, 32, 1, i);
  232. break;
  233. default:
  234. break;
  235. }
  236. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_handler->init)
  237. rtsp_st->dynamic_handler->init(s, st->index,
  238. rtsp_st->dynamic_protocol_context);
  239. return 0;
  240. }
  241. /* parse the attribute line from the fmtp a line of an sdp response. This
  242. * is broken out as a function because it is used in rtp_h264.c, which is
  243. * forthcoming. */
  244. int ff_rtsp_next_attr_and_value(const char **p, char *attr, int attr_size,
  245. char *value, int value_size)
  246. {
  247. *p += strspn(*p, SPACE_CHARS);
  248. if (**p) {
  249. get_word_sep(attr, attr_size, "=", p);
  250. if (**p == '=')
  251. (*p)++;
  252. get_word_sep(value, value_size, ";", p);
  253. if (**p == ';')
  254. (*p)++;
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. typedef struct SDPParseState {
  260. /* SDP only */
  261. struct sockaddr_storage default_ip;
  262. int default_ttl;
  263. int skip_media; ///< set if an unknown m= line occurs
  264. } SDPParseState;
  265. static void sdp_parse_line(AVFormatContext *s, SDPParseState *s1,
  266. int letter, const char *buf)
  267. {
  268. RTSPState *rt = s->priv_data;
  269. char buf1[64], st_type[64];
  270. const char *p;
  271. enum AVMediaType codec_type;
  272. int payload_type, i;
  273. AVStream *st;
  274. RTSPStream *rtsp_st;
  275. struct sockaddr_storage sdp_ip;
  276. int ttl;
  277. av_dlog(s, "sdp: %c='%s'\n", letter, buf);
  278. p = buf;
  279. if (s1->skip_media && letter != 'm')
  280. return;
  281. switch (letter) {
  282. case 'c':
  283. get_word(buf1, sizeof(buf1), &p);
  284. if (strcmp(buf1, "IN") != 0)
  285. return;
  286. get_word(buf1, sizeof(buf1), &p);
  287. if (strcmp(buf1, "IP4") && strcmp(buf1, "IP6"))
  288. return;
  289. get_word_sep(buf1, sizeof(buf1), "/", &p);
  290. if (get_sockaddr(buf1, &sdp_ip))
  291. return;
  292. ttl = 16;
  293. if (*p == '/') {
  294. p++;
  295. get_word_sep(buf1, sizeof(buf1), "/", &p);
  296. ttl = atoi(buf1);
  297. }
  298. if (s->nb_streams == 0) {
  299. s1->default_ip = sdp_ip;
  300. s1->default_ttl = ttl;
  301. } else {
  302. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  303. rtsp_st->sdp_ip = sdp_ip;
  304. rtsp_st->sdp_ttl = ttl;
  305. }
  306. break;
  307. case 's':
  308. av_dict_set(&s->metadata, "title", p, 0);
  309. break;
  310. case 'i':
  311. if (s->nb_streams == 0) {
  312. av_dict_set(&s->metadata, "comment", p, 0);
  313. break;
  314. }
  315. break;
  316. case 'm':
  317. /* new stream */
  318. s1->skip_media = 0;
  319. codec_type = AVMEDIA_TYPE_UNKNOWN;
  320. get_word(st_type, sizeof(st_type), &p);
  321. if (!strcmp(st_type, "audio")) {
  322. codec_type = AVMEDIA_TYPE_AUDIO;
  323. } else if (!strcmp(st_type, "video")) {
  324. codec_type = AVMEDIA_TYPE_VIDEO;
  325. } else if (!strcmp(st_type, "application")) {
  326. codec_type = AVMEDIA_TYPE_DATA;
  327. }
  328. if (codec_type == AVMEDIA_TYPE_UNKNOWN || !(rt->media_type_mask & (1 << codec_type))) {
  329. s1->skip_media = 1;
  330. return;
  331. }
  332. rtsp_st = av_mallocz(sizeof(RTSPStream));
  333. if (!rtsp_st)
  334. return;
  335. rtsp_st->stream_index = -1;
  336. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  337. rtsp_st->sdp_ip = s1->default_ip;
  338. rtsp_st->sdp_ttl = s1->default_ttl;
  339. get_word(buf1, sizeof(buf1), &p); /* port */
  340. rtsp_st->sdp_port = atoi(buf1);
  341. get_word(buf1, sizeof(buf1), &p); /* protocol */
  342. if (!strcmp(buf1, "udp"))
  343. rt->transport = RTSP_TRANSPORT_RAW;
  344. /* XXX: handle list of formats */
  345. get_word(buf1, sizeof(buf1), &p); /* format list */
  346. rtsp_st->sdp_payload_type = atoi(buf1);
  347. if (!strcmp(ff_rtp_enc_name(rtsp_st->sdp_payload_type), "MP2T")) {
  348. /* no corresponding stream */
  349. if (rt->transport == RTSP_TRANSPORT_RAW && !rt->ts && CONFIG_RTPDEC)
  350. rt->ts = ff_mpegts_parse_open(s);
  351. } else if (rt->server_type == RTSP_SERVER_WMS &&
  352. codec_type == AVMEDIA_TYPE_DATA) {
  353. /* RTX stream, a stream that carries all the other actual
  354. * audio/video streams. Don't expose this to the callers. */
  355. } else {
  356. st = avformat_new_stream(s, NULL);
  357. if (!st)
  358. return;
  359. st->id = rt->nb_rtsp_streams - 1;
  360. rtsp_st->stream_index = st->index;
  361. st->codec->codec_type = codec_type;
  362. if (rtsp_st->sdp_payload_type < RTP_PT_PRIVATE) {
  363. RTPDynamicProtocolHandler *handler;
  364. /* if standard payload type, we can find the codec right now */
  365. ff_rtp_get_codec_info(st->codec, rtsp_st->sdp_payload_type);
  366. if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO &&
  367. st->codec->sample_rate > 0)
  368. avpriv_set_pts_info(st, 32, 1, st->codec->sample_rate);
  369. /* Even static payload types may need a custom depacketizer */
  370. handler = ff_rtp_handler_find_by_id(
  371. rtsp_st->sdp_payload_type, st->codec->codec_type);
  372. init_rtp_handler(handler, rtsp_st, st->codec);
  373. if (handler && handler->init)
  374. handler->init(s, st->index,
  375. rtsp_st->dynamic_protocol_context);
  376. }
  377. }
  378. /* put a default control url */
  379. av_strlcpy(rtsp_st->control_url, rt->control_uri,
  380. sizeof(rtsp_st->control_url));
  381. break;
  382. case 'a':
  383. if (av_strstart(p, "control:", &p)) {
  384. if (s->nb_streams == 0) {
  385. if (!strncmp(p, "rtsp://", 7))
  386. av_strlcpy(rt->control_uri, p,
  387. sizeof(rt->control_uri));
  388. } else {
  389. char proto[32];
  390. /* get the control url */
  391. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  392. /* XXX: may need to add full url resolution */
  393. av_url_split(proto, sizeof(proto), NULL, 0, NULL, 0,
  394. NULL, NULL, 0, p);
  395. if (proto[0] == '\0') {
  396. /* relative control URL */
  397. if (rtsp_st->control_url[strlen(rtsp_st->control_url)-1]!='/')
  398. av_strlcat(rtsp_st->control_url, "/",
  399. sizeof(rtsp_st->control_url));
  400. av_strlcat(rtsp_st->control_url, p,
  401. sizeof(rtsp_st->control_url));
  402. } else
  403. av_strlcpy(rtsp_st->control_url, p,
  404. sizeof(rtsp_st->control_url));
  405. }
  406. } else if (av_strstart(p, "rtpmap:", &p) && s->nb_streams > 0) {
  407. /* NOTE: rtpmap is only supported AFTER the 'm=' tag */
  408. get_word(buf1, sizeof(buf1), &p);
  409. payload_type = atoi(buf1);
  410. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  411. if (rtsp_st->stream_index >= 0) {
  412. st = s->streams[rtsp_st->stream_index];
  413. sdp_parse_rtpmap(s, st, rtsp_st, payload_type, p);
  414. }
  415. } else if (av_strstart(p, "fmtp:", &p) ||
  416. av_strstart(p, "framesize:", &p)) {
  417. /* NOTE: fmtp is only supported AFTER the 'a=rtpmap:xxx' tag */
  418. // let dynamic protocol handlers have a stab at the line.
  419. get_word(buf1, sizeof(buf1), &p);
  420. payload_type = atoi(buf1);
  421. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  422. rtsp_st = rt->rtsp_streams[i];
  423. if (rtsp_st->sdp_payload_type == payload_type &&
  424. rtsp_st->dynamic_handler &&
  425. rtsp_st->dynamic_handler->parse_sdp_a_line)
  426. rtsp_st->dynamic_handler->parse_sdp_a_line(s, i,
  427. rtsp_st->dynamic_protocol_context, buf);
  428. }
  429. } else if (av_strstart(p, "range:", &p)) {
  430. int64_t start, end;
  431. // this is so that seeking on a streamed file can work.
  432. rtsp_parse_range_npt(p, &start, &end);
  433. s->start_time = start;
  434. /* AV_NOPTS_VALUE means live broadcast (and can't seek) */
  435. s->duration = (end == AV_NOPTS_VALUE) ?
  436. AV_NOPTS_VALUE : end - start;
  437. } else if (av_strstart(p, "IsRealDataType:integer;",&p)) {
  438. if (atoi(p) == 1)
  439. rt->transport = RTSP_TRANSPORT_RDT;
  440. } else if (av_strstart(p, "SampleRate:integer;", &p) &&
  441. s->nb_streams > 0) {
  442. st = s->streams[s->nb_streams - 1];
  443. st->codec->sample_rate = atoi(p);
  444. } else {
  445. if (rt->server_type == RTSP_SERVER_WMS)
  446. ff_wms_parse_sdp_a_line(s, p);
  447. if (s->nb_streams > 0) {
  448. rtsp_st = rt->rtsp_streams[rt->nb_rtsp_streams - 1];
  449. if (rt->server_type == RTSP_SERVER_REAL)
  450. ff_real_parse_sdp_a_line(s, rtsp_st->stream_index, p);
  451. if (rtsp_st->dynamic_handler &&
  452. rtsp_st->dynamic_handler->parse_sdp_a_line)
  453. rtsp_st->dynamic_handler->parse_sdp_a_line(s,
  454. rtsp_st->stream_index,
  455. rtsp_st->dynamic_protocol_context, buf);
  456. }
  457. }
  458. break;
  459. }
  460. }
  461. int ff_sdp_parse(AVFormatContext *s, const char *content)
  462. {
  463. RTSPState *rt = s->priv_data;
  464. const char *p;
  465. int letter;
  466. /* Some SDP lines, particularly for Realmedia or ASF RTSP streams,
  467. * contain long SDP lines containing complete ASF Headers (several
  468. * kB) or arrays of MDPR (RM stream descriptor) headers plus
  469. * "rulebooks" describing their properties. Therefore, the SDP line
  470. * buffer is large.
  471. *
  472. * The Vorbis FMTP line can be up to 16KB - see xiph_parse_sdp_line
  473. * in rtpdec_xiph.c. */
  474. char buf[16384], *q;
  475. SDPParseState sdp_parse_state = { { 0 } }, *s1 = &sdp_parse_state;
  476. p = content;
  477. for (;;) {
  478. p += strspn(p, SPACE_CHARS);
  479. letter = *p;
  480. if (letter == '\0')
  481. break;
  482. p++;
  483. if (*p != '=')
  484. goto next_line;
  485. p++;
  486. /* get the content */
  487. q = buf;
  488. while (*p != '\n' && *p != '\r' && *p != '\0') {
  489. if ((q - buf) < sizeof(buf) - 1)
  490. *q++ = *p;
  491. p++;
  492. }
  493. *q = '\0';
  494. sdp_parse_line(s, s1, letter, buf);
  495. next_line:
  496. while (*p != '\n' && *p != '\0')
  497. p++;
  498. if (*p == '\n')
  499. p++;
  500. }
  501. rt->p = av_malloc(sizeof(struct pollfd)*2*(rt->nb_rtsp_streams+1));
  502. if (!rt->p) return AVERROR(ENOMEM);
  503. return 0;
  504. }
  505. #endif /* CONFIG_RTPDEC */
  506. void ff_rtsp_undo_setup(AVFormatContext *s)
  507. {
  508. RTSPState *rt = s->priv_data;
  509. int i;
  510. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  511. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  512. if (!rtsp_st)
  513. continue;
  514. if (rtsp_st->transport_priv) {
  515. if (s->oformat) {
  516. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  517. av_write_trailer(rtpctx);
  518. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  519. uint8_t *ptr;
  520. avio_close_dyn_buf(rtpctx->pb, &ptr);
  521. av_free(ptr);
  522. } else {
  523. avio_close(rtpctx->pb);
  524. }
  525. avformat_free_context(rtpctx);
  526. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  527. ff_rdt_parse_close(rtsp_st->transport_priv);
  528. else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC)
  529. ff_rtp_parse_close(rtsp_st->transport_priv);
  530. }
  531. rtsp_st->transport_priv = NULL;
  532. if (rtsp_st->rtp_handle)
  533. ffurl_close(rtsp_st->rtp_handle);
  534. rtsp_st->rtp_handle = NULL;
  535. }
  536. }
  537. /* close and free RTSP streams */
  538. void ff_rtsp_close_streams(AVFormatContext *s)
  539. {
  540. RTSPState *rt = s->priv_data;
  541. int i;
  542. RTSPStream *rtsp_st;
  543. ff_rtsp_undo_setup(s);
  544. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  545. rtsp_st = rt->rtsp_streams[i];
  546. if (rtsp_st) {
  547. if (rtsp_st->dynamic_handler && rtsp_st->dynamic_protocol_context)
  548. rtsp_st->dynamic_handler->free(
  549. rtsp_st->dynamic_protocol_context);
  550. av_free(rtsp_st);
  551. }
  552. }
  553. av_free(rt->rtsp_streams);
  554. if (rt->asf_ctx) {
  555. avformat_close_input(&rt->asf_ctx);
  556. }
  557. if (rt->ts && CONFIG_RTPDEC)
  558. ff_mpegts_parse_close(rt->ts);
  559. av_free(rt->p);
  560. av_free(rt->recvbuf);
  561. }
  562. int ff_rtsp_open_transport_ctx(AVFormatContext *s, RTSPStream *rtsp_st)
  563. {
  564. RTSPState *rt = s->priv_data;
  565. AVStream *st = NULL;
  566. /* open the RTP context */
  567. if (rtsp_st->stream_index >= 0)
  568. st = s->streams[rtsp_st->stream_index];
  569. if (!st)
  570. s->ctx_flags |= AVFMTCTX_NOHEADER;
  571. if (s->oformat && CONFIG_RTSP_MUXER) {
  572. int ret = ff_rtp_chain_mux_open((AVFormatContext **)&rtsp_st->transport_priv, s, st,
  573. rtsp_st->rtp_handle,
  574. RTSP_TCP_MAX_PACKET_SIZE);
  575. /* Ownership of rtp_handle is passed to the rtp mux context */
  576. rtsp_st->rtp_handle = NULL;
  577. if (ret < 0)
  578. return ret;
  579. } else if (rt->transport == RTSP_TRANSPORT_RAW) {
  580. return 0; // Don't need to open any parser here
  581. } else if (rt->transport == RTSP_TRANSPORT_RDT && CONFIG_RTPDEC)
  582. rtsp_st->transport_priv = ff_rdt_parse_open(s, st->index,
  583. rtsp_st->dynamic_protocol_context,
  584. rtsp_st->dynamic_handler);
  585. else if (CONFIG_RTPDEC)
  586. rtsp_st->transport_priv = ff_rtp_parse_open(s, st, rtsp_st->rtp_handle,
  587. rtsp_st->sdp_payload_type,
  588. (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP || !s->max_delay)
  589. ? 0 : RTP_REORDER_QUEUE_DEFAULT_SIZE);
  590. if (!rtsp_st->transport_priv) {
  591. return AVERROR(ENOMEM);
  592. } else if (rt->transport == RTSP_TRANSPORT_RTP && CONFIG_RTPDEC) {
  593. if (rtsp_st->dynamic_handler) {
  594. ff_rtp_parse_set_dynamic_protocol(rtsp_st->transport_priv,
  595. rtsp_st->dynamic_protocol_context,
  596. rtsp_st->dynamic_handler);
  597. }
  598. }
  599. return 0;
  600. }
  601. #if CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER
  602. static void rtsp_parse_range(int *min_ptr, int *max_ptr, const char **pp)
  603. {
  604. const char *q;
  605. char *p;
  606. int v;
  607. q = *pp;
  608. q += strspn(q, SPACE_CHARS);
  609. v = strtol(q, &p, 10);
  610. if (*p == '-') {
  611. p++;
  612. *min_ptr = v;
  613. v = strtol(p, &p, 10);
  614. *max_ptr = v;
  615. } else {
  616. *min_ptr = v;
  617. *max_ptr = v;
  618. }
  619. *pp = p;
  620. }
  621. /* XXX: only one transport specification is parsed */
  622. static void rtsp_parse_transport(RTSPMessageHeader *reply, const char *p)
  623. {
  624. char transport_protocol[16];
  625. char profile[16];
  626. char lower_transport[16];
  627. char parameter[16];
  628. RTSPTransportField *th;
  629. char buf[256];
  630. reply->nb_transports = 0;
  631. for (;;) {
  632. p += strspn(p, SPACE_CHARS);
  633. if (*p == '\0')
  634. break;
  635. th = &reply->transports[reply->nb_transports];
  636. get_word_sep(transport_protocol, sizeof(transport_protocol),
  637. "/", &p);
  638. if (!av_strcasecmp (transport_protocol, "rtp")) {
  639. get_word_sep(profile, sizeof(profile), "/;,", &p);
  640. lower_transport[0] = '\0';
  641. /* rtp/avp/<protocol> */
  642. if (*p == '/') {
  643. get_word_sep(lower_transport, sizeof(lower_transport),
  644. ";,", &p);
  645. }
  646. th->transport = RTSP_TRANSPORT_RTP;
  647. } else if (!av_strcasecmp (transport_protocol, "x-pn-tng") ||
  648. !av_strcasecmp (transport_protocol, "x-real-rdt")) {
  649. /* x-pn-tng/<protocol> */
  650. get_word_sep(lower_transport, sizeof(lower_transport), "/;,", &p);
  651. profile[0] = '\0';
  652. th->transport = RTSP_TRANSPORT_RDT;
  653. } else if (!av_strcasecmp(transport_protocol, "raw")) {
  654. get_word_sep(profile, sizeof(profile), "/;,", &p);
  655. lower_transport[0] = '\0';
  656. /* raw/raw/<protocol> */
  657. if (*p == '/') {
  658. get_word_sep(lower_transport, sizeof(lower_transport),
  659. ";,", &p);
  660. }
  661. th->transport = RTSP_TRANSPORT_RAW;
  662. }
  663. if (!av_strcasecmp(lower_transport, "TCP"))
  664. th->lower_transport = RTSP_LOWER_TRANSPORT_TCP;
  665. else
  666. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP;
  667. if (*p == ';')
  668. p++;
  669. /* get each parameter */
  670. while (*p != '\0' && *p != ',') {
  671. get_word_sep(parameter, sizeof(parameter), "=;,", &p);
  672. if (!strcmp(parameter, "port")) {
  673. if (*p == '=') {
  674. p++;
  675. rtsp_parse_range(&th->port_min, &th->port_max, &p);
  676. }
  677. } else if (!strcmp(parameter, "client_port")) {
  678. if (*p == '=') {
  679. p++;
  680. rtsp_parse_range(&th->client_port_min,
  681. &th->client_port_max, &p);
  682. }
  683. } else if (!strcmp(parameter, "server_port")) {
  684. if (*p == '=') {
  685. p++;
  686. rtsp_parse_range(&th->server_port_min,
  687. &th->server_port_max, &p);
  688. }
  689. } else if (!strcmp(parameter, "interleaved")) {
  690. if (*p == '=') {
  691. p++;
  692. rtsp_parse_range(&th->interleaved_min,
  693. &th->interleaved_max, &p);
  694. }
  695. } else if (!strcmp(parameter, "multicast")) {
  696. if (th->lower_transport == RTSP_LOWER_TRANSPORT_UDP)
  697. th->lower_transport = RTSP_LOWER_TRANSPORT_UDP_MULTICAST;
  698. } else if (!strcmp(parameter, "ttl")) {
  699. if (*p == '=') {
  700. p++;
  701. th->ttl = strtol(p, (char **)&p, 10);
  702. }
  703. } else if (!strcmp(parameter, "destination")) {
  704. if (*p == '=') {
  705. p++;
  706. get_word_sep(buf, sizeof(buf), ";,", &p);
  707. get_sockaddr(buf, &th->destination);
  708. }
  709. } else if (!strcmp(parameter, "source")) {
  710. if (*p == '=') {
  711. p++;
  712. get_word_sep(buf, sizeof(buf), ";,", &p);
  713. av_strlcpy(th->source, buf, sizeof(th->source));
  714. }
  715. } else if (!strcmp(parameter, "mode")) {
  716. if (*p == '=') {
  717. p++;
  718. get_word_sep(buf, sizeof(buf), ";, ", &p);
  719. if (!strcmp(buf, "record") ||
  720. !strcmp(buf, "receive"))
  721. th->mode_record = 1;
  722. }
  723. }
  724. while (*p != ';' && *p != '\0' && *p != ',')
  725. p++;
  726. if (*p == ';')
  727. p++;
  728. }
  729. if (*p == ',')
  730. p++;
  731. reply->nb_transports++;
  732. }
  733. }
  734. static void handle_rtp_info(RTSPState *rt, const char *url,
  735. uint32_t seq, uint32_t rtptime)
  736. {
  737. int i;
  738. if (!rtptime || !url[0])
  739. return;
  740. if (rt->transport != RTSP_TRANSPORT_RTP)
  741. return;
  742. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  743. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  744. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  745. if (!rtpctx)
  746. continue;
  747. if (!strcmp(rtsp_st->control_url, url)) {
  748. rtpctx->base_timestamp = rtptime;
  749. break;
  750. }
  751. }
  752. }
  753. static void rtsp_parse_rtp_info(RTSPState *rt, const char *p)
  754. {
  755. int read = 0;
  756. char key[20], value[1024], url[1024] = "";
  757. uint32_t seq = 0, rtptime = 0;
  758. for (;;) {
  759. p += strspn(p, SPACE_CHARS);
  760. if (!*p)
  761. break;
  762. get_word_sep(key, sizeof(key), "=", &p);
  763. if (*p != '=')
  764. break;
  765. p++;
  766. get_word_sep(value, sizeof(value), ";, ", &p);
  767. read++;
  768. if (!strcmp(key, "url"))
  769. av_strlcpy(url, value, sizeof(url));
  770. else if (!strcmp(key, "seq"))
  771. seq = strtoul(value, NULL, 10);
  772. else if (!strcmp(key, "rtptime"))
  773. rtptime = strtoul(value, NULL, 10);
  774. if (*p == ',') {
  775. handle_rtp_info(rt, url, seq, rtptime);
  776. url[0] = '\0';
  777. seq = rtptime = 0;
  778. read = 0;
  779. }
  780. if (*p)
  781. p++;
  782. }
  783. if (read > 0)
  784. handle_rtp_info(rt, url, seq, rtptime);
  785. }
  786. void ff_rtsp_parse_line(RTSPMessageHeader *reply, const char *buf,
  787. RTSPState *rt, const char *method)
  788. {
  789. const char *p;
  790. /* NOTE: we do case independent match for broken servers */
  791. p = buf;
  792. if (av_stristart(p, "Session:", &p)) {
  793. int t;
  794. get_word_sep(reply->session_id, sizeof(reply->session_id), ";", &p);
  795. if (av_stristart(p, ";timeout=", &p) &&
  796. (t = strtol(p, NULL, 10)) > 0) {
  797. reply->timeout = t;
  798. }
  799. } else if (av_stristart(p, "Content-Length:", &p)) {
  800. reply->content_length = strtol(p, NULL, 10);
  801. } else if (av_stristart(p, "Transport:", &p)) {
  802. rtsp_parse_transport(reply, p);
  803. } else if (av_stristart(p, "CSeq:", &p)) {
  804. reply->seq = strtol(p, NULL, 10);
  805. } else if (av_stristart(p, "Range:", &p)) {
  806. rtsp_parse_range_npt(p, &reply->range_start, &reply->range_end);
  807. } else if (av_stristart(p, "RealChallenge1:", &p)) {
  808. p += strspn(p, SPACE_CHARS);
  809. av_strlcpy(reply->real_challenge, p, sizeof(reply->real_challenge));
  810. } else if (av_stristart(p, "Server:", &p)) {
  811. p += strspn(p, SPACE_CHARS);
  812. av_strlcpy(reply->server, p, sizeof(reply->server));
  813. } else if (av_stristart(p, "Notice:", &p) ||
  814. av_stristart(p, "X-Notice:", &p)) {
  815. reply->notice = strtol(p, NULL, 10);
  816. } else if (av_stristart(p, "Location:", &p)) {
  817. p += strspn(p, SPACE_CHARS);
  818. av_strlcpy(reply->location, p , sizeof(reply->location));
  819. } else if (av_stristart(p, "WWW-Authenticate:", &p) && rt) {
  820. p += strspn(p, SPACE_CHARS);
  821. ff_http_auth_handle_header(&rt->auth_state, "WWW-Authenticate", p);
  822. } else if (av_stristart(p, "Authentication-Info:", &p) && rt) {
  823. p += strspn(p, SPACE_CHARS);
  824. ff_http_auth_handle_header(&rt->auth_state, "Authentication-Info", p);
  825. } else if (av_stristart(p, "Content-Base:", &p) && rt) {
  826. p += strspn(p, SPACE_CHARS);
  827. if (method && !strcmp(method, "DESCRIBE"))
  828. av_strlcpy(rt->control_uri, p , sizeof(rt->control_uri));
  829. } else if (av_stristart(p, "RTP-Info:", &p) && rt) {
  830. p += strspn(p, SPACE_CHARS);
  831. if (method && !strcmp(method, "PLAY"))
  832. rtsp_parse_rtp_info(rt, p);
  833. } else if (av_stristart(p, "Public:", &p) && rt) {
  834. if (strstr(p, "GET_PARAMETER") &&
  835. method && !strcmp(method, "OPTIONS"))
  836. rt->get_parameter_supported = 1;
  837. } else if (av_stristart(p, "x-Accept-Dynamic-Rate:", &p) && rt) {
  838. p += strspn(p, SPACE_CHARS);
  839. rt->accept_dynamic_rate = atoi(p);
  840. } else if (av_stristart(p, "Content-Type:", &p)) {
  841. p += strspn(p, SPACE_CHARS);
  842. av_strlcpy(reply->content_type, p, sizeof(reply->content_type));
  843. }
  844. }
  845. /* skip a RTP/TCP interleaved packet */
  846. void ff_rtsp_skip_packet(AVFormatContext *s)
  847. {
  848. RTSPState *rt = s->priv_data;
  849. int ret, len, len1;
  850. uint8_t buf[1024];
  851. ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
  852. if (ret != 3)
  853. return;
  854. len = AV_RB16(buf + 1);
  855. av_dlog(s, "skipping RTP packet len=%d\n", len);
  856. /* skip payload */
  857. while (len > 0) {
  858. len1 = len;
  859. if (len1 > sizeof(buf))
  860. len1 = sizeof(buf);
  861. ret = ffurl_read_complete(rt->rtsp_hd, buf, len1);
  862. if (ret != len1)
  863. return;
  864. len -= len1;
  865. }
  866. }
  867. int ff_rtsp_read_reply(AVFormatContext *s, RTSPMessageHeader *reply,
  868. unsigned char **content_ptr,
  869. int return_on_interleaved_data, const char *method)
  870. {
  871. RTSPState *rt = s->priv_data;
  872. char buf[4096], buf1[1024], *q;
  873. unsigned char ch;
  874. const char *p;
  875. int ret, content_length, line_count = 0, request = 0;
  876. unsigned char *content = NULL;
  877. start:
  878. line_count = 0;
  879. request = 0;
  880. content = NULL;
  881. memset(reply, 0, sizeof(*reply));
  882. /* parse reply (XXX: use buffers) */
  883. rt->last_reply[0] = '\0';
  884. for (;;) {
  885. q = buf;
  886. for (;;) {
  887. ret = ffurl_read_complete(rt->rtsp_hd, &ch, 1);
  888. av_dlog(s, "ret=%d c=%02x [%c]\n", ret, ch, ch);
  889. if (ret != 1)
  890. return AVERROR_EOF;
  891. if (ch == '\n')
  892. break;
  893. if (ch == '$') {
  894. /* XXX: only parse it if first char on line ? */
  895. if (return_on_interleaved_data) {
  896. return 1;
  897. } else
  898. ff_rtsp_skip_packet(s);
  899. } else if (ch != '\r') {
  900. if ((q - buf) < sizeof(buf) - 1)
  901. *q++ = ch;
  902. }
  903. }
  904. *q = '\0';
  905. av_dlog(s, "line='%s'\n", buf);
  906. /* test if last line */
  907. if (buf[0] == '\0')
  908. break;
  909. p = buf;
  910. if (line_count == 0) {
  911. /* get reply code */
  912. get_word(buf1, sizeof(buf1), &p);
  913. if (!strncmp(buf1, "RTSP/", 5)) {
  914. get_word(buf1, sizeof(buf1), &p);
  915. reply->status_code = atoi(buf1);
  916. av_strlcpy(reply->reason, p, sizeof(reply->reason));
  917. } else {
  918. av_strlcpy(reply->reason, buf1, sizeof(reply->reason)); // method
  919. get_word(buf1, sizeof(buf1), &p); // object
  920. request = 1;
  921. }
  922. } else {
  923. ff_rtsp_parse_line(reply, p, rt, method);
  924. av_strlcat(rt->last_reply, p, sizeof(rt->last_reply));
  925. av_strlcat(rt->last_reply, "\n", sizeof(rt->last_reply));
  926. }
  927. line_count++;
  928. }
  929. if (rt->session_id[0] == '\0' && reply->session_id[0] != '\0' && !request)
  930. av_strlcpy(rt->session_id, reply->session_id, sizeof(rt->session_id));
  931. content_length = reply->content_length;
  932. if (content_length > 0) {
  933. /* leave some room for a trailing '\0' (useful for simple parsing) */
  934. content = av_malloc(content_length + 1);
  935. ffurl_read_complete(rt->rtsp_hd, content, content_length);
  936. content[content_length] = '\0';
  937. }
  938. if (content_ptr)
  939. *content_ptr = content;
  940. else
  941. av_free(content);
  942. if (request) {
  943. char buf[1024];
  944. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  945. const char* ptr = buf;
  946. if (!strcmp(reply->reason, "OPTIONS")) {
  947. snprintf(buf, sizeof(buf), "RTSP/1.0 200 OK\r\n");
  948. if (reply->seq)
  949. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", reply->seq);
  950. if (reply->session_id[0])
  951. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n",
  952. reply->session_id);
  953. } else {
  954. snprintf(buf, sizeof(buf), "RTSP/1.0 501 Not Implemented\r\n");
  955. }
  956. av_strlcat(buf, "\r\n", sizeof(buf));
  957. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  958. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  959. ptr = base64buf;
  960. }
  961. ffurl_write(rt->rtsp_hd_out, ptr, strlen(ptr));
  962. rt->last_cmd_time = av_gettime();
  963. /* Even if the request from the server had data, it is not the data
  964. * that the caller wants or expects. The memory could also be leaked
  965. * if the actual following reply has content data. */
  966. if (content_ptr)
  967. av_freep(content_ptr);
  968. /* If method is set, this is called from ff_rtsp_send_cmd,
  969. * where a reply to exactly this request is awaited. For
  970. * callers from within packet receiving, we just want to
  971. * return to the caller and go back to receiving packets. */
  972. if (method)
  973. goto start;
  974. return 0;
  975. }
  976. if (rt->seq != reply->seq) {
  977. av_log(s, AV_LOG_WARNING, "CSeq %d expected, %d received.\n",
  978. rt->seq, reply->seq);
  979. }
  980. /* EOS */
  981. if (reply->notice == 2101 /* End-of-Stream Reached */ ||
  982. reply->notice == 2104 /* Start-of-Stream Reached */ ||
  983. reply->notice == 2306 /* Continuous Feed Terminated */) {
  984. rt->state = RTSP_STATE_IDLE;
  985. } else if (reply->notice >= 4400 && reply->notice < 5500) {
  986. return AVERROR(EIO); /* data or server error */
  987. } else if (reply->notice == 2401 /* Ticket Expired */ ||
  988. (reply->notice >= 5500 && reply->notice < 5600) /* end of term */ )
  989. return AVERROR(EPERM);
  990. return 0;
  991. }
  992. /**
  993. * Send a command to the RTSP server without waiting for the reply.
  994. *
  995. * @param s RTSP (de)muxer context
  996. * @param method the method for the request
  997. * @param url the target url for the request
  998. * @param headers extra header lines to include in the request
  999. * @param send_content if non-null, the data to send as request body content
  1000. * @param send_content_length the length of the send_content data, or 0 if
  1001. * send_content is null
  1002. *
  1003. * @return zero if success, nonzero otherwise
  1004. */
  1005. static int ff_rtsp_send_cmd_with_content_async(AVFormatContext *s,
  1006. const char *method, const char *url,
  1007. const char *headers,
  1008. const unsigned char *send_content,
  1009. int send_content_length)
  1010. {
  1011. RTSPState *rt = s->priv_data;
  1012. char buf[4096], *out_buf;
  1013. char base64buf[AV_BASE64_SIZE(sizeof(buf))];
  1014. /* Add in RTSP headers */
  1015. out_buf = buf;
  1016. rt->seq++;
  1017. snprintf(buf, sizeof(buf), "%s %s RTSP/1.0\r\n", method, url);
  1018. if (headers)
  1019. av_strlcat(buf, headers, sizeof(buf));
  1020. av_strlcatf(buf, sizeof(buf), "CSeq: %d\r\n", rt->seq);
  1021. if (rt->session_id[0] != '\0' && (!headers ||
  1022. !strstr(headers, "\nIf-Match:"))) {
  1023. av_strlcatf(buf, sizeof(buf), "Session: %s\r\n", rt->session_id);
  1024. }
  1025. if (rt->auth[0]) {
  1026. char *str = ff_http_auth_create_response(&rt->auth_state,
  1027. rt->auth, url, method);
  1028. if (str)
  1029. av_strlcat(buf, str, sizeof(buf));
  1030. av_free(str);
  1031. }
  1032. if (send_content_length > 0 && send_content)
  1033. av_strlcatf(buf, sizeof(buf), "Content-Length: %d\r\n", send_content_length);
  1034. av_strlcat(buf, "\r\n", sizeof(buf));
  1035. /* base64 encode rtsp if tunneling */
  1036. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1037. av_base64_encode(base64buf, sizeof(base64buf), buf, strlen(buf));
  1038. out_buf = base64buf;
  1039. }
  1040. av_dlog(s, "Sending:\n%s--\n", buf);
  1041. ffurl_write(rt->rtsp_hd_out, out_buf, strlen(out_buf));
  1042. if (send_content_length > 0 && send_content) {
  1043. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1044. av_log(s, AV_LOG_ERROR, "tunneling of RTSP requests "
  1045. "with content data not supported\n");
  1046. return AVERROR_PATCHWELCOME;
  1047. }
  1048. ffurl_write(rt->rtsp_hd_out, send_content, send_content_length);
  1049. }
  1050. rt->last_cmd_time = av_gettime();
  1051. return 0;
  1052. }
  1053. int ff_rtsp_send_cmd_async(AVFormatContext *s, const char *method,
  1054. const char *url, const char *headers)
  1055. {
  1056. return ff_rtsp_send_cmd_with_content_async(s, method, url, headers, NULL, 0);
  1057. }
  1058. int ff_rtsp_send_cmd(AVFormatContext *s, const char *method, const char *url,
  1059. const char *headers, RTSPMessageHeader *reply,
  1060. unsigned char **content_ptr)
  1061. {
  1062. return ff_rtsp_send_cmd_with_content(s, method, url, headers, reply,
  1063. content_ptr, NULL, 0);
  1064. }
  1065. int ff_rtsp_send_cmd_with_content(AVFormatContext *s,
  1066. const char *method, const char *url,
  1067. const char *header,
  1068. RTSPMessageHeader *reply,
  1069. unsigned char **content_ptr,
  1070. const unsigned char *send_content,
  1071. int send_content_length)
  1072. {
  1073. RTSPState *rt = s->priv_data;
  1074. HTTPAuthType cur_auth_type;
  1075. int ret, attempts = 0;
  1076. retry:
  1077. cur_auth_type = rt->auth_state.auth_type;
  1078. if ((ret = ff_rtsp_send_cmd_with_content_async(s, method, url, header,
  1079. send_content,
  1080. send_content_length)))
  1081. return ret;
  1082. if ((ret = ff_rtsp_read_reply(s, reply, content_ptr, 0, method) ) < 0)
  1083. return ret;
  1084. attempts++;
  1085. if (reply->status_code == 401 &&
  1086. (cur_auth_type == HTTP_AUTH_NONE || rt->auth_state.stale) &&
  1087. rt->auth_state.auth_type != HTTP_AUTH_NONE && attempts < 2)
  1088. goto retry;
  1089. if (reply->status_code > 400){
  1090. av_log(s, AV_LOG_ERROR, "method %s failed: %d%s\n",
  1091. method,
  1092. reply->status_code,
  1093. reply->reason);
  1094. av_log(s, AV_LOG_DEBUG, "%s\n", rt->last_reply);
  1095. }
  1096. return 0;
  1097. }
  1098. int ff_rtsp_make_setup_request(AVFormatContext *s, const char *host, int port,
  1099. int lower_transport, const char *real_challenge)
  1100. {
  1101. RTSPState *rt = s->priv_data;
  1102. int rtx = 0, j, i, err, interleave = 0, port_off;
  1103. RTSPStream *rtsp_st;
  1104. RTSPMessageHeader reply1, *reply = &reply1;
  1105. char cmd[2048];
  1106. const char *trans_pref;
  1107. if (rt->transport == RTSP_TRANSPORT_RDT)
  1108. trans_pref = "x-pn-tng";
  1109. else if (rt->transport == RTSP_TRANSPORT_RAW)
  1110. trans_pref = "RAW/RAW";
  1111. else
  1112. trans_pref = "RTP/AVP";
  1113. /* default timeout: 1 minute */
  1114. rt->timeout = 60;
  1115. /* Choose a random starting offset within the first half of the
  1116. * port range, to allow for a number of ports to try even if the offset
  1117. * happens to be at the end of the random range. */
  1118. port_off = av_get_random_seed() % ((rt->rtp_port_max - rt->rtp_port_min)/2);
  1119. /* even random offset */
  1120. port_off -= port_off & 0x01;
  1121. for (j = rt->rtp_port_min + port_off, i = 0; i < rt->nb_rtsp_streams; ++i) {
  1122. char transport[2048];
  1123. /*
  1124. * WMS serves all UDP data over a single connection, the RTX, which
  1125. * isn't necessarily the first in the SDP but has to be the first
  1126. * to be set up, else the second/third SETUP will fail with a 461.
  1127. */
  1128. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  1129. rt->server_type == RTSP_SERVER_WMS) {
  1130. if (i == 0) {
  1131. /* rtx first */
  1132. for (rtx = 0; rtx < rt->nb_rtsp_streams; rtx++) {
  1133. int len = strlen(rt->rtsp_streams[rtx]->control_url);
  1134. if (len >= 4 &&
  1135. !strcmp(rt->rtsp_streams[rtx]->control_url + len - 4,
  1136. "/rtx"))
  1137. break;
  1138. }
  1139. if (rtx == rt->nb_rtsp_streams)
  1140. return -1; /* no RTX found */
  1141. rtsp_st = rt->rtsp_streams[rtx];
  1142. } else
  1143. rtsp_st = rt->rtsp_streams[i > rtx ? i : i - 1];
  1144. } else
  1145. rtsp_st = rt->rtsp_streams[i];
  1146. /* RTP/UDP */
  1147. if (lower_transport == RTSP_LOWER_TRANSPORT_UDP) {
  1148. char buf[256];
  1149. if (rt->server_type == RTSP_SERVER_WMS && i > 1) {
  1150. port = reply->transports[0].client_port_min;
  1151. goto have_port;
  1152. }
  1153. /* first try in specified port range */
  1154. while (j <= rt->rtp_port_max) {
  1155. ff_url_join(buf, sizeof(buf), "rtp", NULL, host, -1,
  1156. "?localport=%d", j);
  1157. /* we will use two ports per rtp stream (rtp and rtcp) */
  1158. j += 2;
  1159. if (!ffurl_open(&rtsp_st->rtp_handle, buf, AVIO_FLAG_READ_WRITE,
  1160. &s->interrupt_callback, NULL))
  1161. goto rtp_opened;
  1162. }
  1163. av_log(s, AV_LOG_ERROR, "Unable to open an input RTP port\n");
  1164. err = AVERROR(EIO);
  1165. goto fail;
  1166. rtp_opened:
  1167. port = ff_rtp_get_local_rtp_port(rtsp_st->rtp_handle);
  1168. have_port:
  1169. snprintf(transport, sizeof(transport) - 1,
  1170. "%s/UDP;", trans_pref);
  1171. if (rt->server_type != RTSP_SERVER_REAL)
  1172. av_strlcat(transport, "unicast;", sizeof(transport));
  1173. av_strlcatf(transport, sizeof(transport),
  1174. "client_port=%d", port);
  1175. if (rt->transport == RTSP_TRANSPORT_RTP &&
  1176. !(rt->server_type == RTSP_SERVER_WMS && i > 0))
  1177. av_strlcatf(transport, sizeof(transport), "-%d", port + 1);
  1178. }
  1179. /* RTP/TCP */
  1180. else if (lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  1181. /* For WMS streams, the application streams are only used for
  1182. * UDP. When trying to set it up for TCP streams, the server
  1183. * will return an error. Therefore, we skip those streams. */
  1184. if (rt->server_type == RTSP_SERVER_WMS &&
  1185. (rtsp_st->stream_index < 0 ||
  1186. s->streams[rtsp_st->stream_index]->codec->codec_type ==
  1187. AVMEDIA_TYPE_DATA))
  1188. continue;
  1189. snprintf(transport, sizeof(transport) - 1,
  1190. "%s/TCP;", trans_pref);
  1191. if (rt->transport != RTSP_TRANSPORT_RDT)
  1192. av_strlcat(transport, "unicast;", sizeof(transport));
  1193. av_strlcatf(transport, sizeof(transport),
  1194. "interleaved=%d-%d",
  1195. interleave, interleave + 1);
  1196. interleave += 2;
  1197. }
  1198. else if (lower_transport == RTSP_LOWER_TRANSPORT_UDP_MULTICAST) {
  1199. snprintf(transport, sizeof(transport) - 1,
  1200. "%s/UDP;multicast", trans_pref);
  1201. }
  1202. if (s->oformat) {
  1203. av_strlcat(transport, ";mode=record", sizeof(transport));
  1204. } else if (rt->server_type == RTSP_SERVER_REAL ||
  1205. rt->server_type == RTSP_SERVER_WMS)
  1206. av_strlcat(transport, ";mode=play", sizeof(transport));
  1207. snprintf(cmd, sizeof(cmd),
  1208. "Transport: %s\r\n",
  1209. transport);
  1210. if (rt->accept_dynamic_rate)
  1211. av_strlcat(cmd, "x-Dynamic-Rate: 0\r\n", sizeof(cmd));
  1212. if (i == 0 && rt->server_type == RTSP_SERVER_REAL && CONFIG_RTPDEC) {
  1213. char real_res[41], real_csum[9];
  1214. ff_rdt_calc_response_and_checksum(real_res, real_csum,
  1215. real_challenge);
  1216. av_strlcatf(cmd, sizeof(cmd),
  1217. "If-Match: %s\r\n"
  1218. "RealChallenge2: %s, sd=%s\r\n",
  1219. rt->session_id, real_res, real_csum);
  1220. }
  1221. ff_rtsp_send_cmd(s, "SETUP", rtsp_st->control_url, cmd, reply, NULL);
  1222. if (reply->status_code == 461 /* Unsupported protocol */ && i == 0) {
  1223. err = 1;
  1224. goto fail;
  1225. } else if (reply->status_code != RTSP_STATUS_OK ||
  1226. reply->nb_transports != 1) {
  1227. err = AVERROR_INVALIDDATA;
  1228. goto fail;
  1229. }
  1230. /* XXX: same protocol for all streams is required */
  1231. if (i > 0) {
  1232. if (reply->transports[0].lower_transport != rt->lower_transport ||
  1233. reply->transports[0].transport != rt->transport) {
  1234. err = AVERROR_INVALIDDATA;
  1235. goto fail;
  1236. }
  1237. } else {
  1238. rt->lower_transport = reply->transports[0].lower_transport;
  1239. rt->transport = reply->transports[0].transport;
  1240. }
  1241. /* Fail if the server responded with another lower transport mode
  1242. * than what we requested. */
  1243. if (reply->transports[0].lower_transport != lower_transport) {
  1244. av_log(s, AV_LOG_ERROR, "Nonmatching transport in server reply\n");
  1245. err = AVERROR_INVALIDDATA;
  1246. goto fail;
  1247. }
  1248. switch(reply->transports[0].lower_transport) {
  1249. case RTSP_LOWER_TRANSPORT_TCP:
  1250. rtsp_st->interleaved_min = reply->transports[0].interleaved_min;
  1251. rtsp_st->interleaved_max = reply->transports[0].interleaved_max;
  1252. break;
  1253. case RTSP_LOWER_TRANSPORT_UDP: {
  1254. char url[1024], options[30] = "";
  1255. if (rt->rtsp_flags & RTSP_FLAG_FILTER_SRC)
  1256. av_strlcpy(options, "?connect=1", sizeof(options));
  1257. /* Use source address if specified */
  1258. if (reply->transports[0].source[0]) {
  1259. ff_url_join(url, sizeof(url), "rtp", NULL,
  1260. reply->transports[0].source,
  1261. reply->transports[0].server_port_min, "%s", options);
  1262. } else {
  1263. ff_url_join(url, sizeof(url), "rtp", NULL, host,
  1264. reply->transports[0].server_port_min, "%s", options);
  1265. }
  1266. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) &&
  1267. ff_rtp_set_remote_url(rtsp_st->rtp_handle, url) < 0) {
  1268. err = AVERROR_INVALIDDATA;
  1269. goto fail;
  1270. }
  1271. /* Try to initialize the connection state in a
  1272. * potential NAT router by sending dummy packets.
  1273. * RTP/RTCP dummy packets are used for RDT, too.
  1274. */
  1275. if (!(rt->server_type == RTSP_SERVER_WMS && i > 1) && s->iformat &&
  1276. CONFIG_RTPDEC)
  1277. ff_rtp_send_punch_packets(rtsp_st->rtp_handle);
  1278. break;
  1279. }
  1280. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST: {
  1281. char url[1024], namebuf[50], optbuf[20] = "";
  1282. struct sockaddr_storage addr;
  1283. int port, ttl;
  1284. if (reply->transports[0].destination.ss_family) {
  1285. addr = reply->transports[0].destination;
  1286. port = reply->transports[0].port_min;
  1287. ttl = reply->transports[0].ttl;
  1288. } else {
  1289. addr = rtsp_st->sdp_ip;
  1290. port = rtsp_st->sdp_port;
  1291. ttl = rtsp_st->sdp_ttl;
  1292. }
  1293. if (ttl > 0)
  1294. snprintf(optbuf, sizeof(optbuf), "?ttl=%d", ttl);
  1295. getnameinfo((struct sockaddr*) &addr, sizeof(addr),
  1296. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1297. ff_url_join(url, sizeof(url), "rtp", NULL, namebuf,
  1298. port, "%s", optbuf);
  1299. if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
  1300. &s->interrupt_callback, NULL) < 0) {
  1301. err = AVERROR_INVALIDDATA;
  1302. goto fail;
  1303. }
  1304. break;
  1305. }
  1306. }
  1307. if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
  1308. goto fail;
  1309. }
  1310. if (rt->nb_rtsp_streams && reply->timeout > 0)
  1311. rt->timeout = reply->timeout;
  1312. if (rt->server_type == RTSP_SERVER_REAL)
  1313. rt->need_subscription = 1;
  1314. return 0;
  1315. fail:
  1316. ff_rtsp_undo_setup(s);
  1317. return err;
  1318. }
  1319. void ff_rtsp_close_connections(AVFormatContext *s)
  1320. {
  1321. RTSPState *rt = s->priv_data;
  1322. if (rt->rtsp_hd_out != rt->rtsp_hd) ffurl_close(rt->rtsp_hd_out);
  1323. ffurl_close(rt->rtsp_hd);
  1324. rt->rtsp_hd = rt->rtsp_hd_out = NULL;
  1325. }
  1326. int ff_rtsp_connect(AVFormatContext *s)
  1327. {
  1328. RTSPState *rt = s->priv_data;
  1329. char host[1024], path[1024], tcpname[1024], cmd[2048], auth[128];
  1330. int port, err, tcp_fd;
  1331. RTSPMessageHeader reply1 = {0}, *reply = &reply1;
  1332. int lower_transport_mask = 0;
  1333. char real_challenge[64] = "";
  1334. struct sockaddr_storage peer;
  1335. socklen_t peer_len = sizeof(peer);
  1336. if (rt->rtp_port_max < rt->rtp_port_min) {
  1337. av_log(s, AV_LOG_ERROR, "Invalid UDP port range, max port %d less "
  1338. "than min port %d\n", rt->rtp_port_max,
  1339. rt->rtp_port_min);
  1340. return AVERROR(EINVAL);
  1341. }
  1342. if (!ff_network_init())
  1343. return AVERROR(EIO);
  1344. if (s->max_delay < 0) /* Not set by the caller */
  1345. s->max_delay = s->iformat ? DEFAULT_REORDERING_DELAY : 0;
  1346. rt->control_transport = RTSP_MODE_PLAIN;
  1347. if (rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_HTTP)) {
  1348. rt->lower_transport_mask = 1 << RTSP_LOWER_TRANSPORT_TCP;
  1349. rt->control_transport = RTSP_MODE_TUNNEL;
  1350. }
  1351. /* Only pass through valid flags from here */
  1352. rt->lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1353. redirect:
  1354. lower_transport_mask = rt->lower_transport_mask;
  1355. /* extract hostname and port */
  1356. av_url_split(NULL, 0, auth, sizeof(auth),
  1357. host, sizeof(host), &port, path, sizeof(path), s->filename);
  1358. if (*auth) {
  1359. av_strlcpy(rt->auth, auth, sizeof(rt->auth));
  1360. }
  1361. if (port < 0)
  1362. port = RTSP_DEFAULT_PORT;
  1363. if (!lower_transport_mask)
  1364. lower_transport_mask = (1 << RTSP_LOWER_TRANSPORT_NB) - 1;
  1365. if (s->oformat) {
  1366. /* Only UDP or TCP - UDP multicast isn't supported. */
  1367. lower_transport_mask &= (1 << RTSP_LOWER_TRANSPORT_UDP) |
  1368. (1 << RTSP_LOWER_TRANSPORT_TCP);
  1369. if (!lower_transport_mask || rt->control_transport == RTSP_MODE_TUNNEL) {
  1370. av_log(s, AV_LOG_ERROR, "Unsupported lower transport method, "
  1371. "only UDP and TCP are supported for output.\n");
  1372. err = AVERROR(EINVAL);
  1373. goto fail;
  1374. }
  1375. }
  1376. /* Construct the URI used in request; this is similar to s->filename,
  1377. * but with authentication credentials removed and RTSP specific options
  1378. * stripped out. */
  1379. ff_url_join(rt->control_uri, sizeof(rt->control_uri), "rtsp", NULL,
  1380. host, port, "%s", path);
  1381. if (rt->control_transport == RTSP_MODE_TUNNEL) {
  1382. /* set up initial handshake for tunneling */
  1383. char httpname[1024];
  1384. char sessioncookie[17];
  1385. char headers[1024];
  1386. ff_url_join(httpname, sizeof(httpname), "http", auth, host, port, "%s", path);
  1387. snprintf(sessioncookie, sizeof(sessioncookie), "%08x%08x",
  1388. av_get_random_seed(), av_get_random_seed());
  1389. /* GET requests */
  1390. if (ffurl_alloc(&rt->rtsp_hd, httpname, AVIO_FLAG_READ,
  1391. &s->interrupt_callback) < 0) {
  1392. err = AVERROR(EIO);
  1393. goto fail;
  1394. }
  1395. /* generate GET headers */
  1396. snprintf(headers, sizeof(headers),
  1397. "x-sessioncookie: %s\r\n"
  1398. "Accept: application/x-rtsp-tunnelled\r\n"
  1399. "Pragma: no-cache\r\n"
  1400. "Cache-Control: no-cache\r\n",
  1401. sessioncookie);
  1402. av_opt_set(rt->rtsp_hd->priv_data, "headers", headers, 0);
  1403. /* complete the connection */
  1404. if (ffurl_connect(rt->rtsp_hd, NULL)) {
  1405. err = AVERROR(EIO);
  1406. goto fail;
  1407. }
  1408. /* POST requests */
  1409. if (ffurl_alloc(&rt->rtsp_hd_out, httpname, AVIO_FLAG_WRITE,
  1410. &s->interrupt_callback) < 0 ) {
  1411. err = AVERROR(EIO);
  1412. goto fail;
  1413. }
  1414. /* generate POST headers */
  1415. snprintf(headers, sizeof(headers),
  1416. "x-sessioncookie: %s\r\n"
  1417. "Content-Type: application/x-rtsp-tunnelled\r\n"
  1418. "Pragma: no-cache\r\n"
  1419. "Cache-Control: no-cache\r\n"
  1420. "Content-Length: 32767\r\n"
  1421. "Expires: Sun, 9 Jan 1972 00:00:00 GMT\r\n",
  1422. sessioncookie);
  1423. av_opt_set(rt->rtsp_hd_out->priv_data, "headers", headers, 0);
  1424. av_opt_set(rt->rtsp_hd_out->priv_data, "chunked_post", "0", 0);
  1425. /* Initialize the authentication state for the POST session. The HTTP
  1426. * protocol implementation doesn't properly handle multi-pass
  1427. * authentication for POST requests, since it would require one of
  1428. * the following:
  1429. * - implementing Expect: 100-continue, which many HTTP servers
  1430. * don't support anyway, even less the RTSP servers that do HTTP
  1431. * tunneling
  1432. * - sending the whole POST data until getting a 401 reply specifying
  1433. * what authentication method to use, then resending all that data
  1434. * - waiting for potential 401 replies directly after sending the
  1435. * POST header (waiting for some unspecified time)
  1436. * Therefore, we copy the full auth state, which works for both basic
  1437. * and digest. (For digest, we would have to synchronize the nonce
  1438. * count variable between the two sessions, if we'd do more requests
  1439. * with the original session, though.)
  1440. */
  1441. ff_http_init_auth_state(rt->rtsp_hd_out, rt->rtsp_hd);
  1442. /* complete the connection */
  1443. if (ffurl_connect(rt->rtsp_hd_out, NULL)) {
  1444. err = AVERROR(EIO);
  1445. goto fail;
  1446. }
  1447. } else {
  1448. /* open the tcp connection */
  1449. ff_url_join(tcpname, sizeof(tcpname), "tcp", NULL, host, port, NULL);
  1450. if (ffurl_open(&rt->rtsp_hd, tcpname, AVIO_FLAG_READ_WRITE,
  1451. &s->interrupt_callback, NULL) < 0) {
  1452. err = AVERROR(EIO);
  1453. goto fail;
  1454. }
  1455. rt->rtsp_hd_out = rt->rtsp_hd;
  1456. }
  1457. rt->seq = 0;
  1458. tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
  1459. if (!getpeername(tcp_fd, (struct sockaddr*) &peer, &peer_len)) {
  1460. getnameinfo((struct sockaddr*) &peer, peer_len, host, sizeof(host),
  1461. NULL, 0, NI_NUMERICHOST);
  1462. }
  1463. /* request options supported by the server; this also detects server
  1464. * type */
  1465. for (rt->server_type = RTSP_SERVER_RTP;;) {
  1466. cmd[0] = 0;
  1467. if (rt->server_type == RTSP_SERVER_REAL)
  1468. av_strlcat(cmd,
  1469. /*
  1470. * The following entries are required for proper
  1471. * streaming from a Realmedia server. They are
  1472. * interdependent in some way although we currently
  1473. * don't quite understand how. Values were copied
  1474. * from mplayer SVN r23589.
  1475. * ClientChallenge is a 16-byte ID in hex
  1476. * CompanyID is a 16-byte ID in base64
  1477. */
  1478. "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7\r\n"
  1479. "PlayerStarttime: [28/03/2003:22:50:23 00:00]\r\n"
  1480. "CompanyID: KnKV4M4I/B2FjJ1TToLycw==\r\n"
  1481. "GUID: 00000000-0000-0000-0000-000000000000\r\n",
  1482. sizeof(cmd));
  1483. ff_rtsp_send_cmd(s, "OPTIONS", rt->control_uri, cmd, reply, NULL);
  1484. if (reply->status_code != RTSP_STATUS_OK) {
  1485. err = AVERROR_INVALIDDATA;
  1486. goto fail;
  1487. }
  1488. /* detect server type if not standard-compliant RTP */
  1489. if (rt->server_type != RTSP_SERVER_REAL && reply->real_challenge[0]) {
  1490. rt->server_type = RTSP_SERVER_REAL;
  1491. continue;
  1492. } else if (!av_strncasecmp(reply->server, "WMServer/", 9)) {
  1493. rt->server_type = RTSP_SERVER_WMS;
  1494. } else if (rt->server_type == RTSP_SERVER_REAL)
  1495. strcpy(real_challenge, reply->real_challenge);
  1496. break;
  1497. }
  1498. if (s->iformat && CONFIG_RTSP_DEMUXER)
  1499. err = ff_rtsp_setup_input_streams(s, reply);
  1500. else if (CONFIG_RTSP_MUXER)
  1501. err = ff_rtsp_setup_output_streams(s, host);
  1502. if (err)
  1503. goto fail;
  1504. do {
  1505. int lower_transport = ff_log2_tab[lower_transport_mask &
  1506. ~(lower_transport_mask - 1)];
  1507. err = ff_rtsp_make_setup_request(s, host, port, lower_transport,
  1508. rt->server_type == RTSP_SERVER_REAL ?
  1509. real_challenge : NULL);
  1510. if (err < 0)
  1511. goto fail;
  1512. lower_transport_mask &= ~(1 << lower_transport);
  1513. if (lower_transport_mask == 0 && err == 1) {
  1514. err = AVERROR(EPROTONOSUPPORT);
  1515. goto fail;
  1516. }
  1517. } while (err);
  1518. rt->lower_transport_mask = lower_transport_mask;
  1519. av_strlcpy(rt->real_challenge, real_challenge, sizeof(rt->real_challenge));
  1520. rt->state = RTSP_STATE_IDLE;
  1521. rt->seek_timestamp = 0; /* default is to start stream at position zero */
  1522. return 0;
  1523. fail:
  1524. ff_rtsp_close_streams(s);
  1525. ff_rtsp_close_connections(s);
  1526. if (reply->status_code >=300 && reply->status_code < 400 && s->iformat) {
  1527. av_strlcpy(s->filename, reply->location, sizeof(s->filename));
  1528. av_log(s, AV_LOG_INFO, "Status %d: Redirecting to %s\n",
  1529. reply->status_code,
  1530. s->filename);
  1531. goto redirect;
  1532. }
  1533. ff_network_close();
  1534. return err;
  1535. }
  1536. #endif /* CONFIG_RTSP_DEMUXER || CONFIG_RTSP_MUXER */
  1537. #if CONFIG_RTPDEC
  1538. static int udp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  1539. uint8_t *buf, int buf_size, int64_t wait_end)
  1540. {
  1541. RTSPState *rt = s->priv_data;
  1542. RTSPStream *rtsp_st;
  1543. int n, i, ret, tcp_fd, timeout_cnt = 0;
  1544. int max_p = 0;
  1545. struct pollfd *p = rt->p;
  1546. int *fds = NULL, fdsnum, fdsidx;
  1547. for (;;) {
  1548. if (ff_check_interrupt(&s->interrupt_callback))
  1549. return AVERROR_EXIT;
  1550. if (wait_end && wait_end - av_gettime() < 0)
  1551. return AVERROR(EAGAIN);
  1552. max_p = 0;
  1553. if (rt->rtsp_hd) {
  1554. tcp_fd = ffurl_get_file_handle(rt->rtsp_hd);
  1555. p[max_p].fd = tcp_fd;
  1556. p[max_p++].events = POLLIN;
  1557. } else {
  1558. tcp_fd = -1;
  1559. }
  1560. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1561. rtsp_st = rt->rtsp_streams[i];
  1562. if (rtsp_st->rtp_handle) {
  1563. if (ret = ffurl_get_multi_file_handle(rtsp_st->rtp_handle,
  1564. &fds, &fdsnum)) {
  1565. av_log(s, AV_LOG_ERROR, "Unable to recover rtp ports\n");
  1566. return ret;
  1567. }
  1568. if (fdsnum != 2) {
  1569. av_log(s, AV_LOG_ERROR,
  1570. "Number of fds %d not supported\n", fdsnum);
  1571. return AVERROR_INVALIDDATA;
  1572. }
  1573. for (fdsidx = 0; fdsidx < fdsnum; fdsidx++) {
  1574. p[max_p].fd = fds[fdsidx];
  1575. p[max_p++].events = POLLIN;
  1576. }
  1577. av_free(fds);
  1578. }
  1579. }
  1580. n = poll(p, max_p, POLL_TIMEOUT_MS);
  1581. if (n > 0) {
  1582. int j = 1 - (tcp_fd == -1);
  1583. timeout_cnt = 0;
  1584. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1585. rtsp_st = rt->rtsp_streams[i];
  1586. if (rtsp_st->rtp_handle) {
  1587. if (p[j].revents & POLLIN || p[j+1].revents & POLLIN) {
  1588. ret = ffurl_read(rtsp_st->rtp_handle, buf, buf_size);
  1589. if (ret > 0) {
  1590. *prtsp_st = rtsp_st;
  1591. return ret;
  1592. }
  1593. }
  1594. j+=2;
  1595. }
  1596. }
  1597. #if CONFIG_RTSP_DEMUXER
  1598. if (tcp_fd != -1 && p[0].revents & POLLIN) {
  1599. if (rt->rtsp_flags & RTSP_FLAG_LISTEN) {
  1600. if (rt->state == RTSP_STATE_STREAMING) {
  1601. if (!ff_rtsp_parse_streaming_commands(s))
  1602. return AVERROR_EOF;
  1603. else
  1604. av_log(s, AV_LOG_WARNING,
  1605. "Unable to answer to TEARDOWN\n");
  1606. } else
  1607. return 0;
  1608. } else {
  1609. RTSPMessageHeader reply;
  1610. ret = ff_rtsp_read_reply(s, &reply, NULL, 0, NULL);
  1611. if (ret < 0)
  1612. return ret;
  1613. /* XXX: parse message */
  1614. if (rt->state != RTSP_STATE_STREAMING)
  1615. return 0;
  1616. }
  1617. }
  1618. #endif
  1619. } else if (n == 0 && ++timeout_cnt >= MAX_TIMEOUTS) {
  1620. return AVERROR(ETIMEDOUT);
  1621. } else if (n < 0 && errno != EINTR)
  1622. return AVERROR(errno);
  1623. }
  1624. }
  1625. int ff_rtsp_fetch_packet(AVFormatContext *s, AVPacket *pkt)
  1626. {
  1627. RTSPState *rt = s->priv_data;
  1628. int ret, len;
  1629. RTSPStream *rtsp_st, *first_queue_st = NULL;
  1630. int64_t wait_end = 0;
  1631. if (rt->nb_byes == rt->nb_rtsp_streams)
  1632. return AVERROR_EOF;
  1633. /* get next frames from the same RTP packet */
  1634. if (rt->cur_transport_priv) {
  1635. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1636. ret = ff_rdt_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1637. } else if (rt->transport == RTSP_TRANSPORT_RTP) {
  1638. ret = ff_rtp_parse_packet(rt->cur_transport_priv, pkt, NULL, 0);
  1639. } else if (rt->ts && CONFIG_RTPDEC) {
  1640. ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf + rt->recvbuf_pos, rt->recvbuf_len - rt->recvbuf_pos);
  1641. if (ret >= 0) {
  1642. rt->recvbuf_pos += ret;
  1643. ret = rt->recvbuf_pos < rt->recvbuf_len;
  1644. }
  1645. } else
  1646. av_assert0(0);
  1647. if (ret == 0) {
  1648. rt->cur_transport_priv = NULL;
  1649. return 0;
  1650. } else if (ret == 1) {
  1651. return 0;
  1652. } else
  1653. rt->cur_transport_priv = NULL;
  1654. }
  1655. if (rt->transport == RTSP_TRANSPORT_RTP) {
  1656. int i;
  1657. int64_t first_queue_time = 0;
  1658. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1659. RTPDemuxContext *rtpctx = rt->rtsp_streams[i]->transport_priv;
  1660. int64_t queue_time;
  1661. if (!rtpctx)
  1662. continue;
  1663. queue_time = ff_rtp_queued_packet_time(rtpctx);
  1664. if (queue_time && (queue_time - first_queue_time < 0 ||
  1665. !first_queue_time)) {
  1666. first_queue_time = queue_time;
  1667. first_queue_st = rt->rtsp_streams[i];
  1668. }
  1669. }
  1670. if (first_queue_time)
  1671. wait_end = first_queue_time + s->max_delay;
  1672. }
  1673. /* read next RTP packet */
  1674. redo:
  1675. if (!rt->recvbuf) {
  1676. rt->recvbuf = av_malloc(RECVBUF_SIZE);
  1677. if (!rt->recvbuf)
  1678. return AVERROR(ENOMEM);
  1679. }
  1680. switch(rt->lower_transport) {
  1681. default:
  1682. #if CONFIG_RTSP_DEMUXER
  1683. case RTSP_LOWER_TRANSPORT_TCP:
  1684. len = ff_rtsp_tcp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE);
  1685. break;
  1686. #endif
  1687. case RTSP_LOWER_TRANSPORT_UDP:
  1688. case RTSP_LOWER_TRANSPORT_UDP_MULTICAST:
  1689. len = udp_read_packet(s, &rtsp_st, rt->recvbuf, RECVBUF_SIZE, wait_end);
  1690. if (len > 0 && rtsp_st->transport_priv && rt->transport == RTSP_TRANSPORT_RTP)
  1691. ff_rtp_check_and_send_back_rr(rtsp_st->transport_priv, len);
  1692. break;
  1693. }
  1694. if (len == AVERROR(EAGAIN) && first_queue_st &&
  1695. rt->transport == RTSP_TRANSPORT_RTP) {
  1696. rtsp_st = first_queue_st;
  1697. ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, NULL, 0);
  1698. goto end;
  1699. }
  1700. if (len < 0)
  1701. return len;
  1702. if (len == 0)
  1703. return AVERROR_EOF;
  1704. if (rt->transport == RTSP_TRANSPORT_RDT) {
  1705. ret = ff_rdt_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1706. } else if (rt->transport == RTSP_TRANSPORT_RTP) {
  1707. ret = ff_rtp_parse_packet(rtsp_st->transport_priv, pkt, &rt->recvbuf, len);
  1708. if (ret < 0) {
  1709. /* Either bad packet, or a RTCP packet. Check if the
  1710. * first_rtcp_ntp_time field was initialized. */
  1711. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  1712. if (rtpctx->first_rtcp_ntp_time != AV_NOPTS_VALUE) {
  1713. /* first_rtcp_ntp_time has been initialized for this stream,
  1714. * copy the same value to all other uninitialized streams,
  1715. * in order to map their timestamp origin to the same ntp time
  1716. * as this one. */
  1717. int i;
  1718. AVStream *st = NULL;
  1719. if (rtsp_st->stream_index >= 0)
  1720. st = s->streams[rtsp_st->stream_index];
  1721. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1722. RTPDemuxContext *rtpctx2 = rt->rtsp_streams[i]->transport_priv;
  1723. AVStream *st2 = NULL;
  1724. if (rt->rtsp_streams[i]->stream_index >= 0)
  1725. st2 = s->streams[rt->rtsp_streams[i]->stream_index];
  1726. if (rtpctx2 && st && st2 &&
  1727. rtpctx2->first_rtcp_ntp_time == AV_NOPTS_VALUE) {
  1728. rtpctx2->first_rtcp_ntp_time = rtpctx->first_rtcp_ntp_time;
  1729. rtpctx2->rtcp_ts_offset = av_rescale_q(
  1730. rtpctx->rtcp_ts_offset, st->time_base,
  1731. st2->time_base);
  1732. }
  1733. }
  1734. }
  1735. if (ret == -RTCP_BYE) {
  1736. rt->nb_byes++;
  1737. av_log(s, AV_LOG_DEBUG, "Received BYE for stream %d (%d/%d)\n",
  1738. rtsp_st->stream_index, rt->nb_byes, rt->nb_rtsp_streams);
  1739. if (rt->nb_byes == rt->nb_rtsp_streams)
  1740. return AVERROR_EOF;
  1741. }
  1742. }
  1743. } else if (rt->ts && CONFIG_RTPDEC) {
  1744. ret = ff_mpegts_parse_packet(rt->ts, pkt, rt->recvbuf, len);
  1745. if (ret >= 0) {
  1746. if (ret < len) {
  1747. rt->recvbuf_len = len;
  1748. rt->recvbuf_pos = ret;
  1749. rt->cur_transport_priv = rt->ts;
  1750. return 1;
  1751. } else {
  1752. ret = 0;
  1753. }
  1754. }
  1755. } else {
  1756. return AVERROR_INVALIDDATA;
  1757. }
  1758. end:
  1759. if (ret < 0)
  1760. goto redo;
  1761. if (ret == 1)
  1762. /* more packets may follow, so we save the RTP context */
  1763. rt->cur_transport_priv = rtsp_st->transport_priv;
  1764. return ret;
  1765. }
  1766. #endif /* CONFIG_RTPDEC */
  1767. #if CONFIG_SDP_DEMUXER
  1768. static int sdp_probe(AVProbeData *p1)
  1769. {
  1770. const char *p = p1->buf, *p_end = p1->buf + p1->buf_size;
  1771. /* we look for a line beginning "c=IN IP" */
  1772. while (p < p_end && *p != '\0') {
  1773. if (p + sizeof("c=IN IP") - 1 < p_end &&
  1774. av_strstart(p, "c=IN IP", NULL))
  1775. return AVPROBE_SCORE_MAX / 2;
  1776. while (p < p_end - 1 && *p != '\n') p++;
  1777. if (++p >= p_end)
  1778. break;
  1779. if (*p == '\r')
  1780. p++;
  1781. }
  1782. return 0;
  1783. }
  1784. static int sdp_read_header(AVFormatContext *s)
  1785. {
  1786. RTSPState *rt = s->priv_data;
  1787. RTSPStream *rtsp_st;
  1788. int size, i, err;
  1789. char *content;
  1790. char url[1024];
  1791. if (!ff_network_init())
  1792. return AVERROR(EIO);
  1793. if (s->max_delay < 0) /* Not set by the caller */
  1794. s->max_delay = DEFAULT_REORDERING_DELAY;
  1795. /* read the whole sdp file */
  1796. /* XXX: better loading */
  1797. content = av_malloc(SDP_MAX_SIZE);
  1798. size = avio_read(s->pb, content, SDP_MAX_SIZE - 1);
  1799. if (size <= 0) {
  1800. av_free(content);
  1801. return AVERROR_INVALIDDATA;
  1802. }
  1803. content[size] ='\0';
  1804. err = ff_sdp_parse(s, content);
  1805. av_free(content);
  1806. if (err) goto fail;
  1807. /* open each RTP stream */
  1808. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  1809. char namebuf[50];
  1810. rtsp_st = rt->rtsp_streams[i];
  1811. getnameinfo((struct sockaddr*) &rtsp_st->sdp_ip, sizeof(rtsp_st->sdp_ip),
  1812. namebuf, sizeof(namebuf), NULL, 0, NI_NUMERICHOST);
  1813. ff_url_join(url, sizeof(url), "rtp", NULL,
  1814. namebuf, rtsp_st->sdp_port,
  1815. "?localport=%d&ttl=%d&connect=%d", rtsp_st->sdp_port,
  1816. rtsp_st->sdp_ttl,
  1817. rt->rtsp_flags & RTSP_FLAG_FILTER_SRC ? 1 : 0);
  1818. if (ffurl_open(&rtsp_st->rtp_handle, url, AVIO_FLAG_READ_WRITE,
  1819. &s->interrupt_callback, NULL) < 0) {
  1820. err = AVERROR_INVALIDDATA;
  1821. goto fail;
  1822. }
  1823. if ((err = ff_rtsp_open_transport_ctx(s, rtsp_st)))
  1824. goto fail;
  1825. }
  1826. return 0;
  1827. fail:
  1828. ff_rtsp_close_streams(s);
  1829. ff_network_close();
  1830. return err;
  1831. }
  1832. static int sdp_read_close(AVFormatContext *s)
  1833. {
  1834. ff_rtsp_close_streams(s);
  1835. ff_network_close();
  1836. return 0;
  1837. }
  1838. static const AVClass sdp_demuxer_class = {
  1839. .class_name = "SDP demuxer",
  1840. .item_name = av_default_item_name,
  1841. .option = sdp_options,
  1842. .version = LIBAVUTIL_VERSION_INT,
  1843. };
  1844. AVInputFormat ff_sdp_demuxer = {
  1845. .name = "sdp",
  1846. .long_name = NULL_IF_CONFIG_SMALL("SDP"),
  1847. .priv_data_size = sizeof(RTSPState),
  1848. .read_probe = sdp_probe,
  1849. .read_header = sdp_read_header,
  1850. .read_packet = ff_rtsp_fetch_packet,
  1851. .read_close = sdp_read_close,
  1852. .priv_class = &sdp_demuxer_class,
  1853. };
  1854. #endif /* CONFIG_SDP_DEMUXER */
  1855. #if CONFIG_RTP_DEMUXER
  1856. static int rtp_probe(AVProbeData *p)
  1857. {
  1858. if (av_strstart(p->filename, "rtp:", NULL))
  1859. return AVPROBE_SCORE_MAX;
  1860. return 0;
  1861. }
  1862. static int rtp_read_header(AVFormatContext *s)
  1863. {
  1864. uint8_t recvbuf[1500];
  1865. char host[500], sdp[500];
  1866. int ret, port;
  1867. URLContext* in = NULL;
  1868. int payload_type;
  1869. AVCodecContext codec = { 0 };
  1870. struct sockaddr_storage addr;
  1871. AVIOContext pb;
  1872. socklen_t addrlen = sizeof(addr);
  1873. RTSPState *rt = s->priv_data;
  1874. if (!ff_network_init())
  1875. return AVERROR(EIO);
  1876. ret = ffurl_open(&in, s->filename, AVIO_FLAG_READ,
  1877. &s->interrupt_callback, NULL);
  1878. if (ret)
  1879. goto fail;
  1880. while (1) {
  1881. ret = ffurl_read(in, recvbuf, sizeof(recvbuf));
  1882. if (ret == AVERROR(EAGAIN))
  1883. continue;
  1884. if (ret < 0)
  1885. goto fail;
  1886. if (ret < 12) {
  1887. av_log(s, AV_LOG_WARNING, "Received too short packet\n");
  1888. continue;
  1889. }
  1890. if ((recvbuf[0] & 0xc0) != 0x80) {
  1891. av_log(s, AV_LOG_WARNING, "Unsupported RTP version packet "
  1892. "received\n");
  1893. continue;
  1894. }
  1895. if (RTP_PT_IS_RTCP(recvbuf[1]))
  1896. continue;
  1897. payload_type = recvbuf[1] & 0x7f;
  1898. break;
  1899. }
  1900. getsockname(ffurl_get_file_handle(in), (struct sockaddr*) &addr, &addrlen);
  1901. ffurl_close(in);
  1902. in = NULL;
  1903. if (ff_rtp_get_codec_info(&codec, payload_type)) {
  1904. av_log(s, AV_LOG_ERROR, "Unable to receive RTP payload type %d "
  1905. "without an SDP file describing it\n",
  1906. payload_type);
  1907. goto fail;
  1908. }
  1909. if (codec.codec_type != AVMEDIA_TYPE_DATA) {
  1910. av_log(s, AV_LOG_WARNING, "Guessing on RTP content - if not received "
  1911. "properly you need an SDP file "
  1912. "describing it\n");
  1913. }
  1914. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port,
  1915. NULL, 0, s->filename);
  1916. snprintf(sdp, sizeof(sdp),
  1917. "v=0\r\nc=IN IP%d %s\r\nm=%s %d RTP/AVP %d\r\n",
  1918. addr.ss_family == AF_INET ? 4 : 6, host,
  1919. codec.codec_type == AVMEDIA_TYPE_DATA ? "application" :
  1920. codec.codec_type == AVMEDIA_TYPE_VIDEO ? "video" : "audio",
  1921. port, payload_type);
  1922. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
  1923. ffio_init_context(&pb, sdp, strlen(sdp), 0, NULL, NULL, NULL, NULL);
  1924. s->pb = &pb;
  1925. /* sdp_read_header initializes this again */
  1926. ff_network_close();
  1927. rt->media_type_mask = (1 << (AVMEDIA_TYPE_DATA+1)) - 1;
  1928. ret = sdp_read_header(s);
  1929. s->pb = NULL;
  1930. return ret;
  1931. fail:
  1932. if (in)
  1933. ffurl_close(in);
  1934. ff_network_close();
  1935. return ret;
  1936. }
  1937. static const AVClass rtp_demuxer_class = {
  1938. .class_name = "RTP demuxer",
  1939. .item_name = av_default_item_name,
  1940. .option = rtp_options,
  1941. .version = LIBAVUTIL_VERSION_INT,
  1942. };
  1943. AVInputFormat ff_rtp_demuxer = {
  1944. .name = "rtp",
  1945. .long_name = NULL_IF_CONFIG_SMALL("RTP input"),
  1946. .priv_data_size = sizeof(RTSPState),
  1947. .read_probe = rtp_probe,
  1948. .read_header = rtp_read_header,
  1949. .read_packet = ff_rtsp_fetch_packet,
  1950. .read_close = sdp_read_close,
  1951. .flags = AVFMT_NOFILE,
  1952. .priv_class = &rtp_demuxer_class,
  1953. };
  1954. #endif /* CONFIG_RTP_DEMUXER */