Skip to content

Commit 376fb9e

Browse files
authored
replace New* with Initialize (#723)
1 parent db959f8 commit 376fb9e

File tree

24 files changed

+296
-124
lines changed

24 files changed

+296
-124
lines changed

client.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,8 @@ func (c *Client) trySwitchingProtocol2(medi *description.Media, baseURL *base.UR
854854
}
855855

856856
func (c *Client) startTransportRoutines() {
857-
c.timeDecoder = rtptime.NewGlobalDecoder2()
857+
c.timeDecoder = &rtptime.GlobalDecoder2{}
858+
c.timeDecoder.Initialize()
858859

859860
for _, cm := range c.setuppedMedias {
860861
cm.start()
@@ -1025,7 +1026,7 @@ func (c *Client) do(req *base.Request, skipResponse bool) (*base.Response, error
10251026
// get session from response
10261027
if v, ok := res.Header["Session"]; ok {
10271028
var sx headers.Session
1028-
err := sx.Unmarshal(v)
1029+
err = sx.Unmarshal(v)
10291030
if err != nil {
10301031
return nil, liberrors.ErrClientSessionHeaderInvalid{Err: err}
10311032
}
@@ -1041,7 +1042,12 @@ func (c *Client) do(req *base.Request, skipResponse bool) (*base.Response, error
10411042
pass, _ := req.URL.User.Password()
10421043
user := req.URL.User.Username()
10431044

1044-
sender, err := auth.NewSender(res.Header["WWW-Authenticate"], user, pass)
1045+
sender := &auth.Sender{
1046+
WWWAuth: res.Header["WWW-Authenticate"],
1047+
User: user,
1048+
Pass: pass,
1049+
}
1050+
err = sender.Initialize()
10451051
if err != nil {
10461052
return nil, liberrors.ErrClientAuthSetup{Err: err}
10471053
}

examples/client-play-format-h264-mpeg4audio-to-disk/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// This example shows how to
1414
// 1. connect to a RTSP server
15-
// 2. check if there's a H264 stream and an MPEG-4 audio format
15+
// 2. check if there's a H264 stream and a MPEG-4 audio stream
1616
// 3. save the content of those formats in a file in MPEG-TS format
1717

1818
func main() {

examples/client-play-format-h264/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// This example shows how to
1717
// 1. connect to a RTSP server
18-
// 2. check if there's an H264 format
18+
// 2. check if there's an H264 stream
1919
// 3. decode the H264 stream into RGBA frames
2020

2121
// This example requires the FFmpeg libraries, that can be installed with this command:

examples/client-play-format-h265/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
// This example shows how to
1717
// 1. connect to a RTSP server
18-
// 2. check if there's an H265 format
18+
// 2. check if there's a H265 stream
1919
// 3. decode the H265 stream into RGBA frames
2020

2121
// This example requires the FFmpeg libraries, that can be installed with this command:

examples/client-play-format-lpcm/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// This example shows how to
1313
// 1. connect to a RTSP server
14-
// 2. check if there's an LPCM format
14+
// 2. check if there's a LPCM stream
1515
// 3. get LPCM samples of that format
1616

1717
func main() {

examples/client-play-format-mpeg4audio-to-disk/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
// This example shows how to
1414
// 1. connect to a RTSP server
15-
// 2. check if there's an MPEG-4 audio format
15+
// 2. check if there's a MPEG-4 audio stream
1616
// 3. save the content of the format in a file in MPEG-TS format
1717

1818
func main() {

examples/client-play-format-mpeg4audio/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// This example shows how to
1313
// 1. connect to a RTSP server
14-
// 2. check if there's an MPEG-4 audio format
14+
// 2. check if there's a MPEG-4 audio stream
1515
// 3. get access units of that format
1616

1717
func main() {

examples/client-play-format-opus/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// This example shows how to
1313
// 1. connect to a RTSP server
14-
// 2. check if there's an Opus format
14+
// 2. check if there's an Opus stream
1515
// 3. get Opus packets of that format
1616

1717
func main() {

examples/client-play-format-vp8/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// This example shows how to
1616
// 1. connect to a RTSP server
17-
// 2. check if there's an VP8 format
17+
// 2. check if there's a VP8 stream
1818
// 3. decode the VP8 stream into RGBA frames
1919

2020
// This example requires the FFmpeg libraries, that can be installed with this command:

examples/client-play-format-vp9/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// This example shows how to
1616
// 1. connect to a RTSP server
17-
// 2. check if there's an VP9 format
17+
// 2. check if there's a VP9 stream
1818
// 3. decode the VP9 stream into RGBA frames
1919

2020
// This example requires the FFmpeg libraries, that can be installed with this command:

examples/proxy/server.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ func (s *server) OnPlay(ctx *gortsplib.ServerHandlerOnPlayCtx) (*base.Response,
9898
func (s *server) setStreamReady(desc *description.Session) *gortsplib.ServerStream {
9999
s.mutex.Lock()
100100
defer s.mutex.Unlock()
101-
s.stream = gortsplib.NewServerStream(s.s, desc)
101+
s.stream = &gortsplib.ServerStream{
102+
Server: s.s,
103+
Desc: desc,
104+
}
105+
s.stream.Initialize()
102106
return s.stream
103107
}
104108

examples/server-auth/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ func (sh *serverHandler) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (
117117
}
118118

119119
// create the stream and save the publisher
120-
sh.stream = gortsplib.NewServerStream(sh.s, ctx.Description)
120+
sh.stream = &gortsplib.ServerStream{
121+
Server: sh.s,
122+
Desc: ctx.Description,
123+
}
124+
sh.stream.Initialize()
121125
sh.publisher = ctx.Session
122126

123127
return &base.Response{

examples/server-tls/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ func (sh *serverHandler) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (
8989
}
9090

9191
// create the stream and save the publisher
92-
sh.stream = gortsplib.NewServerStream(sh.s, ctx.Description)
92+
sh.stream = &gortsplib.ServerStream{
93+
Server: sh.s,
94+
Desc: ctx.Description,
95+
}
96+
sh.stream.Initialize()
9397
sh.publisher = ctx.Session
9498

9599
return &base.Response{

examples/server/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,11 @@ func (sh *serverHandler) OnAnnounce(ctx *gortsplib.ServerHandlerOnAnnounceCtx) (
8888
}
8989

9090
// create the stream and save the publisher
91-
sh.stream = gortsplib.NewServerStream(sh.s, ctx.Description)
91+
sh.stream = &gortsplib.ServerStream{
92+
Server: sh.s,
93+
Desc: ctx.Description,
94+
}
95+
sh.stream.Initialize()
9296
sh.publisher = ctx.Session
9397

9498
return &base.Response{

internal/highleveltests/server_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,11 @@ func TestServerRecordRead(t *testing.T) {
328328
}, fmt.Errorf("someone is already publishing")
329329
}
330330

331-
stream = gortsplib.NewServerStream(s, ctx.Description)
331+
stream = &gortsplib.ServerStream{
332+
Server: s,
333+
Desc: ctx.Description,
334+
}
335+
stream.Initialize()
332336
publisher = ctx.Session
333337

334338
return &base.Response{

pkg/auth/sender.go

+32-23
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,51 @@ import (
77
"github.com/bluenviron/gortsplib/v4/pkg/headers"
88
)
99

10-
// Sender allows to send credentials.
11-
type Sender struct {
12-
user string
13-
pass string
14-
authHeader *headers.Authenticate
10+
// NewSender allocates a Sender.
11+
//
12+
// Deprecated: replaced by Sender.Initialize().
13+
func NewSender(wwwAuth base.HeaderValue, user string, pass string) (*Sender, error) {
14+
s := &Sender{
15+
WWWAuth: wwwAuth,
16+
User: user,
17+
Pass: pass,
18+
}
19+
err := s.Initialize()
20+
return s, err
1521
}
1622

17-
// NewSender allocates a Sender.
23+
// Sender allows to send credentials.
1824
// It requires a WWW-Authenticate header (provided by the server)
1925
// and a set of credentials.
20-
func NewSender(wwwAuth base.HeaderValue, user string, pass string) (*Sender, error) {
21-
var bestAuthHeader *headers.Authenticate
26+
type Sender struct {
27+
WWWAuth base.HeaderValue
28+
User string
29+
Pass string
30+
31+
authHeader *headers.Authenticate
32+
}
2233

23-
for _, v := range wwwAuth {
34+
// Initialize initializes a Sender.
35+
func (se *Sender) Initialize() error {
36+
for _, v := range se.WWWAuth {
2437
var auth headers.Authenticate
2538
err := auth.Unmarshal(base.HeaderValue{v})
2639
if err != nil {
2740
continue // ignore unrecognized headers
2841
}
2942

30-
if bestAuthHeader == nil ||
43+
if se.authHeader == nil ||
3144
(auth.Algorithm != nil && *auth.Algorithm == headers.AuthAlgorithmSHA256) ||
32-
(bestAuthHeader.Method == headers.AuthMethodBasic) {
33-
bestAuthHeader = &auth
45+
(se.authHeader.Method == headers.AuthMethodBasic) {
46+
se.authHeader = &auth
3447
}
3548
}
3649

37-
if bestAuthHeader == nil {
38-
return nil, fmt.Errorf("no authentication methods available")
50+
if se.authHeader == nil {
51+
return fmt.Errorf("no authentication methods available")
3952
}
4053

41-
return &Sender{
42-
user: user,
43-
pass: pass,
44-
authHeader: bestAuthHeader,
45-
}, nil
54+
return nil
4655
}
4756

4857
// AddAuthorization adds the Authorization header to a Request.
@@ -53,21 +62,21 @@ func (se *Sender) AddAuthorization(req *base.Request) {
5362
Method: se.authHeader.Method,
5463
}
5564

56-
h.Username = se.user
65+
h.Username = se.User
5766

5867
if se.authHeader.Method == headers.AuthMethodBasic {
59-
h.BasicPass = se.pass
68+
h.BasicPass = se.Pass
6069
} else { // digest
6170
h.Realm = se.authHeader.Realm
6271
h.Nonce = se.authHeader.Nonce
6372
h.URI = urStr
6473
h.Algorithm = se.authHeader.Algorithm
6574

6675
if se.authHeader.Algorithm == nil || *se.authHeader.Algorithm == headers.AuthAlgorithmMD5 {
67-
h.Response = md5Hex(md5Hex(se.user+":"+se.authHeader.Realm+":"+se.pass) + ":" +
76+
h.Response = md5Hex(md5Hex(se.User+":"+se.authHeader.Realm+":"+se.Pass) + ":" +
6877
se.authHeader.Nonce + ":" + md5Hex(string(req.Method)+":"+urStr))
6978
} else { // sha256
70-
h.Response = sha256Hex(sha256Hex(se.user+":"+se.authHeader.Realm+":"+se.pass) + ":" +
79+
h.Response = sha256Hex(sha256Hex(se.User+":"+se.authHeader.Realm+":"+se.Pass) + ":" +
7180
se.authHeader.Nonce + ":" + sha256Hex(string(req.Method)+":"+urStr))
7281
}
7382
}

pkg/rtptime/global_decoder2.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ type GlobalDecoder2Track interface {
3232
PTSEqualsDTS(*rtp.Packet) bool
3333
}
3434

35+
// NewGlobalDecoder2 allocates a GlobalDecoder.
36+
//
37+
// Deprecated: replaced by GlobalDecoder2.Initialize().
38+
func NewGlobalDecoder2() *GlobalDecoder2 {
39+
d := &GlobalDecoder2{}
40+
d.Initialize()
41+
return d
42+
}
43+
3544
// GlobalDecoder2 is a RTP timestamp decoder.
3645
type GlobalDecoder2 struct {
3746
mutex sync.Mutex
@@ -42,11 +51,9 @@ type GlobalDecoder2 struct {
4251
tracks map[GlobalDecoder2Track]*globalDecoder2TrackData
4352
}
4453

45-
// NewGlobalDecoder2 allocates a GlobalDecoder.
46-
func NewGlobalDecoder2() *GlobalDecoder2 {
47-
return &GlobalDecoder2{
48-
tracks: make(map[GlobalDecoder2Track]*globalDecoder2TrackData),
49-
}
54+
// Initialize initializes a GlobalDecoder2.
55+
func (d *GlobalDecoder2) Initialize() {
56+
d.tracks = make(map[GlobalDecoder2Track]*globalDecoder2TrackData)
5057
}
5158

5259
// Decode decodes a timestamp.

server_conn.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ func (sc *ServerConn) handleRequestInner(req *base.Request) (*base.Response, err
348348
}
349349

350350
if stream != nil {
351-
byts, _ := serverSideDescription(stream.desc).Marshal(multicast)
351+
byts, _ := serverSideDescription(stream.Desc).Marshal(multicast)
352352
res.Body = byts
353353
}
354354
}

0 commit comments

Comments
 (0)