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

僤鯓⒐⒋嵵緔 提交于 2020-01-05 04:24:07

问题


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. Manually means performing these steps:

  1. Perform single GET request to the server
  2. Read resp.Body to buffer and write to connected client(s)

And the issue is that VLC doesn't play anything. 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.

Things I've tried, but did not work:

  1. io.Copy(...)
  2. bufio reader/scanner and writing to the connected client. Flushing also did not help.

This is what curl -v <stream_url> says (accessing streaming url directly):

...
* 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:

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

来源:https://stackoverflow.com/questions/59518175/how-do-i-manually-proxy-application-octet-stream-live-video-in-go

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!