[FFmpeg-devel] [PATCH 8/8] doc/example: Add http multi-client example code

Nicolas George george at nsup.org
Tue Jul 21 12:29:54 CEST 2015


Le tridi 3 thermidor, an CCXXIII, Stephan Holljes a écrit :
> Signed-off-by: Stephan Holljes <klaxa1337 at googlemail.com>
> ---
>  doc/examples/Makefile           |   1 +
>  doc/examples/http_multiclient.c | 139 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 140 insertions(+)
>  create mode 100644 doc/examples/http_multiclient.c
> 
> Changes since last version:
>   - Use handshake API properly
>   - Use perror() to print fork()'s error message. 
> 
> diff --git a/doc/examples/Makefile b/doc/examples/Makefile
> index 9699f11..8c9501b 100644
> --- a/doc/examples/Makefile
> +++ b/doc/examples/Makefile
> @@ -18,6 +18,7 @@ EXAMPLES=       avio_list_dir                      \
>                  extract_mvs                        \
>                  filtering_video                    \
>                  filtering_audio                    \
> +                http_multiclient                   \
>                  metadata                           \
>                  muxing                             \
>                  remuxing                           \
> diff --git a/doc/examples/http_multiclient.c b/doc/examples/http_multiclient.c
> new file mode 100644
> index 0000000..a2293b8
> --- /dev/null
> +++ b/doc/examples/http_multiclient.c
> @@ -0,0 +1,139 @@
> +/*
> + * copyright (c) 2015 Stephan Holljes
> + *
> + * This file is part of FFmpeg.
> + *

> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.

Not noticed it before: most examples have a more permissive licence (MIT).

> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +/**
> + * @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 <unistd.h>
> +#include <libavutil/opt.h>

Nit: order is strange.

> +
> +void process_client(AVIOContext *client, const char* in_uri)

Nit: "char *in_uri".

(There is actually a good reason for that: "char* a, b;" means "*a and b are
chars, i.e. a is pointer-to-char, but due to the spacing, it seems to mean
"a and b are pointers-to-char".)

> +{
> +    AVIOContext *input = NULL;
> +    uint8_t buf[1024];
> +    int ret, n, http_code;
> +    char *resource;
> +    for (;;) {

> +        if ((ret = avio_handshake(client)) < 0)
> +            goto end;

I would find it more logical if "if (ret == 0) break;" were immediately
here.

> +        av_opt_get(client, "resource", AV_OPT_SEARCH_CHILDREN, &resource);

Need to check that resource is not NULL: we could be inside the TLS
handshake (not yet implemented of course). And need to do it only once:
someone may decide to change http_handshake() to return for each header
line.

It would probably be simpler to make it two loops:

    /* Loop until we have the request resource */
    while ((ret = avio_handshake(client)) > 0) {
	av_opt_get(client, "resource", AV_OPT_SEARCH_CHILDREN, &resource);
	if (resource)
	    break;
    }
    [ decide the reply code ]
    /* Loop for the end of the handshake. */
    while ((ret = avio_handshake(client)) > 0);

> +        resource++;
> +        av_log(client, AV_LOG_TRACE, "resource=%s\n", resource);
> +        if (strcmp(resource, in_uri)) {
> +            http_code = 404;
> +        } else {
> +            http_code = 200;
> +        }
> +        av_opt_set_int(client, "http_code", http_code, AV_OPT_SEARCH_CHILDREN);
> +        if (ret == 0)
> +            break;
> +    }
> +    fprintf(stderr, "Handshake performed.\n");
> +    if (http_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\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));

> +            ret = n;

Unused assignment.

> +            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)
> +        goto end;
> +    if ((ret = avio_open2(&server, out_uri, AVIO_FLAG_WRITE, NULL, &options)) < 0)
> +        goto end;

I really think you should not try to factor error reporting: just print the
error right there and exit.

> +    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: ");
> +        fprintf(stderr, "%s\n", av_err2str(ret));
> +        return 1;
> +    }
> +    return 0;
> +}

Regards,

-- 
  Nicolas George
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20150721/3088e8fa/attachment.sig>


More information about the ffmpeg-devel mailing list