登录
首页 >  Golang >  Go问答

在 Go 中实现应用程序/八位字节流(实时视频)的手动代理

来源:stackoverflow

时间:2024-02-26 18:27:25 308浏览 收藏

从现在开始,努力学习吧!本文《在 Go 中实现应用程序/八位字节流(实时视频)的手动代理》主要讲解了等等相关知识点,我会在golang学习网中持续更新相关的系列文章,欢迎大家关注并积极留言建议。下面就先一起来看一下本篇正文内容吧,希望能帮到你!

问题内容

我正在尝试构建一个应用程序,它将充当具有缓存功能的流代理服务器。问题是,我想手动执行此操作,而不使用 newsinglehostreverseproxy。手动意味着执行以下步骤:

  1. 向服务器执行单个 get 请求
  2. 读取 resp.body 到缓冲区并写入连接的客户端

问题是 vlc 不播放任何内容。如果我直接访问流 - vlc 播放它没有问题,但如果我通过 go 访问流 - vlc(以及 kodi)只是保持缓冲并且从不开始播放。

我尝试过但没有成功的事情:

  1. io.copy(...)
  2. bufio 读取器/扫描器并写入连接的客户端。冲水也没有帮助。

这就是 curl -v 所说的(直接访问流媒体 url):

...
* mark bundle as not supporting multiuse
< http/1.1 200 ok
< server: myserver
< cache-control: no-cache
< pragma: no-cache
< content-type: application/octet-stream
< access-control-allow-origin: *
< connection: close
< 
warning: binary output can mess up your terminal. use "--output -" to tell 
warning: curl to output it to your terminal anyway, or consider "--output 
warning: " to save to a file.
* failed writing body (0 != 2896)
* closing connection 0

这就是 curl -v 所说的:

...
* mark bundle as not supporting multiuse
< http/1.1 200 ok
< access-control-allow-origin: *
< cache-control: no-cache
< content-type: application/octet-stream
< pragma: no-cache
< server: myserver
< date: sun, 29 dec 2019 09:13:44 gmt
< transfer-encoding: chunked
< 
warning: binary output can mess up your terminal. use "--output -" to tell 
warning: curl to output it to your terminal anyway, or consider "--output 
warning: " to save to a file.
* failed writing body (0 != 14480)
* failed reading the chunked-encoded stream
* closing connection 0

问题似乎就在这里,但是我该如何解决呢?

好:

* failed writing body (0 != 2896)
* closing connection 0

坏:

* failed writing body (0 != 14480)
* failed reading the chunked-encoded stream
* closing connection 0

还添加源代码,例如:

func main() {
    http.HandleFunc("/stream", func(w http.ResponseWriter, r *http.Request) {
        resp, err := http.Get("http://example.com/stream/videostream")
        if err != nil {
            panic(err)
        }
        defer resp.Body.Close()
        reader := bufio.NewReader(resp.Body)
        buf := make([]byte, 1024)
        for {
            k, _ := reader.Read(buf)
            if k == 0 {
                break
            }
            w.Write(buf)
        }
    })
    log.Fatal(http.ListenAndServe(":8080", nil))
}

解决方案


我正在构建一个动态 M3U 播放列表,并在每个链接上放置 .m3u8 结尾。所有链接的类型都是未知的,所以我认为媒体播放器只会查看Content-Type并相应地处理媒体,但实际上它们也会查看URL结尾(无论URL是否包含.m3u8) )。

如果您将 .m3u8 放在链接的末尾,然后将该链接的 Content-Type 设置为 application/octet-stream 并提供实际的 application/octet-stream 流 - 播放器将永远不会开始显示任何视频流。解决方案 - 如果 URL 不是 m3u8 格式媒体,则不要将 .m3u8 附加到 URL。如果没有 m3u8 媒体播放器仍然可以显示视频。

今天关于《在 Go 中实现应用程序/八位字节流(实时视频)的手动代理》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>