Нет описания

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