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-16 16:17:13 +03:00
|
|
|
package rtsp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-04-25 08:16:21 +03:00
|
|
|
"errors"
|
2019-04-27 20:05:13 +03:00
|
|
|
"fmt"
|
2019-04-16 16:17:13 +03:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-27 18:28:33 +03:00
|
|
|
// Minimum response size to be considered valid in bytes.
|
2019-04-29 06:33:39 +03:00
|
|
|
const minResponse = 12
|
2019-04-25 08:16:21 +03:00
|
|
|
|
2019-04-29 07:05:58 +03:00
|
|
|
var errInvalidResponse = errors.New("invalid response")
|
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-26 07:50:58 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
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-27 18:59:47 +03:00
|
|
|
var b strings.Builder
|
|
|
|
b.WriteString(r.Method + " " + r.URL.String() + " " + r.Proto + "/" + strconv.Itoa(r.ProtoMajor) + "." + strconv.Itoa(r.ProtoMinor) + "\r\n")
|
2019-04-17 02:35:34 +03:00
|
|
|
for k, v := range r.Header {
|
|
|
|
for _, v := range v {
|
2019-04-27 18:59:47 +03:00
|
|
|
b.WriteString(k + ": " + v + "\r\n")
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 18:59:47 +03:00
|
|
|
b.WriteString("\r\n")
|
2019-04-17 02:35:34 +03:00
|
|
|
if r.Body != nil {
|
2019-04-27 18:59:47 +03:00
|
|
|
s, _ := ioutil.ReadAll(r.Body)
|
|
|
|
b.WriteString(string(s))
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-27 18:59:47 +03:00
|
|
|
return b.String()
|
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
|
2019-04-27 19:02:44 +03:00
|
|
|
ContentLength int
|
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 {
|
2019-04-27 18:59:47 +03:00
|
|
|
var b strings.Builder
|
2019-04-29 06:31:17 +03:00
|
|
|
b.WriteString(r.Proto + "/" + strconv.Itoa(r.ProtoMajor) + "." + strconv.Itoa(r.ProtoMinor) + " " + strconv.Itoa(r.StatusCode) + "\n")
|
2019-04-17 12:01:03 +03:00
|
|
|
for k, v := range r.Header {
|
2019-04-16 16:17:13 +03:00
|
|
|
for _, v := range v {
|
2019-04-27 18:59:47 +03:00
|
|
|
b.WriteString(k + ": " + v + "\n")
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-04-27 18:59:47 +03:00
|
|
|
return b.String()
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
|
|
|
|
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-26 13:16:03 +03:00
|
|
|
func ReadResponse(r io.Reader) (*Response, error) {
|
|
|
|
resp := &Response{Header: make(map[string][]string)}
|
|
|
|
|
2019-04-28 07:34:45 +03:00
|
|
|
scanner := bufio.NewScanner(r)
|
2019-04-26 13:16:03 +03:00
|
|
|
|
|
|
|
// Read the first line.
|
2019-04-28 07:34:45 +03:00
|
|
|
scanner.Scan()
|
|
|
|
err := scanner.Err()
|
2019-04-26 13:16:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-28 07:34:45 +03:00
|
|
|
s := scanner.Text()
|
2019-04-26 13:16:03 +03:00
|
|
|
|
2019-04-29 07:05:58 +03:00
|
|
|
if len(s) < minResponse || s[:5] != "RTSP/" {
|
|
|
|
return nil, errInvalidResponse
|
2019-04-26 13:16:03 +03:00
|
|
|
}
|
2019-04-28 06:53:35 +03:00
|
|
|
resp.Proto = "RTSP"
|
2019-04-26 13:16:03 +03:00
|
|
|
|
2019-04-29 06:38:05 +03:00
|
|
|
n, err := fmt.Sscanf(s[5:], "%d.%d %d", &resp.ProtoMajor, &resp.ProtoMinor, &resp.StatusCode)
|
|
|
|
if err != nil || n != 3 {
|
|
|
|
return nil, fmt.Errorf("could not Sscanf response, error: %v", err)
|
2019-04-28 05:57:54 +03:00
|
|
|
}
|
2019-04-26 13:16:03 +03:00
|
|
|
|
|
|
|
// Read headers.
|
2019-04-28 07:34:45 +03:00
|
|
|
for scanner.Scan() {
|
|
|
|
err = scanner.Err()
|
2019-04-26 13:16:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-29 11:51:59 +03:00
|
|
|
text := scanner.Text()
|
|
|
|
fmt.Printf("text: %v\n", text)
|
|
|
|
parts := strings.SplitN(text, ":", 2)
|
|
|
|
fmt.Printf("text: %v\n", parts)
|
|
|
|
if len(parts) < 2 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p1 := strings.TrimSpace(parts[0])
|
|
|
|
p2 := strings.TrimSpace(parts[1])
|
|
|
|
fmt.Printf("p1: %v\n,p2: %v\n", p1, p2)
|
|
|
|
resp.Header.Add(p1, p2)
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-29 11:51:59 +03:00
|
|
|
fmt.Printf("header: %v", resp.Header)
|
2019-04-26 13:16:03 +03:00
|
|
|
// Get the content length from the header.
|
2019-04-29 11:51:59 +03:00
|
|
|
resp.ContentLength, _ = strconv.Atoi(resp.Header.Get("Content-Length"))
|
2019-04-16 16:17:13 +03:00
|
|
|
|
2019-04-28 07:34:45 +03:00
|
|
|
resp.Body = closer{r}
|
2019-04-26 13:16:03 +03:00
|
|
|
return resp, nil
|
2019-04-16 16:17:13 +03:00
|
|
|
}
|
2019-04-17 05:48:23 +03:00
|
|
|
|
|
|
|
type closer struct {
|
2019-04-28 07:34:45 +03:00
|
|
|
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
|
|
|
|
}()
|
2019-04-28 07:34:45 +03:00
|
|
|
if r, ok := c.Reader.(io.ReadCloser); ok {
|
2019-04-23 09:52:58 +03:00
|
|
|
return r.Close()
|
|
|
|
}
|
2019-04-17 05:48:23 +03:00
|
|
|
return nil
|
|
|
|
}
|