segments method

List<UDashSegmentRef> segments({
  1. required Duration periodDuration,
  2. String? parentBase,
})

Implementation

List<UDashSegmentRef> segments({required Duration periodDuration, String? parentBase}) {
  final UDashSegmentTemplate? t = template;
  if (t == null || t.media == null) return const <UDashSegmentRef>[];
  final int timescale = t.timescale <= 0 ? 1 : t.timescale;
  final List<UDashSegmentRef> result = <UDashSegmentRef>[];

  if (t.hasTimeline) {
    int number = t.startNumber;
    int time = 0;
    for (final List<int> entry in t.timeline) {
      final int startTime = entry[0] >= 0 ? entry[0] : time;
      final int segmentDuration = entry[1];
      final int repeat = entry[2];
      time = startTime;
      for (int i = 0; i <= repeat; i++) {
        result.add(
          UDashSegmentRef(
            url: _resolve(_expand(t.media!, number, time), parentBase),
            start: Duration(milliseconds: (time / timescale * 1000).round()),
            duration: Duration(milliseconds: (segmentDuration / timescale * 1000).round()),
            number: number,
          ),
        );
        time += segmentDuration;
        number++;
        if (result.length > 20000) return result;
      }
    }
    return result;
  }

  if (t.duration <= 0) return const <UDashSegmentRef>[];
  final double segmentSeconds = t.duration / timescale;
  final int count = (periodDuration.inMilliseconds / 1000 / segmentSeconds).ceil();
  final int capped = count > 20000 ? 20000 : count;
  for (int i = 0; i < capped; i++) {
    final int number = t.startNumber + i;
    result.add(
      UDashSegmentRef(
        url: _resolve(_expand(t.media!, number, i * t.duration), parentBase),
        start: Duration(milliseconds: (i * segmentSeconds * 1000).round()),
        duration: Duration(milliseconds: (segmentSeconds * 1000).round()),
        number: number,
      ),
    );
  }
  return result;
}