three
This commit is contained in:
parent
9e5cd110e0
commit
ab8b9f5315
2 changed files with 55 additions and 52 deletions
|
|
@ -273,7 +273,10 @@ func main() {
|
||||||
}
|
}
|
||||||
relay := libipcamera.CreateRTPRelay(applicationContext)
|
relay := libipcamera.CreateRTPRelay(applicationContext)
|
||||||
rtspServer, err := rtsp.CreateServer(applicationContext, host, port, relay)
|
rtspServer, err := rtsp.CreateServer(applicationContext, host, port, relay)
|
||||||
|
if rtspServer != nil {
|
||||||
defer rtspServer.Close()
|
defer rtspServer.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if err := camera.StartPreviewStream(); err != nil {
|
if err := camera.StartPreviewStream(); err != nil {
|
||||||
|
|
|
||||||
|
|
@ -24,39 +24,41 @@ type Server struct {
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateServer now requires relay as input
|
|
||||||
func CreateServer(parentCtx context.Context, host string, port int, relay *libipcamera.RTPRelay) (*Server, error) {
|
func CreateServer(parentCtx context.Context, host string, port int, relay *libipcamera.RTPRelay) (*Server, error) {
|
||||||
ctx, cancel := context.WithCancel(parentCtx)
|
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 {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return nil, fmt.Errorf("create H264 stream: %w", err)
|
return nil, fmt.Errorf("create H264 stream: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Final server struct assembly
|
||||||
srv := &Server{
|
srv := &Server{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
|
gs: gs,
|
||||||
stream: stream,
|
stream: stream,
|
||||||
media: media,
|
media: media,
|
||||||
}
|
}
|
||||||
|
h.srv = srv // link handler to server instance
|
||||||
|
|
||||||
h := &handler{srv: srv}
|
// Start pumping RTP frames from relay into RTSP
|
||||||
|
|
||||||
gs := &gortsplib.Server{
|
|
||||||
Handler: h,
|
|
||||||
RTSPAddress: fmt.Sprintf("%s:%d", host, port),
|
|
||||||
}
|
|
||||||
srv.gs = gs
|
|
||||||
|
|
||||||
// 🧠 Pump from relay to RTSP
|
|
||||||
srv.wg.Add(1)
|
srv.wg.Add(1)
|
||||||
go srv.relayPump(relay)
|
go srv.relayPump(relay)
|
||||||
|
|
||||||
|
// Start the RTSP server
|
||||||
go func() {
|
go func() {
|
||||||
log.Printf("RTSP server ready on rtsp://%s:%d/", host, port)
|
log.Printf("RTSP server ready on rtsp://%s:%d/", host, port)
|
||||||
err := gs.StartAndWait()
|
if err := gs.StartAndWait(); err != nil {
|
||||||
if err != nil {
|
|
||||||
log.Printf("RTSP stopped: %v", err)
|
log.Printf("RTSP stopped: %v", err)
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
|
|
@ -113,7 +115,7 @@ func (h *handler) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build simple baseline H264 SDP
|
// 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{
|
h264 := &format.H264{
|
||||||
PayloadTyp: 96,
|
PayloadTyp: 96,
|
||||||
PacketizationMode: 1,
|
PacketizationMode: 1,
|
||||||
|
|
@ -130,10 +132,8 @@ func newH264Stream() (*gortsplib.ServerStream, *description.Media, error) {
|
||||||
Medias: []*description.Media{media},
|
Medias: []*description.Media{media},
|
||||||
}
|
}
|
||||||
|
|
||||||
stream := &gortsplib.ServerStream{Desc: desc}
|
// REQUIRED: link stream to the running server
|
||||||
if err := stream.Initialize(); err != nil {
|
stream := gortsplib.NewServerStream(gs, desc)
|
||||||
return nil, nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return stream, media, nil
|
return stream, media, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue