internal int FindPacket(long granulePos, Func <DataPacket, DataPacket, DataPacket, int> packetGranuleCountCallback) { if (granulePos < 0L) { throw new ArgumentOutOfRangeException("granulePos"); } while (this._last == null || this._last.PageGranulePosition < granulePos) { if (this._eosFound) { if (this._first == null) { throw new InvalidDataException(); } else { return(-1); } } else { this.GetMorePackets(); } } Packet packet = this._last; if (packet.IsContinued) { packet = packet.Prev; } do { if (!packet.GranuleCount.HasValue) { if (packet.Prev != null) { packet.Prev.Reset(); } packet.Reset(); if (packet.Next != null) { packet.Next.Reset(); } packet.GranuleCount = new long?((long)packetGranuleCountCallback((DataPacket)packet.Prev, (DataPacket)packet, (DataPacket)packet.Next)); if (packet == this._last || this._eosFound && packet == this._last.Prev || (packet.Next.IsContinued || packet.Next.PageSequenceNumber > packet.PageSequenceNumber)) { if (packet.PageGranulePosition == -1L) { throw new InvalidDataException(); } packet.GranulePosition = packet.PageGranulePosition; if (packet == this._last && this._eosFound) { packet.GranuleCount = new long?(packet.PageGranulePosition - packet.Prev.PageGranulePosition); } } else { packet.GranulePosition = packet.Next.GranulePosition - packet.Next.GranuleCount.Value; } } if (packet.GranulePosition < granulePos) { packet = packet.Next; break; } else { packet = packet.Prev; } }while (packet != null); if (packet == null) { return(-1); } int num = 0; while (packet.Prev != null) { packet = packet.Prev; ++num; } return(num); }