Skip to content

Commit

Permalink
dev: optimize jpeg stream server
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Mar 12, 2024
1 parent d73bef4 commit 2ea7161
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 55 deletions.
59 changes: 16 additions & 43 deletions component/jpegstream/cavas_player.html
Original file line number Diff line number Diff line change
@@ -1,54 +1,27 @@
<!--
Copyright (C) 2024 wwhai
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas Player</title>
<title>Background Image DIV</title>
<style>
#myDiv1 {
width: 960px;
height: 540px;
background-image: url('http://127.0.0.1:9401/jpeg_stream/pull?liveId=123a4509de8a99aa0f680cb5847e0765');
background-size: cover;
background-repeat: no-repeat;
background-color: black;
border-color: aqua;
border: 2px;
background-position: center;
}
</style>

</head>

<body>
<canvas id="canvas" width="320" height="240"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');

// 定义图像地址
const imageUrl = 'http://127.0.0.1:9401/jpeg_stream/pull?liveId=a97607e47c81d43dba8ef6fa48a2cd45';

// 加载图像并在 Canvas 上绘制
function loadImage() {
const image = new Image();
image.crossOrigin = 'Anonymous'; // 允许跨域
image.onload = function () {
// 清除 Canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// 在 Canvas 上绘制图像
ctx.drawImage(image, 0, 0, canvas.width, canvas.height);
};
image.src = imageUrl;
}

// 每隔一段时间加载图像
setInterval(loadImage, 100); // 每秒加载一次
</script>
<div id="myDiv1"></div>
</body>

</html>
4 changes: 3 additions & 1 deletion component/jpegstream/jpeg_stream_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,15 @@ func (s *JpegStreamServer) Init(cfg map[string]any) error {
ctx.Header("Cache-Control", "no-cache, no-store, must-revalidate")
ctx.Header("Pragma", "no-cache")
ctx.Header("Expires", "0")
ctx.Header("Age", "0")
ctx.Writer.Write([]byte("HTTP/1.1 200 OK\r\n"))
for {
_, err := ctx.Writer.Write(JpegStream.GetWebJpegFrame())
if err != nil {
glogger.GLogger.Error("Jpeg Stream Server Write error", err)
break
}
time.Sleep(50 * time.Millisecond) // 50帧
time.Sleep(50 * time.Millisecond) // 1000/50=20帧
}
glogger.GLogger.Infof("Client [%s] pull jpeg stream[%s] finished:",
ctx.Request.RemoteAddr, liveId)
Expand Down
30 changes: 19 additions & 11 deletions utils/gocv_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,32 @@ func CvMatToImageBytes(FrameBuffer []byte) ([]byte, Resolution, error) {
__CGoMutex.Lock()
defer __CGoMutex.Unlock()
imgMat := gocv.NewMat()
err0 := gocv.IMDecodeIntoMat(FrameBuffer,
gocv.IMReadFlag(gocv.ColorBGRToGray), &imgMat)
defer imgMat.Close()
err0 := gocv.IMDecodeIntoMat(FrameBuffer, gocv.IMReadFlag(gocv.IMReadColor), &imgMat)
Resolution := Resolution{
imgMat.Cols(), imgMat.Rows(),
}
if err0 != nil {
__CGoMutex.Unlock()
return nil, Resolution, err0
}
currentTime := time.Now()
formattedTime := currentTime.Format("2006-01-02 15:04:05")
gocv.PutText(&imgMat, fmt.Sprintf("%s(%d*%d)", formattedTime, Resolution.Width, Resolution.Height), image.Point{10, 50},
gocv.FontHersheyPlain, 4, color.RGBA{255, 0, 0, 0}, 4)
ImgBytes, err1 := gocv.IMEncode(".jpg", imgMat)
if err1 != nil {
__CGoMutex.Unlock()
return nil, Resolution, err0

gocv.PutText(&imgMat, fmt.Sprintf("%s(%d*%d)", formattedTime, Resolution.Width, Resolution.Height), image.Point{5, 25},
gocv.FontHersheyPlain, 2, color.RGBA{255, 0, 0, 0}, 2)
NewImgMat := gocv.NewMat()
defer NewImgMat.Close()
if imgMat.Cols() <= 640 {
gocv.Resize(imgMat, &NewImgMat, image.Point{}, 2, 2, gocv.InterpolationArea)
ImgBytes, err1 := gocv.IMEncode(".jpg", NewImgMat)
if err1 != nil {
return nil, Resolution, err0
}
return ImgBytes.GetBytes(), Resolution, nil
} else {
ImgBytes, err1 := gocv.IMEncode(".jpg", imgMat)
if err1 != nil {
return nil, Resolution, err0
}
return ImgBytes.GetBytes(), Resolution, nil
}
return ImgBytes.GetBytes(), Resolution, nil
}

0 comments on commit 2ea7161

Please sign in to comment.