|
| 1 | +import * as Bowser from "bowser"; |
| 2 | +import { createLogger } from "./utils/logger"; |
| 3 | + |
| 4 | +import { OperationSystem, Browser, Platform, Engine, MediaDevice } from './schema/Samples'; |
| 5 | +import { IndexedMap } from "./utils/IndexedMap"; |
| 6 | +import { Sampler } from "./Sampler"; |
| 7 | + |
| 8 | +// import * as proto from "./ProtobufSamples" |
| 9 | +const logger = createLogger("ClientDevices"); |
| 10 | + |
| 11 | +const UNKNOWN_OS: OperationSystem = { |
| 12 | + name: "Unkown", |
| 13 | + version: undefined, |
| 14 | + versionName: undefined, |
| 15 | +}; |
| 16 | + |
| 17 | +const UNKNOWN_BROWSER: Browser = { |
| 18 | + name: "Unknown", |
| 19 | + version: undefined, |
| 20 | +}; |
| 21 | + |
| 22 | +const UNKNOWN_PLATFORM: Platform = { |
| 23 | + type: "Unknown", |
| 24 | + vendor: undefined, |
| 25 | + model: undefined, |
| 26 | +}; |
| 27 | + |
| 28 | +const UNKNOWN_ENGINE: Engine = { |
| 29 | + name: "Unknown", |
| 30 | + version: undefined, |
| 31 | +}; |
| 32 | + |
| 33 | +const MEDIA_DEVICE_KIND = 'mediaDeviceKind'; |
| 34 | + |
| 35 | +export type StoredMediaDevice = MediaDevice & { |
| 36 | + sampled: boolean, |
| 37 | +} |
| 38 | + |
| 39 | +export class ClientMetaData { |
| 40 | + public readonly operationSystem: OperationSystem; |
| 41 | + public readonly browser: Browser; |
| 42 | + public readonly platform: Platform; |
| 43 | + public readonly engine: Engine; |
| 44 | + private readonly _mediaDevices: IndexedMap<string, StoredMediaDevice, MediaDeviceKind>; |
| 45 | + |
| 46 | + public constructor( |
| 47 | + |
| 48 | + ) { |
| 49 | + try { |
| 50 | + /* eslint-disable @typescript-eslint/no-explicit-any */ |
| 51 | + let outerNavigator: any = undefined; |
| 52 | + if (navigator !== undefined) outerNavigator = navigator; |
| 53 | + else if (window !== undefined && window.navigator !== undefined) outerNavigator = window.navigator; |
| 54 | + else throw new Error(`navigator is not available`); |
| 55 | + const parsedResult = Bowser.parse(outerNavigator.userAgent); |
| 56 | + this.browser = parsedResult.browser; |
| 57 | + this.engine = parsedResult.engine; |
| 58 | + this.operationSystem = parsedResult.os; |
| 59 | + this.platform = parsedResult.platform; |
| 60 | + } catch (err) { |
| 61 | + logger.warn(`Cannot collect media devices and navigator data, because an error occurred`, err); |
| 62 | + this.operationSystem = UNKNOWN_OS; |
| 63 | + this.browser = UNKNOWN_BROWSER; |
| 64 | + this.platform = UNKNOWN_PLATFORM; |
| 65 | + this.engine = UNKNOWN_ENGINE; |
| 66 | + } |
| 67 | + |
| 68 | + this._mediaDevices = new IndexedMap<string, StoredMediaDevice, MediaDeviceKind>() |
| 69 | + .addIndex(MEDIA_DEVICE_KIND, (device) => device.kind) |
| 70 | + ; |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + public set mediaDevices(values: MediaDevice[]) { |
| 75 | + const visited = new Set<string>(); |
| 76 | + for (const mediaDevice of values) { |
| 77 | + if (!mediaDevice.id) continue; |
| 78 | + visited.add(mediaDevice.id); |
| 79 | + if (this._mediaDevices.has(mediaDevice.id)) continue; |
| 80 | + this._mediaDevices.set(mediaDevice.id, { |
| 81 | + ...mediaDevice, |
| 82 | + sampled: false, |
| 83 | + }); |
| 84 | + } |
| 85 | + for (const [id] of this._mediaDevices.entries()) { |
| 86 | + if (visited.has(id)) continue; |
| 87 | + this._mediaDevices.delete(id); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + public get mediaDevices(): MediaDevice[] { |
| 92 | + return Array.from(this._mediaDevices.values()); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Iterable iterator for the audio input devices obtained by the observer. |
| 97 | + */ |
| 98 | + public audioInputs(): IterableIterator<StoredMediaDevice> { |
| 99 | + return this._mediaDevices.values(MEDIA_DEVICE_KIND, "audioinput"); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Iterable iterator for the audio output devices obtained by the observer. |
| 104 | + */ |
| 105 | + public audioOutputs(): IterableIterator<StoredMediaDevice> { |
| 106 | + return this._mediaDevices.values(MEDIA_DEVICE_KIND, "audiooutput"); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Iterable iterator for the video input devices obtained by the observer. |
| 111 | + */ |
| 112 | + public videoInputs(): IterableIterator<StoredMediaDevice> { |
| 113 | + return this._mediaDevices.values(MEDIA_DEVICE_KIND, "videoinput"); |
| 114 | + } |
| 115 | +} |
0 commit comments