No Description

agora.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import React, {Component, PureComponent} from 'react';
  2. import {
  3. StyleSheet, Text, View, TouchableOpacity,
  4. Image, Dimensions, Modal, Platform, NativeModules
  5. } from 'react-native';
  6. import {RtcEngine, AgoraView} from 'react-native-agora';
  7. import {
  8. APPID,
  9. isIphoneX, isIphoneXR
  10. } from '../utils';
  11. const {Agora} = NativeModules;
  12. if (!Agora) {
  13. throw new Error("Agora load failed in react-native, please check ur compiler environments");
  14. }
  15. const {
  16. FPS30,
  17. FixedLandscape,
  18. Host,
  19. AudioProfileDefault,
  20. AudioScenarioDefault,
  21. } = Agora;
  22. const BtnEndCall = () => require('../assets/btn_endcall.png');
  23. const BtnMute = () => require('../assets/btn_mute.png');
  24. const BtnSpeaker = () => require('../assets/btn_speaker.png');
  25. const BtnSwitchCamera = () => require('../assets/btn_switch_camera.png');
  26. const BtnVideo = () => require('../assets/btn_video.png');
  27. const EnableCamera = () => require('../assets/enable_camera.png');
  28. const DisableCamera = () => require('../assets/disable_camera.png');
  29. const EnablePhotoflash = () => require('../assets/enable_photoflash.png');
  30. const DisablePhotoflash = () => require('../assets/disable_photoflash.png');
  31. const IconMuted = () => require('../assets/icon_muted.png');
  32. const IconSpeaker = () => require('../assets/icon_speaker.png');
  33. const {width, height} = Dimensions.get('window');
  34. const safeTop = (top) => (isIphoneX(Platform, width, height) ?
  35. (top + 88) :
  36. (isIphoneXR(Platform, width, height) ? ( top + 64 ) : top)
  37. );
  38. const styles = StyleSheet.create({
  39. container: {
  40. flex: 1,
  41. backgroundColor: '#F4F4F4'
  42. },
  43. absView: {
  44. position: 'absolute',
  45. top: safeTop(0),
  46. left: 0,
  47. right: 0,
  48. bottom: 0,
  49. justifyContent: 'space-between',
  50. },
  51. videoView: {
  52. padding: 5,
  53. flexWrap: 'wrap',
  54. flexDirection: 'row',
  55. zIndex: 100
  56. },
  57. localView: {
  58. flex: 1
  59. },
  60. remoteView: {
  61. width: (width - 40) / 3,
  62. height: (width - 40) / 3,
  63. margin: 5
  64. },
  65. bottomView: {
  66. padding: 20,
  67. flexDirection: 'row',
  68. justifyContent: 'space-around'
  69. }
  70. });
  71. class OperateButton extends PureComponent {
  72. render() {
  73. const {onPress, source, style, imgStyle = {width: 50, height: 50}} = this.props;
  74. return (
  75. <TouchableOpacity
  76. style={style}
  77. onPress={onPress}
  78. activeOpacity={.7}
  79. >
  80. <Image
  81. style={imgStyle}
  82. source={source}
  83. />
  84. </TouchableOpacity>
  85. )
  86. }
  87. }
  88. type Props = {
  89. channelProfile: Number,
  90. channelName: String,
  91. videoProfile: Number,
  92. clientRole: Number,
  93. onCancel: Function
  94. }
  95. export default class AgoraComponent extends Component<Props> {
  96. state = {
  97. peerIds: [],
  98. joinSucceed: false,
  99. isSpeak: true,
  100. isMute: false,
  101. isCameraTorch: false,
  102. disableVideo: false,
  103. hideButton: false,
  104. visible: false,
  105. selectedUid: undefined,
  106. };
  107. componentWillMount () {
  108. const config = {
  109. appid: APPID,
  110. channelProfile: this.props.channelProfile,
  111. videoProfile: this.props.videoProfile,
  112. clientRole: this.props.clientRole,
  113. videoEncoderConfig: {
  114. width: 360,
  115. height: 480,
  116. bitrate: 1,
  117. frameRate: FPS30,
  118. orientationMode: FixedLandscape,
  119. },
  120. clientRole: Host,
  121. audioProfile: AudioProfileDefault,
  122. audioScenario: AudioScenarioDefault
  123. }
  124. console.log("[CONFIG]", JSON.stringify(config));
  125. console.log("[CONFIG.encoderConfig", config.videoEncoderConfig);
  126. RtcEngine.init(config);
  127. }
  128. componentDidMount () {
  129. RtcEngine.getSdkVersion((version) => {
  130. console.log('[RtcEngine] getSdkVersion', version);
  131. })
  132. console.log('[joinChannel] ' + this.props.channelName);
  133. RtcEngine.joinChannel(this.props.channelName);
  134. RtcEngine.enableAudioVolumeIndication(500, 3);
  135. RtcEngine.eventEmitter({
  136. onFirstRemoteVideoDecoded: (data) => {
  137. console.log('[RtcEngine] onFirstRemoteVideoDecoded', data);
  138. },
  139. onUserJoined: (data) => {
  140. console.log('[RtcEngine] onUserJoined', data);
  141. const {peerIds} = this.state;
  142. if (peerIds.indexOf(data.uid) !== -1) {
  143. this.setState({
  144. peerIds: [...peerIds, data.uid]
  145. })
  146. }
  147. },
  148. onUserOffline: (data) => {
  149. console.log('[RtcEngine] onUserOffline', data);
  150. this.setState({
  151. peerIds: this.state.peerIds.filter(uid => uid !== data.uid)
  152. })
  153. },
  154. onJoinChannelSuccess: (data) => {
  155. console.log('[RtcEngine] onJoinChannelSuccess', data);
  156. RtcEngine.startPreview();
  157. this.setState({
  158. joinSucceed: true
  159. })
  160. const {peerIds} = this.state;
  161. if (peerIds.indexOf(data.uid) !== -1) {
  162. this.setState({
  163. peerIds: [...peerIds, data.uid]
  164. })
  165. }
  166. },
  167. onAudioVolumeIndication: (data) => {
  168. console.log('[RtcEngine] onAudioVolumeIndication', data);
  169. },
  170. onClientRoleChanged: (data) => {
  171. console.log("[RtcEngine] onClientRoleChanged", data);
  172. },
  173. onError: (data) => {
  174. console.log('[RtcEngine] onError', data);
  175. if (data.error === 17) {
  176. RtcEngine.leaveChannel().then(_ => {
  177. RtcEngine.destroy();
  178. this.props.onCancel(data);
  179. });
  180. }
  181. }
  182. })
  183. }
  184. componentWillUnmount () {
  185. if (this.state.joinSucceed) {
  186. RtcEngine.leaveChannel();
  187. RtcEngine.destroy();
  188. }
  189. }
  190. handleCancel = () => {
  191. RtcEngine.leaveChannel();
  192. RtcEngine.destroy();
  193. this.props.onCancel();
  194. }
  195. switchCamera = () => {
  196. RtcEngine.switchCamera();
  197. }
  198. toggleAllRemoteAudioStreams = () => {
  199. this.setState({
  200. isMute: !this.state.isMute
  201. }, () => {
  202. RtcEngine.muteAllRemoteAudioStreams(this.state.isMute);
  203. })
  204. }
  205. toggleSpeakerPhone = () => {
  206. this.setState({
  207. isSpeak: !this.state.isSpeak
  208. }, () => {
  209. RtcEngine.setDefaultAudioRouteToSpeakerphone(this.state.isSpeak);
  210. })
  211. }
  212. toggleCameraTorch = () => {
  213. this.setState({
  214. isCameraTorch: !this.state.isCameraTorch
  215. }, () => {
  216. RtcEngine.setCameraTorchOn(this.state.isCameraTorch).then(val => {
  217. console.log("setCameraTorch", val);
  218. })
  219. })
  220. }
  221. toggleVideo = () => {
  222. this.setState({
  223. disableVideo: !this.state.videodisableVideo
  224. }, () => {
  225. this.state.disableVideo ? RtcEngine.enableVideo() : RtcEngine.disableVideo()
  226. });
  227. }
  228. toggleHideButtons = () => {
  229. this.setState({
  230. hideButton: !this.state.hideButton
  231. })
  232. }
  233. onPressVideo = (uid) => {
  234. this.setState({
  235. selectedUid: uid
  236. }, () => {
  237. this.setState({
  238. visible: true
  239. })
  240. })
  241. }
  242. buttonsView = ({hideButton, isCameraTorch, disableVideo, isMute, isSpeaker}) => {
  243. if (!hideButton) {
  244. return (
  245. <View>
  246. <OperateButton
  247. style={{alignSelf: 'center', marginBottom: -10}}
  248. onPress={this.handleCancel}
  249. imgStyle={{width: 60, height: 60}}
  250. source={BtnEndCall()}
  251. />
  252. <View style={styles.bottomView}>
  253. <OperateButton
  254. onPress={this.toggleCameraTorch}
  255. imgStyle={{width: 40, height: 40}}
  256. source={isCameraTorch ? EnablePhotoflash() : DisablePhotoflash()}
  257. />
  258. <OperateButton
  259. onPress={this.toggleVideo}
  260. source={disableVideo ? EnableCamera() : DisableCamera()}
  261. />
  262. </View>
  263. <View style={styles.bottomView}>
  264. <OperateButton
  265. onPress={this.toggleAllRemoteAudioStreams}
  266. source={isMute ? IconMuted() : BtnMute()}
  267. />
  268. <OperateButton
  269. onPress={this.switchCamera}
  270. source={BtnSwitchCamera()}
  271. />
  272. <OperateButton
  273. onPress={this.toggleSpeakerPhone}
  274. source={!isSpeaker ? IconSpeaker() : BtnSpeaker()}
  275. />
  276. </View>
  277. </View>)
  278. }
  279. }
  280. agoraPeerViews = ({visible, peerIds}) => {
  281. return (visible ?
  282. <View style={styles.videoView} /> :
  283. <View style={styles.videoView}>{
  284. peerIds.map((uid, key) => (
  285. <TouchableOpacity
  286. activeOpacity={1}
  287. onPress={() => this.onPressVideo(uid)}
  288. key={key}>
  289. <AgoraView
  290. style={styles.remoteView}
  291. zOrderMediaOverlay={true}
  292. remoteUid={uid}
  293. />
  294. </TouchableOpacity>
  295. ))
  296. }</View>)
  297. }
  298. modalView = ({visible}) => {
  299. return (
  300. <Modal
  301. visible={visible}
  302. presentationStyle={'fullScreen'}
  303. animationType={'slide'}
  304. onRequestClose={() => {}}
  305. >
  306. <TouchableOpacity
  307. activeOpacity={1}
  308. style={{flex: 1}}
  309. onPress={() => this.setState({
  310. visible: false
  311. })} >
  312. <AgoraView
  313. style={{flex: 1}}
  314. zOrderMediaOverlay={true}
  315. remoteUid={this.state.selectedUid}
  316. />
  317. </TouchableOpacity>
  318. </Modal>)
  319. }
  320. render () {
  321. if (!this.state.joinSucceed) {
  322. return (
  323. <View style={{flex: 1, backgroundColor: '#fff', justifyContent: 'center', alignItems: 'center'}}>
  324. <Text>Creating a video conference...</Text>
  325. </View>
  326. )
  327. }
  328. return (
  329. <TouchableOpacity
  330. activeOpacity={1}
  331. onPress={this.toggleHideButtons}
  332. style={styles.container}
  333. >
  334. <AgoraView style={styles.localView} showLocalVideo={true} />
  335. <View style={styles.absView}>
  336. <Text>channelName: {this.props.channelName}, peers: {this.state.peerIds.length}</Text>
  337. {this.agoraPeerViews(this.state)}
  338. {this.buttonsView(this.state)}
  339. </View>
  340. {this.modalView(this.state)}
  341. </TouchableOpacity>
  342. )
  343. }
  344. }