index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <div>
  3. <video id="video" style="width: 100%"></video>
  4. <canvas id="canvas" style="display: none"></canvas>
  5. <canvas id="draw" style="display: none"></canvas>
  6. </div>
  7. </template>
  8. <script>
  9. import Player from './libs/player'
  10. export default {
  11. name: "index",
  12. props:['rtspData'],
  13. data(){
  14. return{
  15. options:{
  16. video:null,
  17. canvas: null,
  18. drawer: null,
  19. wsUrl:null,
  20. rtspUrl:null
  21. },
  22. player:null
  23. }
  24. },
  25. watch:{
  26. 'rtspData': {
  27. handler(nv,ov){
  28. console.log('新值1->',nv)
  29. if(nv != null && nv.rtsp && nv.ws && /rtsp:\/\/.+/.test(nv.rtsp) && /ws:\/\/.+/.test(nv.ws)){
  30. console.log('新值2->',nv)
  31. this.options.wsUrl = nv.ws
  32. this.options.rtspUrl = nv.rtsp
  33. if(this.options.video && this.options.wsUrl && this.options.rtspUrl) {
  34. console.log('---watch player---')
  35. this.createPlayer()
  36. }
  37. }
  38. },
  39. immediate:true
  40. }
  41. },
  42. mounted() {
  43. console.log('--mounted--')
  44. this.options.video = document.getElementById('video')
  45. this.options.canvas = document.getElementById('canvas')
  46. this.options.drawer = document.getElementById('draw')
  47. //this.player.connect()
  48. if(this.options.rtspUrl && this.options.wsUrl){
  49. console.log('---mounted player---')
  50. //直接播放
  51. this.createPlayer()
  52. }
  53. },
  54. destroyed() {
  55. if(this.player)
  56. this.player.close()
  57. },
  58. methods:{
  59. createPlayer(){
  60. if(this.player !=null) this.player.close()
  61. this.player = new Player(this.options)
  62. this.player.init(this)
  63. this.player.on('error', function () {
  64. console.log('连接失败')
  65. })
  66. this.player.on('noStream', function () {
  67. console.log('noStream');
  68. this.player.close();
  69. this.player = null;
  70. this.player = new Player(this.options);
  71. this.player.init();
  72. this.player.connect();
  73. })
  74. this.player.on('canplay', function () {
  75. //player.close();
  76. })
  77. this.player.on('initialCompleted', function () {
  78. // let data = [[
  79. // {x: 2861, y: 4395},
  80. // {x: 6403, y: 4013},
  81. // {x: 3260, y: 7986},
  82. // {x: 640, y: 6252}
  83. // ]];
  84. // console.log('initialCompleted')
  85. // player.setROI(data);
  86. })
  87. this.player.on('ROIFinished', function () {
  88. console.log('ROIFinished');
  89. })
  90. this.player.connect()
  91. },
  92. close(){
  93. player.close()
  94. }
  95. }
  96. }
  97. </script>
  98. <style scoped>
  99. </style>