我正在我的项目中开发视频/音频流功能,并且我正在使用react-native-webrtc库来实现上述功能。最初我在ComponentDidMount()上渲染本地流
componentDidMount () {
this.getLocalStream();
}
getLocalStream() {
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 === (this.state.isFront ? "front" : "environment")) {
videoSourceId = sourceInfo.id;
}
}
mediaDevices.getUserMedia({
audio: true,
// IF simulator/audio call is running for webrtc then video to false.
video: this.state.isSwitching,
facingMode: (this.state.isFront ? "user" : "environment"),
optional: (videoSourceId ? [{sourceId: videoSourceId}] : [])
}
)
.then(stream => {
InCallManager.start({media: 'audio'});
InCallManager.startRingtone('_BUNDLE_');
if (this.state.isSwitching === false) {
InCallManager.setSpeakerphoneOn(true);
InCallManager.setForceSpeakerphoneOn(true);
}
this.setState({videoUrl: stream.toURL(),})
localStream = stream;
pc.addStream(localStream);
connectedUser = false;
createPC(this.state.roomID,true);
})
.catch(error => {
// Log error
console.log('Error',error.message);
this.setState({loading: false});
});
});
}
成功渲染后,我正在向另一个同行发起 offer();但是 RTCPeerConnection pc.createOffer()的方法无法触发,并且它没有执行其中的函数和逻辑。我尝试了很多方法来触发方法,但都没有成功。
const createPC = (socketId, isOffer) => {
pcPeers = {
...pcPeers,
[socketId]: pc,
};
if (isOffer){
pc.createOffer(function(desc) {
log("fgf");
console.log('createOffer', desc);
pc.setLocalDescription(desc, function () {
console.log('setLocalDescription', pc.localDescription);
socket.emit('exchange', {'to': socketId, 'sdp': pc.localDescription });
}, logError);
}, logError);
}
// On Add Stream:
pc.onaddstream = event => {
//console.log('onaddstream', event.stream);
const remoteList = this.state.remoteList;
remoteList[socketId] = event.stream.toURL();
this.setState({
remoteList: remoteList,
});
};
// ice candidates
pc.onicecandidate = event => {
log('Howeeeee');
if (event.candidate) {
socket.emit('exchange', { to: socketId, candidate: event.candidate });
}
};
return pc;
}
即使在为方法提供了所有参数之后也没有被解雇的原因是什么。由于我是 React Native 的新手,有人可以帮助我我在哪里犯了错误。帮助将不胜感激。