簡體   English   中英

如何在 Go 中手動代理應用程序/八位字節流(實時視頻)?

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

我正在嘗試構建一個應用程序,它將充當具有緩存功能的流代理服務器。 問題是,我想在不使用NewSingleHostReverseProxy情況下手動NewSingleHostReverseProxy 手動意味着執行以下步驟:

  1. 向服務器執行單個GET請求
  2. 讀取resp.Body以緩沖並寫入連接的客戶端

問題是 VLC 不播放任何內容。 如果我直接訪問流 - VLC 播放它沒有問題,但如果我通過 GO 進行 - VLC(以及 Kodi)只是保持緩沖並且永遠不會開始播放。

我嘗試過但沒有奏效的事情:

  1. io.Copy(...)
  2. bufio閱讀器/掃描器並寫入連接的客戶端。 沖洗也沒有幫助。

這就是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

這就是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

問題似乎在這里,但我該如何解決呢?

好的:

* 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放在鏈接的.m3u8 ,則將該鏈接的 Content-Type 設置為application/octet-stream並提供實際的application/octet-stream流 - 播放器將永遠不會開始顯示任何視頻流。 解決方法-不要追加.m3u8到URL,如果該URL不m3u8格式的媒體。 沒有m3u8媒體播放器仍然可以播放視頻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM