Skip to content

Commit

Permalink
feat: TransferManager
Browse files Browse the repository at this point in the history
Rewritten the entire transfer manager s.t. it handles clips correctly
  • Loading branch information
Balte de Wit authored and Alex Van Camp committed Sep 11, 2018
1 parent 683d83b commit e3e69a1
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 622 deletions.
35 changes: 28 additions & 7 deletions src/atem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,34 @@ export class Atem extends EventEmitter {
return this.sendCommand(command)
}

uploadMedia (props: DT.DataTransferProperties) {
uploadStill (index: number, data: Buffer, name: string, description: string) {
const resolution = Util.getResolution(this.state.settings.videoMode)
return this.dataTransferManager.newTransfer(
props.type,
props.pool,
{ name: props.name, description: props.description },
Util.convertPNGToYUV422(resolution[0], resolution[1], props.data)
return this.dataTransferManager.uploadStill(
index,
Util.convertPNGToYUV422(resolution[0], resolution[1], data),
name,
description
)
}

uploadClip (index: number, frames: Array<Buffer>, name: string) {
const resolution = Util.getResolution(this.state.settings.videoMode)
const data: Array<Buffer> = []
for (const frame of frames) {
data.push(Util.convertPNGToYUV422(resolution[0], resolution[1], frame))
}
return this.dataTransferManager.uploadClip(
index,
data,
name
)
}

uploadAudio (index: number, data: Buffer, name: string) {
return this.dataTransferManager.uploadAudio(
index,
data,
name
)
}

Expand All @@ -368,7 +389,7 @@ export class Atem extends EventEmitter {
}
for (const commandName in DataTransferCommands) {
if (command.constructor.name === commandName) {
this.dataTransferManager.processAtemCommand(command)
this.dataTransferManager.handleCommand(command)
}
}
}
Expand Down
227 changes: 0 additions & 227 deletions src/dataTransfer.ts

This file was deleted.

71 changes: 71 additions & 0 deletions src/dataTransfer/dataLock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Commands, Enums } from '..'

import DataTransfer from './dataTransfer'

export default class DataLock {
storeId: number
state: number
transfer: DataTransfer | undefined
queue: Array<DataTransfer> = []

commandQueue: Array<Commands.AbstractCommand> = []

constructor (storeId: number, commandQueue: Array<Commands.AbstractCommand>) {
this.storeId = storeId
this.commandQueue = commandQueue
}

enqueue (transfer: DataTransfer) {
this.queue.push(transfer)
this.dequeueAndRun()
}

dequeueAndRun () {
if (this.transfer === undefined && this.queue.length > 0) {
this.transfer = this.queue.shift()
// obtain lock
const command = new Commands.LockStateCommand()
command.updateProps({
index: this.storeId,
locked: true
})
this.commandQueue.push(command)
}
}

lockObtained () {
this.state = 1
if (this.transfer && this.transfer.state === Enums.TransferState.Queued) {
this.transfer.gotLock()
}
}

lostLock () {
this.state = 0
if (this.transfer && this.transfer.state === Enums.TransferState.Finished) {
this.transfer.finish(this.transfer)
} else if (this.transfer) {
// @todo: dequeue any old commands
this.transfer.fail()
}
this.transfer = undefined
this.dequeueAndRun()
}

updateLock (locked: boolean) {
this.state = locked ? 1 : 0
}

transferFinished () {
if (this.queue.length > 0) {
this.dequeueAndRun()
} else { // unlock
const command = new Commands.LockStateCommand()
command.updateProps({
index: this.storeId,
locked: false
})
this.commandQueue.push(command)
}
}
}
17 changes: 17 additions & 0 deletions src/dataTransfer/dataTransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Commands, Enums } from '..'

export default abstract class DataTransfer {
startedAt: Date
state: Enums.TransferState = Enums.TransferState.Queued
transferId: number
storeId: number

commandQueue: Array<Commands.AbstractCommand>

finish: (transfer: DataTransfer) => void
fail: () => void
abstract start (): void

abstract handleCommand (command: Commands.AbstractCommand): void
abstract gotLock (): void
}
25 changes: 25 additions & 0 deletions src/dataTransfer/dataTransferAudio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Commands, Enums } from '..'

import DataTransferFrame from './dataTransferFrame'

export default class DataTransferAudio extends DataTransferFrame {
description: { name: string }

start () {
const command = new Commands.DataTransferUploadRequestCommand()
command.updateProps({
transferId: this.transferId,
transferStoreId: this.storeId,
transferIndex: 0,
size: this.data.length,
mode: Enums.TransferMode.WriteAudio
})
this.commandQueue.push(command)
}

sendDescription () {
const command = new Commands.DataTransferFileDescriptionCommand()
command.updateProps({ ...this.description, fileHash: this.hash, transferId: this.transferId })
this.commandQueue.push(command)
}
}
Loading

0 comments on commit e3e69a1

Please sign in to comment.