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.

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