|  | @@ -4,7 +4,7 @@ import * as RTC from '@rongcloud/plugin-rtc'
 | 
	
		
			
				|  |  |  import request from '/src/helpers/request'
 | 
	
		
			
				|  |  |  import { state } from '/src/state'
 | 
	
		
			
				|  |  |  import event, { LIVE_EVENT_MESSAGE } from './event'
 | 
	
		
			
				|  |  | -import { removeMedia } from './helpers'
 | 
	
		
			
				|  |  | +import dayjs from 'dayjs'
 | 
	
		
			
				|  |  |  // import { SeatsCtrl } from './message-type'
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  type imConnectStatus = 'connecting' | 'connected' | 'disconnect'
 | 
	
	
		
			
				|  | @@ -18,9 +18,11 @@ type ActiveTracks = {
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  type DeviceStatus = {
 | 
	
		
			
				|  |  | -  [key in TrackType]: 'init' | 'granted' | 'denied'
 | 
	
		
			
				|  |  | +  [key in TrackType]: 'init' | 'granted' | 'denied' | 'closed' | 'none'
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +export const START_LIVE_TIME = 'start-live-time'
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  const runtime = reactive({
 | 
	
		
			
				|  |  |    /** 房间id */
 | 
	
		
			
				|  |  |    roomUid: 'LIVE-2112263-12345',
 | 
	
	
		
			
				|  | @@ -188,14 +190,17 @@ export const shareScreenVideo = async () => {
 | 
	
		
			
				|  |  |      setTrack([screenTrack as RTC.RCLocalTrack], 'screen')
 | 
	
		
			
				|  |  |      if (runtime.videoRef) {
 | 
	
		
			
				|  |  |        screenTrack.play(runtime.videoRef)
 | 
	
		
			
				|  |  | +      runtime.screenShareStatus = true
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |      screenTrack?.on(RTC.RCLocalTrack.EVENT_LOCAL_TRACK_END, (track: RTC.RCLocalTrack) => {
 | 
	
		
			
				|  |  |        runtime.screenShareStatus = false
 | 
	
		
			
				|  |  |        track.destroy()
 | 
	
		
			
				|  |  |        // removeTrack([track], 'screen')
 | 
	
		
			
				|  |  | -      setTrack([oldTrack as RTC.RCLocalTrack], 'camera')
 | 
	
		
			
				|  |  | -      if (runtime.videoRef) {
 | 
	
		
			
				|  |  | -        oldTrack.play(runtime.videoRef)
 | 
	
		
			
				|  |  | +      if (oldTrack) {
 | 
	
		
			
				|  |  | +        setTrack([oldTrack as RTC.RCLocalTrack], 'camera')
 | 
	
		
			
				|  |  | +        if (runtime.videoRef) {
 | 
	
		
			
				|  |  | +          oldTrack.play(runtime.videoRef)
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |        }
 | 
	
		
			
				|  |  |        // setVideoSrcObject(runtime.videoRef, this.mediaStreams)
 | 
	
		
			
				|  |  |      })
 | 
	
	
		
			
				|  | @@ -230,8 +235,14 @@ export const getCameras = async () => {
 | 
	
		
			
				|  |  |   * 设置当前视频设备
 | 
	
		
			
				|  |  |   * @param camera MediaDeviceInfo
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | -export const setSelectCamera = (camera: MediaDeviceInfo) => {
 | 
	
		
			
				|  |  | +export const setSelectCamera = async (camera: MediaDeviceInfo) => {
 | 
	
		
			
				|  |  |    runtime.selectedCamera = camera
 | 
	
		
			
				|  |  | +  const oldTrack = runtime.activeTracks.camera as RTC.RCLocalTrack
 | 
	
		
			
				|  |  | +  if (oldTrack) {
 | 
	
		
			
				|  |  | +    await removeTrack([oldTrack], 'camera', oldTrack.isPublished())
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  const track = await getTrack('camera')
 | 
	
		
			
				|  |  | +  setTrack([track], 'camera', runtime.videoStatus === 'liveing')
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
	
		
			
				|  | @@ -239,8 +250,14 @@ export const setSelectCamera = (camera: MediaDeviceInfo) => {
 | 
	
		
			
				|  |  |   * 设置当前麦克风设备
 | 
	
		
			
				|  |  |   * @param microphone MediaDeviceInfo
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | -export const setSelectMicrophone = (microphone: MediaDeviceInfo) => {
 | 
	
		
			
				|  |  | +export const setSelectMicrophone = async (microphone: MediaDeviceInfo) => {
 | 
	
		
			
				|  |  |    runtime.selectedMicrophone = microphone
 | 
	
		
			
				|  |  | +  const oldTrack = runtime.activeTracks.microphone as RTC.RCLocalTrack
 | 
	
		
			
				|  |  | +  if (oldTrack) {
 | 
	
		
			
				|  |  | +    await removeTrack([oldTrack], 'microphone', oldTrack.isPublished())
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  | +  const track = await getTrack('microphone')
 | 
	
		
			
				|  |  | +  setTrack([track], 'microphone', runtime.videoStatus === 'liveing')
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  type TrackResult = {
 | 
	
	
		
			
				|  | @@ -259,14 +276,21 @@ export const getTrack = async (trackType: TrackType): Promise<RTC.RCLocalTrack>
 | 
	
		
			
				|  |  |      res = await runtime.rtcClient?.createCameraVideoTrack('RongCloudRTC', {
 | 
	
		
			
				|  |  |        cameraId: runtime.selectedCamera?.deviceId,
 | 
	
		
			
				|  |  |        faceMode: 'user',
 | 
	
		
			
				|  |  | -      frameRate: RTC.RCFrameRate.FPS_15,
 | 
	
		
			
				|  |  | -      resolution: RTC.RCResolution.W1280_H720,
 | 
	
		
			
				|  |  | +      frameRate: RTC.RCFrameRate.FPS_24,
 | 
	
		
			
				|  |  | +      resolution: RTC.RCResolution.W1920_H1080,
 | 
	
		
			
				|  |  |      }) as TrackResult
 | 
	
		
			
				|  |  |    } else {
 | 
	
		
			
				|  |  |      res = await runtime?.rtcClient?.createScreenVideoTrack() as TrackResult
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |    Track = res?.track as RTC.RCLocalTrack
 | 
	
		
			
				|  |  | +  if (trackType === 'camera' && !runtime.cameras.length) {
 | 
	
		
			
				|  |  | +    runtime.deviceStatus[trackType] = 'none'
 | 
	
		
			
				|  |  | +  } else if (trackType === 'microphone' && !runtime.microphones.length) {
 | 
	
		
			
				|  |  | +    runtime.deviceStatus[trackType] = 'none'
 | 
	
		
			
				|  |  | +  } else if (trackType === 'screen' && !runtime.screenShareStatus) {
 | 
	
		
			
				|  |  | +    runtime.deviceStatus[trackType] = 'none'
 | 
	
		
			
				|  |  | +  }
 | 
	
		
			
				|  |  |    if (res.code === RTC.RCRTCCode.PERMISSION_DENIED) {
 | 
	
		
			
				|  |  |      runtime.deviceStatus[trackType] = 'denied'
 | 
	
		
			
				|  |  |    } else {
 | 
	
	
		
			
				|  | @@ -311,6 +335,14 @@ export const removeTrack = async (tracks: RTC.RCLocalTrack[], trackType: TrackTy
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  export const joinRoom = async (roomId: string, type: RTC.RCLivingType, listenEvents: RTC.IRoomEventListener | null) => {
 | 
	
		
			
				|  |  | +  try {
 | 
	
		
			
				|  |  | +    await request.get('/api-web/imLiveBroadcastRoom/joinRoom', {
 | 
	
		
			
				|  |  | +      params: {
 | 
	
		
			
				|  |  | +        roomUid: runtime.roomUid,
 | 
	
		
			
				|  |  | +        userId: state.user?.speakerId,
 | 
	
		
			
				|  |  | +      }
 | 
	
		
			
				|  |  | +    })
 | 
	
		
			
				|  |  | +  } catch (error) {}
 | 
	
		
			
				|  |  |    await RongIMLib.joinChatRoom(roomId, {count: -1})
 | 
	
		
			
				|  |  |    const join = await runtime.rtcClient?.joinLivingRoom(roomId, type)
 | 
	
		
			
				|  |  |    if (join?.code != RTC.RCRTCCode.SUCCESS) throw Error('加入房间失败')
 | 
	
	
		
			
				|  | @@ -330,14 +362,15 @@ export const startLive = async () => {
 | 
	
		
			
				|  |  |      console.log(microphoneAudioTrack)
 | 
	
		
			
				|  |  |      const cameraVideoTrack = await getTrack('camera')
 | 
	
		
			
				|  |  |      await setTrack([cameraVideoTrack], 'camera')
 | 
	
		
			
				|  |  | -    runtime.videoStatus = 'liveing'
 | 
	
		
			
				|  |  |      await setTrack([microphoneAudioTrack], 'microphone')
 | 
	
		
			
				|  |  |      const builder = await runtime.joinedRoom?.getMCUConfigBuilder()
 | 
	
		
			
				|  |  |      // @ts-ignore
 | 
	
		
			
				|  |  |      await builder.setOutputVideoRenderMode?.(RTC.MixVideoRenderMode.WHOLE)
 | 
	
		
			
				|  |  |      // @ts-ignore
 | 
	
		
			
				|  |  |      await builder.flush()
 | 
	
		
			
				|  |  | +    runtime.videoStatus = 'liveing'
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  | +  sessionStorage.setItem(START_LIVE_TIME, dayjs().valueOf().toString())
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
	
		
			
				|  | @@ -345,14 +378,15 @@ export const startLive = async () => {
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  export const closeLive = async () => {
 | 
	
		
			
				|  |  |    // removeMedia(runtime.mediaStreams, runtime.mediaStreamTrack)
 | 
	
		
			
				|  |  | -  await request.post('/api-im/user/statusImUser', {
 | 
	
		
			
				|  |  | -    data: {
 | 
	
		
			
				|  |  | -      os: 'PC',
 | 
	
		
			
				|  |  | -      status: 2,
 | 
	
		
			
				|  |  | -      userId: state.user?.id,
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -  })
 | 
	
		
			
				|  |  | -  runtime.videoStatus = 'stopped'
 | 
	
		
			
				|  |  | +  // await request.post('/api-im/user/statusImUser', {
 | 
	
		
			
				|  |  | +  //   data: {
 | 
	
		
			
				|  |  | +  //     os: 'PC',
 | 
	
		
			
				|  |  | +  //     status: 3,
 | 
	
		
			
				|  |  | +  //     userId: state.user?.id,
 | 
	
		
			
				|  |  | +  //   }
 | 
	
		
			
				|  |  | +  // })
 | 
	
		
			
				|  |  | +  sessionStorage.removeItem(START_LIVE_TIME)
 | 
	
		
			
				|  |  | +  runtime.videoStatus = 'stream'
 | 
	
		
			
				|  |  |    for (const key in runtime.activeTracks) {
 | 
	
		
			
				|  |  |      if (Object.prototype.hasOwnProperty.call(runtime.activeTracks, key)) {
 | 
	
		
			
				|  |  |        const track = runtime.activeTracks[key as TrackType] as RTC.RCLocalTrack
 | 
	
	
		
			
				|  | @@ -367,7 +401,7 @@ export const closeLive = async () => {
 | 
	
		
			
				|  |  |   * 同步点赞数量
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  |  export const loopSyncLike = async () => {
 | 
	
		
			
				|  |  | -  if (runtime.likeCount !== runtime.lastLikeCount || runtime.likeCount === 0) {
 | 
	
		
			
				|  |  | +  if ((runtime.likeCount !== runtime.lastLikeCount || runtime.likeCount === 0) && state.user) {
 | 
	
		
			
				|  |  |      try {
 | 
	
		
			
				|  |  |        await request.get('/api-web/imLiveBroadcastRoom/syncLike', {
 | 
	
		
			
				|  |  |          hideLoading: true,
 | 
	
	
		
			
				|  | @@ -454,8 +488,14 @@ export const toggleDevice = async (trackType: TrackType) => {
 | 
	
		
			
				|  |  |    const track = runtime.activeTracks[trackType]
 | 
	
		
			
				|  |  |    const needPublish = runtime.videoStatus === 'liveing'
 | 
	
		
			
				|  |  |    if (track) {
 | 
	
		
			
				|  |  | +    if (trackType === 'camera') {
 | 
	
		
			
				|  |  | +      runtime.deviceStatus.camera = 'closed'
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |      closeDevice(trackType, needPublish)
 | 
	
		
			
				|  |  |    } else {
 | 
	
		
			
				|  |  | +    if (trackType === 'camera') {
 | 
	
		
			
				|  |  | +      runtime.deviceStatus.camera = 'granted'
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |      openDevice(trackType, needPublish)
 | 
	
		
			
				|  |  |    }
 | 
	
		
			
				|  |  |  }
 |