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