Skip to content

Commit

Permalink
Fix CanvasMediaStream
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Jun 13, 2024
1 parent 9a21932 commit 8a19d29
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 8 additions & 2 deletions core/src/CanvasMediaStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ export class CanvasMediaStream {
#ctx: CanvasRenderingContext2D | null = null
#stream: MediaStream | null = null

begin(frameRequestRate = 60, size?: vec2) {
async begin(
initialDraw: () => PromiseLike<void>,
frameRequestRate = 60,
size?: vec2
) {
if (!this.#ctx) {
const canvas = document.createElement('canvas')
if (size) {
Expand All @@ -14,6 +18,8 @@ export class CanvasMediaStream {
this.#ctx = canvas.getContext('2d')!
}

await initialDraw()

this.#stream = this.#ctx.canvas.captureStream(frameRequestRate)
return this.#stream
}
Expand All @@ -36,6 +42,6 @@ export class CanvasMediaStream {
}

end() {
this.#stream?.stop()
this.#stream?.getTracks().forEach(track => track.stop())
}
}
12 changes: 7 additions & 5 deletions core/src/TethrPTPUSB/TethrPanasonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,22 +579,24 @@ export class TethrPanasonic extends TethrPTPUSB {
#canvasMediaStream = new CanvasMediaStream()

async startLiveview(): Promise<OperationResult<MediaStream>> {
const stream = this.#canvasMediaStream.begin()

await this.device.sendCommand({
label: 'Panasonic Liveview',
opcode: OpCodePanasonic.Liveview,
parameters: [0x0d000010],
})

const stream = await this.#canvasMediaStream.begin(
this.#updateLiveviewFrame
)

this.emit('liveviewChange', readonlyConfigDesc(stream))

this.device.on('idle', this.#updateLiveview)
this.device.on('idle', this.#updateLiveviewFrame)

return {status: 'ok', value: stream}
}

#updateLiveview = async () => {
#updateLiveviewFrame = async () => {
const image = await this.#getLiveviewImage()
if (!image) return

Expand All @@ -610,7 +612,7 @@ export class TethrPanasonic extends TethrPTPUSB {
parameters: [0x0d000011],
})

this.device.off('idle', this.#updateLiveview)
this.device.off('idle', this.#updateLiveviewFrame)
this.emit('liveviewChange', readonlyConfigDesc(null))

return {status: 'ok'}
Expand Down
4 changes: 3 additions & 1 deletion core/src/TethrPTPUSB/TethrSigma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,9 @@ export class TethrSigma extends TethrPTPUSB {
#canvasMediaStream = new CanvasMediaStream()

async startLiveview(): Promise<OperationResult<MediaStream>> {
const stream = this.#canvasMediaStream.begin()
const stream = await this.#canvasMediaStream.begin(
this.#updateLiveviewFrame
)
this.device.on('idle', this.#updateLiveviewFrame)

this.emit('liveviewChange', readonlyConfigDesc(stream))
Expand Down

0 comments on commit 8a19d29

Please sign in to comment.