rtspenc.c 7.5 KB

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