yirtc-sdk-web
Version:
基于WebRTC的实时音视频SDK
111 lines (110 loc) • 3.81 kB
JavaScript
/**
* 设备工具类
*/
export class Device {
/**
* 检查浏览器兼容性
* @returns boolean
*/
static checkCompatibility() {
// 检查WebRTC核心API
const hasRTCPeerConnection = !!window.RTCPeerConnection;
const hasMediaDevices = !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
const hasWebRTC = hasRTCPeerConnection && hasMediaDevices;
// 检查浏览器类型和版本
const ua = navigator.userAgent;
const isChrome = /Chrome/.test(ua) && !/Edge/.test(ua);
const isFirefox = /Firefox/.test(ua);
const isSafari = /Safari/.test(ua) && !/Chrome/.test(ua);
const isEdge = /Edge/.test(ua);
// 简单的版本检查
let isCompatible = false;
if (isChrome) {
// Chrome 60+
const match = ua.match(/Chrome\/(\d+)/);
const version = match ? parseInt(match[1], 10) : 0;
isCompatible = version >= 60;
}
else if (isFirefox) {
// Firefox 52+
const match = ua.match(/Firefox\/(\d+)/);
const version = match ? parseInt(match[1], 10) : 0;
isCompatible = version >= 52;
}
else if (isSafari) {
// Safari 11+
const match = ua.match(/Version\/(\d+)/);
const version = match ? parseInt(match[1], 10) : 0;
isCompatible = version >= 11;
}
else if (isEdge) {
// Edge 79+ (Chromium-based)
const match = ua.match(/Edge\/(\d+)/);
const version = match ? parseInt(match[1], 10) : 0;
isCompatible = version >= 79;
}
return hasWebRTC && (isCompatible || this.compatMode);
}
/**
* 启用兼容模式
*/
static enableCompatMode() {
this.compatMode = true;
console.log('[Device] 兼容模式已启用');
}
/**
* 禁用兼容模式
*/
static disableCompatMode() {
this.compatMode = false;
console.log('[Device] 兼容模式已禁用');
}
/**
* 获取浏览器信息
* @returns {name: string, version: string}
*/
static getBrowserInfo() {
const ua = navigator.userAgent;
let browserName = '未知';
let version = '未知';
if (/Edge/.test(ua)) {
browserName = 'Edge';
const match = ua.match(/Edge\/(\d+\.\d+)/);
version = match ? match[1] : '未知';
}
else if (/Chrome/.test(ua)) {
browserName = 'Chrome';
const match = ua.match(/Chrome\/(\d+\.\d+)/);
version = match ? match[1] : '未知';
}
else if (/Firefox/.test(ua)) {
browserName = 'Firefox';
const match = ua.match(/Firefox\/(\d+\.\d+)/);
version = match ? match[1] : '未知';
}
else if (/Safari/.test(ua) && !/Chrome/.test(ua)) {
browserName = 'Safari';
const match = ua.match(/Version\/(\d+\.\d+)/);
version = match ? match[1] : '未知';
}
return { name: browserName, version };
}
/**
* 检查是否支持屏幕共享
* @returns boolean
*/
static isScreenShareSupported() {
// @ts-ignore
return !!navigator.mediaDevices.getDisplayMedia;
}
/**
* 检查是否支持音频处理
* @returns boolean
*/
static isAudioProcessingSupported() {
return typeof window !== 'undefined' &&
(typeof window.AudioContext !== 'undefined' ||
typeof window.webkitAudioContext !== 'undefined');
}
}
Device.compatMode = false;