<template>
  <div class="cpt-player-webrtc">
    <video id="rtc_media_player" ref="webrtc" controls autoplay :class="{small: !smallFPV}"></video>
    <video v-if="fpvUrl.vUrl" id="rtc_media_player" ref="fpv" controls autoplay :class="{small: smallFPV}"></video>
  </div>
</template>

<script>
import jquery from "jquery";
import {
  SrsRtcPublisherAsync,
  SrsRtcPlayerAsync,
  SrsRtcFormatSenders,
} from "./srs.sdk";
import html2canvas from 'html2canvas';
import {
  dataURLToBlob,
  canvasToDataURL,
} from "../../../../../../utils/image-tool";

const $ = jquery;
window.$ = $;

export default {
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
    smallFPV: {
      type: Boolean,
      default: () => true,
    },
    fpvUrl: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      sdk: null,
    };
  },
  computed: {
    vUrl(){
      return this.data?.vUrl?.replace('srs://', '');
    }
  },
  watch: {
    data: {
      handler(value) {
        this.$nextTick(() => {
          this.init();
        });
      },
      deep: true,
      immediate: true,
    },
  },
  beforeDestroy() {
    this.sdk.close();
  },
  methods: {
    pause() {
      if (this.$refs["webrtc"]) {
        this.$refs["webrtc"].pause();
      }
    },
    play() {
      if (this.$refs["webrtc"]) {
        this.$refs["webrtc"].play();
      }
    },
    init() {
      let _this = this;
      if (this.$refs["webrtc"]) {
        if (this.sdk) {
          this.sdk.close();
        }
        this.sdk = new SrsRtcPlayerAsync();
        this.$refs["webrtc"].srcObject = this.sdk.stream;
        this.sdk
          .play(this.vUrl)
          .then(function (session) {})
          .catch(function (reason) {
            console.log("srs err", reason);
            _this.sdk.close();
          });
      }

      if (this.fpvUrl?.vUrl) {
        this.sdk = new SrsRtcPlayerAsync();
        this.$refs["fpv"].srcObject = this.sdk.stream;
        this.sdk
          .play(this.fpvUrl.vUrl)
          .then(function (session) {})
          .catch(function (reason) {
            console.log("srs err", reason);
          });
      }
    },
    fullScreen() {
      let video = this.$refs["webrtc"];
      if (video.webkitRequestFullScreen) {
        video.webkitRequestFullScreen();
      } else if (video.requestFullscreen) {
        video.requestFullscreen();
      } else if (video.mozRequestFullScreen)
        setTimeout(() => {
          video.play();
        }, 200);
    },
    screenShot() {
      let video = {};
      let scale = 1;
      // video = this.$refs.video;
      video = this.$refs.webrtc;
      if (!video) {
        return null;
      }
      video.useCORS = true; // 解决跨域
      video.crossOrigin = "Anonymous"; // 这两个参数必填 解决跨域
      var canvas = document.createElement("canvas");
      canvas.width = video.clientWidth;
      canvas.height = video.clientHeight;
      const ctx = canvas.getContext("2d");
      ctx.drawImage(video, 0, 0, canvas.width, canvas.height); // 将video中的数据绘制到canvas里
      // const image = canvas.toDataURL("image/png");
      const image = canvasToDataURL(canvas);
      let blob = dataURLToBlob(image);
      return blob;
    },
  },
};
</script>

<style lang="scss" scoped>
.cpt-player-webrtc {
  height: 100%;
  width: 100%;
  background-color: #000;
  video {
    width: 100%;
    height: 100%;
    object-fit: fill;
  }

  .small {
    width: 300px;
    height: 170px;
    position: absolute;
    right: 0;
    z-index: 1;
    bottom: 0;
  }
}

video::-webkit-media-controls-panel {
  display: none !important;
}

video::-webkit-media-controls-timeline {
  display: none !important;
}
</style>