rtspenc.c 5.2 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 <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. "Range: npt=%0.3f-\r\n",
  36. (double) 0);
  37. ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
  38. if (reply->status_code != RTSP_STATUS_OK)
  39. return -1;
  40. rt->state = RTSP_STATE_STREAMING;
  41. return 0;
  42. }
  43. static int rtsp_write_header(AVFormatContext *s)
  44. {
  45. RTSPState *rt = s->priv_data;
  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. url_close(rt->rtsp_hd);
  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[4];
  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. ptr += 4;
  70. size -= 4;
  71. if (packet_len > size || packet_len < 2)
  72. break;
  73. if (ptr[1] >= 200 && ptr[1] <= 204)
  74. id = rtsp_st->interleaved_max; /* RTCP */
  75. else
  76. id = rtsp_st->interleaved_min; /* RTP */
  77. interleave_header[0] = '$';
  78. interleave_header[1] = id;
  79. AV_WB16(interleave_header + 2, packet_len);
  80. url_write(rt->rtsp_hd, interleave_header, 4);
  81. url_write(rt->rtsp_hd, ptr, packet_len);
  82. ptr += packet_len;
  83. size -= packet_len;
  84. }
  85. av_free(buf);
  86. url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
  87. return 0;
  88. }
  89. static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
  90. {
  91. RTSPState *rt = s->priv_data;
  92. RTSPStream *rtsp_st;
  93. fd_set rfds;
  94. int n, tcp_fd;
  95. struct timeval tv;
  96. AVFormatContext *rtpctx;
  97. AVPacket local_pkt;
  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. /* Use a local packet for writing to the chained muxer, otherwise
  129. * the internal stream_index = 0 becomes visible to the muxer user. */
  130. local_pkt = *pkt;
  131. local_pkt.stream_index = 0;
  132. ret = av_write_frame(rtpctx, &local_pkt);
  133. /* av_write_frame 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. url_close(rt->rtsp_hd);
  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_PCM_MULAW,
  157. CODEC_ID_NONE,
  158. rtsp_write_header,
  159. rtsp_write_packet,
  160. rtsp_write_close,
  161. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  162. };