简体   繁体   English

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

[英]How do I manually proxy application/octet-stream (live video) in Go?

I am trying to build an application which would act as a streaming proxy server with caching features.我正在尝试构建一个应用程序,它将充当具有缓存功能的流代理服务器。 The thing is, I want to do it manually without using NewSingleHostReverseProxy .问题是,我想在不使用NewSingleHostReverseProxy情况下手动NewSingleHostReverseProxy Manually means performing these steps:手动意味着执行以下步骤:

  1. Perform single GET request to the server向服务器执行单个GET请求
  2. Read resp.Body to buffer and write to connected client(s)读取resp.Body以缓冲并写入连接的客户端

And the issue is that VLC doesn't play anything.问题是 VLC 不播放任何内容。 If I access stream directly - VLC plays it without problems, but if I do it via GO - VLC (as well as Kodi) just keeps buffering and never starts playing.如果我直接访问流 - VLC 播放它没有问题,但如果我通过 GO 进行 - VLC(以及 Kodi)只是保持缓冲并且永远不会开始播放。

Things I've tried, but did not work:我尝试过但没有奏效的事情:

  1. io.Copy(...)
  2. bufio reader/scanner and writing to the connected client. bufio阅读器/扫描器并写入连接的客户端。 Flushing also did not help.冲洗也没有帮助。

This is what curl -v <stream_url> says (accessing streaming url directly):这就是curl -v <stream_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: <FILE>" to save to a file.
* Failed writing body (0 != 2896)
* Closing connection 0

This is what curl -v <my_app_url> says:这就是curl -v <my_app_url>所说的:

...
* 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: <FILE>" to save to a file.
* Failed writing body (0 != 14480)
* Failed reading the chunked-encoded stream
* Closing connection 0

Seems the issue is here, but how do I solve it?问题似乎在这里,但我该如何解决呢?

Good:好的:

* Failed writing body (0 != 2896)
* Closing connection 0

Bad:坏的:

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

Also adding a source code for example:还添加了一个源代码,例如:

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))
}

I was building a dynamic M3U playlist and putting .m3u8 ending on each link.我正在构建一个动态M3U播放列表并将.m3u8放在每个链接上。 All the links are of unknown type, so I thought media players will just look at Content-Type and handle media accordingly, but actually they look at URL endings as well (whether URL contains .m3u8 or not).所有链接都是未知类型,所以我认为媒体播放器只会查看Content-Type并相应地处理媒体,但实际上他们也会查看 URL 结尾(无论 URL 是否包含.m3u8 )。

If you put at the end of the link .m3u8 , then set that link's Content-Type as application/octet-stream and provide actual application/octet-stream stream - players will never start showing any video stream.如果您将.m3u8放在链接的.m3u8 ,则将该链接的 Content-Type 设置为application/octet-stream并提供实际的application/octet-stream流 - 播放器将永远不会开始显示任何视频流。 Solution - do not append .m3u8 to URL if that URL is not m3u8 format media.解决方法-不要追加.m3u8到URL,如果该URL不m3u8格式的媒体。 Without m3u8 media players can still show the video.没有m3u8媒体播放器仍然可以播放视频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM