Skip to content

Commit 02b8e36

Browse files
committed
fix a few pieces
1 parent f09a8f8 commit 02b8e36

File tree

7 files changed

+28
-20
lines changed

7 files changed

+28
-20
lines changed

cameras.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ func (c *Camera) SaveVideo(ops *VidOps, length time.Duration, maxsize int64, out
109109
// StreamMJPG makes a web request to retrieve a motion JPEG stream.
110110
// Returns an io.ReadCloser that will (hopefully) never end.
111111
func (c *Camera) StreamMJPG(ops *VidOps) (io.ReadCloser, error) {
112-
ctx, cancel := context.WithTimeout(context.Background(), c.server.config.Timeout)
113-
defer cancel()
114-
115-
resp, err := c.server.GetContext(ctx, "++video", c.makeRequestParams(ops))
112+
resp, err := c.server.Get("++video", c.makeRequestParams(ops))
116113
if err != nil {
117114
return nil, fmt.Errorf("getting video: %w", err)
118115
}
@@ -123,10 +120,7 @@ func (c *Camera) StreamMJPG(ops *VidOps) (io.ReadCloser, error) {
123120
// StreamH264 makes a web request to retrieve an H264 stream.
124121
// Returns an io.ReadCloser that will (hopefully) never end.
125122
func (c *Camera) StreamH264(ops *VidOps) (io.ReadCloser, error) {
126-
ctx, cancel := context.WithTimeout(context.Background(), c.server.config.Timeout)
127-
defer cancel()
128-
129-
resp, err := c.server.GetContext(ctx, "++stream", c.makeRequestParams(ops))
123+
resp, err := c.server.Get("++stream", c.makeRequestParams(ops))
130124
if err != nil {
131125
return nil, fmt.Errorf("getting stream: %w", err)
132126
}
@@ -137,10 +131,7 @@ func (c *Camera) StreamH264(ops *VidOps) (io.ReadCloser, error) {
137131
// StreamG711 makes a web request to retrieve an G711 audio stream.
138132
// Returns an io.ReadCloser that will (hopefully) never end.
139133
func (c *Camera) StreamG711() (io.ReadCloser, error) {
140-
ctx, cancel := context.WithTimeout(context.Background(), c.server.config.Timeout)
141-
defer cancel()
142-
143-
resp, err := c.server.GetContext(ctx, "++audio", c.makeRequestParams(nil))
134+
resp, err := c.server.Get("++audio", c.makeRequestParams(nil))
144135
if err != nil {
145136
return nil, fmt.Errorf("getting audio: %w", err)
146137
}
@@ -169,7 +160,7 @@ func (c *Camera) PostG711(audio io.ReadCloser) ([]byte, error) {
169160
func (c *Camera) GetJPEG(ops *VidOps) (image.Image, error) {
170161
ops.FPS = -1 // not used for single image
171162

172-
ctx, cancel := context.WithTimeout(context.Background(), c.server.config.Timeout)
163+
ctx, cancel := context.WithTimeout(context.Background(), c.server.TimeoutDur())
173164
defer cancel()
174165

175166
resp, err := c.server.GetContext(ctx, "++image", c.makeRequestParams(ops))

files.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package securityspy
33
/* This file handles all file transfers for media saved by security spy. */
44

55
import (
6-
"context"
76
"encoding/xml"
87
"fmt"
98
"io"
@@ -169,10 +168,7 @@ func (f *File) Get(highBandwidth bool) (io.ReadCloser, error) {
169168
uri = strings.Replace(f.Link.HREF, "++getfile/", "++getfilehb/", 1)
170169
}
171170

172-
ctx, cancel := context.WithTimeout(context.Background(), f.server.config.Timeout)
173-
defer cancel()
174-
175-
resp, err := f.server.GetContext(ctx, uri, make(url.Values))
171+
resp, err := f.server.Get(uri, make(url.Values))
176172
if err != nil {
177173
return nil, fmt.Errorf("getting file: %w", err)
178174
}

mocks/api.go

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

securityspy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func NewMust(c *server.Config) *Server {
3131
}
3232

3333
// Assign all the sub-interface structs.
34-
server := &Server{config: c, API: c, Encoder: DefaultEncoder}
34+
server := &Server{API: c, Encoder: DefaultEncoder}
3535
server.Files = &Files{server: server}
3636
server.Events = &Events{
3737
server: server,

securityspy_types.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
type Server struct {
2020
server.API
2121
Encoder string
22-
config *server.Config
2322
Files *Files // Files interface.
2423
Events *Events // Events interface.
2524
Cameras *Cameras // Cameras & PTZ interfaces.

server/interface.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10+
"time"
1011
)
1112

1213
// API interface is provided only to allow overriding local methods during local testing.
@@ -19,6 +20,7 @@ type API interface {
1920
Post(apiPath string, params url.Values, post io.ReadCloser) (body []byte, err error)
2021
GetXML(apiPath string, params url.Values, v interface{}) (err error)
2122
SimpleReq(apiURI string, params url.Values, cameraNum int) error
23+
TimeoutDur() time.Duration
2224
BaseURL() string
2325
Auth() string
2426
}

server/server.go

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ func (s *Config) Auth() string {
5959
return s.Password
6060
}
6161

62+
// TimeoutDur returns the configured timeout.
63+
func (s *Config) TimeoutDur() time.Duration {
64+
return s.Timeout
65+
}
66+
6267
func (s *Config) GetContext(ctx context.Context, apiPath string, params url.Values) (*http.Response, error) {
6368
s.getClient()
6469

0 commit comments

Comments
 (0)