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.

372 lines
11KB

  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. int ff_rtsp_setup_input_streams(AVFormatContext *s, RTSPMessageHeader *reply)
  83. {
  84. RTSPState *rt = s->priv_data;
  85. char cmd[1024];
  86. unsigned char *content = NULL;
  87. int ret;
  88. /* describe the stream */
  89. snprintf(cmd, sizeof(cmd),
  90. "Accept: application/sdp\r\n");
  91. if (rt->server_type == RTSP_SERVER_REAL) {
  92. /**
  93. * The Require: attribute is needed for proper streaming from
  94. * Realmedia servers.
  95. */
  96. av_strlcat(cmd,
  97. "Require: com.real.retain-entity-for-setup\r\n",
  98. sizeof(cmd));
  99. }
  100. ff_rtsp_send_cmd(s, "DESCRIBE", rt->control_uri, cmd, reply, &content);
  101. if (!content)
  102. return AVERROR_INVALIDDATA;
  103. if (reply->status_code != RTSP_STATUS_OK) {
  104. av_freep(&content);
  105. return AVERROR_INVALIDDATA;
  106. }
  107. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", content);
  108. /* now we got the SDP description, we parse it */
  109. ret = ff_sdp_parse(s, (const char *)content);
  110. av_freep(&content);
  111. if (ret < 0)
  112. return AVERROR_INVALIDDATA;
  113. return 0;
  114. }
  115. static int rtsp_probe(AVProbeData *p)
  116. {
  117. if (av_strstart(p->filename, "rtsp:", NULL))
  118. return AVPROBE_SCORE_MAX;
  119. return 0;
  120. }
  121. static int rtsp_read_header(AVFormatContext *s,
  122. AVFormatParameters *ap)
  123. {
  124. RTSPState *rt = s->priv_data;
  125. int ret;
  126. ret = ff_rtsp_connect(s);
  127. if (ret)
  128. return ret;
  129. rt->real_setup_cache = av_mallocz(2 * s->nb_streams * sizeof(*rt->real_setup_cache));
  130. if (!rt->real_setup_cache)
  131. return AVERROR(ENOMEM);
  132. rt->real_setup = rt->real_setup_cache + s->nb_streams;
  133. if (ap->initial_pause) {
  134. /* do not start immediately */
  135. } else {
  136. if (rtsp_read_play(s) < 0) {
  137. ff_rtsp_close_streams(s);
  138. ff_rtsp_close_connections(s);
  139. return AVERROR_INVALIDDATA;
  140. }
  141. }
  142. return 0;
  143. }
  144. int ff_rtsp_tcp_read_packet(AVFormatContext *s, RTSPStream **prtsp_st,
  145. uint8_t *buf, int buf_size)
  146. {
  147. RTSPState *rt = s->priv_data;
  148. int id, len, i, ret;
  149. RTSPStream *rtsp_st;
  150. #ifdef DEBUG_RTP_TCP
  151. dprintf(s, "tcp_read_packet:\n");
  152. #endif
  153. redo:
  154. for (;;) {
  155. RTSPMessageHeader reply;
  156. ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
  157. if (ret < 0)
  158. return ret;
  159. if (ret == 1) /* received '$' */
  160. break;
  161. /* XXX: parse message */
  162. if (rt->state != RTSP_STATE_STREAMING)
  163. return 0;
  164. }
  165. ret = url_read_complete(rt->rtsp_hd, buf, 3);
  166. if (ret != 3)
  167. return -1;
  168. id = buf[0];
  169. len = AV_RB16(buf + 1);
  170. #ifdef DEBUG_RTP_TCP
  171. dprintf(s, "id=%d len=%d\n", id, len);
  172. #endif
  173. if (len > buf_size || len < 12)
  174. goto redo;
  175. /* get the data */
  176. ret = url_read_complete(rt->rtsp_hd, buf, len);
  177. if (ret != len)
  178. return -1;
  179. if (rt->transport == RTSP_TRANSPORT_RDT &&
  180. ff_rdt_parse_header(buf, len, &id, NULL, NULL, NULL, NULL) < 0)
  181. return -1;
  182. /* find the matching stream */
  183. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  184. rtsp_st = rt->rtsp_streams[i];
  185. if (id >= rtsp_st->interleaved_min &&
  186. id <= rtsp_st->interleaved_max)
  187. goto found;
  188. }
  189. goto redo;
  190. found:
  191. *prtsp_st = rtsp_st;
  192. return len;
  193. }
  194. static int rtsp_read_packet(AVFormatContext *s, AVPacket *pkt)
  195. {
  196. RTSPState *rt = s->priv_data;
  197. int ret;
  198. RTSPMessageHeader reply1, *reply = &reply1;
  199. char cmd[1024];
  200. if (rt->server_type == RTSP_SERVER_REAL) {
  201. int i;
  202. for (i = 0; i < s->nb_streams; i++)
  203. rt->real_setup[i] = s->streams[i]->discard;
  204. if (!rt->need_subscription) {
  205. if (memcmp (rt->real_setup, rt->real_setup_cache,
  206. sizeof(enum AVDiscard) * s->nb_streams)) {
  207. snprintf(cmd, sizeof(cmd),
  208. "Unsubscribe: %s\r\n",
  209. rt->last_subscription);
  210. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  211. cmd, reply, NULL);
  212. if (reply->status_code != RTSP_STATUS_OK)
  213. return AVERROR_INVALIDDATA;
  214. rt->need_subscription = 1;
  215. }
  216. }
  217. if (rt->need_subscription) {
  218. int r, rule_nr, first = 1;
  219. memcpy(rt->real_setup_cache, rt->real_setup,
  220. sizeof(enum AVDiscard) * s->nb_streams);
  221. rt->last_subscription[0] = 0;
  222. snprintf(cmd, sizeof(cmd),
  223. "Subscribe: ");
  224. for (i = 0; i < rt->nb_rtsp_streams; i++) {
  225. rule_nr = 0;
  226. for (r = 0; r < s->nb_streams; r++) {
  227. if (s->streams[r]->priv_data == rt->rtsp_streams[i]) {
  228. if (s->streams[r]->discard != AVDISCARD_ALL) {
  229. if (!first)
  230. av_strlcat(rt->last_subscription, ",",
  231. sizeof(rt->last_subscription));
  232. ff_rdt_subscribe_rule(
  233. rt->last_subscription,
  234. sizeof(rt->last_subscription), i, rule_nr);
  235. first = 0;
  236. }
  237. rule_nr++;
  238. }
  239. }
  240. }
  241. av_strlcatf(cmd, sizeof(cmd), "%s\r\n", rt->last_subscription);
  242. ff_rtsp_send_cmd(s, "SET_PARAMETER", rt->control_uri,
  243. cmd, reply, NULL);
  244. if (reply->status_code != RTSP_STATUS_OK)
  245. return AVERROR_INVALIDDATA;
  246. rt->need_subscription = 0;
  247. if (rt->state == RTSP_STATE_STREAMING)
  248. rtsp_read_play (s);
  249. }
  250. }
  251. ret = ff_rtsp_fetch_packet(s, pkt);
  252. if (ret < 0)
  253. return ret;
  254. /* send dummy request to keep TCP connection alive */
  255. if ((av_gettime() - rt->last_cmd_time) / 1000000 >= rt->timeout / 2) {
  256. if (rt->server_type == RTSP_SERVER_WMS) {
  257. ff_rtsp_send_cmd_async(s, "GET_PARAMETER", rt->control_uri, NULL);
  258. } else {
  259. ff_rtsp_send_cmd_async(s, "OPTIONS", "*", NULL);
  260. }
  261. }
  262. return 0;
  263. }
  264. /* pause the stream */
  265. static int rtsp_read_pause(AVFormatContext *s)
  266. {
  267. RTSPState *rt = s->priv_data;
  268. RTSPMessageHeader reply1, *reply = &reply1;
  269. if (rt->state != RTSP_STATE_STREAMING)
  270. return 0;
  271. else if (!(rt->server_type == RTSP_SERVER_REAL && rt->need_subscription)) {
  272. ff_rtsp_send_cmd(s, "PAUSE", rt->control_uri, NULL, reply, NULL);
  273. if (reply->status_code != RTSP_STATUS_OK) {
  274. return -1;
  275. }
  276. }
  277. rt->state = RTSP_STATE_PAUSED;
  278. return 0;
  279. }
  280. static int rtsp_read_seek(AVFormatContext *s, int stream_index,
  281. int64_t timestamp, int flags)
  282. {
  283. RTSPState *rt = s->priv_data;
  284. rt->seek_timestamp = av_rescale_q(timestamp,
  285. s->streams[stream_index]->time_base,
  286. AV_TIME_BASE_Q);
  287. switch(rt->state) {
  288. default:
  289. case RTSP_STATE_IDLE:
  290. break;
  291. case RTSP_STATE_STREAMING:
  292. if (rtsp_read_pause(s) != 0)
  293. return -1;
  294. rt->state = RTSP_STATE_SEEKING;
  295. if (rtsp_read_play(s) != 0)
  296. return -1;
  297. break;
  298. case RTSP_STATE_PAUSED:
  299. rt->state = RTSP_STATE_IDLE;
  300. break;
  301. }
  302. return 0;
  303. }
  304. static int rtsp_read_close(AVFormatContext *s)
  305. {
  306. RTSPState *rt = s->priv_data;
  307. #if 0
  308. /* NOTE: it is valid to flush the buffer here */
  309. if (rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP) {
  310. url_fclose(&rt->rtsp_gb);
  311. }
  312. #endif
  313. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  314. ff_rtsp_close_streams(s);
  315. ff_rtsp_close_connections(s);
  316. ff_network_close();
  317. rt->real_setup = NULL;
  318. av_freep(&rt->real_setup_cache);
  319. return 0;
  320. }
  321. AVInputFormat rtsp_demuxer = {
  322. "rtsp",
  323. NULL_IF_CONFIG_SMALL("RTSP input format"),
  324. sizeof(RTSPState),
  325. rtsp_probe,
  326. rtsp_read_header,
  327. rtsp_read_packet,
  328. rtsp_read_close,
  329. rtsp_read_seek,
  330. .flags = AVFMT_NOFILE,
  331. .read_play = rtsp_read_play,
  332. .read_pause = rtsp_read_pause,
  333. };