|
| 1 | +import { SourceLayerType } from '@sofie-automation/blueprints-integration' |
| 2 | +import { DBPart, isPartPlayable } from '@sofie-automation/corelib/dist/dataModel/Part' |
| 3 | +import { PartId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
| 4 | +import { UserError, UserErrorMessage } from '@sofie-automation/corelib/dist/error' |
| 5 | +import { |
| 6 | + PauseCurrentPartProps, |
| 7 | + ResumeCurrentPartProps, |
| 8 | + TakeNextPartResult, |
| 9 | + TakePreviousPartProps, |
| 10 | +} from '@sofie-automation/corelib/dist/worker/studio' |
| 11 | +import { ReadonlyDeep } from 'type-fest' |
| 12 | +import { JobContext } from '../jobs/index.js' |
| 13 | +import { getCurrentTime } from '../lib/index.js' |
| 14 | +import { innerStopPieces } from './adlibUtils.js' |
| 15 | +import { runJobWithPlayoutModel } from './lock.js' |
| 16 | +import { PlayoutModel } from './model/PlayoutModel.js' |
| 17 | +import { setNextPartFromPart } from './setNext.js' |
| 18 | +import { performTakeToNextedPart } from './take.js' |
| 19 | +import { updateTimeline } from './timeline/generate.js' |
| 20 | + |
| 21 | +export function findPreviousPlayablePart< |
| 22 | + T extends Pick<ReadonlyDeep<DBPart>, '_id' | 'invalid' | 'floated'>, |
| 23 | +>(orderedParts: readonly T[], currentPartId: PartId): T | undefined { |
| 24 | + const currentIndex = orderedParts.findIndex((part) => part._id === currentPartId) |
| 25 | + if (currentIndex <= 0) { |
| 26 | + return undefined |
| 27 | + } |
| 28 | + |
| 29 | + for (let i = currentIndex - 1; i >= 0; i--) { |
| 30 | + const candidate = orderedParts[i] |
| 31 | + if (isPartPlayable(candidate)) { |
| 32 | + return candidate |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + return undefined |
| 37 | +} |
| 38 | + |
| 39 | +function isVideoClipLayerType(type: SourceLayerType | undefined): boolean { |
| 40 | + return type === SourceLayerType.VT || type === SourceLayerType.LIVE_SPEAK |
| 41 | +} |
| 42 | + |
| 43 | +async function pauseCurrentPartInner(context: JobContext, playoutModel: PlayoutModel, stopClips: boolean): Promise<void> { |
| 44 | + const playlist = playoutModel.playlist |
| 45 | + if (!playlist.activationId) throw UserError.create(UserErrorMessage.InactiveRundown, undefined, 412) |
| 46 | + |
| 47 | + const currentPartInstance = playoutModel.currentPartInstance |
| 48 | + if (!currentPartInstance) throw UserError.create(UserErrorMessage.NoCurrentPart, undefined, 412) |
| 49 | + |
| 50 | + const now = getCurrentTime() |
| 51 | + if (currentPartInstance.partInstance.timings?.pausedAt) { |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + currentPartInstance.setPausedPlayback(now) |
| 56 | + |
| 57 | + if (stopClips && currentPartInstance.partInstance.timings?.plannedStartedPlayback) { |
| 58 | + const currentRundown = playoutModel.getRundown(currentPartInstance.partInstance.rundownId) |
| 59 | + if (currentRundown) { |
| 60 | + const showStyle = await context.getShowStyleCompound( |
| 61 | + currentRundown.rundown.showStyleVariantId, |
| 62 | + currentRundown.rundown.showStyleBaseId |
| 63 | + ) |
| 64 | + innerStopPieces( |
| 65 | + context, |
| 66 | + playoutModel, |
| 67 | + showStyle.sourceLayers, |
| 68 | + currentPartInstance, |
| 69 | + (pieceInstance) => isVideoClipLayerType(showStyle.sourceLayers[pieceInstance.piece.sourceLayerId]?.type), |
| 70 | + undefined |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + await updateTimeline(context, playoutModel) |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Freeze the current part countdown and stop rolling VT/VO clips. |
| 80 | + */ |
| 81 | +export async function handlePauseCurrentPart(context: JobContext, data: PauseCurrentPartProps): Promise<void> { |
| 82 | + return runJobWithPlayoutModel( |
| 83 | + context, |
| 84 | + data, |
| 85 | + async (playoutModel) => { |
| 86 | + if (!playoutModel.playlist.activationId) throw UserError.create(UserErrorMessage.InactiveRundown, undefined, 412) |
| 87 | + if (!playoutModel.currentPartInstance) throw UserError.create(UserErrorMessage.NoCurrentPart, undefined, 412) |
| 88 | + }, |
| 89 | + async (playoutModel) => { |
| 90 | + await pauseCurrentPartInner(context, playoutModel, true) |
| 91 | + } |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Resume a frozen current-part countdown. |
| 97 | + */ |
| 98 | +export async function handleResumeCurrentPart(context: JobContext, data: ResumeCurrentPartProps): Promise<void> { |
| 99 | + return runJobWithPlayoutModel( |
| 100 | + context, |
| 101 | + data, |
| 102 | + async (playoutModel) => { |
| 103 | + if (!playoutModel.playlist.activationId) throw UserError.create(UserErrorMessage.InactiveRundown, undefined, 412) |
| 104 | + if (!playoutModel.currentPartInstance) throw UserError.create(UserErrorMessage.NoCurrentPart, undefined, 412) |
| 105 | + }, |
| 106 | + async (playoutModel) => { |
| 107 | + const currentPartInstance = playoutModel.currentPartInstance |
| 108 | + if (!currentPartInstance) throw UserError.create(UserErrorMessage.NoCurrentPart, undefined, 412) |
| 109 | + |
| 110 | + if (!currentPartInstance.partInstance.timings?.pausedAt) { |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + currentPartInstance.setPausedPlayback(undefined) |
| 115 | + await updateTimeline(context, playoutModel) |
| 116 | + } |
| 117 | + ) |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Take the previous playable part, then freeze its clock so the operator can regroup. |
| 122 | + */ |
| 123 | +export async function handleTakePreviousPart( |
| 124 | + context: JobContext, |
| 125 | + data: TakePreviousPartProps |
| 126 | +): Promise<TakeNextPartResult> { |
| 127 | + const now = getCurrentTime() |
| 128 | + |
| 129 | + return runJobWithPlayoutModel( |
| 130 | + context, |
| 131 | + data, |
| 132 | + async (playoutModel) => { |
| 133 | + if (!playoutModel.playlist.activationId) throw UserError.create(UserErrorMessage.InactiveRundown, undefined, 412) |
| 134 | + if (!playoutModel.currentPartInstance) throw UserError.create(UserErrorMessage.NoCurrentPart, undefined, 412) |
| 135 | + }, |
| 136 | + async (playoutModel) => { |
| 137 | + const currentPartInstance = playoutModel.currentPartInstance |
| 138 | + if (!currentPartInstance) throw UserError.create(UserErrorMessage.NoCurrentPart, undefined, 412) |
| 139 | + |
| 140 | + const previousPart = findPreviousPlayablePart( |
| 141 | + playoutModel.getAllOrderedParts(), |
| 142 | + currentPartInstance.partInstance.part._id |
| 143 | + ) |
| 144 | + if (!previousPart) { |
| 145 | + throw UserError.create(UserErrorMessage.TakeNoPreviousPart, undefined, 412) |
| 146 | + } |
| 147 | + |
| 148 | + await setNextPartFromPart(context, playoutModel, previousPart, true) |
| 149 | + await performTakeToNextedPart(context, playoutModel, now, undefined) |
| 150 | + |
| 151 | + const takenPart = playoutModel.currentPartInstance |
| 152 | + takenPart?.setPausedPlayback(getCurrentTime()) |
| 153 | + await updateTimeline(context, playoutModel) |
| 154 | + |
| 155 | + return { |
| 156 | + nextTakeTime: now + context.studio.settings.minimumTakeSpan, |
| 157 | + } |
| 158 | + } |
| 159 | + ) |
| 160 | +} |
0 commit comments