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.

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