diff --git a/pkg/mjpeg/client.go b/pkg/mjpeg/client.go index 2211dbd1..1a523336 100644 --- a/pkg/mjpeg/client.go +++ b/pkg/mjpeg/client.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/textproto" + "reflect" "strconv" "strings" "time" @@ -74,13 +75,21 @@ func (c *Client) startJPEG() error { } func (c *Client) startMJPEG(boundary string) error { + r := bufio.NewReader(c.res.Body) + + // fix bug in the project https://github.com/easytarget/esp32-cam-webserver/ + // https://github.com/AlexxIT/go2rtc/issues/545 + if boundary == "123456789000000000000987654321" { + r = bypassChunked(c.res.Body) + _, _ = r.Discard(2) + } + // some cameras add prefix to boundary header: // https://github.com/TheTimeWalker/wallpanel-android if !strings.HasPrefix(boundary, "--") { boundary = "--" + boundary } - r := bufio.NewReader(c.res.Body) tp := textproto.NewReader(r) for !c.closed { @@ -133,3 +142,11 @@ func (c *Client) startMJPEG(boundary string) error { func now() uint32 { return uint32(time.Now().UnixMilli() * 90) } + +// bypassChunked - hacky method to get raw bufio.Reader from http.body +func bypassChunked(body any) *bufio.Reader { + // body is http.body with src as internal.chunkedReader + v := reflect.ValueOf(body).Elem().FieldByName("r") + prtToBuf := (**bufio.Reader)(v.Addr().UnsafePointer()) + return *prtToBuf +}