当前位置:   article > 正文

支持webrtc的摄像头_网络摄像头 支持webrtc

网络摄像头 支持webrtc

目前google chrome 已经原生支持webrtc了,做低延迟播放的小伙伴们有福了。目前已经有淘宝在售支持webrtc的摄像头。超低延迟播放视频不再是难事了。

网页访问代码

  1. function initRtcPeerConnection(streamPath) {
  2. rtcLoading = true;
  3. var rtcPeerConnection = new RTCPeerConnection({
  4. iceServers: [
  5. {
  6. urls: 'stun:stun.cloud.xstrive.com:3478'
  7. },
  8. ]
  9. });
  10. rtcPeerConnection.addTransceiver('video', {
  11. direction: "recvonly"
  12. });
  13. rtcPeerConnection.addTransceiver('audio',{
  14. direction:'recvonly'
  15. });
  16. rtcPeerConnection.onsignalingstatechange = e => {
  17. // console.log('onsignalingstatechange', e);
  18. };
  19. rtcPeerConnection.oniceconnectionstatechange = e => {
  20. console.log('oniceconnectionstatechange', rtcPeerConnection.iceConnectionState);
  21. };
  22. var iceGatheringState = false
  23. rtcPeerConnection.onicecandidate = event => {
  24. if (rtcPeerConnection.iceGatheringState === "complete") {
  25. iceGatheringState = true
  26. // console.log("onicecandidate iceGatheringState: ", rtcPeerConnection.iceGatheringState)
  27. }
  28. };
  29. rtcPeerConnection.ontrack = event => {
  30. // console.log('ontrack', event);
  31. document.getElementById('player').srcObject = event.streams[0];
  32. };
  33. rtcPeerConnection.createOffer().then((ret)=> {
  34. rtcPeerConnection.setLocalDescription(ret).finally(()=> {
  35. var i1 = 0
  36. var inter = setInterval(()=> {
  37. i1 += 1;
  38. if (iceGatheringState || i1 > 200) {
  39. clearInterval(inter)
  40. reqFetch(rtcPeerConnection, streamPath)
  41. }
  42. }, 20)
  43. })
  44. });
  45. rtcPeerConn = rtcPeerConnection
  46. }
  47. function reqFetch(rtcPeerConnection, streamPath) {
  48. var data = {
  49. sdp: rtcPeerConnection.localDescription.sdp,
  50. type: rtcPeerConnection.localDescription.type,
  51. }
  52. $.ajax({
  53. type:"POST",
  54. url:"/api/webrtc/play?streamPath=" + streamPath,
  55. data: JSON.stringify(data),
  56. contentType: "text/plain",
  57. success: function(data){
  58. rtcLoading = false
  59. var result = data;
  60. if (result.code) {
  61. rtcPeerConnection.close();
  62. alert(result.msg)
  63. return;
  64. }
  65. rtcPeerConnection.setRemoteDescription(new RTCSessionDescription({ sdp: result.sdp, type: 'answer' })).catch((ret)=> {
  66. console.log("setRemoteDescription catch:", ret)
  67. alert("播放失败,请稍后尝试")
  68. });
  69. },
  70. error: function () {
  71. rtcLoading = false
  72. rtcPeerConnection.close();
  73. }
  74. })
  75. }
  76. function showPlay() {
  77. // 显示遮罩
  78. $('.mask').css('display','block');
  79. initRtcPeerConnection("hlsram/live0")
  80. }
  81. function closePlayer() {
  82. // 暂时视频
  83. if(rtcPeerConn){
  84. rtcPeerConn.close();
  85. rtcPeerConn = null;
  86. }
  87. var py = document.getElementById('player');
  88. if (py) {
  89. py.srcObject = null
  90. }
  91. // 关闭遮罩
  92. $('.mask').css('display','none');
  93. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/538996
推荐阅读
相关标签
  

闽ICP备14008679号