rtspdec.c 13 KB

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