camera.es6 4.7 KB
Newer Older
W
wangqun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/**
 * @file 视频流类
 * @author zhangmiao06
 */
import $ from 'webpack-zepto';
export default class Camera {
    constructor(option) {
        this.option = option;
        this.video = option.videoDom;
        // 标志是否可以切换摄像头
        this.haveDevice = false;
        // 设置视频流宽度
        if (option.width) {
            this.video.width = option.width;
        }
        else if (option.height) {
            this.video.height = option.height;
        }
        else {
            this.video.width = window.innerWidth;
        }
        this.deviceInfos = [];
        if(navigator.mediaDevices) {
            this.haveDevice = true;
        }
26 27 28
        if (option.constraints) {
            this.constraints = option.constraints;
        }
W
wangqun 已提交
29 30 31
    }

    // 访问用户媒体设备的兼容方法
W
wangqun 已提交
32
    // safari在getusermedia之后 才能拿到deviceid
W
wangqun 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    run(deviceId, callback) {
        if (window.stream) {
            window.stream.getTracks().forEach(function (track) {
                track.stop();
            });
        }

        let constraints = {
            video: {}
        };
        const success = stream => {
            this.success(stream, callback);
        };
        const error = this.error.bind(this);
        if (this.deviceInfos.length) {
W
wangqun 已提交
48 49 50 51 52 53
            constraints.video.deviceId =  {exact: deviceId || this.deviceInfos[0].deviceId};
        }
        if (!constraints.video.deviceId) {
            constraints = {
                video: true
            };
W
wangqun 已提交
54
        }
55 56 57
        else if (this.constraints) {
            constraints = this.constraints;
        }
W
wangqun 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

        if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
            // 最新的标准API
            navigator.mediaDevices.getUserMedia(constraints).then(success).catch(error);
        }
        else if (navigator.webkitGetUserMedia) {
            // webkit核心浏览器
            navigator.webkitGetUserMedia(constraints, success, error);
        }
        else if (navigator.mozGetUserMedia) {
            // firfox浏览器
            navigator.mozGetUserMedia(constraints, success, error);
        }
        else if (navigator.getUserMedia) {
            // 旧版API
            navigator.getUserMedia(constraints, success, error);
        }
        else {
            console.log('您的浏览器不支持获取视频流~');
        }
    }

    success(stream, callback) {
        const domElement = this.video;
        // make stream available to console
        window.stream = stream;
        // 旧的浏览器可能没有srcObject
        const URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
        if ('srcObject' in domElement) {
            try {
                domElement.srcObject = stream;
            } catch (error) {
                domElement.src = URL.createObjectURL(stream) || stream;
            }
        } else {
            // 防止再新的浏览器里使用它,应为它已经不再支持了
            domElement.src = URL.createObjectURL(stream) || stream;
        }
        domElement.addEventListener('loadeddata', () => {
            // 设置视频流高度
            if (this.option.height) {
                domElement.width = $(domElement).width();
            }
            else {
                domElement.height = $(domElement).height();
            }
            domElement.play();
            callback && callback();
        }, false);
    }

    error(error) {
        alert(`访问用户媒体设备失败${error.name}, ${error.message}`);
    }
    // 处理摄像头列表
    gotDevices(deviceInfos) {
        const ua = navigator.userAgent;
        const isIos = /iphone|ipod|ipad/ig.test(ua);

        let delt = -1;
        const range = deviceInfos.length;
        let start = range - 1;
        let end = - 1;
        // ios机型camare顺序相反
        if (isIos) {
            delt = 1;
            start = 0;
            end = range;
        }
        for (let i = start; i !== end; i += delt) {
            const deviceInfo = deviceInfos[i];
            if (deviceInfo.kind === 'videoinput') {
                this.deviceInfos.push(deviceInfos[i]);
            }
        }
    }

    get curVideo() {
        return this.video;
    }
    getDevices() {
        return new Promise((resolve, reject)=> {
            if (this.haveDevice) {
                if (this.deviceInfos.length) {
                    resolve(this.deviceInfos);
                }
                else {
                    navigator.mediaDevices.enumerateDevices()
                    .then(this.gotDevices.bind(this))
                    .then(()=> {
                        resolve(this.deviceInfos);
                    });
                }
            }
            else {
                resolve([]);
            }
        });
    }
}