hub-video-tester/src/App.js

539 lines
18 KiB
JavaScript
Raw Normal View History

2021-01-13 14:04:04 +00:00
import React, { Component } from 'react'
import './App.css';
2021-01-13 14:04:04 +00:00
const io = require("socket.io-client");
2021-02-22 18:23:40 +00:00
var stream, pc;
2021-02-16 11:33:08 +00:00
2021-02-18 15:01:50 +00:00
// var config = {
// iceServers: [{
// //urls: [ "stun:numb.viagenie.ca"
// urls: [ "stun:dev.linx.safemobile.com:19302" ]
// }, {
// //username: "claudiustancu@outlook.com",
// username: "sergiu",
// //credential: "Parola123",
// credential: "test123",
// urls: [
// // "turn:numb.viagenie.ca"
// "turn:dev.linx.safemobile.com:19302"
// ]
// }],
// //FOR CHROME
// rtcpMuxPolicy:"negotiate"
// }
var config = {
iceServers: [{
urls: [ "stun:18.185.97.230:3478" ]
}, {
username: "sergiu",
credential: "test123",
urls: [
"turn:18.185.97.230:3478"
]
}],
//FOR CHROME
rtcpMuxPolicy:"negotiate"
}
2021-02-17 12:23:21 +00:00
// var config = {
// iceServers: [{
// urls: [ "stun:dev.linx.safemobil.com:3478" ]
// }, {
2021-02-18 15:01:50 +00:00
// username: "sergiu",
2021-02-17 12:23:21 +00:00
// credential: "test123",
// urls: [
// "turn:dev.linx.safemobil.com:3478"
// ]
// }],
// //FOR CHROME
// rtcpMuxPolicy:"negotiate"
// }
2021-02-17 11:59:16 +00:00
2021-02-17 12:21:35 +00:00
// var config = {
// iceServers: [{
// urls: [ "stun:numb.viagenie.ca" ]
// }, {
// username: "claudiustancu@outlook.com",
// credential: "Parola123",
// urls: [
// "turn:numb.viagenie.ca"
// ]
// }],
// //FOR CHROME
// rtcpMuxPolicy:"negotiate"
// }
2021-02-18 15:01:50 +00:00
// var config = {
// iceServers: [{
// //urls: [ "stun:numb.viagenie.ca"
// urls: [ "stun:dev.linx.safemobile.com" ]
// }, {
// //username: "claudiustancu@outlook.com",
// username: "sergiu",
// //credential: "Parola123",
// credential: "test123",
// urls: [
// // "turn:numb.viagenie.ca"
// "turn:dev.linx.safemobile.com"
// ]
// }],
// //FOR CHROME
// rtcpMuxPolicy:"negotiate"
// }
// var config = {
// iceServers: [{
// urls: [ "stun:18.194.53.12:3478" ]
// }, {
// username: "omulpaianjen",
// credential: "peterparker",
// urls: [
// "turn:18.194.53.12:3478"
// ]
// }],
// //FOR CHROME
// rtcpMuxPolicy:"negotiate"
// }
2021-02-17 12:19:36 +00:00
2021-02-16 11:33:08 +00:00
let localStream, remoteStream
2021-01-13 14:04:04 +00:00
let localVideo = document.getElementById("localVideo");
let remoteVideo = document.getElementById("remoteVideo");
class App extends Component {
constructor() {
super();
this.state = {
2021-01-29 09:27:56 +00:00
login: 'lx-adi',
2021-01-27 10:04:34 +00:00
password: 'Safemobile123',
2021-01-13 14:04:04 +00:00
user: null,
hubStatus: 0, // 0 uninitialized | 1 connecting | 2 connected | 3 connection error
socket: null,
arsSent: false,
2021-02-16 11:33:08 +00:00
dest_asset_id: '1',
2021-01-13 14:04:04 +00:00
isCaller: false,
callId: null,
2021-01-27 10:04:34 +00:00
// rtcPeerConnection: null,
stunUrl: 'stun:10.120.1.134:19302',
turnUrl: 'turn:10.120.1.134:19302',
2021-01-29 09:27:56 +00:00
turnUsername: 'sergiu',
turnCredential: 'test123'
2021-01-13 14:04:04 +00:00
};
}
2021-02-16 11:33:08 +00:00
componentDidMount= async () => {
2021-01-27 10:04:34 +00:00
// var windowObjectReference;
// var windowFeatures = "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes";
// console.log('11111');
// setTimeout(() => {
// windowObjectReference = window.open("http://www.cnn.com/", "CNN_WindowName", windowFeatures);
// }, 500)
2021-02-16 11:33:08 +00:00
stream = await navigator.mediaDevices.getUserMedia({video:true, audio:false})
localStream = stream
localVideo.srcObject = stream
2021-02-22 18:58:55 +00:00
// pc = new RTCPeerConnection(config)
// pc.ontrack = this.onAddStream;
// // pc.addTrack(localStream.getTracks()[0], localStream);
// pc.addTrack(localStream.getTracks()[0], localStream)
// // pc.addTrack(localStream.getTracks()[1], localStream)
// pc.oniceconnectionstatechange = this.onIceConnectionStateChange;
// pc.onsignalingstatechange = this.onSignalingStateChange;
2021-02-22 18:23:40 +00:00
// pc.onicecandidate = ({candidate}) => {
// console.log('IIIIIIIIIIIIIIIIIIIIIIIII2', this.state.callId);
// this.state.socket.emit('video', JSON.stringify({
// type: 'candidate',
// candidate: candidate,
// dest_asset_id: parseInt(this.state.dest_asset_id),
// video_call_id: this.state.callId,
// test: '1111111111'
// }));
// }
}
onIceConnectionStateChange = (event) => {
console.log('onIceConnectionStateChange', event);
}
onSignalingStateChange = async ({currentTarget}) => {
console.log('onSignalingStateChange', currentTarget);
// if(currentTarget.connectionState === 'new') {
// console.log('pc.localDescription', pc.localDescription);
// if(pc.localDescription) {
// console.log('SEND SDP onSignalingStateChange');
// // await pc.setLocalDescription(await pc.createAnswer())
// this.state.socket.emit('video', JSON.stringify({
// origin_asset_id: this.state.user.asset.id,
// dest_asset_id: parseInt(this.state.dest_asset_id),
// type: 'offer',
// origin_asset_priority: this.state.user.asset.priority,
// origin_asset_type_name: this.state.user.user_role.name,
// origin_asset_name: this.state.user.asset.name,
// video_call_id: this.state.callId,
// sdp: pc.localDescription
// }));
// }
// }
2021-02-16 11:33:08 +00:00
}
onAddStream = (event) => {
console.log('onAddStream', event);
remoteVideo.srcObject = event.streams[0]
remoteStream = event.streams[0]
2021-01-13 14:04:04 +00:00
}
handleLoginClick = async () => {
2021-02-25 13:18:16 +00:00
const loginRequest = await fetch('https://stage.linx.safemobile.com/api/login', {
2021-01-13 14:04:04 +00:00
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"login": this.state.login,
"password": this.state.password
})
});
const loginResponseJson = await loginRequest.json();
this.setState({ user: loginResponseJson.data }, () => {
// Connect to HUB
2021-02-25 13:18:16 +00:00
this.setState({ socket: io("https://hub.stage.linx.safemobile.com/", {reconnect:true, transports: ['websocket']}) }, () => {
2021-02-22 18:23:40 +00:00
// socket = this.state.socket;
this.state.socket.on('connect', () => {
2021-01-13 14:04:04 +00:00
this.setState({ hubStatus: 2 })
// Send ARS after connected to HUB
2021-02-22 18:23:40 +00:00
this.state.socket.emit('ars', JSON.stringify({
2021-01-13 14:04:04 +00:00
ars: true,
asset_id: this.state.user.asset.id,
account_id: this.state.user.account.id
}));
this.setState({ arsSent: true });
});
// HUB 'video' event handler
2021-02-22 18:23:40 +00:00
this.state.socket.on('video', async (data) => {
2021-02-16 11:33:08 +00:00
2021-02-17 12:16:59 +00:00
2021-02-16 11:33:08 +00:00
const parseData = JSON.parse(data);
2021-02-17 12:16:59 +00:00
console.log('[VIDEO]', parseData);
2021-02-16 11:33:08 +00:00
if(parseData.type === 'notify-request') {
2021-02-22 18:58:55 +00:00
pc = new RTCPeerConnection(config)
pc.ontrack = this.onAddStream;
// pc.addTrack(localStream.getTracks()[0], localStream);
pc.addTrack(localStream.getTracks()[0], localStream)
// pc.addTrack(localStream.getTracks()[1], localStream)
pc.oniceconnectionstatechange = this.onIceConnectionStateChange;
pc.onsignalingstatechange = this.onSignalingStateChange;
console.log('NOTIFY-REQUEST');
await pc.setLocalDescription(await pc.createOffer())
this.state.socket.emit('video', JSON.stringify({
origin_asset_id: this.state.user.asset.id,
dest_asset_id: parseInt(parseData.origin_asset_id),
type: 'notify-answer',
origin_asset_priority: this.state.user.asset.priority,
origin_asset_type_name: this.state.user.user_role.name,
origin_asset_name: this.state.user.asset.name,
video_call_id: parseInt(parseData.video_call_id),
answer: 'accepted', // answer: 'rejected'
sdp: pc.localDescription
}));
this.setState({ callId: parseInt(parseData.video_call_id), dest_asset_id: parseInt(parseData.origin_asset_id) })
2021-02-22 18:23:40 +00:00
}
if(parseData.type === 'notify-answer') {
2021-02-22 18:58:55 +00:00
pc = new RTCPeerConnection(config)
pc.ontrack = this.onAddStream;
// pc.addTrack(localStream.getTracks()[0], localStream);
pc.addTrack(localStream.getTracks()[0], localStream)
// pc.addTrack(localStream.getTracks()[1], localStream)
pc.oniceconnectionstatechange = this.onIceConnectionStateChange;
pc.onsignalingstatechange = this.onSignalingStateChange;
2021-02-22 18:23:40 +00:00
console.log('NOTIFY-ANSWER');
this.setState({ callId: parseInt(parseData.video_call_id) })
this.offerReceived(parseData)
// await pc.setLocalDescription(await pc.createOffer())
// console.log('IIIIIIIIIIIIIIIIIIIIIIIII', this.state.callId);
// socket.emit('video', JSON.stringify({
// origin_asset_id: this.state.user.asset.id,
// dest_asset_id: parseInt(parseData.origin_asset_id),
// type: 'offer',
// origin_asset_priority: this.state.user.asset.priority,
// origin_asset_type_name: this.state.user.user_role.name,
// origin_asset_name: this.state.user.asset.name,
// video_call_id: parseInt(parseData.video_call_id),
// answer: 'accepted', // answer: 'rejected'
// sdp: pc.localDescription
// }));
// this.setState({ callId: parseInt(parseData.video_call_id), dest_asset_id: parseInt(parseData.origin_asset_id) })
2021-01-13 14:04:04 +00:00
}
2021-02-16 11:33:08 +00:00
else if(parseData.type === 'offer') {
if(parseData.sdp.type === 'offer') {
console.log('[OFFER]');
this.setState({ callId: parseInt(parseData.video_call_id) })
this.offerReceived(parseData)
} else if(parseData.sdp.type === 'answer') {
console.log('[ANSWER]');
await pc.setRemoteDescription(parseData.sdp)
2021-02-22 18:23:40 +00:00
pc.onicecandidate = ({candidate}) => {
console.log('candidate1', candidate);
this.state.socket.emit('video', JSON.stringify({
type: 'candidate',
candidate,
dest_asset_id: parseInt(this.state.dest_asset_id),
video_call_id: parseData.video_call_id,
test: '222222222222',
}));
2021-02-16 11:33:08 +00:00
}
2021-02-22 18:23:40 +00:00
this.state.socket.emit('video', JSON.stringify({
origin_asset_id: this.state.user.asset.id,
dest_asset_id: parseInt(this.state.dest_asset_id),
type: 'offer',
origin_asset_priority: this.state.user.asset.priority,
origin_asset_type_name: this.state.user.user_role.name,
origin_asset_name: this.state.user.asset.name,
video_call_id: this.state.callId,
sdp: pc.localDescription
}));
2021-01-27 10:04:34 +00:00
}
2021-02-16 11:33:08 +00:00
} else if(parseData.type === 'candidate') {
2021-02-17 12:16:59 +00:00
console.log('candidate parseData', parseData);
2021-02-16 11:33:08 +00:00
if(parseData.candidate) {
2021-02-17 12:16:59 +00:00
console.log('parseData.candidate', parseData.candidate)
2021-02-16 11:33:08 +00:00
pc.addIceCandidate(parseData.candidate).catch(e => {console.log(e)})
}
2021-02-22 18:23:40 +00:00
} else if (parseData.type === 'notify-end') {
this.cleanVideoStreams();
2021-01-13 14:04:04 +00:00
}
2021-02-22 18:58:55 +00:00
2021-02-16 11:33:08 +00:00
2021-01-13 14:04:04 +00:00
});
});
})
}
2021-02-16 11:33:08 +00:00
offerReceived = async (offer) => {
2021-02-17 12:16:59 +00:00
console.log('Received offer', offer)
2021-02-16 11:33:08 +00:00
await pc.setRemoteDescription(offer.sdp)
pc.onicecandidate = ({candidate}) => {
2021-02-17 12:16:59 +00:00
console.log('IIIIIIIIIIIIIIIIIIIIIIIII2', this.state.callId);
2021-02-16 11:33:08 +00:00
this.state.socket.emit('video', JSON.stringify({
type: 'candidate',
candidate: candidate,
dest_asset_id: parseInt(this.state.dest_asset_id),
2021-02-17 12:16:59 +00:00
video_call_id: this.state.callId,
test: '1111111111'
2021-02-16 11:33:08 +00:00
}));
}
await pc.setLocalDescription(await pc.createAnswer())
pc.onnegotiationneeded = async () => {
2021-02-17 12:16:59 +00:00
console.log('---------onnegotiationneeded--------');
2021-02-16 11:33:08 +00:00
try {
2021-02-17 12:16:59 +00:00
console.log('IIIIIIIIIIIIIIIIIIIIIIIII3', this.state.callId);
2021-02-16 11:33:08 +00:00
await pc.setLocalDescription(await pc.createOffer());
// socket.emit('signal', {destination:remoteUser, data:pc.localDescription})
2021-02-22 18:23:40 +00:00
this.state.socket.emit('video', JSON.stringify({
2021-02-16 11:33:08 +00:00
origin_asset_id: this.state.user.asset.id,
dest_asset_id: parseInt(this.state.dest_asset_id),
type: 'offer',
origin_asset_priority: this.state.user.asset.priority,
origin_asset_type_name: this.state.user.user_role.name,
origin_asset_name: this.state.user.asset.name,
video_call_id: this.state.callId,
sdp: pc.localDescription
2021-01-13 14:04:04 +00:00
}));
2021-02-16 11:33:08 +00:00
} catch (err) {
console.error(err);
2021-01-13 14:04:04 +00:00
}
2021-02-16 11:33:08 +00:00
};
2021-02-17 12:16:59 +00:00
console.log('IIIIIIIIIIIIIIIIIIIIIIIII4', this.state.callId);
2021-02-16 11:33:08 +00:00
this.state.socket.emit('video', JSON.stringify({
origin_asset_id: this.state.user.asset.id,
dest_asset_id: parseInt(this.state.dest_asset_id),
type: 'offer',
origin_asset_priority: this.state.user.asset.priority,
origin_asset_type_name: this.state.user.user_role.name,
origin_asset_name: this.state.user.asset.name,
2021-02-17 11:53:54 +00:00
video_call_id: this.state.callId,
2021-02-16 11:33:08 +00:00
sdp: pc.localDescription
}));
2021-02-17 12:16:59 +00:00
console.log('---------onnegotiationneeded END--------');
2021-01-13 14:04:04 +00:00
}
handleChangeLogin = (e) => {
this.setState({ login: e.target.value});
}
handleChangePassword = (e) => {
this.setState({ password: e.target.value});
}
handleChangeDestAssetId = (e) => {
this.setState({ dest_asset_id: e.target.value});
}
2021-01-20 08:25:06 +00:00
handleChangeStunUrl = (e) => {
this.setState({ stunUrl: e.target.value});
}
handleChangeTurnUrl = (e) => {
this.setState({ turnUrl: e.target.value});
}
handleChangeTurnUsername = (e) => {
this.setState({ turnUsername: e.target.value});
}
handleChangeTurnCredential = (e) => {
this.setState({ turnCredential: e.target.value});
}
2021-01-13 14:04:04 +00:00
handleClickEvent = () => {
this.state.socket.emit('video', JSON.stringify({
origin_asset_id: this.state.user.asset.id,
dest_asset_id: parseInt(this.state.dest_asset_id),
type: 'notify-request',
origin_asset_priority: this.state.user.asset.priority,
origin_asset_type_name: this.state.user.user_role.name,
origin_asset_name: this.state.user.name,
2021-02-17 11:53:54 +00:00
video_call_id: this.state.callId
2021-01-13 14:04:04 +00:00
}));
}
closeVideo = () => {
2021-02-17 12:16:59 +00:00
console.log('IIIIIIIIIIIIIIIIIIIIIIIII7', this.state.callId);
2021-01-13 14:04:04 +00:00
this.state.socket.emit('video', JSON.stringify({
origin_asset_id: this.state.user.asset.id,
dest_asset_id: parseInt(this.state.dest_asset_id),
type: 'notify-end',
video_call_id: this.state.callId
}));
this.cleanVideoStreams();
}
2021-02-22 18:58:55 +00:00
cleanVideoStreams = () => {
this.setState({ dest_asset_id: '', callId: null });
pc.close();
// localVideo.srcObject = null;
// remoteVideo.srcObject = null;
}
2021-01-13 14:04:04 +00:00
render() {
2021-01-20 08:25:06 +00:00
const { login, password, user, hubStatus, arsSent, dest_asset_id, isCaller, callId, stunUrl, turnUrl, turnUsername, turnCredential } = { ...this.state };
2021-01-13 14:04:04 +00:00
return (
<div className="App">
2021-01-20 08:25:06 +00:00
2021-02-17 12:21:35 +00:00
<h1>WebRTC Client 0.2 - {Math.random()}</h1>
2021-01-20 08:25:06 +00:00
<br></br>
<h2><u>STUN and TURN servers</u></h2>
STUN Server URL :
<input
placeholder='ex: stun:stun.services.mozilla.com'
onChange={this.handleChangeStunUrl}
value={stunUrl}
style={{ margin: 10, width: 250 }}
/><br></br>
TURN Server URL :
<input
placeholder='ex: turn:numb.viagenie.ca'
onChange={this.handleChangeTurnUrl}
value={turnUrl}
style={{ margin: 10, width: 250 }}
/><br></br>
TURN Server Username :
<input
placeholder='ex: gigel'
onChange={this.handleChangeTurnUsername}
value={turnUsername}
style={{ margin: 10, width: 250 }}
/><br></br>
TURN Server Credential :
<input
placeholder='ex: superGiGi'
onChange={this.handleChangeTurnCredential}
value={turnCredential}
style={{ margin: 10, width: 250 }}
/>
<br></br><br></br>
<h2><u>Login</u></h2>
Username :
2021-01-13 14:04:04 +00:00
<input
placeholder='user'
onChange={this.handleChangeLogin}
value={login}
2021-01-20 08:25:06 +00:00
style={{ margin: 10, width: 250 }}
/><br></br>
Password :
2021-01-13 14:04:04 +00:00
<input
placeholder='password'
onChange={this.handleChangePassword}
value={password}
2021-01-20 08:25:06 +00:00
style={{ margin: 10, width: 250 }}
/><br></br>
<button onClick={this.handleLoginClick} style={{ margin: 10, width: 100 }}>LOGIN</button>
2021-01-27 10:04:34 +00:00
{/* <button onClick={this.testMessage} style={{ margin: 10, width: 200 }}>TEST MESSAGE</button> */}
2021-01-13 14:04:04 +00:00
<br></br>
2021-01-20 08:25:06 +00:00
2021-01-13 14:04:04 +00:00
<h2><u>isCaller:</u> {isCaller ? 'TRUE' : 'FALSE'}</h2>
2021-01-27 10:04:34 +00:00
<h2><u>User details:</u></h2>
2021-01-13 14:04:04 +00:00
{
user && (
<React.Fragment>
<h4>account_id: {user.account.id}</h4>
<h4>user_id: {user.id}</h4>
<h4>asset_id: {user.asset.id}</h4>
<h4>user_role: {user.user_role.name}</h4>
</React.Fragment>
)
}
2021-01-20 08:25:06 +00:00
<h2><u>HUB</u></h2>
2021-01-13 14:04:04 +00:00
<h4>status: {(hubStatus === 0 ? 'uninitialized' : hubStatus === 1 ? 'connecting' : hubStatus === 2 ? 'connected' : 'connection error' )}</h4>
<h4>ARS Sent: {arsSent === true ? 'TRUE' : 'FALSE'}</h4>
2021-02-22 18:58:55 +00:00
<h4>Call id: {callId && callId}</h4>
2021-01-13 14:04:04 +00:00
<br></br>
<input
placeholder='destination_asset_id'
onChange={this.handleChangeDestAssetId}
value={dest_asset_id}
/>
<button onClick={this.handleClickEvent} disabled={dest_asset_id.length === 0 ? true : false}>Send `video` notify-request</button>
<br></br>
<button onClick={this.closeVideo} style={{ margin: 40}} disabled={callId !== null ? false : true}>Close Video</button>
</div>
);
};
};
export default App;