Skip to content

Commit

Permalink
fix: multiview upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Mar 21, 2022
1 parent d83948d commit 9109e33
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
},
"files": [
"/dist",
"/assets",
"/CHANGELOG.md",
"/README.md",
"/LICENSE"
Expand Down Expand Up @@ -86,6 +87,7 @@
"exit-hook": "^2.2.1",
"freetype2": "^1.0.6",
"nanotimer": "^0.3.15",
"p-lazy": "^3.1.0",
"p-queue": "^6.6.2",
"threadedclass": "^0.9.0",
"tslib": "^2.3.1",
Expand Down
39 changes: 39 additions & 0 deletions src/atem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ import {
} from './state/fairlight'
import { FairlightDynamicsResetProps } from './commands/Fairlight/common'
import { MultiViewerPropertiesState } from './state/settings'
import { calculateGenerateMultiviewerLabelProps, generateMultiviewerLabel } from './lib/multiviewLabel'
import { FontFace, NewMemoryFace } from 'freetype2'
import PLazy = require('p-lazy')
import { readFile } from 'fs/promises'
import path = require('path')

export interface AtemOptions {
address?: string
Expand Down Expand Up @@ -236,8 +241,15 @@ export class BasicAtem extends EventEmitter<AtemEvents> {
}

export class Atem extends BasicAtem {
#multiviewerFontFace: Promise<FontFace>

constructor(options?: AtemOptions) {
super(options)

this.#multiviewerFontFace = PLazy.from(async () => {
const fontFile = await readFile(path.join(__dirname, '../assets/roboto/Roboto-Regular.ttf'))
return NewMemoryFace(fontFile)
})
}

public changeProgramInput(input: number, me = 0): Promise<void> {
Expand Down Expand Up @@ -950,4 +962,31 @@ export class Atem extends BasicAtem {
const command = new Commands.MediaPoolSettingsSetCommand(props.maxFrames)
return this.sendCommand(command)
}

/**
* Write a custom multiviewer label buffer
* @param inputId The input id
* @param buffer Label buffer
* @returns Promise that resolves once upload is complete
*/
public writeMultiviewerLabel(inputId: number, buffer: Buffer): Promise<void> {
// TODO - validate input data looks sane and wont crash atem
return this.dataTransferManager.uploadMultiViewerLabel(inputId, buffer)
}

/**
* Generate and upload a multiviewer label
* @param inputId The input id
* @param text Label text
* @returns Promise that resolves once upload is complete
*/
public async drawMultiviewerLabel(inputId: number, text: string): Promise<void> {
const props = calculateGenerateMultiviewerLabelProps(this.state ?? null)
if (!props) throw new Error(`Failed to determine render properties`)

const fontFace = await this.#multiviewerFontFace

const buffer = generateMultiviewerLabel(fontFace, text, props)
return this.dataTransferManager.uploadMultiViewerLabel(inputId, buffer)
}
}
2 changes: 1 addition & 1 deletion src/dataTransfer/dataTransferQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export abstract class DataTransferQueueBase {
* This is done in the queue, and calls back out to this.startTransfer
*/
protected tryStartTransfer(): void {
if (!this.activeTransfer) {
if (this.activeTransfer) {
this.handleCommandQueue
.add(
async () => {
Expand Down
10 changes: 6 additions & 4 deletions src/lib/multiviewLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ const Res4K: ResolutionSpec = {
xPad: 10,
yPadBottom: 8,
yPadTop: 4,
fontHeight: 48,
fontHeight: 46,
}
const Res1080: ResolutionSpec = {
width: 320,
height: 50,
xPad: 10,
yPadBottom: 8,
yPadTop: 4,
fontHeight: 28,
fontHeight: 26,
}
const Res720: ResolutionSpec = {
width: 320, // TODO - is this correct for all models?
height: 40,
xPad: 10,
yPadBottom: 8,
yPadTop: 4,
fontHeight: 18,
fontHeight: 16,
}

// const transparentColour = 0 // encoded value
Expand Down Expand Up @@ -203,7 +203,9 @@ export function generateMultiviewerLabel(face: FontFace, str: string, props: Gen
return buffer
}

export function calculateGenerateMultiviewerLabelProps(state: AtemState | null): GenerateMultiviewerLabelProps | null {
export function calculateGenerateMultiviewerLabelProps(
state: Readonly<AtemState> | null
): GenerateMultiviewerLabelProps | null {
if (state && state.info.supportedVideoModes) {
const res: GenerateMultiviewerLabelProps = {
UHD4K: false,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4412,6 +4412,11 @@ p-finally@^1.0.0:
resolved "https://registry.yarnpkg.com/p-finally/-/p-finally-1.0.0.tgz#3fbcfb15b899a44123b34b6dcc18b724336a2cae"
integrity sha1-P7z7FbiZpEEjs0ttzBi3JDNqLK4=

p-lazy@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/p-lazy/-/p-lazy-3.1.0.tgz#4b1e40482b7ee87853abbcf31824ff64e1816d61"
integrity sha512-sCJn0Cdahs6G6SX9+DUihVFUhrzDEduzE5xeViVBGtoqy5dBWko7W8T6Kk6TjR2uevRXJO7CShfWrqdH5s3w3g==

p-limit@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/p-limit/-/p-limit-1.3.0.tgz#b86bd5f0c25690911c7590fcbfc2010d54b3ccb8"
Expand Down

0 comments on commit 9109e33

Please sign in to comment.