test.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!--
  2. SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  3. SPDX-License-Identifier: MIT
  4. -->
  5. <div id="media"></div>
  6. <script>
  7. const pc = new RTCPeerConnection()
  8. pc.ontrack = event => {
  9. if (event.track.kind === 'audio') {
  10. var el = document.createElement(event.track.kind)
  11. el.srcObject = new MediaStream(event.streams[0].getAudioTracks())
  12. document.getElementById('media').appendChild(el)
  13. }
  14. }
  15. pc.oniceconnectionstatechange = event => {
  16. console.log("connection", pc.iceConnectionState)
  17. if (pc.iceConnectionState == 'connected') {
  18. setInterval(statsReport, 1000)
  19. }
  20. }
  21. pc.onicecandidate = event => {
  22. if (event.candidate === null) {
  23. console.log("sdp", JSON.stringify(pc.localDescription))
  24. }
  25. }
  26. pc.addTransceiver('audio', {'direction': 'recvonly'})
  27. const dc = pc.createDataChannel("upper")
  28. dc.onmessage = event => {
  29. dc.send(event.data.toUpperCase())
  30. }
  31. pc.createOffer().then(d => pc.setLocalDescription(d)).catch(console.log)
  32. const statsReport = async () => {
  33. const stats = await pc.getStats()
  34. var data = []
  35. await stats.forEach(item => {
  36. data.push(item)
  37. })
  38. console.log("stats", JSON.stringify(data))
  39. }
  40. </script>