美文网首页
ffmpeg # http.c - http_open

ffmpeg # http.c - http_open

作者: 爱玩保龄球 | 来源:发表于2024-07-07 17:04 被阅读0次

这是一个 http_open 函数的实现,它是 FFmpeg 的 HTTP 协议处理的一部分。这个函数用于打开一个 HTTP 连接,并进行相应的初始化和错误处理。下面是逐行的中文注释和解释:

static int http_open(URLContext *h, const char *uri, int flags,
                     AVDictionary **options)
{
    // 获取私有数据 HTTPContext
    HTTPContext *s = h->priv_data;
    int ret;

    // 根据 seekable 属性设置 is_streamed 标志
    if (s->seekable == 1)
        h->is_streamed = 0;
    else
        h->is_streamed = 1;

    // 初始化文件大小为最大值
    s->filesize = UINT64_MAX;

    // 复制 URI 到 location 字段
    s->location = av_strdup(uri);
    if (!s->location)
        return AVERROR(ENOMEM); // 内存分配失败,返回错误码

    // 复制 URI 到 uri 字段
    s->uri = av_strdup(uri);
    if (!s->uri)
        return AVERROR(ENOMEM); // 内存分配失败,返回错误码

    // 复制 options 到 chained_options 字段
    if (options)
        av_dict_copy(&s->chained_options, *options, 0);

    // 如果 headers 存在,检查并添加 CRLF
    if (s->headers) {
        int len = strlen(s->headers);
        if (len < 2 || strcmp("\r\n", s->headers + len - 2)) {
            av_log(h, AV_LOG_WARNING,
                   "No trailing CRLF found in HTTP header. Adding it.\n");
            ret = av_reallocp(&s->headers, len + 3);
            if (ret < 0)
                goto bail_out; // 重新分配内存失败,跳转到错误处理部分
            s->headers[len]     = '\r';
            s->headers[len + 1] = '\n';
            s->headers[len + 2] = '\0';
        }
    }

    // 如果是监听模式,调用 http_listen 函数
    if (s->listen) {
        return http_listen(h, uri, flags, options);
    }

    // 否则,调用 http_open_cnx 函数打开连接
    ret = http_open_cnx(h, options);

bail_out:
    // 错误处理部分,释放分配的资源
    if (ret < 0) {
        av_dict_free(&s->chained_options);
        av_dict_free(&s->cookie_dict);
        av_dict_free(&s->redirect_cache);
        av_freep(&s->new_location);
        av_freep(&s->uri);
    }
    return ret; // 返回结果
}

代码解释:

  1. 获取私有数据HTTPContext *s = h->priv_data;URLContext 中获取 HTTPContext 的私有数据。

  2. 设置流标志:根据 s->seekable 属性设置 h->is_streamed 标志。如果 seekable 为 1,则 is_streamed 设置为 0;否则,设置为 1。

  3. 初始化文件大小:将 s->filesize 初始化为 UINT64_MAX,表示文件大小未知或无限大。

  4. 复制 URI:使用 av_strdup 复制 uris->locations->uri,如果分配失败,返回内存错误码 AVERROR(ENOMEM)

  5. 复制选项:如果 options 存在,使用 av_dict_copy 将其复制到 s->chained_options

  6. 检查并添加 CRLF:如果 s->headers 存在,检查其末尾是否有 CRLF,如果没有,使用 av_reallocp 重新分配内存并添加 CRLF。如果内存重新分配失败,跳转到错误处理部分 bail_out

  7. 监听模式:如果 s->listen 为真,调用 http_listen 函数处理监听连接。

  8. 打开连接:调用 http_open_cnx 函数打开 HTTP 连接,并将返回值存储在 ret 中。

  9. 错误处理:在 bail_out 部分,如果 ret 小于 0,表示操作失败,释放之前分配的资源。

  10. 返回结果:返回 ret,表示操作的结果。

这个函数的主要作用是初始化 HTTP 连接,并在出错时进行相应的清理和资源释放。

相关文章

网友评论

      本文标题:ffmpeg # http.c - http_open

      本文链接:https://www.haomeiwen.com/subject/vsfocjtx.html