<commit_before>package com.kaltura.kdpfl.plugin.component
{
	import com.kaltura.KalturaClient;
	import com.kaltura.commands.captionAsset.CaptionAssetGetUrl;
	import com.kaltura.commands.captionAsset.CaptionAssetList;
	import com.kaltura.events.KalturaEvent;
	import com.kaltura.kdpfl.model.MediaProxy;
	import com.kaltura.kdpfl.model.SequenceProxy;
	import com.kaltura.kdpfl.model.ServicesProxy;
	import com.kaltura.kdpfl.model.type.NotificationType;
	import com.kaltura.vo.KalturaCaptionAsset;
	import com.kaltura.vo.KalturaCaptionAssetFilter;
	import com.kaltura.vo.KalturaCaptionAssetListResponse;
	import com.kaltura.vo.KalturaMediaEntry;
	import com.type.ClosedCaptionsNotifications;
	
	import flash.display.DisplayObject;
	import flash.events.AsyncErrorEvent;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.SharedObject;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ClosedCaptionsMediator extends Mediator
	{
		public static const NAME:String = "closedCaptionsMediator";
		
		private var _entryId:String = "";
		private var _flashvars:Object = null;
		private var _defaultFound : Boolean = false;
		private var _closedCaptionsDefs : closedCaptionsPluginCode;
		//new Boolean flag indicating that the entry is currently showing embedded rather than external captions.
		private var _showingEmbeddedCaptions : Boolean = false;
		private var _embeddedCaptionsId : String;
		
		private var _mediaProxy : MediaProxy;

		public function ClosedCaptionsMediator (closedCaptionsDefs:closedCaptionsPluginCode , viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			_closedCaptionsDefs = closedCaptionsDefs;
		}
		
		override public function listNotificationInterests():Array
		{
			return  [
						"mediaReady",
						"mediaLoaded",
						"entryReady",
						"loadMedia",
						"playerUpdatePlayhead",
						"rootResize",
						"closedCaptionsClicked",
						"changedClosedCaptions",
						"layoutReady",
						ClosedCaptionsNotifications.SHOW_HIDE_CLOSED_CAPTIONS,
						"closedCaptionsSelected",
						"closedCaptionsSwitched",
						"newClosedCaptionsData",
						"playerPlayEnd",
						NotificationType.HAS_OPENED_FULL_SCREEN,
						NotificationType.HAS_CLOSED_FULL_SCREEN
					]; 
		}
		
		override public function handleNotification(note:INotification):void
		{
			var eventName:String = note.getName();
			var data:Object = note.getBody();
			_mediaProxy = facade.retrieveProxy(MediaProxy.NAME) as MediaProxy;
			var entry:String = _mediaProxy["vo"]["entry"]["id"];
			
			var sequenceProxy : SequenceProxy = facade.retrieveProxy(SequenceProxy.NAME) as SequenceProxy;
			
			//If the player is currently playing an advertisement, no need to do anything related to captions.
			if(sequenceProxy.vo.isInSequence)
			{
				return;
			}

			switch (eventName)
			{
				case "entryReady":
					(viewComponent as ClosedCaptions).resetAll();
					loadEntryCCData ();
					break;
				case "mediaLoaded":
					addTextHandler();
					
					break;
				case "mediaReady":
				case "changedClosedCaptions":
				{
					
					(view as ClosedCaptions).visible=true
					var config: Object =  facade.retrieveProxy("configProxy");
					var flashvars:Object = config.getData().flashvars;
					
					if (_mediaProxy["vo"]["media"] != null )
					{
						_entryId = entry;
						_flashvars = flashvars;
						
						
						if (_closedCaptionsDefs["type"] == null || _closedCaptionsDefs["type"] == "")
						{
							_closedCaptionsDefs["type"] = "srt";
						}

						if (_closedCaptionsDefs["ccUrl"] != null && _closedCaptionsDefs["ccUrl"] != "")
						{
							(view as ClosedCaptions).loadCaptions(_closedCaptionsDefs["ccUrl"], _closedCaptionsDefs["type"]);
						}
						
					}
				}
				break;
				
				case "layoutReady":
					this.setStyleName(_closedCaptionsDefs.skin);
					
					if (_closedCaptionsDefs.useGlow)
					{
						setGlowFilter();
					}
					else
					{
						this.setBGColor(_closedCaptionsDefs.bg);
					}
					this.setOpacity(_closedCaptionsDefs.opacity);
					this.setFontFamily(_closedCaptionsDefs.fontFamily);
					this.setFontColor( _closedCaptionsDefs.fontColor );
					(viewComponent as ClosedCaptions).defaultFontSize = _closedCaptionsDefs.fontsize;
					if (!_closedCaptionsDefs.fullscreenRatio)
					{
						_closedCaptionsDefs.fullscreenRatio = (viewComponent as ClosedCaptions).stage.fullScreenHeight / (viewComponent as ClosedCaptions).stage.stageHeight;
					}
					(viewComponent as ClosedCaptions).fullScreenRatio = _closedCaptionsDefs.fullscreenRatio;
					(view as ClosedCaptions).addEventListener(IOErrorEvent.IO_ERROR, onCCIOError);
					(view as ClosedCaptions).addEventListener(ErrorEvent.ERROR, onCCGeneralError);
					(view as ClosedCaptions).addEventListener(AsyncErrorEvent.ASYNC_ERROR, onCCGeneralError);
					(view as ClosedCaptions).addEventListener(SecurityErrorEvent.SECURITY_ERROR, onCCGeneralError);
					(view as ClosedCaptions).addEventListener( ClosedCaptions.ERROR_PARSING_SRT, onErrorParsingCC );
					(view as ClosedCaptions).addEventListener( ClosedCaptions.ERROR_PARSING_TT, onErrorParsingCC );
					
					break;
				
				case "playerUpdatePlayhead":
					(view as ClosedCaptions).updatePlayhead (data as Number);
				break;
				
				case "rootResize":
					setScreenSize(data.width, data.height);
				break;

				case "closedCaptionsClicked":
					(view as ClosedCaptions).closedCaptionsClicked ();
				break;
				
				case ClosedCaptionsNotifications.SHOW_HIDE_CLOSED_CAPTIONS:
					(view as ClosedCaptions).visible = !(view as ClosedCaptions).visible;
					break;
				case "closedCaptionsSelected":
					var currentLabel : String = note.getBody().label;
					
					if (currentLabel == _closedCaptionsDefs.noneString)
					{
						(viewComponent as ClosedCaptions).visible = false;
						return;
					}
					else
					{
						(viewComponent as ClosedCaptions).visible = true;
					}
					
					for each (var ccObj : KalturaCaptionAsset in _closedCaptionsDefs.availableCCFiles)
					{
						if (currentLabel == ccObj.label)
						{
							if (ccObj.type == "tx3g")
							{
								switchEmbeddedTrack( ccObj.trackId );
							}
							else
							{
								switchActiveCCFile( ccObj );
							}
							
							if (_flashvars.allowCookies=="true" && ccObj.language )
							{
								try
								{
									var sharedObj : SharedObject = SharedObject.getLocal("Kaltura_CC_SO");
									sharedObj.data.language = ccObj.language;
									sharedObj.flush();
								}
								catch (e : Error)
								{
									sendNotification( NotificationType.ALERT, {message: "Application is unable to access your file system.", title: "Error saving localized settings"} );
								}
							}
							
							break;
						}
					
					}
					
					break;
				case "closedCaptionsSwitched":
					break;
				case "newClosedCaptionsData":
					
					parseEntryCCData();
					
					break;
				case "playerPlayEnd":
					(viewComponent as ClosedCaptions).setText("");
					break;
				case NotificationType.HAS_OPENED_FULL_SCREEN:
					(viewComponent as ClosedCaptions).setFullScreen(true);
//					var time:Number = (facade.retrieveMediator(KMediaPlayerMediator.NAME) as KMediaPlayerMediator).player["currentTime"];
//					(view as ClosedCaptions).updatePlayhead (time);
					break;
				case NotificationType.HAS_CLOSED_FULL_SCREEN:
					(viewComponent as ClosedCaptions).setFullScreen(false);
//					var time:Number = (facade.retrieveMediator(KMediaPlayerMediator.NAME) as KMediaPlayerMediator).player["currentTime"];
//					(view as ClosedCaptions).updatePlayhead (time);
					break;
			}
		}
		
		private function addTextHandler () : void
		{
			if (_mediaProxy.videoElement && _mediaProxy.videoElement.client)
				_mediaProxy.videoElement.client.addHandler( "onTextData" , onTextData );
		}
		
		private function onTextData (info : Object) : void
		{
			if (_showingEmbeddedCaptions)
				(viewComponent as ClosedCaptions).setText(info.text);
		}
		
		private function onCCIOError (e : Event) : void
		{
			sendNotification( ClosedCaptionsNotifications.CC_IO_ERROR );
		}
		
		private function onCCGeneralError (e : Event) : void
		{
			sendNotification( ClosedCaptionsNotifications.CC_ERROR );
		}
		
		private function onErrorParsingCC (e : ErrorEvent) : void
		{
			sendNotification( ClosedCaptionsNotifications.CC_FAILED_TO_VALIDATE );
		}
		
		private function onGetEntryResult(evt:Object):void
		{
			var me:KalturaMediaEntry = evt["data"] as KalturaMediaEntry;
			(view as ClosedCaptions).loadCaptions(me.downloadUrl, _flashvars.captions.type);
		}
		
		private function onGetEntryError(evt:Object):void
		{
			trace ("ClosedCaptionsPlugin: Failed to retrieve media");
		}
		
		private function loadEntryCCData () : void
		{
			var entryCCDataFilter : KalturaCaptionAssetFilter = new KalturaCaptionAssetFilter();
			
			entryCCDataFilter.entryIdEqual = (facade.retrieveProxy(MediaProxy.NAME) as MediaProxy).vo.entry.id;

			var entryCCDataList : CaptionAssetList = new CaptionAssetList( entryCCDataFilter );
			
			var kalturaClient : KalturaClient = (facade.retrieveProxy(ServicesProxy.NAME) as ServicesProxy).vo.kalturaClient;
			
			entryCCDataList.addEventListener( KalturaEvent.COMPLETE, captionsListSuccess );
			entryCCDataList.addEventListener( KalturaEvent.FAILED, captionsListFault );
			
			kalturaClient.post( entryCCDataList );
		}
		
		private function captionsListSuccess (e : KalturaEvent=null) : void
		{
			_closedCaptionsDefs.availableCCFiles = new Array();
			
			var listResponse : KalturaCaptionAssetListResponse = e.data as KalturaCaptionAssetListResponse;
			
			if (listResponse.objects && listResponse.objects.length) {
				for each (var ccItem : KalturaCaptionAsset in listResponse.objects )
				{
					_closedCaptionsDefs.availableCCFiles.push( ccItem );
				}
				_closedCaptionsDefs.hasCaptions = true;
			}
			else {
				_closedCaptionsDefs.hasCaptions = false;
			}

			if ( _closedCaptionsDefs.hasCaptions )
			{
				parseEntryCCData();
				sendNotification( ClosedCaptionsNotifications.CC_DATA_LOADED );<commit_msg>ps: CR-37802 - hide IOS captions from flash player <commit_after>package com.kaltura.kdpfl.plugin.component
{
	import com.kaltura.KalturaClient;
	import com.kaltura.commands.captionAsset.CaptionAssetGetUrl;
	import com.kaltura.commands.captionAsset.CaptionAssetList;
	import com.kaltura.events.KalturaEvent;
	import com.kaltura.kdpfl.model.MediaProxy;
	import com.kaltura.kdpfl.model.SequenceProxy;
	import com.kaltura.kdpfl.model.ServicesProxy;
	import com.kaltura.kdpfl.model.type.NotificationType;
	import com.kaltura.types.KalturaCaptionType;
	import com.kaltura.vo.KalturaCaptionAsset;
	import com.kaltura.vo.KalturaCaptionAssetFilter;
	import com.kaltura.vo.KalturaCaptionAssetListResponse;
	import com.kaltura.vo.KalturaMediaEntry;
	import com.type.ClosedCaptionsNotifications;
	
	import flash.display.DisplayObject;
	import flash.events.AsyncErrorEvent;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.SharedObject;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class ClosedCaptionsMediator extends Mediator
	{
		public static const NAME:String = "closedCaptionsMediator";
		
		private var _entryId:String = "";
		private var _flashvars:Object = null;
		private var _defaultFound : Boolean = false;
		private var _closedCaptionsDefs : closedCaptionsPluginCode;
		//new Boolean flag indicating that the entry is currently showing embedded rather than external captions.
		private var _showingEmbeddedCaptions : Boolean = false;
		private var _embeddedCaptionsId : String;
		
		private var _mediaProxy : MediaProxy;

		public function ClosedCaptionsMediator (closedCaptionsDefs:closedCaptionsPluginCode , viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			_closedCaptionsDefs = closedCaptionsDefs;
		}
		
		override public function listNotificationInterests():Array
		{
			return  [
						"mediaReady",
						"mediaLoaded",
						"entryReady",
						"loadMedia",
						"playerUpdatePlayhead",
						"rootResize",
						"closedCaptionsClicked",
						"changedClosedCaptions",
						"layoutReady",
						ClosedCaptionsNotifications.SHOW_HIDE_CLOSED_CAPTIONS,
						"closedCaptionsSelected",
						"closedCaptionsSwitched",
						"newClosedCaptionsData",
						"playerPlayEnd",
						NotificationType.HAS_OPENED_FULL_SCREEN,
						NotificationType.HAS_CLOSED_FULL_SCREEN
					]; 
		}
		
		override public function handleNotification(note:INotification):void
		{
			var eventName:String = note.getName();
			var data:Object = note.getBody();
			_mediaProxy = facade.retrieveProxy(MediaProxy.NAME) as MediaProxy;
			var entry:String = _mediaProxy["vo"]["entry"]["id"];
			
			var sequenceProxy : SequenceProxy = facade.retrieveProxy(SequenceProxy.NAME) as SequenceProxy;
			
			//If the player is currently playing an advertisement, no need to do anything related to captions.
			if(sequenceProxy.vo.isInSequence)
			{
				return;
			}

			switch (eventName)
			{
				case "entryReady":
					(viewComponent as ClosedCaptions).resetAll();
					loadEntryCCData ();
					break;
				case "mediaLoaded":
					addTextHandler();
					
					break;
				case "mediaReady":
				case "changedClosedCaptions":
				{
					
					(view as ClosedCaptions).visible=true
					var config: Object =  facade.retrieveProxy("configProxy");
					var flashvars:Object = config.getData().flashvars;
					
					if (_mediaProxy["vo"]["media"] != null )
					{
						_entryId = entry;
						_flashvars = flashvars;
						
						
						if (_closedCaptionsDefs["type"] == null || _closedCaptionsDefs["type"] == "")
						{
							_closedCaptionsDefs["type"] = "srt";
						}

						if (_closedCaptionsDefs["ccUrl"] != null && _closedCaptionsDefs["ccUrl"] != "")
						{
							(view as ClosedCaptions).loadCaptions(_closedCaptionsDefs["ccUrl"], _closedCaptionsDefs["type"]);
						}
						
					}
				}
				break;
				
				case "layoutReady":
					this.setStyleName(_closedCaptionsDefs.skin);
					
					if (_closedCaptionsDefs.useGlow)
					{
						setGlowFilter();
					}
					else
					{
						this.setBGColor(_closedCaptionsDefs.bg);
					}
					this.setOpacity(_closedCaptionsDefs.opacity);
					this.setFontFamily(_closedCaptionsDefs.fontFamily);
					this.setFontColor( _closedCaptionsDefs.fontColor );
					(viewComponent as ClosedCaptions).defaultFontSize = _closedCaptionsDefs.fontsize;
					if (!_closedCaptionsDefs.fullscreenRatio)
					{
						_closedCaptionsDefs.fullscreenRatio = (viewComponent as ClosedCaptions).stage.fullScreenHeight / (viewComponent as ClosedCaptions).stage.stageHeight;
					}
					(viewComponent as ClosedCaptions).fullScreenRatio = _closedCaptionsDefs.fullscreenRatio;
					(view as ClosedCaptions).addEventListener(IOErrorEvent.IO_ERROR, onCCIOError);
					(view as ClosedCaptions).addEventListener(ErrorEvent.ERROR, onCCGeneralError);
					(view as ClosedCaptions).addEventListener(AsyncErrorEvent.ASYNC_ERROR, onCCGeneralError);
					(view as ClosedCaptions).addEventListener(SecurityErrorEvent.SECURITY_ERROR, onCCGeneralError);
					(view as ClosedCaptions).addEventListener( ClosedCaptions.ERROR_PARSING_SRT, onErrorParsingCC );
					(view as ClosedCaptions).addEventListener( ClosedCaptions.ERROR_PARSING_TT, onErrorParsingCC );
					
					break;
				
				case "playerUpdatePlayhead":
					(view as ClosedCaptions).updatePlayhead (data as Number);
				break;
				
				case "rootResize":
					setScreenSize(data.width, data.height);
				break;

				case "closedCaptionsClicked":
					(view as ClosedCaptions).closedCaptionsClicked ();
				break;
				
				case ClosedCaptionsNotifications.SHOW_HIDE_CLOSED_CAPTIONS:
					(view as ClosedCaptions).visible = !(view as ClosedCaptions).visible;
					break;
				case "closedCaptionsSelected":
					var currentLabel : String = note.getBody().label;
					
					if (currentLabel == _closedCaptionsDefs.noneString)
					{
						(viewComponent as ClosedCaptions).visible = false;
						return;
					}
					else
					{
						(viewComponent as ClosedCaptions).visible = true;
					}
					
					for each (var ccObj : KalturaCaptionAsset in _closedCaptionsDefs.availableCCFiles)
					{
						if (currentLabel == ccObj.label)
						{
							if (ccObj.type == "tx3g")
							{
								switchEmbeddedTrack( ccObj.trackId );
							}
							else
							{
								switchActiveCCFile( ccObj );
							}
							
							if (_flashvars.allowCookies=="true" && ccObj.language )
							{
								try
								{
									var sharedObj : SharedObject = SharedObject.getLocal("Kaltura_CC_SO");
									sharedObj.data.language = ccObj.language;
									sharedObj.flush();
								}
								catch (e : Error)
								{
									sendNotification( NotificationType.ALERT, {message: "Application is unable to access your file system.", title: "Error saving localized settings"} );
								}
							}
							
							break;
						}
					
					}
					
					break;
				case "closedCaptionsSwitched":
					break;
				case "newClosedCaptionsData":
					
					parseEntryCCData();
					
					break;
				case "playerPlayEnd":
					(viewComponent as ClosedCaptions).setText("");
					break;
				case NotificationType.HAS_OPENED_FULL_SCREEN:
					(viewComponent as ClosedCaptions).setFullScreen(true);
//					var time:Number = (facade.retrieveMediator(KMediaPlayerMediator.NAME) as KMediaPlayerMediator).player["currentTime"];
//					(view as ClosedCaptions).updatePlayhead (time);
					break;
				case NotificationType.HAS_CLOSED_FULL_SCREEN:
					(viewComponent as ClosedCaptions).setFullScreen(false);
//					var time:Number = (facade.retrieveMediator(KMediaPlayerMediator.NAME) as KMediaPlayerMediator).player["currentTime"];
//					(view as ClosedCaptions).updatePlayhead (time);
					break;
			}
		}
		
		private function addTextHandler () : void
		{
			if (_mediaProxy.videoElement && _mediaProxy.videoElement.client)
				_mediaProxy.videoElement.client.addHandler( "onTextData" , onTextData );
		}
		
		private function onTextData (info : Object) : void
		{
			if (_showingEmbeddedCaptions)
				(viewComponent as ClosedCaptions).setText(info.text);
		}
		
		private function onCCIOError (e : Event) : void
		{
			sendNotification( ClosedCaptionsNotifications.CC_IO_ERROR );
		}
		
		private function onCCGeneralError (e : Event) : void
		{
			sendNotification( ClosedCaptionsNotifications.CC_ERROR );
		}
		
		private function onErrorParsingCC (e : ErrorEvent) : void
		{
			sendNotification( ClosedCaptionsNotifications.CC_FAILED_TO_VALIDATE );
		}
		
		private function onGetEntryResult(evt:Object):void
		{
			var me:KalturaMediaEntry = evt["data"] as KalturaMediaEntry;
			(view as ClosedCaptions).loadCaptions(me.downloadUrl, _flashvars.captions.type);
		}
		
		private function onGetEntryError(evt:Object):void
		{
			trace ("ClosedCaptionsPlugin: Failed to retrieve media");
		}
		
		private function loadEntryCCData () : void
		{
			var entryCCDataFilter : KalturaCaptionAssetFilter = new KalturaCaptionAssetFilter();
			
			entryCCDataFilter.entryIdEqual = (facade.retrieveProxy(MediaProxy.NAME) as MediaProxy).vo.entry.id;
			//filter only to XML and SRT (Hiding the WebVTT CC file in m3u8 that is relevant just for IOS)
			entryCCDataFilter.formatIn = KalturaCaptionType.SRT+","+KalturaCaptionType.DFXP;
			
			var entryCCDataList : CaptionAssetList = new CaptionAssetList( entryCCDataFilter );
			
			var kalturaClient : KalturaClient = (facade.retrieveProxy(ServicesProxy.NAME) as ServicesProxy).vo.kalturaClient;
			
			entryCCDataList.addEventListener( KalturaEvent.COMPLETE, captionsListSuccess );
			entryCCDataList.addEventListener( KalturaEvent.FAILED, captionsListFault );
			
			kalturaClient.post( entryCCDataList );
		}
		
		private function captionsListSuccess (e : KalturaEvent=null) : void
		{
			_closedCaptionsDefs.availableCCFiles = new Array();
			
			var listResponse : KalturaCaptionAssetListResponse = e.data as KalturaCaptionAssetListResponse;
			
			if (listResponse.objects && listResponse.objects.length) {
				for each (var ccItem : KalturaCaptionAsset in listResponse.objects )
				{
					_closedCaptionsDefs.availableCCFiles.push( ccItem );
				}
				_closedCaptionsDefs.hasCaptions = true;
			}
			else {
				_closedCaptionsDefs.hasCaptions = false;
			}

			if ( _closedCaptionsDefs.hasCaptions )
			{
				parseEntryCCData();
				sendNotification( ClosedCaptionsNotifications.CC_DATA_LOADED );
{'fraction_non_alphanumeric': 0.09339407744874716, 'fraction_numerical': 0.000630058643919934, 'mean_word_length': 3.622311827956989, 'pattern_counts': {'":': 24, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5017042', 'n_tokens_mistral': 8234, 'n_tokens_neox': 6263, 'n_words': 1137}