|
@@ -1,117 +1,71 @@
|
|
|
import { defineComponent, ref } from 'vue'
|
|
|
-import * as RongIMLib from '@rongcloud/imlib-next'
|
|
|
-import { installer, RCRTCCode, RCRTCClient, RCLocalTrack } from '@rongcloud/plugin-rtc'
|
|
|
-import runtime from './runtime'
|
|
|
-import { requireMedia, removeMedia } from './helpers'
|
|
|
-
|
|
|
-const RONG_IM_TOKEN = 'c9kqb3rdc451j'
|
|
|
-
|
|
|
-RongIMLib.init({
|
|
|
- appkey: RONG_IM_TOKEN,
|
|
|
-})
|
|
|
-
|
|
|
-/**
|
|
|
- * 监听消息通知
|
|
|
- */
|
|
|
- const Events = RongIMLib.Events;
|
|
|
- RongIMLib.addEventListener(Events.MESSAGES, (event) => {
|
|
|
- console.log('received messages', event.messages)
|
|
|
- })
|
|
|
-
|
|
|
- /**
|
|
|
- * 监听 IM 连接状态变化
|
|
|
- */
|
|
|
- RongIMLib.addEventListener(Events.CONNECTING, () => {
|
|
|
- runtime.imConnectStatus = 'connecting'
|
|
|
- })
|
|
|
-
|
|
|
- RongIMLib.addEventListener(Events.CONNECTED, () => {
|
|
|
- runtime.imConnectStatus = 'connected'
|
|
|
- })
|
|
|
-
|
|
|
- RongIMLib.addEventListener(Events.DISCONNECT, () => {
|
|
|
- runtime.imConnectStatus = 'disconnect'
|
|
|
- })
|
|
|
-
|
|
|
- let rtcClient: RCRTCClient | null
|
|
|
-
|
|
|
- RongIMLib.connect('hXAfqkZDs146m6MjMukBMEHvmRoZQV7Hgh8ZG4iscyE=@n56a.cn.rongnav.com;n56a.cn.rongcfg.com').then((user) => {
|
|
|
- console.log('connect success', user.data?.userId);
|
|
|
- rtcClient = RongIMLib.installPlugin(installer, { /*初始化参数请参考下方参数说明*/ })
|
|
|
-})
|
|
|
-.catch((error) => {
|
|
|
- console.log(`连接失败: ${error}`);
|
|
|
-});
|
|
|
-
|
|
|
+import * as RTC from '@rongcloud/plugin-rtc'
|
|
|
+import Header from './header'
|
|
|
+import ActionBar from './action-bar'
|
|
|
+import runtime, * as RuntimeUtils from './runtime'
|
|
|
+import { removeMedia } from './helpers'
|
|
|
|
|
|
const videoRef = ref<HTMLVideoElement | null>(null)
|
|
|
|
|
|
-type VideoStatus = 'init' | 'stream' | 'liveing' | 'stopped' | 'error' | 'loading'
|
|
|
+const mediaStreams = new MediaStream()
|
|
|
|
|
|
export default defineComponent({
|
|
|
name: 'LiveBroadcast',
|
|
|
- data () {
|
|
|
- return {
|
|
|
- mediaStreamTrack: [] as MediaStreamTrack[],
|
|
|
- mediaStreams: null as MediaStream | null,
|
|
|
- videoStatus: 'init' as VideoStatus,
|
|
|
- screenShareStatus: false,
|
|
|
- }
|
|
|
- },
|
|
|
async mounted() {
|
|
|
- try {
|
|
|
- const mediaStreams = await requireMedia({
|
|
|
- audio: true,
|
|
|
- video: true
|
|
|
- })
|
|
|
- this.videoStatus = 'stream'
|
|
|
- this.mediaStreams = mediaStreams
|
|
|
- this.mediaStreamTrack = mediaStreams.getTracks()
|
|
|
- this.setVideoSrcObject(mediaStreams)
|
|
|
- } catch (error) {
|
|
|
- this.videoStatus = 'error'
|
|
|
- console.log(error)
|
|
|
- }
|
|
|
+ this.initializeRoom()
|
|
|
},
|
|
|
methods: {
|
|
|
- setVideoSrcObject (mediaStreams: MediaStream | null) {
|
|
|
- const video = videoRef.value
|
|
|
- if (video && mediaStreams) {
|
|
|
- video.srcObject = mediaStreams
|
|
|
- video.onloadedmetadata = () => {
|
|
|
- video.play()
|
|
|
+ async initializeRoom () {
|
|
|
+ try {
|
|
|
+ await RuntimeUtils.connectIM()
|
|
|
+ runtime.videoRef = videoRef.value
|
|
|
+ await RuntimeUtils.getMicrophones()
|
|
|
+ await RuntimeUtils.getCameras()
|
|
|
+ RuntimeUtils.setSelectCamera(runtime.cameras[0])
|
|
|
+ RuntimeUtils.setSelectMicrophone(runtime.microphones[0])
|
|
|
+ const createTracks = await runtime.rtcClient?.createMicrophoneAndCameraTracks('RongCloudRTC', {
|
|
|
+ audio: {
|
|
|
+ micphoneId: runtime.selectedMicrophone?.deviceId,
|
|
|
+ },
|
|
|
+ video: {
|
|
|
+ cameraId: runtime.selectedCamera?.deviceId,
|
|
|
+ faceMode: 'user',
|
|
|
+ frameRate: RTC.RCFrameRate.FPS_15,
|
|
|
+ resolution: RTC.RCResolution.W640_H360,
|
|
|
+ }
|
|
|
+ })
|
|
|
+ if (createTracks?.code === RTC.RCRTCCode.SUCCESS) {
|
|
|
+ for (const track of createTracks.tracks) {
|
|
|
+ // @ts-ignore
|
|
|
+ await mediaStreams.addTrack(track._msTrack)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ runtime.videoStatus = 'error'
|
|
|
}
|
|
|
+ runtime.videoStatus = 'stream'
|
|
|
+ runtime.mediaStreams = mediaStreams
|
|
|
+ runtime.mediaStreamTrack = mediaStreams.getTracks()
|
|
|
+ RuntimeUtils.setVideoSrcObject(runtime.videoRef, mediaStreams)
|
|
|
+ const join = await RuntimeUtils.joinRoom('1', RTC.RCLivingType.VIDEO, {
|
|
|
+
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ runtime.videoStatus = 'error'
|
|
|
+ console.log(error)
|
|
|
}
|
|
|
},
|
|
|
closeLive() {
|
|
|
- removeMedia(this.mediaStreams, this.mediaStreamTrack)
|
|
|
- this.videoStatus = 'stopped'
|
|
|
+ removeMedia(runtime.mediaStreams, runtime.mediaStreamTrack)
|
|
|
+ runtime.videoStatus = 'stopped'
|
|
|
},
|
|
|
- async screenVideo() {
|
|
|
- if (rtcClient) {
|
|
|
- const { code, track: screenTrack } = await rtcClient.createScreenVideoTrack()
|
|
|
- if (code !== RCRTCCode.SUCCESS) {
|
|
|
- this.screenShareStatus = false
|
|
|
- } else {
|
|
|
- this.screenShareStatus = true
|
|
|
- // @ts-ignore
|
|
|
- this.setVideoSrcObject(screenTrack?._msStream as MediaStream)
|
|
|
- }
|
|
|
- screenTrack?.on(RCLocalTrack.EVENT_LOCAL_TRACK_END, (track: RCLocalTrack) => {
|
|
|
- console.log('screen track end', track)
|
|
|
- this.screenShareStatus = false
|
|
|
- this.setVideoSrcObject(this.mediaStreams)
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
},
|
|
|
render() {
|
|
|
return (
|
|
|
<div>
|
|
|
+ <Header/>
|
|
|
<video ref={videoRef}></video>
|
|
|
- <button onClick={this.closeLive}>关闭直播</button>
|
|
|
- <button onClick={this.screenVideo}>屏幕共享</button>
|
|
|
- <div>video: {this.videoStatus}, imStatus: {runtime.imConnectStatus}</div>
|
|
|
+ <ActionBar/>
|
|
|
+ <div>video: {runtime.videoStatus}, imStatus: {runtime.imConnectStatus}</div>
|
|
|
</div>
|
|
|
)
|
|
|
}
|