private void HandleByteRange(SubStreamSegment segment, ByterangeTagInstance byteRange)
 {
     if (byteRange.Offset.HasValue)
     {
         segment.Offset        = byteRange.Offset.Value;
         this._byteRangeOffset = byteRange.Offset.Value;
     }
     else
     {
         segment.Offset = this._byteRangeOffset;
     }
     segment.Length         = byteRange.Length;
     this._byteRangeOffset += byteRange.Length;
 }
        private ISegment CreateStreamSegment(M3U8Parser.M3U8Uri uri, CancellationToken cancellationToken)
        {
            SubStreamSegment segment = new SubStreamSegment(M3U8ParserExtensions.ResolveUrl(this._parser, uri.Uri), this._parser.BaseUrl);

            if (this._mediaSequence.HasValue)
            {
                SubStreamSegment subStreamSegment = segment;
                long?            nullable1        = this._mediaSequence;
                long             num       = (long)this._segmentIndex;
                long?            nullable2 = nullable1.HasValue ? new long?(nullable1.GetValueOrDefault() + num) : new long?();
                subStreamSegment.MediaSequence = nullable2;
            }
            ++this._segmentIndex;
            M3U8TagInstance[] m3U8TagInstanceArray = uri.Tags;
            if (m3U8TagInstanceArray == null || 0 == m3U8TagInstanceArray.Length)
            {
                return((ISegment)segment);
            }
            ExtinfTagInstance extinfTagInstance = M3U8Tags.ExtXInf.Find((IEnumerable <M3U8TagInstance>)m3U8TagInstanceArray);

            if (null != extinfTagInstance)
            {
                segment.Duration = new TimeSpan?(TimeSpan.FromSeconds((double)extinfTagInstance.Duration));
            }
            ByterangeTagInstance byteRange = M3U8Tags.ExtXByteRange.Find((IEnumerable <M3U8TagInstance>)m3U8TagInstanceArray);

            if (null != byteRange)
            {
                this.HandleByteRange(segment, byteRange);
            }
            IEnumerable <ExtKeyTagInstance> all = M3U8Tags.ExtXKey.FindAll((IEnumerable <M3U8TagInstance>)m3U8TagInstanceArray);

            if (null != all)
            {
                this.HandleKey(segment, all, cancellationToken);
            }
            return((ISegment)segment);
        }
        private void HandleKey(SubStreamSegment segment, IEnumerable <ExtKeyTagInstance> extKeys, CancellationToken cancellationToken)
        {
            ExtKeyTagInstance[] extKeyTagInstanceArray = Enumerable.ToArray <ExtKeyTagInstance>(extKeys);
            if (extKeyTagInstanceArray.Length < 1)
            {
                return;
            }
            string url = (string)null;

            byte[] iv = (byte[])null;
            foreach (ExtKeyTagInstance extKeyTagInstance in extKeyTagInstanceArray)
            {
                string b = M3U8TagInstanceExtensions.AttributeObject <string>((M3U8TagInstance)extKeyTagInstance, ExtKeySupport.AttrMethod);
                if (string.Equals("NONE", b, StringComparison.OrdinalIgnoreCase))
                {
                    url = (string)null;
                }
                else
                {
                    if (!string.Equals("AES-128", b, StringComparison.OrdinalIgnoreCase))
                    {
                        if (string.Equals("SAMPLE-AES", b, StringComparison.OrdinalIgnoreCase))
                        {
                            throw new NotImplementedException("Method SAMPLE-AES decryption is not implemented");
                        }
                        throw new NotSupportedException("Unknown decryption method type: " + b);
                    }
                    string str = M3U8TagInstanceExtensions.AttributeObject <string>((M3U8TagInstance)extKeyTagInstance, ExtKeySupport.AttrUri);
                    if (null != str)
                    {
                        url = str;
                    }
                    byte[] numArray = M3U8TagInstanceExtensions.AttributeObject <byte[]>((M3U8TagInstance)extKeyTagInstance, ExtKeySupport.AttrIv);
                    if (null != numArray)
                    {
                        iv = numArray;
                    }
                }
            }
            if (null == url)
            {
                return;
            }
            if (null == iv)
            {
                iv = new byte[16];
                long num = segment.MediaSequence ?? (long)(this._segmentIndex - 1);
                iv[15] = (byte)((ulong)num & (ulong)byte.MaxValue);
                iv[14] = (byte)((ulong)(num >> 8) & (ulong)byte.MaxValue);
                iv[13] = (byte)((ulong)(num >> 16) & (ulong)byte.MaxValue);
                iv[12] = (byte)((ulong)(num >> 24) & (ulong)byte.MaxValue);
            }
            Func <Stream, CancellationToken, Task <Stream> > filter = segment.AsyncStreamFilter;
            Uri uri = M3U8ParserExtensions.ResolveUrl(this._parser, url);

            segment.AsyncStreamFilter = (Func <Stream, CancellationToken, Task <Stream> >)(async(stream, ct) =>
            {
                if (null != filter)
                {
                    stream = await filter(stream, ct).ConfigureAwait(false);
                }
                byte[] key;
                if (!this._keyCache.TryGetValue(uri, out key))
                {
                    key = await this.LoadKeyAsync(uri, cancellationToken).ConfigureAwait(false);
                    if (16 != key.Length)
                    {
                        throw new FormatException("AES-128 key length mismatch: " + (object)key.Length);
                    }
                    this._keyCache[uri] = key;
                }
                Debug.WriteLine("Segment AES-128: key {0} iv {1}", (object)BitConverter.ToString(key), (object)BitConverter.ToString(iv));
                return(this._platformServices.Aes128DecryptionFilter(stream, key, iv));
            });
        }