  package org.denivip.osmf.net.httpstreaming.hls
 {
 	import flash.net.URLRequest;
+	import flash.utils.ByteArray;
+	import flash.utils.getQualifiedClassName;
 	import flash.utils.getTimer;
 	
 	import org.denivip.osmf.plugins.HLSSettings;
+	import org.denivip.osmf.utility.Padding;
 	import org.osmf.events.DVRStreamInfoEvent;
 	import org.osmf.events.HTTPStreamingEvent;
 	import org.osmf.events.HTTPStreamingIndexHandlerEvent;

				_reloadTime = getTimer() - _reloadTime;
				logger.info("Playlist reload time {0} sec", (_reloadTime/1000));
			}
			
			var quality:int = indexContext as int;
			data = String(data).replace(/\\\s*[\r?\n]\s*/g, "");
			
			if(String(data).localeCompare(_prevPlaylist) == 0)
				++_matchCounter;
			
			if(_matchCounter == HLSSettings.hlsMaxErrors){ // if delivered playlist again not changed then error_event (or all what you want)
				CONFIG::LOGGING
				{
					logger.error("Stream is stuck. Playlist on server don't updated!");
				}
				dispatchEvent(new HTTPStreamingEvent(HTTPStreamingEvent.INDEX_ERROR));
			}
			
			_prevPlaylist = String(data);
			
			var lines:Vector.<String> = Vector.<String>(String(data).split(/\r?\n/));
			var rateItem:HTTPStreamingM3U8IndexRateItem = new HTTPStreamingM3U8IndexRateItem(_streamQualityRates[quality], _streamURLs[quality]);
			var indexItem:HTTPStreamingM3U8IndexItem;
			var len:int = lines.length;
			var discontinuity:Boolean = false;
			var duration:Number = 0;
			for(var i:int = 0; i < len; i++){
				if(i == 0){
					if(lines[i] != '#EXTM3U'){
						dispatchEvent(new HTTPStreamingEvent(HTTPStreamingEvent.INDEX_ERROR));
						return;
					}
				}
				
				if (lines[i].indexOf("#") != 0 && lines[i].length > 0) { //non-empty line not starting with # => segment URI					
					var url:String = (lines[i].search(/(ftp|file|https?):\/\//) == 0) ?  lines[i] : rateItem.url.substr(0, rateItem.url.lastIndexOf('/')+1) + lines[i];
					// spike for hidden discontinuity
					if(url.match(/SegNum(\d+)/)){
						var chunkIndex:int = parseInt(url.match(/SegNum(\d+)/)[1]);
						if(chunkIndex <= _prevChunkIndex)
							discontinuity = true;
						_prevChunkIndex = chunkIndex;
					}
					// _spike
					indexItem = new HTTPStreamingM3U8IndexItem(duration, url, discontinuity);
					rateItem.addIndexItem(indexItem);
					//reset
					discontinuity = false;
					duration = 0;
				}
				else if(lines[i].indexOf("#EXTINF:") == 0){
					duration = parseFloat(lines[i].match(/([\d\.]+)/)[1]);										
				}else if(lines[i].indexOf("#EXT-X-ENDLIST") == 0){
					rateItem.isLive = false;
				}else if(lines[i].indexOf("#EXT-X-MEDIA-SEQUENCE:") == 0){
					rateItem.sequenceNumber = parseInt(lines[i].match(/(\d+)/)[1]);
				}else{
					if(lines[i].indexOf("#EXT-X-TARGETDURATION:") == 0){
						rateItem.targetDuration = parseFloat(lines[i].match(/([\d\.]+)/)[1]);
					}
					if(lines[i].indexOf("#EXT-X-DISCONTINUITY") == 0){
						discontinuity = true;
					}
				}
			}
			
			if(_DVR){
				if(isNaN(_dvrStartTime))
					_dvrStartTime = 0.0;
				else{
					var prevRateItem:HTTPStreamingM3U8IndexRateItem = _rateVec[quality];
					if(prevRateItem){
						len = rateItem.sequenceNumber-prevRateItem.sequenceNumber;
						if(len > prevRateItem.manifest.length){
							len = prevRateItem.manifest.length;
						}
						for(i = 0; i < len; i++){
							_dvrStartTime += prevRateItem.manifest[i].duration;
						}
					}
				}
			}
			
			_rateVec[quality] = rateItem;
			var initialOffset:Number;
			if(rateItem.isLive){
				initialOffset = rateItem.totalTime - ((rateItem.totalTime/rateItem.manifest.length) * 3);
				if(initialOffset < rateItem.totalTime - 30)
					initialOffset = rateItem.totalTime - 30;
			}
			notifyIndexReady(quality, initialOffset);
			if(rateItem.isLive){
				notifyTotalDuration(rateItem.totalTime, rateItem.isLive);
				_quality = quality;
			}
		}
		

					_fileHandler.initialOffset = manifest[_segment].startTime;
					//dispatchEvent(new HTTPHLSStreamingEvent(HTTPHLSStreamingEvent.DISCONTINUITY));
				}
				// Set key and iv on fileHandler
				_fileHandler.key = getCurrentKey();
				_fileHandler.iv = getCurrentIv();
				
				// Increment segments
				++_segment;
				++_absoluteSegment;
			}
			
			return request;
		}
		
		public function getCurrentKey():HTTPStreamingM3U8IndexKey {
			var currentSegment:int = _segment;
			var rateItem:HTTPStreamingM3U8IndexRateItem = _rateVec[_quality];
			if (rateItem.key) {
				var manifest:Vector.<HTTPStreamingM3U8IndexItem> = rateItem.manifest;
				if (currentSegment >= 0 && currentSegment < manifest.length) {
					var item:HTTPStreamingM3U8IndexItem = manifest[currentSegment];
					if (item.key >= 0) {
						var keys:Vector.<HTTPStreamingM3U8IndexKey> = rateItem.key;
						if (item.key < keys.length) {
							return keys[item.key];
						}
					}
				}
			}
			return null;
		}
		
		public function getCurrentIv():String {
			var currentSegment:int = _segment;
			var rateItem:HTTPStreamingM3U8IndexRateItem = _rateVec[_quality];
			if (rateItem.key) {
				var manifest:Vector.<HTTPStreamingM3U8IndexItem> = rateItem.manifest;
				if (currentSegment >= 0 && currentSegment < manifest.length) {
					var item:HTTPStreamingM3U8IndexItem = manifest[currentSegment];
					if (item.key >= 0) {
						var keys:Vector.<HTTPStreamingM3U8IndexKey> = rateItem.key;
						if (item.key < keys.length) {
							return item.iv;
						}
					}
				}
			}
			return null;
		}
		
		/*
