This is not a new question, and the previous questions did not get any solutions.
React-native-peerjs supposed to emit ‘stream’ upon answering call. But we are getting the call and answering it perfectly fine, but no stream gets emitted. What am I doing wrong?
We tried to set options like below when we are sending a call:
var options = {
'constraints': {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
},
offerToReceiveAudio: 1,
offerToReceiveVideo: 1,
}
}
But alas ! It still wont work. Here is the entire code :
/**
*
* @format
* @flow strict-local
*/
import React, { useState, useEffect } from 'react';
import type { Node } from 'react';
import {
SafeAreaView,
ScrollView,
Dimensions,
useColorScheme,
View,
TextInput,
TouchableOpacity,
Text
} from 'react-native';
import { Colors } from 'react-native/Libraries/NewAppScreen';
import Peer from 'react-native-peerjs';
import { RTCView, mediaDevices } from 'react-native-webrtc';
const { width, height } = Dimensions.get('window');
const App: () => Node = () => {
const isDarkMode = useColorScheme() === 'dark';
const backgroundStyle = {
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
flex: 1,
};
const [remotePeerId, setRemotePeerId] = useState('');
const [ongoin, setOngoin] = useState(undefined);
const [localPeer] = useState(() => new Peer()); // using peer cloud
const [localStream, setLocalStream] = useState();
const [remoteStreams, setRemoteStreams] = useState();
useEffect(() => {
console.log('Local peer is opening ...');
let isFront = true;
mediaDevices.enumerateDevices().then(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: {
mandatory: {
minFrameRate: 30,
},
facingMode: isFront ? 'user' : 'environment',
optional: videoSourceId ? [{ sourceId: videoSourceId }] : [],
},
})
.then(stream => {
setLocalStream(stream);
localPeer.on('open', peerId => {
console.log('Local peer is opened with Id = ' + peerId);
localPeer.on('call', call => {
console.log('Local peer receives incoming call');
call.answer(stream); // answring call should emit 'stream', but won't
call?.on('stream', remoteStream => {
console.log('Local peer receives stream from remote peer');
setRemoteStreams([remoteStream, remoteStream, remoteStream]);
});
});
});
})
.catch(error => {
console.log(error);
});
});
return () => {
if (localPeer) {
localPeer.destroy();
console.log('Local peer is destroyed');
}
};
}, []);
const handleCallPress = () => {
console.log("CURRENT STREAM");
var options = {
'constraints': {
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
},
offerToReceiveAudio: 1,
offerToReceiveVideo: 1,
}
}
const outgoingCall = localPeer.call(remotePeerId, localStream, options);
setOngoin(outgoingCall);
};
useEffect(() => {
if (!ongoin) return;
console.log("Running steam");
ongoin.on('stream', (remoteStream) => {
console.log('Local peer receives stream from remote peer');
setRemoteStreams([remoteStream, remoteStream, remoteStream]);
});
}, [ongoin]);
return (
<SafeAreaView style={backgroundStyle}>
<View style={{ flex: 1 }}>
<View
style={{
flex: 0.7,
justifyContent: 'center',
alignItems: 'center',
}}>
{localStream ? (
<RTCView
streamURL={localStream.toURL()}
style={{
width: width * 0.9 - 8,
height: 200,
}}
/>
) : null}
</View>
<ScrollView
horizontal
style={{ flex: 0.8 }}
showsHorizontalScrollIndicator={false}>
<>
{remoteStreams ? (
remoteStreams.length > 0 ? (
<>
{remoteStreams.map((stream, index) => {
return (
<View
key={index}
style={{
width: width * 0.9,
marginRight: 8,
justifyContent: 'center',
alignItems: 'center',
}}>
<RTCView
streamURL={stream.toURL()}
style={{
width: width * 0.9 - 8,
height: 300,
}}
/>
</View>
);
})}
</>
) : null
) : null}
</>
</ScrollView>
<View
style={{
flex: 0.5,
}}></View>
</View>
<View style={{ flex: 0.5, alignItems: 'center', justifyContent: 'center' }}>
<TextInput
style={{
height: 40,
borderColor: 'gray',
borderWidth: 1,
padding: 8,
marginBottom: 16,
width: width * 0.8,
borderRadius: 8,
}}
placeholder="Enter Remote Peer ID"
onChangeText={(text) => setRemotePeerId(text)}
value={remotePeerId}
/>
<TouchableOpacity
onPress={() => handleCallPress()}
style={{
backgroundColor: '#4CAF50',
padding: 10,
borderRadius: 8,
}}>
<Text style={{ color: 'white', fontWeight: 'bold' }}>Initiate Call</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
};
export default App;