12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408 |
- import UserController from './controller/user-controller.js'
- import Pusher from './model/pusher.js'
- import { EVENT, DEFAULT_COMPONENT_CONFIG } from './common/constants.js'
- import Event from './utils/event.js'
- import * as ENV from './utils/environment.js'
- import TIM from './libs/tim-wx.js'
- import MTA from './libs/mta_analysis.js'
- const TAG_NAME = 'TRTC-ROOM'
- const IM_GROUP_TYPE = TIM.TYPES.GRP_CHATROOM
- let touchX = 0
- let touchY = 0
- Component({
-
- properties: {
-
- config: {
- type: Object,
- value: {
- sdkAppID: '',
- userID: '',
- userSig: '',
- template: '',
- debugMode: false,
- enableIM: false,
- },
- observer: function(newVal, oldVal) {
- this._propertyObserver({
- 'name': 'config', newVal, oldVal,
- })
- },
- },
- },
-
- data: {
- pusher: null,
- debugPanel: true,
- debug: false,
- streamList: [],
- visibleStreamList: [],
- userList: [],
- template: '',
- cameraPosition: '',
- panelName: '',
- localVolume: 0,
- remoteVolumeList: [],
- enableIM: false,
- showIMPanel: false,
- exitIMThrottle: false,
- messageContent: '',
- messageList: [],
- maxMessageListLength: 10,
- messageListScrollTop: 0,
- appVersion: ENV.APP_VERSION,
- libVersion: ENV.LIB_VERSION,
- hasGridPageTipsShow: false,
- gridPageCount: 0,
- gridCurrentPage: 1,
- gridPlayerPerPage: 4,
- gridPagePlaceholderStreamList: [],
- isFullscreenDevice: ENV.IS_FULLSCREEN_DEVICE,
- isShowMoreMenu: false,
- MICVolume: 50,
- BGMVolume: 50,
- BGMProgress: 0,
- beautyStyle: 'smooth',
- beautyStyleArray: [
- { value: 'smooth', label: '光滑', checked: true },
- { value: 'nature', label: '自然', checked: false },
- { value: 'close', label: '关闭', checked: false },
- ],
- filterIndex: 0,
- filterArray: [
- { value: 'standard', label: '标准' },
- { value: 'pink', label: '粉嫩' },
- { value: 'nostalgia', label: '怀旧' },
- { value: 'blues', label: '蓝调' },
- { value: 'romantic', label: '浪漫' },
- { value: 'cool', label: '清凉' },
- { value: 'fresher', label: '清新' },
- { value: 'solor', label: '日系' },
- { value: 'aestheticism', label: '唯美' },
- { value: 'whitening', label: '美白' },
- { value: 'cerisered', label: '樱红' },
- ],
- audioReverbType: 0,
- audioReverbTypeArray: ['关闭', 'KTV', '小房间', '大会堂', '低沉', '洪亮', '金属声', '磁性'],
- },
-
- lifetimes: {
- created: function() {
-
- console.log(TAG_NAME, 'created', ENV)
- MTA.App.init({
- appID: '500710685',
- eventID: '500710697',
- autoReport: true,
- statParam: true,
- })
- },
- attached: function() {
-
- console.log(TAG_NAME, 'attached')
- this._init()
- MTA.Page.stat()
- },
- ready: function() {
-
- console.log(TAG_NAME, 'ready')
- },
- detached: function() {
-
- console.log(TAG_NAME, 'detached')
-
- this.exitRoom()
- },
- error: function(error) {
-
- console.log(TAG_NAME, 'error', error)
- },
- },
- pageLifetimes: {
- show: function() {
-
- console.log(TAG_NAME, 'show status:', this.status)
- if (this.status.isPending) {
-
- this.status.isPending = false
-
-
-
-
-
-
-
-
- this.enterRoom({ roomID: this.data.config.roomID }).then(()=>{
-
-
-
-
-
- })
- } else if (ENV.IS_ANDROID && this.status.pageLife === 'hide' && this.status.isOnHideAddStream && this.data.streamList.length > 0) {
-
-
-
-
-
- const streamList = this.data.streamList
- let tempStreamList = []
-
- for (let i = 0; i < streamList.length; i++) {
- if (streamList[i].isOnHideAdd && streamList[i].playerContext) {
- const stream = streamList[i]
- tempStreamList.push(stream)
- stream.playerContext = undefined
- streamList.splice(i, 1)
- }
- }
-
- this._setList({
- streamList: streamList,
- }).then(() => {
- for (let i = 0; i < tempStreamList.length; i++) {
- streamList.push(tempStreamList[i])
- }
-
-
- this._setList({
- streamList: streamList,
- }).then(() => {
- for (let i = 0; i < tempStreamList.length; i++) {
- tempStreamList[i] = wx.createLivePlayerContext(tempStreamList[i].streamID, this)
- }
- tempStreamList = []
- })
-
- })
- this.status.isOnHideAddStream = false
- }
- this.status.pageLife = 'show'
- },
- hide: function() {
-
- console.log(TAG_NAME, 'hide')
- this.status.pageLife = 'hide'
- },
- resize: function(size) {
-
- console.log(TAG_NAME, 'resize', size)
- },
- },
-
- methods: {
-
- _init() {
- console.log(TAG_NAME, '_init')
- this.userController = new UserController(this)
- this._emitter = new Event()
- this.EVENT = EVENT
- this._initStatus()
- this._bindEvent()
- this._gridBindEvent()
- this._keepScreenOn()
- console.log(TAG_NAME, '_init success component:', this)
- },
- _initStatus() {
- this.status = {
- isPush: false,
- isPending: false,
- pageLife: '',
- isOnHideAddStream: false,
- }
- this._lastTapTime = 0
- this._beforeLastTapTime = 0
- this._lastTapCoordinate = { x: 0, y: 0 },
- this._isFullscreen = false
- },
-
- _propertyObserver(data) {
- console.log(TAG_NAME, '_propertyObserver', data, this.data.config)
- if (data.name === 'config') {
- const config = Object.assign({}, DEFAULT_COMPONENT_CONFIG, data.newVal)
- console.log(TAG_NAME, '_propertyObserver config:', config)
-
- if (typeof config.debugMode === 'string') {
- config.debugMode = config.debugMode === 'true' ? true : false
- }
-
- if (config.enableIM && config.sdkAppID) {
- this._initIM(config)
- }
- if (config.sdkAppID && data.oldVal.sdkAppID !== config.sdkAppID && MTA) {
- MTA.Event.stat('sdkAppID', { 'value': config.sdkAppID })
- }
-
- this.setData({
- enableIM: config.enableIM,
- template: config.template,
- debugMode: config.debugMode || false,
- debug: config.debugMode || false,
- })
- this._setPusherConfig(config)
- }
- },
-
-
-
-
-
-
-
-
-
-
- enterRoom(params) {
- return new Promise((resolve, reject) => {
- console.log(TAG_NAME, 'enterRoom')
- console.log(TAG_NAME, 'params', params)
- console.log(TAG_NAME, 'config', this.data.config)
- console.log(TAG_NAME, 'pusher', this.data.pusher)
-
- if (params) {
- Object.assign(this.data.pusher, params)
- Object.assign(this.data.config, params)
- }
- console.log(this.data.config,'参数');
- if (!this._checkParam(this.data.config)) {
- reject(new Error('缺少必要参数'))
- return
- }
-
- this._getPushUrl(this.data.config).then((pushUrl)=> {
- this.data.pusher.url = pushUrl
- this.setData({
- pusher: this.data.pusher,
- }, () => {
-
- console.log(TAG_NAME, 'enterRoom', this.data.pusher)
-
- this.data.pusher.getPusherContext().start()
- this.status.isPush = true
- resolve()
- })
- }).catch((res)=> {
-
- console.error(TAG_NAME, 'enterRoom error', res)
- reject(res)
- })
-
-
-
- this._loginIM({ ...this.data.config, roomID: params.roomID })
- })
- },
-
- exitRoom() {
- if (this.status.pageLife === 'hide') {
-
- console.warn(TAG_NAME, '小程序最小化时不能调用 exitRoom,如果不想听到远端声音,可以调用取消订阅,如果不想远端听到声音,可以调用取消发布')
- }
- return new Promise((resolve, reject) => {
- console.log(TAG_NAME, 'exitRoom')
- this._exitIM()
- this.data.pusher.reset()
- this.status.isPush = false
- const result = this.userController.reset()
- this.setData({
- pusher: this.data.pusher,
- userList: result.userList,
- streamList: result.streamList,
- visibleStreamList: this._filterVisibleStream(result.streamList),
- }, () => {
-
- resolve({ userList: this.data.userList, streamList: this.data.streamList })
- console.log(TAG_NAME, 'exitRoom success', this.data.pusher, this.data.streamList, this.data.userList)
-
- this._emitter.emit(EVENT.LOCAL_LEAVE, { userID: this.data.pusher.userID })
- })
- })
- },
-
- publishLocalVideo() {
-
- console.log(TAG_NAME, 'publishLocalVideo 开启摄像头')
- return this._setPusherConfig({ enableCamera: true })
- },
-
- unpublishLocalVideo() {
-
- console.log(TAG_NAME, 'unpublshLocalVideo 关闭摄像头')
- return this._setPusherConfig({ enableCamera: false })
- },
-
- publishLocalAudio() {
-
- console.log(TAG_NAME, 'publishLocalAudio 开启麦克风')
- return this._setPusherConfig({ enableMic: true })
- },
-
- unpublishLocalAudio() {
-
- console.log(TAG_NAME, 'unpublshLocalAudio 关闭麦克风')
- return this._setPusherConfig({ enableMic: false })
- },
-
- subscribeRemoteVideo(params) {
- console.log(TAG_NAME, 'subscribeRemoteVideo', params)
-
- const config = {
- muteVideo: false,
- }
-
- const streamType = params.streamType === 'small' ? 'main' : params.streamType
- const stream = this.userController.getStream({
- userID: params.userID,
- streamType: streamType,
- })
- stream.muteVideoPrev = false
- if (params.streamType === 'small' || params.streamType === 'main') {
- if (stream && stream.streamType === 'main') {
- console.log(TAG_NAME, 'subscribeRemoteVideo switch small', stream.src)
- if (params.streamType === 'small') {
- config.src = stream.src.replace('main', 'small')
- config._definitionType = 'small'
- } else if (params.streamType === 'main') {
- stream.src = stream.src.replace('small', 'main')
- config._definitionType = 'main'
- }
- console.log(TAG_NAME, 'subscribeRemoteVideo', stream.src)
- }
- }
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: streamType,
- config: config,
- })
- },
-
- unsubscribeRemoteVideo(params) {
- console.log(TAG_NAME, 'unsubscribeRemoteVideo', params)
- const stream = this.userController.getStream({
- userID: params.userID,
- streamType: params.streamType,
- })
- stream.muteVideoPrev = true
-
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- muteVideo: true,
- },
- })
- },
-
- subscribeRemoteAudio(params) {
- console.log(TAG_NAME, 'subscribeRemoteAudio', params)
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: 'main',
- config: {
- muteAudio: false,
- },
- })
- },
-
- unsubscribeRemoteAudio(params) {
- console.log(TAG_NAME, 'unsubscribeRemoteAudio', params)
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: 'main',
- config: {
- muteAudio: true,
- },
- })
- },
- on(eventCode, handler, context) {
- this._emitter.on(eventCode, handler, context)
- },
- off(eventCode, handler) {
- this._emitter.off(eventCode, handler)
- },
- getRemoteUserList() {
- return this.data.userList
- },
-
- switchCamera() {
- if (!this.data.cameraPosition) {
-
- this.data.cameraPosition = this.data.pusher.frontCamera
- }
- console.log(TAG_NAME, 'switchCamera', this.data.cameraPosition)
- this.data.cameraPosition = this.data.cameraPosition === 'front' ? 'back' : 'front'
- this.setData({
- cameraPosition: this.data.cameraPosition,
- }, () => {
- console.log(TAG_NAME, 'switchCamera success', this.data.cameraPosition)
- })
-
- this.data.pusher.getPusherContext().switchCamera()
- },
-
- setViewRect(params) {
- console.log(TAG_NAME, 'setViewRect', params)
- if (this.data.template !== 'custom') {
- console.warn(`如需使用setViewRect方法,请初始化时设置template:"custom", 当前 template:"${this.data.template}"`)
- }
- console.info(`不建议使用该方法动态修改样式,避免引起微信小程序渲染问题,建议直接修改 wxml wxss 进行样式定制化`)
- if (this.data.pusher.userID === params.userID) {
- return this._setPusherConfig({
- xAxis: params.xAxis,
- yAxis: params.yAxis,
- width: params.width,
- height: params.height,
- })
- }
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- xAxis: params.xAxis,
- yAxis: params.yAxis,
- width: params.width,
- height: params.height,
- },
- })
- },
-
- setViewVisible(params) {
- console.log(TAG_NAME, 'setViewVisible', params)
- if (this.data.template !== 'custom') {
- console.warn(`如需使用setViewVisible方法,请初始化时设置template:"custom", 当前 template:"${this.data.template}"`)
- }
- console.info(`不建议使用该方法动态修改样式,避免引起微信小程序渲染问题,建议直接修改 wxml wxss 进行样式定制化`)
- if (this.data.pusher.userID === params.userID) {
- return this._setPusherConfig({
- isVisible: params.isVisible,
- })
- }
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- isVisible: params.isVisible,
- },
- })
- },
-
- setViewZIndex(params) {
- console.log(TAG_NAME, 'setViewZIndex', params)
- if (this.data.template !== 'custom') {
- console.warn(`如需使用setViewZIndex方法,请初始化时设置template:"custom", 当前 template:"${this.data.template}"`)
- }
- console.info(`不建议使用该方法动态修改样式,避免引起微信小程序渲染问题,建议直接修改 wxml wxss 进行样式定制化`)
- if (this.data.pusher.userID === params.userID) {
- return this._setPusherConfig({
- zIndex: params.zindex || params.zIndex,
- })
- }
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- zIndex: params.zindex || params.zIndex,
- },
- })
- },
-
- playBGM(params) {
- return new Promise((resolve, reject) => {
- this.data.pusher.getPusherContext().playBGM({
- url: params.url,
-
- success: () => {
- console.log(TAG_NAME, '播放背景音成功')
-
- resolve()
- },
- fail: () => {
- console.log(TAG_NAME, '播放背景音失败')
- this._emitter.emit(EVENT.BGM_PLAY_FAIL)
- reject(new Error('播放背景音失败'))
- },
-
-
-
-
- })
- })
- },
- stopBGM() {
- this.data.pusher.getPusherContext().stopBGM()
- },
- pauseBGM() {
- this.data.pusher.getPusherContext().pauseBGM()
- },
- resumeBGM() {
- this.data.pusher.getPusherContext().resumeBGM()
- },
-
- setBGMVolume(params) {
- console.log(TAG_NAME, 'setBGMVolume', params)
- this.data.pusher.getPusherContext().setBGMVolume({ volume: params.volume })
- },
-
- setMICVolume(params) {
- console.log(TAG_NAME, 'setMICVolume', params)
- this.data.pusher.getPusherContext().setMICVolume({ volume: params.volume })
- },
-
- sendSEI(params) {
- return new Promise((resolve, reject) => {
- this.data.pusher.getPusherContext().sendMessage({
- msg: params.message,
- success: function(result) {
- resolve(result)
- },
- })
- })
- },
-
- snapshot(params) {
- console.log(TAG_NAME, 'snapshot', params)
- return new Promise((resolve, reject) => {
- this.captureSnapshot(params).then((result)=>{
- wx.saveImageToPhotosAlbum({
- filePath: result.tempImagePath,
- success(res) {
- wx.showToast({
- title: '已保存到相册',
- })
- console.log('save photo is success', res)
- resolve(result)
- },
- fail: function(error) {
- wx.showToast({
- icon: 'none',
- title: '保存失败',
- })
- console.log('save photo is fail', error)
- reject(error)
- },
- })
- }).catch((error)=>{
- reject(error)
- })
- })
- },
-
- captureSnapshot(params) {
- return new Promise((resolve, reject) => {
- if (params.userID === this.data.pusher.userID) {
-
- this.data.pusher.getPusherContext().snapshot({
- quality: 'raw',
- complete: (result) => {
- console.log(TAG_NAME, 'snapshot pusher', result)
- if (result.tempImagePath) {
- resolve(result)
- } else {
- console.log('snapShot 回调失败', result)
- reject(new Error('截图失败'))
- }
- },
- })
- } else {
-
- this.userController.getStream(params).playerContext.snapshot({
- quality: 'raw',
- complete: (result) => {
- console.log(TAG_NAME, 'snapshot player', result)
- if (result.tempImagePath) {
- resolve(result)
- } else {
- console.log('snapShot 回调失败', result)
- reject(new Error('截图失败'))
- }
- },
- })
- }
- })
- },
-
- enterFullscreen(params) {
- console.log(TAG_NAME, 'enterFullscreen', params)
- return new Promise((resolve, reject) => {
- this.userController.getStream(params).playerContext.requestFullScreen({
- direction: params.direction || 0,
- success: (event) => {
- console.log(TAG_NAME, 'enterFullscreen success', event)
- resolve(event)
- },
- fail: (event) => {
- console.log(TAG_NAME, 'enterFullscreen fail', event)
- reject(event)
- },
- })
- })
- },
-
- exitFullscreen(params) {
- console.log(TAG_NAME, 'exitFullscreen', params)
- return new Promise((resolve, reject) => {
- this.userController.getStream(params).playerContext.exitFullScreen({
- success: (event) => {
- console.log(TAG_NAME, 'exitFullScreen success', event)
- resolve(event)
- },
- fail: (event) => {
- console.log(TAG_NAME, 'exitFullScreen fail', event)
- reject(event)
- },
- })
- })
- },
-
- setRemoteOrientation(params) {
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- orientation: params.orientation,
- },
- })
- },
-
- setViewOrientation(params) {
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- orientation: params.orientation,
- },
- })
- },
-
- setRemoteFillMode(params) {
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- objectFit: params.fillMode,
- },
- })
- },
-
- setViewFillMode(params) {
- return this._setPlayerConfig({
- userID: params.userID,
- streamType: params.streamType,
- config: {
- objectFit: params.fillMode,
- },
- })
- },
-
- sendC2CTextMessage(params) {
- if (!this.tim) {
- console.warn(TAG_NAME, '未开启IM功能,该方法无法使用', params)
- return
- }
- console.log(TAG_NAME, 'sendC2CTextMessage', params)
- const message = this.tim.createTextMessage({
- to: params.userID + '',
- conversationType: TIM.TYPES.CONV_C2C,
- payload: {
- text: params.message,
- },
- })
- const promise = this.tim.sendMessage(message)
- promise.then(function(imResponse) {
-
- console.log(TAG_NAME, 'sendC2CTextMessage success', imResponse)
- }).catch(function(imError) {
-
- console.warn(TAG_NAME, 'sendC2CTextMessage error:', imError)
- })
- return promise
- },
-
- sendC2CCustomMessage(params) {
- if (!this.tim) {
- console.warn(TAG_NAME, '未开启IM功能,该方法无法使用', params)
- return
- }
- console.log(TAG_NAME, 'sendC2CCustomMessage', params)
- const message = this.tim.createCustomMessage({
- to: params.userID + '',
- conversationType: TIM.TYPES.CONV_C2C,
- payload: params.payload,
- })
- const promise = this.tim.sendMessage(message)
- promise.then(function(imResponse) {
-
- console.log(TAG_NAME, 'sendMessage success', imResponse)
- }).catch(function(imError) {
-
- console.warn(TAG_NAME, 'sendMessage error:', imError)
- })
- return promise
- },
-
- sendGroupTextMessage(params) {
- if (!this.tim) {
- console.warn(TAG_NAME, '未开启IM功能,该方法无法使用', params)
- return
- }
- console.log(TAG_NAME, 'sendGroupTextMessage', params)
- const message = this.tim.createTextMessage({
- to: params.roomID + '',
- conversationType: TIM.TYPES.CONV_GROUP,
- payload: {
- text: params.message,
- },
- })
- const promise = this.tim.sendMessage(message)
- promise.then(function(imResponse) {
-
- console.log(TAG_NAME, 'sendGroupTextMessage success', imResponse)
- }).catch(function(imError) {
-
- console.warn(TAG_NAME, 'sendGroupTextMessage error:', imError)
- })
- return promise
- },
-
- sendGroupCustomMessage(params) {
- if (!this.tim) {
- console.warn(TAG_NAME, '未开启IM功能,该方法无法使用', params)
- return
- }
- console.log(TAG_NAME, 'sendGroupCustomMessage', params)
- const message = this.tim.createCustomMessage({
- to: params.roomID + '',
- conversationType: TIM.TYPES.CONV_GROUP,
- payload: params.payload,
- })
- const promise = this.tim.sendMessage(message)
- promise.then(function(imResponse) {
-
- console.log(TAG_NAME, 'sendMessage success', imResponse)
- }).catch(function(imError) {
-
- console.warn(TAG_NAME, 'sendMessage error:', imError)
- })
- return promise
- },
-
-
-
-
-
-
-
-
-
-
- _setPusherConfig(config, skipLog = false) {
- if (!skipLog) {
- console.log(TAG_NAME, '_setPusherConfig', config, this.data.pusher)
- }
- return new Promise((resolve, reject) => {
- if (!this.data.pusher) {
- this.data.pusher = new Pusher(config)
- } else {
- Object.assign(this.data.pusher, config)
- }
- this.setData({
- pusher: this.data.pusher,
- }, () => {
- if (!skipLog) {
- console.log(TAG_NAME, '_setPusherConfig setData compelete', 'config:', config, 'pusher:', this.data.pusher)
- }
- resolve(config)
- })
- })
- },
-
- _setPlayerConfig(params) {
- const userID = params.userID
- const streamType = params.streamType
- const config = params.config
- console.log(TAG_NAME, '_setPlayerConfig', params)
- return new Promise((resolve, reject) => {
-
- const user = this.userController.getUser(userID)
- if (user && user.streams[streamType]) {
- Object.assign(user.streams[streamType], config)
-
- this.setData({
- streamList: this.data.streamList,
- visibleStreamList: this._filterVisibleStream(this.data.streamList, true),
- }, () => {
-
- resolve(params)
- })
- } else {
-
- console.warn(TAG_NAME, '指定 userID 或者 streamType 不存在')
-
- }
- })
- },
-
- _setList(params) {
- console.log(TAG_NAME, '_setList', params, this.data.template)
- const { userList, streamList } = params
- return new Promise((resolve, reject) => {
- let visibleStreamList = []
- const data = {
- userList: userList || this.data.userList,
- streamList: streamList || this.data.streamList,
- }
- if (this.data.template === 'grid') {
- visibleStreamList = this._filterVisibleStream(streamList)
- data.visibleStreamList = visibleStreamList || this.data.visibleStreamList
- data.gridPagePlaceholderStreamList = this.data.gridPagePlaceholderStreamList
- data.gridCurrentPage = this.data.gridCurrentPage
- data.gridPageCount = this.data.gridPageCount
- }
- this.setData(data, () => {
- resolve(params)
- })
- })
- },
-
- _checkParam(rtcConfig) {
- console.log(TAG_NAME, 'checkParam config:', rtcConfig)
- if (!rtcConfig.sdkAppID) {
- console.error('未设置 sdkAppID')
- return false
- }
- if (rtcConfig.roomID === undefined) {
- console.error('未设置 roomID')
- return false
- }
- if (rtcConfig.roomID < 1 || rtcConfig.roomID > 4294967296) {
- console.error('roomID 超出取值范围 1 ~ 4294967295')
- return false
- }
- if (!rtcConfig.userID) {
- console.error('未设置 userID')
- return false
- }
- if (!rtcConfig.userSig) {
- console.error('未设置 userSig')
- return false
- }
- if (!rtcConfig.template) {
- console.error('未设置 template')
- return false
- }
- return true
- },
- _getPushUrl(rtcConfig) {
-
- console.log(TAG_NAME, '_getPushUrl', rtcConfig)
- if (ENV.IS_TRTC) {
-
- return new Promise((resolve, reject) => {
-
-
-
-
- rtcConfig.scene = !rtcConfig.scene || rtcConfig.scene === 'rtc' ? 'videocall' : rtcConfig.scene
- rtcConfig.enableBlackStream = rtcConfig.enableBlackStream || ''
- rtcConfig.encsmall = rtcConfig.encsmall || 0
- rtcConfig.cloudenv = rtcConfig.cloudenv || 'PRO'
- rtcConfig.streamID = rtcConfig.streamID || ''
- rtcConfig.userDefineRecordID = rtcConfig.userDefineRecordID || ''
- rtcConfig.privateMapKey = rtcConfig.privateMapKey || ''
- rtcConfig.pureAudioMode = rtcConfig.pureAudioMode || ''
- rtcConfig.recvMode = rtcConfig.recvMode || 1
- let roomID = ''
- if (/^\d+$/.test(rtcConfig.roomID)) {
-
- roomID = '&roomid=' + rtcConfig.roomID
- } else {
-
- roomID = '&strroomid=' + rtcConfig.roomID
- }
- setTimeout(()=> {
- const pushUrl = 'room://cloud.tencent.com/rtc?sdkappid=' + rtcConfig.sdkAppID +
- roomID +
- '&userid=' + rtcConfig.userID +
- '&usersig=' + rtcConfig.userSig +
- '&appscene=' + rtcConfig.scene +
- '&encsmall=' + rtcConfig.encsmall +
- '&cloudenv=' + rtcConfig.cloudenv +
- '&enableBlackStream=' + rtcConfig.enableBlackStream +
- '&streamid=' + rtcConfig.streamID +
- '&userdefinerecordid=' + rtcConfig.userDefineRecordID +
- '&privatemapkey=' + rtcConfig.privateMapKey +
- '&pureaudiomode=' + rtcConfig.pureAudioMode +
- '&recvmode=' + rtcConfig.recvMode
- console.warn(TAG_NAME, 'getPushUrl result:', pushUrl)
- resolve(pushUrl)
- }, 0)
- })
- }
- console.error(TAG_NAME, '组件仅支持微信 App iOS >=7.0.9, Android >= 7.0.8, 小程序基础库版 >= 2.10.0')
- console.error(TAG_NAME, '需要真机运行,开发工具不支持实时音视频')
- },
-
- _requestSigServer(rtcConfig) {
- console.log(TAG_NAME, '_requestSigServer:', rtcConfig)
- const sdkAppID = rtcConfig.sdkAppID
- const userID = rtcConfig.userID
- const userSig = rtcConfig.userSig
- const roomID = rtcConfig.roomID
- const privateMapKey = rtcConfig.privateMapKey
- rtcConfig.useCloud = rtcConfig.useCloud === undefined ? true : rtcConfig.useCloud
- let url = rtcConfig.useCloud ? 'https://official.opensso.tencent-cloud.com/v4/openim/jsonvideoapp' : 'https://yun.tim.qq.com/v4/openim/jsonvideoapp'
- url += '?sdkappid=' + sdkAppID + '&identifier=' + userID + '&usersig=' + userSig + '&random=' + Date.now() + '&contenttype=json'
- const reqHead = {
- 'Cmd': 1,
- 'SeqNo': 1,
- 'BusType': 7,
- 'GroupId': roomID,
- }
- const reqBody = {
- 'PrivMapEncrypt': privateMapKey,
- 'TerminalType': 1,
- 'FromType': 3,
- 'SdkVersion': 26280566,
- }
- console.log(TAG_NAME, '_requestSigServer:', url, reqHead, reqBody)
- return new Promise((resolve, reject) => {
- wx.request({
- url: url,
- data: {
- 'ReqHead': reqHead,
- 'ReqBody': reqBody,
- },
- method: 'POST',
- success: (res) => {
- console.log('_requestSigServer success:', res)
- if (res.data['ErrorCode'] || res.data['RspHead']['ErrorCode'] !== 0) {
-
- console.error('获取roomsig失败')
- reject(res)
- }
- const roomSig = JSON.stringify(res.data['RspBody'])
- let pushUrl = 'room://cloud.tencent.com?sdkappid=' + sdkAppID + '&roomid=' + roomID + '&userid=' + userID + '&roomsig=' + encodeURIComponent(roomSig)
-
-
- if (rtcConfig.pureAudioPushMod || rtcConfig.recordId) {
- const bizbuf = {
- Str_uc_params: {
- pure_audio_push_mod: 0,
- record_id: 0,
- },
- }
-
- if (rtcConfig.pureAudioPushMod) {
- bizbuf.Str_uc_params.pure_audio_push_mod = rtcConfig.pureAudioPushMod
- } else {
- delete bizbuf.Str_uc_params.pure_audio_push_mod
- }
-
- if (rtcConfig.recordId) {
- bizbuf.Str_uc_params.record_id = rtcConfig.recordId
- } else {
- delete bizbuf.Str_uc_params.record_id
- }
- pushUrl += '&bizbuf=' + encodeURIComponent(JSON.stringify(bizbuf))
- }
- console.log('roomSigInfo', pushUrl)
- resolve(pushUrl)
- },
- fail: (res) => {
- console.log(TAG_NAME, 'requestSigServer fail:', res)
- reject(res)
- },
- })
- })
- },
- _doubleTabToggleFullscreen(event) {
- const curTime = event.timeStamp
- const lastTime = this._lastTapTime
- const lastTapCoordinate = this._lastTapCoordinate
- const currentTapCoordinate = event.detail
-
- const distence = Math.sqrt(Math.pow(Math.abs(currentTapCoordinate.x - lastTapCoordinate.x), 2) + Math.pow(Math.abs(currentTapCoordinate.y - lastTapCoordinate.y), 2))
- this._lastTapCoordinate = currentTapCoordinate
-
- const beforeLastTime = this._beforeLastTapTime
- console.log(TAG_NAME, '_doubleTabToggleFullscreen', event, lastTime, beforeLastTime, distence)
- if (curTime - lastTime > 0 && curTime - lastTime < 300 && lastTime - beforeLastTime > 1500 && distence < 20) {
- const userID = event.currentTarget.dataset.userid
- const streamType = event.currentTarget.dataset.streamtype
- if (this._isFullscreen) {
- this.exitFullscreen({ userID, streamType }).then(() => {
- this._isFullscreen = false
- }).catch(() => {
- })
- } else {
-
- let direction
-
-
-
-
-
- this.enterFullscreen({ userID, streamType, direction }).then(() => {
- this._isFullscreen = true
- }).catch(() => {
- })
- }
- this._beforeLastTapTime = lastTime
- }
- this._lastTapTime = curTime
- },
-
- _bindEvent() {
-
- this.userController.on(EVENT.REMOTE_USER_JOIN, (event)=>{
- console.log(TAG_NAME, '远端用户进房', event, event.data.userID)
- this.setData({
- userList: event.data.userList,
- }, () => {
- this._emitter.emit(EVENT.REMOTE_USER_JOIN, { userID: event.data.userID })
- })
- console.log(TAG_NAME, 'REMOTE_USER_JOIN', 'streamList:', this.data.streamList, 'userList:', this.data.userList)
- })
-
- this.userController.on(EVENT.REMOTE_USER_LEAVE, (event)=>{
- console.log(TAG_NAME, '远端用户离开', event, event.data.userID)
- if (event.data.userID) {
- this._setList({
- userList: event.data.userList,
- streamList: event.data.streamList,
- }).then(() => {
- this._emitter.emit(EVENT.REMOTE_USER_LEAVE, { userID: event.data.userID })
- })
- }
- console.log(TAG_NAME, 'REMOTE_USER_LEAVE', 'streamList:', this.data.streamList, 'userList:', this.data.userList)
- })
-
- this.userController.on(EVENT.REMOTE_VIDEO_ADD, (event)=>{
- console.log(TAG_NAME, '远端视频可用', event, event.data.stream.userID)
- const stream = event.data.stream
-
- if (this.status.pageLife === 'hide') {
- this.status.isOnHideAddStream = true
- stream.isOnHideAdd = true
- }
- this._setList({
- userList: event.data.userList,
- streamList: event.data.streamList,
- }).then(() => {
-
- stream.playerContext = wx.createLivePlayerContext(stream.streamID, this)
-
-
-
- this._emitter.emit(EVENT.REMOTE_VIDEO_ADD, { userID: stream.userID, streamType: stream.streamType })
- })
- console.log(TAG_NAME, 'REMOTE_VIDEO_ADD', 'streamList:', this.data.streamList, 'userList:', this.data.userList)
- })
-
- this.userController.on(EVENT.REMOTE_VIDEO_REMOVE, (event)=>{
- console.log(TAG_NAME, '远端视频移除', event, event.data.stream.userID)
- const stream = event.data.stream
- this._setList({
- userList: event.data.userList,
- streamList: event.data.streamList,
- }).then(() => {
-
- if (stream.userID && stream.streamType) {
- this._emitter.emit(EVENT.REMOTE_VIDEO_REMOVE, { userID: stream.userID, streamType: stream.streamType })
- }
- })
- console.log(TAG_NAME, 'REMOTE_VIDEO_REMOVE', 'streamList:', this.data.streamList, 'userList:', this.data.userList)
- })
-
- this.userController.on(EVENT.REMOTE_AUDIO_ADD, (event)=>{
- console.log(TAG_NAME, '远端音频可用', event)
- const stream = event.data.stream
- this._setList({
- userList: event.data.userList,
- streamList: event.data.streamList,
- }).then(() => {
- stream.playerContext = wx.createLivePlayerContext(stream.streamID, this)
-
-
-
- this._emitter.emit(EVENT.REMOTE_AUDIO_ADD, { userID: stream.userID, streamType: stream.streamType })
- })
- console.log(TAG_NAME, 'REMOTE_AUDIO_ADD', 'streamList:', this.data.streamList, 'userList:', this.data.userList)
- })
-
- this.userController.on(EVENT.REMOTE_AUDIO_REMOVE, (event)=>{
- console.log(TAG_NAME, '远端音频移除', event, event.data.stream.userID)
- const stream = event.data.stream
- this._setList({
- userList: event.data.userList,
- streamList: event.data.streamList,
- }).then(() => {
-
- if (stream.userID && stream.streamType) {
- this._emitter.emit(EVENT.REMOTE_AUDIO_REMOVE, { userID: stream.userID, streamType: stream.streamType })
- }
- })
- console.log(TAG_NAME, 'REMOTE_AUDIO_REMOVE', 'streamList:', this.data.streamList, 'userList:', this.data.userList)
- })
- },
-
- _pusherStateChangeHandler(event) {
- const code = event.detail.code
- const message = event.detail.message
- console.log(TAG_NAME, 'pusherStateChange:', code, event)
- switch (code) {
- case 0:
- console.log(TAG_NAME, message, code)
- break
- case 1001:
- console.log(TAG_NAME, '已经连接推流服务器', code)
- break
- case 1002:
- console.log(TAG_NAME, '已经与服务器握手完毕,开始推流', code)
- break
- case 1003:
- console.log(TAG_NAME, '打开摄像头成功', code)
- break
- case 1004:
- console.log(TAG_NAME, '录屏启动成功', code)
- break
- case 1005:
- console.log(TAG_NAME, '推流动态调整分辨率', code)
- break
- case 1006:
- console.log(TAG_NAME, '推流动态调整码率', code)
- break
- case 1007:
- console.log(TAG_NAME, '首帧画面采集完成', code)
- break
- case 1008:
- console.log(TAG_NAME, '编码器启动', code)
- break
- case 1018:
- console.log(TAG_NAME, '进房成功', code)
- this._emitter.emit(EVENT.LOCAL_JOIN, { userID: this.data.pusher.userID })
- break
- case 1019:
- console.log(TAG_NAME, '退出房间', code)
-
-
- break
- case 2003:
- console.log(TAG_NAME, '渲染首帧视频', code)
- break
- case 1020:
- case 1031:
- case 1032:
- case 1033:
- case 1034:
-
- this.userController.userEventHandler(event)
- break
- case -1301:
- console.error(TAG_NAME, '打开摄像头失败: ', code)
- this._emitter.emit(EVENT.ERROR, { code, message })
- break
- case -1302:
- console.error(TAG_NAME, '打开麦克风失败: ', code)
- this._emitter.emit(EVENT.ERROR, { code, message })
- break
- case -1303:
- console.error(TAG_NAME, '视频编码失败: ', code)
- this._emitter.emit(EVENT.ERROR, { code, message })
- break
- case -1304:
- console.error(TAG_NAME, '音频编码失败: ', code)
- this._emitter.emit(EVENT.ERROR, { code, message })
- break
- case -1307:
- console.error(TAG_NAME, '推流连接断开: ', code)
- this._emitter.emit(EVENT.ERROR, { code, message })
- break
- case -100018:
- console.error(TAG_NAME, '进房失败: userSig 校验失败,请检查 userSig 是否填写正确', code, message)
- this._emitter.emit(EVENT.ERROR, { code, message })
- break
- case 5000:
- console.log(TAG_NAME, '小程序被挂起: ', code)
-
-
- break
- case 5001:
-
- console.log(TAG_NAME, '小程序悬浮窗被关闭: ', code)
- this.status.isPending = true
- if (this.status.isPush) {
- this.exitRoom()
- }
- break
- case 1021:
- console.log(TAG_NAME, '网络类型发生变化,需要重新进房', code)
- break
- case 2007:
- console.log(TAG_NAME, '本地视频播放loading: ', code)
- break
- case 2004:
- console.log(TAG_NAME, '本地视频播放开始: ', code)
- break
- default:
- console.log(TAG_NAME, message, code)
- }
- },
- _pusherNetStatusHandler(event) {
-
- this._emitter.emit(EVENT.LOCAL_NET_STATE_UPDATE, event)
- },
- _pusherErrorHandler(event) {
-
- console.warn(TAG_NAME, 'pusher error', event)
- try {
- const code = event.detail.errCode
- const message = event.detail.errMsg
- this._emitter.emit(EVENT.ERROR, { code, message })
- } catch (exception) {
- console.error(TAG_NAME, 'pusher error data parser exception', event, exception)
- }
- },
- _pusherBGMStartHandler(event) {
-
-
- },
- _pusherBGMProgressHandler(event) {
-
- this._emitter.emit(EVENT.BGM_PLAY_PROGRESS, event)
- },
- _pusherBGMCompleteHandler(event) {
-
- this._emitter.emit(EVENT.BGM_PLAY_COMPLETE, event)
- },
- _pusherAudioVolumeNotify: function(event) {
-
- this._emitter.emit(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, event)
- },
-
-
- _playerStateChange(event) {
-
- this._emitter.emit(EVENT.REMOTE_STATE_UPDATE, event)
- },
- _playerFullscreenChange(event) {
-
- this._emitter.emit(EVENT.REMOTE_FULLSCREEN_UPDATE, event)
- this._emitter.emit(EVENT.VIDEO_FULLSCREEN_UPDATE, event)
- },
- _playerNetStatus(event) {
-
-
- const stream = this.userController.getStream({
- userID: event.currentTarget.dataset.userid,
- streamType: event.currentTarget.dataset.streamtype,
- })
- if (stream && (stream.videoWidth !== event.detail.info.videoWidth || stream.videoHeight !== event.detail.info.videoHeight)) {
- console.log(TAG_NAME, '_playerNetStatus update video size', event)
- stream.videoWidth = event.detail.info.videoWidth
- stream.videoHeight = event.detail.info.videoHeight
- }
- this._emitter.emit(EVENT.REMOTE_NET_STATE_UPDATE, event)
- },
- _playerAudioVolumeNotify(event) {
-
- this._emitter.emit(EVENT.REMOTE_AUDIO_VOLUME_UPDATE, event)
- },
- _filterVisibleStream(streamList, skipPagination) {
- const list = streamList.filter((item) => {
-
-
-
- return (item.hasVideo || item.hasAudio)
- })
-
- list.sort((item1, item2)=>{
- const id1 = item1.userID.toUpperCase()
- const id2 = item2.userID.toUpperCase()
- if (id1 < id2) {
- return -1
- }
- if (id1 > id2) {
- return 1
- }
- return 0
- })
- if (this.data.template === 'grid' && !skipPagination) {
- this._filterGridPageVisibleStream(list)
-
- if (
- this.data.gridCurrentPage > 1 &&
- this.data.gridPagePlaceholderStreamList.length === this.data.gridPlayerPerPage) {
-
- this._gridPageToPrev(list)
- }
- }
-
- return list
- },
- _filterGridPageVisibleStream(list) {
-
- const length = list.length
-
- this.data.gridPageCount = Math.ceil((length + 1) / this.data.gridPlayerPerPage)
- this.data.gridPagePlaceholderStreamList = []
- let visibleCount = 0
-
- let interval
- if (this.data.gridPlayerPerPage > 3) {
- if (this.data.gridCurrentPage === 1) {
- interval = [-1, this.data.gridPlayerPerPage - 1]
- } else {
-
-
-
-
-
-
- interval = [this.data.gridCurrentPage * this.data.gridPlayerPerPage - (this.data.gridPlayerPerPage + 2), this.data.gridCurrentPage * this.data.gridPlayerPerPage - 1]
- }
- } else {
-
- interval = [this.data.gridCurrentPage * this.data.gridPlayerPerPage - (this.data.gridPlayerPerPage + 1), this.data.gridCurrentPage * this.data.gridPlayerPerPage]
- }
- for (let i = 0; i < length; i++) {
- if ( i > interval[0] && i < interval[1]) {
- list[i].isVisible = true
- list[i].muteVideo = list[i].muteVideoPrev === undefined ? list[i].muteVideo : list[i].muteVideoPrev
- visibleCount++
- } else {
- list[i].isVisible = false
- list[i].muteVideo = true
- }
- }
-
- if (this.data.gridCurrentPage !== 1) {
- for (let i = 0; i < this.data.gridPlayerPerPage - visibleCount; i++) {
- this.data.gridPagePlaceholderStreamList.push({ id: 'holder-' + i })
- }
- }
- return list
- },
-
- _keepScreenOn() {
- setInterval(() => {
- wx.setKeepScreenOn({
- keepScreenOn: true,
- })
- }, 20000)
- },
-
-
-
-
-
-
-
-
-
-
- _initIM(config) {
- if (!config.enableIM || !config.sdkAppID || this.tim) {
- return
- }
- console.log(TAG_NAME, '_initIM', config)
-
- const tim = TIM.create({
- SDKAppID: config.sdkAppID,
- })
-
-
-
-
-
- if (config.debugMode) {
- tim.setLogLevel(1)
- } else {
- tim.setLogLevel(4)
- }
-
- tim.off(TIM.EVENT.SDK_READY, this._onIMReady)
- tim.off(TIM.EVENT.MESSAGE_RECEIVED, this._onIMMessageReceived)
- tim.off(TIM.EVENT.SDK_NOT_READY, this._onIMNotReady)
- tim.off(TIM.EVENT.KICKED_OUT, this._onIMKickedOut)
- tim.off(TIM.EVENT.ERROR, this._onIMError)
-
- tim.on(TIM.EVENT.SDK_READY, this._onIMReady, this)
- tim.on(TIM.EVENT.MESSAGE_RECEIVED, this._onIMMessageReceived, this)
- tim.on(TIM.EVENT.SDK_NOT_READY, this._onIMNotReady, this)
- tim.on(TIM.EVENT.KICKED_OUT, this._onIMKickedOut, this)
- tim.on(TIM.EVENT.ERROR, this._onIMError, this)
- this.tim = tim
- wx.tim = tim
- },
- _loginIM(params) {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_loginIM', params)
- return this.tim.login({
- userID: params.userID,
- userSig: params.userSig,
- })
- },
- _logoutIM() {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_logoutIM')
- return this.tim.logout()
- },
- _exitIM() {
-
- if (this.data.exitIMThrottle || !this.tim) {
- return
- }
- this.data.exitIMThrottle = true
- const userList = this.getRemoteUserList()
- const roomID = this.data.config.roomID
- const userID = this.data.config.userID
- this._searchGroup({ roomID }).then((imResponse) => {
-
- if (imResponse.data.group.ownerID === userID && userList.length === 0) {
-
- this._dismissGroup({ roomID }).then(()=>{
- this.data.exitIMThrottle = false
- this._logoutIM()
- }).catch((imError) => {
- this.data.exitIMThrottle = false
- this._logoutIM()
- })
- } else if (imResponse.data.group.ownerID === userID) {
- this.data.exitIMThrottle = false
-
- this._logoutIM()
- } else {
-
- this._quitGroup({ roomID }).then(()=>{
- this.data.exitIMThrottle = false
- this._logoutIM()
- }).catch((imError) => {
- this.data.exitIMThrottle = false
- this._logoutIM()
- })
- }
- }).catch((imError) => {
- this.data.exitIMThrottle = false
-
- this._logoutIM()
- })
- },
- _searchGroup(params) {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_searchGroup', params)
- const tim = this.tim
- const promise = tim.searchGroupByID(params.roomID + '')
- promise.then(function(imResponse) {
-
- console.log(TAG_NAME, '_searchGroup success', imResponse)
- }).catch(function(imError) {
- console.warn(TAG_NAME, '_searchGroup fail,TIM 报错信息不影响后续逻辑,可以忽略', imError)
- })
- return promise
- },
-
- _createGroup(params) {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_createGroup', params)
- const promise = this.tim.createGroup({
- groupID: params.roomID + '',
- name: params.roomID + '',
- type: IM_GROUP_TYPE,
- })
- promise.then((imResponse) => {
- console.log(TAG_NAME, '_createGroup success', imResponse.data.group)
- }).catch((imError) => {
- console.warn(TAG_NAME, '_createGroup error', imError)
- })
- return promise
- },
-
- _joinGroup(params) {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_joinGroup', params)
- const promise = this.tim.joinGroup({ groupID: params.roomID + '', type: IM_GROUP_TYPE })
- promise.then((imResponse) => {
- switch (imResponse.data.status) {
- case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL:
- break
- case TIM.TYPES.JOIN_STATUS_SUCCESS:
- case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP:
-
-
-
-
- console.log(TAG_NAME, '_joinGroup success', imResponse)
- break
- default:
- break
- }
- }).catch((imError) => {
- console.warn(TAG_NAME, 'joinGroup error', imError)
- })
- return promise
- },
- _quitGroup(params) {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_quitGroup', params)
- const promise = this.tim.quitGroup(params.roomID + '')
- promise.then((imResponse) => {
- console.log(TAG_NAME, '_quitGroup success', imResponse)
- }).catch((imError) => {
- console.warn(TAG_NAME, 'quitGroup error', imError)
- })
- return promise
- },
- _dismissGroup(params) {
- if (!this.tim) {
- return
- }
- console.log(TAG_NAME, '_dismissGroup', params)
- const promise = this.tim.dismissGroup(params.roomID + '')
- promise.then((imResponse) => {
- console.log(TAG_NAME, '_dismissGroup success', imResponse)
- }).catch((imError) => {
- console.warn(TAG_NAME, '_dismissGroup error', imError)
- })
- return promise
- },
- _onIMReady(event) {
- console.log(TAG_NAME, 'IM.READY', event)
- this._emitter.emit(EVENT.IM_READY, event)
- const roomID = this.data.config.roomID
-
- this._searchGroup({ roomID }).then((res) => {
-
-
- this._joinGroup({ roomID })
- }).catch(() => {
-
- this._createGroup({ roomID }).then((res) => {
-
- this._joinGroup({ roomID })
- }).catch((imError)=> {
- if (imError.code === 10021) {
- console.log(TAG_NAME, '群已存在,直接进群', event)
- this._joinGroup({ roomID })
- }
- })
- })
-
-
- },
- _onIMMessageReceived(event) {
-
- console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', event)
-
- const messageData = event.data
- const roomID = this.data.config.roomID + ''
- const userID = this.data.config.userID + ''
- for (let i = 0; i < messageData.length; i++) {
- const message = messageData[i]
-
- if (message.to === roomID + '' || message.to === userID) {
-
- console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', message, message.type, TIM.TYPES.MSG_TEXT)
- if (message.type === TIM.TYPES.MSG_TEXT) {
- this._pushMessageList({
- name: message.from,
- message: message.payload.text,
- })
- } else {
- if (message.type === TIM.TYPES.MSG_GRP_SYS_NOTICE && message.payload.operationType === 2) {
-
- this._pushMessageList({
- name: '系统通知',
- message: `欢迎 ${userID}`,
- })
- }
-
- }
- }
- }
- this._emitter.emit(EVENT.IM_MESSAGE_RECEIVED, event)
- },
- _onIMNotReady(event) {
- console.log(TAG_NAME, 'IM.NOT_READY', event)
- this._emitter.emit(EVENT.IM_NOT_READY, event)
-
-
- },
- _onIMKickedOut(event) {
- console.log(TAG_NAME, 'IM.KICKED_OUT', event)
- this._emitter.emit(EVENT.IM_KICKED_OUT, event)
-
-
-
-
-
-
- },
- _onIMError(event) {
- console.log(TAG_NAME, 'IM.ERROR', event)
- this._emitter.emit(EVENT.IM_ERROR, event)
-
-
-
-
- },
-
-
-
-
-
-
-
-
-
-
-
-
-
- _toggleVideo() {
- if (this.data.pusher.enableCamera) {
- this.unpublishLocalVideo()
- } else {
- this.publishLocalVideo()
- }
- },
- _toggleAudio() {
- if (this.data.pusher.enableMic) {
- this.unpublishLocalAudio()
- } else {
- this.publishLocalAudio()
- }
- },
- _debugToggleRemoteVideo(event) {
- console.log(TAG_NAME, '_debugToggleRemoteVideo', event.currentTarget.dataset)
- const userID = event.currentTarget.dataset.userID
- const streamType = event.currentTarget.dataset.streamType
- const stream = this.data.streamList.find((item)=>{
- return item.userID === userID && item.streamType === streamType
- })
- if (stream.muteVideo) {
- this.subscribeRemoteVideo({ userID, streamType })
-
- } else {
- this.unsubscribeRemoteVideo({ userID, streamType })
-
- }
- },
- _debugToggleRemoteAudio(event) {
- console.log(TAG_NAME, '_debugToggleRemoteAudio', event.currentTarget.dataset)
- const userID = event.currentTarget.dataset.userID
- const streamType = event.currentTarget.dataset.streamType
- const stream = this.data.streamList.find((item)=>{
- return item.userID === userID && item.streamType === streamType
- })
- if (stream.muteAudio) {
- this.subscribeRemoteAudio({ userID })
- } else {
- this.unsubscribeRemoteAudio({ userID })
- }
- },
- _debugToggleVideoDebug() {
- this.setData({
- debug: !this.data.debug,
- })
- },
- _debugExitRoom() {
- this.exitRoom()
- },
- _debugEnterRoom() {
- Object.assign(this.data.pusher, this.data.config)
- this.enterRoom({ roomID: this.data.config.roomID }).then(()=>{
- setTimeout(()=>{
- this.publishLocalVideo()
- this.publishLocalAudio()
- }, 2000)
-
- })
- },
- _debugGoBack() {
- wx.navigateBack({
- delta: 1,
- })
- },
- _debugTogglePanel() {
- this.setData({
- debugPanel: !this.data.debugPanel,
- })
- },
- _debugSendRandomMessage() {
- const userList = this.getRemoteUserList()
- if (userList.length === 0 || !this.tim) {
- return false
- }
- const roomID = this.data.config.roomID
- const message = `Hello! ${userList[0].userID} ${9999 * Math.random()}`
- const userID = userList[0].userID
- this.sendC2CTextMessage({
- userID: userID,
- message: message,
- })
- const promise = this.sendGroupTextMessage({
- roomID: roomID,
- message: message,
- })
-
- this._pushMessageList({
- name: userID,
- message: message,
- })
- promise.then(function(imResponse) {
-
- console.log(TAG_NAME, '_debugSendRandomMessage success', imResponse)
- wx.showToast({
- title: '发送成功',
- icon: 'success',
- duration: 1000,
- })
- }).catch(function(imError) {
-
- console.warn(TAG_NAME, '_debugSendRandomMessage error', imError)
- wx.showToast({
- title: '发送失败',
- icon: 'none',
- duration: 1000,
- })
- })
- },
- _toggleAudioVolumeType() {
- if (this.data.pusher.audioVolumeType === 'voicecall') {
- this._setPusherConfig({
- audioVolumeType: 'media',
- })
- } else {
- this._setPusherConfig({
- audioVolumeType: 'voicecall',
- })
- }
- },
- _toggleSoundMode() {
- if (this.data.userList.length === 0 ) {
- return
- }
- const stream = this.userController.getStream({
- userID: this.data.userList[0].userID,
- streamType: 'main',
- })
- if (stream) {
- if (stream.soundMode === 'speaker') {
- stream['soundMode'] = 'ear'
- } else {
- stream['soundMode'] = 'speaker'
- }
- this._setPlayerConfig({
- userID: stream.userID,
- streamType: 'main',
- config: {
- soundMode: stream['soundMode'],
- },
- })
- }
- },
-
- _hangUp() {
- this.exitRoom()
- wx.navigateBack({
- delta: 1,
- })
- },
-
- handleSubscribeAudio() {
- if (this.data.pusher.enableMic) {
- this.unpublishLocalAudio()
- } else {
- this.publishLocalAudio()
- }
- },
-
- _handleSubscribeRemoteVideo(event) {
- const userID = event.currentTarget.dataset.userID
- const streamType = event.currentTarget.dataset.streamType
- const stream = this.data.streamList.find((item)=>{
- return item.userID === userID && item.streamType === streamType
- })
- if (stream.muteVideo) {
- this.subscribeRemoteVideo({ userID, streamType })
- } else {
- this.unsubscribeRemoteVideo({ userID, streamType })
- }
- },
-
- _handleSubscribeRemoteAudio(event) {
- const userID = event.currentTarget.dataset.userID
- const streamType = event.currentTarget.dataset.streamType
- const stream = this.data.streamList.find((item)=>{
- return item.userID === userID && item.streamType === streamType
- })
- if (stream.muteAudio) {
- this.subscribeRemoteAudio({ userID })
- } else {
- this.unsubscribeRemoteAudio({ userID })
- }
- },
-
- _switchMemberListPanel() {
- this.setData({
- panelName: this.data.panelName !== 'memberlist-panel' ? 'memberlist-panel' : '',
- })
- },
-
- _switchSettingPanel() {
- this.setData({
- panelName: this.data.panelName !== 'setting-panel' ? 'setting-panel' : '',
- })
- },
- _switchBGMPanel() {
- this.setData({
- panelName: this.data.panelName !== 'bgm-panel' ? 'bgm-panel' : '',
- })
- },
- _handleMaskerClick() {
- this.setData({
- panelName: '',
- })
- },
- _setPuserProperty(event) {
- console.log(TAG_NAME, '_setPuserProperty', event)
- const key = event.currentTarget.dataset.key
- const valueType = event.currentTarget.dataset.valueType
- let value = event.currentTarget.dataset.value
- const config = {}
- if (valueType === 'boolean') {
- value = value === 'true' ? true : false
- config[key] = !this.data.pusher[key]
- }
- if (valueType === 'number' && value.indexOf('|') > 0) {
- value = value.split('|')
-
- if ( this.data.pusher[key] === Number(value[0])) {
- config[key] = Number(value[1])
- } else {
- config[key] = Number(value[0])
- }
- }
- if (valueType === 'string' && value.indexOf('|') > 0) {
- value = value.split('|')
- if ( this.data.pusher[key] === value[0]) {
- config[key] = value[1]
- } else {
- config[key] = value[0]
- }
- }
- this._setPusherConfig(config)
- },
- _setPlayerProperty(event) {
- console.log(TAG_NAME, '_setPlayerProperty', event)
- const userID = event.currentTarget.dataset.userid
- const streamType = event.currentTarget.dataset.streamtype
- const key = event.currentTarget.dataset.key
- let value = event.currentTarget.dataset.value
- const stream = this.userController.getStream({
- userID: userID,
- streamType: streamType,
- })
- if (!stream) {
- return
- }
- const config = {}
- if (value === 'true') {
- value = true
- } else if (value === 'false') {
- value = false
- }
- if (typeof value === 'boolean') {
- config[key] = !stream[key]
- } else if (typeof value === 'string' && value.indexOf('|') > 0) {
- value = value.split('|')
- if (stream[key] === value[0]) {
- config[key] = value[1]
- } else {
- config[key] = value[0]
- }
- }
- console.log(TAG_NAME, '_setPlayerProperty', config)
- this._setPlayerConfig({ userID, streamType, config })
- },
- _changeProperty(event) {
- const propertyName = event.currentTarget.dataset.propertyName
- const newData = {}
- newData[propertyName] = event.detail.value
- this.setData(newData)
- const volume = newData[propertyName] / 100
- switch (propertyName) {
- case 'MICVolume':
- this.setMICVolume({ volume })
- break
- case 'BGMVolume':
- this.setBGMVolume({ volume })
- break
- }
- },
- _switchStreamType(event) {
- const userID = event.currentTarget.dataset.userid
- const streamType = event.currentTarget.dataset.streamtype
- const stream = this.userController.getStream({
- userID: userID,
- streamType: streamType,
- })
- if (stream && stream.streamType === 'main') {
- if (stream._definitionType === 'small') {
- this.subscribeRemoteVideo({ userID, streamType: 'main' })
- } else {
- this.subscribeRemoteVideo({ userID, streamType: 'small' })
- }
- }
- },
- _handleSnapshotClick(event) {
- wx.showToast({
- title: '开始截屏',
- icon: 'none',
- duration: 1000,
- })
- const userID = event.currentTarget.dataset.userid
- const streamType = event.currentTarget.dataset.streamtype
- this.snapshot({ userID, streamType })
- },
-
- _gridBindEvent() {
-
- this.on(EVENT.REMOTE_AUDIO_VOLUME_UPDATE, (event) => {
- const data = event.data
- const userID = data.currentTarget.dataset.userid
- const streamType = data.currentTarget.dataset.streamtype
- const volume = data.detail.volume
-
- const stream = this.userController.getStream({
- userID: userID,
- streamType: streamType === 'aux' ? 'main' : streamType,
- })
- if (stream) {
- stream.volume = volume
- }
- this.setData({
- streamList: this.data.streamList,
- visibleStreamList: this._filterVisibleStream(this.data.streamList, true),
- }, () => {
- })
- })
- this.on(EVENT.BGM_PLAY_PROGRESS, (event) => {
-
- const BGMProgress = event.data.detail.progress / event.data.detail.duration * 100
- this.setData({ BGMProgress })
- })
- this.on(EVENT.LOCAL_AUDIO_VOLUME_UPDATE, (event) => {
-
-
- const volume = event.data.detail.volume
-
- this._setPusherConfig({ volume }, true)
- })
- },
- _handleGridTouchStart(event) {
- touchX = event.changedTouches[0].clientX
- touchY = event.changedTouches[0].clientY
- },
- _handleGridTouchEnd(event) {
- const x = event.changedTouches[0].clientX
- const y = event.changedTouches[0].clientY
- if (x - touchX > 50 && Math.abs(y - touchY) < 50) {
-
- this._gridPagePrev()
- } else if (x - touchX < -50 && Math.abs(y - touchY) < 50) {
-
- this._gridPageNext()
- }
- },
- _gridPageToPrev(streamList) {
- const visibleStreamList = this._filterGridPageVisibleStream(streamList)
- if (this.data.gridPagePlaceholderStreamList.length === this.data.gridPlayerPerPage) {
- this.data.gridCurrentPage--
- this._gridPageToPrev(streamList)
- } else {
- return visibleStreamList
- }
- },
- _gridPageNext() {
- this.data.gridCurrentPage++
- if (this.data.gridCurrentPage > this.data.gridPageCount) {
- this.data.gridCurrentPage = 1
- }
- this._gridPageSetData()
- },
- _gridPagePrev() {
- this.data.gridCurrentPage--
- if (this.data.gridCurrentPage < 1) {
- this.data.gridCurrentPage = this.data.gridPageCount
- }
- this._gridPageSetData()
- },
- _gridPageSetData() {
- this._gridShowPageTips()
- const visibleStreamList = this._filterVisibleStream(this.data.streamList)
- this.setData({
- gridCurrentPage: this.data.gridCurrentPage,
- gridPageCount: this.data.gridPageCount,
- visibleStreamList: visibleStreamList,
- streamList: this.data.streamList,
- gridPagePlaceholderStreamList: this.data.gridPagePlaceholderStreamList,
- }, () => {
- })
- },
- _gridShowPageTips(event) {
- if (this.data.gridPageCount < 2) {
- return
- }
- console.log(TAG_NAME, '_gridShowPageTips', this.data)
- if (this.data.hasGridPageTipsShow) {
- clearTimeout(this.data.hasGridPageTipsShow)
- }
- this.animate('.pages-container', [
- { opacity: 1 },
- ], 100, ()=>{
- })
- this.data.hasGridPageTipsShow = setTimeout(()=>{
- this.animate('.pages-container', [
- { opacity: 1 },
- { opacity: 0.3 },
- ], 600, ()=>{
- })
- }, 3000)
- },
- _toggleFullscreen(event) {
- console.log(TAG_NAME, '_toggleFullscreen', event)
- const userID = event.currentTarget.dataset.userID
- const streamType = event.currentTarget.dataset.streamType
- if (this._isFullscreen) {
- this.exitFullscreen({ userID, streamType }).then(() => {
- this._isFullscreen = false
- }).catch(() => {
- })
- } else {
-
- const direction = 0
-
-
-
-
-
- this.enterFullscreen({ userID, streamType, direction }).then(() => {
- this._isFullscreen = true
- }).catch(() => {
- })
- }
- },
- _toggleMoreMenu() {
- this.setData({
- isShowMoreMenu: !this.data.isShowMoreMenu,
- })
- },
- _toggleIMPanel() {
- if (!this.data.enableIM) {
- wx.showToast({
- icon: 'none',
- title: '当前没有开启IM功能,请设置 enableIM:true',
- })
- }
- this.setData({
- showIMPanel: !this.data.showIMPanel,
- })
- },
- _handleBGMOperation(event) {
- const operationName = event.currentTarget.dataset.operationName
- if (this[operationName]) {
- this[operationName]({ url: 'https://trtc-1252463788.cos.ap-guangzhou.myqcloud.com/web/assets/bgm-test.mp3' })
- }
- },
- _selectBeautyStyle: function(event) {
- console.log(TAG_NAME, '_selectBeautyStyle', event)
-
- const value = event.detail.value
- this.setData({
-
- beautyStyle: value,
- }, () => {
- this._setPusherConfig({
- beautyLevel: value === 'close' ? 0 : 9,
- beautyStyle: value === 'close' ? 'smooth' : value,
- })
- })
- },
- _selectFilter: function(event) {
- console.log(TAG_NAME, '_selectFilter', event)
- const index = parseInt(event.detail.value)
- this.setData({ filterIndex: index }, () => {
- this._setPusherConfig({
- filter: this.data.filterArray[index].value,
- })
- })
- },
- _selectAudioReverbType: function(event) {
- console.log(TAG_NAME, '_selectAudioReverbType', event)
- const audioReverbType = parseInt(event.detail.value)
- this._setPusherConfig({ audioReverbType })
- },
- _sendIMMessage(event) {
- console.log(TAG_NAME, '_sendIMMessage', event)
- if (!this.data.messageContent) {
- return
- }
- const roomID = this.data.config.roomID
- const message = this.data.messageContent
- const userID = this.data.config.userID
- this.sendGroupTextMessage({ roomID, message })
-
- this._pushMessageList({
- name: userID,
- message: message,
- })
- this.setData({
- messageContent: '',
- })
- },
- _inputIMMessage(event) {
-
- this.setData({
- messageContent: event.detail.value,
- })
- },
- _pushMessageList(params) {
- if (this.data.messageList.length === this.data.maxMessageListLength) {
- this.data.messageList.shift()
- }
- this.data.messageList.push(params)
- this.setData({
- messageList: this.data.messageList,
- messageListScrollTop: this.data.messageList.length * 100,
- }, () => {
- })
- },
- },
- })
|