index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { defineComponent, ref } from 'vue'
  2. import * as RTC from '@rongcloud/plugin-rtc'
  3. import Header from './header'
  4. import ActionBar 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 { removeMedia } from './helpers'
  10. import styles from './index.module.less'
  11. const videoRef = ref<HTMLVideoElement | null>(null)
  12. let microphoneAudioTrack: RTC.RCLocalTrack
  13. let cameraVideoTrack: RTC.RCLocalTrack
  14. export default defineComponent({
  15. name: 'LiveBroadcast',
  16. async mounted() {
  17. this.initializeRoom()
  18. RuntimeUtils.loopSyncLike()
  19. event.on(LIVE_EVENT_MESSAGE['RC:Chatroom:Like'], this.onLikeMessage)
  20. },
  21. beforeUnmount() {
  22. event.off(LIVE_EVENT_MESSAGE['RC:Chatroom:Like'], this.onLikeMessage)
  23. },
  24. methods: {
  25. onLikeMessage(msg: any) {
  26. runtime.likeCount += msg.counts
  27. },
  28. async initializeRoom () {
  29. if (!state.user) throw Error('请先登录')
  30. try {
  31. await RuntimeUtils.connectIM(state.user?.imToken)
  32. runtime.videoRef = videoRef.value
  33. await RuntimeUtils.getMicrophones()
  34. await RuntimeUtils.getCameras()
  35. RuntimeUtils.setSelectCamera(runtime.cameras[0])
  36. RuntimeUtils.setSelectMicrophone(runtime.microphones[0])
  37. cameraVideoTrack = await RuntimeUtils.getTrack('camera')
  38. runtime.videoRef && cameraVideoTrack.play(runtime.videoRef)
  39. await RuntimeUtils.setTrack([cameraVideoTrack], 'camera')
  40. microphoneAudioTrack = await RuntimeUtils.getTrack('microphone')
  41. runtime.videoRef && microphoneAudioTrack.play(runtime.videoRef)
  42. console.log(runtime.deviceStatus)
  43. runtime.videoStatus = 'stream'
  44. const join = await RuntimeUtils.joinRoom(state.user?.roomUid, RTC.RCLivingType.VIDEO, {
  45. onMessageReceive(name, content) {
  46. console.log(name, content)
  47. },
  48. onKickOff(byServer: boolean) {
  49. console.log(byServer)
  50. },
  51. async onTrackPublish (tracks: RTC.RCRemoteTrack[]) {
  52. const subscribeRes = await join?.room?.subscribe(tracks)
  53. console.log(subscribeRes)
  54. if (subscribeRes?.code && subscribeRes.code !== RTC.RCRTCCode.SUCCESS) {
  55. console.log('资源订阅失败 ->', subscribeRes.code)
  56. }
  57. },
  58. onTrackUnpublish(tracks: RTC.RCRemoteTrack[]) {
  59. console.log(tracks)
  60. event.emit(LIVE_EVENT_MESSAGE['RM:RTC:TrackUnpublish'], tracks)
  61. },
  62. onSwitchRole(userId: string, role: RTC.RCRTCLiveRole) {
  63. event.emit(LIVE_EVENT_MESSAGE['RM:RTC:SwitchRole'], {
  64. userId,
  65. role,
  66. })
  67. },
  68. onTrackReady (track: RTC.RCRemoteTrack) {
  69. if (track.isAudioTrack()) {
  70. // 音轨不需要传递播放控件
  71. track.play()
  72. }
  73. },
  74. onUserJoin (userIds: string[]) {
  75. console.log('onUserJoin', userIds)
  76. },
  77. onUserLeave (userIds: string[]) {
  78. event.emit(LIVE_EVENT_MESSAGE['RM:RTC:UserLeave'], userIds)
  79. console.log('onUserLeave', userIds)
  80. },
  81. })
  82. if (join.room && join.code === RTC.RCRTCCode.SUCCESS) {
  83. runtime.joinedRoom = join.room
  84. }
  85. } catch (error) {
  86. runtime.videoStatus = 'error'
  87. console.log(error)
  88. }
  89. },
  90. closeLive() {
  91. removeMedia(runtime.mediaStreams, runtime.mediaStreamTrack)
  92. runtime.videoStatus = 'stopped'
  93. },
  94. },
  95. render() {
  96. return (
  97. <div class={styles.main}>
  98. <Header/>
  99. <div class={styles.video}>
  100. <video ref={videoRef}></video>
  101. <VideoStatus/>
  102. </div>
  103. <ActionBar/>
  104. {/* <div>video: {runtime.videoStatus}, imStatus: {runtime.imConnectStatus}</div> */}
  105. </div>
  106. )
  107. }
  108. })