import { agora } from '@config' ; import * as browser from '@app/common/browser' ; export default function ( AgoraRTC , channel ) { if ( ! AgoraRTC . checkSystemRequirements ( ) ) { return false ; } let client , localStream , camera , microphone , channel_key = null , streamList = [ ] ; client = AgoraRTC . createClient ( { mode : 'live' } ) ; client . init ( agora . appid , function ( ) { client . join ( channel_key , channel , null , function ( uid ) { } , function ( err ) { console . log ( "Join channel failed" , err ) ; } ) ; } , function ( err ) { console . log ( "AgoraRTC client init failed" , err ) ; } ) ; client . on ( 'stream-added' , function ( evt ) { var stream = evt . stream ; console . log ( "New stream added: " + stream . getId ( ) ) ; console . log ( "Subscribe " , stream ) ; client . subscribe ( stream , function ( err ) { console . log ( "Subscribe stream failed" , err ) ; } ) ; } ) ; client . on ( 'stream-subscribed' , function ( evt ) { var stream = evt . stream ; if ( $ ( 'div#video #agora_remote' + stream . getId ( ) ) . length === 0 ) { let style = '' ; if ( ! stream . video ) style = 'display:none' ; $ ( 'div#video' ) . append ( '<div id="agora_remote' + stream . getId ( ) + '" style="' + style + '"></div>' ) ; } if ( browser . isSafari ( ) ) { streamList . push ( stream ) ; } else { stream . play ( 'agora_remote' + stream . getId ( ) ) ; } } ) ; client . on ( 'stream-removed' , function ( evt ) { var stream = evt . stream ; stream . stop ( ) ; $ ( '#agora_remote' + stream . getId ( ) ) . remove ( ) ; console . log ( "Remote stream is removed " + stream . getId ( ) ) ; } ) ; client . on ( 'peer-leave' , function ( evt ) { var stream = evt . stream ; if ( stream ) { stream . stop ( ) ; $ ( '#agora_remote' + stream . getId ( ) ) . remove ( ) ; console . log ( evt . uid + " leaved from this channel" ) ; } } ) ; client . on ( "first-audio-frame-decode" , function ( evt ) { console . log ( '音频回调函数' ) ; } ) ; return { play : ( ) => { streamList . map ( stream => { stream . play ( 'agora_remote' + stream . getId ( ) ) ; } ) ; } , stop : ( ) => { streamList . map ( stream => { stream . stop ( ) ; } ) ; } } ; }