This commit is contained in:
aler9
2020-01-26 16:18:41 +01:00
parent 473e05c17f
commit 9e5868a1b8
8 changed files with 30 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import (
"strconv" "strconv"
) )
// ConnClient is a client-side RTSP connection.
type ConnClient struct { type ConnClient struct {
nconn net.Conn nconn net.Conn
br *bufio.Reader br *bufio.Reader
@@ -16,6 +17,7 @@ type ConnClient struct {
authProv *authClientProvider authProv *authClientProvider
} }
// NewConnClient allocates a ConnClient.
func NewConnClient(nconn net.Conn) *ConnClient { func NewConnClient(nconn net.Conn) *ConnClient {
return &ConnClient{ return &ConnClient{
nconn: nconn, nconn: nconn,
@@ -24,22 +26,29 @@ func NewConnClient(nconn net.Conn) *ConnClient {
} }
} }
// NetConn returns the underlying new.Conn.
func (c *ConnClient) NetConn() net.Conn { func (c *ConnClient) NetConn() net.Conn {
return c.nconn return c.nconn
} }
// SetSession sets a Session header that is automatically inserted into every outgoing request.
func (c *ConnClient) SetSession(v string) { func (c *ConnClient) SetSession(v string) {
c.session = v c.session = v
} }
// EnableCseq allows to automatically insert the CSeq header into every outgoing request.
// CSeq is incremented after every request.
func (c *ConnClient) EnableCseq() { func (c *ConnClient) EnableCseq() {
c.cseqEnabled = true c.cseqEnabled = true
} }
// SetCredentials allows to automatically insert Authenticate header into every outgoing request.
// The content of the header is computed with the given user, password, realm and nonce.
func (c *ConnClient) SetCredentials(user string, pass string, realm string, nonce string) { func (c *ConnClient) SetCredentials(user string, pass string, realm string, nonce string) {
c.authProv = newAuthClientProvider(user, pass, realm, nonce) c.authProv = newAuthClientProvider(user, pass, realm, nonce)
} }
// WriteRequest writes a Request.
func (c *ConnClient) WriteRequest(req *Request) error { func (c *ConnClient) WriteRequest(req *Request) error {
if c.session != "" { if c.session != "" {
if req.Header == nil { if req.Header == nil {
@@ -63,14 +72,17 @@ func (c *ConnClient) WriteRequest(req *Request) error {
return req.write(c.bw) return req.write(c.bw)
} }
// ReadResponse reads a response.
func (c *ConnClient) ReadResponse() (*Response, error) { func (c *ConnClient) ReadResponse() (*Response, error) {
return readResponse(c.br) return readResponse(c.br)
} }
// ReadInterleavedFrame reads an InterleavedFrame.
func (c *ConnClient) ReadInterleavedFrame() (*InterleavedFrame, error) { func (c *ConnClient) ReadInterleavedFrame() (*InterleavedFrame, error) {
return readInterleavedFrame(c.br) return readInterleavedFrame(c.br)
} }
// WriteInterleavedFrame writes an InterleavedFrame.
func (c *ConnClient) WriteInterleavedFrame(frame *InterleavedFrame) error { func (c *ConnClient) WriteInterleavedFrame(frame *InterleavedFrame) error {
return frame.write(c.bw) return frame.write(c.bw)
} }

View File

@@ -5,12 +5,14 @@ import (
"net" "net"
) )
// ConnServer is a server-side RTSP connection.
type ConnServer struct { type ConnServer struct {
nconn net.Conn nconn net.Conn
br *bufio.Reader br *bufio.Reader
bw *bufio.Writer bw *bufio.Writer
} }
// NewConnServer allocates a ConnClient.
func NewConnServer(nconn net.Conn) *ConnServer { func NewConnServer(nconn net.Conn) *ConnServer {
return &ConnServer{ return &ConnServer{
nconn: nconn, nconn: nconn,
@@ -19,22 +21,27 @@ func NewConnServer(nconn net.Conn) *ConnServer {
} }
} }
// NetConn returns the underlying new.Conn.
func (s *ConnServer) NetConn() net.Conn { func (s *ConnServer) NetConn() net.Conn {
return s.nconn return s.nconn
} }
// ReadRequest reads a Request.
func (s *ConnServer) ReadRequest() (*Request, error) { func (s *ConnServer) ReadRequest() (*Request, error) {
return readRequest(s.br) return readRequest(s.br)
} }
// WriteResponse writes a response.
func (s *ConnServer) WriteResponse(res *Response) error { func (s *ConnServer) WriteResponse(res *Response) error {
return res.write(s.bw) return res.write(s.bw)
} }
// ReadInterleavedFrame reads an InterleavedFrame.
func (s *ConnServer) ReadInterleavedFrame() (*InterleavedFrame, error) { func (s *ConnServer) ReadInterleavedFrame() (*InterleavedFrame, error) {
return readInterleavedFrame(s.br) return readInterleavedFrame(s.br)
} }
// WriteInterleavedFrame writes an InterleavedFrame.
func (s *ConnServer) WriteInterleavedFrame(frame *InterleavedFrame) error { func (s *ConnServer) WriteInterleavedFrame(frame *InterleavedFrame) error {
return frame.write(s.bw) return frame.write(s.bw)
} }

View File

@@ -12,6 +12,7 @@ const (
_MAX_HEADER_VALUE_LENGTH = 255 _MAX_HEADER_VALUE_LENGTH = 255
) )
// Header is a RTSP reader, present in both Requests and Responses.
type Header map[string][]string type Header map[string][]string
func readHeader(rb *bufio.Reader) (Header, error) { func readHeader(rb *bufio.Reader) (Header, error) {

View File

@@ -6,11 +6,13 @@ import (
"strings" "strings"
) )
// HeaderAuth is an Authenticate or a WWWW-Authenticate header.
type HeaderAuth struct { type HeaderAuth struct {
Prefix string Prefix string
Values map[string]string Values map[string]string
} }
// ReadHeaderAuth parses an Authenticate or a WWW-Authenticate header.
func ReadHeaderAuth(in string) (*HeaderAuth, error) { func ReadHeaderAuth(in string) (*HeaderAuth, error) {
a := &HeaderAuth{ a := &HeaderAuth{
Values: make(map[string]string), Values: make(map[string]string),

View File

@@ -5,8 +5,10 @@ import (
"strings" "strings"
) )
// HeaderTransport is a Transport header.
type HeaderTransport map[string]struct{} type HeaderTransport map[string]struct{}
// ReadHeaderTransport parses a Transport header.
func ReadHeaderTransport(in string) HeaderTransport { func ReadHeaderTransport(in string) HeaderTransport {
th := make(map[string]struct{}) th := make(map[string]struct{})
for _, t := range strings.Split(in, ";") { for _, t := range strings.Split(in, ";") {
@@ -15,6 +17,7 @@ func ReadHeaderTransport(in string) HeaderTransport {
return th return th
} }
// GetValue gets a value from the header.
func (th HeaderTransport) GetValue(key string) string { func (th HeaderTransport) GetValue(key string) string {
prefix := key + "=" prefix := key + "="
for t := range th { for t := range th {
@@ -25,6 +28,7 @@ func (th HeaderTransport) GetValue(key string) string {
return "" return ""
} }
// GetPorts gets a given header value and parses its ports.
func (th HeaderTransport) GetPorts(key string) (int, int) { func (th HeaderTransport) GetPorts(key string) (int, int) {
val := th.GetValue(key) val := th.GetValue(key)
if val == "" { if val == "" {

View File

@@ -12,6 +12,8 @@ const (
_INTERLEAVED_FRAME_MAX_CONTENT_SIZE = (_INTERLEAVED_FRAME_MAX_SIZE - 4) _INTERLEAVED_FRAME_MAX_CONTENT_SIZE = (_INTERLEAVED_FRAME_MAX_SIZE - 4)
) )
// InterleavedFrame is a wrapper for sending and receiving binary data into RTSP connections.
// It is usually used to send RTP and RTCP with RTSP.
type InterleavedFrame struct { type InterleavedFrame struct {
Channel uint8 Channel uint8
Content []byte Content []byte

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
) )
// Request is a RTSP request.
type Request struct { type Request struct {
Method string Method string
Url string Url string

View File

@@ -6,6 +6,7 @@ import (
"strconv" "strconv"
) )
// Response is a RTSP response.
type Response struct { type Response struct {
StatusCode int StatusCode int
Status string Status string