#include "libavutil/avstring.h"
#include "libavutil/lfg.h"
#include "libavutil/random_seed.h"
+#include "libavutil/parseutils.h"
#include "libavcodec/opt.h"
#include <stdarg.h>
#include <unistd.h>
"RTSP_SEND_PACKET",
};
+#if !FF_API_MAX_STREAMS
+#define MAX_STREAMS 20
+#endif
+
#define IOBUFFER_INIT_SIZE 8192
/* timeouts are in ms */
/* RTSP state specific */
uint8_t *pb_buffer; /* XXX: use that in all the code */
- ByteIOContext *pb;
+ AVIOContext *pb;
int seq; /* RTSP sequence number */
/* RTP state specific */
}
}
-static void __attribute__ ((format (printf, 1, 2))) http_log(const char *fmt, ...)
+#ifdef __GNUC__
+__attribute__ ((format (printf, 1, 2)))
+#endif
+static void http_log(const char *fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
second to handle timeouts */
do {
ret = poll(poll_table, poll_entry - poll_table, delay);
- if (ret < 0 && ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR))
+ if (ret < 0 && ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR))
return -1;
} while (ret < 0);
{
char buffer[300];
int len = snprintf(buffer, sizeof(buffer),
- "HTTP/1.0 200 Server too busy\r\n"
+ "HTTP/1.0 503 Server too busy\r\n"
"Content-type: text/html\r\n"
"\r\n"
"<html><head><title>Too busy</title></head><body>\r\n"
ctx = c->rtp_ctx[i];
if (ctx) {
av_write_trailer(ctx);
+ av_metadata_free(&ctx->metadata);
+ av_free(ctx->streams[0]);
av_free(ctx);
}
h = c->rtp_handles[i];
read_loop:
len = recv(c->fd, c->buffer_ptr, 1, 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR))
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR))
return -1;
} else if (len == 0) {
return -1;
return 0;
len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR)) {
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR)) {
/* error : close connection */
av_freep(&c->pb_buffer);
return -1;
return 0;
len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR)) {
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR)) {
/* error : close connection */
av_freep(&c->pb_buffer);
return -1;
len = send(c->fd, c->packet_buffer_ptr,
c->packet_buffer_end - c->packet_buffer_ptr, 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR)) {
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR)) {
/* error : close connection */
av_freep(&c->packet_buffer);
return -1;
return action_required;
}
-
-static void do_switch_stream(HTTPContext *c, int i)
-{
- if (c->switch_feed_streams[i] >= 0) {
-#ifdef PHILIP
- c->feed_streams[i] = c->switch_feed_streams[i];
-#endif
-
- /* Now update the stream */
- }
- c->switch_feed_streams[i] = -1;
-}
-
/* XXX: factorize in utils.c ? */
/* XXX: take care with different space meaning */
static void skip_spaces(const char **pp)
if (modify_current_stream(c, ratebuf)) {
for (i = 0; i < FF_ARRAY_ELEMS(c->feed_streams); i++) {
if (c->switch_feed_streams[i] >= 0)
- do_switch_stream(c, i);
+ c->switch_feed_streams[i] = -1;
}
}
}
}
if (c->post == 0 && max_bandwidth < current_bandwidth) {
- c->http_error = 200;
+ c->http_error = 503;
q = c->buffer;
q += snprintf(q, c->buffer_size,
- "HTTP/1.0 200 Server too busy\r\n"
+ "HTTP/1.0 503 Server too busy\r\n"
"Content-type: text/html\r\n"
"\r\n"
"<html><head><title>Too busy</title></head><body>\r\n"
return 0;
}
-static void fmt_bytecount(ByteIOContext *pb, int64_t count)
+static void fmt_bytecount(AVIOContext *pb, int64_t count)
{
static const char *suffix = " kMGTP";
const char *s;
char *p;
time_t ti;
int i, len;
- ByteIOContext *pb;
+ AVIOContext *pb;
if (url_open_dyn_buf(&pb) < 0) {
/* XXX: return an error ? */
strcpy(input_filename, c->stream->feed->feed_filename);
buf_size = FFM_PACKET_SIZE;
/* compute position (absolute time) */
- if (find_info_tag(buf, sizeof(buf), "date", info)) {
- stream_pos = parse_date(buf, 0);
- if (stream_pos == INT64_MIN)
- return -1;
- } else if (find_info_tag(buf, sizeof(buf), "buffer", info)) {
+ if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
+ if ((ret = av_parse_time(&stream_pos, buf, 0)) < 0)
+ return ret;
+ } else if (av_find_info_tag(buf, sizeof(buf), "buffer", info)) {
int prebuffer = strtol(buf, 0, 10);
stream_pos = av_gettime() - prebuffer * (int64_t)1000000;
} else
strcpy(input_filename, c->stream->feed_filename);
buf_size = 0;
/* compute position (relative time) */
- if (find_info_tag(buf, sizeof(buf), "date", info)) {
- stream_pos = parse_date(buf, 1);
- if (stream_pos == INT64_MIN)
- return -1;
+ if (av_find_info_tag(buf, sizeof(buf), "date", info)) {
+ if ((ret = av_parse_time(&stream_pos, buf, 1)) < 0)
+ return ret;
} else
stream_pos = 0;
}
http_log("Error writing output header\n");
return -1;
}
+ av_metadata_free(&c->fmt_ctx.metadata);
len = url_close_dyn_buf(c->fmt_ctx.pb, &c->pb_buffer);
c->buffer_ptr = c->pb_buffer;
else {
AVPacket pkt;
redo:
- if (av_read_frame(c->fmt_in, &pkt) < 0) {
- if (c->stream->feed && c->stream->feed->feed_opened) {
+ ret = av_read_frame(c->fmt_in, &pkt);
+ if (ret < 0) {
+ if (c->stream->feed) {
/* if coming from feed, it means we reached the end of the
ffm file, so must wait for more data */
c->state = HTTPSTATE_WAIT_FEED;
return 1; /* state changed */
+ } else if (ret == AVERROR(EAGAIN)) {
+ /* input not ready, come back later */
+ return 0;
} else {
if (c->stream->loop) {
av_close_input_file(c->fmt_in);
for(i=0;i<c->stream->nb_streams;i++) {
if (c->switch_feed_streams[i] == pkt.stream_index)
if (pkt.flags & AV_PKT_FLAG_KEY)
- do_switch_stream(c, i);
+ c->switch_feed_streams[i] = -1;
if (c->switch_feed_streams[i] >= 0)
c->switch_pending = 1;
}
}
for(i=0;i<c->stream->nb_streams;i++) {
- if (c->feed_streams[i] == pkt.stream_index) {
+ if (c->stream->feed_streams[i] == pkt.stream_index) {
AVStream *st = c->fmt_in->streams[source_index];
pkt.stream_index = i;
if (pkt.flags & AV_PKT_FLAG_KEY &&
if (c->is_packetized) {
/* compute send time and duration */
c->cur_pts = av_rescale_q(pkt.dts, ist->time_base, AV_TIME_BASE_Q);
- if (ist->start_time != AV_NOPTS_VALUE)
- c->cur_pts -= av_rescale_q(ist->start_time, ist->time_base, AV_TIME_BASE_Q);
+ c->cur_pts -= c->first_pts;
c->cur_frame_duration = av_rescale_q(pkt.duration, ist->time_base, AV_TIME_BASE_Q);
/* find RTP context */
c->packet_stream_index = pkt.stream_index;
if (c->rtp_protocol == RTSP_LOWER_TRANSPORT_TCP) {
/* RTP packets are sent inside the RTSP TCP connection */
- ByteIOContext *pb;
+ AVIOContext *pb;
int interleaved_index, size;
uint8_t header[4];
HTTPContext *rtsp_c;
header[1] = interleaved_index;
header[2] = len >> 8;
header[3] = len;
- put_buffer(pb, header, 4);
+ avio_write(pb, header, 4);
/* write RTP packet data */
c->buffer_ptr += 4;
- put_buffer(pb, c->buffer_ptr, len);
+ avio_write(pb, c->buffer_ptr, len);
size = url_close_dyn_buf(pb, &c->packet_buffer);
/* prepare asynchronous TCP sending */
rtsp_c->packet_buffer_ptr = c->packet_buffer;
/* TCP data output */
len = send(c->fd, c->buffer_ptr, c->buffer_end - c->buffer_ptr, 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR))
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR))
/* error : close connection */
return -1;
else
len = recv(c->fd, c->buffer_ptr, 1, 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR))
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR))
/* error : close connection */
goto fail;
return 0;
len = recv(c->fd, c->buffer_ptr,
FFMIN(c->chunk_size, c->buffer_end - c->buffer_ptr), 0);
if (len < 0) {
- if (ff_neterrno() != FF_NETERROR(EAGAIN) &&
- ff_neterrno() != FF_NETERROR(EINTR))
+ if (ff_neterrno() != AVERROR(EAGAIN) &&
+ ff_neterrno() != AVERROR(EINTR))
/* error : close connection */
goto fail;
} else if (len == 0)
} else {
/* We have a header in our hands that contains useful data */
AVFormatContext *s = NULL;
- ByteIOContext *pb;
+ AVIOContext *pb;
AVInputFormat *fmt_in;
int i;
for (i = 0; i < s->nb_streams; i++) {
AVStream *fst = feed->streams[i];
AVStream *st = s->streams[i];
- memcpy(fst->codec, st->codec, sizeof(AVCodecContext));
- if (fst->codec->extradata_size) {
- fst->codec->extradata = av_malloc(fst->codec->extradata_size);
- if (!fst->codec->extradata)
- goto fail;
- memcpy(fst->codec->extradata, st->codec->extradata,
- fst->codec->extradata_size);
- }
+ avcodec_copy_context(fst->codec, st->codec);
}
av_close_input_stream(s);
if (*p == '\n')
p++;
while (*p != '\0') {
- p1 = strchr(p, '\n');
+ p1 = memchr(p, '\n', (char *)c->buffer_ptr - p);
if (!p1)
break;
p2 = p1;
len = sizeof(line) - 1;
memcpy(line, p, len);
line[len] = '\0';
- ff_rtsp_parse_line(header, line, NULL);
+ ff_rtsp_parse_line(header, line, NULL, NULL);
p = p1 + 1;
}
struct in_addr my_ip)
{
AVFormatContext *avc;
- AVStream avs[MAX_STREAMS];
+ AVStream *avs = NULL;
int i;
avc = avformat_alloc_context();
snprintf(avc->filename, 1024, "rtp://0.0.0.0");
}
+#if !FF_API_MAX_STREAMS
+ if (avc->nb_streams >= INT_MAX/sizeof(*avc->streams) ||
+ !(avc->streams = av_malloc(avc->nb_streams * sizeof(*avc->streams))))
+ goto sdp_done;
+#endif
+ if (avc->nb_streams >= INT_MAX/sizeof(*avs) ||
+ !(avs = av_malloc(avc->nb_streams * sizeof(*avs))))
+ goto sdp_done;
+
for(i = 0; i < stream->nb_streams; i++) {
avc->streams[i] = &avs[i];
avc->streams[i]->codec = stream->streams[i]->codec;
}
*pbuffer = av_mallocz(2048);
avf_sdp_create(&avc, 1, *pbuffer, 2048);
+
+ sdp_done:
+#if !FF_API_MAX_STREAMS
+ av_free(avc->streams);
+#endif
+ av_metadata_free(&avc->metadata);
av_free(avc);
+ av_free(avs);
return strlen(*pbuffer);
}
struct sockaddr_in my_addr;
/* find which url is asked */
- ff_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
+ av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
path = path1;
if (*path == '/')
path++;
return;
}
rtsp_reply_header(c, RTSP_STATUS_OK);
+ url_fprintf(c->pb, "Content-Base: %s/\r\n", url);
url_fprintf(c->pb, "Content-Type: application/sdp\r\n");
url_fprintf(c->pb, "Content-Length: %d\r\n", content_length);
url_fprintf(c->pb, "\r\n");
- put_buffer(c->pb, content, content_length);
+ avio_write(c->pb, content, content_length);
+ av_free(content);
}
static HTTPContext *find_rtp_session(const char *session_id)
RTSPActionServerSetup setup;
/* find which url is asked */
- ff_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
+ av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
path = path1;
if (*path == '/')
path++;
char path1[1024];
const char *path;
char buf[1024];
- int s;
+ int s, len;
rtp_c = find_rtp_session(session_id);
if (!rtp_c)
return NULL;
/* find which url is asked */
- ff_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
+ av_url_split(NULL, 0, NULL, 0, NULL, 0, NULL, path1, sizeof(path1), url);
path = path1;
if (*path == '/')
path++;
return rtp_c;
}
}
+ len = strlen(path);
+ if (len > 0 && path[len - 1] == '/' &&
+ !strncmp(path, rtp_c->stream->filename, len - 1))
+ return rtp_c;
return NULL;
}
st = av_mallocz(sizeof(AVStream));
if (!st)
goto fail;
- st->codec= avcodec_alloc_context();
ctx->nb_streams = 1;
ctx->streams[0] = st;
/********************************************************************/
/* ffserver initialization */
-static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec)
+static AVStream *add_av_stream1(FFStream *stream, AVCodecContext *codec, int copy)
{
AVStream *fst;
fst = av_mallocz(sizeof(AVStream));
if (!fst)
return NULL;
- fst->codec= avcodec_alloc_context();
+ if (copy) {
+ fst->codec= avcodec_alloc_context();
+ memcpy(fst->codec, codec, sizeof(AVCodecContext));
+ if (codec->extradata_size) {
+ fst->codec->extradata = av_malloc(codec->extradata_size);
+ memcpy(fst->codec->extradata, codec->extradata,
+ codec->extradata_size);
+ }
+ } else {
+ /* live streams must use the actual feed's codec since it may be
+ * updated later to carry extradata needed by the streams.
+ */
+ fst->codec = codec;
+ }
fst->priv_data = av_mallocz(sizeof(FeedData));
- memcpy(fst->codec, codec, sizeof(AVCodecContext));
fst->index = stream->nb_streams;
av_set_pts_info(fst, 33, 1, 90000);
+ fst->sample_aspect_ratio = codec->sample_aspect_ratio;
stream->streams[stream->nb_streams++] = fst;
return fst;
}
}
}
- fst = add_av_stream1(feed, av);
+ fst = add_av_stream1(feed, av, 0);
if (!fst)
return -1;
return feed->nb_streams - 1;
extract_mpeg4_header(infile);
for(i=0;i<infile->nb_streams;i++)
- add_av_stream1(stream, infile->streams[i]->codec);
+ add_av_stream1(stream, infile->streams[i]->codec, 1);
av_close_input_file(infile);
}
ccs = ss->codec;
#define CHECK_CODEC(x) (ccf->x != ccs->x)
- if (CHECK_CODEC(codec) || CHECK_CODEC(codec_type)) {
+ if (CHECK_CODEC(codec_id) || CHECK_CODEC(codec_type)) {
http_log("Codecs do not match for stream %d\n", i);
matches = 0;
} else if (CHECK_CODEC(bit_rate) || CHECK_CODEC(flags)) {
}
/* only write the header of the ffm file */
- if (url_fopen(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
+ if (avio_open(&s->pb, feed->feed_filename, URL_WRONLY) < 0) {
http_log("Could not open output feed file '%s'\n",
feed->feed_filename);
exit(1);
}
/* XXX: need better api */
av_freep(&s->priv_data);
- url_fclose(s->pb);
+ avio_close(s->pb);
}
/* get feed size and write index */
fd = open(feed->feed_filename, O_RDONLY);
return ret;
}
+static int ffserver_opt_preset(const char *arg,
+ AVCodecContext *avctx, int type,
+ enum CodecID *audio_id, enum CodecID *video_id)
+{
+ FILE *f=NULL;
+ char filename[1000], tmp[1000], tmp2[1000], line[1000];
+ int ret = 0;
+ AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
+
+ if (!(f = get_preset_file(filename, sizeof(filename), arg, 0,
+ codec ? codec->name : NULL))) {
+ fprintf(stderr, "File for preset '%s' not found\n", arg);
+ return 1;
+ }
+
+ while(!feof(f)){
+ int e= fscanf(f, "%999[^\n]\n", line) - 1;
+ if(line[0] == '#' && !e)
+ continue;
+ e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
+ if(e){
+ fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
+ ret = 1;
+ break;
+ }
+ if(!strcmp(tmp, "acodec")){
+ *audio_id = opt_audio_codec(tmp2);
+ }else if(!strcmp(tmp, "vcodec")){
+ *video_id = opt_video_codec(tmp2);
+ }else if(!strcmp(tmp, "scodec")){
+ /* opt_subtitle_codec(tmp2); */
+ }else if(ffserver_opt_default(tmp, tmp2, avctx, type) < 0){
+ fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
+ ret = 1;
+ break;
+ }
+ }
+
+ fclose(f);
+
+ return ret;
+}
+
static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename,
const char *mime_type)
{
} else if (!strcasecmp(cmd, "AudioBitRate")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
- audio_enc.bit_rate = atoi(arg) * 1000;
+ audio_enc.bit_rate = lrintf(atof(arg) * 1000);
} else if (!strcasecmp(cmd, "AudioChannels")) {
get_arg(arg, sizeof(arg), &p);
if (stream)
} else if (!strcasecmp(cmd, "VideoSize")) {
get_arg(arg, sizeof(arg), &p);
if (stream) {
- av_parse_video_frame_size(&video_enc.width, &video_enc.height, arg);
+ av_parse_video_size(&video_enc.width, &video_enc.height, arg);
if ((video_enc.width % 16) != 0 ||
(video_enc.height % 16) != 0) {
ERROR("Image size must be a multiple of 16\n");
get_arg(arg, sizeof(arg), &p);
if (stream) {
AVRational frame_rate;
- if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
+ if (av_parse_video_rate(&frame_rate, arg) < 0) {
ERROR("Incorrect frame rate: %s\n", arg);
} else {
video_enc.time_base.num = frame_rate.den;
if (ffserver_opt_default(arg, arg2, avctx, type|AV_OPT_FLAG_ENCODING_PARAM)) {
ERROR("AVOption error: %s %s\n", arg, arg2);
}
+ } else if (!strcasecmp(cmd, "AVPresetVideo") ||
+ !strcasecmp(cmd, "AVPresetAudio")) {
+ AVCodecContext *avctx;
+ int type;
+ get_arg(arg, sizeof(arg), &p);
+ if (!strcasecmp(cmd, "AVPresetVideo")) {
+ avctx = &video_enc;
+ video_enc.codec_id = video_id;
+ type = AV_OPT_FLAG_VIDEO_PARAM;
+ } else {
+ avctx = &audio_enc;
+ audio_enc.codec_id = audio_id;
+ type = AV_OPT_FLAG_AUDIO_PARAM;
+ }
+ if (ffserver_opt_preset(arg, avctx, type|AV_OPT_FLAG_ENCODING_PARAM, &audio_id, &video_id)) {
+ ERROR("AVPreset error: %s\n", arg);
+ }
} else if (!strcasecmp(cmd, "VideoTag")) {
get_arg(arg, sizeof(arg), &p);
if ((strlen(arg) == 4) && stream)
unsetenv("http_proxy"); /* Kill the http_proxy */
- av_lfg_init(&random_state, ff_random_get_seed());
+ av_lfg_init(&random_state, av_get_random_seed());
memset(&sigact, 0, sizeof(sigact));
sigact.sa_handler = handle_child_exit;