mirror of https://bitbucket.org/ausocean/av.git
protocol/rtsp/rtsp.go: put tcp dial in new session
tcp dial is now in new session, firstly so that we don't have to do the s.conn == nil check and also so that we dont't have to pass around the url everywhere.
This commit is contained in:
parent
85bb88c325
commit
c9082c1b4c
|
@ -23,43 +23,53 @@ const (
|
||||||
setup = "SETUP"
|
setup = "SETUP"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type session struct {
|
||||||
cSeq int
|
cSeq int
|
||||||
|
url *url.URL
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
session string
|
session string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSession() *Session {
|
func NewSession(u string) (*session, error) {
|
||||||
return &Session{}
|
s := &session{}
|
||||||
|
var err error
|
||||||
|
s.url, err = url.Parse(u)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.conn, err = net.Dial("tcp", s.url.Host)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Describe(urlStr string) (*Response, error) {
|
func (s *session) Describe() (*Response, error) {
|
||||||
return s.writeRequest(describe, urlStr, func(req *Request) { req.Header.Add("Accept", "application/sdp") }, nil)
|
return s.writeRequest(describe, func(req *Request) { req.Header.Add("Accept", "application/sdp") }, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Options(urlStr string) (*Response, error) {
|
func (s *session) Options(urlStr string) (*Response, error) {
|
||||||
return s.writeRequest(options, urlStr, nil, nil)
|
return s.writeRequest(options, nil, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Setup(urlStr, transport string) (*Response, error) {
|
func (s *session) Setup(transport string) (*Response, error) {
|
||||||
return s.writeRequest(
|
return s.writeRequest(
|
||||||
setup,
|
setup,
|
||||||
urlStr,
|
|
||||||
func(req *Request) {
|
func(req *Request) {
|
||||||
req.Header.Add("Transport", transport)
|
req.Header.Add("Transport", transport)
|
||||||
},
|
},
|
||||||
func(resp *Response) {
|
func(resp *Response) {
|
||||||
s.session = resp.Header.Get("Session")
|
s.session = resp.Header.Get("session")
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) Play(urlStr string) (*Response, error) {
|
func (s *session) Play() (*Response, error) {
|
||||||
return s.writeRequest(play, urlStr, func(req *Request) { req.Header.Add("Session", s.session) }, nil)
|
return s.writeRequest(play, func(req *Request) { req.Header.Add("session", s.session) }, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) writeRequest(method, urlStr string, headerOp func(*Request), respOp func(*Response)) (*Response, error) {
|
func (s *session) writeRequest(method string, headerOp func(*Request), respOp func(*Response)) (*Response, error) {
|
||||||
req, err := NewRequest(method, urlStr, s.nextCSeq(), nil)
|
req, err := NewRequest(method, s.nextCSeq(), s.url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -68,14 +78,7 @@ func (s *Session) writeRequest(method, urlStr string, headerOp func(*Request), r
|
||||||
headerOp(req)
|
headerOp(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.conn == nil {
|
_, err = io.WriteString(s.conn, req.String())
|
||||||
s.conn, err = net.Dial("tcp", req.URL.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.WriteString(s.conn, (*Request)(req).String())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -91,7 +94,7 @@ func (s *Session) writeRequest(method, urlStr string, headerOp func(*Request), r
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) nextCSeq() string {
|
func (s *session) nextCSeq() string {
|
||||||
s.cSeq++
|
s.cSeq++
|
||||||
return strconv.Itoa(s.cSeq)
|
return strconv.Itoa(s.cSeq)
|
||||||
}
|
}
|
||||||
|
@ -122,12 +125,7 @@ func (r Request) String() string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRequest(method, urlStr, cSeq string, body io.ReadCloser) (*Request, error) {
|
func NewRequest(method, cSeq string, u *url.URL, body io.ReadCloser) (*Request, error) {
|
||||||
u, err := url.Parse(urlStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req := &Request{
|
req := &Request{
|
||||||
Method: method,
|
Method: method,
|
||||||
URL: u,
|
URL: u,
|
||||||
|
|
Loading…
Reference in New Issue