<commit_before><template>
  <video ref="video" :width="this.width" :height="this.height" :src="this.source" :autoplay="this.autoplay"></video>
</template>

<script>
export default {
    data () {
        return {
            stream: '',
            source: '',
            canvas: null
        };
    },
    props: {
        width: {
            type: Number,
            default: 500
        },
        height: {
            type: Number,
            default: 500
        },
        autoplay: {
            type: Boolean,
            default: true
        },
        screenshotFormat: {
          type: String,
          default: 'image/jpeg'
        }
    },
    mounted() {
        if (!this.hasMedia()) {
            this.$emit('notsupported');
            return;
        }

        this.requestMedia();

        if (navigator.getUserMedia) {
            navigator.getUserMedia({ video: true }, stream => {
            this.source = window.URL.createObjectURL(stream);
            this.stream = stream;
            this.$emit('started', stream);
        }, error => {
            this.$emit('error', error);
          });
        }
    },
    methods: {
        hasMedia() {
            return !!this.getMedia();
        },
        getMedia() {
            return (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia);
        },
        requestMedia() {
            navigator.getUserMedia = this.getMedia();
        },
        capture() {
            if (!this.hasMedia()) {
                this.$emit('notsupported');
                return null;
            }
            return this.getCanvas().toDataURL(this.screenshotFormat);
        },
        getCanvas() {
            let video = this.$refs.video;
            if (!this.ctx) {
                let canvas = document.createElement('canvas');
                canvas.height = video.clientHeight;
                canvas.width = video.clientWidth;
                this.canvas = canvas;

                this.ctx = canvas.getContext('2d');<commit_msg>Update media selection to use most recent API so that it's Safari and iOS 11 compatible.<commit_after><template>
  <video ref="video" :width="this.width" :height="this.height" :src="this.source" :autoplay="this.autoplay"></video>
</template>

<script>
let legacyGetUserMediaSupport = (constraints) => {

    // First get ahold of the legacy getUserMedia, if present
    let getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia || navigator.oGetUserMedia);

    // Some browsers just don't implement it - return a rejected promise with an error
    // to keep a consistent interface
    if (!getUserMedia) {
      return Promise.reject(new Error('getUserMedia is not implemented in this browser'));
    }

    // Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
    return new Promise(function(resolve, reject) {
      getUserMedia.call(navigator, constraints, resolve, reject);
    });
};

export default {
    data () {
        return {
            stream: '',
            source: '',
            canvas: null
        };
    },
    props: {
        width: {
            type: Number,
            default: 500
        },
        height: {
            type: Number,
            default: 500
        },
        autoplay: {
            type: Boolean,
            default: true
        },
        screenshotFormat: {
          type: String,
          default: 'image/jpeg'
        }
    },
    mounted() {
        this.setupMedia();

        navigator.mediaDevices.getUserMedia({ video: true}).then( (stream) => {
            try {
                this.source = window.URL.createObjectURL(stream);
                this.stream = stream;
                this.$emit('started', stream);
            } catch (err) {
                this.$refs.video.srcObject = stream;
                this.stream = stream;
                this.$emit('started', stream);
            }
        }).catch((error) => {
            this.$emit('error', error);
        });
    },
    methods: {
        setupMedia() {
            if (navigator.mediaDevices === undefined) {
              navigator.mediaDevices = {};
            }
            if (navigator.mediaDevices.getUserMedia === undefined) {
              navigator.mediaDevices.getUserMedia = legacyGetUserMediaSupport;
            }
        },
        capture() {
            return this.getCanvas().toDataURL(this.screenshotFormat);
        },
        getCanvas() {
            let video = this.$refs.video;
            if (!this.ctx) {
                let canvas = document.createElement('canvas');
                canvas.height = video.clientHeight;
                canvas.width = video.clientWidth;
                this.canvas = canvas;

                this.ctx = canvas.getContext('2d');
{'fraction_non_alphanumeric': 0.12263575350823673, 'fraction_numerical': 0.003254016676835469, 'mean_word_length': 1.629946524064171, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2588281', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1271, 'n_words': 272}