This commit is contained in:
Soph :3 2025-11-24 20:46:11 +02:00
parent 9e5cd110e0
commit ab8b9f5315
2 changed files with 55 additions and 52 deletions

View file

@ -273,7 +273,10 @@ func main() {
}
relay := libipcamera.CreateRTPRelay(applicationContext)
rtspServer, err := rtsp.CreateServer(applicationContext, host, port, relay)
if rtspServer != nil {
defer rtspServer.Close()
}
if err := camera.StartPreviewStream(); err != nil {

View file

@ -24,39 +24,41 @@ type Server struct {
wg sync.WaitGroup
}
// CreateServer now requires relay as input
func CreateServer(parentCtx context.Context, host string, port int, relay *libipcamera.RTPRelay) (*Server, error) {
ctx, cancel := context.WithCancel(parentCtx)
stream, media, err := newH264Stream()
// Create the RTSP server instance first
h := &handler{}
gs := &gortsplib.Server{
Handler: h,
RTSPAddress: fmt.Sprintf("%s:%d", host, port),
}
// Now create the H264 stream bound to this server
stream, media, err := newH264Stream(gs)
if err != nil {
cancel()
return nil, fmt.Errorf("create H264 stream: %w", err)
}
// Final server struct assembly
srv := &Server{
ctx: ctx,
cancel: cancel,
gs: gs,
stream: stream,
media: media,
}
h.srv = srv // link handler to server instance
h := &handler{srv: srv}
gs := &gortsplib.Server{
Handler: h,
RTSPAddress: fmt.Sprintf("%s:%d", host, port),
}
srv.gs = gs
// 🧠 Pump from relay to RTSP
// Start pumping RTP frames from relay into RTSP
srv.wg.Add(1)
go srv.relayPump(relay)
// Start the RTSP server
go func() {
log.Printf("RTSP server ready on rtsp://%s:%d/", host, port)
err := gs.StartAndWait()
if err != nil {
if err := gs.StartAndWait(); err != nil {
log.Printf("RTSP stopped: %v", err)
}
cancel()
@ -113,7 +115,7 @@ func (h *handler) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response,
}
// Build simple baseline H264 SDP
func newH264Stream() (*gortsplib.ServerStream, *description.Media, error) {
func newH264Stream(gs *gortsplib.Server) (*gortsplib.ServerStream, *description.Media, error) {
h264 := &format.H264{
PayloadTyp: 96,
PacketizationMode: 1,
@ -130,10 +132,8 @@ func newH264Stream() (*gortsplib.ServerStream, *description.Media, error) {
Medias: []*description.Media{media},
}
stream := &gortsplib.ServerStream{Desc: desc}
if err := stream.Initialize(); err != nil {
return nil, nil, err
}
// REQUIRED: link stream to the running server
stream := gortsplib.NewServerStream(gs, desc)
return stream, media, nil
}