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.

147 lines
3.9KB

  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. static int rtsp_write_record(AVFormatContext *s)
  29. {
  30. RTSPState *rt = s->priv_data;
  31. RTSPMessageHeader reply1, *reply = &reply1;
  32. char cmd[1024];
  33. snprintf(cmd, sizeof(cmd),
  34. "RECORD %s RTSP/1.0\r\n"
  35. "Range: npt=%0.3f-\r\n",
  36. rt->control_uri,
  37. (double) 0);
  38. ff_rtsp_send_cmd(s, 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 rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
  59. {
  60. RTSPState *rt = s->priv_data;
  61. RTSPStream *rtsp_st;
  62. fd_set rfds;
  63. int n, tcp_fd;
  64. struct timeval tv;
  65. AVFormatContext *rtpctx;
  66. AVPacket local_pkt;
  67. int ret;
  68. tcp_fd = url_get_file_handle(rt->rtsp_hd);
  69. while (1) {
  70. FD_ZERO(&rfds);
  71. FD_SET(tcp_fd, &rfds);
  72. tv.tv_sec = 0;
  73. tv.tv_usec = 0;
  74. n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
  75. if (n <= 0)
  76. break;
  77. if (FD_ISSET(tcp_fd, &rfds)) {
  78. RTSPMessageHeader reply;
  79. /* Don't let ff_rtsp_read_reply handle interleaved packets,
  80. * since it would block and wait for an RTSP reply on the socket
  81. * (which may not be coming any time soon) if it handles
  82. * interleaved packets internally. */
  83. ret = ff_rtsp_read_reply(s, &reply, NULL, 1);
  84. if (ret < 0)
  85. return AVERROR(EPIPE);
  86. if (ret == 1)
  87. ff_rtsp_skip_packet(s);
  88. /* XXX: parse message */
  89. if (rt->state != RTSP_STATE_STREAMING)
  90. return AVERROR(EPIPE);
  91. }
  92. }
  93. if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
  94. return AVERROR_INVALIDDATA;
  95. rtsp_st = rt->rtsp_streams[pkt->stream_index];
  96. rtpctx = rtsp_st->transport_priv;
  97. /* Use a local packet for writing to the chained muxer, otherwise
  98. * the internal stream_index = 0 becomes visible to the muxer user. */
  99. local_pkt = *pkt;
  100. local_pkt.stream_index = 0;
  101. return av_write_frame(rtpctx, &local_pkt);
  102. }
  103. static int rtsp_write_close(AVFormatContext *s)
  104. {
  105. RTSPState *rt = s->priv_data;
  106. char cmd[1024];
  107. snprintf(cmd, sizeof(cmd),
  108. "TEARDOWN %s RTSP/1.0\r\n",
  109. rt->control_uri);
  110. ff_rtsp_send_cmd_async(s, cmd);
  111. ff_rtsp_close_streams(s);
  112. url_close(rt->rtsp_hd);
  113. ff_network_close();
  114. return 0;
  115. }
  116. AVOutputFormat rtsp_muxer = {
  117. "rtsp",
  118. NULL_IF_CONFIG_SMALL("RTSP output format"),
  119. NULL,
  120. NULL,
  121. sizeof(RTSPState),
  122. CODEC_ID_PCM_MULAW,
  123. CODEC_ID_NONE,
  124. rtsp_write_header,
  125. rtsp_write_packet,
  126. rtsp_write_close,
  127. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  128. };