use worker on remote camera to decode jpg image

This commit is contained in:
lxsang
2020-12-29 20:55:34 +00:00
parent 0a6f8c0f8c
commit 71ed35ae00
11 changed files with 127 additions and 21 deletions

View File

@ -3,6 +3,14 @@ class RemoteCamera extends this.OS.application.BaseApplication
super "RemoteCamera", args
main: () ->
@decoder = new Worker("pkg://RemoteCamera/decoder.js".asFileHandle().getlink())
@decoder.onmessage = (e) =>
@paint e.data
@decoder.postMessage {libjpeg: "pkg://libjpeg/jpg.js".asFileHandle().getlink()}
@mute = false
@player = @find "player"
@ -10,6 +18,8 @@ class RemoteCamera extends this.OS.application.BaseApplication
@fpsctl = @find "fpsctl"
@cam_setting = {
w: 640,
h: 480,
@ -97,7 +107,20 @@ class RemoteCamera extends this.OS.application.BaseApplication
}
.then (v) =>
@setting.channel = v
@openSession()
return @openSession() unless @sub
@sub.onclose = (e) =>
@openSession()
@sub.close()
paint: (msg) ->
# console.log msg
data = new Uint8Array msg.pixels
ctx = @player.getContext "2d", { alpha: false }
@player.width = msg.w
@player.height = msg.h
imgData = ctx.createImageData msg.w, msg.h
imgData.data.set data
ctx.putImageData imgData, 0, 0
menu: () ->
{
@ -142,24 +165,19 @@ class RemoteCamera extends this.OS.application.BaseApplication
@mute = false
@sub.onmessage = (e) =>
jpeg = new JpegImage()
jpeg.parse e.data
context = @player.getContext("2d")
@player.width = jpeg.width
@player.height = jpeg.height
#jpeg.copyToImageData(d)
imgData = context.getImageData(0,0,jpeg.width,jpeg.height)
jpeg.copyToImageData imgData
context.putImageData(imgData, 0, 0)
console.log("receive")
return unless @decoder
@decoder.postMessage e.data.buffer, [e.data.buffer]
@sub.onclose = () =>
@sub = undefined
@notify __("Unsubscribed to the camera service")
@quit()
return @quit()
Antunnel.tunnel.subscribe @sub
cleanup: () ->
@sub.close() if @sub
@decoder.terminate() if @decoder
setCameraSetting: () ->
return unless @sub
@ -171,7 +189,5 @@ class RemoteCamera extends this.OS.application.BaseApplication
@sub.send Antunnel.Msg.CTRL, arr
RemoteCamera.singleton = true
RemoteCamera.dependencies = [
"pkg://libjpeg/jpg.js"
]
this.OS.register "RemoteCamera", RemoteCamera