rtsp

package module
v0.0.0-...-7a3db26 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2022 License: MIT Imports: 19 Imported by: 0

README

go-rtsp

Go Reference

RTSP Client

Features:

  • Authentication
    • Basic
    • Digest
  • Transport
    • UDP unicast
    • TCP
  • Media
    • mpeg4-generic
    • h.264
    • h.265

Installation

To install the library use the following command in the project directory:

go get github.com/cesbo/go-rtsp

Quick Start

type Handler struct{}
func (Handler) OnRTP(mediaID int, packet []byte) {}
func (Handler) OnRTCP(mediaID int, packet []byte) {}

handler := &Handler{}
ctx := context.Background()
url, _ := url.Parse("rtsp://user:pass@localhost:554/live")

rtspClient := &rtsp.Client{
    UseTCP:         false,
    URL:            url,
}
defer rtspClient.Close()

_ = rtspClient.Start(ctx)

for mediaID, sdpItem := range rtspClient.GetSDP() {
    if sdpItem.Media == nil {
        continue
    }

    if err := rtspClient.Setup(ctx, mediaID, sdpItem.URL); err != nil {
        return err
    }
}

_ = rtspClient.Play(ctx, handler)

Documentation

Index

Constants

View Source
const (
	MethodDescribe     = "DESCRIBE"
	MethodGetParameter = "GET_PARAMETER"
	MethodOptions      = "OPTIONS"
	MethodPlay         = "PLAY"
	MethodSetup        = "SETUP"
	MethodTeardown     = "TEARDOWN"
)

Method Definitions

View Source
const SdpMimeType = "application/sdp"

Variables

View Source
var (
	ErrClientClosed    = fmt.Errorf("client closed")
	ErrResponseTimeout = fmt.Errorf("response timeout")
)

Functions

This section is empty.

Types

type Auth

type Auth interface {
	// Header updates token and returns value for Authorization header.
	Header(method, uri string) string
}

func NewAuth

func NewAuth(u *url.URL, header string) Auth

type AuthBasic

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

The 'Basic' HTTP Authentication Scheme https://datatracker.ietf.org/doc/html/rfc7617

func NewAuthBasic

func NewAuthBasic(user, pass, _ string) *AuthBasic

func (*AuthBasic) Header

func (a *AuthBasic) Header(method, uri string) string

type AuthDigest

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

HTTP Digest Access Authentication https://datatracker.ietf.org/doc/html/rfc7616

func NewAuthDigest

func NewAuthDigest(login, password, header string) *AuthDigest

func (*AuthDigest) Header

func (a *AuthDigest) Header(method, uri string) string

type Client

type Client struct {
	UseTCP         bool
	URL            *url.URL
	UserAgent      string
	ConnectTimeout time.Duration
	RequestTimeout time.Duration
	// contains filtered or unexported fields
}

Real Time Streaming Protocol (RTSP) https://datatracker.ietf.org/doc/html/rfc2326

func (*Client) Close

func (c *Client) Close()

Close closes the connection and closes all transports. Should be called after Play() finished

func (*Client) GetSDP

func (c *Client) GetSDP() []*SdpItem

GetSDP returns SDP items

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping sends request to keep the connection alive

func (*Client) Play

func (c *Client) Play(ctx context.Context, handler MediaHandler) error

Play sends request to start the stream delivery. Waits for ctx.Done or any error on transport. For UDP transport it sends keep-alive requests each 30 second

func (*Client) SendRequest

func (c *Client) SendRequest(request *Request) error

SendRequest sends RTSP/1.0 request to the server.

func (*Client) Setup

func (c *Client) Setup(ctx context.Context, mediaID int, control *url.URL) error

Setup sends request to setup the stream delivery

func (*Client) Start

func (c *Client) Start(ctx context.Context) error

Start connectes to the RTSP server and get SDP

func (*Client) Teardown

func (c *Client) Teardown(ctx context.Context) error

Teardown sends request to stop the stream delivery

type Media

type Media interface {
	ParseFMTP(line string)
}

func NewMedia

func NewMedia(mediaType string, clockRate int) Media

type MediaH264

type MediaH264 struct {
	ClockRate         int
	PacketizationMode int
	ProfileLevelID    []byte
	SPS               []byte
	PPS               []byte
}

RTP Payload Format for H.264 Video https://datatracker.ietf.org/doc/html/rfc6184

func NewMediaH264

func NewMediaH264(clockRate int) *MediaH264

func (*MediaH264) ParseFMTP

func (m *MediaH264) ParseFMTP(line string)

type MediaH265

type MediaH265 struct {
	ClockRate int
	LevelID   int
	VPS       []byte
	SPS       []byte
	PPS       []byte
}

RTP Payload Format for High Efficiency Video Coding (HEVC) https://datatracker.ietf.org/doc/html/rfc7798

func NewMediaH265

func NewMediaH265(clockRate int) *MediaH265

func (*MediaH265) ParseFMTP

func (m *MediaH265) ParseFMTP(line string)

type MediaHandler

type MediaHandler interface {
	OnRTP(mediaID int, packet []byte)
	OnRTCP(mediaID int, packet []byte)
}

type MediaMPEG4

type MediaMPEG4 struct {
	ClockRate      int
	Mode           string
	ProfileLevelID int
	Config         []byte
}

RTP Payload Format for Transport of MPEG-4 Elementary Streams https://datatracker.ietf.org/doc/html/rfc3640

func NewMediaMPEG4

func NewMediaMPEG4(clockRate int) *MediaMPEG4

func (*MediaMPEG4) ParseFMTP

func (m *MediaMPEG4) ParseFMTP(line string)

type Request

type Request struct {
	Method string
	URL    *url.URL
	Header http.Header
}

func ReadRequest

func ReadRequest(r *bufio.Reader) (request *Request, err error)

ReadRequest reads request from the client.

type Response

type Response struct {
	Proto      string
	StatusCode int
	Status     string
	Header     http.Header
	Body       []byte
}

func ReadResponse

func ReadResponse(reader *bufio.Reader) (response *Response, err error)

ReadResponse reads a response from the server.

func (*Response) ReadBody

func (r *Response) ReadBody(reader *bufio.Reader) error

ReadBody reads the body after response.

type SdpItem

type SdpItem struct {
	Media     Media
	Port      int
	Transport string
	Format    int
	URL       *url.URL
}

SDP: Session Description Protocol https://datatracker.ietf.org/doc/html/rfc2327

func ParseSDP

func ParseSDP(control *url.URL, data []byte) ([]*SdpItem, error)

ParseSDP parses SDP from the bytes array. Returns list of SDP Items or error.

type Transport

type Transport interface {
	// Setup configures the transport.
	// Returns the transport protocol header for RTSP SETUP request
	Setup(mediaID int) (string, error)
	// Play starts the transport
	Play(handler MediaHandler)
	// Close closes the transport
	Close()
	// Err returns the transport error
	Err() <-chan error
}

type TransportTCP

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

func NewTransportTCP

func NewTransportTCP(reader *bufio.Reader) *TransportTCP

func (*TransportTCP) Close

func (t *TransportTCP) Close()

func (*TransportTCP) Err

func (t *TransportTCP) Err() <-chan error

func (*TransportTCP) Play

func (t *TransportTCP) Play(handler MediaHandler)

Play starts receigin RTP/RTCP packets.

func (*TransportTCP) Setup

func (t *TransportTCP) Setup(mediaID int) (string, error)

Setup prepares the transport and returns the transport parameters.

type TransportUDP

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

func NewTransportUDP

func NewTransportUDP() *TransportUDP

func (*TransportUDP) Close

func (t *TransportUDP) Close()

func (*TransportUDP) Err

func (t *TransportUDP) Err() <-chan error

func (*TransportUDP) Play

func (t *TransportUDP) Play(handler MediaHandler)

Play starts receigin RTP/RTCP packets.

func (*TransportUDP) Setup

func (t *TransportUDP) Setup(mediaID int) (string, error)

Setup prepares the transport and returns the transport parameters.

Jump to

Keyboard shortcuts

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