rtcPlayer.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <div id="rtcPlayer">
  3. <video id='webRtcPlayerBox' controls autoplay style="text-align:left;">
  4. Your browser is too old which doesn't support HTML5 video.
  5. </video>
  6. </div>
  7. </template>
  8. <script>
  9. let webrtcPlayer = null;
  10. export default {
  11. name: 'rtcPlayer',
  12. data() {
  13. return {
  14. timer: null
  15. };
  16. },
  17. props: ['videoUrl', 'error', 'hasaudio'],
  18. mounted () {
  19. let paramUrl = decodeURIComponent(this.$route.params.url)
  20. this.$nextTick(() =>{
  21. if (typeof (this.videoUrl) == "undefined") {
  22. this.videoUrl = paramUrl;
  23. }
  24. console.log("初始化时的地址为: " + this.videoUrl)
  25. this.play(this.videoUrl)
  26. })
  27. },
  28. watch:{
  29. videoUrl(newData, oldData){
  30. this.pause();
  31. this.play(newData);
  32. },
  33. immediate:true
  34. },
  35. methods: {
  36. play: function (url) {
  37. webrtcPlayer = new ZLMRTCClient.Endpoint({
  38. element: document.getElementById('webRtcPlayerBox'),// video 标签
  39. debug: true,// 是否打印日志
  40. zlmsdpUrl: url,//流地址
  41. simulecast: false,
  42. useCamera: false,
  43. audioEnable: false,
  44. videoEnable: false,
  45. recvOnly: true,
  46. })
  47. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_ICE_CANDIDATE_ERROR,(e)=>{// ICE 协商出错
  48. console.error('ICE 协商出错')
  49. this.eventcallbacK("ICE ERROR", "ICE 协商出错")
  50. });
  51. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_ON_REMOTE_STREAMS,(e)=>{//获取到了远端流,可以播放
  52. console.error('播放成功',e.streams)
  53. this.eventcallbacK("playing", "播放成功")
  54. });
  55. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_OFFER_ANWSER_EXCHANGE_FAILED,(e)=>{// offer anwser 交换失败
  56. console.error('offer anwser 交换失败',e)
  57. this.eventcallbacK("OFFER ANSWER ERROR ", "offer anwser 交换失败")
  58. if (e.code ==-400 && e.msg=="流不存在"){
  59. console.log("111111")
  60. this.timer = setTimeout(()=>{
  61. this.webrtcPlayer.close();
  62. this.play(url)
  63. }, 100)
  64. }
  65. });
  66. webrtcPlayer.on(ZLMRTCClient.Events.WEBRTC_ON_LOCAL_STREAM,(s)=>{// 获取到了本地流
  67. // document.getElementById('selfVideo').srcObject=s;
  68. this.eventcallbacK("LOCAL STREAM", "获取到了本地流")
  69. });
  70. },
  71. pause: function () {
  72. if (webrtcPlayer != null) {
  73. webrtcPlayer.close();
  74. webrtcPlayer = null;
  75. }
  76. },
  77. eventcallbacK: function(type, message) {
  78. console.log("player 事件回调")
  79. console.log(type)
  80. console.log(message)
  81. }
  82. },
  83. destroyed() {
  84. clearTimeout(this.timer);
  85. },
  86. }
  87. </script>
  88. <style>
  89. .LodingTitle {
  90. min-width: 70px;
  91. }
  92. #rtcPlayer{
  93. width: 100%;
  94. }
  95. #webRtcPlayerBox{
  96. width: 100%;
  97. max-height: 56vh;
  98. background-color: #000;
  99. }
  100. /* 隐藏logo */
  101. /* .iconqingxiLOGO {
  102. display: none !important;
  103. } */
  104. </style>