protected override void Write(M3UFileInfo fileInfo, string value, LineReader reader) { var source = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .Select(e => KV.Parse(e, '=').Value); if (fileInfo.Streams == null && source.Any()) { fileInfo.Streams = new List <M3UStreamInfo>(); } var m3UstreamInfo = new M3UStreamInfo(); foreach (var keyValuePair in source) { var key = keyValuePair.Key; if (key != "BANDWIDTH") { if (key != "PROGRAM-ID") { if (key != "CODECS") { if (key == "RESOLUTION") { m3UstreamInfo.Resolution = keyValuePair.Value; } } else { m3UstreamInfo.Codecs = keyValuePair.Value; } } else { m3UstreamInfo.ProgramId = To.Value <int>(keyValuePair.Value); } } else { m3UstreamInfo.Bandwidth = To.Value <int>(keyValuePair.Value); } } if (!reader.MoveNext()) { throw new InvalidDataException("Invalid M3U file. Missing a stream URI."); } var relativeUri = new Uri(reader.Current.Trim(), UriKind.RelativeOrAbsolute); if (!relativeUri.IsAbsoluteUri && !relativeUri.IsWellFormedOriginalString()) { throw new InvalidDataException("Invalid M3U file. Include a invalid stream URI.", new UriFormatException(reader.Current)); } if (!relativeUri.IsAbsoluteUri) { var baseUri = Configuration.Default.BaseUri; if (baseUri == null && reader.Adapter is NetworkAdapter adapter) { baseUri = new Uri( adapter.Uri.GetComponents(UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.SafeUnescaped), UriKind.Absolute); } if (baseUri != null) { m3UstreamInfo.Uri = new Uri(baseUri, relativeUri); } } if (m3UstreamInfo.Uri == null) { m3UstreamInfo.Uri = relativeUri; } fileInfo.Streams?.Add(m3UstreamInfo); }
protected override void Write(M3UFileInfo fileInfo, string value, LineReader reader) { var attrs = value .Split(new[] { M3UAttributes.AttributeSeparator }, StringSplitOptions.RemoveEmptyEntries) .Select(e => KV.Parse(e, M3UAttributes.PairSeparator).Value); if (fileInfo.Streams == null && attrs.Any()) { fileInfo.Streams = new List <M3UStreamInfo>(); } var stream = new M3UStreamInfo(); foreach (var attr in attrs) { switch (attr.Key) { case M3UAttributes.StreamInfAttributes.Bandwidth: stream.Bandwidth = To.Value <int>(attr.Value); break; case M3UAttributes.StreamInfAttributes.ProgramId: stream.ProgramId = To.Value <int>(attr.Value); break; case M3UAttributes.StreamInfAttributes.Codecs: stream.Codecs = attr.Value; break; case M3UAttributes.StreamInfAttributes.Resolution: stream.Resolution = attr.Value; break; } } if (!reader.MoveNext()) { throw new InvalidDataException("Invalid M3U file. Missing a stream URI."); } var streamUri = new Uri(reader.Current.Trim(), UriKind.RelativeOrAbsolute); if (!streamUri.IsAbsoluteUri && !streamUri.IsWellFormedOriginalString()) { throw new InvalidDataException("Invalid M3U file. Include a invalid stream URI.", innerException: new UriFormatException(reader.Current)); } if (!streamUri.IsAbsoluteUri) { var baseUri = Configuration.Default.BaseUri; if (baseUri == null && reader.Adapter is NetworkAdapter adapter) { var uri = adapter.Uri; var components = UriComponents.SchemeAndServer | UriComponents.UserInfo; baseUri = new Uri(uri.GetComponents(components, UriFormat.SafeUnescaped), UriKind.Absolute); } if (baseUri != null) { stream.Uri = new Uri(baseUri, streamUri); } } if (stream.Uri == null) { stream.Uri = streamUri; } fileInfo.Streams.Add(stream); }