use-pull.ts 13 KB

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