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.

179 lines
5.0KB

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