diff --git a/machinery/src/capture/main.go b/machinery/src/capture/main.go index 57043b2..4315bfb 100644 --- a/machinery/src/capture/main.go +++ b/machinery/src/capture/main.go @@ -757,7 +757,7 @@ func Base64Image(captureDevice *Capture, communication *models.Communication, co var img image.YCbCr img, err = (*rtspClient).DecodePacket(pkt) if err == nil { - imageResized, _ := utils.ResizeImage(&img, uint(configuration.Config.Capture.IPCamera.BaseWidth)) + imageResized, _ := utils.ResizeImage(&img, uint(configuration.Config.Capture.IPCamera.BaseWidth), uint(configuration.Config.Capture.IPCamera.BaseHeight)) bytes, _ := utils.ImageToBytes(imageResized) encodedImage = base64.StdEncoding.EncodeToString(bytes) break diff --git a/machinery/src/cloud/Cloud.go b/machinery/src/cloud/Cloud.go index 692ce74..e800ec2 100644 --- a/machinery/src/cloud/Cloud.go +++ b/machinery/src/cloud/Cloud.go @@ -706,7 +706,7 @@ func HandleLiveStreamSD(livestreamCursor *packets.QueueCursor, configuration *mo log.Log.Info("cloud.HandleLiveStreamSD(): Sending base64 encoded images to MQTT.") img, err := rtspClient.DecodePacket(pkt) if err == nil { - imageResized, _ := utils.ResizeImage(&img, uint(config.Capture.IPCamera.BaseWidth)) + imageResized, _ := utils.ResizeImage(&img, uint(config.Capture.IPCamera.BaseWidth), uint(config.Capture.IPCamera.BaseHeight)) bytes, _ := utils.ImageToBytes(imageResized) chunking := config.Capture.LiveviewChunking @@ -865,7 +865,7 @@ func HandleRealtimeProcessing(processingCursor *packets.QueueCursor, configurati log.Log.Info("cloud.RealtimeProcessing(): Sending base64 encoded images to MQTT.") img, err := rtspClient.DecodePacket(pkt) if err == nil { - imageResized, _ := utils.ResizeImage(&img, uint(config.Capture.IPCamera.BaseWidth)) + imageResized, _ := utils.ResizeImage(&img, uint(config.Capture.IPCamera.BaseWidth), uint(config.Capture.IPCamera.BaseHeight)) bytes, _ := utils.ImageToBytes(imageResized) encoded := base64.StdEncoding.EncodeToString(bytes) diff --git a/machinery/src/components/Kerberos.go b/machinery/src/components/Kerberos.go index 492ccca..05dfa2e 100644 --- a/machinery/src/components/Kerberos.go +++ b/machinery/src/components/Kerberos.go @@ -713,7 +713,7 @@ func GetSnapshotRaw(c *gin.Context, captureDevice *capture.Capture, configuratio image := capture.JpegImage(captureDevice, communication) // encode image to jpeg - imageResized, _ := utils.ResizeImage(&image, uint(configuration.Config.Capture.IPCamera.BaseWidth)) + imageResized, _ := utils.ResizeImage(&image, uint(configuration.Config.Capture.IPCamera.BaseWidth), uint(configuration.Config.Capture.IPCamera.BaseHeight)) bytes, _ := utils.ImageToBytes(imageResized) // Return image/jpeg diff --git a/machinery/src/config/main.go b/machinery/src/config/main.go index f194347..054c790 100644 --- a/machinery/src/config/main.go +++ b/machinery/src/config/main.go @@ -547,6 +547,7 @@ func OverrideWithEnvironmentVariables(configuration *models.Configuration) { // Hardcoded values, should be configurable in the future. configuration.Config.Capture.IPCamera.BaseWidth = 640 + configuration.Config.Capture.IPCamera.BaseHeight = 0 // we might force the height to 0, so it will be calculated based on the width and aspect ratio of the camera. } func SaveConfig(configDirectory string, config models.Config, configuration *models.Configuration, communication *models.Communication) error { diff --git a/machinery/src/routers/http/routes.go b/machinery/src/routers/http/routes.go index 612f34a..71d1ba3 100644 --- a/machinery/src/routers/http/routes.go +++ b/machinery/src/routers/http/routes.go @@ -15,7 +15,7 @@ import ( func AddRoutes(r *gin.Engine, authMiddleware *jwt.GinJWTMiddleware, configDirectory string, configuration *models.Configuration, communication *models.Communication, captureDevice *capture.Capture) *gin.RouterGroup { r.GET("/ws", func(c *gin.Context) { - websocket.WebsocketHandler(c, communication, captureDevice) + websocket.WebsocketHandler(c, configuration, communication, captureDevice) }) // This is legacy should be removed in future! Now everything diff --git a/machinery/src/routers/websocket/main.go b/machinery/src/routers/websocket/main.go index 2a0442f..f54ffd2 100644 --- a/machinery/src/routers/websocket/main.go +++ b/machinery/src/routers/websocket/main.go @@ -49,7 +49,7 @@ var upgrader = websocket.Upgrader{ }, } -func WebsocketHandler(c *gin.Context, communication *models.Communication, captureDevice *capture.Capture) { +func WebsocketHandler(c *gin.Context, configuration *models.Configuration, communication *models.Communication, captureDevice *capture.Capture) { w := c.Writer r := c.Request conn, err := upgrader.Upgrade(w, r, nil) @@ -112,7 +112,7 @@ func WebsocketHandler(c *gin.Context, communication *models.Communication, captu ctx, cancel := context.WithCancel(context.Background()) sockets[clientID].Cancels["stream-sd"] = cancel - go ForwardSDStream(ctx, clientID, sockets[clientID], communication, captureDevice) + go ForwardSDStream(ctx, clientID, sockets[clientID], configuration, communication, captureDevice) } } } @@ -131,7 +131,7 @@ func WebsocketHandler(c *gin.Context, communication *models.Communication, captu } } -func ForwardSDStream(ctx context.Context, clientID string, connection *Connection, communication *models.Communication, captureDevice *capture.Capture) { +func ForwardSDStream(ctx context.Context, clientID string, connection *Connection, configuration *models.Configuration, communication *models.Communication, captureDevice *capture.Capture) { var queue *packets.Queue var cursor *packets.QueueCursor @@ -159,7 +159,9 @@ logreader: var img image.YCbCr img, err = (*rtspClient).DecodePacket(pkt) if err == nil { - imageResized, _ := utils.ResizeImage(&img, 640) // Resize to 640 width + config := configuration.Config + // Resize the image to the base width and height + imageResized, _ := utils.ResizeImage(&img, uint(config.Capture.IPCamera.BaseHeight), uint(config.Capture.IPCamera.BaseWidth)) bytes, _ := utils.ImageToBytes(imageResized) encodedImage = base64.StdEncoding.EncodeToString(bytes) } else { diff --git a/machinery/src/utils/main.go b/machinery/src/utils/main.go index 8ce54de..11a6e38 100644 --- a/machinery/src/utils/main.go +++ b/machinery/src/utils/main.go @@ -411,14 +411,14 @@ func ImageToBytes(img *image.Image) ([]byte, error) { return buffer.Bytes(), err } -func ResizeImage(img image.Image, newWidth uint) (*image.Image, error) { +func ResizeImage(img image.Image, newWidth uint, newHeight uint) (*image.Image, error) { if img == nil { return nil, errors.New("image is nil") } // resize to width 640 using Lanczos resampling // and preserve aspect ratio - m := resize.Resize(newWidth, 0, img, resize.Lanczos3) + m := resize.Resize(newWidth, newHeight, img, resize.Lanczos3) return &m, nil }