Skip to content

Commit

Permalink
Add support incoming H264 bitstream #745
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Nov 13, 2023
1 parent 2d16ee8 commit aa3c081
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
26 changes: 26 additions & 0 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
}
14 changes: 8 additions & 6 deletions pkg/magic/bitstream/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
}
Expand Down

0 comments on commit aa3c081

Please sign in to comment.