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.

137 lines
3.5KB

  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. s->filename,
  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. FD_ZERO(&rfds);
  68. tcp_fd = url_get_file_handle(rt->rtsp_hd);
  69. FD_SET(tcp_fd, &rfds);
  70. tv.tv_sec = 0;
  71. tv.tv_usec = 0;
  72. n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
  73. if (n > 0) {
  74. if (FD_ISSET(tcp_fd, &rfds)) {
  75. RTSPMessageHeader reply;
  76. if (ff_rtsp_read_reply(s, &reply, NULL, 0) < 0)
  77. return AVERROR(EPIPE);
  78. /* XXX: parse message */
  79. if (rt->state != RTSP_STATE_STREAMING)
  80. return AVERROR(EPIPE);
  81. }
  82. }
  83. if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
  84. return AVERROR_INVALIDDATA;
  85. rtsp_st = rt->rtsp_streams[pkt->stream_index];
  86. rtpctx = rtsp_st->transport_priv;
  87. /* Use a local packet for writing to the chained muxer, otherwise
  88. * the internal stream_index = 0 becomes visible to the muxer user. */
  89. local_pkt = *pkt;
  90. local_pkt.stream_index = 0;
  91. return av_write_frame(rtpctx, &local_pkt);
  92. }
  93. static int rtsp_write_close(AVFormatContext *s)
  94. {
  95. RTSPState *rt = s->priv_data;
  96. char cmd[1024];
  97. snprintf(cmd, sizeof(cmd),
  98. "TEARDOWN %s RTSP/1.0\r\n",
  99. s->filename);
  100. ff_rtsp_send_cmd_async(s, cmd);
  101. ff_rtsp_close_streams(s);
  102. url_close(rt->rtsp_hd);
  103. ff_network_close();
  104. return 0;
  105. }
  106. AVOutputFormat rtsp_muxer = {
  107. "rtsp",
  108. NULL_IF_CONFIG_SMALL("RTSP output format"),
  109. NULL,
  110. NULL,
  111. sizeof(RTSPState),
  112. CODEC_ID_PCM_MULAW,
  113. CODEC_ID_NONE,
  114. rtsp_write_header,
  115. rtsp_write_packet,
  116. rtsp_write_close,
  117. .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
  118. };