音视频

ffmpeg_sample解读_http_multiclient

2020-10-30  本文已影响0人  刘佳阔

title: ffmpeg_sample解读_http_multiclient
date: 2020-10-28 10:15:02
tags: [读书笔记]
typora-copy-images-to: ./imgs
typora-root-url: ./imgs


总结

在内部创建了一个客户端服务端模式,然后创建两个进程,进行握手.读取数据并写回.

这个我没太看懂.

graph TB
afni[avformat_network_init]
-->ao[avio_open2]
-->aa{avio_accept>0?}
-->|no|release[release]
aa-->|yes|fork[fork]
-->pid{pid ==0?}
-->|yes| pc[process_client]
pid-->|no|avco[avio_close]
avco-->aa
pc-->aih[avio_handshake]
-->aog[av_opt_get]
-->aosi[av_opt_set_int]
-->avo[avio_open2]
-->avr[avio_read]
-->aw[avio_write]
-->af[avio_fluh]
-->aa
image-20201029152729639

代码



/**
 * @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>
/**
 * 子进程进行处理
 * @param client iocontext的client 端
 * @param in_uri
 */
static void process_client(AVIOContext *client, const char *in_uri)
{
    AVIOContext *input = NULL;
    uint8_t buf[1024];
    int ret, n, reply_code;
    uint8_t *resource = NULL;
    //执行协议握手,可能会阻塞,与服务端建立链接
    while ((ret = avio_handshake(client)) > 0) {
        //获取 resource 属性
        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;
        av_freep(&resource);
    }
    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");
    //打开一个输入流,从输入url中获取信息
    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(;;) {
        //从输入流中读数据到buf中,读取sizeof(buf)个数据
        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);
    av_freep(&resource);
}

/**
 * 多线程http,这里生成了一堆socket客户端服务端,然后客户端连接服务端.握手.读取数据,并写回
 * @param argc
 * @param argv
 * @return
 */
int http_multiclient_main(int argc, char **argv)
{
    AVDictionary *options = NULL;
    AVIOContext *client = NULL, *server = NULL;
    const char *in_uri, *out_uri;
    int ret, pid;
    av_log_set_level(AV_LOG_TRACE);
    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];
//初始化网络各种模块
    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;
    }
    //初始化服务端io上下文,设置向服务写数据,
    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(;;) {
        //初始化客户端io上下文, 感觉这里之后就是客户端和服务端上下文可以传输数据,
        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();//fork子进程
        if (pid < 0) {
            perror("Fork failed");
            ret = AVERROR(errno);
            goto end;
        }
        if (pid == 0) {//这里是子进程 ,子进程使用client端,关闭server端,发送数据
            fprintf(stderr, "In child.\n");
            process_client(client, in_uri);
            avio_close(server);
            exit(0);
        }
        if (pid > 0) //父进程关闭client端.接收数据
            avio_close(client);
    }
end:
    avio_close(server);
    if (ret < 0 && ret != AVERROR_EOF) {
        fprintf(stderr, "Some errors occurred: %s\n", av_err2str(ret));
        return 1;
    }
    return 0;
}

上一篇下一篇

猜你喜欢

热点阅读