UNPKG

yirtc-sdk-web

Version:

基于WebRTC的实时音视频SDK

187 lines (186 loc) 6.26 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; /** * YiRTC SDK 入口文件 */ import { Client } from './client'; import { Stream } from './stream'; import { Logger } from './utils/logger'; import { Device } from './utils/device'; import { AdapterFactory, PlatformType } from './adapters/factory'; /** * YiRTC SDK 主类 */ export class YiRTC { /** * 创建客户端实例 * @param config 客户端配置 * @returns Client 客户端实例 */ static createClient(config) { this.logger.info('创建客户端', config); // 如果配置了信令服务器,则创建信令适配器 if (config.signaling) { const signalingType = config.signaling.type; const signalingConfig = Object.assign({ url: config.signaling.url, autoReconnect: true }, config.signaling.options); const signalingAdapter = AdapterFactory.createSignalingAdapter(signalingType, signalingConfig); return new Client(config); } return new Client(config); } /** * 创建音视频流实例 * @param config 流配置 * @returns Stream 流实例 */ static createStream(config) { this.logger.info('创建流', config); return new Stream(config); } /** * 获取所有设备 * @returns Promise<{cameras: DeviceInfo[], microphones: DeviceInfo[], speakers: DeviceInfo[]}> */ static getDevices() { return __awaiter(this, void 0, void 0, function* () { try { const devices = yield navigator.mediaDevices.enumerateDevices(); const cameras = devices .filter(device => device.kind === 'videoinput') .map(device => ({ deviceId: device.deviceId, label: device.label || `摄像头 ${device.deviceId.slice(0, 5)}...` })); const microphones = devices .filter(device => device.kind === 'audioinput') .map(device => ({ deviceId: device.deviceId, label: device.label || `麦克风 ${device.deviceId.slice(0, 5)}...` })); const speakers = devices .filter(device => device.kind === 'audiooutput') .map(device => ({ deviceId: device.deviceId, label: device.label || `扬声器 ${device.deviceId.slice(0, 5)}...` })); return { cameras, microphones, speakers }; } catch (error) { this.logger.error('获取设备失败', error); throw error; } }); } /** * 获取摄像头列表 * @returns Promise<DeviceInfo[]> */ static getCameras() { return __awaiter(this, void 0, void 0, function* () { const { cameras } = yield this.getDevices(); return cameras; }); } /** * 获取麦克风列表 * @returns Promise<DeviceInfo[]> */ static getMicrophones() { return __awaiter(this, void 0, void 0, function* () { const { microphones } = yield this.getDevices(); return microphones; }); } /** * 获取扬声器列表 * @returns Promise<DeviceInfo[]> */ static getSpeakers() { return __awaiter(this, void 0, void 0, function* () { const { speakers } = yield this.getDevices(); return speakers; }); } /** * 检查系统要求 * @returns Promise<boolean> */ static checkSystemRequirements() { return __awaiter(this, void 0, void 0, function* () { try { // 检查浏览器是否支持WebRTC const hasWebRTC = !!window.RTCPeerConnection; // 检查是否支持获取媒体设备 const hasMediaDevices = !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia); return hasWebRTC && hasMediaDevices; } catch (error) { this.logger.error('检查系统要求失败', error); return false; } }); } /** * 检查浏览器兼容性 * @returns boolean */ static checkBrowserCompatibility() { return Device.checkCompatibility(); } /** * 设置区域 * @param area 区域代码 */ static setArea(area) { // 在实际应用中,这里可以设置不同区域的服务器地址 this.logger.info('设置区域', area); } } YiRTC.logger = new Logger('YiRTC'); YiRTC.platform = PlatformType.WEB; /** * 日志相关功能 */ YiRTC.Logger = { /** * 启用日志上传 * @param appKey 应用密钥 */ enableLogUpload(appKey) { Logger.enableLogUpload(appKey); }, /** * 禁用日志上传 */ disableLogUpload() { Logger.disableLogUpload(); } }; /** * 设备相关功能 */ YiRTC.Device = { /** * 启用兼容模式 */ enableCompatMode() { Device.enableCompatMode(); }, /** * 禁用兼容模式 */ disableCompatMode() { Device.disableCompatMode(); } }; // 导出类型定义 export * from './types'; export { Client } from './client'; export { Stream } from './stream';