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.

411 lines
13KB

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