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.

250 lines
7.8KB

  1. /*
  2. * RTSP muxer
  3. * Copyright (c) 2010 Martin Storsjo
  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 "avformat.h"
  22. #include <sys/time.h>
  23. #if HAVE_SYS_SELECT_H
  24. #include <sys/select.h>
  25. #endif
  26. #include "network.h"
  27. #include "rtsp.h"
  28. #include "internal.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/avstring.h"
  31. #define SDP_MAX_SIZE 16384
  32. int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
  33. {
  34. RTSPState *rt = s->priv_data;
  35. RTSPMessageHeader reply1, *reply = &reply1;
  36. int i;
  37. char *sdp;
  38. AVFormatContext sdp_ctx, *ctx_array[1];
  39. s->start_time_realtime = av_gettime();
  40. /* Announce the stream */
  41. sdp = av_mallocz(SDP_MAX_SIZE);
  42. if (sdp == NULL)
  43. return AVERROR(ENOMEM);
  44. /* We create the SDP based on the RTSP AVFormatContext where we
  45. * aren't allowed to change the filename field. (We create the SDP
  46. * based on the RTSP context since the contexts for the RTP streams
  47. * don't exist yet.) In order to specify a custom URL with the actual
  48. * peer IP instead of the originally specified hostname, we create
  49. * a temporary copy of the AVFormatContext, where the custom URL is set.
  50. *
  51. * FIXME: Create the SDP without copying the AVFormatContext.
  52. * This either requires setting up the RTP stream AVFormatContexts
  53. * already here (complicating things immensely) or getting a more
  54. * flexible SDP creation interface.
  55. */
  56. sdp_ctx = *s;
  57. ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
  58. "rtsp", NULL, addr, -1, NULL);
  59. ctx_array[0] = &sdp_ctx;
  60. if (avf_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
  61. av_free(sdp);
  62. return AVERROR_INVALIDDATA;
  63. }
  64. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
  65. ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
  66. "Content-Type: application/sdp\r\n",
  67. reply, NULL, sdp, strlen(sdp));
  68. av_free(sdp);
  69. if (reply->status_code != RTSP_STATUS_OK)
  70. return AVERROR_INVALIDDATA;
  71. /* Set up the RTSPStreams for each AVStream */
  72. for (i = 0; i < s->nb_streams; i++) {
  73. RTSPStream *rtsp_st;
  74. AVStream *st = s->streams[i];
  75. rtsp_st = av_mallocz(sizeof(RTSPStream));
  76. if (!rtsp_st)
  77. return AVERROR(ENOMEM);
  78. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  79. st->priv_data = rtsp_st;
  80. rtsp_st->stream_index = i;
  81. av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
  82. /* Note, this must match the relative uri set in the sdp content */
  83. av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
  84. "/streamid=%d", i);
  85. }
  86. return 0;
  87. }
  88. static int rtsp_write_record(AVFormatContext *s)
  89. {
  90. RTSPState *rt = s->priv_data;
  91. RTSPMessageHeader reply1, *reply = &reply1;
  92. char cmd[1024];
  93. snprintf(cmd, sizeof(cmd),
  94. "Range: npt=%0.3f-\r\n",
  95. (double) 0);
  96. ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
  97. if (reply->status_code != RTSP_STATUS_OK)
  98. return -1;
  99. rt->state = RTSP_STATE_STREAMING;
  100. return 0;
  101. }
  102. static int rtsp_write_header(AVFormatContext *s)
  103. {
  104. int ret;
  105. ret = ff_rtsp_connect(s);
  106. if (ret)
  107. return ret;
  108. if (rtsp_write_record(s) < 0) {
  109. ff_rtsp_close_streams(s);
  110. ff_rtsp_close_connections(s);
  111. return AVERROR_INVALIDDATA;
  112. }
  113. return 0;
  114. }
  115. static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
  116. {
  117. RTSPState *rt = s->priv_data;
  118. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  119. uint8_t *buf, *ptr;
  120. int size;
  121. uint8_t *interleave_header, *interleaved_packet;
  122. size = url_close_dyn_buf(rtpctx->pb, &buf);
  123. ptr = buf;
  124. while (size > 4) {
  125. uint32_t packet_len = AV_RB32(ptr);
  126. int id;
  127. /* The interleaving header is exactly 4 bytes, which happens to be
  128. * the same size as the packet length header from
  129. * url_open_dyn_packet_buf. So by writing the interleaving header
  130. * over these bytes, we get a consecutive interleaved packet
  131. * that can be written in one call. */
  132. interleaved_packet = interleave_header = ptr;
  133. ptr += 4;
  134. size -= 4;
  135. if (packet_len > size || packet_len < 2)
  136. break;
  137. if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
  138. id = rtsp_st->interleaved_max; /* RTCP */
  139. else
  140. id = rtsp_st->interleaved_min; /* RTP */
  141. interleave_header[0] = '$';
  142. interleave_header[1] = id;
  143. AV_WB16(interleave_header + 2, packet_len);
  144. url_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
  145. ptr += packet_len;
  146. size -= packet_len;
  147. }
  148. av_free(buf);
  149. url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
  150. return 0;
  151. }
  152. static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
  153. {
  154. RTSPState *rt = s->priv_data;
  155. RTSPStream *rtsp_st;
  156. fd_set rfds;
  157. int n, tcp_fd;
  158. struct timeval tv;
  159. AVFormatContext *rtpctx;
  160. int ret;
  161. tcp_fd = url_get_file_handle(rt->rtsp_hd);
  162. while (1) {
  163. FD_ZERO(&rfds);
  164. FD_SET(tcp_fd, &rfds);
  165. tv.tv_sec = 0;
  166. tv.tv_usec = 0;
  167. n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
  168. if (n <= 0)
  169. break;
  170. if (FD_ISSET(tcp_fd, &rfds)) {
  171. RTSPMessageHeader reply;
  172. /* Don't let ff_rtsp_read_reply handle interleaved packets,
  173. * since it would block and wait for an RTSP reply on the socket
  174. * (which may not be coming any time soon) if it handles
  175. * interleaved packets internally. */
  176. ret = ff_rtsp_read_reply(s, &reply, NULL, 1);
  177. if (ret < 0)
  178. return AVERROR(EPIPE);
  179. if (ret == 1)
  180. ff_rtsp_skip_packet(s);
  181. /* XXX: parse message */
  182. if (rt->state != RTSP_STATE_STREAMING)
  183. return AVERROR(EPIPE);
  184. }
  185. }
  186. if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
  187. return AVERROR_INVALIDDATA;
  188. rtsp_st = rt->rtsp_streams[pkt->stream_index];
  189. rtpctx = rtsp_st->transport_priv;
  190. ret = ff_write_chained(rtpctx, 0, pkt, s);
  191. /* ff_write_chained does all the RTP packetization. If using TCP as
  192. * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
  193. * packets, so we need to send them out on the TCP connection separately.
  194. */
  195. if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
  196. ret = tcp_write_packet(s, rtsp_st);
  197. return ret;
  198. }
  199. static int rtsp_write_close(AVFormatContext *s)
  200. {
  201. RTSPState *rt = s->priv_data;
  202. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  203. ff_rtsp_close_streams(s);
  204. ff_rtsp_close_connections(s);
  205. ff_network_close();
  206. return 0;
  207. }
  208. AVOutputFormat rtsp_muxer = {
  209. "rtsp",
  210. NULL_IF_CONFIG_SMALL("RTSP output format"),
  211. NULL,
  212. NULL,
  213. sizeof(RTSPState),
  214. CODEC_ID_AAC,
  215. CODEC_ID_MPEG4,
  216. rtsp_write_header,
  217. rtsp_write_packet,
  218. rtsp_write_close,
  219. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  220. };