説明なし

agora.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. FirstRemoteVideoDecodedOfUid: (data) => {
  137. console.log('[RtcEngine] firstRemoteVideoDecodedOfUid', data);
  138. },
  139. DidOfflineOfUid: (data) => {
  140. console.log('[RtcEngine] didOfflineOfUid', data);
  141. this.setState({
  142. peerIds: this.state.peerIds.filter(uid => uid !== data.uid)
  143. })
  144. },
  145. DidJoinChannel: (data) => {
  146. console.log('[RtcEngine] didJoinChannel', data);
  147. // RtcEngine.setShowLocalVideo()
  148. RtcEngine.startPreview();
  149. this.setState({
  150. joinSucceed: true
  151. })
  152. const {peerIds} = this.state;
  153. if (peerIds.indexOf(data.uid) !== -1) {
  154. this.setState({
  155. peerIds: [...peerIds, data.uid]
  156. })
  157. }
  158. },
  159. ReportAudioVolumeIndicationOfSpeakers: (data) => {
  160. console.log('[RtcEngine] reportAudioVolumeIndicationOfSpeakers', data);
  161. },
  162. DidClientRoleChanged: (data) => {
  163. console.log("[RtcEngine] DidClientRoleChanged", data);
  164. },
  165. // onUserJoined: (data) => {
  166. // console.log('[RtcEngine] onUserJoined', data);
  167. // const {peerIds} = this.state;
  168. // if (peerIds.indexOf(data.uid) !== -1) {
  169. // this.setState({
  170. // peerIds: [...peerIds, data.uid]
  171. // })
  172. // }
  173. // },
  174. DidOccurError: (data) => {
  175. console.log('[RtcEngine] onError', data);
  176. if (data.error === 17) {
  177. RtcEngine.leaveChannel().then(_ => {
  178. RtcEngine.destroy();
  179. this.props.onCancel(data);
  180. });
  181. }
  182. }
  183. })
  184. }
  185. componentWillUnmount () {
  186. RtcEngine.leaveChannel();
  187. RtcEngine.destroy();
  188. }
  189. handleCancel = () => {
  190. RtcEngine.leaveChannel();
  191. RtcEngine.destroy();
  192. this.props.onCancel();
  193. }
  194. switchCamera = () => {
  195. RtcEngine.switchCamera();
  196. }
  197. toggleAllRemoteAudioStreams = () => {
  198. this.setState({
  199. isMute: !this.state.isMute
  200. }, () => {
  201. RtcEngine.muteAllRemoteAudioStreams(this.state.isMute);
  202. })
  203. }
  204. toggleSpeakerPhone = () => {
  205. this.setState({
  206. isSpeak: !this.state.isSpeak
  207. }, () => {
  208. RtcEngine.setDefaultAudioRouteToSpeakerphone(this.state.isSpeak);
  209. })
  210. }
  211. toggleCameraTorch = () => {
  212. this.setState({
  213. isCameraTorch: !this.state.isCameraTorch
  214. }, () => {
  215. RtcEngine.setCameraTorchOn(this.state.isCameraTorch)
  216. })
  217. }
  218. toggleVideo = () => {
  219. this.setState({
  220. disableVideo: !this.state.videodisableVideo
  221. }, () => {
  222. this.state.disableVideo ? RtcEngine.enableVideo() : RtcEngine.disableVideo()
  223. });
  224. }
  225. toggleHideButtons = () => {
  226. this.setState({
  227. hideButton: !this.state.hideButton
  228. })
  229. }
  230. onPressVideo = (uid) => {
  231. this.setState({
  232. selectedUid: uid
  233. }, () => {
  234. this.setState({
  235. visible: true
  236. })
  237. })
  238. }
  239. buttonsView = ({hideButton, isCameraTorch, disableVideo, isMute, isSpeaker}) => {
  240. if (!hideButton) {
  241. return (
  242. <View>
  243. <OperateButton
  244. style={{alignSelf: 'center', marginBottom: -10}}
  245. onPress={this.handleCancel}
  246. imgStyle={{width: 60, height: 60}}
  247. source={BtnEndCall()}
  248. />
  249. <View style={styles.bottomView}>
  250. <OperateButton
  251. onPress={this.toggleCameraTorch}
  252. imgStyle={{width: 40, height: 40}}
  253. source={isCameraTorch ? EnablePhotoflash() : DisablePhotoflash()}
  254. />
  255. <OperateButton
  256. onPress={this.toggleVideo}
  257. source={disableVideo ? EnableCamera() : DisableCamera()}
  258. />
  259. </View>
  260. <View style={styles.bottomView}>
  261. <OperateButton
  262. onPress={this.toggleAllRemoteAudioStreams}
  263. source={isMute ? IconMuted() : BtnMute()}
  264. />
  265. <OperateButton
  266. onPress={this.switchCamera}
  267. source={BtnSwitchCamera()}
  268. />
  269. <OperateButton
  270. onPress={this.toggleSpeakerPhone}
  271. source={!isSpeaker ? IconSpeaker() : BtnSpeaker()}
  272. />
  273. </View>
  274. </View>)
  275. }
  276. }
  277. agoraPeerViews = ({visible, peerIds}) => {
  278. return (visible ?
  279. <View style={styles.videoView} /> :
  280. <View style={styles.videoView}>{
  281. peerIds.map((uid, key) => (
  282. <TouchableOpacity
  283. activeOpacity={1}
  284. onPress={() => this.onPressVideo(uid)}
  285. key={key}>
  286. <AgoraView
  287. style={styles.remoteView}
  288. zOrderMediaOverlay={true}
  289. remoteUid={uid}
  290. />
  291. </TouchableOpacity>
  292. ))
  293. }</View>)
  294. }
  295. modalView = ({visible}) => {
  296. return (
  297. <Modal
  298. visible={visible}
  299. presentationStyle={'fullScreen'}
  300. animationType={'slide'}
  301. onRequestClose={() => {}}
  302. >
  303. <TouchableOpacity
  304. activeOpacity={1}
  305. style={{flex: 1}}
  306. onPress={() => this.setState({
  307. visible: false
  308. })} >
  309. <AgoraView
  310. style={{flex: 1}}
  311. zOrderMediaOverlay={true}
  312. remoteUid={this.state.selectedUid}
  313. />
  314. </TouchableOpacity>
  315. </Modal>)
  316. }
  317. render () {
  318. if (!this.state.joinSucceed) {
  319. return (
  320. <View style={{flex: 1, backgroundColor: '#fff', justifyContent: 'center', alignItems: 'center'}}>
  321. <Text>Creating a video conference...</Text>
  322. </View>
  323. )
  324. }
  325. return (
  326. <TouchableOpacity
  327. activeOpacity={1}
  328. onPress={this.toggleHideButtons}
  329. style={styles.container}
  330. >
  331. <AgoraView style={styles.localView} showLocalVideo={true} />
  332. <View style={styles.absView}>
  333. <Text>channelName: {this.props.channelName}, peers: {this.state.peerIds.length}</Text>
  334. {this.agoraPeerViews(this.state)}
  335. {this.buttonsView(this.state)}
  336. </View>
  337. {this.modalView(this.state)}
  338. </TouchableOpacity>
  339. )
  340. }
  341. }