深入理解nginx mp4流媒体模块[中]

上接深入理解nginx mp4流媒体模块[上]

  以下对ngx_http_mp4_file_t的结构定义进行说明:

typedef struct {
   
    ngx_file_t            file;              # mp4文件对象

    u_char               *buffer;            # 用于mp4分析的缓冲区
    u_char               *buffer_start;      # buffer空闲的起始位置
    u_char               *buffer_pos;        # buffer中可用于分析的起始位置
    u_char               *buffer_end;        # buffer中可用于分析的结束位置
    size_t                buffer_size;       # mp4分析缓冲区buffer的大小

    off_t                 offset;            # buffer_start对应的mp4文件读取的偏移量
    off_t                 end;               # 当前mp4文件的文件大小
    off_t                 content_length;    # 最终发送给客户端响应的内容长度
    ngx_uint_t            start;             # 请求的起始偏移时间
    ngx_uint_t            length;            # 请求的视频时长
    uint32_t              timescale;         # mp4文件中设置的时间scale值
    ngx_http_request_t   *request;           # 对应当前的http request对象
    ngx_array_t           trak;              # mp4包含的track列表,引用traks,最多2ngx_http_mp4_trak_t   traks[2];          # mp4包含的track列表

    size_t                ftyp_size;         # ftyp atom的大小
    size_t                moov_size;         # moov atom的大小

    ngx_chain_t          *out;
    ngx_chain_t           ftyp_atom;         # 链接了ftyp_atom_buf的缓冲区链
    ngx_chain_t           moov_atom;         # 链接了moov_atom_buf的缓冲区链
    ngx_chain_t           mvhd_atom;         # 链接了mvhd_atom_buf的缓冲区链
    ngx_chain_t           mdat_atom;         # 链接了mdat_atom_buf的缓冲区链
    ngx_chain_t           mdat_data;         # 链接了mdat_data_buf的缓冲区链

    ngx_buf_t             ftyp_atom_buf;     # ftyp atom的缓冲区
    ngx_buf_t             moov_atom_buf;     # moov atom的缓冲区
    ngx_buf_t             mvhd_atom_buf;     # mvhd atom的缓冲区
    ngx_buf_t             mdat_atom_buf;     # mdat atom的缓冲区
    ngx_buf_t             mdat_data_buf;     # mdat atom的缓冲区

    u_char                moov_atom_header[8];
    u_char                mdat_atom_header[16];
} ngx_http_mp4_file_t;

3.2.4 MP4文件的发送

  其实现源码如下,源码里面进行了详细的备注说明:

   log->action = "sending mp4 to client";

    /* 如果需要,开启directio */
    if (clcf->directio <= of.size) {
   

        /*
         * DIRECTIO is set on transfer only
         * to allow kernel to cache "moov" atom
         */

        if (ngx_directio_on(of.fd) == NGX_FILE_ERROR) {
   
            ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
                          ngx_directio_on_n " \"%s\" failed", path.data);
        }

        of.is_directio = 1;

        if (mp4) {
   
            mp4->file.directio = 1;
        }
    }

    /* 设置响应状态和相关响应头信息,然后将响应头发送给用户 */
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.last_modified_time = of.mtime;

    if (ngx_http_set_etag(r) != NGX_OK) {
   
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if (ngx_http_set_content_type(r) != NGX_OK) {
   
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    if (mp4 == NULL) {
   
        b = ngx_calloc_buf(r->pool);
        if (b == NULL) {
   
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }

        b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
        if (b->file == NULL) {
   
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
    }

    rc = ngx_http_send_header(r);

    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
   
        

相关推荐

  1. 深入理解nginx mp4媒体模块[]

    2024-03-28 14:18:01       44 阅读
  2. 深入理解nginx mp4媒体模块[下下]

    2024-03-28 14:18:01       38 阅读
  3. 深入理解python的subprocess模块

    2024-03-28 14:18:01       27 阅读
  4. 深入理解 C++ 的 IO 【iostream篇】

    2024-03-28 14:18:01       42 阅读
  5. 深入理解Golang的Options模式

    2024-03-28 14:18:01       50 阅读
  6. 深入理解sklearn模型参数优化技术

    2024-03-28 14:18:01       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-28 14:18:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-28 14:18:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-28 14:18:01       82 阅读
  4. Python语言-面向对象

    2024-03-28 14:18:01       91 阅读

热门阅读

  1. Shell教程_不同Shell中字符串处理和替换的差异

    2024-03-28 14:18:01       39 阅读
  2. WIFI驱动移植实验:WIFI驱动加载测试

    2024-03-28 14:18:01       38 阅读
  3. C++经典面试题目(七)

    2024-03-28 14:18:01       41 阅读
  4. 【GitLab】Ubuntu使用宝塔安装GitLab最新社区版

    2024-03-28 14:18:01       42 阅读
  5. unity中屏幕坐标转UI坐标

    2024-03-28 14:18:01       44 阅读
  6. CentOs 8 设置 新源的流程。

    2024-03-28 14:18:01       35 阅读
  7. 视频基础学习三——视频帧率、码率与分辨率

    2024-03-28 14:18:01       105 阅读
  8. vue3.0开发手册(实用版)

    2024-03-28 14:18:01       39 阅读
  9. C#基础-五大数据类型

    2024-03-28 14:18:01       34 阅读
  10. Tomcat 启动闪退问题解决方法

    2024-03-28 14:18:01       40 阅读