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.

242 lines
7.6KB

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