理解 Http Stream [Transfer-Encoding- chunked]

在 Http 版本 1.1 中提供支持。

通常情况下我们服务器的每一个 Response 都是以整个内容发送的。Content-Length 消息头字段表示整个内容的长度。数据的长度很重要,因为客户端需要知道哪里是应答消息的结束,以及后续应答消息的开始。然而,使用分块传输编码,数据分解成一系列数据块,并以一个或多个块发送,这样服务器可以发送数据而不需要预先知道发送内容的总大小。

Chunked编码使用若干个Chunk串连而成,由一个标明长度为0的chunk标示结束。

每个Chunk分为头部和正文两部分,头部内容指定下一段正文的字符总数(十六进制的数字)和数量单位(一般不写),正文部分就是指定长度的实际内容,两部分之间用回车换行(CRLF)隔开。

在最后一个长度为0的Chunk中的内容是称为footer的内容,是一些附加的Header信息(通常可以直接忽略)。

参见 [RFC2616] 标准 3.6.1 提供的 编码解码 算法

   每一个 chunk 块都有以下可选指标
   **编码算法**
   Chunked-Body   = *chunk
                    last-chunk
                    trailer
                    CRLF
   chunk          = chunk-size [ chunk-extension ] CRLF
                    chunk-data CRLF
   chunk-size     = 1*HEX
   last-chunk     = 1*("0") [ chunk-extension ] CRLF
   chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
   chunk-ext-name = token
   chunk-ext-val  = token | quoted-string
   chunk-data     = chunk-size(OCTET)
   trailer        = *(entity-header CRLF)



   **解码算法**
   length := 0
   read chunk-size, chunk-ext (if any) and CRLF
   while (chunk-size > 0) {
     read chunk-data and CRLF
     append chunk-data to entity-body
     length := length + chunk-size
     read chunk-size and CRLF
   }

   read entity-header

   while (entity-header not empty) {
     append entity-header to existing header fields
     read entity-header
   }
   Content-Length := length
   Remove "chunked" from Transfer-Encoding
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: 要使用nginx实现http-flv推流,需要按照以下步骤: 1.安装nginx和rtmp-module   首先需要安装nginx和rtmp-module。可以参考以下资料进行下载和安装。   nginx官网:http://nginx.org/   rtmp-module官网:https://github.com/arut/nginx-rtmp-module 2.配置nginx.conf文件   在nginx.conf文件中,需要添加以下配置信息。其中,flv、live和stream是自定义的,可以根据需要修改。 rtmp { server { listen 1935; #监听端口 chunk_size 4096; #设置chunk_size大小 application flv { live on; #启用直播模式 allow publish all; #允许推流 deny play all; #禁止播放 push_reconnect off; #关闭推流断开重连 hls off; #关闭hls flv_metadata on; #开启flv的metadata信息 wait_key on; #开启wait_key exec_pull ffmpeg -i rtmp://xxxx/$app/$name -vcodec libx264 -vprofile baseline -s 720x576 -acodec libfaac -ar 44100 -f flv rtmp://xxxx/flv/$name; #开启转码功能,实现兼容性 notify_method get; #推流成功后,请求指定地址 notify_update http://xxxx/stream/$app/$name; } } } http { server { listen 80; #监听端口 location /live { flv_live on; #开启http-flv推流 chunked_transfer_encoding on; #开启chunked传输 add_header 'Access-Control-Allow-Origin' '*'; #跨域 add_header 'Allow' 'GET, POST, OPTIONS'; #允许的http方法 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; #允许跨域的方法 add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept'; #允许跨域的请求头 } } } 3.启动nginx   将修改后的nginx.conf文件保存,使用以下命令启动nginx。 sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 4.推流   使用推流软件(如OBS)等工具,将视频流推送到rtmp://localhost/flv/{stream_name}的地址,即可在http://localhost/live/{stream_name}.flv,通过http-flv协议播放推送的视频流。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值