00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "avformat.h"
00023
00024 #include <sys/time.h>
00025 #if HAVE_SYS_SELECT_H
00026 #include <sys/select.h>
00027 #endif
00028 #include "network.h"
00029 #include "rtsp.h"
00030 #include <libavutil/intreadwrite.h>
00031
00032 static int rtsp_write_record(AVFormatContext *s)
00033 {
00034 RTSPState *rt = s->priv_data;
00035 RTSPMessageHeader reply1, *reply = &reply1;
00036 char cmd[1024];
00037
00038 snprintf(cmd, sizeof(cmd),
00039 "Range: npt=%0.3f-\r\n",
00040 (double) 0);
00041 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
00042 if (reply->status_code != RTSP_STATUS_OK)
00043 return -1;
00044 rt->state = RTSP_STATE_STREAMING;
00045 return 0;
00046 }
00047
00048 static int rtsp_write_header(AVFormatContext *s)
00049 {
00050 RTSPState *rt = s->priv_data;
00051 int ret;
00052
00053 ret = ff_rtsp_connect(s);
00054 if (ret)
00055 return ret;
00056
00057 if (rtsp_write_record(s) < 0) {
00058 ff_rtsp_close_streams(s);
00059 url_close(rt->rtsp_hd);
00060 return AVERROR_INVALIDDATA;
00061 }
00062 return 0;
00063 }
00064
00065 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
00066 {
00067 RTSPState *rt = s->priv_data;
00068 AVFormatContext *rtpctx = rtsp_st->transport_priv;
00069 uint8_t *buf, *ptr;
00070 int size;
00071 uint8_t interleave_header[4];
00072
00073 size = url_close_dyn_buf(rtpctx->pb, &buf);
00074 ptr = buf;
00075 while (size > 4) {
00076 uint32_t packet_len = AV_RB32(ptr);
00077 int id;
00078 ptr += 4;
00079 size -= 4;
00080 if (packet_len > size || packet_len < 2)
00081 break;
00082 if (ptr[1] >= 200 && ptr[1] <= 204)
00083 id = rtsp_st->interleaved_max;
00084 else
00085 id = rtsp_st->interleaved_min;
00086 interleave_header[0] = '$';
00087 interleave_header[1] = id;
00088 AV_WB16(interleave_header + 2, packet_len);
00089 url_write(rt->rtsp_hd, interleave_header, 4);
00090 url_write(rt->rtsp_hd, ptr, packet_len);
00091 ptr += packet_len;
00092 size -= packet_len;
00093 }
00094 av_free(buf);
00095 url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
00096 return 0;
00097 }
00098
00099 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
00100 {
00101 RTSPState *rt = s->priv_data;
00102 RTSPStream *rtsp_st;
00103 fd_set rfds;
00104 int n, tcp_fd;
00105 struct timeval tv;
00106 AVFormatContext *rtpctx;
00107 AVPacket local_pkt;
00108 int ret;
00109
00110 tcp_fd = url_get_file_handle(rt->rtsp_hd);
00111
00112 while (1) {
00113 FD_ZERO(&rfds);
00114 FD_SET(tcp_fd, &rfds);
00115 tv.tv_sec = 0;
00116 tv.tv_usec = 0;
00117 n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
00118 if (n <= 0)
00119 break;
00120 if (FD_ISSET(tcp_fd, &rfds)) {
00121 RTSPMessageHeader reply;
00122
00123
00124
00125
00126
00127 ret = ff_rtsp_read_reply(s, &reply, NULL, 1);
00128 if (ret < 0)
00129 return AVERROR(EPIPE);
00130 if (ret == 1)
00131 ff_rtsp_skip_packet(s);
00132
00133 if (rt->state != RTSP_STATE_STREAMING)
00134 return AVERROR(EPIPE);
00135 }
00136 }
00137
00138 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
00139 return AVERROR_INVALIDDATA;
00140 rtsp_st = rt->rtsp_streams[pkt->stream_index];
00141 rtpctx = rtsp_st->transport_priv;
00142
00143
00144
00145 local_pkt = *pkt;
00146 local_pkt.stream_index = 0;
00147 ret = av_write_frame(rtpctx, &local_pkt);
00148
00149
00150
00151
00152 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
00153 ret = tcp_write_packet(s, rtsp_st);
00154 return ret;
00155 }
00156
00157 static int rtsp_write_close(AVFormatContext *s)
00158 {
00159 RTSPState *rt = s->priv_data;
00160
00161 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
00162
00163 ff_rtsp_close_streams(s);
00164 url_close(rt->rtsp_hd);
00165 ff_network_close();
00166 return 0;
00167 }
00168
00169 AVOutputFormat rtsp_muxer = {
00170 "rtsp",
00171 NULL_IF_CONFIG_SMALL("RTSP output format"),
00172 NULL,
00173 NULL,
00174 sizeof(RTSPState),
00175 CODEC_ID_PCM_MULAW,
00176 CODEC_ID_NONE,
00177 rtsp_write_header,
00178 rtsp_write_packet,
00179 rtsp_write_close,
00180 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
00181 };
00182