Add support incoming MJPEG stream

This commit is contained in:
Alexey Khit
2023-02-14 14:25:13 +03:00
parent 6e62c442f8
commit 47371fbdcf
2 changed files with 31 additions and 1 deletions
+30
View File
@@ -6,6 +6,7 @@ import (
"github.com/AlexxIT/go2rtc/cmd/streams"
"github.com/AlexxIT/go2rtc/pkg/mjpeg"
"github.com/rs/zerolog/log"
"io"
"net/http"
"strconv"
)
@@ -62,6 +63,14 @@ func handlerKeyframe(w http.ResponseWriter, r *http.Request) {
const header = "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: "
func handlerStream(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
outputMjpeg(w, r)
} else {
inputMjpeg(w, r)
}
}
func outputMjpeg(w http.ResponseWriter, r *http.Request) {
src := r.URL.Query().Get("src")
stream := streams.GetOrNew(src)
if stream == nil {
@@ -108,6 +117,27 @@ func handlerStream(w http.ResponseWriter, r *http.Request) {
//log.Trace().Msg("[api.mjpeg] close")
}
func inputMjpeg(w http.ResponseWriter, r *http.Request) {
dst := r.URL.Query().Get("dst")
stream := streams.Get(dst)
if stream == nil {
http.Error(w, api.StreamNotFound, http.StatusNotFound)
return
}
res := &http.Response{Body: r.Body, Header: r.Header, Request: r}
res.Header.Set("Content-Type", "multipart/mixed;boundary=")
client := mjpeg.NewClient(res)
stream.AddProducer(client)
if err := client.Start(); err != nil && err != io.EOF {
log.Warn().Err(err).Caller().Send()
}
stream.RemoveProducer(client)
}
func handlerWS(tr *api.Transport, _ *api.Message) error {
src := tr.Request.URL.Query().Get("src")
stream := streams.GetOrNew(src)
+1 -1
View File
@@ -136,7 +136,7 @@ func (c *Client) startMJPEG(boundary string) error {
if err != nil {
return err
}
if s != boundary {
if !strings.HasPrefix(s, boundary) {
return errors.New("wrong boundary: " + s)
}