react-native-calling
Version:
React Native webrtc Audio calling package
440 lines (384 loc) • 14 kB
JavaScript
import React from "react";
/*
MIT License
Copyright (c) 2020 Soumyadeep Halder
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import PeerConnection from "./PeerConnection";
import {
View
} from 'native-base';
import { Modal, StyleSheet, Vibration, ToastAndroid, AppState, NativeModules } from 'react-native';
import {
RTCView
} from 'react-native-webrtc';
import Income from "../Screen/CallI/Income";
import Outgoing from "../Screen/CallI/index";
import InCallManager from 'react-native-incall-manager';
const CallingNoti = NativeModules.Calling;
class Call extends React.Component {
constructor(props) {
super(props);
this.props.signalingConnection.addMsgListener(this.onSignalingMessage);
this.signalingConnection = this.props.signalingConnection;
this.localVideoRef = React.createRef();
this.remoteVideoRef = React.createRef();
this.state = {
startDisabled: true,
callDisabled: true,
callScrin: this.props.callScrin ? this.props.callScrin : "default",
hangUpDisabled: true,
localStream: this.props.localStream,
remoteVideoRef: null,
clientID: this.props.clientID,
username: this.props.username,
targetUsername: null,
userList: [],
callUserData: {},
CallData: {},
startCall: false,
modalVisible: false,
calltype: false,
callNoRecevd: false,
appState: AppState.currentState,
};
this.peerConnection = null;
}
onSignalingMessage = async (msg) => {
switch (msg.type) {
case "video-offer": // Invitation and offer to chat
await this.videoOfferOn(msg);
break;
case "CallOwn":
await this.call(msg.data);
break;
case "CallIncomin":
await this.callIncoming(msg.data);
break;
case "CallClose":
await this.onCloseCall(msg.data);
break;
case "Callstart":
await this.callstart();
break;
}
};
componentDidMount() {
AppState.addEventListener('change', this._handleAppStateChange);
}
componentWillUnmount() {
AppState.removeEventListener('change', this._handleAppStateChange);
}
_handleAppStateChange = (nextAppState) => {
if (
this.state.appState.match(/inactive|background/) &&
nextAppState === 'active'
) {
console.log('App has come to the foreground!');
}
this.setState({appState: nextAppState});
};
async callIncoming(data) {
this.state.callUserData = data.callData;
this.state.CallData.name = data.callUser;
console.log('react-native-calling', this.state.callUserData);
this.setState({
calltype: "Income",
callNoRecevd: true,
startCall: true,
modalVisible: !this.state.modalVisible
});
InCallManager.startRingtone('_BUNDLE_');
let incomingNamge = data.callData && data.callData.name ? data.callData.name : data.callUser;
let notiId = new Date().getTime();
if (this.state.appState.match(/inactive|background/)) {
CallingNoti.incoming(incomingNamge, "Incoming Call", ""+notiId+"", "no", "no", Number(notiId));
}
// Vibration.vibrate(1000);
}
async call(user) {
this.state.callUserData = user.callData;
this.state.CallData.name = user.callUser;
console.log('react-native-calling', this.state.callUserData);
this.setState({
targetUsername: user.callUser,
startCall: true,
modalVisible: true,
calltype: "Outgoing",
callNoRecevd: false
});
};
async callstart() {
this.setState({
// startCall: false
});
this.setState({
targetUsername: this.state.CallData.name,
startCall: true,
modalVisible: true,
calltype: "connecting"
// calltype: "Conect"
});
}
async callAccpet() {
this.setState({
targetUsername: this.state.CallData.name,
startCall: true,
modalVisible: true,
calltype: "connecting"
});
this.signalingConnection.sendToServer({
name: this.state.username,
target: this.state.CallData.name,
type: "Callstart",
data: {
username: this.state.CallData.name,
callUser: this.state.username
}
});
InCallManager.stopRingtone();
// InCallManager.stop();
// alert(this.state.CallData.name);
// Vibration.cancel();
setTimeout(async () => {
await this.createPeerConnection();
}, 10);
}
async videoOfferOn(msg) {
await this.createPeerConnection();
this.peerConnection.videoOffer(msg);
}
gotRemoteTrack = async (event) => {
let remoteVideo = this.remoteVideoRef.current;
if (remoteVideo.srcObject !== event.streams[0]) {
this.setState({
// startCall: false
});
remoteVideo.srcObject = event.streams[0];
this.remoteVideoRef.current = event.streams[0];
this.state.remoteVideoRef = event.streams[0];
this.state.calltype = "start";
this.state.callNoRecevd = false;
}
this.setState({
hangUpDisabled: false,
startCall: true
});
};
gotRemoteStream = async (event) => {
this.setState({
// startCall: false
});
this.remoteVideoRef.current = {
srcObject: event.stream
};
this.state.remoteVideoRef = event.stream;
this.state.calltype = "start";
this.state.callNoRecevd = false;
this.setState({
hangUpDisabled: false,
startCall: true
});
};
hangUp = () => {
this.signalingConnection.sendToServer({
name: this.state.username,
target: this.state.targetUsername,
type: "hang-up"
});
// this.peerConnection.close();
// let peerConnection = new PeerConnection({
// gotRemoteStream: this.gotRemoteStream,
// gotRemoteTrack: this.gotRemoteTrack,
// signalingConnection: this.signalingConnection,
// onClose: this.closeVideoCall,
// localStream: this.state.localStream,
// username: this.state.username,
// targetUsername: this.state.targetUsername
// });
if (typeof (this.peerConnection) == "object") {
this.peerConnection.close();
this.peerConnection = null;
}
};
async createPeerConnection() {
if (this.peerConnection) {
// alert('all rady connecting....');
// return;
this.peerConnection = null;
}
this.peerConnection = new PeerConnection({
gotRemoteStream: this.gotRemoteStream,
gotRemoteTrack: this.gotRemoteTrack,
signalingConnection: this.signalingConnection,
onClose: this.closeVideoCall,
localStream: this.state.localStream,
username: this.state.username,
targetUsername: this.state.targetUsername
});
};
closeVideoCall = async () => {
this.remoteVideoRef.current.srcObject &&
this.remoteVideoRef.current.srcObject
.getTracks()
.forEach(track => track.stop());
this.remoteVideoRef.current.src = null;
this.state.remoteVideoRef = null;
this.setState({
targetUsername: null,
callDisabled: false,
startCall: false,
modalVisible: false
});
};
setModalVisible(visible) {
this.setState({ modalVisible: visible });
}
// send props
// call user enter fes
stopRin() {
InCallManager.stopRingtone();
}
async callreject() {
if (this.state.calltype == "Income") {
// Vibration.cancel();
// InCallManager.stop();
}
this.signalingConnection.sendToServer({
name: this.state.username,
target: this.state.CallData.name,
type: "CallClose",
data: {
username: this.state.CallData.name,
callUser: this.state.username
}
});
if (this.peerConnection) {
this.hangUp();
}
}
async onCloseCall(data) {
this.setState({
startCall: false,
modalVisible: false,
calltype: false
});
InCallManager.stopRingtone();
if (this.state.calltype == "Income") {
// Vibration.cancel();
}
if (this.state.callNoRecevd) {
ToastAndroid.show('User busy', ToastAndroid.SHORT);
}
if (this.state.calltype == "Outgoing") {
// InCallManager.stopRingtone();
// InCallManager.stop();
}
}
// call user enter fess end
render() {
const {
startDisabled,
callDisabled,
hangUpDisabled,
username,
userList
} = this.state;
return (
<>
<View style={{ flexDirection: 'row' }} >
<View style={styles.container}>
{
this.state.localStream != null ?
<RTCView streamURL={this.state.localStream.toURL()} />
: null
}
</View>
<View style={styles.container}>
{
this.state.remoteVideoRef != null ?
<RTCView streamURL={this.state.remoteVideoRef.toURL()} />
: null
}
</View>
</View>
{
this.state.callScrin == "default" && this.state.startCall && this.state.calltype ?
<Modal
animationType="slide"
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {
}}>
{
this.state.calltype == "Income" ?
<Income data={this.state.CallData}
callUserData={this.state.callUserData}
callreject={() => {
this.callreject();
this.stopRin();
this.setModalVisible(!this.state.modalVisible);
}}
callAccpet={() => {
this.callAccpet();
}}
/>
:
<Outgoing
data={this.state.CallData}
callUserData={this.state.callUserData}
callreject={() => {
this.callreject();
this.setModalVisible(!this.state.modalVisible);
}}
connecting={this.state.calltype == "connecting" ? true : false}
callAccpet={this.state.calltype == "start" ? true : false}
/>
}
</Modal>
: null
}
</>
);
}
}
var styles = StyleSheet.create({
backgroundVideo: {
// position: 'absolute',
// zIndex: 999999999999999999,
top: 0,
left: 0,
bottom: 0,
right: 0,
width: 800,
height: 500
},
rtc: {
marginTop: 10,
width: '100%',
height: 500,
},
container: {
backgroundColor: '#313131',
justifyContent: 'space-between',
alignItems: 'center',
// height: '100%',
// width: '50%',
// padding: 5
},
});
export default Call;