use-pull.ts 11 KB

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