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.

247 lines
7.9KB

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