index.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { defineComponent, ref } from 'vue'
  2. import * as RTC from '@rongcloud/plugin-rtc'
  3. import Header from './header'
  4. import ActionBar, { state as ActionBarRuntime} from './action-bar'
  5. import VideoStatus from './video-status'
  6. import { state } from '/src/state'
  7. import event, { LIVE_EVENT_MESSAGE } from './event'
  8. import runtime, * as RuntimeUtils from './runtime'
  9. import Chronography from './chronography'
  10. // import { removeMedia } from './helpers'
  11. import styles from './index.module.less'
  12. const videoRef = ref<HTMLVideoElement | null>(null)
  13. let microphoneAudioTrack: RTC.RCLocalTrack
  14. let cameraVideoTrack: RTC.RCLocalTrack
  15. export default defineComponent({
  16. name: 'LiveBroadcast',
  17. async mounted() {
  18. this.initializeRoom()
  19. RuntimeUtils.loopSyncLike()
  20. event.on(LIVE_EVENT_MESSAGE['RC:Chatroom:Like'], this.onLikeMessage)
  21. window.onbeforeunload = this.beforeunload
  22. },
  23. beforeUnmount() {
  24. event.off(LIVE_EVENT_MESSAGE['RC:Chatroom:Like'], this.onLikeMessage)
  25. window.onbeforeunload = null
  26. },
  27. methods: {
  28. beforeunload() {
  29. if (runtime.videoStatus === 'liveing') {
  30. return '当前正在直播中是否确认关闭页面?'
  31. }
  32. },
  33. onLikeMessage(msg: any) {
  34. runtime.likeCount += msg.counts
  35. },
  36. getDeviceByDeviceType(type: RuntimeUtils.TrackType) {
  37. const videoDeviceId = localStorage.getItem(RuntimeUtils.VIDEO_DEVICE_ID)
  38. const audioDeviceId = localStorage.getItem(RuntimeUtils.AUDIO_DEVICE_ID)
  39. if (type === 'camera') {
  40. if (videoDeviceId) {
  41. return runtime.cameras.find(camera => camera.deviceId === videoDeviceId) || runtime.cameras[0]
  42. }
  43. return runtime.cameras[0]
  44. }
  45. if (audioDeviceId) {
  46. return runtime.microphones.find(microphone => microphone.deviceId === audioDeviceId) || runtime.microphones[0]
  47. }
  48. return runtime.microphones[0]
  49. },
  50. async initializeRoom () {
  51. if (!state.user) throw Error('请先登录')
  52. try {
  53. // IM连接
  54. await RuntimeUtils.connectIM(state.user?.imToken)
  55. runtime.videoRef = videoRef.value
  56. // 获取设备
  57. await RuntimeUtils.getMicrophones()
  58. await RuntimeUtils.getCameras()
  59. // 设置播放设备
  60. RuntimeUtils.setSelectCamera(this.getDeviceByDeviceType('camera'))
  61. RuntimeUtils.setSelectMicrophone(this.getDeviceByDeviceType('microphone'))
  62. cameraVideoTrack = await RuntimeUtils.getTrack('camera')
  63. runtime.videoRef && cameraVideoTrack.play(runtime.videoRef)
  64. await RuntimeUtils.setTrack([cameraVideoTrack], 'camera', false)
  65. // microphoneAudioTrack = await RuntimeUtils.getTrack('microphone')
  66. // microphoneAudioTrack.play()
  67. // console.log(microphoneAudioTrack)
  68. // console.log(runtime.deviceStatus)
  69. runtime.videoStatus = 'stream'
  70. const join = await RuntimeUtils.joinRoom(state.user?.roomUid, RTC.RCLivingType.VIDEO, {
  71. onMessageReceive(name, content) {
  72. console.log(name, content)
  73. },
  74. onKickOff(byServer: boolean) {
  75. console.log(byServer)
  76. },
  77. async onTrackPublish (tracks: RTC.RCRemoteTrack[]) {
  78. const subscribeRes = await join?.room?.subscribe(tracks)
  79. console.log(subscribeRes)
  80. if (subscribeRes?.code && subscribeRes.code !== RTC.RCRTCCode.SUCCESS) {
  81. console.log('资源订阅失败 ->', subscribeRes.code)
  82. }
  83. },
  84. onTrackUnpublish(tracks: RTC.RCRemoteTrack[]) {
  85. console.log(tracks)
  86. event.emit(LIVE_EVENT_MESSAGE['RM:RTC:TrackUnpublish'], tracks)
  87. },
  88. onSwitchRole(userId: string, role: RTC.RCRTCLiveRole) {
  89. event.emit(LIVE_EVENT_MESSAGE['RM:RTC:SwitchRole'], {
  90. userId,
  91. role,
  92. })
  93. },
  94. onTrackReady (track: RTC.RCRemoteTrack) {
  95. if (track.isAudioTrack()) {
  96. // 音轨不需要传递播放控件
  97. track.play()
  98. }
  99. },
  100. onUserJoin (userIds: string[]) {
  101. console.log('onUserJoin', userIds)
  102. },
  103. onUserLeave (userIds: string[]) {
  104. event.emit(LIVE_EVENT_MESSAGE['RM:RTC:UserLeave'], userIds)
  105. console.log('onUserLeave', userIds)
  106. },
  107. })
  108. if (join.room && join.code === RTC.RCRTCCode.SUCCESS) {
  109. runtime.joinedRoom = join.room
  110. }
  111. if (sessionStorage.getItem(RuntimeUtils.START_LIVE_STATUS) === 'liveing') {
  112. await RuntimeUtils.startLive(false)
  113. runtime.videoStatus = 'liveing'
  114. }
  115. const volume =localStorage.getItem(RuntimeUtils.AUDIO_DEVICE_VOLUME)
  116. if (volume) {
  117. ActionBarRuntime.volume = parseInt(volume)
  118. RuntimeUtils.setVolume(parseInt(volume))
  119. }
  120. } catch (error) {
  121. runtime.videoStatus = 'error'
  122. console.log(error)
  123. }
  124. },
  125. closeLive() {
  126. // removeMedia(runtime.mediaStreams, runtime.mediaStreamTrack)
  127. runtime.videoStatus = 'stream'
  128. },
  129. },
  130. render() {
  131. return (
  132. <div class={styles.main}>
  133. <Header/>
  134. <div class={styles.video}>
  135. <video ref={videoRef}></video>
  136. {!runtime.screenShareStatus ? <VideoStatus/> : null}
  137. {runtime.videoStatus === 'liveing' ? <Chronography/> : null}
  138. </div>
  139. <ActionBar/>
  140. {/* <div>video: {runtime.videoStatus}, imStatus: {runtime.imConnectStatus}</div> */}
  141. </div>
  142. )
  143. }
  144. })