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

Stephan Holljes klaxa1337 at googlemail.com
Tue Jul 28 04:49:37 CEST 2015


On Sat, Jul 25, 2015 at 5:23 PM, Nicolas George <george at nsup.org> wrote:
> Le septidi 7 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 | 163 ++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 164 insertions(+)
>>  create mode 100644 doc/examples/http_multiclient.c
>> Changes since last version:
>>   - Changed license
>>   - Use simpler code to communicate with the library.
>>
>> 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..84fc739
>> --- /dev/null
>> +++ b/doc/examples/http_multiclient.c
>> @@ -0,0 +1,163 @@
>> +/*
>> + * 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_log(client, AV_LOG_TRACE, "checking for resource\n");
>> +        av_opt_get(client, "resource", AV_OPT_SEARCH_CHILDREN, &resource);
>> +        if (resource && strlen(resource)) {
>> +            resource++;
>> +            av_log(client, AV_LOG_TRACE, "got resource: %s (%lu)\n", resource, strlen(resource));
>> +            break;
>> +        }
>> +    }
>> +
>> +    if (ret < 0)
>> +        goto end;
>> +
>
>> +    if (strcmp(resource, in_uri)) {
>
> Nit: at this point, you can not be sure that resource is not null, unless
> you trust the library to never set it to the empty string.

After some research and looking at av_opt_get(), if I understand it
correctly, it may return empty string if the option to retrieve is
NULL. Therefore I think it is necessary to check for strlen(resource).

>
> And, nit: you accept unintended resources, for example "invalid" for
> "/nvalid".
>
> To fix both issues at once, and IMHO make the code a little bit simpler, you
> can move the handling outside the loop: in the loop, just
> "if (resource) break;", and after the loop, to test the resource, something
> like that:
>
>     if (!(resource && resource[0] == '/' && !strcmp(resource, in_uri)))
>

Done like that now.

>> +        reply_code = AVERROR_HTTP_NOT_FOUND;
>> +    } else {
>> +        reply_code = 200;
>> +    }
>> +    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);
>> +
>
>> +    if ((ret = av_opt_set_int(client, "handshake_finish", 1, AV_OPT_SEARCH_CHILDREN)) < 0) {
>
> As stated earlier, this should be the default.
>
>> +        av_log(client, AV_LOG_ERROR, "Failed to set handshake_finish: %s.\n", av_err2str(ret));
>> +        goto end;
>> +    }
>> +
>
>> +    while ((ret = avio_handshake(client)) > 0);
>
> There is a small API issue here: if for some reason resource never gets set
> (for example someone changes the code to use another protocol than HTTP
> where it is optional), then the code is calling avio_handshake() extraneous
> times.
>
> For that reason, I think it should be ok to call avio_handshake() when the
> handshake is already finished. But it should be stated in the doxy:
>
>  * If the handshake is already finished, avio_handshake() does nothing and
>  * returns 0 immediately.
>
> This is also the reason I advise to have a last step constant for when the
> handshake is finished.

Added.

>
>> +
>> +    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;
>> +}
>
> Regards,
>
> --
>   Nicolas George

Regards,
Stephan


More information about the ffmpeg-devel mailing list