-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add ServerConn.ValidateCredentials()
- Loading branch information
Showing
7 changed files
with
378 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
|
||
"github.com/pion/rtp" | ||
|
||
"github.com/bluenviron/gortsplib/v4" | ||
"github.com/bluenviron/gortsplib/v4/pkg/base" | ||
"github.com/bluenviron/gortsplib/v4/pkg/description" | ||
"github.com/bluenviron/gortsplib/v4/pkg/format" | ||
) | ||
|
||
// This example shows how to | ||
// 1. create a RTSP server which accepts plain connections | ||
// 2. allow a single client to authenticate and publish a stream with TCP or UDP | ||
// 3. allow multiple clients to authenticate and read that stream with TCP, UDP or UDP-multicast | ||
|
||
const ( | ||
readUser = "readuser" | ||
readPass = "readpass" | ||
publishUser = "publishuser" | ||
publishPass = "publishpass" | ||
) | ||
|
||
type serverHandler struct { | ||
s *gortsplib.Server | ||
mutex sync.Mutex | ||
stream *gortsplib.ServerStream | ||
publisher *gortsplib.ServerSession | ||
noncePerConns map[*gortsplib.ServerConn]string | ||
} | ||
|
||
// called when a connection is opened. | ||
func (sh *serverHandler) OnConnOpen(ctx *gortsplib.ServerHandlerOnConnOpenCtx) { | ||
log.Printf("conn opened") | ||
} | ||
|
||
// called when a connection is closed. | ||
func (sh *serverHandler) OnConnClose(ctx *gortsplib.ServerHandlerOnConnCloseCtx) { | ||
log.Printf("conn closed (%v)", ctx.Error) | ||
|
||
delete(sh.noncePerConns, ctx.Conn) | ||
} | ||
|
||
// called when a session is opened. | ||
func (sh *serverHandler) OnSessionOpen(ctx *gortsplib.ServerHandlerOnSessionOpenCtx) { | ||
log.Printf("session opened") | ||
} | ||
|
||
// called when a session is closed. | ||
func (sh *serverHandler) OnSessionClose(ctx *gortsplib.ServerHandlerOnSessionCloseCtx) { | ||
log.Printf("session closed") | ||
|
||
sh.mutex.Lock() | ||
defer sh.mutex.Unlock() | ||
|
||
// if the session is the publisher, | ||
// close the stream and disconnect any reader. | ||
if sh.stream != nil && ctx.Session == sh.publisher { | ||
sh.stream.Close() | ||
sh.stream = nil | ||
} | ||
} | ||
|
||
// called when receiving a DESCRIBE request. | ||
func (sh *serverHandler) OnDescribe(ctx *gortsplib.ServerHandlerOnDescribeCtx) (*base.Response, *gortsplib.ServerStream, error) { | ||
log.Printf("describe request") | ||
|
||
res, err := ctx.Conn.ValidateCredentials(ctx.Request, readUser, readPass, nil, nil) | ||
if err != nil { | ||
return res, nil, err | ||
} | ||
|
||
sh.mutex.Lock() | ||
defer sh.mutex.Unlock() | ||
|
||
// no one is publishing yet | ||
if sh.stream == nil { | ||
return &base.Response{ | ||
StatusCode: base.StatusNotFound, | ||
}, nil, nil | ||
} | ||
|
||
// send medias that are being published to the client | ||
return &base.Response{ | ||
StatusCode: base.StatusOK, | ||
}, sh.stream, nil | ||
} | ||
|
||
// called when receiving an ANNOUNCE request. | ||
func (sh *serverHandler) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (*base.Response, error) { | ||
log.Printf("announce request") | ||
|
||
res, err := ctx.Conn.ValidateCredentials(ctx.Request, publishUser, publishPass, nil, nil) | ||
if err != nil { | ||
return res, err | ||
} | ||
|
||
sh.mutex.Lock() | ||
defer sh.mutex.Unlock() | ||
|
||
// disconnect existing publisher | ||
if sh.stream != nil { | ||
sh.stream.Close() | ||
sh.publisher.Close() | ||
} | ||
|
||
// create the stream and save the publisher | ||
sh.stream = gortsplib.NewServerStream(sh.s, ctx.Description) | ||
sh.publisher = ctx.Session | ||
|
||
return &base.Response{ | ||
StatusCode: base.StatusOK, | ||
}, nil | ||
} | ||
|
||
// called when receiving a SETUP request. | ||
func (sh *serverHandler) OnSetup(ctx *gortsplib.ServerHandlerOnSetupCtx) (*base.Response, *gortsplib.ServerStream, error) { | ||
log.Printf("setup request") | ||
|
||
res, err := ctx.Conn.ValidateCredentials(ctx.Request, readUser, readPass, nil, nil) | ||
if err != nil { | ||
return res, nil, err | ||
} | ||
|
||
// no one is publishing yet | ||
if sh.stream == nil { | ||
return &base.Response{ | ||
StatusCode: base.StatusNotFound, | ||
}, nil, nil | ||
} | ||
|
||
return &base.Response{ | ||
StatusCode: base.StatusOK, | ||
}, sh.stream, nil | ||
} | ||
|
||
// called when receiving a PLAY request. | ||
func (sh *serverHandler) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response, error) { | ||
log.Printf("play request") | ||
|
||
return &base.Response{ | ||
StatusCode: base.StatusOK, | ||
}, nil | ||
} | ||
|
||
// called when receiving a RECORD request. | ||
func (sh *serverHandler) OnRecord(ctx *gortsplib.ServerHandlerOnRecordCtx) (*base.Response, error) { | ||
log.Printf("record request") | ||
|
||
// called when receiving a RTP packet | ||
ctx.Session.OnPacketRTPAny(func(medi *description.Media, forma format.Format, pkt *rtp.Packet) { | ||
// route the RTP packet to all readers | ||
sh.stream.WritePacketRTP(medi, pkt) | ||
}) | ||
|
||
return &base.Response{ | ||
StatusCode: base.StatusOK, | ||
}, nil | ||
} | ||
|
||
func main() { | ||
// configure the server | ||
h := &serverHandler{ | ||
noncePerConns: make(map[*gortsplib.ServerConn]string), | ||
} | ||
h.s = &gortsplib.Server{ | ||
Handler: h, | ||
RTSPAddress: ":8554", | ||
UDPRTPAddress: ":8000", | ||
UDPRTCPAddress: ":8001", | ||
MulticastIPRange: "224.1.0.0/16", | ||
MulticastRTPPort: 8002, | ||
MulticastRTCPPort: 8003, | ||
} | ||
|
||
// start server and wait until a fatal error | ||
log.Printf("server is ready") | ||
panic(h.s.StartAndWait()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.