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.

255 lines
8.4KB

  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. 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. char url[MAX_URL_SIZE];
  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)
  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. sdp_ctx.url = url;
  68. ff_url_join(url, sizeof(url),
  69. "rtsp", NULL, addr, -1, NULL);
  70. ctx_array[0] = &sdp_ctx;
  71. if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
  72. av_free(sdp);
  73. return AVERROR_INVALIDDATA;
  74. }
  75. av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
  76. ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
  77. "Content-Type: application/sdp\r\n",
  78. reply, NULL, sdp, strlen(sdp));
  79. av_free(sdp);
  80. if (reply->status_code != RTSP_STATUS_OK)
  81. return ff_rtsp_averror(reply->status_code, AVERROR_INVALIDDATA);
  82. /* Set up the RTSPStreams for each AVStream */
  83. for (i = 0; i < s->nb_streams; i++) {
  84. RTSPStream *rtsp_st;
  85. rtsp_st = av_mallocz(sizeof(RTSPStream));
  86. if (!rtsp_st)
  87. return AVERROR(ENOMEM);
  88. dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
  89. rtsp_st->stream_index = i;
  90. av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
  91. /* Note, this must match the relative uri set in the sdp content */
  92. av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
  93. "/streamid=%d", i);
  94. }
  95. return 0;
  96. }
  97. static int rtsp_write_record(AVFormatContext *s)
  98. {
  99. RTSPState *rt = s->priv_data;
  100. RTSPMessageHeader reply1, *reply = &reply1;
  101. char cmd[MAX_URL_SIZE];
  102. snprintf(cmd, sizeof(cmd),
  103. "Range: npt=0.000-\r\n");
  104. ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
  105. if (reply->status_code != RTSP_STATUS_OK)
  106. return ff_rtsp_averror(reply->status_code, -1);
  107. rt->state = RTSP_STATE_STREAMING;
  108. return 0;
  109. }
  110. static int rtsp_write_header(AVFormatContext *s)
  111. {
  112. int ret;
  113. ret = ff_rtsp_connect(s);
  114. if (ret)
  115. return ret;
  116. if (rtsp_write_record(s) < 0) {
  117. ff_rtsp_close_streams(s);
  118. ff_rtsp_close_connections(s);
  119. return AVERROR_INVALIDDATA;
  120. }
  121. return 0;
  122. }
  123. int ff_rtsp_tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
  124. {
  125. RTSPState *rt = s->priv_data;
  126. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  127. uint8_t *buf, *ptr;
  128. int size;
  129. uint8_t *interleave_header, *interleaved_packet;
  130. size = avio_close_dyn_buf(rtpctx->pb, &buf);
  131. rtpctx->pb = NULL;
  132. ptr = buf;
  133. while (size > 4) {
  134. uint32_t packet_len = AV_RB32(ptr);
  135. int id;
  136. /* The interleaving header is exactly 4 bytes, which happens to be
  137. * the same size as the packet length header from
  138. * ffio_open_dyn_packet_buf. So by writing the interleaving header
  139. * over these bytes, we get a consecutive interleaved packet
  140. * that can be written in one call. */
  141. interleaved_packet = interleave_header = ptr;
  142. ptr += 4;
  143. size -= 4;
  144. if (packet_len > size || packet_len < 2)
  145. break;
  146. if (RTP_PT_IS_RTCP(ptr[1]))
  147. id = rtsp_st->interleaved_max; /* RTCP */
  148. else
  149. id = rtsp_st->interleaved_min; /* RTP */
  150. interleave_header[0] = '$';
  151. interleave_header[1] = id;
  152. AV_WB16(interleave_header + 2, packet_len);
  153. ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
  154. ptr += packet_len;
  155. size -= packet_len;
  156. }
  157. av_free(buf);
  158. return ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
  159. }
  160. static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
  161. {
  162. RTSPState *rt = s->priv_data;
  163. RTSPStream *rtsp_st;
  164. int n;
  165. struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
  166. AVFormatContext *rtpctx;
  167. int ret;
  168. while (1) {
  169. n = poll(&p, 1, 0);
  170. if (n <= 0)
  171. break;
  172. if (p.revents & POLLIN) {
  173. RTSPMessageHeader reply;
  174. /* Don't let ff_rtsp_read_reply handle interleaved packets,
  175. * since it would block and wait for an RTSP reply on the socket
  176. * (which may not be coming any time soon) if it handles
  177. * interleaved packets internally. */
  178. ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
  179. if (ret < 0)
  180. return AVERROR(EPIPE);
  181. if (ret == 1)
  182. ff_rtsp_skip_packet(s);
  183. /* XXX: parse message */
  184. if (rt->state != RTSP_STATE_STREAMING)
  185. return AVERROR(EPIPE);
  186. }
  187. }
  188. if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
  189. return AVERROR_INVALIDDATA;
  190. rtsp_st = rt->rtsp_streams[pkt->stream_index];
  191. rtpctx = rtsp_st->transport_priv;
  192. ret = ff_write_chained(rtpctx, 0, pkt, s, 0);
  193. /* ff_write_chained does all the RTP packetization. If using TCP as
  194. * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
  195. * packets, so we need to send them out on the TCP connection separately.
  196. */
  197. if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
  198. ret = ff_rtsp_tcp_write_packet(s, rtsp_st);
  199. return ret;
  200. }
  201. static int rtsp_write_close(AVFormatContext *s)
  202. {
  203. RTSPState *rt = s->priv_data;
  204. // If we want to send RTCP_BYE packets, these are sent by av_write_trailer.
  205. // Thus call this on all streams before doing the teardown. This is
  206. // done within ff_rtsp_undo_setup.
  207. ff_rtsp_undo_setup(s, 1);
  208. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  209. ff_rtsp_close_streams(s);
  210. ff_rtsp_close_connections(s);
  211. ff_network_close();
  212. return 0;
  213. }
  214. AVOutputFormat ff_rtsp_muxer = {
  215. .name = "rtsp",
  216. .long_name = NULL_IF_CONFIG_SMALL("RTSP output"),
  217. .priv_data_size = sizeof(RTSPState),
  218. .audio_codec = AV_CODEC_ID_AAC,
  219. .video_codec = AV_CODEC_ID_MPEG4,
  220. .write_header = rtsp_write_header,
  221. .write_packet = rtsp_write_packet,
  222. .write_trailer = rtsp_write_close,
  223. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  224. .priv_class = &rtsp_muxer_class,
  225. };