go - 为什么客户端通过 http.ServeContent 阅读我的视频时一直关闭连接?

标签 go video-streaming

我目前正在做一个小项目,通过 http.ServeContent 向浏览器或其他媒体客户端提供视频服务。我已经实现了自己的 ReadSeeker,如下所示:

//the seek is not fully working yet but works fine for the initial two calls that is being called internally from http to decide the file size.

func (c *Client) Seek(offset int64, whence int) (t int64, e error) {
    switch whence {
    case 0:
        t = offset
    case 1:
        t = c.seek + offset
    case 2:
        t = c.SelectedFile().Length + offset
    }

    if c.seek != t {
        //TODO
        c.seek = t
    }

    return
}

func (c *Client) Read(p []byte) (n int, err error) {
    if c.done {
        return 0, io.EOF
    }

    for {
        result := <-c.Results

        if result.piece == nil {
            return 0, io.EOF
        }

        for i, b := range result.bytes {
            index := int64(i)
            if index < 0 || index > c.SelectedFile().Length {
                continue
            }

            n++
            p[index] = b
        }

        return
    }
}

例如,如果我使用 curl 来获取数据,例如数据一直在流式传输(每次读取 1024 字节),则一切正常且花花公子。但是,只要我使用浏览器甚至 VLC,在读取 5 到 10 次之后,http writer.Write() 就会返回 EPIPE 错误并且连接断开。该错误似乎直接来自 io.go 中的 dst.Write(buf[0:nr]) :

...
func copyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error) {
    ...
    for {
        nr, er := src.Read(buf)
        if nr > 0 {
            nw, ew := dst.Write(buf[0:nr])
            if nw > 0 {
                written += int64(nw)
            }
            if ew != nil {
                err = ew
                break
            }
            if nr != nw {
                err = ErrShortWrite
                break
            }
        }
        if er != nil {
            if er != EOF {
                err = er
            }
            break
        }
    }
    return written, err
}

我已经尝试调试好几天了,但似乎无法找出原因。我一直在将我提供的字节与实际文件进行比较,数据似乎没有任何问题。

任何想法可能是错误的?

最佳答案

没有错。没有什么要求客户端在服务器发送完所有数据之前不断开连接。这是很常见的,尤其是对于 mp4 文件,客户端执行标准的 http 请求,然后在收到 header 后取消。

但是我怀疑视频没有按照您期望的方式播放?如果是这种情况,请确保您的服务器实现了 http 范围请求。

关于go - 为什么客户端通过 http.ServeContent 阅读我的视频时一直关闭连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55688480/

相关文章:

json - 在 Golang 中打印解码后的 JSON

python - 将 ffmpeg 流中的视频与音频分离

ffmpeg - 如何使用 ffmpeg 在横向模式下制作我的视频

Golang 坚持包范围内的函数有一个特定的类型

windows - 是否可以使用 Go 编译器为其他操作系统分发可执行文件?

gorequest 包 : check specifically for timeout

mysql - 与 MysqlStore 的 Golang session 不起作用

c++ - AR.Drone 2, ffmpeg avcodec_decode_video2( ) 段错误

C++ 视频流检测 FPS

Android - MediaRecorder 输出以流式传输 ffmpeg