58 lines
1.0 KiB
Go
58 lines
1.0 KiB
Go
package playback
|
|
|
|
import "time"
|
|
|
|
type PlaylistTimingState struct {
|
|
Revision uint64
|
|
Duration time.Duration
|
|
Started bool
|
|
Expired bool
|
|
Deadline time.Time
|
|
}
|
|
|
|
func NewPlaylistTiming(
|
|
revision uint64,
|
|
duration time.Duration,
|
|
) PlaylistTimingState {
|
|
return PlaylistTimingState{
|
|
Revision: revision,
|
|
Duration: duration,
|
|
Expired: false,
|
|
}
|
|
}
|
|
|
|
func StartPlaylistTiming(
|
|
current PlaylistTimingState,
|
|
revision uint64,
|
|
now time.Time,
|
|
) (PlaylistTimingState, bool) {
|
|
if revision != current.Revision ||
|
|
current.Duration <= 0 ||
|
|
current.Started ||
|
|
current.Expired {
|
|
return current, false
|
|
}
|
|
next := current
|
|
next.Started = true
|
|
next.Deadline = now.Add(current.Duration)
|
|
return next, true
|
|
}
|
|
|
|
func ExpirePlaylistTiming(
|
|
current PlaylistTimingState,
|
|
revision uint64,
|
|
now time.Time,
|
|
) (PlaylistTimingState, bool) {
|
|
if revision != current.Revision ||
|
|
!current.Started ||
|
|
now.Before(current.Deadline) {
|
|
return current, false
|
|
}
|
|
|
|
next := current
|
|
next.Started = false
|
|
next.Deadline = time.Time{}
|
|
next.Expired = true
|
|
return next, true
|
|
}
|