<template>
  <div class="cpt-player-webrtc">
    <video id="rtc_media_player" ref="webrtc" controls autoplay></video>
  </div>
</template>

<script>
import $ from "./jquery-1.10.2.min";
import {
  SrsRtcPublisherAsync,
  SrsRtcPlayerAsync,
  SrsRtcFormatSenders,
} from "./srs.sdk";

window.$ = $;

export default {
  props: {
    data: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      sdk: null,
    };
  },
  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.data.vUrl)
          .then(function (session) {})
          .catch(function (reason) {
            console.log('srs err', reason)
            _this.sdk.close();
          });
      }
    },
    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);
    },
  },
  
};
</script>

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