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.

415 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 "avformat.h"
  24. #include "internal.h"
  25. #include "network.h"
  26. #include "os_support.h"
  27. #include "rtsp.h"
  28. #include "rdt.h"
  29. #include "url.h"
  30. //#define DEBUG
  31. //#define DEBUG_RTP_TCP
  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 (ap->initial_pause) {
  152. /* do not start immediately */
  153. } else {
  154. if (rtsp_read_play(s) < 0) {
  155. ff_rtsp_close_streams(s);
  156. ff_rtsp_close_connections(s);
  157. return AVERROR_INVALIDDATA;
  158. }
  159. }
  160. return 0;
  161. }
  162. int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  163. uint8_t *buf, int buf_size)
  164. {
  165. RTSPState *rt = s->priv_data;
  166. int id, len, i, ret;
  167. RTSPStream *rtsp_st;
  168. #ifdef DEBUG_RTP_TCP
  169. av_dlog(s, "tcp_read_packet:\n");
  170. #endif
  171. redo:
  172. for (;;) {
  173. RTSPMessageHeader reply;
  174. ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
  175. if (ret < 0)
  176. return ret;
  177. if (ret == 1) /* received '$' */
  178. break;
  179. /* XXX: parse message */
  180. if (rt->state != RTSP_STATE_STREAMING)
  181. return 0;
  182. }
  183. ret = ffurl_read_complete(rt->rtsp_hd, buf, 3);
  184. if (ret != 3)
  185. return -1;
  186. id = buf[0];
  187. len = AV_RB16(buf + 1);
  188. #ifdef DEBUG_RTP_TCP
  189. av_dlog(s, "id=%d len=%d\n", id, len);
  190. #endif
  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. #if 0
  348. /* NOTE: it is valid to flush the buffer here */
  349. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  350. avio_close(&rt->rtsp_gb);
  351. }
  352. #endif
  353. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  354. ff_rtsp_close_streams(s);
  355. ff_rtsp_close_connections(s);
  356. ff_network_close();
  357. rt->real_setup = NULL;
  358. av_freep(&rt->real_setup_cache);
  359. return 0;
  360. }
  361. AVInputFormat ff_rtsp_demuxer = {
  362. "rtsp",
  363. NULL_IF_CONFIG_SMALL("RTSP input format"),
  364. sizeof(RTSPState),
  365. rtsp_probe,
  366. rtsp_read_header,
  367. rtsp_read_packet,
  368. rtsp_read_close,
  369. rtsp_read_seek,
  370. .flags = AVFMT_NOFILE,
  371. .read_play = rtsp_read_play,
  372. .read_pause = rtsp_read_pause,
  373. };