improve examples (#778)

This commit is contained in:
Alessandro Ros
2025-05-04 17:00:18 +02:00
committed by GitHub
parent 4e3c5e6c0d
commit cc0c96626c
55 changed files with 494 additions and 435 deletions

View File

@@ -62,6 +62,7 @@ func (c *client) read() error {
return err
}
// notify the server that we are ready
stream := c.server.setStreamReady(desc)
defer c.server.setStreamUnready()

View File

@@ -4,8 +4,8 @@ import "log"
// This example shows how to
// 1. create a server that serves a single stream.
// 2. create a client, read an existing stream from an external server or camera,
// pass the stream to the server in order to serve it.
// 2. create a client, that reads an existing stream from another server or camera.
// 3. route the stream from the client to the server, and from the server to all connected readers.
func main() {
// allocate the server.
@@ -13,11 +13,11 @@ func main() {
s.initialize()
// allocate the client.
// give client access to the server.
// allow client to use the server.
c := &client{server: s}
c.initialize()
// start server and wait until a fatal error
log.Printf("server is ready on %s", s.server.RTSPAddress)
s.server.StartAndWait()
panic(s.server.StartAndWait())
}

View File

@@ -19,9 +19,9 @@ func (s *server) initialize() {
// configure the server
s.server = &gortsplib.Server{
Handler: s,
RTSPAddress: ":8554",
UDPRTPAddress: ":8000",
UDPRTCPAddress: ":8001",
RTSPAddress: ":8556",
UDPRTPAddress: ":8002",
UDPRTCPAddress: ":8003",
MulticastIPRange: "224.1.0.0/16",
MulticastRTPPort: 8002,
MulticastRTCPPort: 8003,
@@ -98,20 +98,24 @@ func (s *server) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response,
func (s *server) setStreamReady(desc *description.Session) *gortsplib.ServerStream {
s.mutex.Lock()
defer s.mutex.Unlock()
s.stream = &gortsplib.ServerStream{
Server: s.server,
Desc: desc,
}
err := s.stream.Initialize()
if err != nil {
panic(err)
}
return s.stream
}
func (s *server) setStreamUnready() {
s.mutex.Lock()
defer s.mutex.Unlock()
s.stream.Close()
s.stream = nil
}