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.

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