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