rtspdec.c 13 KB

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