rtspenc.c 8.0 KB

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