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.

244 lines
7.6KB

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