gortsplib

package module
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 28 Imported by: 0

README

gortsplib

Test Lint Go Report Card CodeCov PkgGoDev

RTSP 1.0 client and server library for the Go programming language, written for rtsp-simple-server.

Go ≥ 1.14 is required.

Features:

  • Client
    • Query servers about available streams
    • Read
      • Read streams from servers with UDP, UDP-multicast, TCP or TLS
      • Switch protocol automatically (switch to TCP in case of server error or UDP timeout)
      • Read only selected tracks of a stream
      • Pause or seek without disconnecting from the server
      • Generate RTCP receiver reports automatically
    • Publish
      • Publish streams to servers with UDP, TCP or TLS
      • Switch protocol automatically (switch to TCP in case of server error)
      • Pause without disconnecting from the server
      • Generate RTCP sender reports automatically
  • Server
    • Handle requests from clients
    • Sessions and connections are independent
    • Read streams from clients with UDP, TCP or TLS
    • Write streams to clients with UDP, UDP-multicast, TCP or TLS
    • Provide SSRC, RTP-Info to clients automatically
    • Generate RTCP receiver reports automatically
  • Utilities
    • Encode and decode RTSP primitives, RTP/H264, RTP/AAC, SDP

Table of contents

Examples

API Documentation

https://pkg.go.dev/github.com/aler9/gortsplib#pkg-index

Related projects

IETF Standards

Conventions

Documentation

Overview

Package gortsplib is a RTSP 1.0 library for the Go programming language, written for rtsp-simple-server.

Examples are available at https://github.com/aler9/gortsplib/tree/master/examples

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = &Client{}

DefaultClient is the default Client.

Functions

This section is empty.

Types

type Client

type Client struct {
	//
	// connection
	//
	// timeout of read operations.
	// It defaults to 10 seconds.
	ReadTimeout time.Duration
	// timeout of write operations.
	// It defaults to 10 seconds.
	WriteTimeout time.Duration
	// a TLS configuration to connect to TLS (RTSPS) servers.
	// It defaults to &tls.Config{InsecureSkipVerify:true}
	TLSConfig *tls.Config

	//
	// initialization
	//
	// disable being redirected to other servers, that can happen during Describe().
	// It defaults to false.
	RedirectDisable bool
	// enable communication with servers which don't provide server ports.
	// this can be a security issue.
	// It defaults to false.
	AnyPortEnable bool

	//
	// reading / writing
	//
	// the stream protocol (UDP, Multicast or TCP).
	// If nil, it is chosen automatically (first UDP, then, if it fails, TCP).
	// It defaults to nil.
	Protocol *ClientProtocol
	// If the client is reading with UDP, it must receive
	// at least a packet within this timeout.
	// It defaults to 3 seconds.
	InitialUDPReadTimeout time.Duration
	// read buffer count.
	// If greater than 1, allows to pass buffers to routines different than the one
	// that is reading frames.
	// It defaults to 1.
	ReadBufferCount int
	// read buffer size.
	// This must be touched only when the server reports problems about buffer sizes.
	// It defaults to 2048.
	ReadBufferSize int

	//
	// callbacks
	//
	// callback called before every request.
	OnRequest func(req *base.Request)
	// callback called after every response.
	OnResponse func(res *base.Response)

	//
	// system functions
	//
	// function used to initialize the TCP client.
	// It defaults to (&net.Dialer{}).DialContext.
	DialContext func(ctx context.Context, network, address string) (net.Conn, error)
	// function used to initialize UDP listeners.
	// It defaults to net.ListenPacket.
	ListenPacket func(network, address string) (net.PacketConn, error)
	// contains filtered or unexported fields
}

Client is a RTSP client.

func (*Client) Dial

func (c *Client) Dial(scheme string, host string) (*ClientConn, error)

Dial connects to a server.

func (*Client) DialPublish

func (c *Client) DialPublish(address string, tracks Tracks) (*ClientConn, error)

DialPublish connects to the address and starts publishing the tracks.

func (*Client) DialPublishContext

func (c *Client) DialPublishContext(ctx context.Context, address string, tracks Tracks) (*ClientConn, error)

DialPublishContext connects to the address with the given context and starts publishing the tracks.

func (*Client) DialRead

func (c *Client) DialRead(address string) (*ClientConn, error)

DialRead connects to the address and starts reading all tracks.

func (*Client) DialReadContext

func (c *Client) DialReadContext(ctx context.Context, address string) (*ClientConn, error)

DialReadContext connects to the address with the given context and starts reading all tracks.

type ClientConn

type ClientConn struct {
	// contains filtered or unexported fields
}

ClientConn is a client-side RTSP connection.

func Dial

func Dial(scheme string, host string) (*ClientConn, error)

Dial connects to a server.

func DialPublish

func DialPublish(address string, tracks Tracks) (*ClientConn, error)

DialPublish connects to a server and starts publishing the tracks.

func DialRead

func DialRead(address string) (*ClientConn, error)

DialRead connects to a server and starts reading all tracks.

func (*ClientConn) Announce

func (cc *ClientConn) Announce(u *base.URL, tracks Tracks) (*base.Response, error)

Announce writes an ANNOUNCE request and reads a Response.

func (*ClientConn) Close

func (cc *ClientConn) Close() error

Close closes the connection and waits for all its resources to exit.

func (*ClientConn) Describe

func (cc *ClientConn) Describe(u *base.URL) (Tracks, *base.URL, *base.Response, error)

Describe writes a DESCRIBE request and reads a Response.

func (*ClientConn) Options

func (cc *ClientConn) Options(u *base.URL) (*base.Response, error)

Options writes an OPTIONS request and reads a response.

func (*ClientConn) Pause

func (cc *ClientConn) Pause() (*base.Response, error)

Pause writes a PAUSE request and reads a Response. This can be called only after Play() or Record().

func (*ClientConn) Play

func (cc *ClientConn) Play(ra *headers.Range) (*base.Response, error)

Play writes a PLAY request and reads a Response. This can be called only after Setup().

func (*ClientConn) ReadFrames

func (cc *ClientConn) ReadFrames(onFrame func(int, StreamType, []byte)) error

ReadFrames starts reading frames.

func (*ClientConn) Record

func (cc *ClientConn) Record() (*base.Response, error)

Record writes a RECORD request and reads a Response. This can be called only after Announce() and Setup().

func (*ClientConn) Seek

func (cc *ClientConn) Seek(ra *headers.Range) (*base.Response, error)

Seek asks the server to re-start the stream from a specific timestamp.

func (*ClientConn) Setup

func (cc *ClientConn) Setup(
	mode headers.TransportMode,
	baseURL *base.URL,
	track *Track,
	rtpPort int,
	rtcpPort int) (*base.Response, error)

Setup writes a SETUP request and reads a Response. rtpPort and rtcpPort are used only if protocol is UDP. if rtpPort and rtcpPort are zero, they are chosen automatically.

func (*ClientConn) Tracks

func (cc *ClientConn) Tracks() Tracks

Tracks returns all the tracks that the connection is reading or publishing.

func (*ClientConn) WriteFrame

func (cc *ClientConn) WriteFrame(trackID int, streamType StreamType, payload []byte) error

WriteFrame writes a frame.

type ClientProtocol

type ClientProtocol int

ClientProtocol is a RTSP stream protocol used by the client.

const (
	ClientProtocolUDP ClientProtocol = iota
	ClientProtocolMulticast
	ClientProtocolTCP
)

standard RTSP stream protocols.

type Server

type Server struct {
	//
	// handler
	//
	// an handler interface to handle requests.
	Handler ServerHandler

	//
	// RTSP parameters
	//
	// timeout of read operations.
	// It defaults to 10 seconds
	ReadTimeout time.Duration
	// timeout of write operations.
	// It defaults to 10 seconds
	WriteTimeout time.Duration
	// a TLS configuration to accept TLS (RTSPS) connections.
	TLSConfig *tls.Config
	// a port to send and receive RTP packets with UDP.
	// If UDPRTPAddress and UDPRTCPAddress are filled, the server can read and write UDP streams.
	UDPRTPAddress string
	// a port to send and receive RTCP packets with UDP.
	// If UDPRTPAddress and UDPRTCPAddress are filled, the server can read and write UDP streams.
	UDPRTCPAddress string
	// a range of multicast IPs to use.
	// If MulticastIPRange, MulticastRTPPort, MulticastRTCPPort are filled, the server can read and write UDP-multicast streams.
	MulticastIPRange string
	// a port to send RTP packets with UDP-multicast.
	// If MulticastIPRange, MulticastRTPPort, MulticastRTCPPort are filled, the server can read and write UDP-multicast streams.
	MulticastRTPPort int
	// a port to send RTCP packets with UDP-multicast.
	// If MulticastIPRange, MulticastRTPPort, MulticastRTCPPort are filled, the server can read and write UDP-multicast streams.
	MulticastRTCPPort int
	// read buffer count.
	// If greater than 1, allows to pass buffers to routines different than the one
	// that is reading frames.
	// It also allows to buffer routed frames and mitigate network fluctuations
	// that are particularly high when using UDP.
	// It defaults to 512
	ReadBufferCount int
	// read buffer size.
	// This must be touched only when the server reports problems about buffer sizes.
	// It defaults to 2048.
	ReadBufferSize int

	//
	// system functions
	//
	// function used to initialize the TCP listener.
	// It defaults to net.Listen.
	Listen func(network string, address string) (net.Listener, error)
	// function used to initialize UDP listeners.
	// It defaults to net.ListenPacket.
	ListenPacket func(network, address string) (net.PacketConn, error)
	// contains filtered or unexported fields
}

Server is a RTSP server.

func (*Server) Close

func (s *Server) Close() error

Close closes the server and waits for all its resources to exit.

func (*Server) Start

func (s *Server) Start(address string) error

Start starts listening on the given address.

func (*Server) StartAndWait

func (s *Server) StartAndWait(address string) error

StartAndWait starts the server and waits until a fatal error.

func (*Server) Wait

func (s *Server) Wait() error

Wait waits until a fatal error.

type ServerConn

type ServerConn struct {
	// contains filtered or unexported fields
}

ServerConn is a server-side RTSP connection.

func (*ServerConn) Close

func (sc *ServerConn) Close() error

Close closes the ServerConn.

func (*ServerConn) NetConn

func (sc *ServerConn) NetConn() net.Conn

NetConn returns the underlying net.Conn.

type ServerHandler

type ServerHandler interface{}

ServerHandler is the interface implemented by all the server handlers.

type ServerHandlerOnAnnounce

type ServerHandlerOnAnnounce interface {
	OnAnnounce(*ServerHandlerOnAnnounceCtx) (*base.Response, error)
}

ServerHandlerOnAnnounce can be implemented by a ServerHandler.

type ServerHandlerOnAnnounceCtx

type ServerHandlerOnAnnounceCtx struct {
	Server  *Server
	Session *ServerSession
	Conn    *ServerConn
	Req     *base.Request
	Path    string
	Query   string
	Tracks  Tracks
}

ServerHandlerOnAnnounceCtx is the context of an ANNOUNCE request.

type ServerHandlerOnConnClose

type ServerHandlerOnConnClose interface {
	OnConnClose(*ServerHandlerOnConnCloseCtx)
}

ServerHandlerOnConnClose can be implemented by a ServerHandler.

type ServerHandlerOnConnCloseCtx

type ServerHandlerOnConnCloseCtx struct {
	Conn  *ServerConn
	Error error
}

ServerHandlerOnConnCloseCtx is the context of a connection closure.

type ServerHandlerOnConnOpen

type ServerHandlerOnConnOpen interface {
	OnConnOpen(*ServerHandlerOnConnOpenCtx)
}

ServerHandlerOnConnOpen can be implemented by a ServerHandler.

type ServerHandlerOnConnOpenCtx

type ServerHandlerOnConnOpenCtx struct {
	Conn *ServerConn
}

ServerHandlerOnConnOpenCtx is the context of a connection opening.

type ServerHandlerOnDescribe

type ServerHandlerOnDescribe interface {
	OnDescribe(*ServerHandlerOnDescribeCtx) (*base.Response, *ServerStream, error)
}

ServerHandlerOnDescribe can be implemented by a ServerHandler.

type ServerHandlerOnDescribeCtx

type ServerHandlerOnDescribeCtx struct {
	Conn  *ServerConn
	Req   *base.Request
	Path  string
	Query string
}

ServerHandlerOnDescribeCtx is the context of a DESCRIBE request.

type ServerHandlerOnFrame

type ServerHandlerOnFrame interface {
	OnFrame(*ServerHandlerOnFrameCtx)
}

ServerHandlerOnFrame can be implemented by a ServerHandler.

type ServerHandlerOnFrameCtx

type ServerHandlerOnFrameCtx struct {
	Session    *ServerSession
	TrackID    int
	StreamType StreamType
	Payload    []byte
}

ServerHandlerOnFrameCtx is the context of a frame.

type ServerHandlerOnGetParameter

type ServerHandlerOnGetParameter interface {
	OnGetParameter(*ServerHandlerOnGetParameterCtx) (*base.Response, error)
}

ServerHandlerOnGetParameter can be implemented by a ServerHandler.

type ServerHandlerOnGetParameterCtx

type ServerHandlerOnGetParameterCtx struct {
	Session *ServerSession
	Conn    *ServerConn
	Req     *base.Request
	Path    string
	Query   string
}

ServerHandlerOnGetParameterCtx is the context of a GET_PARAMETER request.

type ServerHandlerOnPause

type ServerHandlerOnPause interface {
	OnPause(*ServerHandlerOnPauseCtx) (*base.Response, error)
}

ServerHandlerOnPause can be implemented by a ServerHandler.

type ServerHandlerOnPauseCtx

type ServerHandlerOnPauseCtx struct {
	Session *ServerSession
	Conn    *ServerConn
	Req     *base.Request
	Path    string
	Query   string
}

ServerHandlerOnPauseCtx is the context of a PAUSE request.

type ServerHandlerOnPlay

type ServerHandlerOnPlay interface {
	OnPlay(*ServerHandlerOnPlayCtx) (*base.Response, error)
}

ServerHandlerOnPlay can be implemented by a ServerHandler.

type ServerHandlerOnPlayCtx

type ServerHandlerOnPlayCtx struct {
	Session *ServerSession
	Conn    *ServerConn
	Req     *base.Request
	Path    string
	Query   string
}

ServerHandlerOnPlayCtx is the context of a PLAY request.

type ServerHandlerOnRecord

type ServerHandlerOnRecord interface {
	OnRecord(*ServerHandlerOnRecordCtx) (*base.Response, error)
}

ServerHandlerOnRecord can be implemented by a ServerHandler.

type ServerHandlerOnRecordCtx

type ServerHandlerOnRecordCtx struct {
	Session *ServerSession
	Conn    *ServerConn
	Req     *base.Request
	Path    string
	Query   string
}

ServerHandlerOnRecordCtx is the context of a RECORD request.

type ServerHandlerOnRequest

type ServerHandlerOnRequest interface {
	OnRequest(*ServerConn, *base.Request)
}

ServerHandlerOnRequest can be implemented by a ServerHandler.

type ServerHandlerOnResponse

type ServerHandlerOnResponse interface {
	OnResponse(*ServerConn, *base.Response)
}

ServerHandlerOnResponse can be implemented by a ServerHandler.

type ServerHandlerOnSessionClose

type ServerHandlerOnSessionClose interface {
	OnSessionClose(*ServerHandlerOnSessionCloseCtx)
}

ServerHandlerOnSessionClose can be implemented by a ServerHandler.

type ServerHandlerOnSessionCloseCtx

type ServerHandlerOnSessionCloseCtx struct {
	Session *ServerSession
	Error   error
}

ServerHandlerOnSessionCloseCtx is the context of a session closure.

type ServerHandlerOnSessionOpen

type ServerHandlerOnSessionOpen interface {
	OnSessionOpen(*ServerHandlerOnSessionOpenCtx)
}

ServerHandlerOnSessionOpen can be implemented by a ServerHandler.

type ServerHandlerOnSessionOpenCtx

type ServerHandlerOnSessionOpenCtx struct {
	Session *ServerSession
	Conn    *ServerConn
}

ServerHandlerOnSessionOpenCtx is the context of a session opening.

type ServerHandlerOnSetParameter

type ServerHandlerOnSetParameter interface {
	OnSetParameter(*ServerHandlerOnSetParameterCtx) (*base.Response, error)
}

ServerHandlerOnSetParameter can be implemented by a ServerHandler.

type ServerHandlerOnSetParameterCtx

type ServerHandlerOnSetParameterCtx struct {
	Conn  *ServerConn
	Req   *base.Request
	Path  string
	Query string
}

ServerHandlerOnSetParameterCtx is the context of a SET_PARAMETER request.

type ServerHandlerOnSetup

type ServerHandlerOnSetup interface {
	// must return a Response and a stream.
	// the stream is needed to
	// - add the session the the stream's readers
	// - send the stream SSRC to the session
	OnSetup(*ServerHandlerOnSetupCtx) (*base.Response, *ServerStream, error)
}

ServerHandlerOnSetup can be implemented by a ServerHandler.

type ServerHandlerOnSetupCtx

type ServerHandlerOnSetupCtx struct {
	Server    *Server
	Session   *ServerSession
	Conn      *ServerConn
	Req       *base.Request
	Path      string
	Query     string
	TrackID   int
	Transport *headers.Transport
}

ServerHandlerOnSetupCtx is the context of a OPTIONS request.

type ServerSession

type ServerSession struct {
	// contains filtered or unexported fields
}

ServerSession is a server-side RTSP session.

func (*ServerSession) AnnouncedTracks

func (ss *ServerSession) AnnouncedTracks() []ServerSessionAnnouncedTrack

AnnouncedTracks returns the announced tracks.

func (*ServerSession) Close

func (ss *ServerSession) Close() error

Close closes the ServerSession.

func (*ServerSession) ID

func (ss *ServerSession) ID() string

ID returns the ID of the session.

func (*ServerSession) SetuppedDelivery

func (ss *ServerSession) SetuppedDelivery() *base.StreamDelivery

SetuppedDelivery returns the delivery method of the setupped tracks.

func (*ServerSession) SetuppedProtocol

func (ss *ServerSession) SetuppedProtocol() *base.StreamProtocol

SetuppedProtocol returns the stream protocol of the setupped tracks.

func (*ServerSession) SetuppedTracks

func (ss *ServerSession) SetuppedTracks() map[int]ServerSessionSetuppedTrack

SetuppedTracks returns the setupped tracks.

func (*ServerSession) State

func (ss *ServerSession) State() ServerSessionState

State returns the state of the session.

func (*ServerSession) WriteFrame

func (ss *ServerSession) WriteFrame(trackID int, streamType StreamType, payload []byte)

WriteFrame writes a frame to the session.

type ServerSessionAnnouncedTrack

type ServerSessionAnnouncedTrack struct {
	// contains filtered or unexported fields
}

ServerSessionAnnouncedTrack is an announced track of a ServerSession.

type ServerSessionSetuppedTrack

type ServerSessionSetuppedTrack struct {
	// contains filtered or unexported fields
}

ServerSessionSetuppedTrack is a setupped track of a ServerSession.

type ServerSessionState

type ServerSessionState int

ServerSessionState is a state of a ServerSession.

const (
	ServerSessionStateInitial ServerSessionState = iota
	ServerSessionStatePrePlay
	ServerSessionStatePlay
	ServerSessionStatePreRecord
	ServerSessionStateRecord
)

standard states.

func (ServerSessionState) String

func (s ServerSessionState) String() string

String implements fmt.Stringer.

type ServerStream

type ServerStream struct {
	// contains filtered or unexported fields
}

ServerStream represents a single stream. This is in charge of - distributing the stream to each reader - allocating multicast listeners - gathering infos about the stream to generate SSRC and RTP-Info

func NewServerStream

func NewServerStream(tracks Tracks) *ServerStream

NewServerStream allocates a ServerStream.

func (*ServerStream) Close

func (st *ServerStream) Close() error

Close closes a ServerStream.

func (*ServerStream) Tracks

func (st *ServerStream) Tracks() Tracks

Tracks returns the tracks of the stream.

func (*ServerStream) WriteFrame

func (st *ServerStream) WriteFrame(trackID int, streamType StreamType, payload []byte)

WriteFrame writes a frame to all the readers of the stream.

type StreamType

type StreamType = base.StreamType

StreamType is the stream type.

const (
	// StreamTypeRTP means that the stream contains RTP packets
	StreamTypeRTP StreamType = base.StreamTypeRTP

	// StreamTypeRTCP means that the stream contains RTCP packets
	StreamTypeRTCP StreamType = base.StreamTypeRTCP
)

type Track

type Track struct {
	// attributes in SDP format
	Media *psdp.MediaDescription
}

Track is a RTSP track.

func NewTrackAAC

func NewTrackAAC(payloadType uint8, config []byte) (*Track, error)

NewTrackAAC initializes an AAC track from a configuration.

func NewTrackH264

func NewTrackH264(payloadType uint8, sps []byte, pps []byte) (*Track, error)

NewTrackH264 initializes an H264 track from a SPS and PPS.

func (*Track) ClockRate

func (t *Track) ClockRate() (int, error)

ClockRate returns the clock rate of the track.

func (*Track) ExtractDataAAC

func (t *Track) ExtractDataAAC() ([]byte, error)

ExtractDataAAC extracts the config from an AAC track.

func (*Track) ExtractDataH264

func (t *Track) ExtractDataH264() ([]byte, []byte, error)

ExtractDataH264 extracts the SPS and PPS from an H264 track.

func (*Track) IsAAC

func (t *Track) IsAAC() bool

IsAAC checks whether the track is an AAC track.

func (*Track) IsH264

func (t *Track) IsH264() bool

IsH264 checks whether the track is a H264 track.

func (*Track) URL

func (t *Track) URL(baseURL *base.URL) (*base.URL, error)

URL returns the track url.

type Tracks

type Tracks []*Track

Tracks is a list of tracks.

func ReadTracks

func ReadTracks(byts []byte) (Tracks, error)

ReadTracks decodes tracks from SDP.

func (Tracks) Write

func (ts Tracks) Write() []byte

Write encodes tracks into SDP.

Directories

Path Synopsis
examples
pkg
auth
Package auth contains utilities to perform authentication.
Package auth contains utilities to perform authentication.
base
Package base contains the base elements of the RTSP protocol.
Package base contains the base elements of the RTSP protocol.
headers
Package headers contains various RTSP headers.
Package headers contains various RTSP headers.
liberrors
Package liberrors contains errors returned by the library.
Package liberrors contains errors returned by the library.
multibuffer
Package multibuffer contains a buffer with multiple levels.
Package multibuffer contains a buffer with multiple levels.
ringbuffer
Package ringbuffer contains a ring buffer.
Package ringbuffer contains a ring buffer.
rtcpreceiver
Package rtcpreceiver contains a utility to generate RTCP receiver reports.
Package rtcpreceiver contains a utility to generate RTCP receiver reports.
rtcpsender
Package rtcpsender contains a utility to generate RTCP sender reports.
Package rtcpsender contains a utility to generate RTCP sender reports.
rtpaac
Package rtpaac contains a RTP/AAC decoder and encoder.
Package rtpaac contains a RTP/AAC decoder and encoder.
rtph264
Package rtph264 contains a RTP/H264 decoder and encoder.
Package rtph264 contains a RTP/H264 decoder and encoder.
sdp
Package sdp contains a SDP encoder/decoder compatible with most RTSP implementations.
Package sdp contains a SDP encoder/decoder compatible with most RTSP implementations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL