I want to make the video call with this package (react-native-webrtc).
I followed the code in the doc, But I can only turn on the camera.
I think there are other steps that I miss to make video call with this package.
I need the sample code or any advices.
My code:
import React, {Component} from 'react';
import {View, Text, StyleSheet, Platform} from 'react-native';
import {
RTCPeerConnection,
RTCIceCandidate,
RTCSessionDescription,
RTCView,
MediaStream,
MediaStreamTrack,
mediaDevices,
registerGlobals,
} from 'react-native-webrtc';
class VideoChat extends Component {
state = {
videoURL: null,
isFront: false,
};
componentDidMount() {
const configuration = {iceServers: [{url: 'stun:stun.l.google.com:19302'}]};
const pc = new RTCPeerConnection(configuration);
const {isFront} = this.state;
mediaDevices.enumerateDevices().then((sourceInfos) => {
console.log('mediaDevices.enumerateDevices()', sourceInfos);
let videoSourceId;
for (let i = 0; i < sourceInfos.length; i++) {
const sourceInfo = sourceInfos[i];
if (
sourceInfo.kind === 'videoinput' &&
sourceInfo.facing === (isFront ? 'front' : 'environment')
) {
videoSourceId = sourceInfo.deviceId;
}
}
mediaDevices
.getUserMedia({
audio: true,
video: {
video: true,
facingMode: isFront ? 'user' : 'environment',
optional: videoSourceId ? [{sourceId: videoSourceId}] : [],
},
})
.then((stream) => {
// Got stream!
console.log('stream =>', stream);
this.setState({
videoURL: stream.toURL(),
});
pc.addStream(stream);
})
.catch((error) => {
// Log error
console.log('error =>', error);
});
});
pc.createOffer().then((desc) => {
pc.setLocalDescription(desc).then(() => {
// Send pc.localDescription to peer
console.log('setLocationDescription', desc);
});
});
pc.onicecandidate = (event) => {
// send event.candidate to peer
console.log('onicecandidate', event);
};
}
render() {
const {videoURL} = this.state;
return <RTCView streamURL={videoURL} style={styles.container} />;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
borderWidth: 1,
borderColor: 'red',
},
});
export default VideoChat;