Stream.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import InternalError from "./error/InternalError.js"
  2. import Logger from "./Logger.js"
  3. const logger = new Logger('stream')
  4. export default class Stream {
  5. constructor(e) {
  6. this.el = null
  7. this._streamPlayTimer = null
  8. if (!e) {
  9. this.el = this.createVideoElement();
  10. return
  11. }
  12. this.el = e
  13. }
  14. play(){
  15. return new Promise((e,t)=>{
  16. this._streamPlayTimer = new Timeout(()=>{
  17. t(new InternalError("Stream play timeout"))
  18. }
  19. ,5e3),
  20. this.el && this.el.play().then(()=>{
  21. var r;
  22. e(),
  23. logger.info("Media can autoplay"),
  24. (r = this._streamPlayTimer) == null || r.clear()
  25. }
  26. ).catch(r=>{
  27. var n;
  28. logger.error("Media Failed to autoplay"),
  29. logger.error(r),
  30. t(new InternalError("Media Failed to autoplay")),
  31. (n = this._streamPlayTimer) == null || n.clear()
  32. }
  33. )
  34. }
  35. )
  36. }
  37. createVideoElement() {
  38. const e = document.createElement("video");
  39. return e.muted = !0,
  40. e.autoplay = !1,
  41. e.playsInline = !0,
  42. e.setAttribute("autostart", "false"),
  43. e.setAttribute("controls", "controls"),
  44. e.setAttribute("muted", "true"),
  45. e.setAttribute("preload", "auto"),
  46. e.setAttribute("hidden", "hidden"),
  47. document.body.appendChild(e),
  48. e
  49. }
  50. }