rtc-client.js 4.34 KB
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)
    })
  }

}