jsで画面共有の機能を実装しようとしています。
Video、Audioのmute,unmuteはできたのですが、画面共有だけできていません。
mute,unmute と同様にlocalStreamに値を入れてみたのですが、remoteのStreamに反映されません。
以下、ソースコードです。
javascript
1 2const Peer = window.Peer; 3 4(async function main() { 5 const localVideo = document.getElementById('js-local-stream'); 6 const joinTrigger = document.getElementById('js-join-trigger'); 7 const leaveTrigger = document.getElementById('js-leave-trigger'); 8 const remoteVideos = document.getElementById('js-remote-streams'); 9 const roomId = document.getElementById('js-room-id'); 10 const roomMode = document.getElementById('js-room-mode'); 11 const localText = document.getElementById('js-local-text'); 12 const sendTrigger = document.getElementById('js-send-trigger'); 13 const messages = document.getElementById('js-messages'); 14 const meta = document.getElementById('js-meta'); 15 const sdkSrc = document.querySelector('script[src*=skyway]'); 16 const Audio = document.getElementById('Audio') 17 const Video = document.getElementById('Video') 18 const ScreenShare = document.getElementById('ScreenShare') 19 meta.innerText = ` 20 UA: ${navigator.userAgent} 21 SDK: ${sdkSrc ? sdkSrc.src : 'unknown'} 22 `.trim(); 23 24 const getRoomModeByHash = () => (location.hash === '#sfu' ? 'sfu' : 'mesh'); 25 26 roomMode.textContent = getRoomModeByHash(); 27 window.addEventListener( 28 'hashchange', 29 () => (roomMode.textContent = getRoomModeByHash()) 30 ); 31 32 const localStream = await navigator.mediaDevices 33 .getUserMedia({ 34 audio: true, 35 video: true, 36 }) 37 .catch(console.error); 38 39 // Render local stream 40 localVideo.muted = true; 41 localVideo.srcObject = localStream; 42 localVideo.playsInline = true; 43 await localVideo.play().catch(console.error); 44 45 // eslint-disable-next-line require-atomic-updates 46 const peer = (window.peer = new Peer({ 47 key: window.__SKYWAY_KEY__, 48 debug: 3, 49 })); 50 51 // Register join handler 52 joinTrigger.addEventListener('click', () => { 53 // Note that you need to ensure the peer has connected to signaling server 54 // before using methods of peer instance. 55 if (!peer.open) { 56 return; 57 } 58 59 const room = peer.joinRoom(roomId.value, { 60 mode: getRoomModeByHash(), 61 stream: localStream, 62 }); 63 64 room.once('open', () => { 65 messages.textContent += '=== You joined ===\n'; 66 }); 67 room.on('peerJoin', peerId => { 68 messages.textContent += `=== ${peerId} joined ===\n`; 69 }); 70 71 // Render remote stream for new peer join in the room 72 room.on('stream', async stream => { 73 const newVideo = document.createElement('video'); 74 newVideo.srcObject = stream; 75 newVideo.playsInline = true; 76 // mark peerId to find it later at peerLeave event 77 newVideo.setAttribute('data-peer-id', stream.peerId); 78 remoteVideos.append(newVideo); 79 await newVideo.play().catch(console.error); 80 }); 81 82 room.on('data', ({ data, src }) => { 83 // Show a message sent to the room and who sent 84 messages.textContent += `${src}: ${data}\n`; 85 }); 86 87 // for closing room members 88 room.on('peerLeave', peerId => { 89 const remoteVideo = remoteVideos.querySelector( 90 `[data-peer-id="${peerId}"]` 91 ); 92 remoteVideo.srcObject.getTracks().forEach(track => track.stop()); 93 remoteVideo.srcObject = null; 94 remoteVideo.remove(); 95 96 messages.textContent += `=== ${peerId} left ===\n`; 97 }); 98 99 // for closing myself 100 room.once('close', () => { 101 sendTrigger.removeEventListener('click', onClickSend); 102 messages.textContent += '== You left ===\n'; 103 Array.from(remoteVideos.children).forEach(remoteVideo => { 104 remoteVideo.srcObject.getTracks().forEach(track => track.stop()); 105 remoteVideo.srcObject = null; 106 remoteVideo.remove(); 107 }); 108 }); 109 110 sendTrigger.addEventListener('click', onClickSend); 111 leaveTrigger.addEventListener('click', () => room.close(), { once: true }); 112 113 function onClickSend() { 114 // Send message to all of the peers in the room via websocket 115 room.send(localText.value); 116 117 messages.textContent += `${peer.id}: ${localText.value}\n`; 118 messages.scrollTop = messages.scrollHeight-messages.clientHeight; 119 localText.value = ''; 120 } 121 122 123//画面共有部分 124 ScreenShare.addEventListener('click',()=>{ 125 const ssStream = navigator.mediaDevices.getDisplayMedia({video:{width: 1280,height:720,frameRate:60}, audio: false}).then( stream => { 126 localVideo.srcObject = stream; 127 localVideo.play(); 128 localStream.getVideoTracks()[0] = stream;//映像をグローバル変数に 129 }) 130 131 Audio.addEventListener('click',()=>{ 132 if(localStream.getAudioTracks()[0].enabled == true){ 133 localStream.getAudioTracks()[0].enabled = false 134 Audio.style.textDecoration = 'line-through' 135 Audio.style.backgroundColor="red" 136 }else{ 137 localStream.getAudioTracks()[0].enabled = true 138 Audio.style.textDecoration = 'none' 139 Audio.style.backgroundColor="rgb(239,239,239)" 140 } 141 }) 142 143 Video.addEventListener('click',()=>{ 144 if(localStream.getVideoTracks()[0].enabled == true){ 145 localStream.getVideoTracks()[0].enabled = false 146 Video.style.textDecoration = 'line-through' 147 Video.style.backgroundColor="red" 148 }else{ 149 localStream.getVideoTracks()[0].enabled = true 150 Video.style.textDecoration = 'none' 151 Video.style.backgroundColor="rgb(239,239,239)" 152 } 153 }) 154 }); 155 156 peer.on('error', console.error); 157})(); 158
Skyway SDKを使用しているので、以下を参考にしています。
https://webrtc.ecl.ntt.com/api-reference/javascript.html#%E7%94%BB%E9%9D%A2%E5%85%B1%E6%9C%89
あなたの回答
tips
プレビュー