Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor casparcg device #292

Draft
wants to merge 2 commits into
base: release51
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Commands as orgCommands, AMCPCommand as orgAMCPCommand, SendResult } fr
import { ResponseTypes } from 'casparcg-connection/dist/connection'
import { EventEmitter } from 'events'

const mockDo = jest.fn()
export const mockDo = jest.fn()

const instances: Array<BasicCasparCGAPI> = []

Expand All @@ -28,8 +28,13 @@ export class BasicCasparCGAPI extends EventEmitter {
instances.push(this)
}

connect() {
this.connected = true
this.emit('connect')
}

async executeCommand(command: AMCPCommand): Promise<SendResult> {
mockDo.apply(this, command)
mockDo.apply(this, [command])

if (command.command === Commands.Info) {
return Promise.resolve({
Expand Down Expand Up @@ -89,3 +94,5 @@ export class BasicCasparCGAPI extends EventEmitter {
return instances
}
}

export class CasparCG extends BasicCasparCGAPI {}
82 changes: 51 additions & 31 deletions packages/timeline-state-resolver/src/__tests__/conductor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jest.mock('../service/DeviceInstance', () => ({

import { Conductor, TimelineTriggerTimeResult } from '../conductor'
import type { DeviceInstanceWrapper } from '../service/DeviceInstance'
import { mockDo } from '../__mocks__/casparcg-connection'

describe('Conductor', () => {
const mockTime = new MockTime()
Expand Down Expand Up @@ -452,10 +453,6 @@ describe('Conductor', () => {
}, 1500)

test('Changing of mappings live', async () => {
const commandReceiver0: any = jest.fn(async () => {
return Promise.resolve()
})

const myLayerMapping0: Mapping<SomeMappingCasparCG> = {
device: DeviceType.CASPARCG,
deviceId: 'device0',
Expand All @@ -480,7 +477,6 @@ describe('Conductor', () => {
options: {
host: '127.0.0.1',
},
commandReceiver: commandReceiver0,
})
conductor.setTimelineAndMappings([], myLayerMapping)

Expand Down Expand Up @@ -516,37 +512,52 @@ describe('Conductor', () => {

await mockTime.advanceTimeToTicks(10500)

expect(commandReceiver0).toHaveBeenCalledTimes(1)
expect(getMockCall(commandReceiver0, 0, 1).command).toEqual(Commands.Play)
expect(getMockCall(commandReceiver0, 0, 1).params).toMatchObject({
clip: 'AMB',
channel: 1,
layer: 10,
expect(mockDo).toHaveBeenCalledTimes(1)
expect(mockDo).toHaveBeenNthCalledWith(1, {
command: Commands.Play,
params: {
channel: 1,
layer: 10,
clip: 'AMB',
},
context: {
context: '',
layerId: '',
},
})

commandReceiver0.mockClear()
mockDo.mockClear()

// modify the mapping:
myLayerMapping0.options.layer = 20
conductor.setTimelineAndMappings(conductor.timeline, myLayerMapping)

await mockTime.advanceTimeTicks(100) // just a little bit

expect(commandReceiver0).toHaveBeenCalledTimes(2)
expect(getMockCall(commandReceiver0, 0, 1).command).toEqual(Commands.Clear)
expect(getMockCall(commandReceiver0, 0, 1).params).toMatchObject({
// clip: 'AMB',
channel: 1,
layer: 10,
expect(mockDo).toHaveBeenCalledTimes(2)
expect(mockDo).toHaveBeenNthCalledWith(1, {
command: Commands.Clear,
params: {
channel: 1,
layer: 10,
},
context: {
context: '',
layerId: '',
},
})
expect(getMockCall(commandReceiver0, 1, 1).command).toEqual(Commands.Play)
expect(getMockCall(commandReceiver0, 1, 1).params).toMatchObject({
clip: 'AMB',
channel: 1,
layer: 20,
expect(mockDo).toHaveBeenNthCalledWith(2, {
command: Commands.Play,
params: {
channel: 1,
layer: 20,
clip: 'AMB',
},
context: {
context: '',
layerId: '',
},
})

commandReceiver0.mockClear()
mockDo.mockClear()

// Replace the mapping altogether:
delete myLayerMapping['myLayer0']
Expand Down Expand Up @@ -580,23 +591,32 @@ describe('Conductor', () => {
// },
// })
// } else {
expect(commandReceiver0).toHaveBeenCalledTimes(2)
expect(getMockCall(commandReceiver0, 0, 1)).toMatchObject({

expect(mockDo).toHaveBeenCalledTimes(2)
expect(mockDo).toHaveBeenNthCalledWith(1, {
command: Commands.Clear,
params: {
channel: 1,
layer: 20,
},
context: {
context: '',
layerId: '',
},
})

expect(getMockCall(commandReceiver0, 1, 1)).toMatchObject({
expect(mockDo).toHaveBeenNthCalledWith(1, {
command: Commands.Play,
params: {
clip: 'AMB',
channel: 2,
layer: 10,
clip: 'AMB',
},
context: {
context: '',
layerId: '',
},
})
mockDo.mockClear()
// }
})

Expand Down
14 changes: 3 additions & 11 deletions packages/timeline-state-resolver/src/conductor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
DeviceOptionsHTTPWatcher,
DeviceOptionsAbstract,
DeviceOptionsTCPSend,
DeviceOptionsCasparCG,
} from 'timeline-state-resolver-types'

import { DoOnTime } from './devices/doOnTime'
Expand All @@ -36,7 +37,6 @@ import { assertNever, endTrace, fillStateFromDatastore, FinishedTrace, startTrac
import { CommandWithContext } from './devices/device'
import { DeviceContainer } from './devices/deviceContainer'

import { CasparCGDevice, DeviceOptionsCasparCGInternal } from './integrations/casparCG'
import { AtemDevice, DeviceOptionsAtemInternal } from './integrations/atem'
import { LawoDevice, DeviceOptionsLawoInternal } from './integrations/lawo'
import { PanasonicPtzDevice, DeviceOptionsPanasonicPTZInternal } from './integrations/panasonicPTZ'
Expand Down Expand Up @@ -501,15 +501,6 @@ export class Conductor extends EventEmitter<ConductorEvents> {
threadedClassOptions: ThreadedClassConfig
): Promise<BaseRemoteDeviceIntegration<DeviceOptionsBase<any>>> | null {
switch (deviceOptions.type) {
case DeviceType.CASPARCG:
return DeviceContainer.create<DeviceOptionsCasparCGInternal, typeof CasparCGDevice>(
'../../dist/integrations/casparCG/index.js',
'CasparCGDevice',
deviceId,
deviceOptions,
getCurrentTime,
threadedClassOptions
)
case DeviceType.ATEM:
return DeviceContainer.create<DeviceOptionsAtemInternal, typeof AtemDevice>(
'../../dist/integrations/atem/index.js',
Expand Down Expand Up @@ -646,6 +637,7 @@ export class Conductor extends EventEmitter<ConductorEvents> {
threadedClassOptions
)
case DeviceType.ABSTRACT:
case DeviceType.CASPARCG:
case DeviceType.HTTPSEND:
case DeviceType.HTTPWATCHER:
case DeviceType.OSC:
Expand Down Expand Up @@ -1553,7 +1545,7 @@ export class Conductor extends EventEmitter<ConductorEvents> {
}
export type DeviceOptionsAnyInternal =
| DeviceOptionsAbstract
| DeviceOptionsCasparCGInternal
| DeviceOptionsCasparCG
| DeviceOptionsAtemInternal
| DeviceOptionsLawoInternal
| DeviceOptionsHTTPSend
Expand Down
Loading