parseMDP method

List<_StreamInfo> parseMDP(
  1. XmlDocument root
)

Implementation

List<_StreamInfo> parseMDP(xml.XmlDocument root) {
  if (root.getAttribute('type') == 'dynamic') {
    return const [];
  }

  final formats = <_StreamInfo>[];
  final periods = root.findAllElements('Period');
  for (final period in periods) {
    final periodMsInfo = extractMultiSegmentInfo(period, _MsInfo());
    final adaptionSets = period.findAllElements('AdaptationSet');
    for (final adaptionSet in adaptionSets) {
      if (_isDrmProtected(adaptionSet)) {
        continue;
      }
      final adaptionSetMsInfo =
          extractMultiSegmentInfo(adaptionSet, periodMsInfo);
      for (final representation
          in adaptionSet.findAllElements('Representation')) {
        if (_isDrmProtected(representation)) {
          continue;
        }
        final representationAttrib = {
          for (final e in adaptionSet.attributes) e.name.local: e.value,
          for (final e in representation.attributes) e.name.local: e.value,
        };

        final mimeType = MediaType.parse(representationAttrib['mimeType']!);

        if (mimeType.type == 'video' || mimeType.type == 'audio') {
          // Extract the base url
          final baseUrl = <xml.XmlElement>[
            ...representation.childElements,
            ...adaptionSet.childElements,
            ...period.childElements,
            ...root.childElements,
          ]
              .firstWhereOrNull((e) {
                final baseUrlE = e.getElement('BaseURL')?.innerText.trim();
                if (baseUrlE == null) {
                  return false;
                }
                return baseUrlE.contains(RegExp('^https?://'));
              })
              ?.innerText
              .trim();

          if (baseUrl == null || !baseUrl.startsWith('http')) {
            throw UnimplementedError(
                'This kind of DASH Stream is not yet implemented. '
                'Please open a new issue on this project GitHub.');
          }

          final representationMsInfo =
              extractMultiSegmentInfo(representation, adaptionSetMsInfo);

          if (representationMsInfo.segmentUrls != null &&
              representationMsInfo.segmentTimeline != null) {
            final fragments = <Fragment>[];
            var segmentIndex = 0;
            for (final s in representationMsInfo.segmentTimeline!.segments) {
              for (var i = 0; i < (s.r + 1); i++) {
                final segmentUri =
                    representationMsInfo.segmentUrls![segmentIndex];
                if (segmentUri.contains(RegExp('^https?://'))) {
                  throw UnimplementedError(
                      'This kind of DASH Stream is not yet implemented. '
                      'Please open a new issue on this project GitHub.');
                }
                fragments.add(Fragment(segmentUri));
                segmentIndex++;
              }
            }
            representationMsInfo.fragments = fragments;
          }

          final fragments = <Fragment>[
            if (representationMsInfo.fragments != null &&
                representationMsInfo.initializationUrl != null)
              Fragment(representationMsInfo.initializationUrl!),
            ...?representationMsInfo.fragments,
          ];

          formats.add(
            _StreamInfo(
              int.parse(representationAttrib['id']!),
              baseUrl,
              mimeType,
              int.tryParse(representationAttrib['width'] ?? ''),
              int.tryParse(representationAttrib['height'] ?? ''),
              int.tryParse(representationAttrib['frameRate'] ?? ''),
              fragments,
            ),
          );
        }
      }
    }
  }

  return formats;
}