feat(webp): add WebP streaming and snapshot APIs

- implement WebP streaming with multipart support
- add snapshot endpoint for WebP format with quality options
- introduce WebP encoding using pure Go library without FFmpeg
- update documentation and links for new WebP features
This commit is contained in:
Sergey Krashevich
2026-03-10 21:37:29 +03:00
parent beb96dd076
commit ea03aa832d
14 changed files with 641 additions and 1 deletions
+35
View File
@@ -1,13 +1,16 @@
package image
import (
"bytes"
"errors"
"image/jpeg"
"io"
"net/http"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/tcp"
"github.com/pion/rtp"
webp "github.com/skrashevich/go-webp"
)
type Producer struct {
@@ -49,6 +52,12 @@ func (c *Producer) Start() error {
return err
}
if isWebP(body) {
if converted, err2 := webpToJPEG(body); err2 == nil {
body = converted
}
}
pkt := &rtp.Packet{
Header: rtp.Header{Timestamp: core.Now90000()},
Payload: body,
@@ -74,6 +83,12 @@ func (c *Producer) Start() error {
return err
}
if isWebP(body) {
if converted, err2 := webpToJPEG(body); err2 == nil {
body = converted
}
}
c.Recv += len(body)
pkt = &rtp.Packet{
@@ -90,3 +105,23 @@ func (c *Producer) Stop() error {
c.closed = true
return c.Connection.Stop()
}
// isWebP returns true if data starts with RIFF....WEBP magic bytes.
func isWebP(data []byte) bool {
return len(data) >= 12 &&
data[0] == 'R' && data[1] == 'I' && data[2] == 'F' && data[3] == 'F' &&
data[8] == 'W' && data[9] == 'E' && data[10] == 'B' && data[11] == 'P'
}
// webpToJPEG decodes WebP bytes and re-encodes as JPEG.
func webpToJPEG(data []byte) ([]byte, error) {
img, err := webp.Decode(bytes.NewReader(data))
if err != nil {
return nil, err
}
var buf bytes.Buffer
if err = jpeg.Encode(&buf, img, nil); err != nil {
return nil, err
}
return buf.Bytes(), nil
}