text: (window.webpackJsonp=window.webpackJsonp||[]).push([[2],{264:function(e,t){},265:function(e,t){},278:function(e,t){},279:function(e,t){},280:function(e,t){},283:function(e,t){},284:function(e,t){},289:function(e,t){},338:function(e,t){},340:function(e,t){},348:function(e,t){},350:function(e,t){},357:function(e,t){},360:function(e,t){},362:function(e,t){},370:function(e,t){},372:function(e,t){},377:function(e,t){},380:function(e,t){},381:function(e,t){},383:function(e,t){},384:function(e,t){},386:function(e,t){},388:function(e,t){},410:function(e,t){},415:function(e,t){},417:function(e,t){},424:function(e,t){},434:function(e,t){},435:function(e,t){},438:function(e,t){},439:function(e,t){},442:function(e,t){},445:function(e,t){},446:function(e,t){},448:function(e,t){},450:function(e,t){},459:function(e,t){},461:function(e,t){},470:function(e,t){},472:function(e,t){},486:function(e,t){},488:function(e,t){},497:function(e,t){},499:function(e,t){},505:function(e,t){},507:function(e,t,i){"use strict";(function(e,n){i.d(t,"a",(function(){return u}));var r=i(1),s=i(159),o=i(508);class a extends o.a{constructor(e){super(e),this.version(1).stores({chunks:"id"})}}class l extends o.a{constructor(){super("webtorrent-expiration"),this.version(1).stores({databases:"name,expiration"})}}class u extends s.EventEmitter{constructor(e,t){if(super(),this.pendingPut=[],this.memoryChunks={},this.databaseName="webtorrent-chunks-",t||(t={}),t.torrent&&t.torrent.infoHash?this.databaseName+=t.torrent.infoHash:this.databaseName+="-default",this.setMaxListeners(100),this.chunkLength=Number(e),!this.chunkLength)throw new Error("First argument must be a chunk length");this.length=Number(t.length)||1/0,this.length!==1/0&&(this.lastChunkLength=this.length%this.chunkLength||this.chunkLength,this.lastChunkIndex=Math.ceil(this.length/this.chunkLength)-1),this.db=new a(this.databaseName),this.expirationDB=new l,this.runCleaner()}put(e,t,i){const n=e===this.lastChunkIndex;return n&&t.length!==this.lastChunkLength?this.nextTick(i,new Error("Last chunk length must be "+this.lastChunkLength)):n||t.length===this.chunkLength?(this.memoryChunks[e]=!0,this.pendingPut.push({id:e,buf:t,cb:i}),void(this.putBulkTimeout||(this.putBulkTimeout=setTimeout((()=>Object(r.a)(this,void 0,void 0,(function*(){const e=this.pendingPut;this.pendingPut=[],this.putBulkTimeout=void 0;try{yield this.db.transaction("rw",this.db.chunks,(()=>this.db.chunks.bulkPut(e.map((e=>({id:e.id,buf:e.buf}))))))}catch(t){console.log("Cannot bulk insert chunks. Store them in memory.",{err:t}),e.forEach((e=>this.memoryChunks[e.id]=e.buf))}finally{e.forEach((e=>e.cb()))}}))),u.BUFFERING_PUT_MS)))):this.nextTick(i,new Error("Chunk length must be "+this.chunkLength))}get(t,i,s){if("function"==typeof i)return this.get(t,null,i);const o=this.memoryChunks[t];if(void 0===o){const t=new Error("Chunk not found");return t.notFound=!0,e.nextTick((()=>s(t)))}if(!0!==o)return s(null,o);this.db.transaction("r",this.db.chunks,(()=>Object(r.a)(this,void 0,void 0,(function*(){const e=yield this.db.chunks.get({id:t});if(void 0===e)return s(null,n.alloc(0));const r=e.buf;if(!i)return this.nextTick(s,null,r);const o=i.offset||0,a=i.length||r.length-o;return s(null,r.slice(o,a+o))})))).catch((e=>(console.error(e),s(e))))}close(e){return this.destroy(e)}destroy(e){return Object(r.a)(this,void 0,void 0,(function*(){try{return this.pendingPut&&(clearTimeout(this.putBulkTimeout),this.pendingPut=null),this.cleanerInterval&&(clearInterval(this.cleanerInterval),this.cleanerInterval=null),this.db&&(this.db.close(),yield this.dropDatabase(this.databaseName)),this.expirationDB&&(this.expirationDB.close(),this.expirationDB=null),e()}catch(t){return console.error("Cannot destroy peertube chunk store.",t),e(t)}}))}runCleaner(){this.checkExpiration(),this.cleanerInterval=setInterval((()=>Object(r.a)(this,void 0,void 0,(function*(){this.checkExpiration()}))),u.CLEANER_INTERVAL_MS)}checkExpiration(){return Object(r.a)(this,void 0,void 0,(function*(){let e=[];try{yield this.expirationDB.transaction("rw",this.expirationDB.databases,(()=>Object(r.a)(this,void 0,void 0,(function*(){yield this.expirationDB.databases.put({name:this.databaseName,expiration:(new Date).getTime()+u.CLEANER_EXPIRATION_MS});const t=(new Date).getTime();e=yield this.expirationDB.databases.where("expiration").below(t).toArray()}))))}catch(e){console.error("Cannot update expiration of fetch expired databases.",e)}for(const t of e)yield this.dropDatabase(t.name)}))}dropDatabase(e){return Object(r.a)(this,void 0,void 0,(function*(){const t=new a(e);console.log("Destroying IndexDB database %s.",e);try{yield t.delete(),yield this.expirationDB.transaction("rw",this.expirationDB.databases,(()=>this.expirationDB.databases.where({name:e}).delete()))}catch(t){console.error("Cannot delete %s.",e,t)}}))}nextTick(t,i,n){e.nextTick((()=>t(i,n)),void 0)}}u.BUFFERING_PUT_MS=1e3,u.CLEANER_INTERVAL_MS=6e4,u.CLEANER_EXPIRATION_MS=3e5}).call(this,i(74),i(73).Buffer)},520:function(e,t,i){"use strict";i.r(t),i.d(t,"WebTorrentPlugin",(function(){return g}));var n=i(0),r=i.n(n),s=i(333),o=i(22);const a=i(214),l=i(307),u=[".m4a",".m4v",".mp4"];function h(e,t,i,n){return function(e){if(null==e)throw new Error("file cannot be null or undefined");if("string"!=typeof e.name)throw new Error("missing or invalid file.name property");if("function"!=typeof e.createReadStream)throw new Error("missing or invalid file.createReadStream property")}(e),function(e,t,i,n){const r=Object(o.extname)(e.name).toLowerCase();let s,h,d=0;try{h=u.indexOf(r)>=0?c():p()}catch(e){return n(e)}function c(){return y(),s.addEventListener("error",(function e(t){return s.removeEventListener("error",e),n(t)})),s.addEventListener("loadedmetadata",g),new l(e,s)}function p(t=!1){const i=function(e,t=!1){const i=Object(o.extname)(e).toLowerCase();if(".mp4"===i)return'video/mp4; codecs="avc1.640029, mp4a.40.5"';if(".webm"===i)return!0===t?'video/webm; codecs="vp9, opus"':'video/webm; codecs="vp8, vorbis"';return}(e.name,t);y(),s.addEventListener("error",(function e(t){return s.removeEventListener("error",e),-1!==i.indexOf("vp8")?f(!0):n(t)})),s.addEventListener("loadedmetadata",g);const r=new a(s),l=r.createWriteStream(i);return e.createReadStream().pipe(l),d&&(s.currentTime=d),r}function f(e=!1){!0===e?console.log("Falling back to media source with VP9 enabled."):console.log("Falling back to media source.."),p(e)}function y(){void 0===s&&(s=t,s.addEventListener("progress",(function(){d=t.currentTime})))}function g(){s.removeEventListener("loadedmetadata",g),i.autoplay&&s.play(),n(null,h)}}(e,t,i,n)}var d=i(3),c=i(507),p=i(12);const f=i(509),y=r.a.getPlugin("plugin");class g extends y{constructor(e,t){super(e),this.autoplay=!1,this.startTime=0,this.CONSTANTS={INFO_SCHEDULER:1e3,AUTO_QUALITY_SCHEDULER:3e3,AUTO_QUALITY_THRESHOLD_PERCENT:30,AUTO_QUALITY_OBSERVATION_TIME:1e4,AUTO_QUALITY_HIGHER_RESOLUTION_DELAY:5e3,BANDWIDTH_AVERAGE_NUMBER_OF_VALUES:5},this.webtorrent=new s({tracker:{rtcConfig:Object(d.d)()},dht:!1}),this.destroyingFakeRenderer=!1,this.autoResolution=!0,this.autoResolutionPossible=!0,this.isAutoResolutionObservation=!1,this.playerRefusedP2P=!1,this.downloadSpeeds=[],this.startTime=Object(d.j)(t.startTime),this.autoplay=t.autoplay,this.playerRefusedP2P=!Object(p.c)(),this.videoFiles=t.videoFiles,this.videoDuration=t.videoDuration,this.savePlayerSrcFunction=this.player.src,this.playerElement=t.playerElement,this.player.ready((()=>{this.player.options_;this.player.duration(t.videoDuration),this.initializePlayer(),this.runTorrentInfoScheduler(),this.player.one("play",(()=>{this.runAutoQualitySchedulerTimer=setTimeout((()=>this.runAutoQualityScheduler()),this.CONSTANTS.AUTO_QUALITY_SCHEDULER)}))}))}dispose(){clearTimeout(this.addTorrentDelay),clearTimeout(this.qualityObservationTimer),clearTimeout(this.runAutoQualitySchedulerTimer),clearInterval(this.torrentInfoInterval),clearInterval(this.autoQualityInterval),this.flushVideoFile(this.currentVideoFile,!1),this.destroyFakeRenderer()}getCurrentResolutionId(){return this.currentVideoFile?this.currentVideoFile.resolution.id:-1}updateVideoFile(e,t={},i=(()=>{})){if(!e){const t=Object(p.a)();e=t?this.getAppropriateFile(t):this.pickAverageVideoFile()}if(!e)throw Error("Can't update video file since videoFile is undefined.");if(void 0!==this.currentVideoFile&&this.currentVideoFile.magnetUri===e.magnetUri)return;this.disableErrorDisplay(),this.player.src=()=>!0;const n=this.player.playbackRate(),r=this.currentVideoFile;if(this.currentVideoFile=e,Object(d.f)()||this.playerRefusedP2P)return this.fallbackToHttp(t,(()=>(this.player.playbackRate(n),i())));this.addTorrent(this.currentVideoFile.magnetUri,r,t,(()=>(this.player.playbackRate(n),i()))),this.changeQuality(),this.trigger("resolutionChange",{auto:this.autoResolution,resolutionId:this.currentVideoFile.resolution.id})}updateResolution(e,t=0){const i=this.player.currentTime();this.player.paused()||this.player.bigPlayButton.hide(),0===e?(this.player.addClass("vjs-playing-audio-only-content"),this.player.posterImage.show()):(this.player.removeClass("vjs-playing-audio-only-content"),this.player.posterImage.hide());const n=this.videoFiles.find((t=>t.resolution.id===e)),r={forcePlay:!1,delay:t,seek:i+t/1e3};this.updateVideoFile(n,r)}flushVideoFile(e,t=!0){void 0!==e&&this.webtorrent.get(e.magnetUri)&&(!0===t&&this.renderer&&this.renderer.destroy&&this.renderer.destroy(),this.webtorrent.remove(e.magnetUri))}enableAutoResolution(){this.autoResolution=!0,this.trigger("resolutionChange",{auto:this.autoResolution,resolutionId:this.getCurrentResolutionId()})}disableAutoResolution(e=!1){!0===e&&(this.autoResolutionPossible=!1),this.autoResolution=!1,this.trigger("autoResolutionChange",{possible:this.autoResolutionPossible}),this.trigger("resolutionChange",{auto:this.autoResolution,resolutionId:this.getCurrentResolutionId()})}isAutoResolutionPossible(){return this.autoResolutionPossible}getTorrent(){return this.torrent}getCurrentVideoFile(){return this.currentVideoFile}addTorrent(e,t,i,n){if(!e)return this.fallbackToHttp(i,n);const r=this.torrent,s={store:function(e,t){return new f(new c.a(e,t),{max:100})}};this.torrent=this.webtorrent.add(e,s,(e=>{r&&(this.stopTorrent(r),i.delay&&this.renderFileInFakeElement(e.files[0],i.delay)),this.addTorrentDelay=setTimeout((()=>{this.destroyFakeRenderer();const r=this.player.paused();this.flushVideoFile(t),i.seek&&this.player.currentTime(i.seek);h(e.files[0],this.playerElement,{autoplay:!1,controls:!0},((e,t)=>{if(this.renderer=t,e)return this.fallbackToHttp(i,n);setTimeout((()=>this.tryToPlay((e=>e?n(e):(i.seek&&this.seek(i.seek),!1===i.forcePlay&&!0===r&&this.player.pause(),n())))),10)}))}),i.delay||0)})),this.torrent.on("error",(e=>console.error(e))),this.torrent.on("warning",(e=>{if(-1===e.message.indexOf("Unsupported tracker protocol"))if(-1===e.message.indexOf("Ice connection failed")){if(-1!==e.message.indexOf("incorrect info hash")){console.error("Incorrect info hash detected, falling back to torrent file.");const e={forcePlay:!0,seek:i.seek};return this.addTorrent(this.torrent.xs,t,e,n)}-1!==e.message.indexOf("from xs param")&&this.handleError(e)}else console.log(e)}))}tryToPlay(e){e||(e=function(){});const t=this.player.play();return void 0!==t?t.then((()=>e())).catch((t=>{if(-1===t.message.indexOf("The play() request"))return this.player.pause(),this.player.posterImage.show(),this.player.removeClass("vjs-has-autoplay"),this.player.removeClass("vjs-has-big-play-button-clicked"),this.player.removeClass("vjs-playing-audio-only-content"),e()})):e()}seek(e){this.player.currentTime(e),this.player.handleTechSeeked_()}getAppropriateFile(e){if(void 0===this.videoFiles)return;const t=this.videoFiles.filter((e=>0!==e.resolution.id));if(0===t.length)return;if(1===t.length)return t[0];if(this.torrent&&1===this.torrent.progress&&this.player.ended())return this.currentVideoFile;e||(e=this.getAndSaveActualDownloadSpeed());const i=this.playerElement.offsetHeight;let n=t[0].resolution.id;for(let e=t.length-1;e>=0;e--){const r=t[e].resolution.id;if(0!==r&&r>=i){n=r;break}}const r=t.filter((e=>e.resolution.id<=n)).filter((t=>{const i=t.size/this.videoDuration;let n=i;return(!this.currentVideoFile||t.resolution.id>this.currentVideoFile.resolution.id)&&(n+=i*this.CONSTANTS.AUTO_QUALITY_THRESHOLD_PERCENT/100),e>n}));return 0===r.length?Object(d.m)(t):Object(d.l)(r)}getAndSaveActualDownloadSpeed(){const e=Math.max(this.downloadSpeeds.length-this.CONSTANTS.BANDWIDTH_AVERAGE_NUMBER_OF_VALUES,0),t=this.downloadSpeeds.slice(e,this.downloadSpeeds.length);if(0===t.length)return-1;const i=t.reduce(((e,t)=>e+t)),n=Math.round(i/t.length);return Object(p.e)(n),n}initializePlayer(){if(this.buildQualities(),this.autoplay)return this.player.posterImage.hide(),this.updateVideoFile(void 0,{forcePlay:!0,seek:this.startTime});const e=this.player.play.bind(this.player);this.player.play=()=>{this.player.addClass("vjs-has-big-play-button-clicked"),this.player.play=e,this.updateVideoFile(void 0,{forcePlay:!0,seek:this.startTime})}}runAutoQualityScheduler(){this.autoQualityInterval=setInterval((()=>{if(void 0===this.torrent||null===this.torrent)return;if(!1===this.autoResolution)return;if(!0===this.isAutoResolutionObservation)return;const e=this.getAppropriateFile();let t=!1,i=0;this.isPlayerWaiting()&&e.resolution.id<this.currentVideoFile.resolution.id?t=!0:e.resolution.id>this.currentVideoFile.resolution.id&&(t=!0,i=this.CONSTANTS.AUTO_QUALITY_HIGHER_RESOLUTION_DELAY),!0===t&&(this.updateResolution(e.resolution.id,i),this.isAutoResolutionObservation=!0,this.qualityObservationTimer=setTimeout((()=>{this.isAutoResolutionObservation=!1}),this.CONSTANTS.AUTO_QUALITY_OBSERVATION_TIME))}),this.CONSTANTS.AUTO_QUALITY_SCHEDULER)}isPlayerWaiting(){return this.player&&this.player.hasClass("vjs-waiting")}runTorrentInfoScheduler(){this.torrentInfoInterval=setInterval((()=>{if(void 0!==this.torrent)return null===this.torrent?this.player.trigger("p2pInfo",!1):(0!==this.webtorrent.downloadSpeed&&this.downloadSpeeds.push(this.webtorrent.downloadSpeed),this.player.trigger("p2pInfo",{source:"webtorrent",http:{downloadSpeed:0,uploadSpeed:0,downloaded:0,uploaded:0},p2p:{downloadSpeed:this.torrent.downloadSpeed,numPeers:this.torrent.numPeers,uploadSpeed:this.torrent.uploadSpeed,downloaded:this.torrent.downloaded,uploaded:this.torrent.uploaded}}))}),this.CONSTANTS.INFO_SCHEDULER)}fallbackToHttp(e,t){const i=this.player.paused();this.disableAutoResolution(!0),this.flushVideoFile(this.currentVideoFile,!0),this.torrent=null,this.player.one("error",(()=>this.enableErrorDisplay()));const n=this.currentVideoFile.fileUrl;return this.player.src=this.savePlayerSrcFunction,this.player.src(n),this.changeQuality(),this.player.trigger("sourcechange"),this.tryToPlay((n=>n&&t?t(n):(e.seek&&this.seek(e.seek),!1===e.forcePlay&&!0===i&&this.player.pause(),t?t():void 0)))}handleError(e){return this.player.trigger("customError",{err:e})}enableErrorDisplay(){this.player.addClass("vjs-error-display-enabled")}disableErrorDisplay(){this.player.removeClass("vjs-error-display-enabled")}pickAverageVideoFile(){return 1===this.videoFiles.length?this.videoFiles[0]:this.videoFiles[Math.floor(this.videoFiles.length/2)]}stopTorrent(e){e.pause(),e.removePeer(e.ws)}renderFileInFakeElement(e,t){this.destroyingFakeRenderer=!1;const i=document.createElement("video");h(e,i,{autoplay:!1,controls:!1},((e,n)=>{this.fakeRenderer=n,!1===this.destroyingFakeRenderer&&e&&console.error("Cannot render new torrent in fake video element.",e),i.currentTime=this.player.currentTime()+(t-2e3)}))}destroyFakeRenderer(){if(this.fakeRenderer){if(this.destroyingFakeRenderer=!0,this.fakeRenderer.destroy)try{this.fakeRenderer.destroy()}catch(e){console.log("Cannot destroy correctly fake renderer.",e)}this.fakeRenderer=void 0}}buildQualities(){const e=[];for(const t of this.videoFiles){const i={id:t.resolution.id,label:this.buildQualityLabel(t),height:t.resolution.id,_enabled:!0};this.player.qualityLevels().addQualityLevel(i),e.push({id:i.id,label:i.label,selected:!1})}const t={qualitySwitchCallback:e=>this.qualitySwitchCallback(e),qualityData:{video:e}};this.player.tech(!0).trigger("loadedqualitydata",t)}buildQualityLabel(e){let t=e.resolution.label;return e.fps&&e.fps>=50&&(t+=e.fps),t}qualitySwitchCallback(e){-1!==e?(this.disableAutoResolution(),this.updateResolution(e)):!0===this.autoResolutionPossible&&this.enableAutoResolution()}changeQuality(){const e=this.currentVideoFile.resolution.id,t=this.player.qualityLevels();if(-1!==e)for(let i=0;i<t.length;i++){t[i].height===e&&(t.selectedIndex_=i)}else t.selectedIndex=-1}}r.a.registerPlugin("webtorrent",g)}}]);
  //# sourceMappingURL=2.chunk.js.map