1 |
- import md5 from"../../common/utils/md5";import{Base64}from"../../common/utils/js-base64";import globalField from"../utils/globalField";const getChannelJson=Symbol("getChannelJson"),descryptChannelJson=Symbol("descryptChannelJson"),handleJson=Symbol("handleJson"),promiseRequest=Symbol("promiseRequest"),app=getApp(),stopImage="https://player.polyv.net/live_player/image/streamPause.jpg";export default class PolyLive{constructor(){this.version="v2.2.2",this.buildMetaData="20210917",this.channel={},this.ProxyFieldArr=[],this.sendRtasTime=10,this.apiState="",this.apiTime=10,this.apiCheckTs=0,this.netWorkTime=6e3,this.netWorkTs=0,this.rtasTS=0,this.renderInterval=null,this.playTime=0,this.playState="",this.stateType={playing:"playing",pause:"pause",end:"end"},this.changingType="live",this.vodCurrentTime=0,this.vodRecordTime=0,this.uid="",this.cid="",this.isAutoChange=!1,this.vodsrc="",this.success=()=>{},this.onChangeType=()=>{},this.onApiStatus=()=>{},this.onNetWorkChange=()=>{},this.onUpdateChannelJson=()=>{},this.statistics={},this.params={},this.allowSendStats=!0,this.forceSessionId=!1,globalField.playerLiveCtrl&&globalField.playerLiveCtrl instanceof PolyLive&&globalField.playerLiveCtrl.destroy(),globalField.playerLiveCtrl=this}resetConfig(t){return!(!t.uid||!t.vid)&&(Object.assign(this,t),this.uid=t.uid,this.cid=t.vid,this.isSkinWhite=t.isSkinWhite,this.config=new Map([["ROUTER_HOST","https://router.polyv.net/proxy/"],["ASSETS_HOST","livestatic.videocc.net/assets/wimages/"],["JSON_URL",`https://router.polyv.net/proxy/player.polyv.net/service/v1/channel_${this.uid}_${this.cid}.json?ran=${Math.floor(9999999*Math.random())}`],["API_HOST","https://api.polyv.net/live/v3/live-status-sessionid/query"],["RTAS_URL","https://rtas.videocc.net/v2/view"],["GET_RESTRICT","https://router.polyv.net/proxy/livejson.polyv.net/service/v3/restrict.json"],["GET_BACK_RESTRICT","https://router.polyv.net/proxy/live.polyv.net/service/v3/restrict.json"]]),this.ProxyFieldArr=["coverImage","waitImage","logoImage","logoHref"],this.timeStamp=this.getDate(),t.vodsrc||(this.vodsrc=""),this.handleRtas(),!0)}getRestrict(){const t={uid:this.uid,cid:this.vid,pageDomain:"https://live.polyv.cn"};return new Promise(((e,s)=>{this[promiseRequest](this.config.get("GET_RESTRICT"),t).then((t=>{e(t)})).catch((t=>{this.getBackRestrict().then((t=>{e(t)})).catch((t=>{s(t)}))}))}))}getBackRestrict(){const t={uid:this.uid,vid:this.vid,pageDomain:"https://live.polyv.cn"};return new Promise(((e,s)=>{this[promiseRequest](this.config.get("GET_BACK_RESTRICT"),t).then((t=>{e(t)})).catch((()=>{s({msg:"fail"})}))}))}getVideo(t){this.resetConfig(t)&&this.getRestrict().then((t=>{t.canWatch?this.normalPlay():this.success(t)})).catch((t=>{this.normalPlay()}))}normalPlay(){this[getChannelJson]().then((()=>{const t={flvSrc:this.channel.flvSrc,src:this.channel.src,poster:this.channel.coverImage};"function"==typeof this.success&&this.success(t)})),this.setPlayId(),this.startRender()}getReconnectSrc(t){this[getChannelJson]().then((()=>{"function"==typeof t&&t(this.channel.flvSrc)}))}changeSessionId(t){this.statistics.session_id=t,this.handleRtas()}changeLiveMode(){}changeVodMode(){}updateTime(t){"vod"===!this.changingType&&(this.changingType="vod"),this.vodCurrentTime=t}destroy(){this.renderInterval&&(clearInterval(this.renderInterval),this.renderInterval=null),app.globalData&&app.globalData.interval&&(clearInterval(app.globalData.interval),app.globalData.interval=null)}startRender(){this.destroy();const t=()=>{this.startCheckApi(),this.sendRtas(),this.startCheckNetWork()};if(this.globalInterval)return app.globalData||(app.globalData={}),void(app.globalData.interval=setInterval((()=>{t()}),1e3));this.renderInterval=setInterval((()=>{t()}),1e3)}async[getChannelJson](){const t=this.config.get("JSON_URL"),e=await this[promiseRequest](t);await this[handleJson](e)}[handleJson](t){Object.keys(t).forEach(((e,s,i)=>{this.ProxyFieldArr.includes(e)&&(t[e]=this.proxyUrl(t[e]))})),t.src=t.lines[0].m3u8,"Y"===t.isOnlyAudio&&(t.src=this.concatSrc(t.src,"only-audio=1")),t.flvSrc=t.lines[0].flv,this.sendRtasTime=t.reportFreq||this.sendRtasTime,this.apiTime=t.channelLiveStatusQueryFrequency||this.apiTime,this.channel={},Object.assign(this.channel,t),this.onUpdateChannelJson(this.channel)}[descryptChannelJson](){}[promiseRequest](t,e={}){return new Promise(((s,i)=>{wx.request({url:t,method:"GET",data:e,success:t=>{s(t.data)}})}))}concatSrc(t,e){return t.includes("?")?`${t}&${e}`:`${t}?${e}`}handleRtas(){for(const t in this.statistics)this.params[t]=Base64.encodeURI(this.statistics[t]);/vod|live/i.test(this.changingType)&&(this.params.param3=Base64.encodeURI(this.changingType))}startCheckNetWork(){const t=Date.now();t-this.netWorkTs>this.netWorkTime&&(this.netWorkTs=t,wx.getNetworkType({success:t=>{const e=t.networkType;this.onNetWorkChange&&this.onNetWorkChange("none"===e)}}))}startCheckApi(){const t=Date.now();t-this.apiCheckTs>1e3*this.apiTime&&(this.apiCheckTs=t,(async()=>{if(!this.channel.stream)return;const t=await this[promiseRequest](`${this.config.get("API_HOST")}?stream=${this.channel.stream}&channelId=${this.cid}&ran=${Date.now()}`);this.updateApiState(t.status.trim(),t.sessionId)})())}updateApiState(t,e){(async()=>{let s="",i="",a=null;const n=t;if(n!==this.apiState){this.apiState=n,"end"===n?(a=await this.getLatestVodSrc(),s=a.recordUrl,a&&a.recordFileSessionId&&this.changeSessionId(a.recordFileSessionId)):(this.playState=this.stateType.playing,a=await this.getChannel(),s=a.src,i=a.flvSrc,!this.forceSessionId&&this.statistics.session_id||this.changeSessionId(e));const t={apiState:this.apiState,coverImage:a.coverImage,waitImage:a.waitImage,liveType:a.liveType,latestSrc:s,latestFlvSrc:i};this.updateChangeType(t),this.onApiStatus&&this.onApiStatus(this.apiState,e)}})()}getChannel(){const t=this;return new Promise(((e,s)=>{wx.request({url:this.config.get("JSON_URL"),method:"GET",success(s){const i={};i.title=s.data.name,i.src=s.data.m3u8Url,i.poster=t.proxyUrl(s.data.coverImage),i.waitImage=t.proxyUrl(s.data.waitImage),i.logoImage=t.proxyUrl(s.data.logoImage),i.logoHref=t.proxyUrl(s.data.logoHref),i.logoOpacity=s.data.logoOpacity,i.logoPosition=s.data.logoPosition,i.liveType=s.data.liveType;const a=i.src;"Y"===s.data.isOnlyAudio&&(a.indexOf("?")>-1?i.src=`${i.src}&only-audio=1`:i.src=`${i.src}?only-audio=1`),i.flvSrc=s.data.lines[0].flv,e(i)}})}))}getLatestVodSrc(){const t=this;return new Promise(((e,s)=>{wx.request({url:`https://router.polyv.net/proxy/player.polyv.net/service/v1/channel_${t.uid}_${t.cid}.json?ran=${Math.floor(9999999*Math.random())}`,method:"GET",success(s){const{recordFileM3u8Url:i,recordFileUrl:a,coverImage:n,waitImage:o,liveType:r,recordFileSessionId:h}=s.data;let l=a||"";l||(l=i||""),t.recordFileTest&&(l=a||""),e({recordUrl:l,coverImage:n,waitImage:o,liveType:r,recordFileSessionId:h})}})}))}updateChangeType(t){if(t.coverImage&&(t.coverImage=t.coverImage.replace("livestatic.videocc.net","router.polyv.net/proxy/livestatic.videocc.net")),!t.coverImage){const e=`${this.config.get("ROUTER_HOST")}${this.config.get("ASSETS_HOST")}mini-images/nolive_webapp_ppt.png`,s=`${this.config.get("ROUTER_HOST")}${this.config.get("ASSETS_HOST")}player/ppt/nolive_ppt_white.png`;t.coverImage=this.isSkinWhite?s:e}let e=!1;"live"!==t.apiState?t.latestSrc.length>0&&this.isAutoChange?(this.changingType="vod",e=!0):t.waitImage.length>0&&t.waitImage.indexOf(".mp4")>0?(this.changingType="warmVideo",t.latestSrc=t.waitImage):t.coverImage.length>0&&(this.changingType="warmImage",t.latestSrc=t.coverImage):"live"===t.apiState&&t.latestSrc.length>0&&(this.changingType="live",e=!0),this.vodsrc&&(this.changingType="vod",t.latestSrc=this.vodsrc),/vod|live/i.test(this.changingType)&&(this.params.param3=Base64.encodeURI(this.changingType)),e&&!this.vodsrc&&(this.setPlayId(),this.playTime=0),this.onChangeType&&this.onChangeType(this.changingType,t.latestSrc,t.latestFlvSrc)}sendRtas(){"live"===this.changingType&&"live"===this.apiState&&this.playTime++,"vod"===this.changingType&&(this.vodRecordTime!==this.vodCurrentTime?(this.playState=this.stateType.playing,this.vodRecordTime=this.vodCurrentTime,this.playTime++):this.playState=this.stateType.pause);const t=Date.now(),e=t-this.rtasTS>1e3*this.sendRtasTime,s=this.playState===this.stateType.playing||this.playState===this.stateType.end,{param1:i,param2:a,param3:n,param4:o,param5:r,session_id:h}=this.params;if(e&&this.playTime>this.sendRtasTime&&s&&this.allowSendStats){this.rtasTS=t;const e=this.getDate(),s=`rtas.net${this.pid}${this.cid}0${this.playTime}`,l=`${md5(s)}`,c={pid:this.pid,uid:this.uid,cid:this.cid,pd:this.playTime,sd:this.playTime,sign:l,flow:0,ts:e,pn:"webapp_sdk_live",pv:`${this.version}_${this.buildMetaData}`,param1:i,param2:a,param3:n,param4:o,param5:r,session_id:h};this.rtasTS=t,this[promiseRequest](this.config.get("RTAS_URL"),c)}}resumeStats(t){this.allowSendStats=t}setPlayId(){const t=`${Date.now()}`,e=`${parseInt(1e6*Math.random()+1e6)}`;this.pid=`${t}X${e}`}proxyUrl(t){return""===(t=t.replace(/.*?:\/\//g,""))?t:`https://router.polyv.net/proxy/${t}`}getDate(){return Date.now()}}
|