react-native-calling
Version:
React Native webrtc Audio calling package
172 lines (150 loc) • 4.96 kB
JavaScript
/*
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 {
RTCPeerConnection,
RTCIceCandidate,
RTCSessionDescription,
RTCView,
MediaStream,
MediaStreamTrack,
mediaDevices,
registerGlobals
} from 'react-native-webrtc';
import SignalingConnection from "../Component/SignalingConnection";
import PeerConnection from "../Component//PeerConnection";
import Call from "../Component/Call";
var localStream = null;
var signalingConnection = null;
var loading = true;
var UserCallList = [];
async function InitMedia(socketURL, video = true) {
await WebSoket(socketURL);
let isFront = true;
if (localStream === null) {
return await mediaDevices.enumerateDevices().then(async (sourceInfos) => {
let videoSourceId;
for (let i = 0; i < sourceInfos.length; i++) {
const sourceInfo = sourceInfos[i];
if (sourceInfo.kind == "videoinput" && sourceInfo.facing == (isFront ? "front" : "back")) {
videoSourceId = sourceInfo.deviceId;
}
}
return await mediaDevices.getUserMedia({
audio: true,
// video: false,
video: video ? {
mandatory: {
minWidth: 200, // Provide your own width, height and frame rate here
minHeight: 150,
minFrameRate: 60
},
facingMode: (isFront ? "user" : "environment"),
optional: (videoSourceId ? [{ sourceId: videoSourceId }] : [])
}: video
})
.then(async (stream) => {
// await this.gotStream(stream);
console.log('stream', stream);
localStream = stream;
return stream;
})
.catch(error => {
console.error(error);
});
});
} else {
return localStream;
}
};
async function GetlocalStream() {
return localStream;
}
async function WebSoket(socketURL) {
if (socketURL === null) {
console('Please put socket server URL');
null
}
signalingConnection = new SignalingConnection({
socketURL: socketURL,
// socketURL: "webrtc-sample-signaling.now.sh",
onOpen: () => {
},
onMessage: onSignalingMessage
});
}
const onSignalingMessage = async (msg) => {
switch (msg.type) {
case "userlist": // Received an updated user list
UserCallList = msg.userfull;
break;
}
}
async function LoginServe(username, clientID) {
if (signalingConnection === null) {
console.error('signalingConnection', 'soket note login');
return false;
} else {
signalingConnection.sendToServer({
name: username,
username: username,
date: Date.now(),
id: clientID,
type: "username"
});
}
}
async function LoginCall(username, callUser) {
if (signalingConnection == null) {
return false;
}
signalingConnection.sendToServer({
name: username,
target: username,
type: "CallOwn",
data: {
username: username,
callUser: callUser
}
});
signalingConnection.sendToServer({
name: username,
target: callUser,
type: "CallIncomin",
data: {
username: callUser,
callUser: username
}
});
}
function Online() {
return UserCallList;
}
export {
InitMedia,
LoginCall,
signalingConnection,
onSignalingMessage,
Online,
LoginServe,
WebSoket,
Call,
GetlocalStream,
PeerConnection
}