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)
res, err = sess.Play(*rtspServerPtr, res.Header.Get("Session"))
res, err = sess.Play()
if err != nil {
log.Fatalln(err)
}
log.Println(res)
// Send back rtcp
// TODO(saxon): use RTCP client here to maintain stream.
for {
}
}

View File

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