forked from Sofie-Automation/sofie-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickLoop.ts
More file actions
154 lines (139 loc) · 5.8 KB
/
quickLoop.ts
File metadata and controls
154 lines (139 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
import {
DBRundownPlaylist,
QuickLoopMarker,
QuickLoopMarkerType,
} from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist/RundownPlaylist'
import { ForceQuickLoopAutoNext } from '@sofie-automation/shared-lib/dist/core/model/StudioSettings'
import { MarkerPosition, compareMarkerPositions } from '@sofie-automation/corelib/dist/playout/playlist'
import { ProtectedString, unprotectString } from '@sofie-automation/corelib/dist/protectedString'
import { DEFAULT_FALLBACK_PART_DURATION } from '@sofie-automation/shared-lib/dist/core/constants'
import { getCurrentTime } from '../../lib/lib'
import { generateTranslation } from '@sofie-automation/corelib/dist/lib'
import { IStudioSettings } from '@sofie-automation/corelib/dist/dataModel/Studio'
import { DBPartInstance } from '@sofie-automation/corelib/dist/dataModel/PartInstance'
import { DBSegment } from '@sofie-automation/corelib/dist/dataModel/Segment'
import { ReadonlyObjectDeep } from 'type-fest/source/readonly-deep'
import { ReactiveCacheCollection } from './ReactiveCacheCollection'
export function findPartPosition(
part: DBPart,
segmentRanks: Record<string, number>,
rundownRanks: Record<string, number>
): MarkerPosition {
return {
rundownRank: rundownRanks[part.rundownId as unknown as string] ?? 0,
segmentRank: segmentRanks[part.segmentId as unknown as string] ?? 0,
partRank: part._rank,
}
}
export function stringsToIndexLookup(strings: string[]): Record<string, number> {
return strings.reduce(
(result, str, index) => {
result[str] = index
return result
},
{} as Record<string, number>
)
}
export function extractRanks(docs: { _id: ProtectedString<any>; _rank: number }[]): Record<string, number> {
return docs.reduce(
(result, doc) => {
result[doc._id as unknown as string] = doc._rank
return result
},
{} as Record<string, number>
)
}
export function modifyPartForQuickLoop(
part: DBPart,
segmentRanks: Record<string, number>,
rundownRanks: Record<string, number>,
playlist: Pick<DBRundownPlaylist, 'quickLoop'>,
studioSettings: IStudioSettings,
quickLoopStartPosition: MarkerPosition | undefined,
quickLoopEndPosition: MarkerPosition | undefined,
canSetAutoNext = () => true
): void {
const partPosition = findPartPosition(part, segmentRanks, rundownRanks)
const isLoopDefined = quickLoopStartPosition && quickLoopEndPosition
const isLoopingOverriden =
isLoopDefined &&
playlist.quickLoop?.forceAutoNext !== ForceQuickLoopAutoNext.DISABLED &&
compareMarkerPositions(quickLoopStartPosition, partPosition) >= 0 &&
compareMarkerPositions(partPosition, quickLoopEndPosition) >= 0
const fallbackPartDuration = studioSettings.fallbackPartDuration ?? DEFAULT_FALLBACK_PART_DURATION
if (isLoopingOverriden && (part.expectedDuration ?? 0) < fallbackPartDuration) {
if (playlist.quickLoop?.forceAutoNext === ForceQuickLoopAutoNext.ENABLED_FORCING_MIN_DURATION) {
part.expectedDuration = fallbackPartDuration
part.expectedDurationWithTransition = fallbackPartDuration
} else if (playlist.quickLoop?.forceAutoNext === ForceQuickLoopAutoNext.ENABLED_WHEN_VALID_DURATION) {
part.invalid = true
part.invalidReason = {
message: generateTranslation('Part duration is 0.'),
}
}
}
if (!canSetAutoNext()) return
part.autoNext = part.autoNext || (isLoopingOverriden && (part.expectedDuration ?? 0) > 0)
}
export function modifyPartInstanceForQuickLoop(
partInstance: Omit<DBPartInstance, 'part.privateData'>,
segmentRanks: Record<string, number>,
rundownRanks: Record<string, number>,
playlist: Pick<DBRundownPlaylist, 'quickLoop'>,
studioSettings: IStudioSettings,
quickLoopStartPosition: MarkerPosition | undefined,
quickLoopEndPosition: MarkerPosition | undefined
): void {
// note that the logic for when a part does not do autonext in quickloop should reflect the logic in the QuickLoopService in job worker
const canAutoNext = () => {
const start = partInstance.timings?.plannedStartedPlayback
if (start !== undefined && partInstance.part.expectedDuration) {
// date.now - start = playback duration, duration + offset gives position in part
const playbackDuration = getCurrentTime() - start
// If there is an auto next planned soon or was in the past
if (partInstance.part.expectedDuration - playbackDuration < 0) {
return false
}
}
return true
}
modifyPartForQuickLoop(
partInstance.part,
segmentRanks,
rundownRanks,
playlist,
studioSettings,
quickLoopStartPosition,
quickLoopEndPosition,
canAutoNext // do not adjust the part instance if we have passed the time where we can still enable auto next
)
}
export function findMarkerPosition(
marker: QuickLoopMarker,
fallback: number,
contentCache: {
segments: ReadonlyObjectDeep<ReactiveCacheCollection<Pick<DBSegment, '_id' | '_rank' | 'rundownId'>>>
parts: ReadonlyObjectDeep<ReactiveCacheCollection<Pick<DBPart, '_id' | '_rank' | 'segmentId'>>>
partInstances?: ReadonlyObjectDeep<ReactiveCacheCollection<DBPartInstance>>
},
rundownRanks: Record<string, number>
): MarkerPosition {
const part =
marker.type === QuickLoopMarkerType.PART
? (contentCache.partInstances?.findOne({ 'part._id': marker.id })?.part ??
contentCache.parts?.findOne(marker.id))
: undefined
const partRank = part?._rank ?? fallback
const segmentId = marker.type === QuickLoopMarkerType.SEGMENT ? marker.id : part?.segmentId
const segment = segmentId && contentCache.segments.findOne(segmentId)
const segmentRank = segment?._rank ?? fallback
const rundownId = marker.type === QuickLoopMarkerType.RUNDOWN ? marker.id : segment?.rundownId
let rundownRank = rundownId ? rundownRanks[unprotectString(rundownId)] : fallback
if (marker.type === QuickLoopMarkerType.PLAYLIST) rundownRank = fallback
return {
rundownRank: rundownRank,
segmentRank: segmentRank,
partRank: partRank,
}
}