UNPKG

npaw-plugin-adapters

Version:
347 lines (320 loc) 10.5 kB
import GenericAdsAdapter from "./ads/GenericAdsAdapter"; export default class ReactNativeVideoAdapter { adsAdapters = { genericAdsAdapter: GenericAdsAdapter } getVersion() { return '7.0.3-react-native-video-jsclass'; } getPlayhead() { const adsAdapter = this.getVideo().getAdsAdapter(); return adsAdapter && adsAdapter.isShowingAds() ? this.lastPlayhead : this.playhead; } getIsLive() { return typeof this.duration !== 'undefined' ? !this.duration : undefined; } getDuration() { return this.duration; } getResource() { return this.resource; } getRendition() { if (this.width && this.height) { return this.getNpawUtils().buildRenditionString(this.width, this.height); } return; } getPlayerName() { return 'React Native Video'; } setPlayerVersion(version) { if (typeof version === 'string' && version) { this.playerVersion = version; } } getPlayerVersion() { return this.playerVersion; } setPlatform(platform) { this.reactPlatform = platform; } registerListeners() { if (!this.getVideo().getAdsAdapter()) { this.plugin.setAdsAdapter(this.getAdapterClass('genericAdsAdapter'), this.getVideo().getVideoKey()); } } unregisterListeners() { if (this.monitor) this.monitor.stop(); } onLoad(e) { const size = e.naturalSize; if (size) { this.width = Math.round(size.width); this.height = Math.round(size.height); } this.duration = e.duration; this.fireStart({}, 'onLoad'); if (!this.flags.isJoined) { this.onBuffering = false; this.fireJoin({}, 'onLoadStart'); if (this.reactPlatform && this.reactPlatform === 'ios' && this.monitor) { this.monitor.skipNextTick(); } } if (this.pausePlayhead && Math.abs(this.getPlayhead() - this.pausePlayhead) > 0.5) { this.fireSeekEnd({}, 'onLoadStart'); } else { if (this.pausePlayhead) { this.chronos.pause.pauseTime = undefined; } } this.pausePlayhead = undefined; this.fireResume({}, 'onLoadStart'); } onLoadStart(e) { if (e.src) { this.resource = e.src.uri; } this.fireStart({}, 'onLoadStart'); } onPlaybackRateChange(e) { this.playrate = e.playbackRate; if (this.pendingPauseTimer && this.playrate) { clearTimeout(this.pendingPauseTimer); this.pendingPauseTimer = undefined; this.pendingPauseAt = undefined; this.pendingPausePlayhead = undefined; } if (this.getVideo().getAdsAdapter() && this.getVideo().getAdsAdapter().isShowingAds()) { return; } if (this.playrate && this.playhead > 0.1) { this.fireStart({}, 'onPlaybackRateChange'); if (!this.flags.isJoined) { this.onBuffering = false; this.fireJoin({}, 'onPlaybackRateChange'); if (this.reactPlatform && this.reactPlatform === 'ios' && this.monitor) { this.monitor.skipNextTick(); } } if (this.pausePlayhead && Math.abs(this.getPlayhead() - this.pausePlayhead) > 0.5 && !this.flags.isPaused) { this.fireSeekEnd({}, 'onPlaybackRateChange'); this.pausePlayhead = undefined; } else if (this.pausePlayhead && Math.abs(this.getPlayhead() - this.pausePlayhead) < 0.5 && !this.onBuffering) { this.cancelSeek(); this.pausePlayhead = undefined; } this.fireResume({}, 'onPlaybackRateChange'); } else if (this.playhead > 0.1) { if (this.reactPlatform && this.reactPlatform === 'android') { if (this.onBuffering) { return; } if (this.pendingPauseTimer) { return; } this.pendingPauseAt = Date.now(); this.pendingPausePlayhead = this.getPlayhead(); this.pendingPauseTimer = setTimeout(() => { this.pendingPauseTimer = undefined; const shouldPause = !this.onBuffering && !this.playrate && !this.flags.isPaused && this.pendingPauseAt; if (!shouldPause) { this.pendingPauseAt = undefined; this.pendingPausePlayhead = undefined; return; } if (typeof this.pendingPausePlayhead === 'number') { this.pausePlayhead = this.pendingPausePlayhead; } else { this.pausePlayhead = this.getPlayhead(); } this.fireSeekBegin({}, true, 'onPlaybackRateChange'); this.firePause({}, 'onPlaybackRateChange'); this.chronos.pause.startTime = this.pendingPauseAt || new Date().getTime(); this.pendingPauseAt = undefined; this.pendingPausePlayhead = undefined; }, 450); return; } if (this.reactPlatform && this.reactPlatform === 'ios') { this.lastPlayhead = this.playhead; } this.firePause({}, 'onPlaybackRateChange'); } } onProgress(e) { const adsAdapter = this.getVideo().getAdsAdapter(); if (adsAdapter && adsAdapter.isShowingAds() && this.reactPlatform && this.reactPlatform === 'android' && !this.lastPlayhead) { this.lastPlayhead = this.playhead; } else if (adsAdapter && !adsAdapter.isShowingAds() && !this.flags.isPaused) { this.lastPlayhead = undefined; } if (adsAdapter && !adsAdapter.isShowingAds() && !this.duration) { this.duration = e.seekableDuration; } this.playhead = e.currentTime; if (e.playableDuration - e.currentTime > 0.5) { this.fireStart({}, 'onProgress'); } if (this.playhead > 0.5 && !this.flags.isJoined) { this.onBuffering = false; this.fireJoin({}, 'onProgress'); if (this.reactPlatform && this.reactPlatform === 'ios' && this.monitor) { this.monitor.skipNextTick(); } } if (this.pausePlayhead && !this.flags.isPaused && this.lastSeekTrigger !== 'uiSlider') { if (this.onBuffering) { this.fireBufferBegin({}, true, 'onProgress'); this.fireBufferEnd({}, 'onProgress'); this.onBuffering = false; } else { this.chronos.pause.pauseTime = undefined; } this.pausePlayhead = undefined; } if (this.playrate) { this.onBuffering = false; } } markSeekDetected(seekTime, triggeredEvent) { const now = Date.now(); if (this.lastSeekTimestamp && now - this.lastSeekTimestamp < 1200) { if (this.lastSeekTrigger && this.lastSeekTrigger !== triggeredEvent) { return; } } this.lastSeekTimestamp = now; this.lastSeekTrigger = triggeredEvent; if (this.flags.isSeeking || this.pendingSeek) { return; } if (typeof seekTime === 'number') { this.pausePlayhead = seekTime; } else if (typeof this.getPlayhead() !== 'undefined') { this.pausePlayhead = this.getPlayhead(); } this.pendingSeek = true; this.suppressBufferUntil = Date.now() + 1500; this.fireSeekBegin({}, false, triggeredEvent); if (this.monitor) this.monitor.skipNextTick(); } onSeekStart(e) { const seekTime = e && typeof e.seekTime === 'number' ? e.seekTime : undefined; this.uiSeekInProgress = true; this.useUiSeek = true; this.uiSeekPendingEnd = true; if (typeof seekTime === 'number') { this.pausePlayhead = seekTime; } if (!this.flags.isSeeking) { this.fireSeekBegin({}, false, 'uiSlider'); } } onSeekEnd(e) { // Seek end is handled by native onSeek. } onSeek(e) { const seekTime = e && typeof e.seekTime === 'number' ? e.seekTime : undefined; if (this.uiSeekPendingEnd || this.uiSeekInProgress) { this.uiSeekInProgress = false; this.uiSeekPendingEnd = false; if (typeof seekTime === 'number') { this.pausePlayhead = seekTime; } if (!this.flags.isSeeking) { this.fireSeekBegin({}, false, 'onSeek'); } this.fireSeekEnd({}, 'onSeek'); this.pendingSeek = false; this.pausePlayhead = undefined; return; } if (!this.flags.isSeeking) { this.fireSeekBegin({}, false, 'onSeek'); } this.fireSeekEnd({}, 'onSeek'); this.pendingSeek = false; this.pausePlayhead = undefined; } onPlaybackStateChanged(e) { if (this.useUiSeek) { return; } if (!e) { return; } if (e.isSeeking) { this.markSeekDetected(undefined, 'onPlaybackStateChanged'); return; } if (this.pendingSeek) { this.fireSeekEnd({}, 'onPlaybackStateChanged'); this.pendingSeek = false; this.pausePlayhead = undefined; } } onBuffer(e) { if (!e || typeof e.isBuffering === 'undefined') { return; } if (this.suppressBufferUntil && Date.now() < this.suppressBufferUntil) { return; } if (typeof this.getPlayhead() === 'undefined') { return; } const isSeeking = this.flags.isSeeking || this.pendingSeek; if (e.isBuffering) { if (this.flags.isPaused || isSeeking) { return; } this.lastBufferStartTs = Date.now(); if (this.pendingPauseTimer) { clearTimeout(this.pendingPauseTimer); this.pendingPauseTimer = undefined; this.pendingPauseAt = undefined; this.pendingPausePlayhead = undefined; } this.onBuffering = true; this.fireBufferBegin({}, false, 'onBuffer'); } else { this.lastBufferEndTs = Date.now(); this.onBuffering = false; this.fireBufferEnd({}, 'onBuffer'); if (this.pendingSeek && !this.flags.isPaused) { this.fireSeekEnd({}, 'onBuffer'); this.pendingSeek = false; this.pausePlayhead = undefined; } } } onError(e) { let errorCode; let errorException; if(this.reactPlatform && this.reactPlatform === 'android') { errorCode = e.error.errorCode; errorException = e.error.errorException; } else if (this.reactPlatform && this.reactPlatform === 'ios') { errorCode = e.error.code; errorException = e.error.localizedDescription; } this.fireError(errorCode, errorException); } onEnd(e) { if (this.reactPlatform && this.reactPlatform === 'ios' && this.getVideo().getAdsAdapter()) { this.stopTimer = setTimeout(() => { this.fireStop(); }, this.getVideo().options['ad.postRollTimeout'] || 1000); return; } this.fireStop(); } onAdEvent(e) { if (this.getVideo() && this.getVideo().getAdsAdapter()) { this.getVideo().getAdsAdapter().onAdEvent(e); } } }