trtc-room.vue 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663
  1. <template>
  2. <view>
  3. <view class="trtc-room-container">
  4. <view v-if="template === '1v1'">
  5. <view data-type="template" data-is="1v1" data-attr="pusher, streamList, debug">
  6. <view class="template-1v1">
  7. <view
  8. v-for="(item, streamID) in streamList"
  9. :key="streamID"
  10. v-if="item.src && (item.hasVideo || item.hasAudio)"
  11. :class="'view-container player-container ' + (item.isVisible ? '' : 'none')"
  12. style="display:none"
  13. >
  14. <live-player
  15. class="player"
  16. :data-userid="item.userID"
  17. :data-streamid="item.streamID"
  18. :data-streamtype="item.streamType"
  19. :src="item.src"
  20. mode="RTC"
  21. :autoplay="item.autoplay"
  22. :mute-audio="item.muteAudio"
  23. :mute-video="item.muteVideo"
  24. :orientation="item.orientation"
  25. :object-fit="item.objectFit"
  26. :background-mute="item.enableBackgroundMute"
  27. :min-cache="item.minCache"
  28. :max-cache="item.maxCache"
  29. :sound-mode="item.soundMode"
  30. :enable-recv-message="item.enableRecvMessage"
  31. :auto-pause-if-navigate="item.autoPauseIfNavigate"
  32. :auto-pause-if-open-native="item.autoPauseIfOpenNative"
  33. :debug="debug"
  34. @statechange="playerStateChangeFun"
  35. @fullscreenchange="playerFullscreenChangeFun"
  36. @netstatus="playerNetStatusFun"
  37. @audiovolumenotify="playerAudioVolumeNotifyFun"
  38. :idAttr="item.streamID"
  39. ></live-player>
  40. </view>
  41. <view :class="'view-container pusher-container ' + (pusher.isVisible ? '' : '') + ' ' + (JSON.stringify(streamList) == '[]' ? 'fullscreen' : 'fullscreen')">
  42. <live-pusher
  43. class="pusher"
  44. :url="pusher.url"
  45. :mode="pusher.mode"
  46. :autopush="pusher.autopush"
  47. :enable-camera="pusher.enableCamera"
  48. :enable-mic="pusher.enableMic"
  49. :enable-agc="pusher.enableAgc"
  50. :enable-ans="pusher.enableAns"
  51. :enable-ear-monitor="pusher.enableEarMonitor"
  52. :auto-focus="pusher.enableAutoFocus"
  53. :zoom="pusher.enableZoom"
  54. :min-bitrate="pusher.minBitrate"
  55. :max-bitrate="pusher.maxBitrate"
  56. :video-width="pusher.videoWidth"
  57. :video-height="pusher.videoHeight"
  58. :beauty="pusher.beautyLevel"
  59. :whiteness="pusher.whitenessLevel"
  60. :orientation="pusher.videoOrientation"
  61. :aspect="pusher.videoAspect"
  62. :device-position="pusher.frontCamera"
  63. :remote-mirror="pusher.enableRemoteMirror"
  64. :local-mirror="pusher.localMirror"
  65. :background-mute="pusher.enableBackgroundMute"
  66. :audio-quality="pusher.audioQuality"
  67. :audio-volume-type="pusher.audioVolumeType"
  68. :audio-reverb-type="pusher.audioReverbType"
  69. :waiting-image="pusher.waitingImage"
  70. :debug="debug"
  71. @statechange="pusherStateChangeHandlerFun"
  72. @netstatus="pusherNetStatusHandlerFun"
  73. @error="pusherErrorHandlerFun"
  74. @bgmstart="pusherBGMStartHandlerFun"
  75. @bgmprogress="pusherBGMProgressHandlerFun"
  76. @bgmcomplete="pusherBGMCompleteHandlerFun"
  77. ></live-pusher>
  78. <view class="loading" v-if="streamList.length === 0">
  79. <!-- <view class="loading-img"><image src="../../static/components/trtc-room/static/loading.png" class="rotate-img"></image></view> -->
  80. <view class="loading-text">等待接听中...</view>
  81. </view>
  82. </view>
  83. <view class="handle-btns">
  84. <view class="btn-normal" @tap="toggleAudioFun">
  85. <image
  86. :src="pusher.enableMic ? '../../static/components/trtc-room/static/audio-true.png' : '../../static/components/trtc-room/static/audio-false.png'"
  87. ></image>
  88. </view>
  89. <!-- <view class="btn-normal" @tap="switchCamera"><image src="../../static/components/trtc-room/static/switch.png"></image></view> -->
  90. <!-- <view class="btn-normal" bindtap="toggleVideoFun">
  91. <image src="{{pusher.enableCamera? '../../static/components/trtc-room/static/camera-true.png': '../../static/components/trtc-room/static/camera-false.png'}} "></image>
  92. </view> -->
  93. <view class="btn-normal" @tap="toggleSoundModeFun">
  94. <image
  95. :src="
  96. streamList[0].soundMode === 'ear'
  97. ? '../../static/components/trtc-room/static/phone.png'
  98. : '../../static/components/trtc-room/static/speaker-true.png'
  99. "
  100. ></image>
  101. </view>
  102. </view>
  103. <view class="bottom-btns">
  104. <!-- <view class="btn-hangup" @tap="hangUpFun"><image src="../../static/components/trtc-room/static/hangup.png"></image></view> -->
  105. </view>
  106. </view>
  107. </view>
  108. </view>
  109. <view v-if="template === 'grid'">
  110. <view data-type="template" data-is="grid" data-attr="pusher, streamList, debug, panelName">
  111. <view :class="'template-grid ' + (streamList.length < 6 ? 'stream-' + streamList.length : 'stream-5')">
  112. <view
  113. v-for="(item, streamID) in streamList"
  114. :key="streamID"
  115. v-if="item.src && (item.hasVideo || item.hasAudio)"
  116. :class="'view-container player-container ' + (item.isVisible ? '' : 'none')"
  117. :data-userid="item.userID"
  118. :data-streamtype="item.streamType"
  119. @tap="doubleTabToggleFullscreenFun"
  120. >
  121. <live-player
  122. class="player"
  123. :data-userid="item.userID"
  124. :data-streamid="item.streamID"
  125. :data-streamtype="item.streamType"
  126. :src="item.src"
  127. mode="RTC"
  128. :autoplay="item.autoplay"
  129. :mute-audio="item.muteAudio"
  130. :mute-video="item.muteVideo"
  131. :orientation="item.orientation"
  132. :object-fit="item.objectFit"
  133. :background-mute="item.enableBackgroundMute"
  134. :min-cache="item.minCache"
  135. :max-cache="item.maxCache"
  136. :sound-mode="item.soundMode"
  137. :enable-recv-message="item.enableRecvMessage"
  138. :auto-pause-if-navigate="item.autoPauseIfNavigate"
  139. :auto-pause-if-open-native="item.autoPauseIfOpenNative"
  140. :debug="debug"
  141. @statechange="playerStateChangeFun"
  142. @fullscreenchange="playerFullscreenChangeFun"
  143. @netstatus="playerNetStatusFun"
  144. @audiovolumenotify="playerAudioVolumeNotifyFun"
  145. :idAttr="item.streamID"
  146. ></live-player>
  147. <view class="operation-bar">
  148. <view class="btn-normal" @tap="handleSubscribeRemoteAudioFun" :data-user-i-d="item.userID" :data-stream-type="item.streamType">
  149. <image
  150. :src="
  151. item.muteAudio
  152. ? '../../static/components/trtc-room/static/speaker-false.png'
  153. : '../../static/components/trtc-room/static/speaker-true.png'
  154. "
  155. ></image>
  156. </view>
  157. <view class="btn-normal" @tap="handleSubscribeRemoteVideoFun" :data-user-i-d="item.userID" :data-stream-type="item.streamType">
  158. <image
  159. :src="
  160. item.muteVideo
  161. ? '../../static/components/trtc-room/static/camera-false.png'
  162. : '../../static/components/trtc-room/static/camera-true.png'
  163. "
  164. ></image>
  165. </view>
  166. <view class="btn-normal" @tap="toggleFullscreenFun" :data-user-i-d="item.userID" :data-stream-type="item.streamType">
  167. <!-- <image src="../../static/components/trtc-room/static/fullscreen.png"></image> -->
  168. </view>
  169. </view>
  170. <progress class="volume-progress" :percent="item.volume" stroke-width="4"></progress>
  171. </view>
  172. <view :class="'view-container pusher-container ' + (pusher.isVisible ? '' : 'none')">
  173. <live-pusher
  174. class="pusher"
  175. :url="pusher.url"
  176. :mode="pusher.mode"
  177. :autopush="pusher.autopush"
  178. :enable-camera="pusher.enableCamera"
  179. :enable-mic="pusher.enableMic"
  180. :enable-agc="pusher.enableAgc"
  181. :enable-ans="pusher.enableAns"
  182. :enable-ear-monitor="pusher.enableEarMonitor"
  183. :auto-focus="pusher.enableAutoFocus"
  184. :zoom="pusher.enableZoom"
  185. :min-bitrate="pusher.minBitrate"
  186. :max-bitrate="pusher.maxBitrate"
  187. :video-width="pusher.videoWidth"
  188. :video-height="pusher.videoHeight"
  189. :beauty="pusher.beautyLevel"
  190. :whiteness="pusher.whitenessLevel"
  191. :orientation="pusher.videoOrientation"
  192. :aspect="pusher.videoAspect"
  193. :device-position="pusher.frontCamera"
  194. :remote-mirror="pusher.enableRemoteMirror"
  195. :local-mirror="pusher.localMirror"
  196. :background-mute="pusher.enableBackgroundMute"
  197. :audio-quality="pusher.audioQuality"
  198. :audio-volume-type="pusher.audioVolumeType"
  199. :audio-reverb-type="pusher.audioReverbType"
  200. :waiting-image="pusher.waitingImage"
  201. :debug="debug"
  202. @statechange="pusherStateChangeHandlerFun"
  203. @netstatus="pusherNetStatusHandlerFun"
  204. @error="pusherErrorHandlerFun"
  205. @bgmstart="pusherBGMStartHandlerFun"
  206. @bgmprogress="pusherBGMProgressHandlerFun"
  207. @bgmcomplete="pusherBGMCompleteHandlerFun"
  208. ></live-pusher>
  209. <view class="operation-bar">
  210. <view class="btn-normal" @tap="switchMemberListPanelFun"><i<!-- mage src="../../static/components/trtc-room/static/list.png"></image></view>
  211. <view class="btn-normal" @tap="switchSettingPanelFun"><image src="../../static/components/trtc-room/static/setting.png"></image></view>
  212. <view class="btn-normal btn-hangup" @tap="hangUpFun"><image --> src="../../static/components/trtc-room/static/hangup.png"></image></view>
  213. </view>
  214. </view>
  215. <view :class="'panel memberlist-panel ' + (panelName === 'memberlist-panel' ? '' : 'none')">
  216. <view @tap="handleMaskerClickFun" class="close-btn">X</view>
  217. <view class="panel-header">成员列表</view>
  218. <view class="panel-body">
  219. <view class="panel-tips" v-if="streamList.length === 0">暂无成员</view>
  220. <scroll-view class="scroll-container" scroll-y="true">
  221. <view class="member-item" v-for="(item, streamID) in streamList" :key="streamID">
  222. <view class="member-id">{{ item.userID }}</view>
  223. <view class="member-btns">
  224. <button
  225. class="btn"
  226. hover-class="btn-hover"
  227. :data-userid="item.userID"
  228. :data-streamtype="item.streamType"
  229. data-key="objectFit"
  230. data-value="fillCrop|contain"
  231. @tap="setPlayerPropertyFun"
  232. >
  233. {{ item.objectFit === 'fillCrop' ? '填充' : '适应' }}
  234. </button>
  235. <button
  236. class="btn"
  237. hover-class="btn-hover"
  238. :data-userid="item.userID"
  239. :data-streamtype="item.streamType"
  240. data-key="orientation"
  241. data-value="vertical|horizontal"
  242. @tap="setPlayerPropertyFun"
  243. >
  244. {{ item.orientation === 'vertical' ? '竖屏' : '横屏' }}
  245. </button>
  246. <button
  247. class="btn"
  248. hover-class="btn-hover"
  249. :data-userid="item.userID"
  250. :data-streamtype="item.streamType"
  251. @tap="switchStreamTypeFun"
  252. v-if="item.streamType === 'main'"
  253. >
  254. {{ item._definitionType === 'small' ? '小画面' : '主画面' }}
  255. </button>
  256. <button class="btn" hover-class="btn-hover" :data-userid="item.userID" :data-streamtype="item.streamType" @tap="handleSnapshotClickFun">
  257. 截屏
  258. </button>
  259. </view>
  260. </view>
  261. </scroll-view>
  262. </view>
  263. </view>
  264. <view :class="'panel setting-panel ' + (panelName === 'setting-panel' ? '' : 'none')">
  265. <view @tap="handleMaskerClickFun" class="close-btn">X</view>
  266. <view class="panel-header">推流设置</view>
  267. <view class="panel-body">
  268. <scroll-view class="scroll-container" scroll-y="true">
  269. <view class="setting-option">
  270. <view class="label">启用摄像头</view>
  271. <view class="btn-normal" @tap="toggleVideoFun">
  272. <image
  273. :src="
  274. pusher.enableCamera
  275. ? '../../static/components/trtc-room/static/camera-true.png'
  276. : '../../static/components/trtc-room/static/camera-false.png'
  277. "
  278. ></image>
  279. </view>
  280. </view>
  281. <view class="setting-option">
  282. <view class="label">启用麦克风</view>
  283. <view class="btn-normal" @tap="toggleAudioFun">
  284. <image
  285. :src="
  286. pusher.enableMic
  287. ? '../../static/components/trtc-room/static/audio-true.png'
  288. : '../../static/components/trtc-room/static/audio-false.png'
  289. "
  290. ></image>
  291. </view>
  292. </view>
  293. <view class="setting-option">
  294. <view class="label">切换摄像头</view>
  295. <!-- <view class="btn-normal" @tap="switchCamera"><image src="../../static/components/trtc-room/static/switch.png"></image></view> -->
  296. </view>
  297. <view class="setting-option">
  298. <view class="label">开启美颜</view>
  299. <switch
  300. color="#006eff"
  301. :checked="pusher.beautyLevel == 9 ? true : false"
  302. data-key="beautyLevel"
  303. data-value="9|0"
  304. @change="setPuserPropertyFun"
  305. ></switch>
  306. </view>
  307. <view class="setting-option">
  308. <view class="label">开启AGC</view>
  309. <switch color="#006eff" :checked="pusher.enableAgc" data-key="enableAgc" data-value="true" @change="setPuserPropertyFun"></switch>
  310. </view>
  311. <view class="setting-option">
  312. <view class="label">开启ANS</view>
  313. <switch color="#006eff" :checked="pusher.enableAns" data-key="enableAns" data-value="true" @change="setPuserPropertyFun"></switch>
  314. </view>
  315. <view class="setting-option">
  316. <view class="label">开启横屏推流</view>
  317. <switch
  318. color="#006eff"
  319. :checked="pusher.videoOrientation === 'vertical' ? false : true"
  320. data-key="videoOrientation"
  321. data-value="horizontal|vertical"
  322. @change="setPuserPropertyFun"
  323. ></switch>
  324. </view>
  325. </scroll-view>
  326. </view>
  327. </view>
  328. <view :class="'masker ' + (panelName == '' ? 'none' : '')" @tap="handleMaskerClickFun"></view>
  329. </view>
  330. </view>
  331. </view>
  332. <view v-if="template === 'custom'">
  333. <view data-type="template" data-is="custom" data-attr="pusher, streamList, debug">
  334. <view class="template-custom">
  335. <view class="players-container">
  336. <view
  337. v-for="(item, streamID) in streamList"
  338. :key="streamID"
  339. v-if="item.src && (item.hasVideo || item.hasAudio)"
  340. :class="'view-container player-container ' + (item.isVisible ? '' : 'none')"
  341. :style="'left:' + item.xAxis + 'top:' + item.yAxis + 'width:' + item.width + 'height:' + item.height + 'zindex:' + item.zindex + ''"
  342. >
  343. <live-player
  344. class="player"
  345. :data-userid="item.userID"
  346. :data-streamid="item.streamID"
  347. :data-streamtype="item.streamType"
  348. :src="item.src"
  349. :mode="item.mode"
  350. :autoplay="item.autoplay"
  351. :mute-audio="item.muteAudio"
  352. :mute-video="item.muteVideo"
  353. :orientation="item.orientation"
  354. :object-fit="item.objectFit"
  355. :background-mute="item.enableBackgroundMute"
  356. :min-cache="item.minCache"
  357. :max-cache="item.maxCache"
  358. :sound-mode="item.soundMode"
  359. :enable-recv-message="item.enableRecvMessage"
  360. :auto-pause-if-navigate="item.autoPauseIfNavigate"
  361. :auto-pause-if-open-native="item.autoPauseIfOpenNative"
  362. :debug="debug"
  363. @statechange="playerStateChangeFun"
  364. @fullscreenchange="playerFullscreenChangeFun"
  365. @netstatus="playerNetStatusFun"
  366. @audiovolumenotify="playerAudioVolumeNotifyFun"
  367. :idAttr="item.streamID"
  368. ></live-player>
  369. </view>
  370. </view>
  371. <view
  372. :class="'view-container pusher-container ' + (pusher.isVisible ? '' : 'none')"
  373. :style="'left:' + pusher.xAxis + 'top:' + pusher.yAxis + 'width:' + pusher.width + 'height:' + pusher.height + 'zindex:' + pusher.zindex + ''"
  374. >
  375. <live-pusher
  376. class="pusher"
  377. :url="pusher.url"
  378. :mode="pusher.mode"
  379. :autopush="pusher.autopush"
  380. :enable-camera="pusher.enableCamera"
  381. :enable-mic="pusher.enableMic"
  382. :enable-agc="pusher.enableAgc"
  383. :enable-ans="pusher.enableAns"
  384. :enable-ear-monitor="pusher.enableEarMonitor"
  385. :auto-focus="pusher.enableAutoFocus"
  386. :zoom="pusher.enableZoom"
  387. :min-bitrate="pusher.minBitrate"
  388. :max-bitrate="pusher.maxBitrate"
  389. :video-width="pusher.videoWidth"
  390. :video-height="pusher.videoHeight"
  391. :beauty="pusher.beautyLevel"
  392. :whiteness="pusher.whitenessLevel"
  393. :orientation="pusher.videoOrientation"
  394. :aspect="pusher.videoAspect"
  395. :device-position="pusher.frontCamera"
  396. :remote-mirror="pusher.enableRemoteMirror"
  397. :local-mirror="pusher.localMirror"
  398. :background-mute="pusher.enableBackgroundMute"
  399. :audio-quality="pusher.audioQuality"
  400. :audio-volume-type="pusher.audioVolumeType"
  401. :audio-reverb-type="pusher.audioReverbType"
  402. :waiting-image="pusher.waitingImage"
  403. :debug="debug"
  404. @statechange="pusherStateChangeHandlerFun"
  405. @netstatus="pusherNetStatusHandlerFun"
  406. @error="pusherErrorHandlerFun"
  407. @bgmstart="pusherBGMStartHandlerFun"
  408. @bgmprogress="pusherBGMProgressHandlerFun"
  409. @bgmcomplete="pusherBGMCompleteHandlerFun"
  410. ></live-pusher>
  411. </view>
  412. </view>
  413. </view>
  414. </view>
  415. <view :class="'debug-info-btn ' + (debugMode && !debugPanel ? '' : 'none')"><button @tap="debugTogglePanelFun" hover-class="button-hover">Debug</button></view>
  416. <view :class="'debug-info ' + (debugMode && debugPanel ? '' : 'none')">
  417. <view @tap="debugTogglePanelFun" class="close-btn">X</view>
  418. <view>appVersion: {{ appVersion }}</view>
  419. <view>libVersion: {{ libVersion }}</view>
  420. <view>template: {{ template }}</view>
  421. <view>
  422. debug:
  423. <button :class="debug ? '' : 'false'" @tap="debugToggleVideoDebugFun" hover-class="button-hover">{{ debug }}</button>
  424. </view>
  425. <view>userID: {{ pusher.userID }}</view>
  426. <view>roomID: {{ pusher.roomID }}</view>
  427. <view>
  428. camera:
  429. <button :class="pusher.enableCamera ? '' : 'false'" @tap="toggleVideoFun" hover-class="button-hover">{{ pusher.enableCamera }}</button>
  430. </view>
  431. <view>
  432. mic:
  433. <button :class="pusher.enableMic ? '' : 'false'" @tap="toggleAudioFun" hover-class="button-hover">{{ pusher.enableMic }}</button>
  434. </view>
  435. <view>
  436. switch camera:
  437. <button @tap="switchCamera" hover-class="button-hover">{{ cameraPosition || pusher.frontCamera }}</button>
  438. </view>
  439. <view>
  440. Room:
  441. <button @tap="debugEnterRoomFun" hover-class="button-hover">Enter</button>
  442. <button @tap="debugExitRoomFun" hover-class="button-hover">Exit</button>
  443. <button @tap="debugGoBackFun" hover-class="button-hover">Go back</button>
  444. </view>
  445. <view>user count: {{ userList.length }}</view>
  446. <view v-for="(item, userID) in userList" :key="userID">
  447. {{ item.userID }}|mainV:{{ item.hasMainVideo || false }}|mainA:{{ item.hasMainAudio || false }}|auxV:{{ item.hasAuxVideo || false }}
  448. </view>
  449. <view>stream count: {{ streamList.length }}</view>
  450. <view v-for="(item, streamID) in streamList" :key="streamID">
  451. {{ item.userID }}|{{ item.streamType }}| SubV:
  452. <button
  453. :class="!item.muteVideo ? '' : 'false'"
  454. @tap="debugToggleRemoteVideoFun"
  455. hover-class="button-hover"
  456. :data-user-i-d="item.userID"
  457. :data-stream-type="item.streamType"
  458. >
  459. {{ !item.muteVideo }}
  460. </button>
  461. | SubA:
  462. <button
  463. :class="!item.muteAudio ? '' : 'false'"
  464. @tap="debugToggleRemoteAudioFun"
  465. hover-class="button-hover"
  466. :data-user-i-d="item.userID"
  467. :data-stream-type="item.streamType"
  468. >
  469. {{ !item.muteAudio }}
  470. </button>
  471. </view>
  472. </view>
  473. </view>
  474. </view>
  475. </template>
  476. <script>
  477. import { setData } from '@/pagesMedia/debug/GenerateTestUserSig';
  478. import UserController from './controller/user-controller';
  479. import Pusher from './model/pusher';
  480. import { EVENT } from './common/constants';
  481. import TIM from './common/tim-wx';
  482. import Event from './utils/event';
  483. import * as ENV from './utils/environment';
  484. const TAG_NAME = 'TRTC-ROOM';
  485. const IM_GROUP_TYPE = TIM.TYPES.GRP_CHATROOM // TIM.TYPES.GRP_CHATROOM 体验版IM无数量限制,成员20个, TIM.TYPES.GRP_AVCHATROOM IM体验版最多10个,升级后无限制
  486. export default {
  487. data() {
  488. return {
  489. pusher: null,
  490. // debugMode: false, // 是否开启调试模式
  491. debugPanel: true,
  492. // 是否打开组件调试面板
  493. debug: false,
  494. // 是否打开player pusher 的调试信息
  495. streamList: [],
  496. // 用于渲染player列表,存储stram
  497. userList: [],
  498. // 扁平化的数据用来返回给用户
  499. template: '',
  500. // 不能设置默认值,当默认值和传入组件的值不一致时,iOS渲染失败
  501. cameraPosition: '',
  502. panelName: '',
  503. // 控制面板名称,包括 setting-panel memberlist-panel
  504. localVolume: 0,
  505. remoteVolumeList: [],
  506. appVersion: ENV.APP_VERSION,
  507. libVersion: ENV.LIB_VERSION,
  508. debugMode: '',
  509. enableIM: true, // 用于组件内渲染
  510. showIMPanel: false,
  511. exitIMThrottle: false,
  512. messageContent: '',
  513. messageList: [], // 仅保留10条消息
  514. maxMessageListLength: 10,
  515. messageListScrollTop: 0,
  516. };
  517. },
  518. components: {},
  519. props: {
  520. // 必要的初始化参数
  521. config: {
  522. type: Object,
  523. default: () => ({
  524. sdkAppID: '',
  525. userID: '',
  526. userSig: '',
  527. template: '',
  528. debugMode: '',
  529. enableIM: true, // 用于组件内渲染
  530. showIMPanel: false,
  531. exitIMThrottle: false,
  532. messageContent: '',
  533. messageList: [], // 仅保留10条消息
  534. maxMessageListLength: 10,
  535. messageListScrollTop: 0
  536. })
  537. }
  538. },
  539. watch: {
  540. streamList(newVal, oldVal){
  541. console.log('streamList::::::::',newVal, oldVal)
  542. this.streamList = newVal
  543. },
  544. config: {
  545. handler: function(newVal, oldVal) {
  546. console.log('watch config');
  547. this.propertyObserverFun({
  548. name: 'config',
  549. newVal,
  550. oldVal
  551. });
  552. },
  553. deep: true
  554. }
  555. },
  556. created: function() {
  557. // 在组件实例刚刚被创建时执行
  558. console.log(TAG_NAME, 'created', ENV);
  559. },
  560. beforeMount: function() {
  561. // 在组件实例进入页面节点树时执行
  562. console.log(TAG_NAME, 'attached');
  563. this.initFun();
  564. },
  565. mounted: function() {
  566. // 在组件在视图层布局完成后执行
  567. console.log(TAG_NAME, 'ready');
  568. },
  569. destroyed: function() {
  570. // 在组件实例被从页面节点树移除时执行
  571. console.log(TAG_NAME, 'detached'); // 停止所有拉流,并重置数据
  572. this.exitRoom();
  573. },
  574. error: function(error) {
  575. // 每当组件方法抛出错误时执行
  576. console.log(TAG_NAME, 'error', error);
  577. },
  578. onPageShow: function() {
  579. // 组件所在的页面被展示时执行
  580. console.log(TAG_NAME, 'show status:', this.status);
  581. if (this.status.isPending) {
  582. // 经历了 5000 挂起事件
  583. this.status.isPending = false;
  584. }
  585. if (this.status.isPush) {
  586. // 小程序hide - show 有一定概率本地黑屏或静止,远端正常,或者远端和本地同时黑屏或静止,需要手动启动预览,非必现
  587. // this.data.pusher.getPusherContext().startPreview()
  588. // this.data.pusher.getPusherContext().resume()
  589. }
  590. },
  591. onPageHide: function() {
  592. // 组件所在的页面被隐藏时执行
  593. console.log(TAG_NAME, 'hide');
  594. },
  595. onPageResize: function(size) {
  596. // 组件所在的页面尺寸变化时执行
  597. console.log(TAG_NAME, 'resize', size);
  598. },
  599. methods: {
  600. setData,
  601. /**
  602. * 初始化各项参数和用户控制模块,在组件实例触发 attached 时调用,此时不建议对View进行变更渲染(调用setData方法)
  603. */
  604. initFun: function() {
  605. console.log(TAG_NAME, '_init');
  606. this.userController = new UserController(this);
  607. this._emitter = new Event();
  608. this.EVENT = EVENT;
  609. this.initStatusFun();
  610. this.bindEventFun();
  611. this.bindEventGridFun();
  612. console.log(TAG_NAME, '_init success component:', this);
  613. },
  614. /**
  615. * 进房
  616. * @param {Object} params 必传 roomID 取值范围 1 ~ 4294967295
  617. * @returns {Promise}
  618. */
  619. enterRoom: function(params) {
  620. return new Promise((resolve, reject) => {
  621. console.log(TAG_NAME, 'enterRoom');
  622. console.log(TAG_NAME, 'params', params);
  623. console.log(TAG_NAME, 'config', this.config);
  624. console.log(TAG_NAME, 'pusher', this.pusher); // 1. 补齐进房参数,校验必要参数是否齐全
  625. console.log('进房......', params, this.config, this.pusher);
  626. if (this.config.enableIM && this.config.sdkAppID) {
  627. this._initIM(this.config,params.roomID)
  628. // this._loginIM({ ...this.config, roomID: params.roomID })
  629. }
  630. if (params) {
  631. Object.assign(this.pusher, params);
  632. Object.assign(this.config, params);
  633. }
  634. if (!this.checkParamFun(this.config)) {
  635. reject(new Error('缺少必要参数'));
  636. return;
  637. } // 2. 根据参数拼接 push url,赋值给 live-pusher,
  638. this.getPushUrlFun(this.config)
  639. .then(pushUrl => {
  640. this.pusher.url = pushUrl;
  641. this.setData(
  642. {
  643. pusher: this.pusher
  644. },
  645. () => {
  646. console.log(TAG_NAME, 'enterRoom success', this.pusher); // view 渲染成功回调后,开始推流
  647. this.pusher.getPusherContext().start();
  648. this.status.isPush = true;
  649. resolve();
  650. }
  651. );
  652. })
  653. .catch(res => {
  654. // 获取 room sig 失败, 进房失败需要通过 pusher state 事件通知
  655. console.error(TAG_NAME, 'enterRoom fail', res);
  656. reject(res);
  657. });
  658. });
  659. },
  660. /**
  661. * 退房,停止推流和拉流,并重置数据
  662. * @returns {Promise}
  663. */
  664. exitRoom: function() {
  665. return new Promise((resolve, reject) => {
  666. console.log(TAG_NAME, 'exitRoom');
  667. this.pusher.reset();
  668. this.status.isPush = false;
  669. const result = this.userController.reset();
  670. this._exitIM();
  671. this.setData(
  672. {
  673. pusher: this.pusher,
  674. userList: result.userList,
  675. streamList: result.streamList
  676. },
  677. () => {
  678. // 在销毁页面时调用,不会走到这里
  679. resolve({
  680. userList: this.userList,
  681. streamList: this.streamList
  682. });
  683. console.log(TAG_NAME, 'exitRoom success', this.pusher, this.streamList, this.userList);
  684. }
  685. );
  686. });
  687. },
  688. /**
  689. * 开启摄像头
  690. * @returns {Promise}
  691. */
  692. publishLocalVideo: function() {
  693. // 设置 pusher enableCamera
  694. console.log(TAG_NAME, 'publishLocalVideo 开启摄像头');
  695. return this.setPusherConfigFun({
  696. enableCamera: true
  697. });
  698. },
  699. /**
  700. * 关闭摄像头
  701. * @returns {Promise}
  702. */
  703. unpublishLocalVideo: function() {
  704. // 设置 pusher enableCamera
  705. console.log(TAG_NAME, 'unpublshLocalVideo 关闭摄像头');
  706. return this.setPusherConfigFun({
  707. enableCamera: false
  708. });
  709. },
  710. /**
  711. * 开启麦克风
  712. * @returns {Promise}
  713. */
  714. publishLocalAudio: function() {
  715. // 设置 pusher enableCamera
  716. console.log(TAG_NAME, 'publishLocalAudio 开启麦克风');
  717. return this.setPusherConfigFun({
  718. enableMic: true
  719. });
  720. },
  721. /**
  722. * 关闭麦克风
  723. * @returns {Promise}
  724. */
  725. unpublishLocalAudio: function() {
  726. // 设置 pusher enableCamera
  727. console.log(TAG_NAME, 'unpublshLocalAudio 关闭麦克风');
  728. return this.setPusherConfigFun({
  729. enableMic: false
  730. });
  731. },
  732. /**
  733. * 订阅远端视频 主流 小画面 辅流
  734. * @param {Object} params {userID,streamType} streamType 传入 small 时修改对应的主流url的 streamtype 参数为small
  735. * @returns {Promise}
  736. */
  737. subscribeRemoteVideo(params) {
  738. console.log(TAG_NAME, 'subscribeRemoteVideo', params); // 设置指定 user streamType 的 muteVideo 为 false
  739. const config = {
  740. muteVideo: false
  741. }; // 本地数据结构里的 streamType 只支持 main 和 aux ,订阅small 也是对main进行处理
  742. const streamType = params.streamType === 'small' ? 'main' : params.streamType;
  743. if (params.streamType === 'small' || params.streamType === 'main') {
  744. const stream = this.userController.getStream({
  745. userID: params.userID,
  746. streamType: streamType
  747. });
  748. if (stream && stream.streamType === 'main') {
  749. console.log(TAG_NAME, 'subscribeRemoteVideo switch small', stream.src);
  750. if (params.streamType === 'small') {
  751. config.src = stream.src.replace('main', 'small');
  752. config._definitionType = 'small'; // 用于设置面板的渲染
  753. } else if (params.streamType === 'main') {
  754. stream.src = stream.src.replace('small', 'main');
  755. config._definitionType = 'main';
  756. }
  757. console.log(TAG_NAME, 'subscribeRemoteVideo', stream.src);
  758. }
  759. }
  760. return this.setPlayerConfigFun({
  761. userID: params.userID,
  762. streamType: streamType,
  763. config: config
  764. });
  765. },
  766. /**
  767. * 取消订阅远端视频
  768. * @param {Object} params {userID,streamType}
  769. * @returns {Promise}
  770. */
  771. unsubscribeRemoteVideo(params) {
  772. console.log(TAG_NAME, 'unsubscribeRemoteVideo', params); // 设置指定 user streamType 的 muteVideo 为 true
  773. return this.setPlayerConfigFun({
  774. userID: params.userID,
  775. streamType: params.streamType,
  776. config: {
  777. muteVideo: true
  778. }
  779. });
  780. },
  781. /**
  782. * 订阅远端音频
  783. * @param {Object} params userID 用户ID
  784. * @returns {Promise}
  785. */
  786. subscribeRemoteAudio(params) {
  787. console.log(TAG_NAME, 'subscribeRemoteAudio', params);
  788. return this.setPlayerConfigFun({
  789. userID: params.userID,
  790. streamType: 'main',
  791. config: {
  792. muteAudio: false
  793. }
  794. });
  795. },
  796. /**
  797. * 取消订阅远端音频
  798. * @param {Object} params userID 用户ID
  799. * @returns {Promise}
  800. */
  801. unsubscribeRemoteAudio(params) {
  802. console.log(TAG_NAME, 'unsubscribeRemoteAudio', params);
  803. return this.setPlayerConfigFun({
  804. userID: params.userID,
  805. streamType: 'main',
  806. config: {
  807. muteAudio: true
  808. }
  809. });
  810. },
  811. on: function(eventCode, handler, context) {
  812. this._emitter.on(eventCode, handler, context);
  813. },
  814. off: function(eventCode, handler) {
  815. this._emitter.off(eventCode, handler);
  816. },
  817. getRemoteUserList: function() {
  818. return this.userList;
  819. },
  820. /**
  821. * 切换前后摄像头
  822. */
  823. switchCamera: function() {
  824. if (!this.cameraPosition) {
  825. // this.data.pusher.cameraPosition 是初始值,不支持动态设置
  826. this.cameraPosition = this.pusher.frontCamera;
  827. }
  828. console.log(TAG_NAME, 'switchCamera', this.cameraPosition);
  829. this.cameraPosition = this.cameraPosition === 'front' ? 'back' : 'front';
  830. this.setData(
  831. {
  832. cameraPosition: this.cameraPosition
  833. },
  834. () => {
  835. console.log(TAG_NAME, 'switchCamera success', this.cameraPosition);
  836. }
  837. ); // wx 7.0.9 不支持动态设置 pusher.devicePosition ,需要调用api设置,这里修改cameraPosition是为了记录状态
  838. this.pusher.getPusherContext().switchCamera();
  839. },
  840. /**
  841. * 设置指定player view的渲染坐标和尺寸
  842. * @param {object} params
  843. * userID: string
  844. * streamType: string
  845. * xAxis: number
  846. * yAxis: number
  847. * width: number
  848. * height: number
  849. * @returns {Promise}
  850. */
  851. setViewRect: function(params) {
  852. console.log(TAG_NAME, 'setViewRect', params);
  853. if (this.pusher.template !== 'custom') {
  854. console.warn(`如需使用setViewRect方法,请设置template:"custom", 当前 template:"${this.pusher.template}"`);
  855. }
  856. if (this.pusher.userID === params.userID) {
  857. return this.setPusherConfigFun({
  858. xAxis: params.xAxis,
  859. yAxis: params.yAxis,
  860. width: params.width,
  861. height: params.height
  862. });
  863. }
  864. return this.setPlayerConfigFun({
  865. userID: params.userID,
  866. streamType: params.streamType,
  867. config: {
  868. xAxis: params.xAxis,
  869. yAxis: params.yAxis,
  870. width: params.width,
  871. height: params.height
  872. }
  873. });
  874. },
  875. /**
  876. * 设置指定 player 或者 pusher view 是否可见
  877. * @param {object} params
  878. * userID: string
  879. * streamType: string
  880. * isVisible:boolean
  881. * @returns {Promise}
  882. */
  883. setViewVisible: function(params) {
  884. console.log(TAG_NAME, 'setViewVisible', params); // if (this.data.pusher.template !== 'custom') {
  885. // console.warn(`如需使用setViewVisible方法,请设置template:"custom", 当前 template:"${this.data.pusher.template}"`)
  886. // }
  887. if (this.pusher.userID === params.userID) {
  888. return this.setPusherConfigFun({
  889. isVisible: params.isVisible
  890. });
  891. }
  892. return this.setPlayerConfigFun({
  893. userID: params.userID,
  894. streamType: params.streamType,
  895. config: {
  896. isVisible: params.isVisible
  897. }
  898. });
  899. },
  900. /**
  901. * 设置指定player view的层级
  902. * @param {Object} params
  903. * userID: string
  904. * streamType: string
  905. * zindex: number
  906. * @returns {Promise}
  907. */
  908. setViewZIndex: function(params) {
  909. console.log(TAG_NAME, 'setViewZIndex', params);
  910. if (this.pusher.template !== 'custom') {
  911. console.warn(`如需使用setViewZIndex方法,请设置template:"custom", 当前 template:"${this.pusher.template}"`);
  912. }
  913. if (this.pusher.userID === params.userID) {
  914. return this.setPusherConfigFun({
  915. zindex: params.zindex
  916. });
  917. }
  918. return this.setPlayerConfigFun({
  919. userID: params.userID,
  920. streamType: params.streamType,
  921. config: {
  922. zindex: params.zindex
  923. }
  924. });
  925. },
  926. /**
  927. * 播放背景音
  928. * @param {Object} params url
  929. * @returns {Promise}
  930. */
  931. playBGM: function(params) {
  932. return new Promise((resolve, reject) => {
  933. this.pusher.getPusherContext().playBGM({
  934. url: params.url,
  935. // 已经有相关事件不需要在这里监听,目前用于测试
  936. success: () => {
  937. console.log(TAG_NAME, '播放背景音成功'); // this._emitter.emit(EVENT.BGM_PLAY_START)
  938. resolve();
  939. },
  940. fail: () => {
  941. console.log(TAG_NAME, '播放背景音失败');
  942. this._emitter.emit(EVENT.BGM_PLAY_FAIL);
  943. reject(new Error('播放背景音失败'));
  944. } // complete: () => {
  945. // console.log(TAG_NAME, '背景完成')
  946. // this._emitter.emit(EVENT.BGM_PLAY_COMPLETE)
  947. // },
  948. });
  949. });
  950. },
  951. stopBGM: function() {
  952. this.pusher.getPusherContext().stopBGM();
  953. },
  954. pauseBGM: function() {
  955. this.pusher.getPusherContext().pauseBGM();
  956. },
  957. resumeBGM: function() {
  958. this.pusher.getPusherContext().resumeBGM();
  959. },
  960. /**
  961. * 设置背景音音量
  962. * @param {Object} params volume
  963. */
  964. setBGMVolume: function(params) {
  965. this.pusher.getPusherContext().setBGMVolume({
  966. volume: params.volume
  967. });
  968. },
  969. /**
  970. * 设置麦克风音量
  971. * @param {Object} params volume
  972. */
  973. setMICVolume: function(params) {
  974. this.pusher.getPusherContext().setMICVolume({
  975. volume: params.volume
  976. });
  977. },
  978. /**
  979. * 发送SEI消息
  980. * @param {Object} params message
  981. * @returns {Promise}
  982. */
  983. sendSEI: function(params) {
  984. return new Promise((resolve, reject) => {
  985. this.pusher.getPusherContext().sendMessage({
  986. msg: params.message,
  987. success: function(result) {
  988. resolve(result);
  989. }
  990. });
  991. });
  992. },
  993. /**
  994. * pusher 和 player 的截图并保存
  995. * @param {Object} params userID streamType
  996. * @returns {Promise}
  997. */
  998. snapshot: function(params) {
  999. console.log(TAG_NAME, 'snapshot', params);
  1000. return new Promise((resolve, reject) => {
  1001. this.captureSnapshot(params)
  1002. .then(result => {
  1003. wx.saveImageToPhotosAlbum({
  1004. filePath: result.tempImagePath,
  1005. success(res) {
  1006. wx.showToast({
  1007. title: '已保存到相册'
  1008. });
  1009. console.log('save photo is success', res);
  1010. resolve(result);
  1011. },
  1012. fail: function(error) {
  1013. wx.showToast({
  1014. icon: 'none',
  1015. title: '保存失败'
  1016. });
  1017. console.log('save photo is fail', error);
  1018. reject(error);
  1019. }
  1020. });
  1021. })
  1022. .catch(error => {
  1023. reject(error);
  1024. });
  1025. });
  1026. },
  1027. /**
  1028. * 获取pusher 和 player 的截图
  1029. * @param {Object} params userID streamType
  1030. * @returns {Promise}
  1031. */
  1032. captureSnapshot: function(params) {
  1033. return new Promise((resolve, reject) => {
  1034. if (params.userID === this.pusher.userID) {
  1035. // pusher
  1036. this.pusher.getPusherContext().snapshot({
  1037. quality: 'raw',
  1038. complete: result => {
  1039. console.log(TAG_NAME, 'snapshot pusher', result);
  1040. if (result.tempImagePath) {
  1041. resolve(result);
  1042. } else {
  1043. console.log('snapShot 回调失败', result);
  1044. reject(new Error('截图失败'));
  1045. }
  1046. }
  1047. });
  1048. } else {
  1049. // player
  1050. this.userController.getStream(params).playerContext.snapshot({
  1051. quality: 'raw',
  1052. complete: result => {
  1053. console.log(TAG_NAME, 'snapshot player', result);
  1054. if (result.tempImagePath) {
  1055. resolve(result);
  1056. } else {
  1057. console.log('snapShot 回调失败', result);
  1058. reject(new Error('截图失败'));
  1059. }
  1060. }
  1061. });
  1062. }
  1063. });
  1064. },
  1065. /**
  1066. * 将远端视频全屏
  1067. * @param {Object} params userID streamType direction
  1068. * @returns {Promise}
  1069. */
  1070. enterFullscreen: function(params) {
  1071. console.log(TAG_NAME, 'enterFullscreen', params);
  1072. return new Promise((resolve, reject) => {
  1073. this.userController.getStream(params).playerContext.requestFullScreen({
  1074. direction: params.direction || 0,
  1075. success: event => {
  1076. console.log(TAG_NAME, 'enterFullscreen success', event);
  1077. resolve(event);
  1078. },
  1079. fail: event => {
  1080. console.log(TAG_NAME, 'enterFullscreen fail', event);
  1081. reject(event);
  1082. }
  1083. });
  1084. });
  1085. },
  1086. /**
  1087. * 将远端视频取消全屏
  1088. * @param {Object} params userID streamType
  1089. * @returns {Promise}
  1090. */
  1091. exitFullscreen: function(params) {
  1092. console.log(TAG_NAME, 'exitFullscreen', params);
  1093. return new Promise((resolve, reject) => {
  1094. this.userController.getStream(params).playerContext.exitFullScreen({
  1095. success: event => {
  1096. console.log(TAG_NAME, 'exitFullScreen success', event);
  1097. resolve(event);
  1098. },
  1099. fail: event => {
  1100. console.log(TAG_NAME, 'exitFullScreen fail', event);
  1101. reject(event);
  1102. }
  1103. });
  1104. });
  1105. },
  1106. /**
  1107. * 设置 player 视图的横竖屏显示
  1108. * @param {Object} params userID streamType orientation: vertical, horizontal
  1109. * @returns {Promise}
  1110. */
  1111. setRemoteOrientation: function(params) {
  1112. return this.setPlayerConfigFun({
  1113. userID: params.userID,
  1114. streamType: params.streamType,
  1115. config: {
  1116. orientation: params.orientation
  1117. }
  1118. });
  1119. },
  1120. // 改为:
  1121. setViewOrientation: function(params) {
  1122. return this.setPlayerConfigFun({
  1123. userID: params.userID,
  1124. streamType: params.streamType,
  1125. config: {
  1126. orientation: params.orientation
  1127. }
  1128. });
  1129. },
  1130. /**
  1131. * 设置 player 视图的填充模式
  1132. * @param {Object} params userID streamType fillMode: contain,fillCrop
  1133. * @returns {Promise}
  1134. */
  1135. setRemoteFillMode: function(params) {
  1136. return this.setPlayerConfigFun({
  1137. userID: params.userID,
  1138. streamType: params.streamType,
  1139. config: {
  1140. objectFit: params.fillMode
  1141. }
  1142. });
  1143. },
  1144. // 改为:
  1145. setViewFillMode: function(params) {
  1146. return this.setPlayerConfigFun({
  1147. userID: params.userID,
  1148. streamType: params.streamType,
  1149. config: {
  1150. objectFit: params.fillMode
  1151. }
  1152. });
  1153. },
  1154. /**
  1155. * 切换 player 大小画面
  1156. * @param {Object} params userID streamType definition: HD SD
  1157. * @returns {Promise}
  1158. */
  1159. setRemoteDefinitionFun: function(params) {
  1160. params.streamType = 'main';
  1161. return new Promise((resolve, reject) => {
  1162. const stream = this.userController.getStream({
  1163. userID: params.userID,
  1164. streamType: params.streamType
  1165. });
  1166. if (stream && stream.streamType === 'main') {
  1167. console.log(TAG_NAME, '_switchStreamType', stream.src); // stream.volume = volume
  1168. if (stream.src.indexOf('main') > -1) {
  1169. stream.src = stream.src.replace('main', 'small');
  1170. stream._streamType = 'small'; // 用于设置面板的渲染
  1171. } else if (stream.src.indexOf('small') > -1) {
  1172. stream.src = stream.src.replace('small', 'main');
  1173. stream._streamType = 'main';
  1174. }
  1175. console.log(TAG_NAME, '_switchStreamType', stream.src);
  1176. this.setData(
  1177. {
  1178. streamList: this.streamList
  1179. },
  1180. () => {}
  1181. );
  1182. }
  1183. });
  1184. },
  1185. initStatusFun() {
  1186. this.status = {
  1187. isPush: false,
  1188. // 推流状态
  1189. isPending: false // 挂起状态,触发5000事件标记为true,onShow后标记为false
  1190. };
  1191. this._lastTapTime = 0;
  1192. this._beforeLastTapTime = 0;
  1193. this._isFullscreen = false;
  1194. },
  1195. /**
  1196. * 设置推流参数并触发页面渲染更新
  1197. * @param {Object} config live-pusher 的配置
  1198. * @returns {Promise}
  1199. */
  1200. setPusherConfigFun(config) {
  1201. console.log(TAG_NAME, '_setPusherConfig', config, this.pusher);
  1202. return new Promise((resolve, reject) => {
  1203. if (!this.pusher) {
  1204. this.pusher = new Pusher(config);
  1205. } else {
  1206. Object.assign(this.pusher, config);
  1207. }
  1208. this.setData(
  1209. {
  1210. pusher: this.pusher
  1211. },
  1212. () => {
  1213. // console.log(TAG_NAME, '_setPusherConfig setData compelete', 'config:', config, 'pusher:', this.data.pusher)
  1214. resolve(config);
  1215. }
  1216. );
  1217. });
  1218. },
  1219. /**
  1220. *
  1221. * @param {Object} params include userID,streamType,config
  1222. * @returns {Promise}
  1223. */
  1224. setPlayerConfigFun(params) {
  1225. const userID = params.userID;
  1226. const streamType = params.streamType;
  1227. const config = params.config;
  1228. console.log(TAG_NAME, '_setPlayerConfig', params);
  1229. return new Promise((resolve, reject) => {
  1230. // 获取指定的userID streamType 的 stream
  1231. const user = this.userController.getUser(userID);
  1232. if (user && user.streams[streamType]) {
  1233. user.streams[streamType] = Object.assign(user.streams[streamType], config); // user.streams引用的对象和 streamList 里的是同一个
  1234. this.setData(
  1235. {
  1236. streamList: this.streamList
  1237. },
  1238. () => {
  1239. // console.log(TAG_NAME, '_setPlayerConfig complete', params, 'streamList:', this.data.streamList)
  1240. resolve(params);
  1241. }
  1242. );
  1243. } else {
  1244. // 不需要reject,静默处理
  1245. console.warn(TAG_NAME, '指定 userID 或者 streamType 不存在'); // reject(new Error('指定 userID 或者 streamType 不存在'))
  1246. }
  1247. });
  1248. },
  1249. /**
  1250. * 必选参数检测
  1251. * @param {Object} rtcConfig rtc参数
  1252. * @returns {Boolean}
  1253. */
  1254. checkParamFun: function(rtcConfig) {
  1255. console.log(TAG_NAME, 'checkParam config:', rtcConfig);
  1256. if (!rtcConfig.sdkAppID) {
  1257. console.error('未设置 sdkAppID');
  1258. return false;
  1259. }
  1260. if (rtcConfig.roomID === undefined) {
  1261. console.error('未设置 roomID');
  1262. return false;
  1263. }
  1264. if (rtcConfig.roomID < 1 || rtcConfig.roomID > 4294967296) {
  1265. console.error('roomID 超出取值范围 1 ~ 4294967295');
  1266. return false;
  1267. }
  1268. if (!rtcConfig.userID) {
  1269. console.error('未设置 userID');
  1270. return false;
  1271. }
  1272. if (!rtcConfig.userSig) {
  1273. console.error('未设置 userSig');
  1274. return false;
  1275. }
  1276. if (!rtcConfig.template) {
  1277. console.error('未设置 template');
  1278. return false;
  1279. }
  1280. return true;
  1281. },
  1282. getPushUrlFun: function(rtcConfig) {
  1283. // 拼接 puhser url rtmp 方案
  1284. console.log(TAG_NAME, 'getPushUrl', rtcConfig);
  1285. if (ENV.IS_TRTC) {
  1286. // 版本高于7.0.8,基础库版本高于2.10.0 使用新的 url
  1287. return new Promise((resolve, reject) => {
  1288. // appscene videocall live
  1289. // cloudenv PRO CCC DEV UAT
  1290. // encsmall 0
  1291. // 对外的默认值是rtc ,对内的默认值是videocall
  1292. rtcConfig.scene = !rtcConfig.scene || rtcConfig.scene === 'rtc' ? 'videocall' : 'live';
  1293. rtcConfig.enableBlackStream = rtcConfig.enableBlackStream || 1;
  1294. rtcConfig.encsmall = rtcConfig.encsmall || 0;
  1295. rtcConfig.cloudenv = rtcConfig.cloudenv || 'PRO';
  1296. setTimeout(() => {
  1297. const pushUrl =
  1298. 'room://cloud.tencent.com/rtc?sdkappid=' +
  1299. rtcConfig.sdkAppID +
  1300. '&roomid=' +
  1301. rtcConfig.roomID +
  1302. '&userid=' +
  1303. rtcConfig.userID +
  1304. '&usersig=' +
  1305. rtcConfig.userSig +
  1306. '&appscene=' +
  1307. rtcConfig.scene +
  1308. '&encsmall=' +
  1309. rtcConfig.encsmall +
  1310. '&cloudenv=' +
  1311. rtcConfig.cloudenv;
  1312. console.log(TAG_NAME, 'getPushUrl result:', pushUrl);
  1313. resolve(pushUrl);
  1314. }, 0);
  1315. });
  1316. }
  1317. return this.requestSigServerFun(rtcConfig);
  1318. },
  1319. /**
  1320. * 获取签名和推流地址
  1321. * @param {Object} rtcConfig 进房参数配置
  1322. * @returns {Promise}
  1323. */
  1324. requestSigServerFun: function(rtcConfig) {
  1325. console.log('requestSigServer:', rtcConfig);
  1326. const sdkAppID = rtcConfig.sdkAppID;
  1327. const userID = rtcConfig.userID;
  1328. const userSig = rtcConfig.userSig;
  1329. const roomID = rtcConfig.roomID;
  1330. const privateMapKey = rtcConfig.privateMapKey;
  1331. rtcConfig.useCloud = rtcConfig.useCloud === undefined ? true : rtcConfig.useCloud;
  1332. let url = rtcConfig.useCloud ? 'https://official.opensso.tencent-cloud.com/v4/openim/jsonvideoapp' : 'https://yun.tim.qq.com/v4/openim/jsonvideoapp';
  1333. url += '?sdkappid=' + sdkAppID + '&identifier=' + userID + '&usersig=' + userSig + '&random=' + Date.now() + '&contenttype=json';
  1334. const reqHead = {
  1335. Cmd: 1,
  1336. SeqNo: 1,
  1337. BusType: 7,
  1338. GroupId: roomID
  1339. };
  1340. const reqBody = {
  1341. PrivMapEncrypt: privateMapKey,
  1342. TerminalType: 1,
  1343. FromType: 3,
  1344. SdkVersion: 26280566
  1345. };
  1346. console.log('requestSigServer:', url, reqHead, reqBody);
  1347. return new Promise((resolve, reject) => {
  1348. wx.request({
  1349. url: url,
  1350. data: {
  1351. ReqHead: reqHead,
  1352. ReqBody: reqBody
  1353. },
  1354. method: 'POST',
  1355. success: res => {
  1356. console.log('requestSigServer success:', res);
  1357. if (res.data['ErrorCode'] || res.data['RspHead']['ErrorCode'] !== 0) {
  1358. // console.error(res.data['ErrorInfo'] || res.data['RspHead']['ErrorInfo'])
  1359. console.error('获取roomsig失败');
  1360. reject(res);
  1361. }
  1362. const roomSig = JSON.stringify(res.data['RspBody']);
  1363. let pushUrl = 'room://cloud.tencent.com?sdkappid=' + sdkAppID + '&roomid=' + roomID + '&userid=' + userID + '&roomsig=' + encodeURIComponent(roomSig); // TODO 需要重新整理的逻辑
  1364. // 如果有配置纯音频推流或者recordId参数
  1365. if (rtcConfig.pureAudioPushMod || rtcConfig.recordId) {
  1366. const bizbuf = {
  1367. Str_uc_params: {
  1368. pure_audio_push_mod: 0,
  1369. record_id: 0
  1370. }
  1371. }; // 纯音频推流
  1372. if (rtcConfig.pureAudioPushMod) {
  1373. bizbuf.Str_uc_params.pure_audio_push_mod = rtcConfig.pureAudioPushMod;
  1374. } else {
  1375. delete bizbuf.Str_uc_params.pure_audio_push_mod;
  1376. } // 自动录制时业务自定义id
  1377. if (rtcConfig.recordId) {
  1378. bizbuf.Str_uc_params.record_id = rtcConfig.recordId;
  1379. } else {
  1380. delete bizbuf.Str_uc_params.record_id;
  1381. }
  1382. pushUrl += '&bizbuf=' + encodeURIComponent(JSON.stringify(bizbuf));
  1383. }
  1384. console.log('roomSigInfo', pushUrl);
  1385. resolve(pushUrl);
  1386. },
  1387. fail: res => {
  1388. console.log('requestSigServer fail:', res);
  1389. reject(res);
  1390. }
  1391. });
  1392. });
  1393. },
  1394. doubleTabToggleFullscreenFun: function(event) {
  1395. const curTime = event.timeStamp;
  1396. const lastTime = this._lastTapTime; // 已知问题:上次全屏操作后,必须等待1.5s后才能再次进行全屏操作,否则引发SDK全屏异常,因此增加节流逻辑
  1397. const beforeLastTime = this._beforeLastTapTime;
  1398. console.log(TAG_NAME, 'doubleTabToggleFullscreenFun', event, lastTime, beforeLastTime);
  1399. if (curTime - lastTime > 0 && curTime - lastTime < 300 && lastTime - beforeLastTime > 1500) {
  1400. const userID = event.currentTarget.dataset.userid;
  1401. const streamType = event.currentTarget.dataset.streamtype;
  1402. if (this._isFullscreen) {
  1403. this.exitFullscreen({
  1404. userID,
  1405. streamType
  1406. })
  1407. .then(() => {
  1408. this._isFullscreen = false;
  1409. })
  1410. .catch(() => {});
  1411. } else {
  1412. // const stream = this.userController.getStream({ userID, streamType })
  1413. let direction; // // 已知问题:视频的尺寸需要等待player触发NetStatus事件才能获取到,如果进房就双击全屏,全屏后的方向有可能不对。
  1414. // if (stream && stream.videoWidth && stream.videoHeight) {
  1415. // // 如果是横视频,全屏时进行横屏处理。如果是竖视频,则为0
  1416. // direction = stream.videoWidth > stream.videoHeight ? 90 : 0
  1417. // }
  1418. this.enterFullscreen({
  1419. userID,
  1420. streamType,
  1421. direction
  1422. })
  1423. .then(() => {
  1424. this._isFullscreen = true;
  1425. })
  1426. .catch(() => {});
  1427. }
  1428. this._beforeLastTapTime = lastTime;
  1429. }
  1430. this._lastTapTime = curTime;
  1431. },
  1432. /**
  1433. * TRTC-room 远端用户和音视频状态处理
  1434. */
  1435. bindEventFun: function() {
  1436. // 远端用户进房
  1437. this.userController.on(EVENT.REMOTE_USER_JOIN, event => {
  1438. console.log(TAG_NAME, '远端用户进房', event, event.data.userID);
  1439. this.setData(
  1440. {
  1441. userList: event.data.userList
  1442. },
  1443. () => {
  1444. this._emitter.emit(EVENT.REMOTE_USER_JOIN, {
  1445. userID: event.data.userID
  1446. });
  1447. }
  1448. );
  1449. console.log(TAG_NAME, 'REMOTE_USER_JOIN', 'streamList:', this.streamList, 'userList:', this.userList);
  1450. }); // 远端用户离开
  1451. this.userController.on(EVENT.REMOTE_USER_LEAVE, event => {
  1452. console.log(TAG_NAME, '远端用户离开', event, event.data.userID);
  1453. if (event.data.userID) {
  1454. this.setData(
  1455. {
  1456. userList: event.data.userList,
  1457. streamList: event.data.streamList
  1458. },
  1459. () => {
  1460. this._emitter.emit(EVENT.REMOTE_USER_LEAVE, {
  1461. userID: event.data.userID
  1462. });
  1463. }
  1464. );
  1465. }
  1466. console.log(TAG_NAME, 'REMOTE_USER_LEAVE', 'streamList:', this.streamList, 'userList:', this.userList);
  1467. }); // 视频状态 true
  1468. this.userController.on(EVENT.REMOTE_VIDEO_ADD, event => {
  1469. console.log(TAG_NAME, '远端视频可用', event, event.data.stream.userID);
  1470. const stream = event.data.stream;
  1471. this.setData(
  1472. {
  1473. userList: event.data.userList,
  1474. streamList: event.data.streamList
  1475. },
  1476. () => {
  1477. // 完善 的stream 的 playerContext
  1478. stream.playerContext = wx.createLivePlayerContext(stream.streamID, this); // 新增的需要触发一次play 默认属性才能生效
  1479. // stream.playerContext.play()
  1480. // console.log(TAG_NAME, 'REMOTE_VIDEO_ADD playerContext.play()', stream)
  1481. // TODO 视频通话模版默认订阅且显示
  1482. this._emitter.emit(EVENT.REMOTE_VIDEO_ADD, {
  1483. userID: stream.userID,
  1484. streamType: stream.streamType
  1485. });
  1486. }
  1487. );
  1488. console.log(TAG_NAME, 'REMOTE_VIDEO_ADD', 'streamList:', this.streamList, 'userList:', this.userList);
  1489. }); // 视频状态 false
  1490. this.userController.on(EVENT.REMOTE_VIDEO_REMOVE, event => {
  1491. console.log(TAG_NAME, '远端视频移除', event, event.data.stream.userID);
  1492. const stream = event.data.stream;
  1493. this.setData(
  1494. {
  1495. userList: event.data.userList,
  1496. streamList: event.data.streamList
  1497. },
  1498. () => {
  1499. // 有可能先触发了退房事件,用户名下的所有stream都已清除
  1500. if (stream.userID && stream.streamType) {
  1501. this._emitter.emit(EVENT.REMOTE_VIDEO_REMOVE, {
  1502. userID: stream.userID,
  1503. streamType: stream.streamType
  1504. });
  1505. }
  1506. }
  1507. );
  1508. console.log(TAG_NAME, 'REMOTE_VIDEO_REMOVE', 'streamList:', this.streamList, 'userList:', this.userList);
  1509. }); // 音频可用
  1510. this.userController.on(EVENT.REMOTE_AUDIO_ADD, event => {
  1511. console.log(TAG_NAME, '远端音频可用', event);
  1512. const stream = event.data.stream;
  1513. this.setData(
  1514. {
  1515. userList: event.data.userList,
  1516. streamList: event.data.streamList
  1517. },
  1518. () => {
  1519. stream.playerContext = wx.createLivePlayerContext(stream.streamID, this); // 新增的需要触发一次play 默认属性才能生效
  1520. // stream.playerContext.play()
  1521. // console.log(TAG_NAME, 'REMOTE_AUDIO_ADD playerContext.play()', stream)
  1522. this._emitter.emit(EVENT.REMOTE_AUDIO_ADD, {
  1523. userID: stream.userID,
  1524. streamType: stream.streamType
  1525. });
  1526. }
  1527. );
  1528. console.log(TAG_NAME, 'REMOTE_AUDIO_ADD', 'streamList:', this.streamList, 'userList:', this.userList);
  1529. }); // 音频不可用
  1530. this.userController.on(EVENT.REMOTE_AUDIO_REMOVE, event => {
  1531. console.log(TAG_NAME, '远端音频移除', event, event.data.stream.userID);
  1532. const stream = event.data.stream;
  1533. this.setData(
  1534. {
  1535. userList: event.data.userList,
  1536. streamList: event.data.streamList
  1537. },
  1538. () => {
  1539. // 有可能先触发了退房事件,用户名下的所有stream都已清除
  1540. if (stream.userID && stream.streamType) {
  1541. this._emitter.emit(EVENT.REMOTE_AUDIO_REMOVE, {
  1542. userID: stream.userID,
  1543. streamType: stream.streamType
  1544. });
  1545. }
  1546. }
  1547. );
  1548. console.log(TAG_NAME, 'REMOTE_AUDIO_REMOVE', 'streamList:', this.streamList, 'userList:', this.userList);
  1549. });
  1550. },
  1551. /**
  1552. * pusher event handler
  1553. * @param {*} event 事件实例
  1554. */
  1555. pusherStateChangeHandlerFun: function(event) {
  1556. const code = event.detail.code;
  1557. const message = event.detail.message;
  1558. console.log(TAG_NAME, 'pusherStateChange:', code, event);
  1559. switch (code) {
  1560. case 0:
  1561. console.log(message, code);
  1562. break;
  1563. case 1001:
  1564. console.log('已经连接推流服务器', code);
  1565. break;
  1566. case 1002:
  1567. console.log('已经与服务器握手完毕,开始推流', code);
  1568. break;
  1569. case 1003:
  1570. console.log('打开摄像头成功', code);
  1571. break;
  1572. case 1004:
  1573. console.log('录屏启动成功', code);
  1574. break;
  1575. case 1005:
  1576. console.log('推流动态调整分辨率', code);
  1577. break;
  1578. case 1006:
  1579. console.log('推流动态调整码率', code);
  1580. break;
  1581. case 1007:
  1582. console.log('首帧画面采集完成', code);
  1583. break;
  1584. case 1008:
  1585. console.log('编码器启动', code);
  1586. break;
  1587. case 1018:
  1588. console.log('进房成功', code);
  1589. this._emitter.emit(EVENT.LOCAL_JOIN, {
  1590. userID: this.pusher.userID
  1591. });
  1592. break;
  1593. case 1019:
  1594. console.log('退出房间', code);
  1595. this._emitter.emit(EVENT.LOCAL_LEAVE, {
  1596. userID: this.pusher.userID
  1597. });
  1598. break;
  1599. case 2003:
  1600. console.log('渲染首帧视频', code);
  1601. break;
  1602. case 1020:
  1603. case 1031:
  1604. case 1032:
  1605. case 1033:
  1606. case 1034:
  1607. // 通过 userController 处理 1020 1031 1032 1033 1034
  1608. this.userController.userEventHandler(event);
  1609. break;
  1610. case -1301:
  1611. console.error('打开摄像头失败: ', code);
  1612. this._emitter.emit(EVENT.ERROR, {
  1613. code,
  1614. message
  1615. });
  1616. break;
  1617. case -1302:
  1618. console.error('打开麦克风失败: ', code);
  1619. this._emitter.emit(EVENT.ERROR, {
  1620. code,
  1621. message
  1622. });
  1623. break;
  1624. case -1303:
  1625. console.error('视频编码失败: ', code);
  1626. this._emitter.emit(EVENT.ERROR, {
  1627. code,
  1628. message
  1629. });
  1630. break;
  1631. case -1304:
  1632. console.error('音频编码失败: ', code);
  1633. this._emitter.emit(EVENT.ERROR, {
  1634. code,
  1635. message
  1636. });
  1637. break;
  1638. case -1307:
  1639. console.error('推流连接断开: ', code);
  1640. this._emitter.emit(EVENT.ERROR, {
  1641. code,
  1642. message
  1643. });
  1644. break;
  1645. case -100018:
  1646. console.error('进房失败: ', code, message);
  1647. this._emitter.emit(EVENT.ERROR, {
  1648. code,
  1649. message
  1650. });
  1651. break;
  1652. case 5000:
  1653. console.log('小程序被挂起: ', code); // 终端 sdk 建议执行退房操作,唤起时重新进房,临时解决方案,待小程序SDK完全实现自动重新推流后可以去掉
  1654. this.status.isPending = true;
  1655. if (this.status.isPush) {
  1656. // this.exitRoom()
  1657. const tempUrl = this.pusher.url;
  1658. this.pusher.url = ''; // console.log('5000 小程序被挂起后更换pusher', this.data.pusher.getPusherContext().webviewId)
  1659. this.setData(
  1660. {
  1661. pusher: this.pusher
  1662. },
  1663. () => {
  1664. this.pusher.url = tempUrl;
  1665. this.setData(
  1666. {
  1667. pusher: this.pusher
  1668. },
  1669. () => {
  1670. this.pusher.getPusherContext().start();
  1671. console.log('5000 小程序被挂起后更换pusher', this.pusher);
  1672. }
  1673. );
  1674. }
  1675. );
  1676. }
  1677. break;
  1678. case 1021:
  1679. console.log('网络类型发生变化,需要重新进房', code);
  1680. break;
  1681. case 2007:
  1682. console.log('本地视频播放loading: ', code);
  1683. break;
  1684. case 2004:
  1685. console.log('本地视频播放开始: ', code);
  1686. break;
  1687. default:
  1688. console.log(message, code);
  1689. }
  1690. this._emitter.emit(EVENT.LOCAL_STATE_UPDATE, {
  1691. data: event
  1692. });
  1693. },
  1694. pusherNetStatusHandlerFun: function(event) {
  1695. // 触发 LOCAL_NET_STATE_UPDATE
  1696. this._emitter.emit(EVENT.LOCAL_NET_STATE_UPDATE, event);
  1697. },
  1698. pusherErrorHandlerFun: function(event) {
  1699. // 触发 ERROR
  1700. console.warn(TAG_NAME, 'pusher error', event);
  1701. try {
  1702. const code = event.detail.errCode;
  1703. const message = event.detail.errMsg;
  1704. this._emitter.emit(EVENT.ERROR, {
  1705. code,
  1706. message
  1707. });
  1708. } catch (exception) {
  1709. console.error(TAG_NAME, 'pusher error data parser exception', event, exception);
  1710. }
  1711. },
  1712. pusherBGMStartHandlerFun: function(event) {
  1713. // 触发 BGM_START 已经在playBGM方法中进行处理
  1714. // this._emitter.emit(EVENT.BGM_PLAY_START, { data: event })
  1715. },
  1716. pusherBGMProgressHandlerFun: function(event) {
  1717. // BGM_PROGRESS
  1718. this._emitter.emit(EVENT.BGM_PLAY_PROGRESS, event);
  1719. },
  1720. pusherBGMCompleteHandlerFun: function(event) {
  1721. // BGM_COMPLETE
  1722. this._emitter.emit(EVENT.BGM_PLAY_COMPLETE, event);
  1723. },
  1724. // player event handler
  1725. // 获取 player ID 再进行触发
  1726. playerStateChangeFun: function(event) {
  1727. // console.log(TAG_NAME, 'playerStateChangeFun', event)
  1728. this._emitter.emit(EVENT.REMOTE_STATE_UPDATE, event);
  1729. },
  1730. playerFullscreenChangeFun: function(event) {
  1731. // console.log(TAG_NAME, '_playerFullscreenChange', event)
  1732. this._emitter.emit(EVENT.REMOTE_NET_STATE_UPDATE, event);
  1733. },
  1734. playerNetStatusFun: function(event) {
  1735. // console.log(TAG_NAME, 'playerNetStatusFun', event)
  1736. // 获取player 视频的宽高
  1737. const stream = this.userController.getStream({
  1738. userID: event.currentTarget.dataset.userid,
  1739. streamType: event.currentTarget.dataset.streamtype
  1740. });
  1741. if (stream && (stream.videoWidth !== event.detail.info.videoWidth || stream.videoHeight !== event.detail.info.videoHeight)) {
  1742. console.log(TAG_NAME, 'playerNetStatusFun update video size', event);
  1743. stream.videoWidth = event.detail.info.videoWidth;
  1744. stream.videoHeight = event.detail.info.videoHeight;
  1745. }
  1746. this._emitter.emit(EVENT.REMOTE_FULLSCREEN_UPDATE, event);
  1747. },
  1748. playerAudioVolumeNotifyFun: function(event) {
  1749. // console.log(TAG_NAME, 'playerAudioVolumeNotifyFun', event)
  1750. this._emitter.emit(EVENT.REMOTE_AUDIO_VOLUME_UPDATE, event);
  1751. },
  1752. /**
  1753. * 监听组件属性变更,外部变更组件属性时触发该监听,用于检查属性设置是否正常
  1754. * @param {Object} data 变更数据
  1755. */
  1756. propertyObserverFun: function(data) {
  1757. console.log(TAG_NAME, '_propertyObserver', data, this.config);
  1758. if (data.name === 'config') {
  1759. // const config = Object.assign(DEFAULT_PUSHER_CONFIG, data.newVal)
  1760. const config = data.newVal; // querystring 只支持String类型,做一个类型防御
  1761. if (typeof config.debugMode === 'string') {
  1762. config.debugMode === 'true' ? true : false;
  1763. } // 独立设置与pusher无关的配置
  1764. this.setData({
  1765. template: config.template,
  1766. debugMode: config.debugMode || false,
  1767. debug: config.debugMode || false
  1768. });
  1769. this.setPusherConfigFun(config);
  1770. }
  1771. },
  1772. toggleVideoFun() {
  1773. if (this.pusher.enableCamera) {
  1774. this.unpublishLocalVideo();
  1775. } else {
  1776. this.publishLocalVideo();
  1777. }
  1778. },
  1779. toggleAudioFun() {
  1780. if (this.pusher.enableMic) {
  1781. this.unpublishLocalAudio();
  1782. } else {
  1783. this.publishLocalAudio();
  1784. }
  1785. },
  1786. debugToggleRemoteVideoFun(event) {
  1787. console.log(TAG_NAME, '_debugToggleRemoteVideo', event.currentTarget.dataset);
  1788. const userID = event.currentTarget.dataset.userID;
  1789. const streamType = event.currentTarget.dataset.streamType;
  1790. const stream = this.streamList.find(item => {
  1791. return item.userID === userID && item.streamType === streamType;
  1792. });
  1793. if (stream.muteVideo) {
  1794. this.subscribeRemoteVideo({
  1795. userID,
  1796. streamType
  1797. });
  1798. this.setViewVisible({
  1799. userID,
  1800. streamType,
  1801. isVisible: true
  1802. });
  1803. } else {
  1804. this.unsubscribeRemoteVideo({
  1805. userID,
  1806. streamType
  1807. });
  1808. this.setViewVisible({
  1809. userID,
  1810. streamType,
  1811. isVisible: false
  1812. });
  1813. }
  1814. },
  1815. debugToggleRemoteAudioFun(event) {
  1816. console.log(TAG_NAME, '_debugToggleRemoteAudio', event.currentTarget.dataset);
  1817. const userID = event.currentTarget.dataset.userID;
  1818. const streamType = event.currentTarget.dataset.streamType;
  1819. const stream = this.streamList.find(item => {
  1820. return item.userID === userID && item.streamType === streamType;
  1821. });
  1822. if (stream.muteAudio) {
  1823. this.subscribeRemoteAudio({
  1824. userID
  1825. });
  1826. } else {
  1827. this.unsubscribeRemoteAudio({
  1828. userID
  1829. });
  1830. }
  1831. },
  1832. debugToggleVideoDebugFun() {
  1833. this.setData({
  1834. debug: !this.debug
  1835. });
  1836. },
  1837. debugExitRoomFun() {
  1838. this.exitRoom();
  1839. },
  1840. debugEnterRoomFun() {
  1841. this.publishLocalVideo();
  1842. this.publishLocalAudio();
  1843. this.enterRoom({
  1844. roomID: this.config.roomID
  1845. }).then(() => {
  1846. // 进房后开始推送视频或音频
  1847. });
  1848. },
  1849. debugGoBackFun() {
  1850. wx.navigateBack({
  1851. delta: 1
  1852. });
  1853. },
  1854. debugTogglePanelFun() {
  1855. this.setData({
  1856. debugPanel: !this.debugPanel
  1857. });
  1858. },
  1859. toggleAudioVolumeTypeFun() {
  1860. if (this.pusher.audioVolumeType === 'voicecall') {
  1861. this.setPusherConfigFun({
  1862. audioVolumeType: 'media'
  1863. });
  1864. } else {
  1865. this.setPusherConfigFun({
  1866. audioVolumeType: 'voicecall'
  1867. });
  1868. }
  1869. },
  1870. toggleSoundModeFun() {
  1871. if (this.userList.length === 0) {
  1872. return;
  1873. }
  1874. const stream = this.userController.getStream({
  1875. userID: this.userList[0].userID,
  1876. streamType: 'main'
  1877. });
  1878. if (stream) {
  1879. if (stream.soundMode === 'speaker') {
  1880. stream['soundMode'] = 'ear';
  1881. } else {
  1882. stream['soundMode'] = 'speaker';
  1883. }
  1884. this.setPlayerConfigFun({
  1885. userID: stream.userID,
  1886. streamType: 'main',
  1887. config: {
  1888. soundMode: stream['soundMode']
  1889. }
  1890. });
  1891. }
  1892. },
  1893. /**
  1894. * 退出通话
  1895. */
  1896. hangUpFun: function() {
  1897. this.exitRoom();
  1898. wx.navigateBack({
  1899. delta: 1
  1900. });
  1901. },
  1902. /**
  1903. * 切换订阅音频状态
  1904. */
  1905. handleSubscribeAudio: function() {
  1906. if (this.pusher.enableMic) {
  1907. this.unpublishLocalAudio();
  1908. } else {
  1909. this.publishLocalAudio();
  1910. }
  1911. },
  1912. /**
  1913. * 切换订阅远端视频状态
  1914. * @param event
  1915. */
  1916. handleSubscribeRemoteVideoFun: function(event) {
  1917. const userID = event.currentTarget.dataset.userID;
  1918. const streamType = event.currentTarget.dataset.streamType;
  1919. const stream = this.streamList.find(item => {
  1920. return item.userID === userID && item.streamType === streamType;
  1921. });
  1922. if (stream.muteVideo) {
  1923. this.subscribeRemoteVideo({
  1924. userID,
  1925. streamType
  1926. });
  1927. } else {
  1928. this.unsubscribeRemoteVideo({
  1929. userID,
  1930. streamType
  1931. });
  1932. }
  1933. },
  1934. /**
  1935. * 将远端视频取消全屏
  1936. * @param event
  1937. */
  1938. handleSubscribeRemoteAudioFun: function(event) {
  1939. const userID = event.currentTarget.dataset.userID;
  1940. const streamType = event.currentTarget.dataset.streamType;
  1941. const stream = this.streamList.find(item => {
  1942. return item.userID === userID && item.streamType === streamType;
  1943. });
  1944. if (stream.muteAudio) {
  1945. this.subscribeRemoteAudio({
  1946. userID
  1947. });
  1948. } else {
  1949. this.unsubscribeRemoteAudio({
  1950. userID
  1951. });
  1952. }
  1953. },
  1954. /**
  1955. * grid布局, 唤起 memberlist-panel
  1956. */
  1957. switchMemberListPanelFun() {
  1958. this.setData({
  1959. panelName: this.panelName !== 'memberlist-panel' ? 'memberlist-panel' : ''
  1960. });
  1961. },
  1962. /**
  1963. * grid布局, 唤起setting-panel
  1964. */
  1965. switchSettingPanelFun() {
  1966. this.setData({
  1967. panelName: this.panelName !== 'setting-panel' ? 'setting-panel' : ''
  1968. });
  1969. },
  1970. handleMaskerClickFun() {
  1971. this.setData({
  1972. panelName: ''
  1973. });
  1974. },
  1975. setPuserPropertyFun(event) {
  1976. // console.log(TAG_NAME, '_setPuserProperty', event)
  1977. const key = event.currentTarget.dataset.key;
  1978. let value = event.currentTarget.dataset.value;
  1979. const config = {};
  1980. if (value === 'true') {
  1981. value = true;
  1982. } else if (value === 'false') {
  1983. value = false;
  1984. }
  1985. if (typeof value === 'boolean') {
  1986. config[key] = !this.pusher[key];
  1987. } else if (typeof value === 'string' && value.indexOf('|') > 0) {
  1988. value = value.split('|');
  1989. if (this.pusher[key] === value[0]) {
  1990. config[key] = value[1];
  1991. } else {
  1992. config[key] = value[0];
  1993. }
  1994. } // console.log(TAG_NAME, '_setPuserProperty', config)
  1995. this.setPusherConfigFun(config);
  1996. },
  1997. setPlayerPropertyFun(event) {
  1998. console.log(TAG_NAME, '_setPlayerProperty', event);
  1999. const userID = event.currentTarget.dataset.userid;
  2000. const streamType = event.currentTarget.dataset.streamtype;
  2001. const key = event.currentTarget.dataset.key;
  2002. let value = event.currentTarget.dataset.value;
  2003. const stream = this.userController.getStream({
  2004. userID: userID,
  2005. streamType: streamType
  2006. });
  2007. if (!stream) {
  2008. return;
  2009. }
  2010. const config = {};
  2011. if (value === 'true') {
  2012. value = true;
  2013. } else if (value === 'false') {
  2014. value = false;
  2015. }
  2016. if (typeof value === 'boolean') {
  2017. config[key] = !stream[key];
  2018. } else if (typeof value === 'string' && value.indexOf('|') > 0) {
  2019. value = value.split('|');
  2020. if (stream[key] === value[0]) {
  2021. config[key] = value[1];
  2022. } else {
  2023. config[key] = value[0];
  2024. }
  2025. }
  2026. console.log(TAG_NAME, '_setPlayerProperty', config);
  2027. this.setPlayerConfigFun({
  2028. userID,
  2029. streamType,
  2030. config
  2031. });
  2032. },
  2033. switchStreamTypeFun(event) {
  2034. const userID = event.currentTarget.dataset.userid;
  2035. const streamType = event.currentTarget.dataset.streamtype;
  2036. const stream = this.userController.getStream({
  2037. userID: userID,
  2038. streamType: streamType
  2039. });
  2040. if (stream && stream.streamType === 'main') {
  2041. if (stream._definitionType === 'small') {
  2042. this.subscribeRemoteVideo({
  2043. userID,
  2044. streamType: 'main'
  2045. });
  2046. } else {
  2047. this.subscribeRemoteVideo({
  2048. userID,
  2049. streamType: 'small'
  2050. });
  2051. }
  2052. }
  2053. },
  2054. handleSnapshotClickFun(event) {
  2055. wx.showToast({
  2056. title: '开始截屏',
  2057. icon: 'none',
  2058. duration: 1000
  2059. });
  2060. const userID = event.currentTarget.dataset.userid;
  2061. const streamType = event.currentTarget.dataset.streamtype;
  2062. this.snapshot({
  2063. userID,
  2064. streamType
  2065. });
  2066. },
  2067. /**
  2068. * grid布局, 绑定事件
  2069. */
  2070. bindEventGridFun() {
  2071. // 远端音量变更
  2072. this.on(EVENT.REMOTE_AUDIO_VOLUME_UPDATE, event => {
  2073. const data = event.data;
  2074. const userID = data.currentTarget.dataset.userid;
  2075. const streamType = data.currentTarget.dataset.streamtype;
  2076. const volume = data.detail.volume; // console.log(TAG_NAME, '远端音量变更', userID, streamType, volume)
  2077. const stream = this.userController.getStream({
  2078. userID: userID,
  2079. streamType: streamType
  2080. });
  2081. if (stream) {
  2082. stream.volume = volume;
  2083. }
  2084. this.setData(
  2085. {
  2086. streamList: this.streamList
  2087. },
  2088. () => {}
  2089. );
  2090. });
  2091. },
  2092. toggleFullscreenFun(event) {
  2093. console.log(TAG_NAME, '_toggleFullscreen', event);
  2094. const userID = event.currentTarget.dataset.userID;
  2095. const streamType = event.currentTarget.dataset.streamType;
  2096. if (this._isFullscreen) {
  2097. this.exitFullscreen({
  2098. userID,
  2099. streamType
  2100. })
  2101. .then(() => {
  2102. this._isFullscreen = false;
  2103. })
  2104. .catch(() => {});
  2105. } else {
  2106. // const stream = this.userController.getStream({ userID, streamType })
  2107. const direction = 0; // 已知问题:视频的尺寸需要等待player触发NetStatus事件才能获取到,如果进房就双击全屏,全屏后的方向有可能不对。
  2108. // if (stream && stream.videoWidth && stream.videoHeight) {
  2109. // // 如果是横视频,全屏时进行横屏处理。如果是竖视频,则为0
  2110. // direction = stream.videoWidth > stream.videoHeight ? 90 : 0
  2111. // }
  2112. this.enterFullscreen({
  2113. userID,
  2114. streamType,
  2115. direction
  2116. })
  2117. .then(() => {
  2118. this._isFullscreen = true;
  2119. })
  2120. .catch(() => {});
  2121. }
  2122. },
  2123. // ______ __ __ ______ __ __
  2124. // | \| \ / \ | \ | \ | \
  2125. // \$$$$$$| $$\ / $$ \$$$$$$ _______ _| $$_ ______ ______ _______ ______ | $$
  2126. // | $$ | $$$\ / $$$ | $$ | \| $$ \ / \ / \ | \ | \ | $$
  2127. // | $$ | $$$$\ $$$$ | $$ | $$$$$$$\\$$$$$$ | $$$$$$\| $$$$$$\| $$$$$$$\ \$$$$$$\| $$
  2128. // | $$ | $$\$$ $$ $$ | $$ | $$ | $$ | $$ __ | $$ $$| $$ \$$| $$ | $$ / $$| $$
  2129. // _| $$_ | $$ \$$$| $$ _| $$_ | $$ | $$ | $$| \| $$$$$$$$| $$ | $$ | $$| $$$$$$$| $$
  2130. // | $$ \| $$ \$ | $$ | $$ \| $$ | $$ \$$ $$ \$$ \| $$ | $$ | $$ \$$ $$| $$
  2131. // \$$$$$$ \$$ \$$ \$$$$$$ \$$ \$$ \$$$$ \$$$$$$$ \$$ \$$ \$$ \$$$$$$$ \$$
  2132. getCountNum(params) {
  2133. let promise = this.tim.getGroupProfile({ groupID: this.config.roomID+"" });
  2134. promise.then(function(imResponse) {
  2135. console.log(imResponse.data.group);
  2136. }).catch(function(imError) {
  2137. console.warn('getGroupProfile error:', imError); // 获取群详细资料失败的相关信息
  2138. });
  2139. },
  2140. /**
  2141. * 初始化 IM SDK
  2142. * @param {Object} config sdkAppID
  2143. */
  2144. sendGroupCustomMessage(params) {
  2145. if (!this.tim) {
  2146. console.warn(TAG_NAME, '未开启IM功能,该方法无法使用', params)
  2147. return
  2148. }
  2149. console.log(TAG_NAME, 'sendGroupCustomMessage', params)
  2150. const message = this.tim.createCustomMessage({
  2151. to: params.roomID + '',
  2152. conversationType: TIM.TYPES.CONV_GROUP,
  2153. payload: params.payload,
  2154. })
  2155. const promise = this.tim.sendMessage(message)
  2156. promise.then(function(imResponse) {
  2157. // 发送成功
  2158. console.log(TAG_NAME, 'sendMessage success', imResponse)
  2159. }).catch(function(imError) {
  2160. // 发送失败
  2161. console.warn(TAG_NAME, 'sendMessage error:', imError)
  2162. })
  2163. return promise
  2164. },
  2165. _initIM(config,roomId) {
  2166. if (!config.enableIM || !config.sdkAppID || this.tim) {
  2167. return
  2168. }
  2169. console.log(TAG_NAME, '_initIM', config)
  2170. // 初始化 sdk 实例
  2171. const tim = TIM.create({
  2172. SDKAppID: config.sdkAppID,
  2173. })
  2174. // 0 普通级别,日志量较多,接入时建议使用
  2175. // 1 release级别,SDK 输出关键信息,生产环境时建议使用
  2176. // 2 告警级别,SDK 只输出告警和错误级别的日志
  2177. // 3 错误级别,SDK 只输出错误级别的日志
  2178. // 4 无日志级别,SDK 将不打印任何日志
  2179. if (config.debugMode) {
  2180. tim.setLogLevel(1)
  2181. } else {
  2182. tim.setLogLevel(4)
  2183. }
  2184. // 取消监听
  2185. tim.off(TIM.EVENT.SDK_READY, this._onIMReady)
  2186. tim.off(TIM.EVENT.MESSAGE_RECEIVED, this._onIMMessageReceived)
  2187. tim.off(TIM.EVENT.SDK_NOT_READY, this._onIMNotReady)
  2188. tim.off(TIM.EVENT.KICKED_OUT, this._onIMKickedOut)
  2189. tim.off(TIM.EVENT.ERROR, this._onIMError)
  2190. // 监听事件
  2191. tim.on(TIM.EVENT.SDK_READY, this._onIMReady, this)
  2192. tim.on(TIM.EVENT.MESSAGE_RECEIVED, this._onIMMessageReceived, this)
  2193. tim.on(TIM.EVENT.SDK_NOT_READY, this._onIMNotReady, this)
  2194. tim.on(TIM.EVENT.KICKED_OUT, this._onIMKickedOut, this)
  2195. tim.on(TIM.EVENT.ERROR, this._onIMError, this)
  2196. this.tim = tim
  2197. wx.tim = tim
  2198. this._loginIM({ ...this.config, roomID: roomId })
  2199. },
  2200. _loginIM(params) {
  2201. if (!this.tim) {
  2202. return
  2203. }
  2204. console.log(TAG_NAME, '_loginIM', params)
  2205. var that=this;
  2206. setTimeout(function(){
  2207. that.sendGroupCustomMessage({
  2208. roomID: Number(that.config.roomID), // 房间 ID
  2209. payload: {
  2210. data: 'genxin',
  2211. description: 'genxin',
  2212. extension: 'genxin'
  2213. }
  2214. })
  2215. },2000)
  2216. return this.tim.login({
  2217. userID: params.userID,
  2218. userSig: params.userSig,
  2219. })
  2220. },
  2221. _logoutIM() {
  2222. if (!this.tim) {
  2223. return
  2224. }
  2225. console.log(TAG_NAME, '_logoutIM')
  2226. return this.tim.logout()
  2227. },
  2228. _exitIM() {
  2229. this.sendGroupCustomMessage({
  2230. roomID: Number(this.config.roomID), // 房间 ID
  2231. payload: {
  2232. data: 'genxin',
  2233. description: 'genxin',
  2234. extension: 'genxin'
  2235. }
  2236. })
  2237. var that=this;
  2238. setTimeout(function(){
  2239. // 方法需要调用限制,否则重复解散群 退群会有warn
  2240. if (that.config.exitIMThrottle || !that.tim) {
  2241. return
  2242. }
  2243. that.config.exitIMThrottle = true
  2244. const userList = that.getRemoteUserList()
  2245. const roomID = that.config.roomID
  2246. const userID = that.config.userID
  2247. that._searchGroup({ roomID }).then((imResponse) => {
  2248. // 查询群资料,判断是否为群主
  2249. if (imResponse.data.group.ownerID === userID && userList.length === 0) {
  2250. // 如果 userList 为 0 群主可以解散群,并登出IM
  2251. that._dismissGroup({ roomID }).then(()=>{
  2252. that.config.exitIMThrottle = false
  2253. that._logoutIM()
  2254. }).catch((imError) => {
  2255. that.config.exitIMThrottle = false
  2256. that._logoutIM()
  2257. })
  2258. } else if (imResponse.data.group.ownerID === userID) {
  2259. that.config.exitIMThrottle = false
  2260. // 群主不能退群只能登出
  2261. that._logoutIM()
  2262. } else {
  2263. // 普通成员退群并登出IM
  2264. that._quitGroup({ roomID }).then(()=>{
  2265. that.config.exitIMThrottle = false
  2266. that._logoutIM()
  2267. }).catch((imError) => {
  2268. that.config.exitIMThrottle = false
  2269. that._logoutIM()
  2270. })
  2271. }
  2272. }).catch((imError) => {
  2273. that.config.exitIMThrottle = false
  2274. // 查询异常直接登出
  2275. that._logoutIM()
  2276. })
  2277. },2000)
  2278. },
  2279. _searchGroup(params) {
  2280. if (!this.tim) {
  2281. return
  2282. }
  2283. console.log(TAG_NAME, '_searchGroup', params)
  2284. const tim = this.tim
  2285. const promise = tim.searchGroupByID(params.roomID + '')
  2286. promise.then(function(imResponse) {
  2287. // const group = imResponse.data.group // 群组信息
  2288. console.log(TAG_NAME, '_searchGroup success', imResponse)
  2289. }).catch(function(imError) {
  2290. console.warn(TAG_NAME, '_searchGroup fail,TIM 报错信息不影响后续逻辑,可以忽略', imError) // 搜素群组失败的相关信息
  2291. })
  2292. return promise
  2293. },
  2294. /**
  2295. * 创建 AVchatroom
  2296. * @param {*} params roomID
  2297. * @returns {Promise}
  2298. */
  2299. _createGroup(params) {
  2300. if (!this.tim) {
  2301. return
  2302. }
  2303. console.log(TAG_NAME, '_createGroup', params)
  2304. const promise = this.tim.createGroup({
  2305. groupID: params.roomID + '',
  2306. name: params.roomID + '',
  2307. type: IM_GROUP_TYPE,
  2308. })
  2309. promise.then((imResponse) => { // 创建成功
  2310. console.log(TAG_NAME, '_createGroup success', imResponse.data.group) // 创建的群的资料
  2311. }).catch((imError) => {
  2312. console.warn(TAG_NAME, '_createGroup error', imError) // 创建群组失败的相关信息
  2313. })
  2314. return promise
  2315. },
  2316. /**
  2317. * 进入 AVchatroom
  2318. * @param {*} params roomID
  2319. * @returns {Promise}
  2320. */
  2321. _joinGroup(params) {
  2322. if (!this.tim) {
  2323. return
  2324. }
  2325. console.log(TAG_NAME, '_joinGroup', params)
  2326. const promise = this.tim.joinGroup({ groupID: params.roomID + '', type: IM_GROUP_TYPE })
  2327. promise.then((imResponse) => {
  2328. switch (imResponse.data.status) {
  2329. case TIM.TYPES.JOIN_STATUS_WAIT_APPROVAL: // 等待管理员同意
  2330. break
  2331. case TIM.TYPES.JOIN_STATUS_SUCCESS: // 加群成功
  2332. case TIM.TYPES.JOIN_STATUS_ALREADY_IN_GROUP: // 已经在群中
  2333. // console.log(imResponse.data.group) // 加入的群组资料
  2334. // wx.showToast({
  2335. // title: '进群成功',
  2336. // })
  2337. console.log(TAG_NAME, '_joinGroup success', imResponse)
  2338. break
  2339. default:
  2340. break
  2341. }
  2342. }).catch((imError) => {
  2343. console.warn(TAG_NAME, 'joinGroup error', imError) // 申请加群失败的相关信息
  2344. })
  2345. return promise
  2346. },
  2347. _quitGroup(params) {
  2348. if (!this.tim) {
  2349. return
  2350. }
  2351. console.log(TAG_NAME, '_quitGroup', params)
  2352. const promise = this.tim.quitGroup(params.roomID + '')
  2353. promise.then((imResponse) => {
  2354. console.log(TAG_NAME, '_quitGroup success', imResponse)
  2355. }).catch((imError) => {
  2356. console.warn(TAG_NAME, 'quitGroup error', imError)
  2357. })
  2358. return promise
  2359. },
  2360. _dismissGroup(params) {
  2361. if (!this.tim) {
  2362. return
  2363. }
  2364. console.log(TAG_NAME, '_dismissGroup', params)
  2365. const promise = this.tim.dismissGroup(params.roomID + '')
  2366. promise.then((imResponse) => {
  2367. console.log(TAG_NAME, '_dismissGroup success', imResponse)
  2368. }).catch((imError) => {
  2369. console.warn(TAG_NAME, '_dismissGroup error', imError)
  2370. })
  2371. return promise
  2372. },
  2373. _onIMReady(event) {
  2374. console.log(TAG_NAME, 'IM.READY', event)
  2375. this._emitter.emit(EVENT.IM_READY, event)
  2376. const roomID = this.config.roomID
  2377. // 查询群组是否存在
  2378. this._searchGroup({ roomID }).then((res) => {
  2379. // console.log(TAG_NAME, 'searchGroup', res)
  2380. // 存在直接进群
  2381. this._joinGroup({ roomID })
  2382. }).catch(() => {
  2383. // 不存在则创建,如果是avchatroom 创建后进群
  2384. this._createGroup({ roomID }).then((res) => {
  2385. // 进群
  2386. this._joinGroup({ roomID })
  2387. }).catch((imError)=> {
  2388. if (imError.code === 10021) {
  2389. console.log(TAG_NAME, '群已存在,直接进群', event)
  2390. this._joinGroup({ roomID })
  2391. }
  2392. })
  2393. })
  2394. // 收到离线消息和会话列表同步完毕通知,接入侧可以调用 sendMessage 等需要鉴权的接口
  2395. // event.name - TIM.EVENT.IM_READY
  2396. },
  2397. _onIMMessageReceived(event) {
  2398. // 收到推送的单聊、群聊、群提示、群系统通知的新消息,可通过遍历 event.data 获取消息列表数据并渲染到页面
  2399. console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', event)
  2400. // messageList 仅保留10条消息
  2401. const messageData = event.data
  2402. const roomID = this.config.roomID + ''
  2403. const userID = this.config.userID + ''
  2404. for (let i = 0; i < messageData.length; i++) {
  2405. const message = messageData[i]
  2406. // console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', message, this.config, TIM.TYPES.MSG_TEXT)
  2407. if (message.to === roomID + '' || message.to === userID) {
  2408. // 遍历messageData 获取当前room 或者当前user的消息
  2409. console.log(TAG_NAME, 'IM.MESSAGE_RECEIVED', message, message.type, TIM.TYPES.MSG_TEXT)
  2410. if (message.type === TIM.TYPES.MSG_TEXT) {
  2411. this._pushMessageList({
  2412. name: message.from,
  2413. message: message.payload.text,
  2414. })
  2415. } else {
  2416. if (message.type === TIM.TYPES.MSG_GRP_SYS_NOTICE && message.payload.operationType === 2) {
  2417. // 群系统通知
  2418. this._pushMessageList({
  2419. name: '系统通知',
  2420. message: `欢迎 ${userID}`,
  2421. })
  2422. }
  2423. // 其他消息暂不处理
  2424. }
  2425. }
  2426. }
  2427. this._emitter.emit(EVENT.IM_MESSAGE_RECEIVED, event)
  2428. },
  2429. _onIMNotReady(event) {
  2430. console.log(TAG_NAME, 'IM.NOT_READY', event)
  2431. this._emitter.emit(EVENT.IM_NOT_READY, event)
  2432. // 收到 SDK 进入 not ready 状态通知,此时 SDK 无法正常工作
  2433. // event.name - TIM.EVENT.IM_NOT_READY
  2434. },
  2435. _onIMKickedOut(event) {
  2436. console.log(TAG_NAME, 'IM.KICKED_OUT', event)
  2437. this._emitter.emit(EVENT.IM_KICKED_OUT, event)
  2438. // 收到被踢下线通知
  2439. // event.name - TIM.EVENT.KICKED_OUT
  2440. // event.data.type - 被踢下线的原因,例如 :
  2441. // - TIM.TYPES.KICKED_OUT_MULT_ACCOUNT 多实例登录被踢
  2442. // - TIM.TYPES.KICKED_OUT_MULT_DEVICE 多终端登录被踢
  2443. // - TIM.TYPES.KICKED_OUT_USERSIG_EXPIRED 签名过期被踢。使用前需要将SDK版本升级至v2.4.0或以上。
  2444. },
  2445. _onIMError(event) {
  2446. console.log(TAG_NAME, 'IM.ERROR', event)
  2447. this._emitter.emit(EVENT.IM_ERROR, event)
  2448. // 收到 SDK 发生错误通知,可以获取错误码和错误信息
  2449. // event.name - TIM.EVENT.ERROR
  2450. // event.data.code - 错误码
  2451. // event.data.message - 错误信息
  2452. }
  2453. }
  2454. };
  2455. </script>
  2456. <style>
  2457. @import './trtc-room.css';
  2458. </style>