UNPKG

@convivainc/conviva-js-html5

Version:
3 lines (2 loc) 13.6 kB
/*! (C) 2024 Conviva, Inc. All rights reserved. Confidential and proprietary. */ !function(t,i){"function"==typeof define&&define.amd?define(i):("object"==typeof exports||"object"==typeof module&&module.exports)&&(module.exports=i()),void 0!==t&&(void 0===t.Conviva?void 0===t.ConvivaModule&&(t.ConvivaModuleLoading||(t.ConvivaModuleLoading=!0,t.ConvivaModule=i(),delete t.ConvivaModuleLoading)):void 0===t.Conviva.ProxyMonitor&&(t.ConvivaModuleLoading||(i=i(),t.ConvivaModuleLoading=!0,t.Conviva.ProxyMonitor=i.ProxyMonitor,t.Conviva.Impl.Html5PlayerInterface=i.Impl.Html5PlayerInterface,t.Conviva.Impl.Html5Proxy=i.Impl.Html5Proxy,delete t.ConvivaModuleLoading)))}(this,function(){var o={};return function(){"use strict";void 0!==o&&(o.Impl=o.Impl||{},o.Impl.Html5PlayerInterface=function(t,i,n){var s=this;s.t=[],s.i=0,s.n=0,s.e=!1,s.o=n,this.a=new Conviva.Impl.Html5Timer,this.r=n.buildLogger(),this.r.setModuleName("Html5PlayerInterface"),this.s=-1,this.u=-1,this.v=function(t,i,n){void 0===n&&(n=s.h),s.c.push([t,i,n]),window.addEventListener?n.addEventListener(t,i,!1):n.attachEvent("on"+t,i)},this.f=function(t,i,n){void 0===n&&(n=s.h),window.removeEventListener?n.removeEventListener(t,i,!1):n.detachEvent("on"+t,i)},this.d=function(){s.v("ended",function(){s.l("ended")}),s.v("pause",function(){s.l("pause")}),s.v("playing",function(){0===s.h.currentTime?s.e=!0:0<s.h.currentTime&&(s.e=!1)}),s.v("waiting",function(){s.l("waiting")}),s.v("timeupdate",function(){s.e&&(s.i++,s.C.getPlayerState()===Conviva.PlayerStateManager.PlayerState.PLAYING||s.h.seeking||s.l("playing"))}),s.v("error",function(){var t;s.h.error&&(t=s.h.error.code,s.N(t))}),s.v("loadedmetadata",s.p),s.v("seeking",function(){s.isSeekStarted||(s.isSeekStarted=!0,s.C.setPlayerSeekStart(-1)),s.e&&s.C.getPlayerState()!==Conviva.PlayerStateManager.PlayerState.BUFFERING&&(s.g("Adjusting Conviva player state to: BUFFERING"),s.l("waiting"))}),s.v("seeked",function(){s.isSeekStarted=!1,s.C.setPlayerSeekEnd()}),s.R()},this.getPHT=function(){return s.h?1e3*s.h.currentTime:Conviva.PlayerStateManager.DEFAULT_PHT},this.getBufferLength=function(){if(!s.h)return Conviva.PlayerStateManager.DEFAULT_BUFFER_LENGTH;var t=s.h.buffered;if(void 0!==t){for(var i=0,n=0;n<t.length;n++){var e=t.start(n),o=t.end(n);e<=s.h.currentTime&&s.h.currentTime<o&&(i+=o-s.h.currentTime)}return s.I=i,1e3*s.I}},this.getSignalStrength=function(){return Conviva.PlayerStateManager.DEFAULT_SIGNAL_STRENGTH},this.getRenderedFrameRate=function(){return Conviva.PlayerStateManager.DEFAULT_RENDERED_FRAME_RATE},this.R=function(){if(void 0!==s.h.children){var t=function(){s.g("Caught non-specific error from <source> element, reporting as ERR_UNKNOWN"),s.N(0)};s.h.A=s.h.children;for(var i=0;i<s.h.A.length;i++){var n=s.h.A[i];"SOURCE"===n.tagName&&s.v("error",t,n)}}},this.m=function(){for(var t=0;t<s.c.length;t++){var i=s.c[t];s.f(i[0],i[1],i[2])}s.c=[]},this.y=function(){s.w=s.h.readyState,0===s.h.readyState||s.h.ended?s._(Conviva.PlayerStateManager.PlayerState.STOPPED):(s.h.paused||s.h.seeking)&&s._(Conviva.PlayerStateManager.PlayerState.PAUSED),s.h.readyState>=s.h.HAVE_METADATA&&s.p()},this.l=function(t){var i=s.M(t);s.g("Received HTML5 event: "+t+". Mapped to Conviva player state: "+i),s._(i)},this._=function(t){s.C.setPlayerState(t),s.O(),s.D=!0},this.M=function(t){switch(t){case"playing":return Conviva.PlayerStateManager.PlayerState.PLAYING;case"waiting":return Conviva.PlayerStateManager.PlayerState.BUFFERING;case"ended":case"stopped":return Conviva.PlayerStateManager.PlayerState.STOPPED;case"pause":return Conviva.PlayerStateManager.PlayerState.PAUSED;default:return Conviva.PlayerStateManager.PlayerState.UNKNOWN}},this.N=function(t){var i;switch(t){case 1:i="MEDIA_ERR_ABORTED";break;case 2:i="MEDIA_ERR_NETWORK";break;case 3:i="MEDIA_ERR_DECODE";break;case 4:i="MEDIA_ERR_SRC_NOT_SUPPORTED";break;default:i="MEDIA_ERR_UNKNOWN"}s.g("Reporting error: code="+t+" message="+i);t=Conviva.Client.ErrorSeverity.FATAL;s.C.sendError(i,t)},this.p=function(){var t=s.h.duration,i=s.h.videoWidth,n=s.h.videoHeight;isNaN(t)||t===1/0||s.C.setDuration(t),!isNaN(i)&&0<=i&&s.C.setVideoResolutionWidth(i),!isNaN(n)&&0<=n&&s.C.setVideoResolutionHeight(n)},this.P=function(){this.U=0,this.H=0,this.I=0,this.T=this.a.createTimer(this.V,500,"Html5PlayerInterface._poll()")},this.V=function(){s.h&&(s.b(),s.j(),s.k())},this.b=function(){var t=s.h.videoWidth;!isNaN(t)&&0<=t&&t!==s.s&&(s.C.setVideoResolutionWidth(t),s.s=t);t=s.h.videoHeight;!isNaN(t)&&0<=t&&t!==s.u&&(s.C.setVideoResolutionHeight(t),s.u=t)},this.j=function(){s.U=s.H,s.H=s.h.currentTime;var t,i=Date.now();0<s.x&&i>s.x&&((t=s.H-s.U)<0&&(t=0),t=t/(i-s.x)*1e3,s.t.push(t)),s.x=i,s.t.length>Math.max(8,4)&&s.t.shift()},this.k=function(){var t=s.t.length;if(t>=Math.min(4,8)){for(var i=0,n=s.t.slice(),e=0;e<n.length;e++)i+=n[e];i/=t;var o=1,a=.25,r=s.h.playbackRate;!isNaN(r)&&r!==1/0&&0<r&&(0<Object.prototype.toString.call(window.HTMLElement).indexOf("Constructor")&&r<.5&&(r=.5),o*=r,a*=r);r=s.C.getPlayerState();if(r!==Conviva.PlayerStateManager.PlayerState.PLAYING&&4<=t&&Math.abs(i-o)<a)return s.g("Adjusting Conviva player state to: PLAYING"),void s._(Conviva.PlayerStateManager.PlayerState.PLAYING);r===Conviva.PlayerStateManager.PlayerState.PLAYING&&8<=t&&0===i?s.h.paused?r!==Conviva.PlayerStateManager.PlayerState.PAUSED&&(s.g("Adjusting Conviva player state to: PAUSED"),s._(Conviva.PlayerStateManager.PlayerState.PAUSED)):s.h.seeking||(s.g("Adjusting Conviva player state to: BUFFERING"),s._(Conviva.PlayerStateManager.PlayerState.BUFFERING)):s.e&&(s.h.paused?(r!==Conviva.PlayerStateManager.PlayerState.PAUSED&&(s.g("Adjusting Conviva player state to: PAUSED"),s._(Conviva.PlayerStateManager.PlayerState.PAUSED)),s.i=s.n):s.h.seeking||(1<s.i&&s.i===s.n&&(s.g("Adjusting Conviva player state to: BUFFERING"),s._(Conviva.PlayerStateManager.PlayerState.BUFFERING)),s.n=s.i))}},this.F=function(){this.T()},this.O=function(){s.t=[],s.U=-1,s.x=0},this.S=function(){s.n=0,s.i=0},this.g=function(t){this.r.log(t,Conviva.SystemSettings.LogLevel.DEBUG)},function(t,i){if(this.g("Html5PlayerInterface._constr()"),!t)throw new Error("Html5PlayerInterface: playerStateManager argument cannot be null.");if(!i)throw new Error("Html5PlayerInterface: videoElement argument cannot be null.");this.C=t,this.h=i,this.c=[],this.d(),this.O(),this.S(),this.P(),this.y(),this.C.setClientMeasureInterface(this),this.C.setModuleNameAndVersion("HTML5","4.0.13L"),this.o&&this.o.B&&!this.o.B.getFrameworkName()&&this.C.setPlayerType("HTML5")}.apply(this,arguments),this.cleanup=function(){this.g("Html5PlayerInterface.cleanup()"),this.F(),this.m(),this.h=null,this.C=null}}),void 0!==o&&(o.Impl=o.Impl||{},o.Impl.Html5Proxy=function(t,i,n,s){var u=this;function v(){return/MSIE|Trident|Edge\//.test(window.navigator.userAgent)}function e(){return 0<u.h.currentTime&&(u.e=!1,1)}u.t=[],u.i=0,u.n=0,u.e=!1,u.G=!1,this.a=new s.Impl.Html5Timer,this.s=-1,this.u=-1,this.L=s.Constants.PlayerState.UNKNOWN,this.K=0,this.v=function(t,i,n){void 0===n&&(n=u.h),u.c.push([t,i,n]),window.addEventListener?n.addEventListener(t,i,!1):n.attachEvent("on"+t,i)},this.f=function(t,i,n){void 0===n&&(n=u.h),window.removeEventListener?n.removeEventListener(t,i,!1):n.detachEvent("on"+t,i)},this.d=function(){u.v("ended",function(){u.g("rx event ended: "+u.h.currentTime),u._(s.Constants.PlayerState.STOPPED)}),u.v("pause",function(){u.g("rx event pause: "+u.h.currentTime),e(),u._(s.Constants.PlayerState.PAUSED)}),u.v("playing",function(){u.g("rx event playing: "+u.h.currentTime),0===u.h.currentTime?u.e=!0:e()}),u.v("waiting",function(){u.g("rx event waiting: "+u.h.currentTime),e(),u._(s.Constants.PlayerState.BUFFERING)}),u.v("timeupdate",function(){u.g("rx event timeupdate"+u.h.currentTime),e(),u.e&&(u.i++,u.L===s.Constants.PlayerState.PLAYING||u.h.seeking||u._(s.Constants.PlayerState.PLAYING))}),u.v("error",function(){var t;u.g("rx event error: "+u.h.currentTime),u.h&&u.h.error&&(t="Error Message: "+u.h.error.message+", Error Code: "+u.h.error.code,u.W.reportPlaybackError(t,s.Constants.ErrorSeverity.FATAL))}),u.v("loadedmetadata",function(){u.g("rx event loadedmetadata: "+u.h.currentTime);var t,i=u.h.duration,n=u.h.videoWidth,e=u.h.videoHeight;isNaN(i)||i===1/0||i===Number.MAX_VALUE||((t={})[s.Constants.DURATION]=i,u.W.setContentInfo(t)),(!isNaN(n)&&0<n||!isNaN(e)&&0<e)&&u.W.reportPlaybackMetric(s.Constants.Playback.RESOLUTION,n,e,"CONVIVA")}),u.v("seeking",function(){u.g("rx event seeking: "+u.h.currentTime),u.isSeekStarted||(u.isSeekStarted=!0,u.W.reportPlaybackMetric(s.Constants.Playback.SEEK_STARTED,"CONVIVA")),e(),u.e&&u.L!==s.Constants.PlayerState.BUFFERING&&(u.g("Adjusting Conviva player state to: BUFFERING"),u._(s.Constants.PlayerState.BUFFERING))}),u.v("seeked",function(){u.g("rx event seeked: "+u.h.currentTime),e(),u.isSeekStarted=!1,u.W.reportPlaybackMetric(s.Constants.Playback.SEEK_ENDED,"CONVIVA")}),u.z(),u.R()},this.getBufferLength=function(){var t=u.h.buffered;if(void 0!==t){for(var i=0,n=0;n<t.length;n++){var e=t.start(n),o=t.end(n);e<=u.h.currentTime&&u.h.currentTime<o&&(i+=o-u.h.currentTime)}return u.I=i,1e3*u.I}},this.R=function(){if(void 0!==u.h.children){var t=function(){u.g("Caught non-specific error from <source> element, reporting as ERR_UNKNOWN"),u.N(0)};u.h.A=u.h.children;for(var i=0;i<u.h.A.length;i++){var n=u.h.A[i];"SOURCE"===n.tagName&&u.v("error",t,n)}}},this.z=function(){var i=["moz","ms","o","webkit"],t=function(){for(var t=0;t<i.length;t++)if(n(i[t])in document)return i[t];return null}();function n(t){return t?t+"Hidden":"hidden"}u.v((t||"")+"visibilitychange",function(){document[n(t)]?u.G=!0:u.G=!1},window)},this.m=function(){for(var t=0;t<u.c.length;t++){var i=u.c[t];u.f(i[0],i[1],i[2])}u.c=[]},this.y=function(){u.w=u.h.readyState,0===u.h.readyState||u.h.ended?u._(s.Constants.PlayerState.STOPPED):(u.h.paused||u.h.seeking)&&u._(s.Constants.PlayerState.PAUSED)},this._=function(t){u.L=t,u.W.reportPlaybackMetric(s.Constants.Playback.PLAYER_STATE,u.L,"CONVIVA"),u.O(),u.S(),u.D=!0},this.N=function(t){var i;switch(t){case 1:i="MEDIA_ERR_ABORTED";break;case 2:i="MEDIA_ERR_NETWORK";break;case 3:i="MEDIA_ERR_DECODE";break;case 4:i="MEDIA_ERR_SRC_NOT_SUPPORTED";break;default:i="MEDIA_ERR_UNKNOWN"}u.g("Reporting error: code="+t+" message="+i);t=s.Client.ErrorSeverity.FATAL;u.W.reportPlaybackError(i,t)},this.P=function(){this.U=0,this.H=0,this.I=0,this.T=this.a.createTimer(this.V,250,"Html5Proxy._poll()")},this.V=function(){u.h?u.W.getAdType()!==s.Constants.AdType.CLIENT_SIDE?(u.W.reportPlaybackMetric(s.Constants.Playback.PLAY_HEAD_TIME,1e3*u.h.currentTime,"CONVIVA"),u.W.reportPlaybackMetric(s.Constants.Playback.BUFFER_LENGTH,u.getBufferLength(),"CONVIVA"),u.W.reportPlaybackMetric(s.Constants.Playback.RESOLUTION,u.h.videoWidth,u.h.videoHeight,"CONVIVA"),u.j(),u.k()):u.L!==s.Constants.PlayerState.UNKNOWN&&(u.L=s.Constants.PlayerState.UNKNOWN,u.O(),u.S()):(u.W.reportPlaybackMetric(s.Constants.Playback.PLAY_HEAD_TIME,s.PlayerStateManager.DEFAULT_PHT,"CONVIVA"),u.W.reportPlaybackMetric(s.Constants.Playback.BUFFER_LENGTH,s.PlayerStateManager.DEFAULT_BUFFER_LENGTH,"CONVIVA"))},this.j=function(){u.U=u.H,u.H=u.h.currentTime;var t,i=Date.now();0<u.x&&i>u.x&&(t=u.H-u.U,(u.G&&(t<0||4<Math.abs(t))||!u.G&&(t<0||1<=Math.abs(t)))&&(t=0),t=t/(i-u.x)*1e3,u.t.push(t)),u.x=i,u.t.length>Math.max(4,4)&&u.t.shift()},this.k=function(){var t=u.t.length;if(t>=Math.min(4,4)){for(var i=0,n=u.t.slice(),e=0;e<n.length;e++)i+=n[e];i/=t;var o=1,a=.25,r=u.h.playbackRate;if(!isNaN(r)&&r!==1/0&&0<r&&(/apple/i.test(navigator.vendor)&&r<.5&&(r=.5),o*=r,a*=r),u.L!==s.Constants.PlayerState.PLAYING&&4<=t&&Math.abs(i-o)<a)return u.g("Adjusting Conviva player state to: PLAYING"),void u._(s.Constants.PlayerState.PLAYING);u.L===s.Constants.PlayerState.PLAYING&&4<=t&&0===i?u.h.paused?u.L!==s.Constants.PlayerState.PAUSED&&(u.g("Adjusting Conviva player state to: PAUSED"),u._(s.Constants.PlayerState.PAUSED)):u.h.seeking&&!v()?u.g("Adjusting Conviva player state to: BUFFERING ignored due to seeking"):(u.g("Adjusting Conviva player state to: BUFFERING"),u._(s.Constants.PlayerState.BUFFERING)):u.e&&(u.K++,u.h.paused?(u.L!==s.Constants.PlayerState.PAUSED&&(u.g("Adjusting Conviva player state to: PAUSED when currentTimeIsInvalid"),u._(s.Constants.PlayerState.PAUSED)),u.i=u.n):u.h.seeking&&!v()?u.g("Adjusting Conviva player state to: BUFFERING ignored due to seeking when currentTimeIsInvalid"):(1<u.i&&u.i===u.n&&!u.K%4&&(u.g("Adjusting Conviva player state to: BUFFERING when currentTimeIsInvalid"),u._(s.Constants.PlayerState.BUFFERING)),u.n=u.i),!u.K%4&&(u.K=0))}},this.F=function(){this.T&&this.T()},this.O=function(){u.t=[],u.U=-1,u.x=0},this.S=function(){u.n=0,u.i=0,u.K=0},this.g=function(t){this.r.log(t,s.SystemSettings.LogLevel.DEBUG)},function(t,i,n){if(!t)throw new Error("Html5Proxy: videoElement argument cannot be null.");this.h=t,this.W=n,this.r=i.buildLogger(),this.r.setModuleName("Html5Proxy"),this.g("Html5Proxy._constr()"),this.c=[],this.d(),this.O(),this.S(),this.P(),this.y(),(i={})[s.Constants.MODULE_NAME]="HTML5",i[s.Constants.MODULE_VERSION]="4.0.13",this.W.setContentInfo(i),(i={})[s.Constants.FRAMEWORK_NAME]="HTML5",this.W.setPlayerInfo(i)}.apply(this,arguments),this.cleanup=function(){this.g("Html5Proxy.cleanup()"),this.F(),this.m(),this.h=null}}),o.ProxyMonitor={Y:null,release:function(){this.Y&&this.Y.cleanup()},initConvivaDropIn:function(t,i,n,e){if(t&&t instanceof HTMLMediaElement)return this.Y=new o.Impl.Html5Proxy(t,i,n,e),this.Y;throw new Error("No player proxy initialized")}}}(),o});