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.

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