feat: add bark battle browser prototype
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import type { MicrophoneFailureReason } from '../domain/BarkBattleTypes';
|
||||
|
||||
export function mapGetUserMediaError(error: unknown): MicrophoneFailureReason {
|
||||
const name = error && typeof error === 'object' && 'name' in error ? String((error as { name?: unknown }).name) : '';
|
||||
if (name === 'NotAllowedError' || name === 'SecurityError') return 'permission-denied';
|
||||
if (name === 'NotFoundError' || name === 'DevicesNotFoundError') return 'not-found';
|
||||
if (name === 'NotReadableError' || name === 'TrackStartError') return 'not-readable';
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
export function isMicrophoneApiSupported(windowLike: { isSecureContext?: boolean; navigator?: Navigator | { mediaDevices?: { getUserMedia?: unknown } } }) {
|
||||
if (windowLike.isSecureContext === false) {
|
||||
return { ok: false as const, reason: 'non-secure-context' as const };
|
||||
}
|
||||
const getUserMedia = windowLike.navigator?.mediaDevices?.getUserMedia;
|
||||
if (typeof getUserMedia !== 'function') {
|
||||
return { ok: false as const, reason: 'unsupported' as const };
|
||||
}
|
||||
return { ok: true as const, reason: null };
|
||||
}
|
||||
|
||||
export function stopMediaStreamTracks(stream: MediaStream) {
|
||||
stream.getTracks().forEach((track) => track.stop());
|
||||
}
|
||||
Reference in New Issue
Block a user