You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
13KB

  1. /*
  2. * RTSP demuxer
  3. * Copyright (c) 2002 Fabrice Bellard
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. //#define DEBUG
  32. //#define DEBUG_RTP_TCP
  33. static int rtsp_read_play(AVFormatContext *s)
  34. {
  35. RTSPState *rt = s->priv_data;
  36. RTSPMessageHeader reply1, *reply = &reply1;
  37. int i;
  38. char cmd[1024];
  39. av_log(s, AV_LOG_DEBUG, "hello state=%d\n", rt->state);
  40. rt->nb_byes = 0;
  41. if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  42. if (rt->transport == RTSP_TRANSPORT_RTP) {
  43. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  44. RTSPStream *rtsp_st = rt->rtsp_streams[i];
  45. RTPDemuxContext *rtpctx = rtsp_st->transport_priv;
  46. if (!rtpctx)
  47. continue;
  48. ff_rtp_reset_packet_queue(rtpctx);
  49. rtpctx->last_rtcp_ntp_time = AV_NOPTS_VALUE;
  50. rtpctx->first_rtcp_ntp_time = AV_NOPTS_VALUE;
  51. rtpctx->base_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 FF_API_FORMAT_PARAMETERS
  153. if (ap->initial_pause)
  154. rt->initial_pause = ap->initial_pause;
  155. #endif
  156. if (rt->initial_pause) {
  157. /* do not start immediately */
  158. } else {
  159. if (rtsp_read_play(s) < 0) {
  160. ff_rtsp_close_streams(s);
  161. ff_rtsp_close_connections(s);
  162. return AVERROR_INVALIDDATA;
  163. }
  164. }
  165. return 0;
  166. }
  167. int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  168. uint8_t *buf, int buf_size)
  169. {
  170. RTSPState *rt = s->priv_data;
  171. int id, len, i, ret;
  172. RTSPStream *rtsp_st;
  173. #ifdef DEBUG_RTP_TCP
  174. av_dlog(s, "tcp_read_packet:\n");
  175. #endif
  176. redo:
  177. for (;;) {
  178. RTSPMessageHeader reply;
  179. ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
  180. if (ret < 0)
  181. return ret;
  182. if (ret == 1) /* received '$' */
  183. break;
  184. /* XXX: parse message */
  185. if (rt->state != RTSP_STATE_STREAMING)
  186. return 0;
  187. }
  188. ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
  189. if (ret != 3)
  190. return -1;
  191. id = buf[0];
  192. len = AV_RB16(buf + 1);
  193. #ifdef DEBUG_RTP_TCP
  194. av_dlog(s, "id=%d len=%d\n", id, len);
  195. #endif
  196. if (len > buf_size || len < 12)
  197. goto redo;
  198. /* get the data */
  199. ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
  200. if (ret != len)
  201. return -1;
  202. if (rt->transport == RTSP_TRANSPORT_RDT &&
  203. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  204. return -1;
  205. /* find the matching stream */
  206. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  207. rtsp_st = rt->rtsp_streams[i];
  208. if (id >= rtsp_st->interleaved_min &&
  209. id <= rtsp_st->interleaved_max)
  210. goto found;
  211. }
  212. goto redo;
  213. found:
  214. *prtsp_st = rtsp_st;
  215. return len;
  216. }
  217. static int resetup_tcp(AVFormatContext *s)
  218. {
  219. RTSPState *rt = s->priv_data;
  220. char host[1024];
  221. int port;
  222. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
  223. s->filename);
  224. ff_rtsp_undo_setup(s);
  225. return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
  226. rt->real_challenge);
  227. }
  228. static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
  229. {
  230. RTSPState *rt = s->priv_data;
  231. int ret;
  232. RTSPMessageHeader reply1, *reply = &reply1;
  233. char cmd[1024];
  234. retry:
  235. if (rt->server_type == RTSP_SERVER_REAL) {
  236. int i;
  237. for (i = 0; i < s->nb_streams; i++)
  238. rt->real_setup[i] = s->streams[i]->discard;
  239. if (!rt->need_subscription) {
  240. if (memcmp (rt->real_setup, rt->real_setup_cache,
  241. sizeof(enum AVDiscard) * s->nb_streams)) {
  242. snprintf(cmd, sizeof(cmd),
  243. "Unsubscribe: %s\r\n",
  244. rt->last_subscription);
  245. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  246. cmd, reply, NULL);
  247. if (reply->status_code != RTSP_STATUS_OK)
  248. return AVERROR_INVALIDDATA;
  249. rt->need_subscription = 1;
  250. }
  251. }
  252. if (rt->need_subscription) {
  253. int r, rule_nr, first = 1;
  254. memcpy(rt->real_setup_cache, rt->real_setup,
  255. sizeof(enum AVDiscard) * s->nb_streams);
  256. rt->last_subscription[0] = 0;
  257. snprintf(cmd, sizeof(cmd),
  258. "Subscribe: ");
  259. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  260. rule_nr = 0;
  261. for (r = 0; r < s->nb_streams; r++) {
  262. if (s->streams[r]->id == i) {
  263. if (s->streams[r]->discard != AVDISCARD_ALL) {
  264. if (!first)
  265. av_strlcat(rt->last_subscription, ",",
  266. sizeof(rt->last_subscription));
  267. ff_rdt_subscribe_rule(
  268. rt->last_subscription,
  269. sizeof(rt->last_subscription), i, rule_nr);
  270. first = 0;
  271. }
  272. rule_nr++;
  273. }
  274. }
  275. }
  276. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  277. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  278. cmd, reply, NULL);
  279. if (reply->status_code != RTSP_STATUS_OK)
  280. return AVERROR_INVALIDDATA;
  281. rt->need_subscription = 0;
  282. if (rt->state == RTSP_STATE_STREAMING)
  283. rtsp_read_play (s);
  284. }
  285. }
  286. ret = ff_rtsp_fetch_packet(s, pkt);
  287. if (ret < 0) {
  288. if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
  289. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  290. rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
  291. RTSPMessageHeader reply1, *reply = &reply1;
  292. av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
  293. if (rtsp_read_pause(s) != 0)
  294. return -1;
  295. // TEARDOWN is required on Real-RTSP, but might make
  296. // other servers close the connection.
  297. if (rt->server_type == RTSP_SERVER_REAL)
  298. ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
  299. reply, NULL);
  300. rt->session_id[0] = '\0';
  301. if (resetup_tcp(s) == 0) {
  302. rt->state = RTSP_STATE_IDLE;
  303. rt->need_subscription = 1;
  304. if (rtsp_read_play(s) != 0)
  305. return -1;
  306. goto retry;
  307. }
  308. }
  309. }
  310. return ret;
  311. }
  312. rt->packets++;
  313. /* send dummy request to keep TCP connection alive */
  314. if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
  315. if (rt->server_type == RTSP_SERVER_WMS ||
  316. (rt->server_type != RTSP_SERVER_REAL &&
  317. rt->get_parameter_supported)) {
  318. ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
  319. } else {
  320. ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
  321. }
  322. }
  323. return 0;
  324. }
  325. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  326. int64_t timestamp, int flags)
  327. {
  328. RTSPState *rt = s->priv_data;
  329. rt->seek_timestamp = av_rescale_q(timestamp,
  330. s->streams[stream_index]->time_base,
  331. AV_TIME_BASE_Q);
  332. switch(rt->state) {
  333. default:
  334. case RTSP_STATE_IDLE:
  335. break;
  336. case RTSP_STATE_STREAMING:
  337. if (rtsp_read_pause(s) != 0)
  338. return -1;
  339. rt->state = RTSP_STATE_SEEKING;
  340. if (rtsp_read_play(s) != 0)
  341. return -1;
  342. break;
  343. case RTSP_STATE_PAUSED:
  344. rt->state = RTSP_STATE_IDLE;
  345. break;
  346. }
  347. return 0;
  348. }
  349. static int rtsp_read_close(AVFormatContext *s)
  350. {
  351. RTSPState *rt = s->priv_data;
  352. #if 0
  353. /* NOTE: it is valid to flush the buffer here */
  354. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  355. avio_close(&rt->rtsp_gb);
  356. }
  357. #endif
  358. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  359. ff_rtsp_close_streams(s);
  360. ff_rtsp_close_connections(s);
  361. ff_network_close();
  362. rt->real_setup = NULL;
  363. av_freep(&rt->real_setup_cache);
  364. return 0;
  365. }
  366. static const AVOption options[] = {
  367. { "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 },
  368. { NULL },
  369. };
  370. const AVClass rtsp_demuxer_class = {
  371. .class_name = "RTSP demuxer",
  372. .item_name = av_default_item_name,
  373. .option = options,
  374. .version = LIBAVUTIL_VERSION_INT,
  375. };
  376. AVInputFormat ff_rtsp_demuxer = {
  377. "rtsp",
  378. NULL_IF_CONFIG_SMALL("RTSP input format"),
  379. sizeof(RTSPState),
  380. rtsp_probe,
  381. rtsp_read_header,
  382. rtsp_read_packet,
  383. rtsp_read_close,
  384. rtsp_read_seek,
  385. .flags = AVFMT_NOFILE,
  386. .read_play = rtsp_read_play,
  387. .read_pause = rtsp_read_pause,
  388. .priv_class = &rtsp_demuxer_class,
  389. };