| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- import { onUnmounted, ref, watch } from 'vue';
- import { useRoute } from 'vue-router';
- import { useFlvPlay, useHlsPlay } from '@/hooks/use-play';
- import { useSrsWs } from '@/hooks/use-srs-ws';
- import {
- DanmuMsgTypeEnum,
- IDanmu,
- ILiveRoom,
- IMessage,
- LiveLineEnum,
- LiveRoomTypeEnum,
- } from '@/interface';
- import { WsMsgTypeEnum } from '@/interface-ws';
- import { useAppStore } from '@/store/app';
- import { usePiniaCacheStore } from '@/store/cache';
- import { useNetworkStore } from '@/store/network';
- import { useUserStore } from '@/store/user';
- import { createVideo, videoToCanvas } from '@/utils';
- export function usePull() {
- const route = useRoute();
- const userStore = useUserStore();
- const networkStore = useNetworkStore();
- const cacheStore = usePiniaCacheStore();
- const appStore = useAppStore();
- const roomId = ref(route.params.roomId as string);
- const localStream = ref<MediaStream>();
- const danmuStr = ref('');
- const autoplayVal = ref(false);
- const videoLoading = ref(false);
- const flvurl = ref('');
- const hlsurl = ref('');
- const videoHeight = ref();
- const sidebarList = ref<
- {
- socketId: string;
- }[]
- >([]);
- const videoElArr = ref<HTMLVideoElement[]>([]);
- const remoteVideo = ref<HTMLElement[]>([]);
- const {
- isPull,
- mySocketId,
- initSrsWs,
- roomLiving,
- anchorInfo,
- liveUserList,
- damuList,
- } = useSrsWs();
- isPull.value = true;
- const { flvVideoEl, startFlvPlay, destroyFlv } = useFlvPlay();
- const { hlsVideoEl, startHlsPlay, destroyHls } = useHlsPlay();
- const stopDrawingArr = ref<any[]>([]);
- onUnmounted(() => {
- handleStopDrawing();
- });
- function handleStopDrawing() {
- destroyFlv();
- destroyHls();
- stopDrawingArr.value.forEach((cb) => cb());
- stopDrawingArr.value = [];
- remoteVideo.value.forEach((el) => el.remove());
- remoteVideo.value = [];
- }
- watch(hlsVideoEl, () => {
- stopDrawingArr.value = [];
- stopDrawingArr.value.forEach((cb) => cb());
- const { canvas, stopDrawing } = videoToCanvas({
- videoEl: hlsVideoEl.value!,
- resize: ({ w, h }) => {
- videoHeight.value = `${w}x${h}`;
- },
- });
- stopDrawingArr.value.push(stopDrawing);
- remoteVideo.value.push(canvas);
- videoLoading.value = false;
- });
- function handleHlsPlay(url?: string) {
- console.log('handleHlsPlay', url);
- handleStopDrawing();
- videoLoading.value = true;
- appStore.setLiveLine(LiveLineEnum.hls);
- startHlsPlay({
- hlsurl: url || hlsurl.value,
- });
- }
- watch(flvVideoEl, () => {
- stopDrawingArr.value = [];
- stopDrawingArr.value.forEach((cb) => cb());
- const { canvas, stopDrawing } = videoToCanvas({
- videoEl: flvVideoEl.value!,
- resize: ({ w, h }) => {
- videoHeight.value = `${w}x${h}`;
- },
- });
- stopDrawingArr.value.push(stopDrawing);
- remoteVideo.value.push(canvas);
- videoLoading.value = false;
- });
- function handleFlvPlay() {
- console.log('handleFlvPlay');
- handleStopDrawing();
- videoLoading.value = true;
- appStore.setLiveLine(LiveLineEnum.flv);
- startFlvPlay({
- flvurl: flvurl.value,
- });
- }
- function handlePlay(data: ILiveRoom) {
- roomLiving.value = true;
- flvurl.value = data.flv_url!;
- hlsurl.value = data.hls_url!;
- switch (data.type) {
- case LiveRoomTypeEnum.user_srs:
- if (appStore.liveLine === LiveLineEnum.flv) {
- handleFlvPlay();
- } else if (appStore.liveLine === LiveLineEnum.hls) {
- handleHlsPlay(data.hls_url);
- }
- break;
- case LiveRoomTypeEnum.user_obs:
- if (appStore.liveLine === LiveLineEnum.flv) {
- handleFlvPlay();
- } else if (appStore.liveLine === LiveLineEnum.hls) {
- handleHlsPlay(data.hls_url);
- }
- break;
- case LiveRoomTypeEnum.system:
- if (appStore.liveLine === LiveLineEnum.flv) {
- handleFlvPlay();
- } else if (appStore.liveLine === LiveLineEnum.hls) {
- handleHlsPlay(data.hls_url);
- }
- break;
- case LiveRoomTypeEnum.user_wertc:
- appStore.setLiveLine(LiveLineEnum.rtc);
- break;
- }
- }
- watch(
- () => roomLiving.value,
- (val) => {
- if (val) {
- if (appStore.liveRoomInfo?.type !== LiveRoomTypeEnum.user_wertc) {
- handlePlay(appStore.liveRoomInfo!);
- }
- } else {
- closeRtc();
- handleStopDrawing();
- }
- }
- );
- watch(
- () => appStore.liveLine,
- (newVal) => {
- console.log('liveLine变了', newVal);
- if (!roomLiving.value) {
- return;
- }
- switch (newVal) {
- case LiveLineEnum.flv:
- handleFlvPlay();
- break;
- case LiveLineEnum.hls:
- handleHlsPlay();
- break;
- case LiveLineEnum.rtc:
- break;
- }
- }
- );
- watch(
- () => cacheStore.muted,
- (newVal) => {
- videoElArr.value.forEach((el) => {
- el.muted = newVal;
- });
- if (!newVal) {
- cacheStore.setVolume(cacheStore.volume || appStore.normalVolume);
- } else {
- cacheStore.setVolume(0);
- }
- }
- );
- watch(
- () => cacheStore.volume,
- (newVal) => {
- videoElArr.value.forEach((el) => {
- el.volume = newVal / 100;
- });
- if (!newVal) {
- cacheStore.setMuted(true);
- } else {
- cacheStore.setMuted(false);
- }
- }
- );
- watch(
- () => appStore.play,
- (newVal) => {
- videoElArr.value.forEach((el) => {
- if (newVal) {
- el.play();
- } else {
- el.pause();
- }
- });
- }
- );
- watch(
- () => networkStore.rtcMap,
- (newVal) => {
- if (appStore.liveRoomInfo?.type === LiveRoomTypeEnum.user_wertc) {
- newVal.forEach((item) => {
- videoLoading.value = false;
- const { canvas } = videoToCanvas({
- videoEl: item.videoEl,
- resize: ({ w, h }) => {
- videoHeight.value = `${w}x${h}`;
- },
- });
- videoElArr.value.push(item.videoEl);
- remoteVideo.value.push(canvas);
- });
- }
- },
- {
- deep: true,
- immediate: true,
- }
- );
- watch(
- () => localStream.value,
- (val) => {
- if (val) {
- console.log('localStream变了');
- console.log('音频轨:', val?.getAudioTracks());
- console.log('视频轨:', val?.getVideoTracks());
- if (appStore.liveRoomInfo?.type === LiveRoomTypeEnum.user_wertc) {
- videoElArr.value.forEach((dom) => {
- dom.remove();
- });
- val?.getVideoTracks().forEach((track) => {
- console.log('视频轨enabled:', track.id, track.enabled);
- const video = createVideo({});
- video.setAttribute('track-id', track.id);
- video.srcObject = new MediaStream([track]);
- remoteVideo.value.push(video);
- videoElArr.value.push(video);
- });
- val?.getAudioTracks().forEach((track) => {
- console.log('音频轨enabled:', track.id, track.enabled);
- const video = createVideo({});
- video.setAttribute('track-id', track.id);
- video.srcObject = new MediaStream([track]);
- remoteVideo.value.push(video);
- videoElArr.value.push(video);
- });
- videoLoading.value = false;
- } else {
- videoElArr.value.forEach((dom) => {
- dom.remove();
- });
- val?.getVideoTracks().forEach((track) => {
- console.log('视频轨enabled:', track.id, track.enabled);
- const video = createVideo({});
- video.setAttribute('track-id', track.id);
- video.srcObject = new MediaStream([track]);
- remoteVideo.value.push(video);
- videoElArr.value.push(video);
- });
- val?.getAudioTracks().forEach((track) => {
- console.log('音频轨enabled:', track.id, track.enabled);
- const video = createVideo({});
- video.setAttribute('track-id', track.id);
- video.srcObject = new MediaStream([track]);
- remoteVideo.value.push(video);
- videoElArr.value.push(video);
- });
- videoLoading.value = false;
- }
- } else {
- videoElArr.value?.forEach((item) => {
- item.remove();
- });
- }
- },
- { deep: true }
- );
- watch(
- [
- () => userStore.userInfo,
- () => networkStore.wsMap.get(roomId.value)?.socketIo?.connected,
- ],
- ([userInfo, connected]) => {
- if (userInfo && connected) {
- const instance = networkStore.wsMap.get(roomId.value);
- if (!instance) return;
- }
- }
- );
- function initPull(autolay = true) {
- autoplayVal.value = autolay;
- if (autoplayVal.value) {
- videoLoading.value = true;
- }
- initSrsWs({
- roomId: roomId.value,
- isAnchor: false,
- });
- }
- function closeWs() {
- const instance = networkStore.wsMap.get(roomId.value);
- instance?.close();
- }
- function closeRtc() {
- networkStore.rtcMap.forEach((rtc) => {
- rtc.close();
- networkStore.removeRtc(rtc.roomId);
- });
- }
- function addVideo() {
- sidebarList.value.push({ socketId: mySocketId.value });
- }
- function keydownDanmu(event: KeyboardEvent) {
- const key = event.key.toLowerCase();
- if (key === 'enter') {
- event.preventDefault();
- sendDanmu();
- }
- }
- function sendDanmu() {
- if (!danmuStr.value.trim().length) {
- window.$message.warning('请输入弹幕内容!');
- return;
- }
- const instance = networkStore.wsMap.get(roomId.value);
- if (!instance) return;
- const danmu: IDanmu = {
- socket_id: mySocketId.value,
- userInfo: userStore.userInfo,
- msgType: DanmuMsgTypeEnum.danmu,
- msg: danmuStr.value,
- };
- const messageData: IMessage['data'] = {
- msg: danmuStr.value,
- msgType: DanmuMsgTypeEnum.danmu,
- live_room_id: Number(roomId.value),
- };
- instance.send({
- msgType: WsMsgTypeEnum.message,
- data: messageData,
- });
- damuList.value.push(danmu);
- danmuStr.value = '';
- }
- return {
- handlePlay,
- handleStopDrawing,
- initPull,
- closeWs,
- closeRtc,
- keydownDanmu,
- sendDanmu,
- addVideo,
- mySocketId,
- videoHeight,
- remoteVideo,
- roomLiving,
- autoplayVal,
- videoLoading,
- damuList,
- liveUserList,
- sidebarList,
- danmuStr,
- anchorInfo,
- };
- }
|