Merge branch 'simple_http_server_api_implementation' of http://git.klaxa.eu/git/ffmpeg
Reviewed-by: Nicolas George <george@nsup.org> Merged-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
commit
55ea31ab89
@ -18,6 +18,7 @@ EXAMPLES= avio_list_dir \
|
||||
extract_mvs \
|
||||
filtering_video \
|
||||
filtering_audio \
|
||||
http_multiclient \
|
||||
metadata \
|
||||
muxing \
|
||||
remuxing \
|
||||
|
155
doc/examples/http_multiclient.c
Normal file
155
doc/examples/http_multiclient.c
Normal file
@ -0,0 +1,155 @@
|
||||
/*
|
||||
* Copyright (c) 2015 Stephan Holljes
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* libavformat multi-client network API usage example.
|
||||
*
|
||||
* @example http_multiclient.c
|
||||
* This example will serve a file without decoding or demuxing it over http.
|
||||
* Multiple clients can connect and will receive the same file.
|
||||
*/
|
||||
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/opt.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void process_client(AVIOContext *client, const char *in_uri)
|
||||
{
|
||||
AVIOContext *input = NULL;
|
||||
uint8_t buf[1024];
|
||||
int ret, n, reply_code;
|
||||
char *resource = NULL;
|
||||
while ((ret = avio_handshake(client)) > 0) {
|
||||
av_opt_get(client, "resource", AV_OPT_SEARCH_CHILDREN, &resource);
|
||||
// check for strlen(resource) is necessary, because av_opt_get()
|
||||
// may return empty string.
|
||||
if (resource && strlen(resource))
|
||||
break;
|
||||
}
|
||||
if (ret < 0)
|
||||
goto end;
|
||||
av_log(client, AV_LOG_TRACE, "resource=%p\n", resource);
|
||||
if (resource && resource[0] == '/' && !strcmp((resource + 1), in_uri)) {
|
||||
reply_code = 200;
|
||||
} else {
|
||||
reply_code = AVERROR_HTTP_NOT_FOUND;
|
||||
}
|
||||
if ((ret = av_opt_set_int(client, "reply_code", reply_code, AV_OPT_SEARCH_CHILDREN)) < 0) {
|
||||
av_log(client, AV_LOG_ERROR, "Failed to set reply_code: %s.\n", av_err2str(ret));
|
||||
goto end;
|
||||
}
|
||||
av_log(client, AV_LOG_TRACE, "Set reply code to %d\n", reply_code);
|
||||
|
||||
while ((ret = avio_handshake(client)) > 0);
|
||||
|
||||
if (ret < 0)
|
||||
goto end;
|
||||
|
||||
fprintf(stderr, "Handshake performed.\n");
|
||||
if (reply_code != 200)
|
||||
goto end;
|
||||
fprintf(stderr, "Opening input file.\n");
|
||||
if ((ret = avio_open2(&input, in_uri, AVIO_FLAG_READ, NULL, NULL)) < 0) {
|
||||
av_log(input, AV_LOG_ERROR, "Failed to open input: %s: %s.\n", in_uri,
|
||||
av_err2str(ret));
|
||||
goto end;
|
||||
}
|
||||
for(;;) {
|
||||
n = avio_read(input, buf, sizeof(buf));
|
||||
if (n < 0) {
|
||||
if (n == AVERROR_EOF)
|
||||
break;
|
||||
av_log(input, AV_LOG_ERROR, "Error reading from input: %s.\n",
|
||||
av_err2str(n));
|
||||
break;
|
||||
}
|
||||
avio_write(client, buf, n);
|
||||
avio_flush(client);
|
||||
}
|
||||
end:
|
||||
fprintf(stderr, "Flushing client\n");
|
||||
avio_flush(client);
|
||||
fprintf(stderr, "Closing client\n");
|
||||
avio_close(client);
|
||||
fprintf(stderr, "Closing input\n");
|
||||
avio_close(input);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
av_log_set_level(AV_LOG_TRACE);
|
||||
AVDictionary *options = NULL;
|
||||
AVIOContext *client = NULL, *server = NULL;
|
||||
const char *in_uri, *out_uri;
|
||||
int ret, pid;
|
||||
if (argc < 3) {
|
||||
printf("usage: %s input http://hostname[:port]\n"
|
||||
"API example program to serve http to multiple clients.\n"
|
||||
"\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
in_uri = argv[1];
|
||||
out_uri = argv[2];
|
||||
|
||||
av_register_all();
|
||||
avformat_network_init();
|
||||
|
||||
if ((ret = av_dict_set(&options, "listen", "2", 0)) < 0) {
|
||||
fprintf(stderr, "Failed to set listen mode for server: %s\n", av_err2str(ret));
|
||||
return ret;
|
||||
}
|
||||
if ((ret = avio_open2(&server, out_uri, AVIO_FLAG_WRITE, NULL, &options)) < 0) {
|
||||
fprintf(stderr, "Failed to open server: %s\n", av_err2str(ret));
|
||||
return ret;
|
||||
}
|
||||
fprintf(stderr, "Entering main loop.\n");
|
||||
for(;;) {
|
||||
if ((ret = avio_accept(server, &client)) < 0)
|
||||
goto end;
|
||||
fprintf(stderr, "Accepted client, forking process.\n");
|
||||
// XXX: Since we don't reap our children and don't ignore signals
|
||||
// this produces zombie processes.
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
perror("Fork failed");
|
||||
ret = AVERROR(errno);
|
||||
goto end;
|
||||
}
|
||||
if (pid == 0) {
|
||||
fprintf(stderr, "In child.\n");
|
||||
process_client(client, in_uri);
|
||||
avio_close(server);
|
||||
exit(0);
|
||||
}
|
||||
if (pid > 0)
|
||||
avio_close(client);
|
||||
}
|
||||
end:
|
||||
avio_close(server);
|
||||
if (ret < 0 && ret != AVERROR_EOF) {
|
||||
fprintf(stderr, "Some errors occured: %s\n", av_err2str(ret));
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -304,6 +304,8 @@ autodetection in the future.
|
||||
If set to 1 enables experimental HTTP server. This can be used to send data when
|
||||
used as an output option, or read data from a client with HTTP POST when used as
|
||||
an input option.
|
||||
If set to 2 enables experimental mutli-client HTTP server. This is not yet implemented
|
||||
in ffmpeg.c or ffserver.c and thus must not be used as a command line option.
|
||||
@example
|
||||
# Server side (sending):
|
||||
ffmpeg -i somefile.ogg -c copy -listen 1 -f ogg http://@var{server}:@var{port}
|
||||
|
@ -211,6 +211,26 @@ int ffurl_connect(URLContext *uc, AVDictionary **options)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ffurl_accept(URLContext *s, URLContext **c)
|
||||
{
|
||||
av_assert0(!*c);
|
||||
if (s->prot->url_accept)
|
||||
return s->prot->url_accept(s, c);
|
||||
return AVERROR(EBADF);
|
||||
}
|
||||
|
||||
int ffurl_handshake(URLContext *c)
|
||||
{
|
||||
int ret;
|
||||
if (c->prot->url_handshake) {
|
||||
ret = c->prot->url_handshake(c);
|
||||
if (ret)
|
||||
return ret;
|
||||
}
|
||||
c->is_connected = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define URL_SCHEME_CHARS \
|
||||
"abcdefghijklmnopqrstuvwxyz" \
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
|
||||
|
@ -648,4 +648,33 @@ struct AVBPrint;
|
||||
*/
|
||||
int avio_read_to_bprint(AVIOContext *h, struct AVBPrint *pb, size_t max_size);
|
||||
|
||||
/**
|
||||
* Accept and allocate a client context on a server context.
|
||||
* @param s the server context
|
||||
* @param c the client context, must be unallocated
|
||||
* @return >= 0 on success or a negative value corresponding
|
||||
* to an AVERROR on failure
|
||||
*/
|
||||
int avio_accept(AVIOContext *s, AVIOContext **c);
|
||||
|
||||
/**
|
||||
* Perform one step of the protocol handshake to accept a new client.
|
||||
* This function must be called on a client returned by avio_accept() before
|
||||
* using it as a read/write context.
|
||||
* It is separate from avio_accept() because it may block.
|
||||
* A step of the handshake is defined by places where the application may
|
||||
* decide to change the proceedings.
|
||||
* For example, on a protocol with a request header and a reply header, each
|
||||
* one can constitute a step because the application may use the parameters
|
||||
* from the request to change parameters in the reply; or each individual
|
||||
* chunk of the request can constitute a step.
|
||||
* If the handshake is already finished, avio_handshake() does nothing and
|
||||
* returns 0 immediately.
|
||||
*
|
||||
* @param c the client context to perform the handshake on
|
||||
* @return 0 on a complete and successful handshake
|
||||
* > 0 if the handshake progressed, but is not complete
|
||||
* < 0 for an AVERROR code
|
||||
*/
|
||||
int avio_handshake(AVIOContext *c);
|
||||
#endif /* AVFORMAT_AVIO_H */
|
||||
|
@ -1021,6 +1021,23 @@ int avio_read_to_bprint(AVIOContext *h, AVBPrint *pb, size_t max_size)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int avio_accept(AVIOContext *s, AVIOContext **c)
|
||||
{
|
||||
int ret;
|
||||
URLContext *sc = s->opaque;
|
||||
URLContext *cc = NULL;
|
||||
ret = ffurl_accept(sc, &cc);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
return ffio_fdopen(c, cc);
|
||||
}
|
||||
|
||||
int avio_handshake(AVIOContext *c)
|
||||
{
|
||||
URLContext *cc = c->opaque;
|
||||
return ffurl_handshake(cc);
|
||||
}
|
||||
|
||||
/* output in a dynamic buffer */
|
||||
|
||||
typedef struct DynBuffer {
|
||||
|
@ -25,6 +25,7 @@
|
||||
#include <zlib.h>
|
||||
#endif /* CONFIG_ZLIB */
|
||||
|
||||
#include "libavutil/avassert.h"
|
||||
#include "libavutil/avstring.h"
|
||||
#include "libavutil/opt.h"
|
||||
|
||||
@ -44,6 +45,14 @@
|
||||
* path names). */
|
||||
#define BUFFER_SIZE MAX_URL_SIZE
|
||||
#define MAX_REDIRECTS 8
|
||||
#define HTTP_SINGLE 1
|
||||
#define HTTP_MUTLI 2
|
||||
typedef enum {
|
||||
LOWER_PROTO,
|
||||
READ_HEADERS,
|
||||
WRITE_REPLY_HEADERS,
|
||||
FINISH
|
||||
}HandshakeState;
|
||||
|
||||
typedef struct HTTPContext {
|
||||
const AVClass *class;
|
||||
@ -97,6 +106,11 @@ typedef struct HTTPContext {
|
||||
char *method;
|
||||
int reconnect;
|
||||
int listen;
|
||||
char *resource;
|
||||
int reply_code;
|
||||
int is_multi_client;
|
||||
HandshakeState handshake_step;
|
||||
int is_connected_server;
|
||||
} HTTPContext;
|
||||
|
||||
#define OFFSET(x) offsetof(HTTPContext, x)
|
||||
@ -128,7 +142,9 @@ static const AVOption options[] = {
|
||||
{ "end_offset", "try to limit the request to bytes preceding this offset", OFFSET(end_off), AV_OPT_TYPE_INT64, { .i64 = 0 }, 0, INT64_MAX, D },
|
||||
{ "method", "Override the HTTP method or set the expected HTTP method from a client", OFFSET(method), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, D | E },
|
||||
{ "reconnect", "auto reconnect after disconnect before EOF", OFFSET(reconnect), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D },
|
||||
{ "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, D | E },
|
||||
{ "listen", "listen on HTTP", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, D | E },
|
||||
{ "resource", "The resource requested by a client", OFFSET(resource), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, E },
|
||||
{ "reply_code", "The http status code to return to a client", OFFSET(reply_code), AV_OPT_TYPE_INT, { .i64 = 200}, INT_MIN, 599, E},
|
||||
{ NULL }
|
||||
};
|
||||
|
||||
@ -299,51 +315,143 @@ int ff_http_averror(int status_code, int default_averror)
|
||||
return default_averror;
|
||||
}
|
||||
|
||||
static int http_write_reply(URLContext* h, int status_code)
|
||||
{
|
||||
int ret, body = 0, reply_code, message_len;
|
||||
const char *reply_text, *content_type;
|
||||
HTTPContext *s = h->priv_data;
|
||||
char message[BUFFER_SIZE];
|
||||
content_type = "text/plain";
|
||||
|
||||
if (status_code < 0)
|
||||
body = 1;
|
||||
switch (status_code) {
|
||||
case AVERROR_HTTP_BAD_REQUEST:
|
||||
case 400:
|
||||
reply_code = 400;
|
||||
reply_text = "Bad Request";
|
||||
break;
|
||||
case AVERROR_HTTP_FORBIDDEN:
|
||||
case 403:
|
||||
reply_code = 403;
|
||||
reply_text = "Forbidden";
|
||||
break;
|
||||
case AVERROR_HTTP_NOT_FOUND:
|
||||
case 404:
|
||||
reply_code = 404;
|
||||
reply_text = "Not Found";
|
||||
break;
|
||||
case 200:
|
||||
reply_code = 200;
|
||||
reply_text = "OK";
|
||||
content_type = "application/octet-stream";
|
||||
break;
|
||||
case AVERROR_HTTP_SERVER_ERROR:
|
||||
case 500:
|
||||
reply_code = 500;
|
||||
reply_text = "Internal server error";
|
||||
break;
|
||||
default:
|
||||
return AVERROR(EINVAL);
|
||||
}
|
||||
if (body) {
|
||||
s->chunked_post = 0;
|
||||
message_len = snprintf(message, sizeof(message),
|
||||
"HTTP/1.1 %03d %s\r\n"
|
||||
"Content-Type: %s\r\n"
|
||||
"Content-Length: %zu\r\n"
|
||||
"\r\n"
|
||||
"%03d %s\r\n",
|
||||
reply_code,
|
||||
reply_text,
|
||||
content_type,
|
||||
strlen(reply_text) + 6, // 3 digit status code + space + \r\n
|
||||
reply_code,
|
||||
reply_text);
|
||||
} else {
|
||||
s->chunked_post = 1;
|
||||
message_len = snprintf(message, sizeof(message),
|
||||
"HTTP/1.1 %03d %s\r\n"
|
||||
"Content-Type: %s\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n",
|
||||
reply_code,
|
||||
reply_text,
|
||||
content_type);
|
||||
}
|
||||
av_log(h, AV_LOG_TRACE, "HTTP reply header: \n%s----\n", message);
|
||||
if ((ret = ffurl_write(s->hd, message, message_len)) < 0)
|
||||
return ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void handle_http_errors(URLContext *h, int error)
|
||||
{
|
||||
static const char bad_request[] = "HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n400 Bad Request\r\n";
|
||||
static const char internal_server_error[] = "HTTP/1.1 500 Internal server error\r\nContent-Type: text/plain\r\n\r\n500 Internal server error\r\n";
|
||||
HTTPContext *s = h->priv_data;
|
||||
if (h->is_connected) {
|
||||
switch(error) {
|
||||
case AVERROR_HTTP_BAD_REQUEST:
|
||||
ffurl_write(s->hd, bad_request, strlen(bad_request));
|
||||
break;
|
||||
default:
|
||||
av_log(h, AV_LOG_ERROR, "Unhandled HTTP error.\n");
|
||||
ffurl_write(s->hd, internal_server_error, strlen(internal_server_error));
|
||||
av_assert0(error < 0);
|
||||
http_write_reply(h, error);
|
||||
}
|
||||
|
||||
static int http_handshake(URLContext *c)
|
||||
{
|
||||
int ret, err, new_location;
|
||||
HTTPContext *ch = c->priv_data;
|
||||
URLContext *cl = ch->hd;
|
||||
switch (ch->handshake_step) {
|
||||
case LOWER_PROTO:
|
||||
av_log(c, AV_LOG_TRACE, "Lower protocol\n");
|
||||
if ((ret = ffurl_handshake(cl) > 0))
|
||||
return 2 + ret;
|
||||
if ((ret < 0))
|
||||
return ret;
|
||||
ch->handshake_step = READ_HEADERS;
|
||||
ch->is_connected_server = 1;
|
||||
return 2;
|
||||
case READ_HEADERS:
|
||||
av_log(c, AV_LOG_TRACE, "Read headers\n");
|
||||
if ((err = http_read_header(c, &new_location)) < 0) {
|
||||
handle_http_errors(c, err);
|
||||
return err;
|
||||
}
|
||||
ch->handshake_step = WRITE_REPLY_HEADERS;
|
||||
return 1;
|
||||
case WRITE_REPLY_HEADERS:
|
||||
av_log(c, AV_LOG_TRACE, "Reply code: %d\n", ch->reply_code);
|
||||
if ((err = http_write_reply(c, ch->reply_code)) < 0)
|
||||
return err;
|
||||
ch->handshake_step = FINISH;
|
||||
return 1;
|
||||
case FINISH:
|
||||
return 0;
|
||||
}
|
||||
// this should never be reached.
|
||||
return AVERROR(EINVAL);
|
||||
}
|
||||
|
||||
static int http_listen(URLContext *h, const char *uri, int flags,
|
||||
AVDictionary **options) {
|
||||
HTTPContext *s = h->priv_data;
|
||||
int ret;
|
||||
static const char header[] = "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nTransfer-Encoding: chunked\r\n\r\n";
|
||||
char hostname[1024], proto[10];
|
||||
char lower_url[100];
|
||||
const char *lower_proto = "tcp";
|
||||
int port, new_location;
|
||||
s->chunked_post = 1;
|
||||
int port;
|
||||
av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), &port,
|
||||
NULL, 0, uri);
|
||||
if (!strcmp(proto, "https"))
|
||||
lower_proto = "tls";
|
||||
ff_url_join(lower_url, sizeof(lower_url), lower_proto, NULL, hostname, port,
|
||||
NULL);
|
||||
av_dict_set(options, "listen", "1", 0);
|
||||
if ((ret = av_dict_set_int(options, "listen", s->listen, 0)) < 0)
|
||||
goto fail;
|
||||
if ((ret = ffurl_open(&s->hd, lower_url, AVIO_FLAG_READ_WRITE,
|
||||
&h->interrupt_callback, options)) < 0)
|
||||
goto fail;
|
||||
if ((ret = http_read_header(h, &new_location)) < 0)
|
||||
goto fail;
|
||||
if ((ret = ffurl_write(s->hd, header, strlen(header))) < 0)
|
||||
goto fail;
|
||||
return 0;
|
||||
|
||||
s->handshake_step = LOWER_PROTO;
|
||||
if (s->listen == HTTP_SINGLE) { /* single client */
|
||||
s->reply_code = 200;
|
||||
while ((ret = http_handshake(h)) > 0);
|
||||
}
|
||||
fail:
|
||||
handle_http_errors(h, ret);
|
||||
av_dict_free(&s->chained_options);
|
||||
return ret;
|
||||
}
|
||||
@ -389,6 +497,26 @@ static int http_open(URLContext *h, const char *uri, int flags,
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int http_accept(URLContext *s, URLContext **c)
|
||||
{
|
||||
int ret;
|
||||
HTTPContext *sc = s->priv_data;
|
||||
HTTPContext *cc;
|
||||
URLContext *sl = sc->hd;
|
||||
URLContext *cl = NULL;
|
||||
|
||||
av_assert0(sc->listen);
|
||||
if ((ret = ffurl_alloc(c, s->filename, s->flags, &sl->interrupt_callback)) < 0)
|
||||
goto fail;
|
||||
cc = (*c)->priv_data;
|
||||
if ((ret = ffurl_accept(sl, &cl)) < 0)
|
||||
goto fail;
|
||||
cc->hd = cl;
|
||||
cc->is_multi_client = 1;
|
||||
fail:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int http_getc(HTTPContext *s)
|
||||
{
|
||||
int len;
|
||||
@ -583,7 +711,7 @@ static int process_line(URLContext *h, char *line, int line_count,
|
||||
|
||||
p = line;
|
||||
if (line_count == 0) {
|
||||
if (s->listen) {
|
||||
if (s->is_connected_server) {
|
||||
// HTTP method
|
||||
method = p;
|
||||
while (!av_isspace(*p))
|
||||
@ -604,6 +732,8 @@ static int process_line(URLContext *h, char *line, int line_count,
|
||||
"(%s autodetected %s received)\n", auto_method, method);
|
||||
return ff_http_averror(400, AVERROR(EIO));
|
||||
}
|
||||
if (!(s->method = av_strdup(method)))
|
||||
return AVERROR(ENOMEM);
|
||||
}
|
||||
|
||||
// HTTP resource
|
||||
@ -614,6 +744,8 @@ static int process_line(URLContext *h, char *line, int line_count,
|
||||
p++;
|
||||
*(p++) = '\0';
|
||||
av_log(h, AV_LOG_TRACE, "Requested resource: %s\n", resource);
|
||||
if (!(s->resource = av_strdup(resource)))
|
||||
return AVERROR(ENOMEM);
|
||||
|
||||
// HTTP version
|
||||
while (av_isspace(*p))
|
||||
@ -1353,6 +1485,8 @@ HTTP_CLASS(http);
|
||||
URLProtocol ff_http_protocol = {
|
||||
.name = "http",
|
||||
.url_open2 = http_open,
|
||||
.url_accept = http_accept,
|
||||
.url_handshake = http_handshake,
|
||||
.url_read = http_read,
|
||||
.url_write = http_write,
|
||||
.url_seek = http_seek,
|
||||
|
@ -187,12 +187,11 @@ int ff_socket(int af, int type, int proto)
|
||||
return fd;
|
||||
}
|
||||
|
||||
int ff_listen_bind(int fd, const struct sockaddr *addr,
|
||||
socklen_t addrlen, int timeout, URLContext *h)
|
||||
int ff_listen(int fd, const struct sockaddr *addr,
|
||||
socklen_t addrlen)
|
||||
{
|
||||
int ret;
|
||||
int reuse = 1;
|
||||
struct pollfd lp = { fd, POLLIN, 0 };
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse))) {
|
||||
av_log(NULL, AV_LOG_WARNING, "setsockopt(SO_REUSEADDR) failed\n");
|
||||
}
|
||||
@ -203,6 +202,13 @@ int ff_listen_bind(int fd, const struct sockaddr *addr,
|
||||
ret = listen(fd, 1);
|
||||
if (ret)
|
||||
return ff_neterrno();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ff_accept(int fd, int timeout, URLContext *h)
|
||||
{
|
||||
int ret;
|
||||
struct pollfd lp = { fd, POLLIN, 0 };
|
||||
|
||||
ret = ff_poll_interrupt(&lp, 1, timeout, &h->interrupt_callback);
|
||||
if (ret < 0)
|
||||
@ -211,15 +217,24 @@ int ff_listen_bind(int fd, const struct sockaddr *addr,
|
||||
ret = accept(fd, NULL, NULL);
|
||||
if (ret < 0)
|
||||
return ff_neterrno();
|
||||
|
||||
closesocket(fd);
|
||||
|
||||
if (ff_socket_nonblock(ret, 1) < 0)
|
||||
av_log(NULL, AV_LOG_DEBUG, "ff_socket_nonblock failed\n");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ff_listen_bind(int fd, const struct sockaddr *addr,
|
||||
socklen_t addrlen, int timeout, URLContext *h)
|
||||
{
|
||||
int ret;
|
||||
if ((ret = ff_listen(fd, addr, addrlen)) < 0)
|
||||
return ret;
|
||||
if ((ret = ff_accept(fd, timeout, h)) < 0)
|
||||
return ret;
|
||||
closesocket(fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ff_listen_connect(int fd, const struct sockaddr *addr,
|
||||
socklen_t addrlen, int timeout, URLContext *h,
|
||||
int will_try_next)
|
||||
|
@ -254,6 +254,26 @@ int ff_listen_bind(int fd, const struct sockaddr *addr,
|
||||
socklen_t addrlen, int timeout,
|
||||
URLContext *h);
|
||||
|
||||
/**
|
||||
* Bind to a file descriptor to an address without accepting connections.
|
||||
* @param fd First argument of bind().
|
||||
* @param addr Second argument of bind().
|
||||
* @param addrlen Third argument of bind().
|
||||
* @return 0 on success or an AVERROR on failure.
|
||||
*/
|
||||
int ff_listen(int fd, const struct sockaddr *addr, socklen_t addrlen);
|
||||
|
||||
/**
|
||||
* Poll for a single connection on the passed file descriptor.
|
||||
* @param fd The listening socket file descriptor.
|
||||
* @param timeout Polling timeout in milliseconds.
|
||||
* @param h URLContext providing interrupt check
|
||||
* callback and logging context.
|
||||
* @return A non-blocking file descriptor on success
|
||||
* or an AVERROR on failure.
|
||||
*/
|
||||
int ff_accept(int fd, int timeout, URLContext *h);
|
||||
|
||||
/**
|
||||
* Connect to a file descriptor and poll for result.
|
||||
*
|
||||
|
@ -19,6 +19,7 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include "avformat.h"
|
||||
#include "libavutil/avassert.h"
|
||||
#include "libavutil/parseutils.h"
|
||||
#include "libavutil/opt.h"
|
||||
#include "libavutil/time.h"
|
||||
@ -44,7 +45,7 @@ typedef struct TCPContext {
|
||||
#define D AV_OPT_FLAG_DECODING_PARAM
|
||||
#define E AV_OPT_FLAG_ENCODING_PARAM
|
||||
static const AVOption options[] = {
|
||||
{ "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, .flags = D|E },
|
||||
{ "listen", "Listen for incoming connections", OFFSET(listen), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, .flags = D|E },
|
||||
{ "timeout", "set timeout (in microseconds) of socket I/O operations", OFFSET(rw_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
|
||||
{ "listen_timeout", "Connection awaiting timeout (in milliseconds)", OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, .flags = D|E },
|
||||
{ NULL }
|
||||
@ -125,12 +126,17 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (s->listen) {
|
||||
if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
|
||||
s->listen_timeout, h)) < 0) {
|
||||
if (s->listen == 2) {
|
||||
// multi-client
|
||||
if ((ret = ff_listen(fd, cur_ai->ai_addr, cur_ai->ai_addrlen)) < 0)
|
||||
goto fail1;
|
||||
} else if (s->listen == 1) {
|
||||
// single client
|
||||
if ((fd = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
|
||||
s->listen_timeout, h)) < 0) {
|
||||
ret = fd;
|
||||
goto fail1;
|
||||
}
|
||||
fd = ret;
|
||||
} else {
|
||||
if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
|
||||
s->open_timeout / 1000, h, !!cur_ai->ai_next)) < 0) {
|
||||
@ -163,6 +169,22 @@ static int tcp_open(URLContext *h, const char *uri, int flags)
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int tcp_accept(URLContext *s, URLContext **c)
|
||||
{
|
||||
TCPContext *sc = s->priv_data;
|
||||
TCPContext *cc;
|
||||
int ret;
|
||||
av_assert0(sc->listen);
|
||||
if ((ret = ffurl_alloc(c, s->filename, s->flags, &s->interrupt_callback)) < 0)
|
||||
return ret;
|
||||
cc = (*c)->priv_data;
|
||||
ret = ff_accept(sc->fd, sc->listen_timeout, s);
|
||||
if (ret < 0)
|
||||
return ff_neterrno();
|
||||
cc->fd = ret;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tcp_read(URLContext *h, uint8_t *buf, int size)
|
||||
{
|
||||
TCPContext *s = h->priv_data;
|
||||
@ -223,6 +245,7 @@ static int tcp_get_file_handle(URLContext *h)
|
||||
URLProtocol ff_tcp_protocol = {
|
||||
.name = "tcp",
|
||||
.url_open = tcp_open,
|
||||
.url_accept = tcp_accept,
|
||||
.url_read = tcp_read,
|
||||
.url_write = tcp_write,
|
||||
.url_close = tcp_close,
|
||||
|
@ -58,6 +58,8 @@ typedef struct URLProtocol {
|
||||
* for those nested protocols.
|
||||
*/
|
||||
int (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options);
|
||||
int (*url_accept)(URLContext *s, URLContext **c);
|
||||
int (*url_handshake)(URLContext *c);
|
||||
|
||||
/**
|
||||
* Read data from the protocol.
|
||||
@ -139,6 +141,29 @@ int ffurl_connect(URLContext *uc, AVDictionary **options);
|
||||
int ffurl_open(URLContext **puc, const char *filename, int flags,
|
||||
const AVIOInterruptCB *int_cb, AVDictionary **options);
|
||||
|
||||
/**
|
||||
* Accept an URLContext c on an URLContext s
|
||||
*
|
||||
* @param s server context
|
||||
* @param c client context, must be unallocated.
|
||||
* @return >= 0 on success, ff_neterrno() on failure.
|
||||
*/
|
||||
int ffurl_accept(URLContext *s, URLContext **c);
|
||||
|
||||
/**
|
||||
* Perform one step of the protocol handshake to accept a new client.
|
||||
* See avio_handshake() for details.
|
||||
* Implementations should try to return decreasing values.
|
||||
* If the protocol uses an underlying protocol, the underlying handshake is
|
||||
* usually the first step, and the return value can be:
|
||||
* (largest value for this protocol) + (return value from other protocol)
|
||||
*
|
||||
* @param c the client context
|
||||
* @return >= 0 on success or a negative value corresponding
|
||||
* to an AVERROR code on failure
|
||||
*/
|
||||
int ffurl_handshake(URLContext *c);
|
||||
|
||||
/**
|
||||
* Read up to size bytes from the resource accessed by h, and store
|
||||
* the read bytes in buf.
|
||||
|
Loading…
x
Reference in New Issue
Block a user