rtspdec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * RTSP demuxer
  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/avstring.h"
  22. #include "libavutil/intreadwrite.h"
  23. #include "libavutil/mathematics.h"
  24. #include "libavutil/opt.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "network.h"
  28. #include "os_support.h"
  29. #include "rtsp.h"
  30. #include "rdt.h"
  31. #include "url.h"
  32. static int rtsp_read_play(AVFormatContext *s)
  33. {
  34. RTSPState *rt = s->priv_data;
  35. RTSPMessageHeader reply1, *reply = &reply1;
  36. int i;
  37. char cmd[1024];
  38. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  39. rt->nb_byes = 0;
  40. if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  41. if (rt->transport == RTSP_TRANSPORT_RTP) {
  42. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  43. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  44. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  45. if (!rtpctx)
  46. continue;
  47. ff_rtp_reset_packet_queue(rtpctx);
  48. rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
  49. rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
  50. rtpctx->base_timestamp = 0;
  51. rtpctx->rtcp_ts_offset = 0;
  52. }
  53. }
  54. if (rt->state == RTSP_STATE_PAUSED) {
  55. cmd[0] = 0;
  56. } else {
  57. snprintf(cmd, sizeof(cmd),
  58. "Range: npt=%"PRId64".%03"PRId64"-\r\n",
  59. rt->seek_timestamp / AV_TIME_BASE,
  60. rt->seek_timestamp / (AV_TIME_BASE / 1000) % 1000);
  61. }
  62. ff_rtsp_send_cmd(s, "PLAY", rt->control_uri, cmd, reply, NULL);
  63. if (reply->status_code != RTSP_STATUS_OK) {
  64. return -1;
  65. }
  66. if (rt->transport == RTSP_TRANSPORT_RTP &&
  67. reply->range_start != AV_NOPTS_VALUE) {
  68. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  69. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  70. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  71. AVStream *st = NULL;
  72. if (!rtpctx || rtsp_st->stream_index < 0)
  73. continue;
  74. st = s->streams[rtsp_st->stream_index];
  75. rtpctx->range_start_offset =
  76. av_rescale_q(reply->range_start, AV_TIME_BASE_Q,
  77. st->time_base);
  78. }
  79. }
  80. }
  81. rt->state = RTSP_STATE_STREAMING;
  82. return 0;
  83. }
  84. /* pause the stream */
  85. static int rtsp_read_pause(AVFormatContext *s)
  86. {
  87. RTSPState *rt = s->priv_data;
  88. RTSPMessageHeader reply1, *reply = &reply1;
  89. if (rt->state != RTSP_STATE_STREAMING)
  90. return 0;
  91. else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  92. ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
  93. if (reply->status_code != RTSP_STATUS_OK) {
  94. return -1;
  95. }
  96. }
  97. rt->state = RTSP_STATE_PAUSED;
  98. return 0;
  99. }
  100. int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
  101. {
  102. RTSPState *rt = s->priv_data;
  103. char cmd[1024];
  104. unsigned char *content = NULL;
  105. int ret;
  106. /* describe the stream */
  107. snprintf(cmd, sizeof(cmd),
  108. "Accept: application/sdp\r\n");
  109. if (rt->server_type == RTSP_SERVER_REAL) {
  110. /**
  111. * The Require: attribute is needed for proper streaming from
  112. * Realmedia servers.
  113. */
  114. av_strlcat(cmd,
  115. "Require: com.real.retain-entity-for-setup\r\n",
  116. sizeof(cmd));
  117. }
  118. ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
  119. if (!content)
  120. return AVERROR_INVALIDDATA;
  121. if (reply->status_code != RTSP_STATUS_OK) {
  122. av_freep(&content);
  123. return AVERROR_INVALIDDATA;
  124. }
  125. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
  126. /* now we got the SDP description, we parse it */
  127. ret = ff_sdp_parse(s, (const char *)content);
  128. av_freep(&content);
  129. if (ret < 0)
  130. return ret;
  131. return 0;
  132. }
  133. static int rtsp_probe(AVProbeData *p)
  134. {
  135. if (av_strstart(p->filename, "rtsp:", NULL))
  136. return AVPROBE_SCORE_MAX;
  137. return 0;
  138. }
  139. static int rtsp_read_header(AVFormatContext *s,
  140. AVFormatParameters *ap)
  141. {
  142. RTSPState *rt = s->priv_data;
  143. int ret;
  144. ret = ff_rtsp_connect(s);
  145. if (ret)
  146. return ret;
  147. rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
  148. if (!rt->real_setup_cache)
  149. return AVERROR(ENOMEM);
  150. rt->real_setup = rt->real_setup_cache + s->nb_streams;
  151. #if FF_API_FORMAT_PARAMETERS
  152. if (ap->initial_pause)
  153. rt->initial_pause = ap->initial_pause;
  154. #endif
  155. if (rt->initial_pause) {
  156. /* do not start immediately */
  157. } else {
  158. if (rtsp_read_play(s) < 0) {
  159. ff_rtsp_close_streams(s);
  160. ff_rtsp_close_connections(s);
  161. return AVERROR_INVALIDDATA;
  162. }
  163. }
  164. return 0;
  165. }
  166. int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  167. uint8_t *buf, int buf_size)
  168. {
  169. RTSPState *rt = s->priv_data;
  170. int id, len, i, ret;
  171. RTSPStream *rtsp_st;
  172. av_dlog(s, "tcp_read_packet:\n");
  173. redo:
  174. for (;;) {
  175. RTSPMessageHeader reply;
  176. ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
  177. if (ret < 0)
  178. return ret;
  179. if (ret == 1) /* received '$' */
  180. break;
  181. /* XXX: parse message */
  182. if (rt->state != RTSP_STATE_STREAMING)
  183. return 0;
  184. }
  185. ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
  186. if (ret != 3)
  187. return -1;
  188. id = buf[0];
  189. len = AV_RB16(buf + 1);
  190. av_dlog(s, "id=%d len=%d\n", id, len);
  191. if (len > buf_size || len < 12)
  192. goto redo;
  193. /* get the data */
  194. ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
  195. if (ret != len)
  196. return -1;
  197. if (rt->transport == RTSP_TRANSPORT_RDT &&
  198. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  199. return -1;
  200. /* find the matching stream */
  201. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  202. rtsp_st = rt->rtsp_streams[i];
  203. if (id >= rtsp_st->interleaved_min &&
  204. id <= rtsp_st->interleaved_max)
  205. goto found;
  206. }
  207. goto redo;
  208. found:
  209. *prtsp_st = rtsp_st;
  210. return len;
  211. }
  212. static int resetup_tcp(AVFormatContext *s)
  213. {
  214. RTSPState *rt = s->priv_data;
  215. char host[1024];
  216. int port;
  217. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
  218. s->filename);
  219. ff_rtsp_undo_setup(s);
  220. return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
  221. rt->real_challenge);
  222. }
  223. static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
  224. {
  225. RTSPState *rt = s->priv_data;
  226. int ret;
  227. RTSPMessageHeader reply1, *reply = &reply1;
  228. char cmd[1024];
  229. retry:
  230. if (rt->server_type == RTSP_SERVER_REAL) {
  231. int i;
  232. for (i = 0; i < s->nb_streams; i++)
  233. rt->real_setup[i] = s->streams[i]->discard;
  234. if (!rt->need_subscription) {
  235. if (memcmp (rt->real_setup, rt->real_setup_cache,
  236. sizeof(enum AVDiscard) * s->nb_streams)) {
  237. snprintf(cmd, sizeof(cmd),
  238. "Unsubscribe: %s\r\n",
  239. rt->last_subscription);
  240. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  241. cmd, reply, NULL);
  242. if (reply->status_code != RTSP_STATUS_OK)
  243. return AVERROR_INVALIDDATA;
  244. rt->need_subscription = 1;
  245. }
  246. }
  247. if (rt->need_subscription) {
  248. int r, rule_nr, first = 1;
  249. memcpy(rt->real_setup_cache, rt->real_setup,
  250. sizeof(enum AVDiscard) * s->nb_streams);
  251. rt->last_subscription[0] = 0;
  252. snprintf(cmd, sizeof(cmd),
  253. "Subscribe: ");
  254. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  255. rule_nr = 0;
  256. for (r = 0; r < s->nb_streams; r++) {
  257. if (s->streams[r]->id == i) {
  258. if (s->streams[r]->discard != AVDISCARD_ALL) {
  259. if (!first)
  260. av_strlcat(rt->last_subscription, ",",
  261. sizeof(rt->last_subscription));
  262. ff_rdt_subscribe_rule(
  263. rt->last_subscription,
  264. sizeof(rt->last_subscription), i, rule_nr);
  265. first = 0;
  266. }
  267. rule_nr++;
  268. }
  269. }
  270. }
  271. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  272. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  273. cmd, reply, NULL);
  274. if (reply->status_code != RTSP_STATUS_OK)
  275. return AVERROR_INVALIDDATA;
  276. rt->need_subscription = 0;
  277. if (rt->state == RTSP_STATE_STREAMING)
  278. rtsp_read_play (s);
  279. }
  280. }
  281. ret = ff_rtsp_fetch_packet(s, pkt);
  282. if (ret < 0) {
  283. if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
  284. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  285. rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
  286. RTSPMessageHeader reply1, *reply = &reply1;
  287. av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
  288. if (rtsp_read_pause(s) != 0)
  289. return -1;
  290. // TEARDOWN is required on Real-RTSP, but might make
  291. // other servers close the connection.
  292. if (rt->server_type == RTSP_SERVER_REAL)
  293. ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
  294. reply, NULL);
  295. rt->session_id[0] = '\0';
  296. if (resetup_tcp(s) == 0) {
  297. rt->state = RTSP_STATE_IDLE;
  298. rt->need_subscription = 1;
  299. if (rtsp_read_play(s) != 0)
  300. return -1;
  301. goto retry;
  302. }
  303. }
  304. }
  305. return ret;
  306. }
  307. rt->packets++;
  308. /* send dummy request to keep TCP connection alive */
  309. if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
  310. if (rt->server_type == RTSP_SERVER_WMS ||
  311. (rt->server_type != RTSP_SERVER_REAL &&
  312. rt->get_parameter_supported)) {
  313. ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
  314. } else {
  315. ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
  316. }
  317. }
  318. return 0;
  319. }
  320. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  321. int64_t timestamp, int flags)
  322. {
  323. RTSPState *rt = s->priv_data;
  324. rt->seek_timestamp = av_rescale_q(timestamp,
  325. s->streams[stream_index]->time_base,
  326. AV_TIME_BASE_Q);
  327. switch(rt->state) {
  328. default:
  329. case RTSP_STATE_IDLE:
  330. break;
  331. case RTSP_STATE_STREAMING:
  332. if (rtsp_read_pause(s) != 0)
  333. return -1;
  334. rt->state = RTSP_STATE_SEEKING;
  335. if (rtsp_read_play(s) != 0)
  336. return -1;
  337. break;
  338. case RTSP_STATE_PAUSED:
  339. rt->state = RTSP_STATE_IDLE;
  340. break;
  341. }
  342. return 0;
  343. }
  344. static int rtsp_read_close(AVFormatContext *s)
  345. {
  346. RTSPState *rt = s->priv_data;
  347. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  348. ff_rtsp_close_streams(s);
  349. ff_rtsp_close_connections(s);
  350. ff_network_close();
  351. rt->real_setup = NULL;
  352. av_freep(&rt->real_setup_cache);
  353. return 0;
  354. }
  355. static const AVOption options[] = {
  356. { "initial_pause", "Don't start playing the stream immediately", offsetof(RTSPState, initial_pause), FF_OPT_TYPE_INT, {.dbl = 0}, 0, 1, AV_OPT_FLAG_DECODING_PARAM },
  357. { NULL },
  358. };
  359. const AVClass rtsp_demuxer_class = {
  360. .class_name = "RTSP demuxer",
  361. .item_name = av_default_item_name,
  362. .option = options,
  363. .version = LIBAVUTIL_VERSION_INT,
  364. };
  365. AVInputFormat ff_rtsp_demuxer = {
  366. .name = "rtsp",
  367. .long_name = NULL_IF_CONFIG_SMALL("RTSP input format"),
  368. .priv_data_size = sizeof(RTSPState),
  369. .read_probe = rtsp_probe,
  370. .read_header = rtsp_read_header,
  371. .read_packet = rtsp_read_packet,
  372. .read_close = rtsp_read_close,
  373. .read_seek = rtsp_read_seek,
  374. .flags = AVFMT_NOFILE,
  375. .read_play = rtsp_read_play,
  376. .read_pause = rtsp_read_pause,
  377. .priv_class = &rtsp_demuxer_class,
  378. };