2019-04-26 07:27:18 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
client.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
client.go provides a Client type providing functionality to send RTSP requests
|
|
|
|
of methods DESCRIBE, OPTIONS, SETUP and PLAY to an RTSP server.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
2019-04-30 15:02:05 +03:00
|
|
|
|
2019-04-26 07:27:18 +03:00
|
|
|
package rtsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client describes an RTSP Client.
|
|
|
|
type Client struct {
|
2019-04-26 13:23:08 +03:00
|
|
|
cSeq int
|
|
|
|
addr string
|
|
|
|
url *url.URL
|
|
|
|
conn net.Conn
|
|
|
|
sessionID string
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
|
|
|
|
2019-05-20 13:15:59 +03:00
|
|
|
// NewClient returns a pointer to a new Client and the local address of the
|
|
|
|
// RTSP connection. The address addr will be parsed and a connection to the
|
|
|
|
// RTSP server will be made.
|
|
|
|
func NewClient(addr string) (c *Client, local, remote *net.TCPAddr, err error) {
|
|
|
|
c = &Client{addr: addr}
|
2019-04-27 18:20:17 +03:00
|
|
|
c.url, err = url.Parse(addr)
|
2019-04-26 07:27:18 +03:00
|
|
|
if err != nil {
|
2019-05-20 13:15:59 +03:00
|
|
|
return nil, nil,nil, err
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
2019-04-27 18:20:17 +03:00
|
|
|
c.conn, err = net.Dial("tcp", c.url.Host)
|
2019-04-26 07:27:18 +03:00
|
|
|
if err != nil {
|
2019-05-20 13:15:59 +03:00
|
|
|
return nil, nil, nil, err
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
2019-05-20 13:15:59 +03:00
|
|
|
local = c.conn.LocalAddr().(*net.TCPAddr)
|
|
|
|
remote = c.conn.RemoteAddr().(*net.TCPAddr)
|
|
|
|
return
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:44:23 +03:00
|
|
|
// Close closes the RTSP connection.
|
|
|
|
func (c *Client) Close() error {
|
|
|
|
return c.conn.Close()
|
|
|
|
}
|
|
|
|
|
2019-04-26 07:27:18 +03:00
|
|
|
// Describe forms and sends an RTSP request of method DESCRIBE to the RTSP server.
|
2019-04-26 08:39:59 +03:00
|
|
|
func (c *Client) Describe() (*Response, error) {
|
2019-04-27 18:23:47 +03:00
|
|
|
req, err := NewRequest("DESCRIBE", c.nextCSeq(), c.url, nil)
|
2019-04-26 08:39:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", "application/sdp")
|
|
|
|
return c.Do(req)
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Options forms and sends an RTSP request of method OPTIONS to the RTSP server.
|
2019-04-26 08:39:59 +03:00
|
|
|
func (c *Client) Options() (*Response, error) {
|
2019-04-27 18:23:47 +03:00
|
|
|
req, err := NewRequest("OPTIONS", c.nextCSeq(), c.url, nil)
|
2019-04-26 07:27:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 08:39:59 +03:00
|
|
|
return c.Do(req)
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 08:39:59 +03:00
|
|
|
// Setup forms and sends an RTSP request of method SETUP to the RTSP server.
|
|
|
|
func (c *Client) Setup(track, transport string) (*Response, error) {
|
2019-04-30 15:03:35 +03:00
|
|
|
u, err := url.Parse(c.addr + "/" + track)
|
2019-04-26 07:27:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:03:35 +03:00
|
|
|
req, err := NewRequest("SETUP", c.nextCSeq(), u, nil)
|
2019-04-26 08:39:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
2019-04-26 08:39:59 +03:00
|
|
|
req.Header.Add("Transport", transport)
|
2019-04-26 07:27:18 +03:00
|
|
|
|
2019-04-26 07:50:58 +03:00
|
|
|
resp, err := c.Do(req)
|
2019-04-26 07:27:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-26 13:23:08 +03:00
|
|
|
c.sessionID = resp.Header.Get("Session")
|
2019-04-26 08:39:59 +03:00
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
2019-04-26 07:27:18 +03:00
|
|
|
|
2019-04-26 08:39:59 +03:00
|
|
|
// Play forms and sends an RTSP request of method PLAY to the RTSP server
|
|
|
|
func (c *Client) Play() (*Response, error) {
|
2019-04-27 18:23:47 +03:00
|
|
|
req, err := NewRequest("PLAY", c.nextCSeq(), c.url, nil)
|
2019-04-26 08:39:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-26 07:50:58 +03:00
|
|
|
}
|
2019-04-26 13:23:08 +03:00
|
|
|
req.Header.Add("Session", c.sessionID)
|
2019-04-26 08:39:59 +03:00
|
|
|
|
|
|
|
return c.Do(req)
|
2019-04-26 07:50:58 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 06:09:58 +03:00
|
|
|
// Do sends the given RTSP request req, reads any responses and returns the response
|
2019-04-26 07:50:58 +03:00
|
|
|
// and any errors.
|
2019-04-27 18:24:44 +03:00
|
|
|
func (c *Client) Do(req *Request) (*Response, error) {
|
|
|
|
err := req.Write(c.conn)
|
2019-04-26 07:27:18 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-30 15:06:06 +03:00
|
|
|
resp, err := ReadResponse(c.conn)
|
2019-04-26 07:50:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
2019-04-26 07:50:58 +03:00
|
|
|
|
2019-04-29 06:09:58 +03:00
|
|
|
return resp, nil
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|
|
|
|
|
2019-04-29 06:09:58 +03:00
|
|
|
// nextCSeq provides the next CSeq number for the next RTSP request.
|
2019-04-26 08:39:59 +03:00
|
|
|
func (c *Client) nextCSeq() string {
|
|
|
|
c.cSeq++
|
|
|
|
return strconv.Itoa(c.cSeq)
|
2019-04-26 07:27:18 +03:00
|
|
|
}
|