diff --git a/internal/http/http.go b/internal/http/http.go index 4b55d2cf..8b1903f3 100644 --- a/internal/http/http.go +++ b/internal/http/http.go @@ -7,6 +7,7 @@ import ( "net/url" "strings" + "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hls" @@ -22,6 +23,8 @@ func Init() { streams.HandleFunc("httpx", handleHTTP) streams.HandleFunc("tcp", handleTCP) + + api.HandleFunc("api/stream", apiStream) } func handleHTTP(rawURL string) (core.Producer, error) { @@ -89,3 +92,26 @@ func handleTCP(rawURL string) (core.Producer, error) { return magic.Open(conn) } + +func apiStream(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 + } + + client, err := magic.Open(r.Body) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + stream.AddProducer(client) + defer stream.RemoveProducer(client) + + if err = client.Start(); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} diff --git a/pkg/magic/bitstream/producer.go b/pkg/magic/bitstream/producer.go index 7951a77c..2ffa964e 100644 --- a/pkg/magic/bitstream/producer.go +++ b/pkg/magic/bitstream/producer.go @@ -70,13 +70,15 @@ func (c *Producer) Start() error { break } - pkt := &rtp.Packet{ - Header: rtp.Header{Timestamp: core.Now90000()}, - Payload: annexb.EncodeToAVCC(buf[:i], true), + if len(c.Receivers) > 0 { + pkt := &rtp.Packet{ + Header: rtp.Header{Timestamp: core.Now90000()}, + Payload: annexb.EncodeToAVCC(buf[:i], true), + } + c.Receivers[0].WriteRTP(pkt) + + //log.Printf("[AVC] %v, len: %d", h264.Types(pkt.Payload), len(pkt.Payload)) } - c.Receivers[0].WriteRTP(pkt) - - //log.Printf("[AVC] %v, len: %d", h264.Types(pkt.Payload), len(pkt.Payload)) buf = buf[i:] }