2019-04-17 12:01:03 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
rtsp.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
rtsp.go provides functionality for forming and sending RTSP requests for
|
2019-04-25 09:00:28 +03:00
|
|
|
methods, DESCRIBE, OPTIONS, SETUP and PLAY, as described by
|
|
|
|
the RTSP standards, see https://tools.ietf.org/html/rfc7826
|
2019-04-17 12:01:03 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-04-23 09:03:07 +03:00
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean).
|
2019-04-17 12:01:03 +03:00
|
|
|
|
|
|
|
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
|
2019-04-23 09:03:07 +03:00
|
|
|
for more details.
|
2019-04-17 12:01:03 +03:00
|
|
|
|
|
|
|
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-23 09:03:07 +03:00
|
|
|
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.
|
2019-04-17 12:01:03 +03:00
|
|
|
*/
|
2019-04-16 16:17:13 +03:00
|
|
|
package rtsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-04-25 08:16:21 +03:00
|
|
|
"errors"
|
2019-04-16 16:17:13 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-16 17:02:50 +03:00
|
|
|
// RTSP methods.
|
2019-04-16 16:17:13 +03:00
|
|
|
const (
|
2019-04-16 17:02:50 +03:00
|
|
|
describe = "DESCRIBE"
|
|
|
|
options = "OPTIONS"
|
|
|
|
play = "PLAY"
|
|
|
|
setup = "SETUP"
|
2019-04-16 16:17:13 +03:00
|
|
|
)
|
|
|
|
|
2019-04-25 09:00:28 +03:00
|
|
|
// Minimum response size to be considered valid.
|
2019-04-25 08:16:21 +03:00
|
|
|
const minResponse = 15
|
|
|
|
|
2019-04-25 09:00:28 +03:00
|
|
|
var errSmallResponse = errors.New("response too small")
|
2019-04-25 08:16:21 +03:00
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// Request describes an RTSP request.
|
2019-04-17 05:48:23 +03:00
|
|
|
type Request struct {
|
|
|
|
Method string
|
|
|
|
URL *url.URL
|
|
|
|
Proto string
|
|
|
|
ProtoMajor int
|
|
|
|
ProtoMinor int
|
|
|
|
Header http.Header
|
|
|
|
ContentLength int
|
|
|
|
Body io.ReadCloser
|
|
|
|
}
|
2019-04-16 16:17:13 +03:00
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// String returns a formatted string of the Request.
|
2019-04-17 05:48:23 +03:00
|
|
|
func (r Request) String() string {
|
2019-04-17 02:35:34 +03:00
|
|
|
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)
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 02:35:34 +03:00
|
|
|
s += "\r\n"
|
|
|
|
if r.Body != nil {
|
|
|
|
str, _ := ioutil.ReadAll(r.Body)
|
|
|
|
s += string(str)
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-17 02:35:34 +03:00
|
|
|
return s
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// NewRequest returns a pointer to a new Request.
|
2019-04-17 06:14:54 +03:00
|
|
|
func NewRequest(method, cSeq string, u *url.URL, body io.ReadCloser) (*Request, error) {
|
2019-04-17 05:48:23 +03:00
|
|
|
req := &Request{
|
|
|
|
Method: method,
|
|
|
|
URL: u,
|
|
|
|
Proto: "RTSP",
|
|
|
|
ProtoMajor: 1,
|
|
|
|
ProtoMinor: 0,
|
|
|
|
Header: map[string][]string{"CSeq": []string{cSeq}},
|
|
|
|
Body: body,
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-17 05:48:23 +03:00
|
|
|
return req, nil
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// Response describes an RTSP response.
|
2019-04-16 16:17:13 +03:00
|
|
|
type Response struct {
|
2019-04-17 12:01:03 +03:00
|
|
|
Proto string
|
|
|
|
ProtoMajor int
|
|
|
|
ProtoMinor int
|
|
|
|
StatusCode int
|
|
|
|
Status string
|
2019-04-16 16:17:13 +03:00
|
|
|
ContentLength int64
|
2019-04-17 12:01:03 +03:00
|
|
|
Header http.Header
|
|
|
|
Body io.ReadCloser
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// 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 {
|
2019-04-16 16:17:13 +03:00
|
|
|
for _, v := range v {
|
|
|
|
s += fmt.Sprintf("%s: %s\n", k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// ReadResponse will read the response of the RTSP request from the connection,
|
|
|
|
// and return a pointer to a new Response.
|
2019-04-23 09:52:58 +03:00
|
|
|
func ReadResponse(r io.Reader) (res *Response, err error) {
|
|
|
|
res = new(Response)
|
2019-04-16 16:17:13 +03:00
|
|
|
res.Header = make(map[string][]string)
|
2019-04-23 09:52:58 +03:00
|
|
|
b := bufio.NewReader(r)
|
|
|
|
var s string
|
2019-04-16 16:17:13 +03:00
|
|
|
// TODO: allow CR, LF, or CRLF
|
2019-04-23 09:52:58 +03:00
|
|
|
if s, err = b.ReadString('\n'); err != nil {
|
|
|
|
return
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-25 08:16:21 +03:00
|
|
|
if len(s) < minResponse {
|
2019-04-25 09:00:28 +03:00
|
|
|
return nil, errSmallResponse
|
2019-04-25 08:16:21 +03:00
|
|
|
}
|
2019-04-16 16:17:13 +03:00
|
|
|
parts := strings.SplitN(s, " ", 3)
|
2019-04-17 12:01:03 +03:00
|
|
|
res.Proto, res.ProtoMajor, res.ProtoMinor, err = parseVersion(parts[0])
|
2019-04-16 16:17:13 +03:00
|
|
|
if err != nil {
|
2019-04-23 09:52:58 +03:00
|
|
|
return
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-23 09:52:58 +03:00
|
|
|
if res.StatusCode, err = strconv.Atoi(parts[1]); err != nil {
|
|
|
|
return
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
res.Status = strings.TrimSpace(parts[2])
|
|
|
|
|
2019-04-23 09:52:58 +03:00
|
|
|
// read headers
|
2019-04-16 16:17:13 +03:00
|
|
|
for {
|
2019-04-23 09:52:58 +03:00
|
|
|
if s, err = b.ReadString('\n'); err != nil {
|
|
|
|
return
|
|
|
|
} else if s = strings.TrimRight(s, "\r\n"); s == "" {
|
2019-04-16 16:17:13 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-04-23 09:52:58 +03:00
|
|
|
parts := strings.SplitN(s, ":", 2)
|
2019-04-16 16:17:13 +03:00
|
|
|
res.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
|
|
|
|
}
|
|
|
|
|
2019-04-23 09:52:58 +03:00
|
|
|
res.ContentLength, _ = strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64)
|
2019-04-16 16:17:13 +03:00
|
|
|
|
2019-04-23 09:52:58 +03:00
|
|
|
res.Body = closer{b, r}
|
|
|
|
return
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-17 05:48:23 +03:00
|
|
|
|
|
|
|
type closer struct {
|
|
|
|
*bufio.Reader
|
2019-04-23 09:52:58 +03:00
|
|
|
r io.Reader
|
2019-04-17 05:48:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c closer) Close() error {
|
|
|
|
if c.Reader == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-23 09:52:58 +03:00
|
|
|
defer func() {
|
|
|
|
c.Reader = nil
|
|
|
|
c.r = nil
|
|
|
|
}()
|
|
|
|
if r, ok := c.r.(io.ReadCloser); ok {
|
|
|
|
return r.Close()
|
|
|
|
}
|
2019-04-17 05:48:23 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 12:01:03 +03:00
|
|
|
// parseVersion returns the protocol type, major version and minor version.
|
|
|
|
func parseVersion(s string) (proto string, maj, min int, err error) {
|
2019-04-17 05:48:23 +03:00
|
|
|
parts := strings.SplitN(s, "/", 2)
|
|
|
|
proto = parts[0]
|
|
|
|
parts = strings.SplitN(parts[1], ".", 2)
|
2019-04-17 12:01:03 +03:00
|
|
|
|
|
|
|
maj, err = strconv.Atoi(parts[0])
|
|
|
|
if err != nil {
|
2019-04-17 05:48:23 +03:00
|
|
|
return
|
|
|
|
}
|
2019-04-17 12:01:03 +03:00
|
|
|
|
|
|
|
min, err = strconv.Atoi(parts[0])
|
|
|
|
if err != nil {
|
2019-04-17 05:48:23 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|