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_POLL_H
00026 #include <poll.h>
00027 #endif
00028 #include "network.h"
00029 #include "os_support.h"
00030 #include "rtsp.h"
00031 #include "internal.h"
00032 #include "avio_internal.h"
00033 #include "libavutil/intreadwrite.h"
00034 #include "libavutil/avstring.h"
00035 #include "url.h"
00036
00037 #define SDP_MAX_SIZE 16384
00038
00039 static const AVClass rtsp_muxer_class = {
00040 .class_name = "RTSP muxer",
00041 .item_name = av_default_item_name,
00042 .option = ff_rtsp_options,
00043 .version = LIBAVUTIL_VERSION_INT,
00044 };
00045
00046 int ff_rtsp_setup_output_streams(AVFormatContext *s, const char *addr)
00047 {
00048 RTSPState *rt = s->priv_data;
00049 RTSPMessageHeader reply1, *reply = &reply1;
00050 int i;
00051 char *sdp;
00052 AVFormatContext sdp_ctx, *ctx_array[1];
00053
00054 s->start_time_realtime = av_gettime();
00055
00056
00057 sdp = av_mallocz(SDP_MAX_SIZE);
00058 if (sdp == NULL)
00059 return AVERROR(ENOMEM);
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 sdp_ctx = *s;
00073 ff_url_join(sdp_ctx.filename, sizeof(sdp_ctx.filename),
00074 "rtsp", NULL, addr, -1, NULL);
00075 ctx_array[0] = &sdp_ctx;
00076 if (av_sdp_create(ctx_array, 1, sdp, SDP_MAX_SIZE)) {
00077 av_free(sdp);
00078 return AVERROR_INVALIDDATA;
00079 }
00080 av_log(s, AV_LOG_VERBOSE, "SDP:\n%s\n", sdp);
00081 ff_rtsp_send_cmd_with_content(s, "ANNOUNCE", rt->control_uri,
00082 "Content-Type: application/sdp\r\n",
00083 reply, NULL, sdp, strlen(sdp));
00084 av_free(sdp);
00085 if (reply->status_code != RTSP_STATUS_OK)
00086 return AVERROR_INVALIDDATA;
00087
00088
00089 for (i = 0; i < s->nb_streams; i++) {
00090 RTSPStream *rtsp_st;
00091
00092 rtsp_st = av_mallocz(sizeof(RTSPStream));
00093 if (!rtsp_st)
00094 return AVERROR(ENOMEM);
00095 dynarray_add(&rt->rtsp_streams, &rt->nb_rtsp_streams, rtsp_st);
00096
00097 rtsp_st->stream_index = i;
00098
00099 av_strlcpy(rtsp_st->control_url, rt->control_uri, sizeof(rtsp_st->control_url));
00100
00101 av_strlcatf(rtsp_st->control_url, sizeof(rtsp_st->control_url),
00102 "/streamid=%d", i);
00103 }
00104
00105 return 0;
00106 }
00107
00108 static int rtsp_write_record(AVFormatContext *s)
00109 {
00110 RTSPState *rt = s->priv_data;
00111 RTSPMessageHeader reply1, *reply = &reply1;
00112 char cmd[1024];
00113
00114 snprintf(cmd, sizeof(cmd),
00115 "Range: npt=0.000-\r\n");
00116 ff_rtsp_send_cmd(s, "RECORD", rt->control_uri, cmd, reply, NULL);
00117 if (reply->status_code != RTSP_STATUS_OK)
00118 return -1;
00119 rt->state = RTSP_STATE_STREAMING;
00120 return 0;
00121 }
00122
00123 static int rtsp_write_header(AVFormatContext *s)
00124 {
00125 int ret;
00126
00127 ret = ff_rtsp_connect(s);
00128 if (ret)
00129 return ret;
00130
00131 if (rtsp_write_record(s) < 0) {
00132 ff_rtsp_close_streams(s);
00133 ff_rtsp_close_connections(s);
00134 return AVERROR_INVALIDDATA;
00135 }
00136 return 0;
00137 }
00138
00139 static int tcp_write_packet(AVFormatContext *s, RTSPStream *rtsp_st)
00140 {
00141 RTSPState *rt = s->priv_data;
00142 AVFormatContext *rtpctx = rtsp_st->transport_priv;
00143 uint8_t *buf, *ptr;
00144 int size;
00145 uint8_t *interleave_header, *interleaved_packet;
00146
00147 size = avio_close_dyn_buf(rtpctx->pb, &buf);
00148 ptr = buf;
00149 while (size > 4) {
00150 uint32_t packet_len = AV_RB32(ptr);
00151 int id;
00152
00153
00154
00155
00156
00157 interleaved_packet = interleave_header = ptr;
00158 ptr += 4;
00159 size -= 4;
00160 if (packet_len > size || packet_len < 2)
00161 break;
00162 if (ptr[1] >= RTCP_SR && ptr[1] <= RTCP_APP)
00163 id = rtsp_st->interleaved_max;
00164 else
00165 id = rtsp_st->interleaved_min;
00166 interleave_header[0] = '$';
00167 interleave_header[1] = id;
00168 AV_WB16(interleave_header + 2, packet_len);
00169 ffurl_write(rt->rtsp_hd_out, interleaved_packet, 4 + packet_len);
00170 ptr += packet_len;
00171 size -= packet_len;
00172 }
00173 av_free(buf);
00174 ffio_open_dyn_packet_buf(&rtpctx->pb, RTSP_TCP_MAX_PACKET_SIZE);
00175 return 0;
00176 }
00177
00178 static int rtsp_write_packet(AVFormatContext *s, AVPacket *pkt)
00179 {
00180 RTSPState *rt = s->priv_data;
00181 RTSPStream *rtsp_st;
00182 int n;
00183 struct pollfd p = {ffurl_get_file_handle(rt->rtsp_hd), POLLIN, 0};
00184 AVFormatContext *rtpctx;
00185 int ret;
00186
00187 while (1) {
00188 n = poll(&p, 1, 0);
00189 if (n <= 0)
00190 break;
00191 if (p.revents & POLLIN) {
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 ff_rtsp_muxer = {
00237 .name = "rtsp",
00238 .long_name = NULL_IF_CONFIG_SMALL("RTSP output format"),
00239 .priv_data_size = sizeof(RTSPState),
00240 .audio_codec = CODEC_ID_AAC,
00241 .video_codec = CODEC_ID_MPEG4,
00242 .write_header = rtsp_write_header,
00243 .write_packet = rtsp_write_packet,
00244 .write_trailer = rtsp_write_close,
00245 .flags = AVFMT_NOFILE | AVFMT_GLOBALHEADER,
00246 .priv_class = &rtsp_muxer_class,
00247 };
00248