rtc-client.js
4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { addView, removeView} from './common'
console.log('agora sdk version: ' + AgoraRTC.VERSION + ' compatible: ' + AgoraRTC.checkSystemRequirements())
export default class RTCClient {
constructor () {
this._client = null
this._joined = false
this._published = false
this._localStream = null
this._remoteStreams = []
this._rtsStreamProfile = {}
this._params = {}
this._showProfile = false
}
handleEvents() {
this._client.on('error', (err) => {
console.log(err)
})
// Occurs when the peer user leaves the channel; for example, the peer user calls Client.leave.
this._client.on('peer-leave', (evt) => {
const id = evt.uid
if (id != this._params.uid) {
removeView(id)
}
// Toast.notice('peer leave')
console.log('peer-leave', id)
})
// Occurs when the remote stream is added.
this._client.on('stream-added', (evt) => {
const remoteStream = evt.stream
const id = remoteStream.getId()
// Toast.info('stream-added uid: ' + id)
if (id !== this._params.uid && remoteStream.hasVideo()) {
this._client.subscribe(remoteStream, (err) => {
console.log('stream subscribe failed', err)
})
}
console.log('stream-added remote-uid: ', id)
})
// Occurs when a user subscribes to a remote stream.
this._client.on('stream-subscribed', (evt) => {
const remoteStream = evt.stream
const id = remoteStream.getId()
this._remoteStreams.push(remoteStream)
addView(id)
remoteStream.play('remote_video_' + id, {fit: 'cover'})
// Toast.info('stream-subscribed remote-uid: ' + id)
console.log('stream-subscribed remote-uid: ', id)
})
// Occurs when the remote stream is removed; for example, a peer user calls Client.unpublish.
this._client.on('stream-removed', (evt) => {
const remoteStream = evt.stream
const id = remoteStream.getId()
// Toast.info('stream-removed uid: ' + id)
remoteStream.stop()
this._remoteStreams = this._remoteStreams.filter((stream) => {
return stream.getId() !== id
})
removeView(id)
console.log('stream-removed remote-uid: ', id)
})
}
join (sid) {
this.leave();
return new Promise((resolve, reject) => {
this._client = AgoraRTC.createClient({mode: 'live', codec: 'h264'})
AgoraRTS.init(AgoraRTC, {
wasmDecoderPath: 'AgoraRTS.wasm',
asmDecoderPath: 'AgoraRTS.asm',
bufferDelay: 3000,
maxBufferDelay: 10000,
}).catch(e => {
if (e === 'LOAD_DECODER_FAILED') {
console.log('Codec load failed!')
}
})
AgoraRTS.proxy(this._client)
this._params = {
sid
}
// handle AgoraRTC client event
this.handleEvents()
// init client
this._client.init('ac385634df8f4f62b4357ab35c54e88e', () => {
console.log('init success')
this._client.join(null, sid, null, (uid) => {
this._params.uid = uid
// Toast.notice('join channel: ' + sid + ' success, uid: ' + uid)
console.log('join channel: ' + sid + ' success, uid: ' + uid)
this._joined = true
// start stream interval stats
}, function(err) {
// Toast.error('client join failed, please open console see more detail')
console.error('client join failed', err)
})
}, (err) => {
// Toast.error('client init failed, please open console see more detail')
console.error(err)
})
})
}
leave () {
if (!this._client) {
// Toast.error('Please Join First!')
return
}
if (!this._joined) {
// Toast.error('You are not in channel')
return
}
// leave channel
this._client.leave(() => {
// stop stream
while (this._remoteStreams.length > 0) {
const stream = this._remoteStreams.shift()
const id = stream.getId()
stream.stop()
removeView(id)
}
this._localStream = null
this._remoteStreams = []
this._client = null
console.log('client leaves channel success')
this._published = false
this._joined = false
// Toast.notice('leave success')
}, (err) => {
console.log('channel leave failed')
// Toast.error('leave success')
console.error(err)
})
}
}