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.

189 lines
5.3KB

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