73 lines
2.3 KiB
JavaScript
73 lines
2.3 KiB
JavaScript
// agent-renderer.js — 브라우저 환경에서 실행됨 → WebRTC 사용 가능
|
|
|
|
let peerConnection = null;
|
|
let currentStream = null;
|
|
|
|
// 메인 프로세스로부터 명령 수신 (IPC)
|
|
console.log('electronAPI:', window.electronAPI);
|
|
console.log('desktopCapturer:', window.desktopCapturer);
|
|
|
|
window.electronAPI.send('displays', { types: ['screen'] });
|
|
|
|
console.log('start webrtc');
|
|
// 1. Socket 연결 (렌더러에서도 가능)
|
|
|
|
window.electronAPI.receive('start-webrtc', async (data) => {
|
|
const source = data.sources.find(s => {
|
|
return s.id === data.displayId
|
|
});
|
|
|
|
// 3. WebRTC 연결
|
|
peerConnection = new RTCPeerConnection({
|
|
iceServers: [{urls: 'stun:stun.l.google.com:19302'}]
|
|
});
|
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({
|
|
audio: false,
|
|
video: {
|
|
mandatory: {
|
|
chromeMediaSource: 'desktop',
|
|
chromeMediaSourceId: source.id,
|
|
minWidth: 1920,
|
|
minHeight: 1080
|
|
}
|
|
}
|
|
});
|
|
currentStream = stream;
|
|
stream.getTracks().forEach(track => {
|
|
console.log('track', track, stream);
|
|
peerConnection.addTrack(track, stream)
|
|
});
|
|
|
|
const offer = await peerConnection.createOffer({offerToReceiveVideo: true})
|
|
await peerConnection.setLocalDescription(offer);
|
|
window.electronAPI.send('offer', {type:'offer', targetId: data.targetId, offer})
|
|
});
|
|
|
|
window.electronAPI.receive('displays', async (payload) => {
|
|
window.desktopCapturer.getSources(['display'])
|
|
});
|
|
|
|
|
|
window.electronAPI.receive('answer', async (data) => {
|
|
console.log('answer', data)
|
|
await peerConnection.setRemoteDescription(new RTCSessionDescription(data.answer));
|
|
console.log('connect')
|
|
})
|
|
|
|
window.electronAPI.receive('webrtcSignal', async (data) => {
|
|
try {
|
|
console.log('webrtcSignal', data)
|
|
if (!peerConnection) return;
|
|
if (data.type === 'icecandidate' && data.candidate) {
|
|
await peerConnection.addIceCandidate(new RTCIceCandidate(data.candidate));
|
|
} else if(data.type === 'answer') {
|
|
await peerConnection.setRemoteDescription(new RTCSessionDescription(data.answer));
|
|
}
|
|
} catch (err) {
|
|
console.error('ICE candidate 처리 오류:', err);
|
|
}
|
|
|
|
})
|
|
// desktopCapturer 사용을 위해 preload 필요
|
|
// → 다음 단계에서 preload.js 설정
|