66 "image"
77 "image/jpeg"
88 "io"
9+ "net/http"
910 "net/url"
1011 "os"
1112 "strconv"
@@ -116,7 +117,13 @@ func (c *Camera) SaveVideo(ops *VidOps, length time.Duration, maxsize int64, out
116117// StreamMJPG makes a web request to retrieve a motion JPEG stream.
117118// Returns an io.ReadCloser that will (hopefully) never end.
118119func (c * Camera ) StreamMJPG (ops * VidOps ) (io.ReadCloser , error ) {
119- resp , err := c .server .Get ("++video" , c .makeRequestParams (ops ))
120+ return c .StreamMJPGContext (context .Background (), ops )
121+ }
122+
123+ // StreamMJPGContext makes a web request to retrieve a motion JPEG stream.
124+ // Returns an io.ReadCloser that will (hopefully) never end.
125+ func (c * Camera ) StreamMJPGContext (ctx context.Context , ops * VidOps ) (io.ReadCloser , error ) {
126+ resp , err := c .server .GetContextClient (ctx , "++video" , c .makeRequestParams (ops ), c .streamHTTPClient ())
120127 if err != nil {
121128 return nil , fmt .Errorf ("getting video: %w" , err )
122129 }
@@ -127,7 +134,13 @@ func (c *Camera) StreamMJPG(ops *VidOps) (io.ReadCloser, error) {
127134// StreamH264 makes a web request to retrieve an H264 stream.
128135// Returns an io.ReadCloser that will (hopefully) never end.
129136func (c * Camera ) StreamH264 (ops * VidOps ) (io.ReadCloser , error ) {
130- resp , err := c .server .Get ("++stream" , c .makeRequestParams (ops ))
137+ return c .StreamH264Context (context .Background (), ops )
138+ }
139+
140+ // StreamH264Context makes a web request to retrieve an H264 stream.
141+ // Returns an io.ReadCloser that will (hopefully) never end.
142+ func (c * Camera ) StreamH264Context (ctx context.Context , ops * VidOps ) (io.ReadCloser , error ) {
143+ resp , err := c .server .GetContextClient (ctx , "++stream" , c .makeRequestParams (ops ), c .streamHTTPClient ())
131144 if err != nil {
132145 return nil , fmt .Errorf ("getting stream: %w" , err )
133146 }
@@ -138,7 +151,13 @@ func (c *Camera) StreamH264(ops *VidOps) (io.ReadCloser, error) {
138151// StreamG711 makes a web request to retrieve an G711 audio stream.
139152// Returns an io.ReadCloser that will (hopefully) never end.
140153func (c * Camera ) StreamG711 () (io.ReadCloser , error ) {
141- resp , err := c .server .Get ("++audio" , c .makeRequestParams (nil ))
154+ return c .StreamG711Context (context .Background ())
155+ }
156+
157+ // StreamG711Context makes a web request to retrieve an G711 audio stream.
158+ // Returns an io.ReadCloser that will (hopefully) never end.
159+ func (c * Camera ) StreamG711Context (ctx context.Context ) (io.ReadCloser , error ) {
160+ resp , err := c .server .GetContextClient (ctx , "++audio" , c .makeRequestParams (nil ), c .streamHTTPClient ())
142161 if err != nil {
143162 return nil , fmt .Errorf ("getting audio: %w" , err )
144163 }
@@ -165,6 +184,10 @@ func (c *Camera) PostG711(audio io.ReadCloser) ([]byte, error) {
165184// GetJPEG returns an images from a camera.
166185// VidOps defines the image size. ops.FPS is ignored.
167186func (c * Camera ) GetJPEG (ops * VidOps ) (image.Image , error ) {
187+ if ops == nil {
188+ ops = & VidOps {}
189+ }
190+
168191 ops .FPS = - 1 // not used for single image
169192
170193 ctx , cancel := context .WithTimeout (context .Background (), c .server .TimeoutDur ())
@@ -329,3 +352,10 @@ func (c *Camera) makeRequestParams(ops *VidOps) url.Values {
329352
330353 return params
331354}
355+
356+ func (c * Camera ) streamHTTPClient () * http.Client {
357+ client := c .server .HTTPClient ()
358+ client .Timeout = 0
359+
360+ return client
361+ }
0 commit comments