av/protocol/rtsp/rtsp.go

270 lines
4.9 KiB
Go
Raw Normal View History

package rtsp
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
)
// rtsp protocol described by:
// https://tools.ietf.org/html/rfc7826
// RTSP methods.
const (
describe = "DESCRIBE"
options = "OPTIONS"
play = "PLAY"
setup = "SETUP"
)
type Request struct {
Method string
URL *url.URL
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
ContentLength int
Body io.ReadCloser
}
func (r Request) String() string {
s := fmt.Sprintf("%s %s %s/%d.%d\r\n", r.Method, r.URL, r.Proto, r.ProtoMajor, r.ProtoMinor)
for k, v := range r.Header {
for _, v := range v {
s += fmt.Sprintf("%s: %s\r\n", k, v)
}
}
s += "\r\n"
if r.Body != nil {
str, _ := ioutil.ReadAll(r.Body)
s += string(str)
}
return s
}
func NewRequest(method, urlStr, cSeq string, body io.ReadCloser) (*Request, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
req := &Request{
Method: method,
URL: u,
Proto: "RTSP",
ProtoMajor: 1,
ProtoMinor: 0,
Header: map[string][]string{"CSeq": []string{cSeq}},
Body: body,
}
return req, nil
}
type Session struct {
cSeq int
conn net.Conn
session string
}
func NewSession() *Session {
return &Session{}
}
func (s *Session) nextCSeq() string {
s.cSeq++
return strconv.Itoa(s.cSeq)
}
func (s *Session) Describe(urlStr string) (*Response, error) {
req, err := NewRequest(describe, urlStr, s.nextCSeq(), nil)
if err != nil {
panic(err)
}
req.Header.Add("Accept", "application/sdp")
if s.conn == nil {
s.conn, err = net.Dial("tcp", req.URL.Host)
if err != nil {
return nil, err
}
}
_, err = io.WriteString(s.conn, req.String())
if err != nil {
return nil, err
}
return ReadResponse(s.conn)
}
func (s *Session) Options(urlStr string) (*Response, error) {
req, err := NewRequest(options, urlStr, s.nextCSeq(), nil)
if err != nil {
panic(err)
}
if s.conn == nil {
s.conn, err = net.Dial("tcp", req.URL.Host)
if err != nil {
return nil, err
}
}
_, err = io.WriteString(s.conn, req.String())
if err != nil {
return nil, err
}
return ReadResponse(s.conn)
}
func (s *Session) Setup(urlStr, transport string) (*Response, error) {
req, err := NewRequest(setup, urlStr, s.nextCSeq(), nil)
if err != nil {
panic(err)
}
req.Header.Add("Transport", transport)
if s.conn == nil {
s.conn, err = net.Dial("tcp", req.URL.Host)
if err != nil {
return nil, err
}
}
_, err = io.WriteString(s.conn, req.String())
if err != nil {
return nil, err
}
resp, err := ReadResponse(s.conn)
s.session = resp.Header.Get("Session")
return resp, err
}
func (s *Session) Play(urlStr, sessionId string) (*Response, error) {
req, err := NewRequest(play, urlStr+"/", s.nextCSeq(), nil)
if err != nil {
panic(err)
}
req.Header.Add("Session", sessionId)
if s.conn == nil {
s.conn, err = net.Dial("tcp", req.URL.Host)
if err != nil {
return nil, err
}
}
_, err = io.WriteString(s.conn, req.String())
if err != nil {
return nil, err
}
return ReadResponse(s.conn)
}
type closer struct {
*bufio.Reader
r io.Reader
}
func (c closer) Close() error {
if c.Reader == nil {
return nil
}
defer func() {
c.Reader = nil
c.r = nil
}()
if r, ok := c.r.(io.ReadCloser); ok {
return r.Close()
}
return nil
}
func ParseRTSPVersion(s string) (proto string, major int, minor int, err error) {
parts := strings.SplitN(s, "/", 2)
proto = parts[0]
parts = strings.SplitN(parts[1], ".", 2)
if major, err = strconv.Atoi(parts[0]); err != nil {
return
}
if minor, err = strconv.Atoi(parts[0]); err != nil {
return
}
return
}
type Response struct {
Proto string
ProtoMajor int
ProtoMinor int
StatusCode int
Status string
ContentLength int64
Header http.Header
Body io.ReadCloser
}
func (res Response) String() string {
s := fmt.Sprintf("%s/%d.%d %d %s\n", res.Proto, res.ProtoMajor, res.ProtoMinor, res.StatusCode, res.Status)
for k, v := range res.Header {
for _, v := range v {
s += fmt.Sprintf("%s: %s\n", k, v)
}
}
return s
}
func ReadResponse(r io.Reader) (res *Response, err error) {
res = new(Response)
res.Header = make(map[string][]string)
b := bufio.NewReader(r)
var s string
// TODO: allow CR, LF, or CRLF
if s, err = b.ReadString('\n'); err != nil {
return
}
parts := strings.SplitN(s, " ", 3)
res.Proto, res.ProtoMajor, res.ProtoMinor, err = ParseRTSPVersion(parts[0])
if err != nil {
return
}
if res.StatusCode, err = strconv.Atoi(parts[1]); err != nil {
return
}
res.Status = strings.TrimSpace(parts[2])
// read headers
for {
if s, err = b.ReadString('\n'); err != nil {
return
} else if s = strings.TrimRight(s, "\r\n"); s == "" {
break
}
parts := strings.SplitN(s, ":", 2)
res.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
res.ContentLength, _ = strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64)
res.Body = closer{b, r}
return
}