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 "internal.h"
00031 #include "libavutil/intreadwrite.h"
00032 #include "libavutil/avstring.h"
00033
00034 #define SDP_MAX_SIZE 16384
00035
00036 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
00037 {
00038 RTSPState *rt = s->priv_data;
00039 RTSPMessageHeader reply1, *reply = &reply1;
00040 int i;
00041 char *sdp;
00042 AVFormatContext sdp_ctx, *ctx_array[1];
00043
00044 s->start_time_realtime = av_gettime();
00045
00046
00047 sdp = av_mallocz(SDP_MAX_SIZE);
00048 if (sdp == NULL)
00049 return AVERROR(ENOMEM);
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 sdp_ctx = *s;
00063 ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
00064 "rtsp", NULL, addr, -1, NULL);
00065 ctx_array[0] = &sdp_ctx;
00066 if (avf_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
00067 av_free(sdp);
00068 return AVERROR_INVALIDDATA;
00069 }
00070 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
00071 ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
00072 "Content-Type: application/sdp\r\n",
00073 reply, NULL, sdp, strlen(sdp));
00074 av_free(sdp);
00075 if (reply->status_code != RTSP_STATUS_OK)
00076 return AVERROR_INVALIDDATA;
00077
00078
00079 for (i = 0; i < s->nb_streams; i++) {
00080 RTSPStream *rtsp_st;
00081 AVStream *st = s->streams[i];
00082
00083 rtsp_st = av_mallocz(sizeof(RTSPStream));
00084 if (!rtsp_st)
00085 return AVERROR(ENOMEM);
00086 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
00087
00088 st->priv_data = rtsp_st;
00089 rtsp_st->stream_index = i;
00090
00091 av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
00092
00093 av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
00094 "/streamid=%d", i);
00095 }
00096
00097 return 0;
00098 }
00099
00100 static int rtsp_write_record(AVFormatContext *s)
00101 {
00102 RTSPState *rt = s->priv_data;
00103 RTSPMessageHeader reply1, *reply = &reply1;
00104 char cmd[1024];
00105
00106 snprintf(cmd, sizeof(cmd),
00107 "Range: npt=%0.3f-\r\n",
00108 (double) 0);
00109 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
00110 if (reply->status_code != RTSP_STATUS_OK)
00111 return -1;
00112 rt->state = RTSP_STATE_STREAMING;
00113 return 0;
00114 }
00115
00116 static int rtsp_write_header(AVFormatContext *s)
00117 {
00118 int ret;
00119
00120 ret = ff_rtsp_connect(s);
00121 if (ret)
00122 return ret;
00123
00124 if (rtsp_write_record(s) < 0) {
00125 ff_rtsp_close_streams(s);
00126 ff_rtsp_close_connections(s);
00127 return AVERROR_INVALIDDATA;
00128 }
00129 return 0;
00130 }
00131
00132 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
00133 {
00134 RTSPState *rt = s->priv_data;
00135 AVFormatContext *rtpctx = rtsp_st->transport_priv;
00136 uint8_t *buf, *ptr;
00137 int size;
00138 uint8_t *interleave_header, *interleaved_packet;
00139
00140 size = url_close_dyn_buf(rtpctx->pb, &buf);
00141 ptr = buf;
00142 while (size > 4) {
00143 uint32_t packet_len = AV_RB32(ptr);
00144 int id;
00145
00146
00147
00148
00149
00150 interleaved_packet = interleave_header = ptr;
00151 ptr += 4;
00152 size -= 4;
00153 if (packet_len > size || packet_len < 2)
00154 break;
00155 if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
00156 id = rtsp_st->interleaved_max;
00157 else
00158 id = rtsp_st->interleaved_min;
00159 interleave_header[0] = '$';
00160 interleave_header[1] = id;
00161 AV_WB16(interleave_header + 2, packet_len);
00162 url_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
00163 ptr += packet_len;
00164 size -= packet_len;
00165 }
00166 av_free(buf);
00167 url_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
00168 return 0;
00169 }
00170
00171 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
00172 {
00173 RTSPState *rt = s->priv_data;
00174 RTSPStream *rtsp_st;
00175 fd_set rfds;
00176 int n, tcp_fd;
00177 struct timeval tv;
00178 AVFormatContext *rtpctx;
00179 int ret;
00180
00181 tcp_fd = url_get_file_handle(rt->rtsp_hd);
00182
00183 while (1) {
00184 FD_ZERO(&rfds);
00185 FD_SET(tcp_fd, &rfds);
00186 tv.tv_sec = 0;
00187 tv.tv_usec = 0;
00188 n = select(tcp_fd + 1, &rfds, NULL, NULL, &tv);
00189 if (n <= 0)
00190 break;
00191 if (FD_ISSET(tcp_fd, &rfds)) {
00192 RTSPMessageHeader reply;
00193
00194
00195
00196
00197
00198 ret = ff_rtsp_read_reply(s, &reply, NULL, 1, NULL);
00199 if (ret < 0)
00200 return AVERROR(EPIPE);
00201 if (ret == 1)
00202 ff_rtsp_skip_packet(s);
00203
00204 if (rt->state != RTSP_STATE_STREAMING)
00205 return AVERROR(EPIPE);
00206 }
00207 }
00208
00209 if (pkt->stream_index < 0 || pkt->stream_index >= rt->nb_rtsp_streams)
00210 return AVERROR_INVALIDDATA;
00211 rtsp_st = rt->rtsp_streams[pkt->stream_index];
00212 rtpctx = rtsp_st->transport_priv;
00213
00214 ret = ff_write_chained(rtpctx, 0, pkt, s);
00215
00216
00217
00218
00219 if (!ret && rt->lower_transport == RTSP_LOWER_TRANSPORT_TCP)
00220 ret = tcp_write_packet(s, rtsp_st);
00221 return ret;
00222 }
00223
00224 static int rtsp_write_close(AVFormatContext *s)
00225 {
00226 RTSPState *rt = s->priv_data;
00227
00228 ff_rtsp_send_cmd_async(s, "TEARDOWN", rt->control_uri, NULL);
00229
00230 ff_rtsp_close_streams(s);
00231 ff_rtsp_close_connections(s);
00232 ff_network_close();
00233 return 0;
00234 }
00235
00236 AVOutputFormat rtsp_muxer = {
00237 "rtsp",
00238 NULL_IF_CONFIG_SMALL("RTSP output format"),
00239 NULL,
00240 NULL,
00241 sizeof(RTSPState),
00242 CODEC_ID_AAC,
00243 CODEC_ID_MPEG4,
00244 rtsp_write_header,
00245 rtsp_write_packet,
00246 rtsp_write_close,
00247 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
00248 };
00249