use-pull.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. import { getRandomString } from 'billd-utils';
  2. import { nextTick, onUnmounted, ref, watch } from 'vue';
  3. import { useRoute } from 'vue-router';
  4. import { commentAuthTip, loginTip } from '@/hooks/use-login';
  5. import { useFlvPlay, useHlsPlay } from '@/hooks/use-play';
  6. import { useWebsocket } from '@/hooks/use-websocket';
  7. import {
  8. DanmuMsgTypeEnum,
  9. LiveLineEnum,
  10. LiveRenderEnum,
  11. WsMessageMsgIsFileEnum,
  12. } from '@/interface';
  13. import { useAppStore } from '@/store/app';
  14. import { usePiniaCacheStore } from '@/store/cache';
  15. import { useNetworkStore } from '@/store/network';
  16. import { ILiveRoom, LiveRoomTypeEnum } from '@/types/ILiveRoom';
  17. import { WsMessageType, WsMsgTypeEnum } from '@/types/websocket';
  18. import { videoFullBox, videoToCanvas } from '@/utils';
  19. export function usePull(roomId: string) {
  20. const route = useRoute();
  21. const networkStore = useNetworkStore();
  22. const cacheStore = usePiniaCacheStore();
  23. const appStore = useAppStore();
  24. const danmuStr = ref('');
  25. const msgIsFile = ref(WsMessageMsgIsFileEnum.no);
  26. const danmuMsgType = ref<DanmuMsgTypeEnum>(DanmuMsgTypeEnum.danmu);
  27. const autoplayVal = ref(false);
  28. const videoLoading = ref(false);
  29. const isPlaying = ref(false);
  30. const showPlayBtn = ref(false);
  31. const flvurl = ref('');
  32. const hlsurl = ref('');
  33. const videoWrapRef = ref<HTMLDivElement>();
  34. const videoResolution = ref();
  35. const isRemoteDesk = ref(false);
  36. const videoElArr = ref<HTMLVideoElement[]>([]);
  37. const remoteVideo = ref<HTMLElement[]>([]);
  38. const {
  39. mySocketId,
  40. initWs,
  41. roomLiving,
  42. anchorInfo,
  43. liveUserList,
  44. damuList,
  45. handleSendGetLiveUser,
  46. } = useWebsocket();
  47. const { flvVideoEl, flvIsPlaying, startFlvPlay, destroyFlv } = useFlvPlay();
  48. const { hlsVideoEl, hlsIsPlaying, startHlsPlay, destroyHls } = useHlsPlay();
  49. const stopDrawingArr = ref<any[]>([]);
  50. let changeWrapSizeFn;
  51. onUnmounted(() => {
  52. handleStopDrawing();
  53. });
  54. function handleStopDrawing() {
  55. destroyFlv();
  56. destroyHls();
  57. changeWrapSizeFn = undefined;
  58. stopDrawingArr.value.forEach((cb) => cb());
  59. stopDrawingArr.value = [];
  60. remoteVideo.value.forEach((el) => el.remove());
  61. remoteVideo.value = [];
  62. if (isRemoteDesk.value && videoWrapRef.value) {
  63. videoWrapRef.value.removeAttribute('style');
  64. }
  65. }
  66. function handleVideoWrapResize() {
  67. nextTick(() => {
  68. if (videoWrapRef.value) {
  69. const rect = videoWrapRef.value.getBoundingClientRect();
  70. changeWrapSizeFn?.({ width: rect.width, height: rect.height });
  71. }
  72. });
  73. }
  74. function videoPlay(videoEl: HTMLVideoElement) {
  75. stopDrawingArr.value = [];
  76. stopDrawingArr.value.forEach((cb) => cb());
  77. if (appStore.videoControls.renderMode === LiveRenderEnum.canvas) {
  78. if (videoEl && videoWrapRef.value) {
  79. const rect = videoWrapRef.value.getBoundingClientRect();
  80. const { canvas, stopDrawing, changeWrapSize } = videoToCanvas({
  81. wrapSize: {
  82. width: rect.width,
  83. height: rect.height,
  84. },
  85. videoEl,
  86. videoResize: ({ w, h }) => {
  87. videoResolution.value = `${w}x${h}`;
  88. },
  89. });
  90. changeWrapSizeFn = changeWrapSize;
  91. stopDrawingArr.value.push(stopDrawing);
  92. remoteVideo.value.push(canvas);
  93. videoElArr.value.push(videoEl);
  94. videoLoading.value = false;
  95. }
  96. } else if (appStore.videoControls.renderMode === LiveRenderEnum.video) {
  97. if (videoEl && videoWrapRef.value) {
  98. const rect = videoWrapRef.value.getBoundingClientRect();
  99. const { changeWrapSize } = videoFullBox({
  100. wrapSize: {
  101. width: rect.width,
  102. height: rect.height,
  103. },
  104. videoEl,
  105. videoResize: ({ w, h }) => {
  106. videoResolution.value = `${w}x${h}`;
  107. },
  108. });
  109. changeWrapSizeFn = changeWrapSize;
  110. remoteVideo.value.push(videoEl);
  111. videoElArr.value.push(videoEl);
  112. videoLoading.value = false;
  113. }
  114. }
  115. }
  116. watch(hlsVideoEl, (newval) => {
  117. if (newval) {
  118. videoPlay(newval);
  119. }
  120. });
  121. watch(flvVideoEl, (newval) => {
  122. if (newval) {
  123. videoPlay(newval);
  124. }
  125. });
  126. watch(
  127. () => appStore.videoControlsValue.pageFullMode,
  128. () => {
  129. handleVideoWrapResize();
  130. }
  131. );
  132. watch(
  133. () => appStore.videoControls.renderMode,
  134. () => {
  135. if (appStore.liveRoomInfo) {
  136. handlePlay(appStore.liveRoomInfo);
  137. }
  138. }
  139. );
  140. watch(
  141. () => networkStore.rtcMap,
  142. (newVal) => {
  143. if (newVal.size) {
  144. roomLiving.value = true;
  145. videoLoading.value = false;
  146. }
  147. if (
  148. isRemoteDesk.value ||
  149. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.wertc_meeting_one ||
  150. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.wertc_live ||
  151. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.pk ||
  152. appStore.liveRoomInfo?.type === LiveRoomTypeEnum.tencent_css_pk
  153. ) {
  154. newVal.forEach((item) => {
  155. if (appStore.allTrack.find((v) => v.mediaName === item.receiver)) {
  156. return;
  157. }
  158. const rect = videoWrapRef.value?.getBoundingClientRect();
  159. if (rect) {
  160. videoFullBox({
  161. wrapSize: {
  162. width: rect.width,
  163. height: rect.height,
  164. },
  165. videoEl: item.videoEl,
  166. videoResize: ({ w, h }) => {
  167. videoResolution.value = `${w}x${h}`;
  168. },
  169. });
  170. remoteVideo.value.push(item.videoEl);
  171. videoElArr.value.push(item.videoEl);
  172. }
  173. });
  174. nextTick(() => {
  175. if (isRemoteDesk.value && videoWrapRef.value) {
  176. if (newVal.size) {
  177. videoWrapRef.value.style.display = 'inline-block';
  178. } else {
  179. videoWrapRef.value.style.removeProperty('display');
  180. }
  181. }
  182. });
  183. }
  184. },
  185. {
  186. deep: true,
  187. immediate: true,
  188. }
  189. );
  190. watch(
  191. () => remoteVideo.value,
  192. (newval) => {
  193. newval.forEach((videoEl) => {
  194. videoWrapRef.value?.appendChild(videoEl);
  195. });
  196. },
  197. {
  198. deep: true,
  199. immediate: true,
  200. }
  201. );
  202. function handleHlsPlay(url: string) {
  203. console.log('handleHlsPlay', url);
  204. handleStopDrawing();
  205. videoLoading.value = true;
  206. appStore.setLiveLine(LiveLineEnum.hls);
  207. startHlsPlay({
  208. hlsurl: url,
  209. });
  210. }
  211. function handleFlvPlay() {
  212. console.log('handleFlvPlay', flvurl.value);
  213. handleStopDrawing();
  214. videoLoading.value = true;
  215. appStore.setLiveLine(LiveLineEnum.flv);
  216. startFlvPlay({
  217. flvurl: flvurl.value,
  218. });
  219. }
  220. function handlePlay(data: ILiveRoom) {
  221. roomLiving.value = true;
  222. flvurl.value = data.flv_url!;
  223. hlsurl.value = data.hls_url!;
  224. function play() {
  225. if (appStore.liveLine === LiveLineEnum.flv) {
  226. handleFlvPlay();
  227. } else if (appStore.liveLine === LiveLineEnum.hls) {
  228. handleHlsPlay(data.hls_url!);
  229. }
  230. }
  231. if (LiveRoomTypeEnum.pk === data.type && !route.query.pkKey) {
  232. play();
  233. } else if (
  234. [
  235. LiveRoomTypeEnum.system,
  236. LiveRoomTypeEnum.srs,
  237. LiveRoomTypeEnum.obs,
  238. LiveRoomTypeEnum.msr,
  239. LiveRoomTypeEnum.pk,
  240. LiveRoomTypeEnum.forward_bilibili,
  241. LiveRoomTypeEnum.forward_huya,
  242. LiveRoomTypeEnum.forward_all,
  243. ].includes(data.type!)
  244. ) {
  245. play();
  246. } else if (
  247. [
  248. LiveRoomTypeEnum.wertc_live,
  249. LiveRoomTypeEnum.wertc_meeting_one,
  250. LiveRoomTypeEnum.wertc_meeting_two,
  251. ].includes(data.type!)
  252. ) {
  253. appStore.setLiveLine(LiveLineEnum.rtc);
  254. }
  255. }
  256. watch(
  257. [() => roomLiving.value, () => appStore.liveRoomInfo],
  258. ([val, liveRoomInfo]) => {
  259. if (val && liveRoomInfo) {
  260. showPlayBtn.value = false;
  261. if (
  262. [
  263. LiveRoomTypeEnum.system,
  264. LiveRoomTypeEnum.msr,
  265. LiveRoomTypeEnum.srs,
  266. LiveRoomTypeEnum.obs,
  267. LiveRoomTypeEnum.tencent_css,
  268. LiveRoomTypeEnum.tencent_css_pk,
  269. LiveRoomTypeEnum.forward_bilibili,
  270. LiveRoomTypeEnum.forward_huya,
  271. LiveRoomTypeEnum.forward_all,
  272. ].includes(liveRoomInfo.type!)
  273. ) {
  274. handlePlay(liveRoomInfo!);
  275. } else if (LiveRoomTypeEnum.pk === liveRoomInfo.type!) {
  276. if (!route.query.pkKey) {
  277. handlePlay(liveRoomInfo!);
  278. }
  279. }
  280. }
  281. if (!roomLiving.value) {
  282. closeRtc();
  283. handleStopDrawing();
  284. }
  285. },
  286. {
  287. deep: true,
  288. immediate: true,
  289. }
  290. );
  291. watch(
  292. () => appStore.liveLine,
  293. (newVal) => {
  294. console.log('liveLine变了', newVal);
  295. if (!roomLiving.value) {
  296. return;
  297. }
  298. switch (newVal) {
  299. case LiveLineEnum.flv:
  300. handleFlvPlay();
  301. break;
  302. case LiveLineEnum.hls:
  303. handleHlsPlay(hlsurl.value);
  304. break;
  305. case LiveLineEnum.rtc:
  306. break;
  307. }
  308. }
  309. );
  310. watch(
  311. () => cacheStore.muted,
  312. (newVal) => {
  313. videoElArr.value.forEach((el) => {
  314. el.muted = newVal;
  315. });
  316. if (!newVal) {
  317. cacheStore.setVolume(cacheStore.volume || appStore.normalVolume);
  318. } else {
  319. cacheStore.setVolume(0);
  320. }
  321. }
  322. );
  323. watch(
  324. () => cacheStore.volume,
  325. (newVal) => {
  326. videoElArr.value.forEach((el) => {
  327. el.volume = newVal / 100;
  328. });
  329. if (!newVal) {
  330. cacheStore.setMuted(true);
  331. } else {
  332. cacheStore.setMuted(false);
  333. }
  334. }
  335. );
  336. watch(
  337. () => appStore.playing,
  338. (newVal) => {
  339. videoElArr.value.forEach((el) => {
  340. if (newVal) {
  341. el.play();
  342. } else {
  343. el.pause();
  344. }
  345. });
  346. }
  347. );
  348. watch(
  349. () => hlsIsPlaying.value,
  350. (newVal) => {
  351. isPlaying.value = newVal;
  352. }
  353. );
  354. watch(
  355. () => flvIsPlaying.value,
  356. (newVal) => {
  357. isPlaying.value = newVal;
  358. }
  359. );
  360. watch(
  361. () => appStore.remoteDesk.isClose,
  362. (newval) => {
  363. if (newval) {
  364. handleStopDrawing();
  365. }
  366. }
  367. );
  368. function initPull(data: { autolay?: boolean; isRemoteDesk?: boolean }) {
  369. if (data.autolay === undefined) {
  370. autoplayVal.value = true;
  371. } else {
  372. autoplayVal.value = data.autolay;
  373. }
  374. if (autoplayVal.value) {
  375. videoLoading.value = true;
  376. }
  377. isRemoteDesk.value = !!data.isRemoteDesk;
  378. initWs({
  379. isRemoteDesk: data.isRemoteDesk,
  380. roomId,
  381. isAnchor: false,
  382. });
  383. }
  384. function closeWs() {
  385. networkStore.wsMap.forEach((ws) => {
  386. networkStore.removeWs(ws.roomId);
  387. });
  388. }
  389. function closeRtc() {
  390. networkStore.rtcMap.forEach((rtc) => {
  391. networkStore.removeRtc(rtc.receiver);
  392. });
  393. }
  394. function keydownDanmu(event: KeyboardEvent) {
  395. const key = event.key.toLowerCase();
  396. if (key === 'enter') {
  397. event.preventDefault();
  398. danmuMsgType.value = DanmuMsgTypeEnum.danmu;
  399. sendDanmu();
  400. }
  401. }
  402. function sendDanmu() {
  403. if (!loginTip()) {
  404. return;
  405. }
  406. if (!commentAuthTip()) {
  407. return;
  408. }
  409. if (!danmuStr.value.trim().length) {
  410. window.$message.warning('请输入弹幕内容!');
  411. return;
  412. }
  413. const instance = networkStore.wsMap.get(roomId);
  414. if (!instance) return;
  415. const requestId = getRandomString(8);
  416. console.log(danmuMsgType.value, 2221);
  417. const messageData: WsMessageType['data'] = {
  418. socket_id: '',
  419. msg: danmuStr.value,
  420. msgType: danmuMsgType.value,
  421. live_room_id: Number(roomId),
  422. msgIsFile: msgIsFile.value,
  423. send_msg_time: +new Date(),
  424. user_agent: navigator.userAgent,
  425. };
  426. instance.send({
  427. requestId,
  428. msgType: WsMsgTypeEnum.message,
  429. data: messageData,
  430. });
  431. danmuStr.value = '';
  432. }
  433. return {
  434. videoWrapRef,
  435. handlePlay,
  436. handleStopDrawing,
  437. initPull,
  438. closeWs,
  439. closeRtc,
  440. keydownDanmu,
  441. sendDanmu,
  442. handleSendGetLiveUser,
  443. showPlayBtn,
  444. danmuMsgType,
  445. isPlaying,
  446. msgIsFile,
  447. mySocketId,
  448. videoResolution,
  449. remoteVideo,
  450. roomLiving,
  451. autoplayVal,
  452. videoLoading,
  453. damuList,
  454. liveUserList,
  455. danmuStr,
  456. anchorInfo,
  457. };
  458. }