<commit_before>( function( mw, $ ) {"use strict";

	// Add chromecast player:
	$( mw ).bind('EmbedPlayerUpdateMediaPlayers', function( event, mediaPlayers ){
		var chromecastSupportedProtocols = ['video/mp4'];
		var chromecastPlayer = new mw.MediaPlayer( 'chromecast', chromecastSupportedProtocols, 'Chromecast' );
		mediaPlayers.addPlayer( chromecastPlayer );
		// add 
		$.each( chromecastSupportedProtocols, function(inx, mimeType){
			if( mediaPlayers.defaultPlayers[ mimeType ] ){
				mediaPlayers.defaultPlayers[ mimeType ].push( 'Chromecast' );
				return true;
			}
			mediaPlayers.defaultPlayers[ mimeType ] = ['Chromecast'];
		});
	});

	mw.PluginManager.add( 'chromecast', mw.KBaseComponent.extend({

		defaultConfig: {
			'parent': 'controlsContainer',
			'order': 7,
			'visible': false,
			'align': "right",
			'applicationID': "DB6462E9",
			'showTooltip': true,
			'tooltip': 'Chromecast'
		},
		isDisabled: false,

		progressFlag: 1,
		currentMediaSession: null,
		mediaCurrentTime: 0,
		mediaDuration: null,
		casting: false,
		session: null,
		request: null,
		updateInterval: null,

		monitorInterval: null,

		savedPlayer: null, // save player before cast
		savedVolume: 0,	// save player volume before cast
		savedPosition: 0,  // save video position before cast

		startCastTitle: gM( 'mwe-chromecast-startcast' ),
		stopCastTitle: gM( 'mwe-chromecast-stopcast' ),

		receiverName: '',

		setup: function( embedPlayer ) {
			var _this = this;
			this.addBindings();
			window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
				if (loaded) {
					_this.initializeCastApi();
				} else {
					_this.log(errorInfo);
				}
			};
		},

		addBindings: function() {
			var _this = this;
			this.bind('chromecastPlay', function(){_this.playMedia();});
			this.bind('chromecastPause', function(){_this.pauseMedia();});
			this.bind('chromecastSwitchMedia', function(e, url, mime){_this.loadMedia(url, mime);});
			this.bind('chromecastGetCurrentTime', function(){_this.getCurrentTime();});
			this.bind('chromecastSetVolume', function(e, percent){_this.setVolume(e,percent);});
			this.bind('chromecastSeek', function(e, percent){_this.seekMedia(percent);});
			this.bind('stopCasting', function(){_this.toggleCast();});

			$( this.embedPlayer).bind('chromecastDeviceConnected', function(){
				_this.getComponent().css("color","#35BCDA");
				$(_this.embedPlayer).html(_this.getPlayingScreen());
				$(".chromecastThumb").load(function(){
					setTimeout(function(){
						_this.setPlayingScreen();
					},0);
				});
			});
			$( this.embedPlayer).bind('chromecastDeviceDisConnected', function(){
				_this.getComponent().css("color","white");
				_this.embedPlayer.disablePlayer();
				_this.embedPlayer.updatePlaybackInterface()
			});

			$( this.embedPlayer).bind('chromecastShowConnectingMsg', function(){
				_this.showConnectingMessage();
			});
		},

		getComponent: function() {
			var _this = this;
			if( !this.$el ) {
				this.$el = $( '<button/>' )
					.attr( 'title', this.startCastTitle )
					.addClass( "btn icon-chromecast" + this.getCssClass() )
					.click( function() {
						if( _this.embedPlayer.selectedPlayer && _this.embedPlayer.selectedPlayer.library != "NativeComponent" ) {
								_this.toggleCast();
						} else {
							// 'NativeComponent' event for showing chromecast device list on mobile native apps
							$( _this.embedPlayer ).trigger( 'showChromecastDeviceList' );

							var chromeCastSource = _this.getChromecastSource();
							// set source using a timeout to avoid setting auto source by Akamai Analytics
							setTimeout(function() {
								_this.embedPlayer.mediaElement.setSource(chromeCastSource);
							},300);
						}
					});
			}
			return this.$el;
		},

		showConnectingMessage: function(){
			this.embedPlayer.showErrorMsg(
				{'title':'Chromecast Player',
					'message': gM('mwe-chromecast-connecting'),
					'props':{
						'customAlertContainerCssClass': 'connectingMsg',
						'customAlertTitleCssClass': 'hidden',
						'textColor': '#ffffff'
					}
				}
			);
		},

		toggleCast : function(){
			if (this.isDisabled){
				return false;
			}
			var _this = this;
			if (!this.casting){
				// launch app
				this.showConnectingMessage();
				this.embedPlayer.disablePlayControls(["chromecast"]);
				chrome.cast.requestSession(
					function(e){
						_this.onRequestSessionSuccess(e);
					}, 
					function(){
						_this.onLaunchError();
					}
				);
			}else{
				// stop casting
				this.stopMedia();
				this.stopApp();
			}
		},

		onRequestSessionSuccess: function(e) {
			this.embedPlayer.layoutBuilder.closeAlert();
			this.log( "Session success: " + e.sessionId);
			this.session = e;
			this.getComponent().css("color","#35BCDA");
			this.updateTooltip(this.stopCastTitle);
			this.casting = true;
			this.loadMedia();
		},

		onLaunchError: function() {
			this.embedPlayer.layoutBuilder.closeAlert();
			this.embedPlayer.enablePlayControls();
			this.log("launch error");
		},

		initializeCastApi: function() {
			var _this = this;
			var sessionRequest = new chrome.cast.SessionRequest(this.getConfig("applicationID")); // 'Castv2Player'
			var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
				function(event){
					_this.sessionListener(event);
				}, 
				function(event){
					_this.receiverListener(event);
				}
			);
			chrome.cast.initialize(apiConfig, 
				function(){
					_this.onInitSuccess();
				}, 
				function(){
					_this.onError();
				}
			);
		},

		sessionListener: function( e ) {
			this.log("New session ID: ' + e.sessionId);");
			this.session = e;
			if (this.session.media.length !== 0) {
				this.log('Found ' + this.session.media.length + ' existing media sessions.');<commit_msg>expose launch error message in console for debugging<commit_after>( function( mw, $ ) {"use strict";

	// Add chromecast player:
	$( mw ).bind('EmbedPlayerUpdateMediaPlayers', function( event, mediaPlayers ){
		var chromecastSupportedProtocols = ['video/mp4'];
		var chromecastPlayer = new mw.MediaPlayer( 'chromecast', chromecastSupportedProtocols, 'Chromecast' );
		mediaPlayers.addPlayer( chromecastPlayer );
		// add 
		$.each( chromecastSupportedProtocols, function(inx, mimeType){
			if( mediaPlayers.defaultPlayers[ mimeType ] ){
				mediaPlayers.defaultPlayers[ mimeType ].push( 'Chromecast' );
				return true;
			}
			mediaPlayers.defaultPlayers[ mimeType ] = ['Chromecast'];
		});
	});

	mw.PluginManager.add( 'chromecast', mw.KBaseComponent.extend({

		defaultConfig: {
			'parent': 'controlsContainer',
			'order': 7,
			'visible': false,
			'align': "right",
			'applicationID': "DB6462E9",
			'showTooltip': true,
			'tooltip': 'Chromecast'
		},
		isDisabled: false,

		progressFlag: 1,
		currentMediaSession: null,
		mediaCurrentTime: 0,
		mediaDuration: null,
		casting: false,
		session: null,
		request: null,
		updateInterval: null,

		monitorInterval: null,

		savedPlayer: null, // save player before cast
		savedVolume: 0,	// save player volume before cast
		savedPosition: 0,  // save video position before cast

		startCastTitle: gM( 'mwe-chromecast-startcast' ),
		stopCastTitle: gM( 'mwe-chromecast-stopcast' ),

		receiverName: '',

		setup: function( embedPlayer ) {
			var _this = this;
			this.addBindings();
			window['__onGCastApiAvailable'] = function(loaded, errorInfo) {
				if (loaded) {
					_this.initializeCastApi();
				} else {
					_this.log(errorInfo);
				}
			};
		},

		addBindings: function() {
			var _this = this;
			this.bind('chromecastPlay', function(){_this.playMedia();});
			this.bind('chromecastPause', function(){_this.pauseMedia();});
			this.bind('chromecastSwitchMedia', function(e, url, mime){_this.loadMedia(url, mime);});
			this.bind('chromecastGetCurrentTime', function(){_this.getCurrentTime();});
			this.bind('chromecastSetVolume', function(e, percent){_this.setVolume(e,percent);});
			this.bind('chromecastSeek', function(e, percent){_this.seekMedia(percent);});
			this.bind('stopCasting', function(){_this.toggleCast();});

			$( this.embedPlayer).bind('chromecastDeviceConnected', function(){
				_this.getComponent().css("color","#35BCDA");
				$(_this.embedPlayer).html(_this.getPlayingScreen());
				$(".chromecastThumb").load(function(){
					setTimeout(function(){
						_this.setPlayingScreen();
					},0);
				});
			});
			$( this.embedPlayer).bind('chromecastDeviceDisConnected', function(){
				_this.getComponent().css("color","white");
				_this.embedPlayer.disablePlayer();
				_this.embedPlayer.updatePlaybackInterface()
			});

			$( this.embedPlayer).bind('chromecastShowConnectingMsg', function(){
				_this.showConnectingMessage();
			});
		},

		getComponent: function() {
			var _this = this;
			if( !this.$el ) {
				this.$el = $( '<button/>' )
					.attr( 'title', this.startCastTitle )
					.addClass( "btn icon-chromecast" + this.getCssClass() )
					.click( function() {
						if( _this.embedPlayer.selectedPlayer && _this.embedPlayer.selectedPlayer.library != "NativeComponent" ) {
								_this.toggleCast();
						} else {
							// 'NativeComponent' event for showing chromecast device list on mobile native apps
							$( _this.embedPlayer ).trigger( 'showChromecastDeviceList' );

							var chromeCastSource = _this.getChromecastSource();
							// set source using a timeout to avoid setting auto source by Akamai Analytics
							setTimeout(function() {
								_this.embedPlayer.mediaElement.setSource(chromeCastSource);
							},300);
						}
					});
			}
			return this.$el;
		},

		showConnectingMessage: function(){
			this.embedPlayer.showErrorMsg(
				{'title':'Chromecast Player',
					'message': gM('mwe-chromecast-connecting'),
					'props':{
						'customAlertContainerCssClass': 'connectingMsg',
						'customAlertTitleCssClass': 'hidden',
						'textColor': '#ffffff'
					}
				}
			);
		},

		toggleCast : function(){
			if (this.isDisabled){
				return false;
			}
			var _this = this;
			if (!this.casting){
				// launch app
				this.showConnectingMessage();
				this.embedPlayer.disablePlayControls(["chromecast"]);
				chrome.cast.requestSession(
					function(e){
						_this.onRequestSessionSuccess(e);
					}, 
					function(error){
						_this.onLaunchError(error);
					}
				);
			}else{
				// stop casting
				this.stopMedia();
				this.stopApp();
			}
		},

		onRequestSessionSuccess: function(e) {
			this.embedPlayer.layoutBuilder.closeAlert();
			this.log( "Session success: " + e.sessionId);
			this.session = e;
			this.getComponent().css("color","#35BCDA");
			this.updateTooltip(this.stopCastTitle);
			this.casting = true;
			this.loadMedia();
		},

		onLaunchError: function(error) {
			this.embedPlayer.layoutBuilder.closeAlert();
			this.embedPlayer.enablePlayControls();
			this.log("launch error: "+error.code);
		},

		initializeCastApi: function() {
			var _this = this;
			var sessionRequest = new chrome.cast.SessionRequest(this.getConfig("applicationID")); // 'Castv2Player'
			var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
				function(event){
					_this.sessionListener(event);
				}, 
				function(event){
					_this.receiverListener(event);
				}
			);
			chrome.cast.initialize(apiConfig, 
				function(){
					_this.onInitSuccess();
				}, 
				function(){
					_this.onError();
				}
			);
		},

		sessionListener: function( e ) {
			this.log("New session ID: ' + e.sessionId);");
			this.session = e;
			if (this.session.media.length !== 0) {
				this.log('Found ' + this.session.media.length + ' existing media sessions.');
{'fraction_non_alphanumeric': 0.15338593600971126, 'fraction_numerical': 0.0036417237492413074, 'mean_word_length': 3.700081499592502, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2128378', 'n_tokens_mistral': 4629, 'n_tokens_neox': 3635, 'n_words': 647}