rtspenc.c 8.0 KB

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