diff --git a/src/webrtc/webrtcManager.js b/src/webrtc/webrtcManager.js index 1bc885d..d47bdba 100644 --- a/src/webrtc/webrtcManager.js +++ b/src/webrtc/webrtcManager.js @@ -16,7 +16,7 @@ export class WebRTCManager { async init() { try { - await this.acquireLocalStream(); // ✅ 이름도 의미에 맞게 변경 + await this.acquireLocalStream(); // ✅ 이름도 의미에 맞게 변경gg this.setupSignaling(); console.log('✅ WebRTC 매니저 초기화 완료'); } catch (err) { @@ -58,6 +58,7 @@ export class WebRTCManager { this.ws.onmessage = (event) => { const msg = JSON.parse(event.data); + console.log('socket 메시지', msg) if (msg.type === 'your-id') { this.myId = msg.id; console.log('내 ID:', this.myId); @@ -68,7 +69,9 @@ export class WebRTCManager { this.addIceCandidateQueue.forEach(c => this.addIceCandidate(c)); this.addIceCandidateQueue = []; } + this.peerId = msg.source } else if (msg.type === 'answer') { + console.log('answer:', this.myId); this.setRemoteDescription(msg.sdp); } else if (msg.type === 'candidate') { this.addIceCandidate(msg.candidate); @@ -80,6 +83,7 @@ export class WebRTCManager { }; } + createPeerConnection() { if (!this.isInitialized || !this.localStream) { throw new Error('RTC가 초기화되지 않았거나 마이크 스트림이 없습니다.'); @@ -95,20 +99,36 @@ export class WebRTCManager { this.localStream.getTracks().forEach(track => { this.pc.addTrack(track, this.localStream); + console.log(this.pc); }); this.pc.ontrack = (event) => { this.remoteStream = event.streams[0]; + console.log('ontrack', this.remoteStream) if (this.onRemoteStream) this.onRemoteStream(this.remoteStream); }; + this.onRemoteStream = (stream) => { + const audio = new Audio(); + audio.muted = false; + audio.volume = 1; + audio.srcObject = stream; + audio.play().catch(e => console.warn('자동 재생 실패:', e)); + console.log('음성실행') + } + this.pc.onicecandidate = (event) => { - if (event.candidate && this.ws?.readyState === WebSocket.OPEN) { - this.ws.send(JSON.stringify({ - target: this.peerId, - type: 'candidate', - candidate: event.candidate - })); + if (event.candidate) { + console.log('📤 ICE Candidate:', event.candidate.candidate); + if (this.ws?.readyState === WebSocket.OPEN) { + this.ws.send(JSON.stringify({ + target: this.peerId, + type: 'candidate', + candidate: event.candidate + })); + } + } else { + console.log('✅ ICE Candidate 수집 완료'); } }; @@ -131,22 +151,26 @@ export class WebRTCManager { this.peerId = targetId; this.createPeerConnection(); const offer = await this.pc.createOffer(); + await this.pc.setLocalDescription(offer); this.ws.send(JSON.stringify({ target: targetId, + source: this.myId, type: 'offer', sdp: offer })); console.log('📤 Offer 전송 완료'); - console.log('📤 Offer SDP:', offer.sdp); } async setRemoteDescription(sdp) { // ✅ PeerConnection이 없으면 자동 생성 (수신 측 대응) if (!this.pc) { - this.createPeerConnection(); // ← pc 생성 + localStream 트랙 추가 + if (!this.localStream) { + throw new Error('로컬 스트림이 준비되지 않았습니다. init()을 먼저 호출하세요.'); + } + this.createPeerConnection(); } await this.pc.setRemoteDescription(new RTCSessionDescription(sdp)); @@ -155,17 +179,20 @@ export class WebRTCManager { const answer = await this.pc.createAnswer(); await this.pc.setLocalDescription(answer); + console.log(this.peerId) this.ws.send(JSON.stringify({ target: this.peerId || 'unknown', type: 'answer', sdp: answer })); - console.log('📥 Answer 전송 완료'); + console.log('📥 Answer 전송 완료', sdp); console.log('로컬 스트림 트랙 수:', this.localStream.getTracks().length); } + } + async addIceCandidate(candidate) { if (this.pc && this.pc.remoteDescription) { await this.pc.addIceCandidate(new RTCIceCandidate(candidate));