rtspenc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_SYS_SELECT_H
  24. #include <sys/select.h>
  25. #endif
  26. #include "network.h"
  27. #include "rtsp.h"
  28. #include "internal.h"
  29. #include "libavutil/intreadwrite.h"
  30. static int rtsp_write_record(AVFormatContext *s)
  31. {
  32. RTSPState *rt = s->priv_data;
  33. RTSPMessageHeader reply1, *reply = &reply1;
  34. char cmd[1024];
  35. snprintf(cmd, sizeof(cmd),
  36. "Range: npt=%0.3f-\r\n",
  37. (double) 0);
  38. ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
  39. if (reply->status_code != RTSP_STATUS_OK)
  40. return -1;
  41. rt->state = RTSP_STATE_STREAMING;
  42. return 0;
  43. }
  44. static int rtsp_write_header(AVFormatContext *s)
  45. {
  46. int ret;
  47. ret = ff_rtsp_connect(s);
  48. if (ret)
  49. return ret;
  50. if (rtsp_write_record(s) < 0) {
  51. ff_rtsp_close_streams(s);
  52. ff_rtsp_close_connections(s);
  53. return AVERROR_INVALIDDATA;
  54. }
  55. return 0;
  56. }
  57. static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
  58. {
  59. RTSPState *rt = s->priv_data;
  60. AVFormatContext *rtpctx = rtsp_st->transport_priv;
  61. uint8_t *buf, *ptr;
  62. int size;
  63. uint8_t *interleave_header, *interleaved_packet;
  64. size = url_close_dyn_buf(rtpctx->pb, &buf);
  65. ptr = buf;
  66. while (size > 4) {
  67. uint32_t packet_len = AV_RB32(ptr);
  68. int id;
  69. /* The interleaving header is exactly 4 bytes, which happens to be
  70. * the same size as the packet length header from
  71. * url_open_dyn_packet_buf. So by writing the interleaving header
  72. * over these bytes, we get a consecutive interleaved packet
  73. * that can be written in one call. */
  74. interleaved_packet = interleave_header = ptr;
  75. ptr += 4;
  76. size -= 4;
  77. if (packet_len > size || packet_len < 2)
  78. break;
  79. if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
  80. id = rtsp_st->interleaved_max; /* RTCP */
  81. else
  82. id = rtsp_st->interleaved_min; /* RTP */
  83. interleave_header[0] = '$';
  84. interleave_header[1] = id;
  85. AV_WB16(interleave_header + 2, packet_len);
  86. url_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
  87. ptr += packet_len;
  88. size -= packet_len;
  89. }
  90. av_free(buf);
  91. url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
  92. return 0;
  93. }
  94. static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
  95. {
  96. RTSPState *rt = s->priv_data;
  97. RTSPStream *rtsp_st;
  98. fd_set rfds;
  99. int n, tcp_fd;
  100. struct timeval tv;
  101. AVFormatContext *rtpctx;
  102. int ret;
  103. tcp_fd = url_get_file_handle(rt->rtsp_hd);
  104. while (1) {
  105. FD_ZERO(&rfds);
  106. FD_SET(tcp_fd, &rfds);
  107. tv.tv_sec = 0;
  108. tv.tv_usec = 0;
  109. n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
  110. if (n <= 0)
  111. break;
  112. if (FD_ISSET(tcp_fd, &rfds)) {
  113. RTSPMessageHeader reply;
  114. /* Don't let ff_rtsp_read_reply handle interleaved packets,
  115. * since it would block and wait for an RTSP reply on the socket
  116. * (which may not be coming any time soon) if it handles
  117. * interleaved packets internally. */
  118. ret = ff_rtsp_read_reply(s, &reply, NULL, 1);
  119. if (ret < 0)
  120. return AVERROR(EPIPE);
  121. if (ret == 1)
  122. ff_rtsp_skip_packet(s);
  123. /* XXX: parse message */
  124. if (rt->state != RTSP_STATE_STREAMING)
  125. return AVERROR(EPIPE);
  126. }
  127. }
  128. if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
  129. return AVERROR_INVALIDDATA;
  130. rtsp_st = rt->rtsp_streams[pkt->stream_index];
  131. rtpctx = rtsp_st->transport_priv;
  132. ret = ff_write_chained(rtpctx, 0, pkt, s);
  133. /* ff_write_chained does all the RTP packetization. If using TCP as
  134. * transport, rtpctx->pb is only a dyn_packet_buf that queues up the
  135. * packets, so we need to send them out on the TCP connection separately.
  136. */
  137. if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
  138. ret = tcp_write_packet(s, rtsp_st);
  139. return ret;
  140. }
  141. static int rtsp_write_close(AVFormatContext *s)
  142. {
  143. RTSPState *rt = s->priv_data;
  144. ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
  145. ff_rtsp_close_streams(s);
  146. ff_rtsp_close_connections(s);
  147. ff_network_close();
  148. return 0;
  149. }
  150. AVOutputFormat rtsp_muxer = {
  151. "rtsp",
  152. NULL_IF_CONFIG_SMALL("RTSP output format"),
  153. NULL,
  154. NULL,
  155. sizeof(RTSPState),
  156. CODEC_ID_AAC,
  157. CODEC_ID_MPEG4,
  158. rtsp_write_header,
  159. rtsp_write_packet,
  160. rtsp_write_close,
  161. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  162. };