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.

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