Skip to content

Commit

Permalink
Fix bug with esp32-cam-webserver #545
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jul 31, 2023
1 parent ba6c964 commit 855b97c
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/mjpeg/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/http"
"net/textproto"
"reflect"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 855b97c

Please sign in to comment.