protocol/rtsp: made rtsp/cmd/stream functional

This commit is contained in:
Saxon 2019-04-23 16:22:58 +09:30
parent 34c59c95d6
commit a256dc9133
2 changed files with 28 additions and 26 deletions

View File

@ -94,13 +94,13 @@ func main() {
} }
log.Println(res) log.Println(res)
res, err = sess.Play(*rtspServerPtr, res.Header.Get("Session")) res, err = sess.Play()
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
log.Println(res) log.Println(res)
// Send back rtcp // TODO(saxon): use RTCP client here to maintain stream.
for { for {
} }
} }

View File

@ -239,62 +239,64 @@ func (r Response) String() string {
// ReadResponse will read the response of the RTSP request from the connection, // ReadResponse will read the response of the RTSP request from the connection,
// and return a pointer to a new Response. // and return a pointer to a new Response.
func ReadResponse(b *bufio.Reader) (*Response, error) { func ReadResponse(r io.Reader) (res *Response, err error) {
res := &Response{} res = new(Response)
res.Header = make(map[string][]string) res.Header = make(map[string][]string)
b := bufio.NewReader(r)
var s string
// TODO: allow CR, LF, or CRLF // TODO: allow CR, LF, or CRLF
s, err := b.ReadString('\n') if s, err = b.ReadString('\n'); err != nil {
if err != nil { return
return nil, err
} }
parts := strings.SplitN(s, " ", 3) parts := strings.SplitN(s, " ", 3)
res.Proto, res.ProtoMajor, res.ProtoMinor, err = parseVersion(parts[0]) res.Proto, res.ProtoMajor, res.ProtoMinor, err = parseVersion(parts[0])
if err != nil { if err != nil {
return nil, err return
} }
res.StatusCode, err = strconv.Atoi(parts[1]) if res.StatusCode, err = strconv.Atoi(parts[1]); err != nil {
if err != nil { return
return nil, err
} }
res.Status = strings.TrimSpace(parts[2]) res.Status = strings.TrimSpace(parts[2])
// read headers
for { for {
s, err = b.ReadString('\n') if s, err = b.ReadString('\n'); err != nil {
if err != nil { return
return nil, err } else if s = strings.TrimRight(s, "\r\n"); s == "" {
}
s = strings.TrimRight(s, "\r\n")
if s == "" {
break break
} }
parts = strings.SplitN(s, ":", 2) parts := strings.SplitN(s, ":", 2)
res.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])) res.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]))
} }
res.ContentLength, err = strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64) res.ContentLength, _ = strconv.ParseInt(res.Header.Get("Content-Length"), 10, 64)
if err != nil {
return nil, err
}
res.Body = closer{b} res.Body = closer{b, r}
return res, nil return
} }
type closer struct { type closer struct {
*bufio.Reader *bufio.Reader
r io.Reader
} }
func (c closer) Close() error { func (c closer) Close() error {
if c.Reader == nil { if c.Reader == nil {
return nil return nil
} }
defer func() {
c.Reader = nil c.Reader = nil
c.r = nil
}()
if r, ok := c.r.(io.ReadCloser); ok {
return r.Close()
}
return nil return nil
} }