RealPlayer.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import PlayerItem from './PlayerItem.js'
  2. import {Spinner} from './spin.js'
  3. import CONSTANT from './CONSTANT.js'
  4. const PlayerControl = window.PlayerControl;
  5. /* ---------------- PlayerItem ---------------- */
  6. class RealPlayerItem extends PlayerItem {
  7. /**
  8. * @param {*} opt.wrapperDomId 父级id
  9. * @param {*} opt.index 索引
  10. */
  11. constructor(opt) {
  12. super(opt)
  13. this.canvasId = `${this.domId}-livecanvas`
  14. this.videoId = `${this.domId}-liveVideo`
  15. this.initDom()
  16. this.defaultStatus = $('.default-status', this.$el)
  17. this.error = $('.error', this.$el)
  18. this.controller = $('.player-control', this.$el)
  19. this.initMouseEvent()
  20. /**
  21. * this.state 当前Player状态
  22. * created, ready, playing, pause, stop, closed, error
  23. */
  24. this.setStatus('created')
  25. }
  26. /**
  27. * 播放器模板
  28. */
  29. getTemplate() {
  30. let template = `
  31. <div id="${this.domId}" class="wsplayer-item wsplayer-item-${this.index} ${this.index === 0 ? 'selected' : 'unselected'}">
  32. <div class="full-content flex">
  33. <canvas id="${this.canvasId}" class="kind-stream-canvas" kind-channel-id="0" width="800" height="600"></canvas>
  34. <video id="${this.videoId}" class="kind-stream-canvas" kind-channel-id="0" muted style="display:none" width="800" height="600"></video>
  35. </div>
  36. <div class="default-status">
  37. <img src="./static/WSPlayer/icon/default.png" alt="">
  38. </div>
  39. <div class="player-control top-control-bar">
  40. <span class="stream-info"></span>
  41. <div class="opt-icons">
  42. <div class="opt-icon audio-icon off"></div>
  43. <div class="opt-icon capture-icon"></div>
  44. <div class="opt-icon close-icon"></div>
  45. </div>
  46. </div>
  47. <div class="error">
  48. <div class="error-message"></div>
  49. </div>
  50. </div>
  51. `
  52. return template
  53. }
  54. /**
  55. * 事件监听
  56. */
  57. initMouseEvent() {
  58. super.initMouseEvent()
  59. this.hideTimer = null
  60. this.$el.on('mouseenter mousemove', (evt) => {
  61. if (this.status === 'playing') {
  62. this.hideTimer && clearTimeout(this.hideTimer)
  63. this.setDomVisible($('.player-control', $(`#${this.domId}`)), true)
  64. }
  65. })
  66. this.$el.on('mouseleave', (evt) => {
  67. this.hideTimer = setTimeout(() => {
  68. this.setDomVisible($('.player-control', $(`#${this.domId}`)), false)
  69. }, 300)
  70. })
  71. }
  72. /**
  73. * 设置状态,同时控制组件显示
  74. * created, playing, pause, stop, closed, error
  75. */
  76. setStatus(status, msg) {
  77. this.status = status
  78. switch (this.status) {
  79. case 'created':
  80. case 'closed':
  81. this.setDomVisible(this.defaultStatus, true)
  82. this.setDomVisible(this.error, false)
  83. this.setDomVisible(this.controller, false)
  84. $('.audio-icon', this.$el).removeClass('on').addClass('off')
  85. break;
  86. case 'ready':
  87. case 'playing':
  88. case 'pause':
  89. this.setDomVisible(this.defaultStatus, false)
  90. this.setDomVisible(this.error, false)
  91. break;
  92. case 'error':
  93. this.setDomVisible(this.defaultStatus, false)
  94. $('.error-message', this.$el).text(CONSTANT.errorInfo[msg.errorCode] ? CONSTANT.errorInfo[msg.errorCode] : CONSTANT.errorInfo['defaultErrorMsg'])
  95. this.setDomVisible(this.error, true)
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. /**
  102. * 初始化播放器
  103. * @param {*} options.rtspURL
  104. * @param {*} options.decodeMode 可选参数
  105. * @param {*} options.wsURL 可选参数
  106. */
  107. init(options) {
  108. // testCode
  109. // options.rtspURL = "rtsp://sdfasdf/"
  110. if (this.player) {
  111. this.player.close()
  112. }
  113. if (this.spinner) {
  114. this.spinner.stop()
  115. }
  116. this.spinner = new Spinner({
  117. color: '#ffffff'
  118. }).spin(this.$el[0])
  119. let self = this
  120. this.player = new PlayerControl(Object.assign({
  121. wsURL: this.wsPlayer.wsURL
  122. }, options))
  123. this.setStatus('ready')
  124. this.player.on('ResolutionChanged', function (e) {
  125. console.log(e)
  126. });
  127. this.player.on('PlayStart', function (e) {
  128. console.log(e)
  129. self.setStatus('playing')
  130. });
  131. this.player.on('DecodeStart', function (e) {
  132. console.log('DecodeStart', e)
  133. self.spinner.stop()
  134. if (e.decodeMode === 'video') {
  135. self.videoElem.style.display = '';
  136. self.canvasElem.style.display = 'none';
  137. } else {
  138. self.videoElem.style.display = 'none';
  139. self.canvasElem.style.display = '';
  140. }
  141. $('.stream-info', $(`#${self.domId}`)).text(`${e.encodeMode}, ${e.width}*${e.height}`)
  142. });
  143. this.player.on('UpdateCanvas', function (e) {
  144. if (self.firstTime === 0) {
  145. self.firstTime = e.timestamp;//获取录像文件的第一帧的时间戳
  146. }
  147. });
  148. this.player.on('GetFrameRate', function (e) {
  149. console.log('GetFrameRate: ', e)
  150. });
  151. this.player.on('FrameTypeChange', function (e) {
  152. console.log('FrameTypeChange: ', e)
  153. });
  154. this.player.on('Error', function (e) {
  155. self.spinner.stop()
  156. console.log('Error: ' + JSON.stringify(e))
  157. self.setStatus('error', e)
  158. });
  159. this.player.on('MSEResolutionChanged', function (e) {
  160. console.log('MSEResolutionChanged: ', e)
  161. });
  162. this.player.on('audioChange', function (e) {
  163. console.log('audioChange: ', e)
  164. });
  165. this.player.on('IvsDraw', function (e) {
  166. console.log('IvsDraw: ', e)
  167. });
  168. this.player.on('WorkerReady', function () {
  169. console.log('WorkerReady')
  170. self.player.connect();
  171. })
  172. // this.player.on('FileOver', function (e) {
  173. // console.log('FileOver: ', e)
  174. // });
  175. this.player.on('Waiting', function (e) {
  176. console.log('Waiting: ', e)
  177. });
  178. this.player.on('UpdateTime', function (e) {
  179. console.log('UpdateTime: ', e)
  180. });
  181. this.player.init(this.canvasElem, this.videoElem);
  182. }
  183. }
  184. export default RealPlayerItem