av/protocol/rtsp/rtsp.go

247 lines
6.2 KiB
Go

/*
NAME
rtsp.go
DESCRIPTION
rtsp.go provides functionality for forming and sending RTSP requests for
methods, DESCRIBE, OPTIONS, SETUP and PLAY, as described by
the RTSP standards, see https://tools.ietf.org/html/rfc7826
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.
PRE-EXISTING LICENSE
Copyright (c) 2015, T. Jameson Little <t.jameson.little@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package rtsp
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)
// RTSP methods.
const (
describe = "DESCRIBE"
options = "OPTIONS"
play = "PLAY"
setup = "SETUP"
)
// Minimum response size to be considered valid.
const minResponse = 2
var errSmallResponse = errors.New("response too small")
// Request describes an RTSP request.
type Request struct {
Method string
URL *url.URL
Proto string
ProtoMajor int
ProtoMinor int
Header http.Header
ContentLength int
Body io.ReadCloser
}
// NewRequest returns a pointer to a new Request.
func NewRequest(method, cSeq string, u *url.URL, body io.ReadCloser) (*Request, error) {
req := &Request{
Method: method,
URL: u,
Proto: "RTSP",
ProtoMajor: 1,
ProtoMinor: 0,
Header: map[string][]string{"CSeq": []string{cSeq}},
Body: body,
}
return req, nil
}
// Write writes the request r to the given io.Writer w.
func (r *Request) Write(w io.Writer) error {
_, err := w.Write([]byte(r.String()))
return err
}
// String returns a formatted string of the Request.
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
}
// Response describes an RTSP response.
type Response struct {
Proto string
ProtoMajor int
ProtoMinor int
StatusCode int
Status string
ContentLength int64
Header http.Header
Body io.ReadCloser
}
// String returns a formatted string of the Response.
func (r Response) String() string {
s := fmt.Sprintf("%s/%d.%d %d %s\n", r.Proto, r.ProtoMajor, r.ProtoMinor, r.StatusCode, r.Status)
for k, v := range r.Header {
for _, v := range v {
s += fmt.Sprintf("%s: %s\n", k, v)
}
}
return s
}
// ReadResponse will read the response of the RTSP request from the connection,
// and return a pointer to a new Response.
func ReadResponse(r io.Reader) (*Response, error) {
resp := &Response{Header: make(map[string][]string)}
b := bufio.NewReader(r)
// Read the first line.
s, err := b.ReadString('\n')
if err != nil {
return nil, err
}
if len(s) < minResponse {
return nil, errSmallResponse
}
// Check that it was terminated by CRLF.
if []byte(s)[len(s)-2] != '\r' {
return nil, errors.New("line not terminated by CRLF")
}
// Split first line.
s1 := strings.SplitN(s, " ", 3)
// Get protocol.
s2 := strings.SplitN(s1[0], "/", 2)
resp.Proto = s2[0]
// Get major version.
s3 := strings.SplitN(s2[1], ".", 2)
resp.ProtoMajor, err = strconv.Atoi(s3[0])
if err != nil {
return nil, err
}
// Get minor version.
resp.ProtoMinor, err = strconv.Atoi(s3[1])
if err != nil {
return nil, err
}
// Get status code.
resp.StatusCode, err = strconv.Atoi(s1[1])
if err != nil {
return nil, err
}
// Get status
resp.Status = strings.TrimSpace(s1[2])
// Read headers.
for {
s, err := b.ReadString('\n')
if err != nil {
return nil, err
}
if strings.TrimRight(s, "\r\n") == "" {
break
}
parts := strings.SplitN(s, ":", 2)
resp.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
}
// Get the content length from the header.
resp.ContentLength, _ = strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 64)
resp.Body = closer{b, r}
return resp, nil
}
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
}