trtc-room.vue 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  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 Event from './utils/event';
  482. import * as ENV from './utils/environment';
  483. const TAG_NAME = 'TRTC-ROOM';
  484. export default {
  485. data() {
  486. return {
  487. pusher: null,
  488. // debugMode: false, // 是否开启调试模式
  489. debugPanel: true,
  490. // 是否打开组件调试面板
  491. debug: false,
  492. // 是否打开player pusher 的调试信息
  493. streamList: [],
  494. // 用于渲染player列表,存储stram
  495. userList: [],
  496. // 扁平化的数据用来返回给用户
  497. template: '',
  498. // 不能设置默认值,当默认值和传入组件的值不一致时,iOS渲染失败
  499. cameraPosition: '',
  500. panelName: '',
  501. // 控制面板名称,包括 setting-panel memberlist-panel
  502. localVolume: 0,
  503. remoteVolumeList: [],
  504. appVersion: ENV.APP_VERSION,
  505. libVersion: ENV.LIB_VERSION,
  506. debugMode: ''
  507. };
  508. },
  509. components: {},
  510. props: {
  511. // 必要的初始化参数
  512. config: {
  513. type: Object,
  514. default: () => ({
  515. sdkAppID: '',
  516. userID: '',
  517. userSig: '',
  518. template: '',
  519. debugMode: ''
  520. })
  521. }
  522. },
  523. watch: {
  524. streamList(newVal, oldVal){
  525. console.log('streamList::::::::',newVal, oldVal)
  526. this.streamList = newVal
  527. },
  528. config: {
  529. handler: function(newVal, oldVal) {
  530. console.log('watch config');
  531. this.propertyObserverFun({
  532. name: 'config',
  533. newVal,
  534. oldVal
  535. });
  536. },
  537. deep: true
  538. }
  539. },
  540. created: function() {
  541. // 在组件实例刚刚被创建时执行
  542. console.log(TAG_NAME, 'created', ENV);
  543. },
  544. beforeMount: function() {
  545. // 在组件实例进入页面节点树时执行
  546. console.log(TAG_NAME, 'attached');
  547. this.initFun();
  548. },
  549. mounted: function() {
  550. // 在组件在视图层布局完成后执行
  551. console.log(TAG_NAME, 'ready');
  552. },
  553. destroyed: function() {
  554. // 在组件实例被从页面节点树移除时执行
  555. console.log(TAG_NAME, 'detached'); // 停止所有拉流,并重置数据
  556. this.exitRoom();
  557. },
  558. error: function(error) {
  559. // 每当组件方法抛出错误时执行
  560. console.log(TAG_NAME, 'error', error);
  561. },
  562. onPageShow: function() {
  563. // 组件所在的页面被展示时执行
  564. console.log(TAG_NAME, 'show status:', this.status);
  565. if (this.status.isPending) {
  566. // 经历了 5000 挂起事件
  567. this.status.isPending = false;
  568. }
  569. if (this.status.isPush) {
  570. // 小程序hide - show 有一定概率本地黑屏或静止,远端正常,或者远端和本地同时黑屏或静止,需要手动启动预览,非必现
  571. // this.data.pusher.getPusherContext().startPreview()
  572. // this.data.pusher.getPusherContext().resume()
  573. }
  574. },
  575. onPageHide: function() {
  576. // 组件所在的页面被隐藏时执行
  577. console.log(TAG_NAME, 'hide');
  578. },
  579. onPageResize: function(size) {
  580. // 组件所在的页面尺寸变化时执行
  581. console.log(TAG_NAME, 'resize', size);
  582. },
  583. methods: {
  584. setData,
  585. /**
  586. * 初始化各项参数和用户控制模块,在组件实例触发 attached 时调用,此时不建议对View进行变更渲染(调用setData方法)
  587. */
  588. initFun: function() {
  589. console.log(TAG_NAME, '_init');
  590. this.userController = new UserController(this);
  591. this._emitter = new Event();
  592. this.EVENT = EVENT;
  593. this.initStatusFun();
  594. this.bindEventFun();
  595. this.bindEventGridFun();
  596. console.log(TAG_NAME, '_init success component:', this);
  597. },
  598. /**
  599. * 进房
  600. * @param {Object} params 必传 roomID 取值范围 1 ~ 4294967295
  601. * @returns {Promise}
  602. */
  603. enterRoom: function(params) {
  604. return new Promise((resolve, reject) => {
  605. console.log(TAG_NAME, 'enterRoom');
  606. console.log(TAG_NAME, 'params', params);
  607. console.log(TAG_NAME, 'config', this.config);
  608. console.log(TAG_NAME, 'pusher', this.pusher); // 1. 补齐进房参数,校验必要参数是否齐全
  609. console.log('进房......', params, this.config, this.pusher);
  610. if (params) {
  611. Object.assign(this.pusher, params);
  612. Object.assign(this.config, params);
  613. }
  614. if (!this.checkParamFun(this.config)) {
  615. reject(new Error('缺少必要参数'));
  616. return;
  617. } // 2. 根据参数拼接 push url,赋值给 live-pusher,
  618. this.getPushUrlFun(this.config)
  619. .then(pushUrl => {
  620. this.pusher.url = pushUrl;
  621. this.setData(
  622. {
  623. pusher: this.pusher
  624. },
  625. () => {
  626. console.log(TAG_NAME, 'enterRoom success', this.pusher); // view 渲染成功回调后,开始推流
  627. this.pusher.getPusherContext().start();
  628. this.status.isPush = true;
  629. resolve();
  630. }
  631. );
  632. })
  633. .catch(res => {
  634. // 获取 room sig 失败, 进房失败需要通过 pusher state 事件通知
  635. console.error(TAG_NAME, 'enterRoom fail', res);
  636. reject(res);
  637. });
  638. });
  639. },
  640. /**
  641. * 退房,停止推流和拉流,并重置数据
  642. * @returns {Promise}
  643. */
  644. exitRoom: function() {
  645. return new Promise((resolve, reject) => {
  646. console.log(TAG_NAME, 'exitRoom');
  647. this.pusher.reset();
  648. this.status.isPush = false;
  649. const result = this.userController.reset();
  650. this.setData(
  651. {
  652. pusher: this.pusher,
  653. userList: result.userList,
  654. streamList: result.streamList
  655. },
  656. () => {
  657. // 在销毁页面时调用,不会走到这里
  658. resolve({
  659. userList: this.userList,
  660. streamList: this.streamList
  661. });
  662. console.log(TAG_NAME, 'exitRoom success', this.pusher, this.streamList, this.userList);
  663. }
  664. );
  665. });
  666. },
  667. /**
  668. * 开启摄像头
  669. * @returns {Promise}
  670. */
  671. publishLocalVideo: function() {
  672. // 设置 pusher enableCamera
  673. console.log(TAG_NAME, 'publishLocalVideo 开启摄像头');
  674. return this.setPusherConfigFun({
  675. enableCamera: true
  676. });
  677. },
  678. /**
  679. * 关闭摄像头
  680. * @returns {Promise}
  681. */
  682. unpublishLocalVideo: function() {
  683. // 设置 pusher enableCamera
  684. console.log(TAG_NAME, 'unpublshLocalVideo 关闭摄像头');
  685. return this.setPusherConfigFun({
  686. enableCamera: false
  687. });
  688. },
  689. /**
  690. * 开启麦克风
  691. * @returns {Promise}
  692. */
  693. publishLocalAudio: function() {
  694. // 设置 pusher enableCamera
  695. console.log(TAG_NAME, 'publishLocalAudio 开启麦克风');
  696. return this.setPusherConfigFun({
  697. enableMic: true
  698. });
  699. },
  700. /**
  701. * 关闭麦克风
  702. * @returns {Promise}
  703. */
  704. unpublishLocalAudio: function() {
  705. // 设置 pusher enableCamera
  706. console.log(TAG_NAME, 'unpublshLocalAudio 关闭麦克风');
  707. return this.setPusherConfigFun({
  708. enableMic: false
  709. });
  710. },
  711. /**
  712. * 订阅远端视频 主流 小画面 辅流
  713. * @param {Object} params {userID,streamType} streamType 传入 small 时修改对应的主流url的 streamtype 参数为small
  714. * @returns {Promise}
  715. */
  716. subscribeRemoteVideo(params) {
  717. console.log(TAG_NAME, 'subscribeRemoteVideo', params); // 设置指定 user streamType 的 muteVideo 为 false
  718. const config = {
  719. muteVideo: false
  720. }; // 本地数据结构里的 streamType 只支持 main 和 aux ,订阅small 也是对main进行处理
  721. const streamType = params.streamType === 'small' ? 'main' : params.streamType;
  722. if (params.streamType === 'small' || params.streamType === 'main') {
  723. const stream = this.userController.getStream({
  724. userID: params.userID,
  725. streamType: streamType
  726. });
  727. if (stream && stream.streamType === 'main') {
  728. console.log(TAG_NAME, 'subscribeRemoteVideo switch small', stream.src);
  729. if (params.streamType === 'small') {
  730. config.src = stream.src.replace('main', 'small');
  731. config._definitionType = 'small'; // 用于设置面板的渲染
  732. } else if (params.streamType === 'main') {
  733. stream.src = stream.src.replace('small', 'main');
  734. config._definitionType = 'main';
  735. }
  736. console.log(TAG_NAME, 'subscribeRemoteVideo', stream.src);
  737. }
  738. }
  739. return this.setPlayerConfigFun({
  740. userID: params.userID,
  741. streamType: streamType,
  742. config: config
  743. });
  744. },
  745. /**
  746. * 取消订阅远端视频
  747. * @param {Object} params {userID,streamType}
  748. * @returns {Promise}
  749. */
  750. unsubscribeRemoteVideo(params) {
  751. console.log(TAG_NAME, 'unsubscribeRemoteVideo', params); // 设置指定 user streamType 的 muteVideo 为 true
  752. return this.setPlayerConfigFun({
  753. userID: params.userID,
  754. streamType: params.streamType,
  755. config: {
  756. muteVideo: true
  757. }
  758. });
  759. },
  760. /**
  761. * 订阅远端音频
  762. * @param {Object} params userID 用户ID
  763. * @returns {Promise}
  764. */
  765. subscribeRemoteAudio(params) {
  766. console.log(TAG_NAME, 'subscribeRemoteAudio', params);
  767. return this.setPlayerConfigFun({
  768. userID: params.userID,
  769. streamType: 'main',
  770. config: {
  771. muteAudio: false
  772. }
  773. });
  774. },
  775. /**
  776. * 取消订阅远端音频
  777. * @param {Object} params userID 用户ID
  778. * @returns {Promise}
  779. */
  780. unsubscribeRemoteAudio(params) {
  781. console.log(TAG_NAME, 'unsubscribeRemoteAudio', params);
  782. return this.setPlayerConfigFun({
  783. userID: params.userID,
  784. streamType: 'main',
  785. config: {
  786. muteAudio: true
  787. }
  788. });
  789. },
  790. on: function(eventCode, handler, context) {
  791. this._emitter.on(eventCode, handler, context);
  792. },
  793. off: function(eventCode, handler) {
  794. this._emitter.off(eventCode, handler);
  795. },
  796. getRemoteUserList: function() {
  797. return this.userList;
  798. },
  799. /**
  800. * 切换前后摄像头
  801. */
  802. switchCamera: function() {
  803. if (!this.cameraPosition) {
  804. // this.data.pusher.cameraPosition 是初始值,不支持动态设置
  805. this.cameraPosition = this.pusher.frontCamera;
  806. }
  807. console.log(TAG_NAME, 'switchCamera', this.cameraPosition);
  808. this.cameraPosition = this.cameraPosition === 'front' ? 'back' : 'front';
  809. this.setData(
  810. {
  811. cameraPosition: this.cameraPosition
  812. },
  813. () => {
  814. console.log(TAG_NAME, 'switchCamera success', this.cameraPosition);
  815. }
  816. ); // wx 7.0.9 不支持动态设置 pusher.devicePosition ,需要调用api设置,这里修改cameraPosition是为了记录状态
  817. this.pusher.getPusherContext().switchCamera();
  818. },
  819. /**
  820. * 设置指定player view的渲染坐标和尺寸
  821. * @param {object} params
  822. * userID: string
  823. * streamType: string
  824. * xAxis: number
  825. * yAxis: number
  826. * width: number
  827. * height: number
  828. * @returns {Promise}
  829. */
  830. setViewRect: function(params) {
  831. console.log(TAG_NAME, 'setViewRect', params);
  832. if (this.pusher.template !== 'custom') {
  833. console.warn(`如需使用setViewRect方法,请设置template:"custom", 当前 template:"${this.pusher.template}"`);
  834. }
  835. if (this.pusher.userID === params.userID) {
  836. return this.setPusherConfigFun({
  837. xAxis: params.xAxis,
  838. yAxis: params.yAxis,
  839. width: params.width,
  840. height: params.height
  841. });
  842. }
  843. return this.setPlayerConfigFun({
  844. userID: params.userID,
  845. streamType: params.streamType,
  846. config: {
  847. xAxis: params.xAxis,
  848. yAxis: params.yAxis,
  849. width: params.width,
  850. height: params.height
  851. }
  852. });
  853. },
  854. /**
  855. * 设置指定 player 或者 pusher view 是否可见
  856. * @param {object} params
  857. * userID: string
  858. * streamType: string
  859. * isVisible:boolean
  860. * @returns {Promise}
  861. */
  862. setViewVisible: function(params) {
  863. console.log(TAG_NAME, 'setViewVisible', params); // if (this.data.pusher.template !== 'custom') {
  864. // console.warn(`如需使用setViewVisible方法,请设置template:"custom", 当前 template:"${this.data.pusher.template}"`)
  865. // }
  866. if (this.pusher.userID === params.userID) {
  867. return this.setPusherConfigFun({
  868. isVisible: params.isVisible
  869. });
  870. }
  871. return this.setPlayerConfigFun({
  872. userID: params.userID,
  873. streamType: params.streamType,
  874. config: {
  875. isVisible: params.isVisible
  876. }
  877. });
  878. },
  879. /**
  880. * 设置指定player view的层级
  881. * @param {Object} params
  882. * userID: string
  883. * streamType: string
  884. * zindex: number
  885. * @returns {Promise}
  886. */
  887. setViewZIndex: function(params) {
  888. console.log(TAG_NAME, 'setViewZIndex', params);
  889. if (this.pusher.template !== 'custom') {
  890. console.warn(`如需使用setViewZIndex方法,请设置template:"custom", 当前 template:"${this.pusher.template}"`);
  891. }
  892. if (this.pusher.userID === params.userID) {
  893. return this.setPusherConfigFun({
  894. zindex: params.zindex
  895. });
  896. }
  897. return this.setPlayerConfigFun({
  898. userID: params.userID,
  899. streamType: params.streamType,
  900. config: {
  901. zindex: params.zindex
  902. }
  903. });
  904. },
  905. /**
  906. * 播放背景音
  907. * @param {Object} params url
  908. * @returns {Promise}
  909. */
  910. playBGM: function(params) {
  911. return new Promise((resolve, reject) => {
  912. this.pusher.getPusherContext().playBGM({
  913. url: params.url,
  914. // 已经有相关事件不需要在这里监听,目前用于测试
  915. success: () => {
  916. console.log(TAG_NAME, '播放背景音成功'); // this._emitter.emit(EVENT.BGM_PLAY_START)
  917. resolve();
  918. },
  919. fail: () => {
  920. console.log(TAG_NAME, '播放背景音失败');
  921. this._emitter.emit(EVENT.BGM_PLAY_FAIL);
  922. reject(new Error('播放背景音失败'));
  923. } // complete: () => {
  924. // console.log(TAG_NAME, '背景完成')
  925. // this._emitter.emit(EVENT.BGM_PLAY_COMPLETE)
  926. // },
  927. });
  928. });
  929. },
  930. stopBGM: function() {
  931. this.pusher.getPusherContext().stopBGM();
  932. },
  933. pauseBGM: function() {
  934. this.pusher.getPusherContext().pauseBGM();
  935. },
  936. resumeBGM: function() {
  937. this.pusher.getPusherContext().resumeBGM();
  938. },
  939. /**
  940. * 设置背景音音量
  941. * @param {Object} params volume
  942. */
  943. setBGMVolume: function(params) {
  944. this.pusher.getPusherContext().setBGMVolume({
  945. volume: params.volume
  946. });
  947. },
  948. /**
  949. * 设置麦克风音量
  950. * @param {Object} params volume
  951. */
  952. setMICVolume: function(params) {
  953. this.pusher.getPusherContext().setMICVolume({
  954. volume: params.volume
  955. });
  956. },
  957. /**
  958. * 发送SEI消息
  959. * @param {Object} params message
  960. * @returns {Promise}
  961. */
  962. sendSEI: function(params) {
  963. return new Promise((resolve, reject) => {
  964. this.pusher.getPusherContext().sendMessage({
  965. msg: params.message,
  966. success: function(result) {
  967. resolve(result);
  968. }
  969. });
  970. });
  971. },
  972. /**
  973. * pusher 和 player 的截图并保存
  974. * @param {Object} params userID streamType
  975. * @returns {Promise}
  976. */
  977. snapshot: function(params) {
  978. console.log(TAG_NAME, 'snapshot', params);
  979. return new Promise((resolve, reject) => {
  980. this.captureSnapshot(params)
  981. .then(result => {
  982. wx.saveImageToPhotosAlbum({
  983. filePath: result.tempImagePath,
  984. success(res) {
  985. wx.showToast({
  986. title: '已保存到相册'
  987. });
  988. console.log('save photo is success', res);
  989. resolve(result);
  990. },
  991. fail: function(error) {
  992. wx.showToast({
  993. icon: 'none',
  994. title: '保存失败'
  995. });
  996. console.log('save photo is fail', error);
  997. reject(error);
  998. }
  999. });
  1000. })
  1001. .catch(error => {
  1002. reject(error);
  1003. });
  1004. });
  1005. },
  1006. /**
  1007. * 获取pusher 和 player 的截图
  1008. * @param {Object} params userID streamType
  1009. * @returns {Promise}
  1010. */
  1011. captureSnapshot: function(params) {
  1012. return new Promise((resolve, reject) => {
  1013. if (params.userID === this.pusher.userID) {
  1014. // pusher
  1015. this.pusher.getPusherContext().snapshot({
  1016. quality: 'raw',
  1017. complete: result => {
  1018. console.log(TAG_NAME, 'snapshot pusher', result);
  1019. if (result.tempImagePath) {
  1020. resolve(result);
  1021. } else {
  1022. console.log('snapShot 回调失败', result);
  1023. reject(new Error('截图失败'));
  1024. }
  1025. }
  1026. });
  1027. } else {
  1028. // player
  1029. this.userController.getStream(params).playerContext.snapshot({
  1030. quality: 'raw',
  1031. complete: result => {
  1032. console.log(TAG_NAME, 'snapshot player', result);
  1033. if (result.tempImagePath) {
  1034. resolve(result);
  1035. } else {
  1036. console.log('snapShot 回调失败', result);
  1037. reject(new Error('截图失败'));
  1038. }
  1039. }
  1040. });
  1041. }
  1042. });
  1043. },
  1044. /**
  1045. * 将远端视频全屏
  1046. * @param {Object} params userID streamType direction
  1047. * @returns {Promise}
  1048. */
  1049. enterFullscreen: function(params) {
  1050. console.log(TAG_NAME, 'enterFullscreen', params);
  1051. return new Promise((resolve, reject) => {
  1052. this.userController.getStream(params).playerContext.requestFullScreen({
  1053. direction: params.direction || 0,
  1054. success: event => {
  1055. console.log(TAG_NAME, 'enterFullscreen success', event);
  1056. resolve(event);
  1057. },
  1058. fail: event => {
  1059. console.log(TAG_NAME, 'enterFullscreen fail', event);
  1060. reject(event);
  1061. }
  1062. });
  1063. });
  1064. },
  1065. /**
  1066. * 将远端视频取消全屏
  1067. * @param {Object} params userID streamType
  1068. * @returns {Promise}
  1069. */
  1070. exitFullscreen: function(params) {
  1071. console.log(TAG_NAME, 'exitFullscreen', params);
  1072. return new Promise((resolve, reject) => {
  1073. this.userController.getStream(params).playerContext.exitFullScreen({
  1074. success: event => {
  1075. console.log(TAG_NAME, 'exitFullScreen success', event);
  1076. resolve(event);
  1077. },
  1078. fail: event => {
  1079. console.log(TAG_NAME, 'exitFullScreen fail', event);
  1080. reject(event);
  1081. }
  1082. });
  1083. });
  1084. },
  1085. /**
  1086. * 设置 player 视图的横竖屏显示
  1087. * @param {Object} params userID streamType orientation: vertical, horizontal
  1088. * @returns {Promise}
  1089. */
  1090. setRemoteOrientation: function(params) {
  1091. return this.setPlayerConfigFun({
  1092. userID: params.userID,
  1093. streamType: params.streamType,
  1094. config: {
  1095. orientation: params.orientation
  1096. }
  1097. });
  1098. },
  1099. // 改为:
  1100. setViewOrientation: function(params) {
  1101. return this.setPlayerConfigFun({
  1102. userID: params.userID,
  1103. streamType: params.streamType,
  1104. config: {
  1105. orientation: params.orientation
  1106. }
  1107. });
  1108. },
  1109. /**
  1110. * 设置 player 视图的填充模式
  1111. * @param {Object} params userID streamType fillMode: contain,fillCrop
  1112. * @returns {Promise}
  1113. */
  1114. setRemoteFillMode: function(params) {
  1115. return this.setPlayerConfigFun({
  1116. userID: params.userID,
  1117. streamType: params.streamType,
  1118. config: {
  1119. objectFit: params.fillMode
  1120. }
  1121. });
  1122. },
  1123. // 改为:
  1124. setViewFillMode: function(params) {
  1125. return this.setPlayerConfigFun({
  1126. userID: params.userID,
  1127. streamType: params.streamType,
  1128. config: {
  1129. objectFit: params.fillMode
  1130. }
  1131. });
  1132. },
  1133. /**
  1134. * 切换 player 大小画面
  1135. * @param {Object} params userID streamType definition: HD SD
  1136. * @returns {Promise}
  1137. */
  1138. setRemoteDefinitionFun: function(params) {
  1139. params.streamType = 'main';
  1140. return new Promise((resolve, reject) => {
  1141. const stream = this.userController.getStream({
  1142. userID: params.userID,
  1143. streamType: params.streamType
  1144. });
  1145. if (stream && stream.streamType === 'main') {
  1146. console.log(TAG_NAME, '_switchStreamType', stream.src); // stream.volume = volume
  1147. if (stream.src.indexOf('main') > -1) {
  1148. stream.src = stream.src.replace('main', 'small');
  1149. stream._streamType = 'small'; // 用于设置面板的渲染
  1150. } else if (stream.src.indexOf('small') > -1) {
  1151. stream.src = stream.src.replace('small', 'main');
  1152. stream._streamType = 'main';
  1153. }
  1154. console.log(TAG_NAME, '_switchStreamType', stream.src);
  1155. this.setData(
  1156. {
  1157. streamList: this.streamList
  1158. },
  1159. () => {}
  1160. );
  1161. }
  1162. });
  1163. },
  1164. initStatusFun() {
  1165. this.status = {
  1166. isPush: false,
  1167. // 推流状态
  1168. isPending: false // 挂起状态,触发5000事件标记为true,onShow后标记为false
  1169. };
  1170. this._lastTapTime = 0;
  1171. this._beforeLastTapTime = 0;
  1172. this._isFullscreen = false;
  1173. },
  1174. /**
  1175. * 设置推流参数并触发页面渲染更新
  1176. * @param {Object} config live-pusher 的配置
  1177. * @returns {Promise}
  1178. */
  1179. setPusherConfigFun(config) {
  1180. console.log(TAG_NAME, '_setPusherConfig', config, this.pusher);
  1181. return new Promise((resolve, reject) => {
  1182. if (!this.pusher) {
  1183. this.pusher = new Pusher(config);
  1184. } else {
  1185. Object.assign(this.pusher, config);
  1186. }
  1187. this.setData(
  1188. {
  1189. pusher: this.pusher
  1190. },
  1191. () => {
  1192. // console.log(TAG_NAME, '_setPusherConfig setData compelete', 'config:', config, 'pusher:', this.data.pusher)
  1193. resolve(config);
  1194. }
  1195. );
  1196. });
  1197. },
  1198. /**
  1199. *
  1200. * @param {Object} params include userID,streamType,config
  1201. * @returns {Promise}
  1202. */
  1203. setPlayerConfigFun(params) {
  1204. const userID = params.userID;
  1205. const streamType = params.streamType;
  1206. const config = params.config;
  1207. console.log(TAG_NAME, '_setPlayerConfig', params);
  1208. return new Promise((resolve, reject) => {
  1209. // 获取指定的userID streamType 的 stream
  1210. const user = this.userController.getUser(userID);
  1211. if (user && user.streams[streamType]) {
  1212. user.streams[streamType] = Object.assign(user.streams[streamType], config); // user.streams引用的对象和 streamList 里的是同一个
  1213. this.setData(
  1214. {
  1215. streamList: this.streamList
  1216. },
  1217. () => {
  1218. // console.log(TAG_NAME, '_setPlayerConfig complete', params, 'streamList:', this.data.streamList)
  1219. resolve(params);
  1220. }
  1221. );
  1222. } else {
  1223. // 不需要reject,静默处理
  1224. console.warn(TAG_NAME, '指定 userID 或者 streamType 不存在'); // reject(new Error('指定 userID 或者 streamType 不存在'))
  1225. }
  1226. });
  1227. },
  1228. /**
  1229. * 必选参数检测
  1230. * @param {Object} rtcConfig rtc参数
  1231. * @returns {Boolean}
  1232. */
  1233. checkParamFun: function(rtcConfig) {
  1234. console.log(TAG_NAME, 'checkParam config:', rtcConfig);
  1235. if (!rtcConfig.sdkAppID) {
  1236. console.error('未设置 sdkAppID');
  1237. return false;
  1238. }
  1239. if (rtcConfig.roomID === undefined) {
  1240. console.error('未设置 roomID');
  1241. return false;
  1242. }
  1243. if (rtcConfig.roomID < 1 || rtcConfig.roomID > 4294967296) {
  1244. console.error('roomID 超出取值范围 1 ~ 4294967295');
  1245. return false;
  1246. }
  1247. if (!rtcConfig.userID) {
  1248. console.error('未设置 userID');
  1249. return false;
  1250. }
  1251. if (!rtcConfig.userSig) {
  1252. console.error('未设置 userSig');
  1253. return false;
  1254. }
  1255. if (!rtcConfig.template) {
  1256. console.error('未设置 template');
  1257. return false;
  1258. }
  1259. return true;
  1260. },
  1261. getPushUrlFun: function(rtcConfig) {
  1262. // 拼接 puhser url rtmp 方案
  1263. console.log(TAG_NAME, 'getPushUrl', rtcConfig);
  1264. if (ENV.IS_TRTC) {
  1265. // 版本高于7.0.8,基础库版本高于2.10.0 使用新的 url
  1266. return new Promise((resolve, reject) => {
  1267. // appscene videocall live
  1268. // cloudenv PRO CCC DEV UAT
  1269. // encsmall 0
  1270. // 对外的默认值是rtc ,对内的默认值是videocall
  1271. rtcConfig.scene = !rtcConfig.scene || rtcConfig.scene === 'rtc' ? 'videocall' : 'live';
  1272. rtcConfig.enableBlackStream = rtcConfig.enableBlackStream || 1;
  1273. rtcConfig.encsmall = rtcConfig.encsmall || 0;
  1274. rtcConfig.cloudenv = rtcConfig.cloudenv || 'PRO';
  1275. setTimeout(() => {
  1276. const pushUrl =
  1277. 'room://cloud.tencent.com/rtc?sdkappid=' +
  1278. rtcConfig.sdkAppID +
  1279. '&roomid=' +
  1280. rtcConfig.roomID +
  1281. '&userid=' +
  1282. rtcConfig.userID +
  1283. '&usersig=' +
  1284. rtcConfig.userSig +
  1285. '&appscene=' +
  1286. rtcConfig.scene +
  1287. '&encsmall=' +
  1288. rtcConfig.encsmall +
  1289. '&cloudenv=' +
  1290. rtcConfig.cloudenv;
  1291. console.log(TAG_NAME, 'getPushUrl result:', pushUrl);
  1292. resolve(pushUrl);
  1293. }, 0);
  1294. });
  1295. }
  1296. return this.requestSigServerFun(rtcConfig);
  1297. },
  1298. /**
  1299. * 获取签名和推流地址
  1300. * @param {Object} rtcConfig 进房参数配置
  1301. * @returns {Promise}
  1302. */
  1303. requestSigServerFun: function(rtcConfig) {
  1304. console.log('requestSigServer:', rtcConfig);
  1305. const sdkAppID = rtcConfig.sdkAppID;
  1306. const userID = rtcConfig.userID;
  1307. const userSig = rtcConfig.userSig;
  1308. const roomID = rtcConfig.roomID;
  1309. const privateMapKey = rtcConfig.privateMapKey;
  1310. rtcConfig.useCloud = rtcConfig.useCloud === undefined ? true : rtcConfig.useCloud;
  1311. let url = rtcConfig.useCloud ? 'https://official.opensso.tencent-cloud.com/v4/openim/jsonvideoapp' : 'https://yun.tim.qq.com/v4/openim/jsonvideoapp';
  1312. url += '?sdkappid=' + sdkAppID + '&identifier=' + userID + '&usersig=' + userSig + '&random=' + Date.now() + '&contenttype=json';
  1313. const reqHead = {
  1314. Cmd: 1,
  1315. SeqNo: 1,
  1316. BusType: 7,
  1317. GroupId: roomID
  1318. };
  1319. const reqBody = {
  1320. PrivMapEncrypt: privateMapKey,
  1321. TerminalType: 1,
  1322. FromType: 3,
  1323. SdkVersion: 26280566
  1324. };
  1325. console.log('requestSigServer:', url, reqHead, reqBody);
  1326. return new Promise((resolve, reject) => {
  1327. wx.request({
  1328. url: url,
  1329. data: {
  1330. ReqHead: reqHead,
  1331. ReqBody: reqBody
  1332. },
  1333. method: 'POST',
  1334. success: res => {
  1335. console.log('requestSigServer success:', res);
  1336. if (res.data['ErrorCode'] || res.data['RspHead']['ErrorCode'] !== 0) {
  1337. // console.error(res.data['ErrorInfo'] || res.data['RspHead']['ErrorInfo'])
  1338. console.error('获取roomsig失败');
  1339. reject(res);
  1340. }
  1341. const roomSig = JSON.stringify(res.data['RspBody']);
  1342. let pushUrl = 'room://cloud.tencent.com?sdkappid=' + sdkAppID + '&roomid=' + roomID + '&userid=' + userID + '&roomsig=' + encodeURIComponent(roomSig); // TODO 需要重新整理的逻辑
  1343. // 如果有配置纯音频推流或者recordId参数
  1344. if (rtcConfig.pureAudioPushMod || rtcConfig.recordId) {
  1345. const bizbuf = {
  1346. Str_uc_params: {
  1347. pure_audio_push_mod: 0,
  1348. record_id: 0
  1349. }
  1350. }; // 纯音频推流
  1351. if (rtcConfig.pureAudioPushMod) {
  1352. bizbuf.Str_uc_params.pure_audio_push_mod = rtcConfig.pureAudioPushMod;
  1353. } else {
  1354. delete bizbuf.Str_uc_params.pure_audio_push_mod;
  1355. } // 自动录制时业务自定义id
  1356. if (rtcConfig.recordId) {
  1357. bizbuf.Str_uc_params.record_id = rtcConfig.recordId;
  1358. } else {
  1359. delete bizbuf.Str_uc_params.record_id;
  1360. }
  1361. pushUrl += '&bizbuf=' + encodeURIComponent(JSON.stringify(bizbuf));
  1362. }
  1363. console.log('roomSigInfo', pushUrl);
  1364. resolve(pushUrl);
  1365. },
  1366. fail: res => {
  1367. console.log('requestSigServer fail:', res);
  1368. reject(res);
  1369. }
  1370. });
  1371. });
  1372. },
  1373. doubleTabToggleFullscreenFun: function(event) {
  1374. const curTime = event.timeStamp;
  1375. const lastTime = this._lastTapTime; // 已知问题:上次全屏操作后,必须等待1.5s后才能再次进行全屏操作,否则引发SDK全屏异常,因此增加节流逻辑
  1376. const beforeLastTime = this._beforeLastTapTime;
  1377. console.log(TAG_NAME, 'doubleTabToggleFullscreenFun', event, lastTime, beforeLastTime);
  1378. if (curTime - lastTime > 0 && curTime - lastTime < 300 && lastTime - beforeLastTime > 1500) {
  1379. const userID = event.currentTarget.dataset.userid;
  1380. const streamType = event.currentTarget.dataset.streamtype;
  1381. if (this._isFullscreen) {
  1382. this.exitFullscreen({
  1383. userID,
  1384. streamType
  1385. })
  1386. .then(() => {
  1387. this._isFullscreen = false;
  1388. })
  1389. .catch(() => {});
  1390. } else {
  1391. // const stream = this.userController.getStream({ userID, streamType })
  1392. let direction; // // 已知问题:视频的尺寸需要等待player触发NetStatus事件才能获取到,如果进房就双击全屏,全屏后的方向有可能不对。
  1393. // if (stream && stream.videoWidth && stream.videoHeight) {
  1394. // // 如果是横视频,全屏时进行横屏处理。如果是竖视频,则为0
  1395. // direction = stream.videoWidth > stream.videoHeight ? 90 : 0
  1396. // }
  1397. this.enterFullscreen({
  1398. userID,
  1399. streamType,
  1400. direction
  1401. })
  1402. .then(() => {
  1403. this._isFullscreen = true;
  1404. })
  1405. .catch(() => {});
  1406. }
  1407. this._beforeLastTapTime = lastTime;
  1408. }
  1409. this._lastTapTime = curTime;
  1410. },
  1411. /**
  1412. * TRTC-room 远端用户和音视频状态处理
  1413. */
  1414. bindEventFun: function() {
  1415. // 远端用户进房
  1416. this.userController.on(EVENT.REMOTE_USER_JOIN, event => {
  1417. console.log(TAG_NAME, '远端用户进房', event, event.data.userID);
  1418. this.setData(
  1419. {
  1420. userList: event.data.userList
  1421. },
  1422. () => {
  1423. this._emitter.emit(EVENT.REMOTE_USER_JOIN, {
  1424. userID: event.data.userID
  1425. });
  1426. }
  1427. );
  1428. console.log(TAG_NAME, 'REMOTE_USER_JOIN', 'streamList:', this.streamList, 'userList:', this.userList);
  1429. }); // 远端用户离开
  1430. this.userController.on(EVENT.REMOTE_USER_LEAVE, event => {
  1431. console.log(TAG_NAME, '远端用户离开', event, event.data.userID);
  1432. if (event.data.userID) {
  1433. this.setData(
  1434. {
  1435. userList: event.data.userList,
  1436. streamList: event.data.streamList
  1437. },
  1438. () => {
  1439. this._emitter.emit(EVENT.REMOTE_USER_LEAVE, {
  1440. userID: event.data.userID
  1441. });
  1442. }
  1443. );
  1444. }
  1445. console.log(TAG_NAME, 'REMOTE_USER_LEAVE', 'streamList:', this.streamList, 'userList:', this.userList);
  1446. }); // 视频状态 true
  1447. this.userController.on(EVENT.REMOTE_VIDEO_ADD, event => {
  1448. console.log(TAG_NAME, '远端视频可用', event, event.data.stream.userID);
  1449. const stream = event.data.stream;
  1450. this.setData(
  1451. {
  1452. userList: event.data.userList,
  1453. streamList: event.data.streamList
  1454. },
  1455. () => {
  1456. // 完善 的stream 的 playerContext
  1457. stream.playerContext = wx.createLivePlayerContext(stream.streamID, this); // 新增的需要触发一次play 默认属性才能生效
  1458. // stream.playerContext.play()
  1459. // console.log(TAG_NAME, 'REMOTE_VIDEO_ADD playerContext.play()', stream)
  1460. // TODO 视频通话模版默认订阅且显示
  1461. this._emitter.emit(EVENT.REMOTE_VIDEO_ADD, {
  1462. userID: stream.userID,
  1463. streamType: stream.streamType
  1464. });
  1465. }
  1466. );
  1467. console.log(TAG_NAME, 'REMOTE_VIDEO_ADD', 'streamList:', this.streamList, 'userList:', this.userList);
  1468. }); // 视频状态 false
  1469. this.userController.on(EVENT.REMOTE_VIDEO_REMOVE, event => {
  1470. console.log(TAG_NAME, '远端视频移除', event, event.data.stream.userID);
  1471. const stream = event.data.stream;
  1472. this.setData(
  1473. {
  1474. userList: event.data.userList,
  1475. streamList: event.data.streamList
  1476. },
  1477. () => {
  1478. // 有可能先触发了退房事件,用户名下的所有stream都已清除
  1479. if (stream.userID && stream.streamType) {
  1480. this._emitter.emit(EVENT.REMOTE_VIDEO_REMOVE, {
  1481. userID: stream.userID,
  1482. streamType: stream.streamType
  1483. });
  1484. }
  1485. }
  1486. );
  1487. console.log(TAG_NAME, 'REMOTE_VIDEO_REMOVE', 'streamList:', this.streamList, 'userList:', this.userList);
  1488. }); // 音频可用
  1489. this.userController.on(EVENT.REMOTE_AUDIO_ADD, event => {
  1490. console.log(TAG_NAME, '远端音频可用', event);
  1491. const stream = event.data.stream;
  1492. this.setData(
  1493. {
  1494. userList: event.data.userList,
  1495. streamList: event.data.streamList
  1496. },
  1497. () => {
  1498. stream.playerContext = wx.createLivePlayerContext(stream.streamID, this); // 新增的需要触发一次play 默认属性才能生效
  1499. // stream.playerContext.play()
  1500. // console.log(TAG_NAME, 'REMOTE_AUDIO_ADD playerContext.play()', stream)
  1501. this._emitter.emit(EVENT.REMOTE_AUDIO_ADD, {
  1502. userID: stream.userID,
  1503. streamType: stream.streamType
  1504. });
  1505. }
  1506. );
  1507. console.log(TAG_NAME, 'REMOTE_AUDIO_ADD', 'streamList:', this.streamList, 'userList:', this.userList);
  1508. }); // 音频不可用
  1509. this.userController.on(EVENT.REMOTE_AUDIO_REMOVE, event => {
  1510. console.log(TAG_NAME, '远端音频移除', event, event.data.stream.userID);
  1511. const stream = event.data.stream;
  1512. this.setData(
  1513. {
  1514. userList: event.data.userList,
  1515. streamList: event.data.streamList
  1516. },
  1517. () => {
  1518. // 有可能先触发了退房事件,用户名下的所有stream都已清除
  1519. if (stream.userID && stream.streamType) {
  1520. this._emitter.emit(EVENT.REMOTE_AUDIO_REMOVE, {
  1521. userID: stream.userID,
  1522. streamType: stream.streamType
  1523. });
  1524. }
  1525. }
  1526. );
  1527. console.log(TAG_NAME, 'REMOTE_AUDIO_REMOVE', 'streamList:', this.streamList, 'userList:', this.userList);
  1528. });
  1529. },
  1530. /**
  1531. * pusher event handler
  1532. * @param {*} event 事件实例
  1533. */
  1534. pusherStateChangeHandlerFun: function(event) {
  1535. const code = event.detail.code;
  1536. const message = event.detail.message;
  1537. console.log(TAG_NAME, 'pusherStateChange:', code, event);
  1538. switch (code) {
  1539. case 0:
  1540. console.log(message, code);
  1541. break;
  1542. case 1001:
  1543. console.log('已经连接推流服务器', code);
  1544. break;
  1545. case 1002:
  1546. console.log('已经与服务器握手完毕,开始推流', code);
  1547. break;
  1548. case 1003:
  1549. console.log('打开摄像头成功', code);
  1550. break;
  1551. case 1004:
  1552. console.log('录屏启动成功', code);
  1553. break;
  1554. case 1005:
  1555. console.log('推流动态调整分辨率', code);
  1556. break;
  1557. case 1006:
  1558. console.log('推流动态调整码率', code);
  1559. break;
  1560. case 1007:
  1561. console.log('首帧画面采集完成', code);
  1562. break;
  1563. case 1008:
  1564. console.log('编码器启动', code);
  1565. break;
  1566. case 1018:
  1567. console.log('进房成功', code);
  1568. this._emitter.emit(EVENT.LOCAL_JOIN, {
  1569. userID: this.pusher.userID
  1570. });
  1571. break;
  1572. case 1019:
  1573. console.log('退出房间', code);
  1574. this._emitter.emit(EVENT.LOCAL_LEAVE, {
  1575. userID: this.pusher.userID
  1576. });
  1577. break;
  1578. case 2003:
  1579. console.log('渲染首帧视频', code);
  1580. break;
  1581. case 1020:
  1582. case 1031:
  1583. case 1032:
  1584. case 1033:
  1585. case 1034:
  1586. // 通过 userController 处理 1020 1031 1032 1033 1034
  1587. this.userController.userEventHandler(event);
  1588. break;
  1589. case -1301:
  1590. console.error('打开摄像头失败: ', code);
  1591. this._emitter.emit(EVENT.ERROR, {
  1592. code,
  1593. message
  1594. });
  1595. break;
  1596. case -1302:
  1597. console.error('打开麦克风失败: ', code);
  1598. this._emitter.emit(EVENT.ERROR, {
  1599. code,
  1600. message
  1601. });
  1602. break;
  1603. case -1303:
  1604. console.error('视频编码失败: ', code);
  1605. this._emitter.emit(EVENT.ERROR, {
  1606. code,
  1607. message
  1608. });
  1609. break;
  1610. case -1304:
  1611. console.error('音频编码失败: ', code);
  1612. this._emitter.emit(EVENT.ERROR, {
  1613. code,
  1614. message
  1615. });
  1616. break;
  1617. case -1307:
  1618. console.error('推流连接断开: ', code);
  1619. this._emitter.emit(EVENT.ERROR, {
  1620. code,
  1621. message
  1622. });
  1623. break;
  1624. case -100018:
  1625. console.error('进房失败: ', code, message);
  1626. this._emitter.emit(EVENT.ERROR, {
  1627. code,
  1628. message
  1629. });
  1630. break;
  1631. case 5000:
  1632. console.log('小程序被挂起: ', code); // 终端 sdk 建议执行退房操作,唤起时重新进房,临时解决方案,待小程序SDK完全实现自动重新推流后可以去掉
  1633. this.status.isPending = true;
  1634. if (this.status.isPush) {
  1635. // this.exitRoom()
  1636. const tempUrl = this.pusher.url;
  1637. this.pusher.url = ''; // console.log('5000 小程序被挂起后更换pusher', this.data.pusher.getPusherContext().webviewId)
  1638. this.setData(
  1639. {
  1640. pusher: this.pusher
  1641. },
  1642. () => {
  1643. this.pusher.url = tempUrl;
  1644. this.setData(
  1645. {
  1646. pusher: this.pusher
  1647. },
  1648. () => {
  1649. this.pusher.getPusherContext().start();
  1650. console.log('5000 小程序被挂起后更换pusher', this.pusher);
  1651. }
  1652. );
  1653. }
  1654. );
  1655. }
  1656. break;
  1657. case 1021:
  1658. console.log('网络类型发生变化,需要重新进房', code);
  1659. break;
  1660. case 2007:
  1661. console.log('本地视频播放loading: ', code);
  1662. break;
  1663. case 2004:
  1664. console.log('本地视频播放开始: ', code);
  1665. break;
  1666. default:
  1667. console.log(message, code);
  1668. }
  1669. this._emitter.emit(EVENT.LOCAL_STATE_UPDATE, {
  1670. data: event
  1671. });
  1672. },
  1673. pusherNetStatusHandlerFun: function(event) {
  1674. // 触发 LOCAL_NET_STATE_UPDATE
  1675. this._emitter.emit(EVENT.LOCAL_NET_STATE_UPDATE, event);
  1676. },
  1677. pusherErrorHandlerFun: function(event) {
  1678. // 触发 ERROR
  1679. console.warn(TAG_NAME, 'pusher error', event);
  1680. try {
  1681. const code = event.detail.errCode;
  1682. const message = event.detail.errMsg;
  1683. this._emitter.emit(EVENT.ERROR, {
  1684. code,
  1685. message
  1686. });
  1687. } catch (exception) {
  1688. console.error(TAG_NAME, 'pusher error data parser exception', event, exception);
  1689. }
  1690. },
  1691. pusherBGMStartHandlerFun: function(event) {
  1692. // 触发 BGM_START 已经在playBGM方法中进行处理
  1693. // this._emitter.emit(EVENT.BGM_PLAY_START, { data: event })
  1694. },
  1695. pusherBGMProgressHandlerFun: function(event) {
  1696. // BGM_PROGRESS
  1697. this._emitter.emit(EVENT.BGM_PLAY_PROGRESS, event);
  1698. },
  1699. pusherBGMCompleteHandlerFun: function(event) {
  1700. // BGM_COMPLETE
  1701. this._emitter.emit(EVENT.BGM_PLAY_COMPLETE, event);
  1702. },
  1703. // player event handler
  1704. // 获取 player ID 再进行触发
  1705. playerStateChangeFun: function(event) {
  1706. // console.log(TAG_NAME, 'playerStateChangeFun', event)
  1707. this._emitter.emit(EVENT.REMOTE_STATE_UPDATE, event);
  1708. },
  1709. playerFullscreenChangeFun: function(event) {
  1710. // console.log(TAG_NAME, '_playerFullscreenChange', event)
  1711. this._emitter.emit(EVENT.REMOTE_NET_STATE_UPDATE, event);
  1712. },
  1713. playerNetStatusFun: function(event) {
  1714. // console.log(TAG_NAME, 'playerNetStatusFun', event)
  1715. // 获取player 视频的宽高
  1716. const stream = this.userController.getStream({
  1717. userID: event.currentTarget.dataset.userid,
  1718. streamType: event.currentTarget.dataset.streamtype
  1719. });
  1720. if (stream && (stream.videoWidth !== event.detail.info.videoWidth || stream.videoHeight !== event.detail.info.videoHeight)) {
  1721. console.log(TAG_NAME, 'playerNetStatusFun update video size', event);
  1722. stream.videoWidth = event.detail.info.videoWidth;
  1723. stream.videoHeight = event.detail.info.videoHeight;
  1724. }
  1725. this._emitter.emit(EVENT.REMOTE_FULLSCREEN_UPDATE, event);
  1726. },
  1727. playerAudioVolumeNotifyFun: function(event) {
  1728. // console.log(TAG_NAME, 'playerAudioVolumeNotifyFun', event)
  1729. this._emitter.emit(EVENT.REMOTE_AUDIO_VOLUME_UPDATE, event);
  1730. },
  1731. /**
  1732. * 监听组件属性变更,外部变更组件属性时触发该监听,用于检查属性设置是否正常
  1733. * @param {Object} data 变更数据
  1734. */
  1735. propertyObserverFun: function(data) {
  1736. console.log(TAG_NAME, '_propertyObserver', data, this.config);
  1737. if (data.name === 'config') {
  1738. // const config = Object.assign(DEFAULT_PUSHER_CONFIG, data.newVal)
  1739. const config = data.newVal; // querystring 只支持String类型,做一个类型防御
  1740. if (typeof config.debugMode === 'string') {
  1741. config.debugMode === 'true' ? true : false;
  1742. } // 独立设置与pusher无关的配置
  1743. this.setData({
  1744. template: config.template,
  1745. debugMode: config.debugMode || false,
  1746. debug: config.debugMode || false
  1747. });
  1748. this.setPusherConfigFun(config);
  1749. }
  1750. },
  1751. toggleVideoFun() {
  1752. if (this.pusher.enableCamera) {
  1753. this.unpublishLocalVideo();
  1754. } else {
  1755. this.publishLocalVideo();
  1756. }
  1757. },
  1758. toggleAudioFun() {
  1759. if (this.pusher.enableMic) {
  1760. this.unpublishLocalAudio();
  1761. } else {
  1762. this.publishLocalAudio();
  1763. }
  1764. },
  1765. debugToggleRemoteVideoFun(event) {
  1766. console.log(TAG_NAME, '_debugToggleRemoteVideo', event.currentTarget.dataset);
  1767. const userID = event.currentTarget.dataset.userID;
  1768. const streamType = event.currentTarget.dataset.streamType;
  1769. const stream = this.streamList.find(item => {
  1770. return item.userID === userID && item.streamType === streamType;
  1771. });
  1772. if (stream.muteVideo) {
  1773. this.subscribeRemoteVideo({
  1774. userID,
  1775. streamType
  1776. });
  1777. this.setViewVisible({
  1778. userID,
  1779. streamType,
  1780. isVisible: true
  1781. });
  1782. } else {
  1783. this.unsubscribeRemoteVideo({
  1784. userID,
  1785. streamType
  1786. });
  1787. this.setViewVisible({
  1788. userID,
  1789. streamType,
  1790. isVisible: false
  1791. });
  1792. }
  1793. },
  1794. debugToggleRemoteAudioFun(event) {
  1795. console.log(TAG_NAME, '_debugToggleRemoteAudio', event.currentTarget.dataset);
  1796. const userID = event.currentTarget.dataset.userID;
  1797. const streamType = event.currentTarget.dataset.streamType;
  1798. const stream = this.streamList.find(item => {
  1799. return item.userID === userID && item.streamType === streamType;
  1800. });
  1801. if (stream.muteAudio) {
  1802. this.subscribeRemoteAudio({
  1803. userID
  1804. });
  1805. } else {
  1806. this.unsubscribeRemoteAudio({
  1807. userID
  1808. });
  1809. }
  1810. },
  1811. debugToggleVideoDebugFun() {
  1812. this.setData({
  1813. debug: !this.debug
  1814. });
  1815. },
  1816. debugExitRoomFun() {
  1817. this.exitRoom();
  1818. },
  1819. debugEnterRoomFun() {
  1820. this.publishLocalVideo();
  1821. this.publishLocalAudio();
  1822. this.enterRoom({
  1823. roomID: this.config.roomID
  1824. }).then(() => {
  1825. // 进房后开始推送视频或音频
  1826. });
  1827. },
  1828. debugGoBackFun() {
  1829. wx.navigateBack({
  1830. delta: 1
  1831. });
  1832. },
  1833. debugTogglePanelFun() {
  1834. this.setData({
  1835. debugPanel: !this.debugPanel
  1836. });
  1837. },
  1838. toggleAudioVolumeTypeFun() {
  1839. if (this.pusher.audioVolumeType === 'voicecall') {
  1840. this.setPusherConfigFun({
  1841. audioVolumeType: 'media'
  1842. });
  1843. } else {
  1844. this.setPusherConfigFun({
  1845. audioVolumeType: 'voicecall'
  1846. });
  1847. }
  1848. },
  1849. toggleSoundModeFun() {
  1850. if (this.userList.length === 0) {
  1851. return;
  1852. }
  1853. const stream = this.userController.getStream({
  1854. userID: this.userList[0].userID,
  1855. streamType: 'main'
  1856. });
  1857. if (stream) {
  1858. if (stream.soundMode === 'speaker') {
  1859. stream['soundMode'] = 'ear';
  1860. } else {
  1861. stream['soundMode'] = 'speaker';
  1862. }
  1863. this.setPlayerConfigFun({
  1864. userID: stream.userID,
  1865. streamType: 'main',
  1866. config: {
  1867. soundMode: stream['soundMode']
  1868. }
  1869. });
  1870. }
  1871. },
  1872. /**
  1873. * 退出通话
  1874. */
  1875. hangUpFun: function() {
  1876. this.exitRoom();
  1877. wx.navigateBack({
  1878. delta: 1
  1879. });
  1880. },
  1881. /**
  1882. * 切换订阅音频状态
  1883. */
  1884. handleSubscribeAudio: function() {
  1885. if (this.pusher.enableMic) {
  1886. this.unpublishLocalAudio();
  1887. } else {
  1888. this.publishLocalAudio();
  1889. }
  1890. },
  1891. /**
  1892. * 切换订阅远端视频状态
  1893. * @param event
  1894. */
  1895. handleSubscribeRemoteVideoFun: function(event) {
  1896. const userID = event.currentTarget.dataset.userID;
  1897. const streamType = event.currentTarget.dataset.streamType;
  1898. const stream = this.streamList.find(item => {
  1899. return item.userID === userID && item.streamType === streamType;
  1900. });
  1901. if (stream.muteVideo) {
  1902. this.subscribeRemoteVideo({
  1903. userID,
  1904. streamType
  1905. });
  1906. } else {
  1907. this.unsubscribeRemoteVideo({
  1908. userID,
  1909. streamType
  1910. });
  1911. }
  1912. },
  1913. /**
  1914. * 将远端视频取消全屏
  1915. * @param event
  1916. */
  1917. handleSubscribeRemoteAudioFun: function(event) {
  1918. const userID = event.currentTarget.dataset.userID;
  1919. const streamType = event.currentTarget.dataset.streamType;
  1920. const stream = this.streamList.find(item => {
  1921. return item.userID === userID && item.streamType === streamType;
  1922. });
  1923. if (stream.muteAudio) {
  1924. this.subscribeRemoteAudio({
  1925. userID
  1926. });
  1927. } else {
  1928. this.unsubscribeRemoteAudio({
  1929. userID
  1930. });
  1931. }
  1932. },
  1933. /**
  1934. * grid布局, 唤起 memberlist-panel
  1935. */
  1936. switchMemberListPanelFun() {
  1937. this.setData({
  1938. panelName: this.panelName !== 'memberlist-panel' ? 'memberlist-panel' : ''
  1939. });
  1940. },
  1941. /**
  1942. * grid布局, 唤起setting-panel
  1943. */
  1944. switchSettingPanelFun() {
  1945. this.setData({
  1946. panelName: this.panelName !== 'setting-panel' ? 'setting-panel' : ''
  1947. });
  1948. },
  1949. handleMaskerClickFun() {
  1950. this.setData({
  1951. panelName: ''
  1952. });
  1953. },
  1954. setPuserPropertyFun(event) {
  1955. // console.log(TAG_NAME, '_setPuserProperty', event)
  1956. const key = event.currentTarget.dataset.key;
  1957. let value = event.currentTarget.dataset.value;
  1958. const config = {};
  1959. if (value === 'true') {
  1960. value = true;
  1961. } else if (value === 'false') {
  1962. value = false;
  1963. }
  1964. if (typeof value === 'boolean') {
  1965. config[key] = !this.pusher[key];
  1966. } else if (typeof value === 'string' && value.indexOf('|') > 0) {
  1967. value = value.split('|');
  1968. if (this.pusher[key] === value[0]) {
  1969. config[key] = value[1];
  1970. } else {
  1971. config[key] = value[0];
  1972. }
  1973. } // console.log(TAG_NAME, '_setPuserProperty', config)
  1974. this.setPusherConfigFun(config);
  1975. },
  1976. setPlayerPropertyFun(event) {
  1977. console.log(TAG_NAME, '_setPlayerProperty', event);
  1978. const userID = event.currentTarget.dataset.userid;
  1979. const streamType = event.currentTarget.dataset.streamtype;
  1980. const key = event.currentTarget.dataset.key;
  1981. let value = event.currentTarget.dataset.value;
  1982. const stream = this.userController.getStream({
  1983. userID: userID,
  1984. streamType: streamType
  1985. });
  1986. if (!stream) {
  1987. return;
  1988. }
  1989. const config = {};
  1990. if (value === 'true') {
  1991. value = true;
  1992. } else if (value === 'false') {
  1993. value = false;
  1994. }
  1995. if (typeof value === 'boolean') {
  1996. config[key] = !stream[key];
  1997. } else if (typeof value === 'string' && value.indexOf('|') > 0) {
  1998. value = value.split('|');
  1999. if (stream[key] === value[0]) {
  2000. config[key] = value[1];
  2001. } else {
  2002. config[key] = value[0];
  2003. }
  2004. }
  2005. console.log(TAG_NAME, '_setPlayerProperty', config);
  2006. this.setPlayerConfigFun({
  2007. userID,
  2008. streamType,
  2009. config
  2010. });
  2011. },
  2012. switchStreamTypeFun(event) {
  2013. const userID = event.currentTarget.dataset.userid;
  2014. const streamType = event.currentTarget.dataset.streamtype;
  2015. const stream = this.userController.getStream({
  2016. userID: userID,
  2017. streamType: streamType
  2018. });
  2019. if (stream && stream.streamType === 'main') {
  2020. if (stream._definitionType === 'small') {
  2021. this.subscribeRemoteVideo({
  2022. userID,
  2023. streamType: 'main'
  2024. });
  2025. } else {
  2026. this.subscribeRemoteVideo({
  2027. userID,
  2028. streamType: 'small'
  2029. });
  2030. }
  2031. }
  2032. },
  2033. handleSnapshotClickFun(event) {
  2034. wx.showToast({
  2035. title: '开始截屏',
  2036. icon: 'none',
  2037. duration: 1000
  2038. });
  2039. const userID = event.currentTarget.dataset.userid;
  2040. const streamType = event.currentTarget.dataset.streamtype;
  2041. this.snapshot({
  2042. userID,
  2043. streamType
  2044. });
  2045. },
  2046. /**
  2047. * grid布局, 绑定事件
  2048. */
  2049. bindEventGridFun() {
  2050. // 远端音量变更
  2051. this.on(EVENT.REMOTE_AUDIO_VOLUME_UPDATE, event => {
  2052. const data = event.data;
  2053. const userID = data.currentTarget.dataset.userid;
  2054. const streamType = data.currentTarget.dataset.streamtype;
  2055. const volume = data.detail.volume; // console.log(TAG_NAME, '远端音量变更', userID, streamType, volume)
  2056. const stream = this.userController.getStream({
  2057. userID: userID,
  2058. streamType: streamType
  2059. });
  2060. if (stream) {
  2061. stream.volume = volume;
  2062. }
  2063. this.setData(
  2064. {
  2065. streamList: this.streamList
  2066. },
  2067. () => {}
  2068. );
  2069. });
  2070. },
  2071. toggleFullscreenFun(event) {
  2072. console.log(TAG_NAME, '_toggleFullscreen', event);
  2073. const userID = event.currentTarget.dataset.userID;
  2074. const streamType = event.currentTarget.dataset.streamType;
  2075. if (this._isFullscreen) {
  2076. this.exitFullscreen({
  2077. userID,
  2078. streamType
  2079. })
  2080. .then(() => {
  2081. this._isFullscreen = false;
  2082. })
  2083. .catch(() => {});
  2084. } else {
  2085. // const stream = this.userController.getStream({ userID, streamType })
  2086. const direction = 0; // 已知问题:视频的尺寸需要等待player触发NetStatus事件才能获取到,如果进房就双击全屏,全屏后的方向有可能不对。
  2087. // if (stream && stream.videoWidth && stream.videoHeight) {
  2088. // // 如果是横视频,全屏时进行横屏处理。如果是竖视频,则为0
  2089. // direction = stream.videoWidth > stream.videoHeight ? 90 : 0
  2090. // }
  2091. this.enterFullscreen({
  2092. userID,
  2093. streamType,
  2094. direction
  2095. })
  2096. .then(() => {
  2097. this._isFullscreen = true;
  2098. })
  2099. .catch(() => {});
  2100. }
  2101. }
  2102. }
  2103. };
  2104. </script>
  2105. <style>
  2106. @import './trtc-room.css';
  2107. </style>