gortsplib

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2021 License: MIT Imports: 23 Imported by: 0

README

gortsplib

Test Lint 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
    • Read streams from servers with UDP or TCP
    • Publish streams to servers with UDP or TCP
    • Encrypt streams with TLS (RTSPS)
    • Query servers about published streams
    • Read only selected tracks of a stream
    • Pause reading or publishing without disconnecting from the server
  • Server
    • Handle requests from clients
    • Read streams from clients with UDP or TCP
    • Send streams to clients with UDP or TCP
    • Encrypt streams with TLS (RTSPS)
  • General
    • RTCP reports are generated automatically
    • Encode and decode RTSP primitives, RTP/H264, RTP/AAC, SDP

Table of contents

Examples

API Documentation

https://pkg.go.dev/github.com/majoyz/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/majoyz/gortsplib/tree/master/examples

Index

Constants

This section is empty.

Variables

View Source
var DefaultClientConf = ClientConf{}

DefaultClientConf is the default ClientConf.

View Source
var DefaultServerConf = ServerConf{}

DefaultServerConf is the default ServerConf.

Functions

This section is empty.

Types

type ClientConf

type ClientConf struct {
	// the stream protocol (UDP or TCP).
	// If nil, it is chosen automatically (first UDP, then, if it fails, TCP).
	// It defaults to nil.
	StreamProtocol *StreamProtocol

	// a TLS configuration to connect to TLS (RTSPS) servers.
	// It defaults to &tls.Config{InsecureSkipVerify:true}
	TLSConfig *tls.Config

	// 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

	// timeout of read operations.
	// It defaults to 10 seconds.
	ReadTimeout time.Duration

	// timeout of write operations.
	// It defaults to 10 seconds.
	WriteTimeout 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

	// callback called before every request.
	OnRequest func(req *base.Request)

	// callback called after very response.
	OnResponse func(res *base.Response)

	// function used to initialize the TCP client.
	// It defaults to net.DialTimeout.
	DialTimeout func(network, address string, timeout time.Duration) (net.Conn, error)

	// function used to initialize UDP listeners.
	// It defaults to net.ListenPacket.
	ListenPacket func(network, address string) (net.PacketConn, error)
}

ClientConf allows to initialize a ClientConn. All fields are optional.

func (ClientConf) Dial

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

Dial connects to a server.

func (ClientConf) DialPublish

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

DialPublish connects to the address and starts publishing the tracks.

func (ClientConf) DialRead

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

DialRead connects to the address 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 (c *ClientConn) Announce(u *base.URL, tracks Tracks) (*base.Response, error)

Announce writes an ANNOUNCE request and reads a Response.

func (*ClientConn) Close

func (c *ClientConn) Close() error

Close closes all the ClientConn resources.

func (*ClientConn) Describe

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

Describe writes a DESCRIBE request and reads a Response.

func (*ClientConn) Do

func (c *ClientConn) Do(req *base.Request) (*base.Response, error)

Do writes a Request and reads a Response. Interleaved frames received before the response are ignored.

func (*ClientConn) NetConn

func (c *ClientConn) NetConn() net.Conn

NetConn returns the underlying net.Conn.

func (*ClientConn) Options

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

Options writes an OPTIONS request and reads a response.

func (*ClientConn) Pause

func (c *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 (c *ClientConn) Play() (*base.Response, error)

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

func (*ClientConn) RTPInfo

func (c *ClientConn) RTPInfo() *headers.RTPInfo

RTPInfo returns the RTP-Info header sent by the server in the PLAY response.

func (*ClientConn) ReadFrames

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

ReadFrames starts reading frames. it returns a channel that is written when the reading stops. This can be called only after Play().

func (*ClientConn) Record

func (c *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) Setup

func (c *ClientConn) Setup(mode headers.TransportMode, 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 (c *ClientConn) Tracks() Tracks

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

func (*ClientConn) WriteFrame

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

WriteFrame writes a frame. This can be called only after Record().

type Server

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

Server is a RTSP server.

func Serve

func Serve(address string) (*Server, error)

Serve starts a server on the given address.

func (*Server) Accept

func (s *Server) Accept() (*ServerConn, error)

Accept accepts a connection.

func (*Server) Close

func (s *Server) Close() error

Close closes the server.

type ServerConf

type ServerConf struct {
	// a TLS configuration to accept TLS (RTSPS) connections.
	TLSConfig *tls.Config

	// a port to send and receive UDP/RTP packets.
	// If UDPRTPAddress and UDPRTCPAddress are != "", the server can accept and send UDP streams.
	UDPRTPAddress string

	// a port to send and receive UDP/RTCP packets.
	// If UDPRTPAddress and UDPRTCPAddress are != "", the server can accept and send UDP streams.
	UDPRTCPAddress string

	// timeout of read operations.
	// It defaults to 10 seconds
	ReadTimeout time.Duration

	// timeout of write operations.
	// It defaults to 10 seconds
	WriteTimeout time.Duration

	// 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

	// function used to initialize the TCP listener.
	// It defaults to net.Listen
	Listen func(network string, address string) (net.Listener, error)
}

ServerConf allows to configure a Server. All fields are optional.

func (ServerConf) Serve

func (c ServerConf) Serve(address string) (*Server, error)

Serve starts a server on the given address.

type ServerConn

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

ServerConn is a server-side RTSP connection.

func (*ServerConn) AnnouncedTracks

func (sc *ServerConn) AnnouncedTracks() []ServerConnAnnouncedTrack

AnnouncedTracks returns the announced tracks.

func (*ServerConn) Close

func (sc *ServerConn) Close() error

Close closes all the connection resources.

func (*ServerConn) NetConn

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

NetConn returns the underlying net.Conn.

func (*ServerConn) Read

func (sc *ServerConn) Read(readHandlers ServerConnReadHandlers) chan error

Read starts reading requests and frames. it returns a channel that is written when the reading stops.

func (*ServerConn) SetuppedTracks

func (sc *ServerConn) SetuppedTracks() map[int]ServerConnSetuppedTrack

SetuppedTracks returns the setupped tracks.

func (*ServerConn) State

func (sc *ServerConn) State() ServerConnState

State returns the state.

func (*ServerConn) StreamProtocol

func (sc *ServerConn) StreamProtocol() *StreamProtocol

StreamProtocol returns the stream protocol of the setupped tracks.

func (*ServerConn) WriteFrame

func (sc *ServerConn) WriteFrame(trackID int, streamType StreamType, payload []byte)

WriteFrame writes a frame.

type ServerConnAnnounceCtx

type ServerConnAnnounceCtx struct {
	Req    *base.Request
	Path   string
	Query  string
	Tracks Tracks
}

ServerConnAnnounceCtx is the context of a ANNOUNCE request.

type ServerConnAnnouncedTrack

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

ServerConnAnnouncedTrack is an announced track of a ServerConn.

type ServerConnDescribeCtx

type ServerConnDescribeCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnDescribeCtx is the context of a DESCRIBE request.

type ServerConnGetParameterCtx

type ServerConnGetParameterCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnGetParameterCtx is the context of a GET_PARAMETER request.

type ServerConnOptionsCtx

type ServerConnOptionsCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnOptionsCtx is the context of a OPTIONS request.

type ServerConnPauseCtx

type ServerConnPauseCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnPauseCtx is the context of a PAUSE request.

type ServerConnPlayCtx

type ServerConnPlayCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnPlayCtx is the context of a PLAY request.

type ServerConnReadHandlers

type ServerConnReadHandlers struct {
	// called after receiving any request.
	OnRequest func(req *base.Request)

	// called before sending any response.
	OnResponse func(res *base.Response)

	// called after receiving a OPTIONS request.
	// if nil, it is generated automatically.
	OnOptions func(ctx *ServerConnOptionsCtx) (*base.Response, error)

	// called after receiving a DESCRIBE request.
	// the 2nd return value is a SDP, that is inserted into the response.
	OnDescribe func(ctx *ServerConnDescribeCtx) (*base.Response, []byte, error)

	// called after receiving an ANNOUNCE request.
	OnAnnounce func(ctx *ServerConnAnnounceCtx) (*base.Response, error)

	// called after receiving a SETUP request.
	OnSetup func(ctx *ServerConnSetupCtx) (*base.Response, error)

	// called after receiving a PLAY request.
	OnPlay func(ctx *ServerConnPlayCtx) (*base.Response, error)

	// called after receiving a RECORD request.
	OnRecord func(ctx *ServerConnRecordCtx) (*base.Response, error)

	// called after receiving a PAUSE request.
	OnPause func(ctx *ServerConnPauseCtx) (*base.Response, error)

	// called after receiving a GET_PARAMETER request.
	// if nil, it is generated automatically.
	OnGetParameter func(ctx *ServerConnGetParameterCtx) (*base.Response, error)

	// called after receiving a SET_PARAMETER request.
	OnSetParameter func(ctx *ServerConnSetParameterCtx) (*base.Response, error)

	// called after receiving a TEARDOWN request.
	// if nil, it is generated automatically.
	OnTeardown func(ctx *ServerConnTeardownCtx) (*base.Response, error)

	// called after receiving a frame.
	OnFrame func(trackID int, streamType StreamType, payload []byte)
}

ServerConnReadHandlers allows to set the handlers required by ServerConn.Read. all fields are optional.

type ServerConnRecordCtx

type ServerConnRecordCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnRecordCtx is the context of a RECORD request.

type ServerConnSetParameterCtx

type ServerConnSetParameterCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnSetParameterCtx is the context of a SET_PARAMETER request.

type ServerConnSetupCtx

type ServerConnSetupCtx struct {
	Req       *base.Request
	Path      string
	Query     string
	TrackID   int
	Transport *headers.Transport
}

ServerConnSetupCtx is the context of a OPTIONS request.

type ServerConnSetuppedTrack

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

ServerConnSetuppedTrack is a setupped track of a ServerConn.

type ServerConnState

type ServerConnState int

ServerConnState is the state of the connection.

const (
	ServerConnStateInitial ServerConnState = iota
	ServerConnStatePrePlay
	ServerConnStatePlay
	ServerConnStatePreRecord
	ServerConnStateRecord
)

standard states.

func (ServerConnState) String

func (s ServerConnState) String() string

String implements fmt.Stringer.

type ServerConnTeardownCtx

type ServerConnTeardownCtx struct {
	Req   *base.Request
	Path  string
	Query string
}

ServerConnTeardownCtx is the context of a TEARDOWN request.

type StreamProtocol

type StreamProtocol = base.StreamProtocol

StreamProtocol is the protocol of a stream.

const (
	// StreamProtocolUDP means that the stream uses the UDP protocol
	StreamProtocolUDP StreamProtocol = base.StreamProtocolUDP

	// StreamProtocolTCP means that the stream uses the TCP protocol
	StreamProtocolTCP StreamProtocol = base.StreamProtocolTCP
)

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 {
	// base URL
	BaseURL *base.URL

	// id
	ID int

	// codec and info in SDP format
	Media *psdp.MediaDescription
}

Track is a track available in a certain URL.

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 a 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() (*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, baseURL *base.URL) (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