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.

417 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 "libavutil/opt.h"
  25. #include "avformat.h"
  26. #include "internal.h"
  27. #include "network.h"
  28. #include "os_support.h"
  29. #include "rtsp.h"
  30. #include "rdt.h"
  31. #include "url.h"
  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 (rt->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. av_dlog(s, "tcp_read_packet:\n");
  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 = ffurl_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. av_dlog(s, "id=%d len=%d\n", id, len);
  187. if (len > buf_size || len < 8)
  188. goto redo;
  189. /* get the data */
  190. ret = ffurl_read_complete(rt->rtsp_hd, buf, len);
  191. if (ret != len)
  192. return -1;
  193. if (rt->transport == RTSP_TRANSPORT_RDT &&
  194. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  195. return -1;
  196. /* find the matching stream */
  197. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  198. rtsp_st = rt->rtsp_streams[i];
  199. if (id >= rtsp_st->interleaved_min &&
  200. id <= rtsp_st->interleaved_max)
  201. goto found;
  202. }
  203. goto redo;
  204. found:
  205. *prtsp_st = rtsp_st;
  206. return len;
  207. }
  208. static int resetup_tcp(AVFormatContext *s)
  209. {
  210. RTSPState *rt = s->priv_data;
  211. char host[1024];
  212. int port;
  213. av_url_split(NULL, 0, NULL, 0, host, sizeof(host), &port, NULL, 0,
  214. s->filename);
  215. ff_rtsp_undo_setup(s);
  216. return ff_rtsp_make_setup_request(s, host, port, RTSP_LOWER_TRANSPORT_TCP,
  217. rt->real_challenge);
  218. }
  219. static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
  220. {
  221. RTSPState *rt = s->priv_data;
  222. int ret;
  223. RTSPMessageHeader reply1, *reply = &reply1;
  224. char cmd[1024];
  225. retry:
  226. if (rt->server_type == RTSP_SERVER_REAL) {
  227. int i;
  228. for (i = 0; i < s->nb_streams; i++)
  229. rt->real_setup[i] = s->streams[i]->discard;
  230. if (!rt->need_subscription) {
  231. if (memcmp (rt->real_setup, rt->real_setup_cache,
  232. sizeof(enum AVDiscard) * s->nb_streams)) {
  233. snprintf(cmd, sizeof(cmd),
  234. "Unsubscribe: %s\r\n",
  235. rt->last_subscription);
  236. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  237. cmd, reply, NULL);
  238. if (reply->status_code != RTSP_STATUS_OK)
  239. return AVERROR_INVALIDDATA;
  240. rt->need_subscription = 1;
  241. }
  242. }
  243. if (rt->need_subscription) {
  244. int r, rule_nr, first = 1;
  245. memcpy(rt->real_setup_cache, rt->real_setup,
  246. sizeof(enum AVDiscard) * s->nb_streams);
  247. rt->last_subscription[0] = 0;
  248. snprintf(cmd, sizeof(cmd),
  249. "Subscribe: ");
  250. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  251. rule_nr = 0;
  252. for (r = 0; r < s->nb_streams; r++) {
  253. if (s->streams[r]->id == i) {
  254. if (s->streams[r]->discard != AVDISCARD_ALL) {
  255. if (!first)
  256. av_strlcat(rt->last_subscription, ",",
  257. sizeof(rt->last_subscription));
  258. ff_rdt_subscribe_rule(
  259. rt->last_subscription,
  260. sizeof(rt->last_subscription), i, rule_nr);
  261. first = 0;
  262. }
  263. rule_nr++;
  264. }
  265. }
  266. }
  267. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  268. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  269. cmd, reply, NULL);
  270. if (reply->status_code != RTSP_STATUS_OK)
  271. return AVERROR_INVALIDDATA;
  272. rt->need_subscription = 0;
  273. if (rt->state == RTSP_STATE_STREAMING)
  274. rtsp_read_play (s);
  275. }
  276. }
  277. ret = ff_rtsp_fetch_packet(s, pkt);
  278. if (ret < 0) {
  279. if (ret == AVERROR(ETIMEDOUT) && !rt->packets) {
  280. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_UDP &&
  281. rt->lower_transport_mask & (1 << RTSP_LOWER_TRANSPORT_TCP)) {
  282. RTSPMessageHeader reply1, *reply = &reply1;
  283. av_log(s, AV_LOG_WARNING, "UDP timeout, retrying with TCP\n");
  284. if (rtsp_read_pause(s) != 0)
  285. return -1;
  286. // TEARDOWN is required on Real-RTSP, but might make
  287. // other servers close the connection.
  288. if (rt->server_type == RTSP_SERVER_REAL)
  289. ff_rtsp_send_cmd(s, "TEARDOWN", rt->control_uri, NULL,
  290. reply, NULL);
  291. rt->session_id[0] = '\0';
  292. if (resetup_tcp(s) == 0) {
  293. rt->state = RTSP_STATE_IDLE;
  294. rt->need_subscription = 1;
  295. if (rtsp_read_play(s) != 0)
  296. return -1;
  297. goto retry;
  298. }
  299. }
  300. }
  301. return ret;
  302. }
  303. rt->packets++;
  304. /* send dummy request to keep TCP connection alive */
  305. if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
  306. if (rt->server_type == RTSP_SERVER_WMS ||
  307. (rt->server_type != RTSP_SERVER_REAL &&
  308. rt->get_parameter_supported)) {
  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. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  344. ff_rtsp_close_streams(s);
  345. ff_rtsp_close_connections(s);
  346. ff_network_close();
  347. rt->real_setup = NULL;
  348. av_freep(&rt->real_setup_cache);
  349. return 0;
  350. }
  351. static const AVOption options[] = {
  352. { "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 },
  353. { NULL },
  354. };
  355. const AVClass rtsp_demuxer_class = {
  356. .class_name = "RTSP demuxer",
  357. .item_name = av_default_item_name,
  358. .option = options,
  359. .version = LIBAVUTIL_VERSION_INT,
  360. };
  361. AVInputFormat ff_rtsp_demuxer = {
  362. .name = "rtsp",
  363. .long_name = NULL_IF_CONFIG_SMALL("RTSP input format"),
  364. .priv_data_size = sizeof(RTSPState),
  365. .read_probe = rtsp_probe,
  366. .read_header = rtsp_read_header,
  367. .read_packet = rtsp_read_packet,
  368. .read_close = rtsp_read_close,
  369. .read_seek = rtsp_read_seek,
  370. .flags = AVFMT_NOFILE,
  371. .read_play = rtsp_read_play,
  372. .read_pause = rtsp_read_pause,
  373. .priv_class = &rtsp_demuxer_class,
  374. };