X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ffserver.c;h=83cd4e4cf1cf1ad61d78ef09fe0b0bb781046f55;hb=792098c2ec867e151b8457d6b99978cd074db04e;hp=394134b373217194838bc2d4f9df9e9a9d856376;hpb=c60202dffe995aef7dae4419ca6e2895e976767e;p=ffmpeg diff --git a/ffserver.c b/ffserver.c index 394134b3732..83cd4e4cf1c 100644 --- a/ffserver.c +++ b/ffserver.c @@ -94,7 +94,7 @@ const char *http_state[] = { typedef struct { int64_t count1, count2; - long time1, time2; + int64_t time1, time2; } DataRateData; /* context associated with one connection */ @@ -103,7 +103,7 @@ typedef struct HTTPContext { int fd; /* socket file descriptor */ struct sockaddr_in from_addr; /* origin */ struct pollfd *poll_entry; /* used when polling */ - long timeout; + int64_t timeout; uint8_t *buffer_ptr, *buffer_end; int http_error; int post; @@ -114,7 +114,7 @@ typedef struct HTTPContext { int feed_fd; /* input format handling */ AVFormatContext *fmt_in; - long start_time; /* In milliseconds - this wraps fairly often */ + int64_t start_time; /* In milliseconds - this wraps fairly often */ int64_t first_pts; /* initial pts value */ int64_t cur_pts; /* current pts value from the stream in us */ int64_t cur_frame_duration; /* duration of the current frame in us */ @@ -194,7 +194,7 @@ typedef struct FFStream { IPAddressACL *acl; int nb_streams; int prebuffer; /* Number of millseconds early to start */ - long max_time; /* Number of milliseconds to run */ + int64_t max_time; /* Number of milliseconds to run */ int send_on_key; AVStream *streams[MAX_STREAMS]; int feed_streams[MAX_STREAMS]; /* index of streams in the feed */ @@ -290,7 +290,7 @@ static int nb_connections; static int max_bandwidth; static int current_bandwidth; -static long cur_time; // Making this global saves on passing it around everywhere +static int64_t cur_time; // Making this global saves on passing it around everywhere static AVRandomState random_state; @@ -597,7 +597,7 @@ static int http_server(void) return -1; } while (ret <= 0); - cur_time = (long)(av_gettime()/1000); + cur_time = av_gettime() / 1000; if (need_to_start_children) { need_to_start_children = 0; @@ -762,6 +762,13 @@ static void close_connection(HTTPContext *c) if (c->stream && !c->post && c->stream->stream_type == STREAM_TYPE_LIVE) current_bandwidth -= c->stream->bandwidth; + + /* signal that there is no feed if we are the feeder socket */ + if (c->state == HTTPSTATE_RECEIVE_DATA && c->stream) { + c->stream->feed_opened = 0; + close(c->feed_fd); + } + av_freep(&c->pb_buffer); av_freep(&c->packet_buffer); av_free(c->buffer); @@ -863,6 +870,9 @@ static int handle_connection(HTTPContext *c) } if (http_send_data(c) < 0) return -1; + /* close connection if trailer sent */ + if (c->state == HTTPSTATE_SEND_DATA_TRAILER) + return -1; break; case HTTPSTATE_RECEIVE_DATA: /* no need to read if no events */ @@ -1165,7 +1175,7 @@ static int http_parse_request(HTTPContext *c) char *p; enum RedirType redir_type; char cmd[32]; - char info[1024], *filename; + char info[1024], filename[1024]; char url[1024], *q; char protocol[32]; char msg[1024]; @@ -1199,11 +1209,7 @@ static int http_parse_request(HTTPContext *c) http_log("New connection: %s %s\n", cmd, url); /* find the filename and the optional info string in the request */ - p = url; - if (*p == '/') - p++; - filename = p; - p = strchr(p, '?'); + p = strchr(url, '?'); if (p) { pstrcpy(info, sizeof(info), p); *p = '\0'; @@ -1211,6 +1217,8 @@ static int http_parse_request(HTTPContext *c) info[0] = '\0'; } + pstrcpy(filename, sizeof(filename)-1, url + ((*url == '/') ? 1 : 0)); + for (p = c->buffer; *p && *p != '\r' && *p != '\n'; ) { if (strncasecmp(p, "User-Agent:", 11) == 0) { useragent = p + 11; @@ -1238,12 +1246,16 @@ static int http_parse_request(HTTPContext *c) strcpy(filename + strlen(filename)-2, "m"); } else if (match_ext(filename, "rtsp")) { redir_type = REDIR_RTSP; - compute_real_filename(filename, sizeof(url) - 1); + compute_real_filename(filename, sizeof(filename) - 1); } else if (match_ext(filename, "sdp")) { redir_type = REDIR_SDP; - compute_real_filename(filename, sizeof(url) - 1); + compute_real_filename(filename, sizeof(filename) - 1); } + // "redirect" / request to index.html + if (!strlen(filename)) + pstrcpy(filename, sizeof(filename) - 1, "index.html"); + stream = first_stream; while (stream != NULL) { if (!strcmp(stream->filename, filename) && validate_acl(stream, c)) @@ -1287,6 +1299,12 @@ static int http_parse_request(HTTPContext *c) } } + /* If already streaming this feed, dont let start an another feeder */ + if (stream->feed_opened) { + snprintf(msg, sizeof(msg), "This feed is already being received."); + goto send_error; + } + if (c->post == 0 && stream->stream_type == STREAM_TYPE_LIVE) { current_bandwidth += stream->bandwidth; } @@ -1748,6 +1766,7 @@ static void compute_stats(HTTPContext *c) switch(st->codec->codec_type) { case CODEC_TYPE_AUDIO: type = "audio"; + snprintf(parameters, sizeof(parameters), "%d channel(s), %d Hz", st->codec->channels, st->codec->sample_rate); break; case CODEC_TYPE_VIDEO: type = "video"; @@ -1950,7 +1969,7 @@ static int open_input_stream(HTTPContext *c, const char *info) static int64_t get_server_clock(HTTPContext *c) { /* compute current pts value from system time */ - return (int64_t)(cur_time - c->start_time) * 1000LL; + return (cur_time - c->start_time) * 1000; } /* return the estimated time at which the current packet must be sent @@ -2023,7 +2042,8 @@ static int http_prepare_data(HTTPContext *c) c->fmt_ctx.pb.is_streamed = 1; av_set_parameters(&c->fmt_ctx, NULL); - av_write_header(&c->fmt_ctx); + if (av_write_header(&c->fmt_ctx) < 0) + return -1; len = url_close_dyn_buf(&c->fmt_ctx.pb, &c->pb_buffer); c->buffer_ptr = c->pb_buffer; @@ -2164,6 +2184,14 @@ static int http_prepare_data(HTTPContext *c) /* XXX: potential leak */ return -1; } + if (pkt.dts != AV_NOPTS_VALUE) + pkt.dts = av_rescale_q(pkt.dts, + c->fmt_in->streams[pkt.stream_index]->time_base, + ctx->streams[pkt.stream_index]->time_base); + if (pkt.pts != AV_NOPTS_VALUE) + pkt.pts = av_rescale_q(pkt.pts, + c->fmt_in->streams[pkt.stream_index]->time_base, + ctx->streams[pkt.stream_index]->time_base); if (av_write_frame(ctx, &pkt)) { c->state = HTTPSTATE_SEND_DATA_TRAILER; } @@ -3319,7 +3347,7 @@ static void extract_mpeg4_header(AVFormatContext *infile) if (p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x01 && p[3] == 0xb6) { size = p - pkt.data; - // av_hex_dump(pkt.data, size); + // av_hex_dump_log(infile, AV_LOG_DEBUG, pkt.data, size); st->codec->extradata = av_malloc(size); st->codec->extradata_size = size; memcpy(st->codec->extradata, pkt.data, size); @@ -3515,7 +3543,10 @@ static void build_feed_streams(void) s->streams[i] = st; } av_set_parameters(s, NULL); - av_write_header(s); + if (av_write_header(s) < 0) { + fprintf(stderr, "Container doesn't supports the required parameters\n"); + exit(1); + } /* XXX: need better api */ av_freep(&s->priv_data); url_fclose(&s->pb); @@ -4146,6 +4177,11 @@ static int parse_ffconfig(const char *filename) video_enc.mb_decision = FF_MB_DECISION_BITS; //FIXME remove video_enc.flags |= CODEC_FLAG_4MV; } + } else if (!strcasecmp(cmd, "VideoTag")) { + get_arg(arg, sizeof(arg), &p); + if ((strlen(arg) == 4) && stream) { + video_enc.codec_tag = ff_get_fourcc(arg); + } } else if (!strcasecmp(cmd, "BitExact")) { if (stream) { video_enc.flags |= CODEC_FLAG_BITEXACT;