protocol/rtsp: removed writeRequest receiver method

This commit is contained in:
Saxon 2019-04-26 15:09:59 +09:30
parent 4998d8d4d6
commit f0a1a866dd
2 changed files with 71 additions and 63 deletions

View File

@ -60,62 +60,55 @@ func NewClient(u string) (*Client, error) {
}
// Describe forms and sends an RTSP request of method DESCRIBE to the RTSP server.
func (s *Client) Describe() (*Response, error) {
return s.writeRequest(s.url, describe, func(req *Request) { req.Header.Add("Accept", "application/sdp") }, nil)
func (c *Client) Describe() (*Response, error) {
req, err := NewRequest(describe, c.nextCSeq(), c.url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/sdp")
return c.Do(req)
}
// Options forms and sends an RTSP request of method OPTIONS to the RTSP server.
func (s *Client) Options() (*Response, error) {
return s.writeRequest(s.url, options, nil, nil)
func (c *Client) Options() (*Response, error) {
req, err := NewRequest(options, c.nextCSeq(), c.url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// Setup forms and sends an RTSP request of method SETUP to the RTSP server.
func (s *Client) Setup(track, transport string) (*Response, error) {
url, err := url.Parse(s.addr + "/" + track)
func (c *Client) Setup(track, transport string) (*Response, error) {
url, err := url.Parse(c.addr + "/" + track)
if err != nil {
return nil, err
}
req, err := NewRequest(setup, c.nextCSeq(), url, nil)
if err != nil {
return nil, err
}
return s.writeRequest(
url,
setup,
func(req *Request) {
req.Header.Add("Transport", transport)
},
func(resp *Response) {
s.session = resp.Header.Get("Session")
},
)
}
// Play forms and sends an RTSP request of method PLAY to the RTSP server
func (s *Client) Play() (*Response, error) {
return s.writeRequest(s.url, play, func(req *Request) { req.Header.Add("Session", s.session) }, nil)
}
// writeRequest creates an RTSP request of the method given and writes it to the
// the Client connection.
//
// headerOp and respOp define any operation that needs
// to occur to the request or response for the given method.
func (c *Client) writeRequest(url *url.URL, method string, headerOp func(*Request), respOp func(*Response)) (*Response, error) {
req, err := NewRequest(method, c.nextCSeq(), url, nil)
if err != nil {
return nil, err
}
if headerOp != nil {
headerOp(req)
}
resp, err := c.Do(req)
if err != nil {
return nil, err
}
c.session = resp.Header.Get("Session")
if respOp != nil {
respOp(resp)
return resp, err
}
// Play forms and sends an RTSP request of method PLAY to the RTSP server
func (c *Client) Play() (*Response, error) {
req, err := NewRequest(play, c.nextCSeq(), c.url, nil)
if err != nil {
return nil, err
}
return resp, nil
req.Header.Add("Session", c.session)
return c.Do(req)
}
// Do sends the given RTSP request r and reads any responses, return the response
@ -134,7 +127,7 @@ func (c *Client) Do(r *Request) (*Response, error) {
return res, nil
}
func (s *Client) nextCSeq() string {
s.cSeq++
return strconv.Itoa(s.cSeq)
func (c *Client) nextCSeq() string {
c.cSeq++
return strconv.Itoa(c.cSeq)
}

View File

@ -51,14 +51,19 @@ func TestMethods(t *testing.T) {
// tests holds tests which consist of a function used to create and write a
// request of a particular method, and also the expected request bytes
// to be received on the server side. The bytes in these tests have been
// obtained from a valid RTSP communication session..
// obtained from a valid RTSP communication cltion..
tests := []struct {
method func(s *Client) (*Response, error)
method func(c *Client) (*Response, error)
expected []byte
}{
{
method: func(s *Client) (*Response, error) {
return s.writeRequest(url, describe, func(req *Request) { req.Header.Add("Accept", "application/sdp") }, nil)
method: func(c *Client) (*Response, error) {
req, err := NewRequest(describe, c.nextCSeq(), url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/sdp")
return c.Do(req)
},
expected: []byte{
0x44, 0x45, 0x53, 0x43, 0x52, 0x49, 0x42, 0x45, 0x20, 0x72, 0x74, 0x73,
@ -73,8 +78,12 @@ func TestMethods(t *testing.T) {
},
},
{
method: func(s *Client) (*Response, error) {
return s.writeRequest(url, options, nil, nil)
method: func(c *Client) (*Response, error) {
req, err := NewRequest(options, c.nextCSeq(), url, nil)
if err != nil {
return nil, err
}
return c.Do(req)
},
expected: []byte{
0x4f, 0x50, 0x54, 0x49, 0x4f, 0x4e, 0x53, 0x20, 0x72, 0x74, 0x73, 0x70,
@ -87,19 +96,19 @@ func TestMethods(t *testing.T) {
},
},
{
method: func(s *Client) (*Response, error) {
url, err := url.Parse(dummyURL + "/track1")
method: func(c *Client) (*Response, error) {
u, err := url.Parse(dummyURL + "/track1")
if err != nil {
t.Fatalf("could not parse url with track, failed with err: %v", err)
return nil, err
}
req, err := NewRequest(setup, c.nextCSeq(), u, nil)
if err != nil {
return nil, err
}
return s.writeRequest(
url,
setup,
func(req *Request) {
req.Header.Add("Transport", fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", 6870, 6871))
},
nil,
)
return c.Do(req)
},
expected: []byte{
0x53, 0x45, 0x54, 0x55, 0x50, 0x20, 0x72, 0x74, 0x73, 0x70, 0x3a, 0x2f,
@ -116,8 +125,14 @@ func TestMethods(t *testing.T) {
},
},
{
method: func(s *Client) (*Response, error) {
return s.writeRequest(url, play, func(req *Request) { req.Header.Add("Session", "00000021") }, nil)
method: func(c *Client) (*Response, error) {
req, err := NewRequest(play, c.nextCSeq(), url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Session", "00000021")
return c.Do(req)
},
expected: []byte{
0x50, 0x4c, 0x41, 0x59, 0x20, 0x72, 0x74, 0x73, 0x70, 0x3a, 0x2f, 0x2f,
@ -184,12 +199,12 @@ func TestMethods(t *testing.T) {
// This routine acts as the client.
go func() {
var sess *Client
var clt *Client
var err error
// Keep trying to connect to server.
for retry := 0; ; retry++ {
sess, err = NewClient(serverAddr)
clt, err = NewClient(serverAddr)
if err == nil {
break
}
@ -201,7 +216,7 @@ func TestMethods(t *testing.T) {
}
for i, test := range tests {
_, err = test.method(sess)
_, err = test.method(clt)
if err != nil && err != io.EOF && err != errSmallResponse {
clientErr <- errors.New(fmt.Sprintf("error request for: %v err: %v", i, err))
}