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.

254 lines
8.3KB

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