feat(release): prepare 1.1.0 candidate
This commit is contained in:
parent
5a34f66981
commit
5205dcdfa9
736 changed files with 115667 additions and 12203 deletions
677
apps/mobile-rn/src/lib/audio-recorder.ts
Normal file
677
apps/mobile-rn/src/lib/audio-recorder.ts
Normal file
|
|
@ -0,0 +1,677 @@
|
|||
import { NativeModules, PermissionsAndroid, Platform } from 'react-native';
|
||||
import { Dirs, FileSystem } from 'react-native-file-access';
|
||||
import {
|
||||
AudioEncoderAndroidType,
|
||||
AudioSourceAndroidType,
|
||||
AVEncoderAudioQualityIOSType,
|
||||
OutputFormatAndroidType,
|
||||
createSound,
|
||||
type AudioSet,
|
||||
type RecordBackType,
|
||||
} from 'react-native-nitro-sound';
|
||||
import { createUuidV4 } from './random-id';
|
||||
|
||||
const RECORDING_PREFIX = 'd3ro-recording-';
|
||||
const RECORDING_EXTENSION = '.m4a';
|
||||
const RECORDING_MIME_TYPE = 'audio/mp4';
|
||||
const ANDROID_RECORDING_MIME_TYPE = 'audio/wav';
|
||||
|
||||
const AUDIO_SETTINGS: AudioSet = {
|
||||
AudioSourceAndroid: AudioSourceAndroidType.MIC,
|
||||
OutputFormatAndroid: OutputFormatAndroidType.MPEG_4,
|
||||
AudioEncoderAndroid: AudioEncoderAndroidType.AAC,
|
||||
AudioQuality: 'medium',
|
||||
AudioChannels: 1,
|
||||
AudioSamplingRate: 44100,
|
||||
AudioEncodingBitRate: 128000,
|
||||
AVFormatIDKeyIOS: 'aac',
|
||||
AVEncoderAudioQualityKeyIOS: AVEncoderAudioQualityIOSType.high,
|
||||
AVNumberOfChannelsKeyIOS: 1,
|
||||
AVSampleRateKeyIOS: 44100,
|
||||
};
|
||||
|
||||
type NativeRecorderState =
|
||||
| 'idle'
|
||||
| 'starting'
|
||||
| 'recording'
|
||||
| 'pausing'
|
||||
| 'paused'
|
||||
| 'resuming'
|
||||
| 'stopping'
|
||||
| 'stopped';
|
||||
|
||||
export interface RecordingProgress {
|
||||
durationMs: number;
|
||||
meteringDb: number | null;
|
||||
}
|
||||
|
||||
export interface RecordedAudio {
|
||||
uri: string;
|
||||
path: string;
|
||||
fileName: string;
|
||||
mimeType: string;
|
||||
size: number;
|
||||
durationMs: number;
|
||||
}
|
||||
|
||||
export type RecordingRuntimeState =
|
||||
| 'idle'
|
||||
| 'starting'
|
||||
| 'recording'
|
||||
| 'paused'
|
||||
| 'stopped'
|
||||
| 'recoverable';
|
||||
|
||||
export interface RecordingRuntimeSnapshot {
|
||||
state: RecordingRuntimeState;
|
||||
recording: RecordedAudio | null;
|
||||
meetingId: string | null;
|
||||
interruptionReason: string | null;
|
||||
startedAtMs: number;
|
||||
}
|
||||
|
||||
export interface StartRecordingOptions {
|
||||
meetingId?: string;
|
||||
onStateChange?: (snapshot: RecordingRuntimeSnapshot) => void;
|
||||
}
|
||||
|
||||
interface NativeRecordingSnapshot {
|
||||
state: RecordingRuntimeState;
|
||||
path: string | null;
|
||||
uri: string | null;
|
||||
fileName: string | null;
|
||||
mimeType: string;
|
||||
size: number;
|
||||
durationMs: number;
|
||||
meteringDb: number | null;
|
||||
meetingId: string | null;
|
||||
interruptionReason: string | null;
|
||||
startedAtMs: number;
|
||||
recoverable: boolean;
|
||||
}
|
||||
|
||||
interface D3RORecordingNativeModule {
|
||||
getStatus: () => Promise<NativeRecordingSnapshot>;
|
||||
start: (options: { meetingId?: string }) => Promise<NativeRecordingSnapshot>;
|
||||
pause: () => Promise<NativeRecordingSnapshot>;
|
||||
resume: () => Promise<NativeRecordingSnapshot>;
|
||||
stop: () => Promise<NativeRecordingSnapshot>;
|
||||
cancel: () => Promise<NativeRecordingSnapshot>;
|
||||
dispose: (path: string) => Promise<void>;
|
||||
}
|
||||
|
||||
function androidRecordingModule(): D3RORecordingNativeModule {
|
||||
const candidate = NativeModules.D3RORecording as
|
||||
| Partial<D3RORecordingNativeModule>
|
||||
| undefined;
|
||||
if (
|
||||
candidate === undefined ||
|
||||
typeof candidate.getStatus !== 'function' ||
|
||||
typeof candidate.start !== 'function' ||
|
||||
typeof candidate.pause !== 'function' ||
|
||||
typeof candidate.resume !== 'function' ||
|
||||
typeof candidate.stop !== 'function' ||
|
||||
typeof candidate.cancel !== 'function' ||
|
||||
typeof candidate.dispose !== 'function'
|
||||
) {
|
||||
throw new Error('Android foreground recording module is unavailable');
|
||||
}
|
||||
return candidate as D3RORecordingNativeModule;
|
||||
}
|
||||
|
||||
function assertFiniteNonNegative(value: unknown, field: string): number {
|
||||
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
|
||||
throw new Error(`Android recorder returned invalid ${field}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function parseNativeSnapshot(value: NativeRecordingSnapshot): RecordingRuntimeSnapshot {
|
||||
const states: readonly RecordingRuntimeState[] = [
|
||||
'idle',
|
||||
'starting',
|
||||
'recording',
|
||||
'paused',
|
||||
'stopped',
|
||||
'recoverable',
|
||||
];
|
||||
if (!states.includes(value.state)) {
|
||||
throw new Error('Android recorder returned an invalid state');
|
||||
}
|
||||
const durationMs = assertFiniteNonNegative(value.durationMs, 'duration');
|
||||
const size = assertFiniteNonNegative(value.size, 'file size');
|
||||
const startedAtMs = assertFiniteNonNegative(value.startedAtMs, 'start time');
|
||||
const hasFile = value.path !== null || value.uri !== null || value.fileName !== null;
|
||||
if (
|
||||
hasFile &&
|
||||
(typeof value.path !== 'string' ||
|
||||
value.path.length === 0 ||
|
||||
value.uri !== `file://${value.path}` ||
|
||||
typeof value.fileName !== 'string' ||
|
||||
!value.fileName.endsWith('.wav') ||
|
||||
value.mimeType !== ANDROID_RECORDING_MIME_TYPE)
|
||||
) {
|
||||
throw new Error('Android recorder returned an invalid recording file');
|
||||
}
|
||||
if (value.state !== 'idle' && !hasFile) {
|
||||
throw new Error('Android recorder state has no recording file');
|
||||
}
|
||||
const recording = hasFile
|
||||
? {
|
||||
uri: value.uri as string,
|
||||
path: value.path as string,
|
||||
fileName: value.fileName as string,
|
||||
mimeType: ANDROID_RECORDING_MIME_TYPE,
|
||||
size,
|
||||
durationMs,
|
||||
}
|
||||
: null;
|
||||
return {
|
||||
state: value.state,
|
||||
recording,
|
||||
meetingId: typeof value.meetingId === 'string' ? value.meetingId : null,
|
||||
interruptionReason:
|
||||
typeof value.interruptionReason === 'string'
|
||||
? value.interruptionReason
|
||||
: null,
|
||||
startedAtMs,
|
||||
};
|
||||
}
|
||||
|
||||
function createTemporaryPath(): string {
|
||||
return `${Dirs.CacheDir}/${RECORDING_PREFIX}${createUuidV4()}${RECORDING_EXTENSION}`;
|
||||
}
|
||||
|
||||
function pathFromFileUri(uri: string): string | null {
|
||||
if (!uri.startsWith('file://')) return null;
|
||||
|
||||
try {
|
||||
return decodeURIComponent(uri.slice('file://'.length));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function isOwnedTemporaryPath(path: string): boolean {
|
||||
const cachePrefix = `${Dirs.CacheDir}/`;
|
||||
if (!path.startsWith(cachePrefix)) return false;
|
||||
|
||||
const fileName = path.slice(cachePrefix.length);
|
||||
return (
|
||||
!fileName.includes('/') &&
|
||||
!fileName.includes('\\') &&
|
||||
fileName.startsWith(RECORDING_PREFIX) &&
|
||||
fileName.endsWith(RECORDING_EXTENSION)
|
||||
);
|
||||
}
|
||||
|
||||
async function deleteOwnedTemporaryFile(path: string | null): Promise<void> {
|
||||
if (path === null) return;
|
||||
if (!isOwnedTemporaryPath(path)) {
|
||||
throw new Error('Refusing to delete a recording outside the app cache');
|
||||
}
|
||||
|
||||
if (await FileSystem.exists(path)) {
|
||||
await FileSystem.unlink(path);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteStaleTemporaryFiles(): Promise<void> {
|
||||
const entries = await FileSystem.ls(Dirs.CacheDir);
|
||||
const stalePaths = entries
|
||||
.filter(
|
||||
entry =>
|
||||
entry.startsWith(RECORDING_PREFIX) &&
|
||||
entry.endsWith(RECORDING_EXTENSION),
|
||||
)
|
||||
.map(entry => `${Dirs.CacheDir}/${entry}`);
|
||||
|
||||
await Promise.all(stalePaths.map(deleteOwnedTemporaryFile));
|
||||
}
|
||||
|
||||
export class AudioRecorder {
|
||||
private state: NativeRecorderState = 'idle';
|
||||
private currentPath: string | null = null;
|
||||
private progress: RecordingProgress = { durationMs: 0, meteringDb: null };
|
||||
private pendingStart: Promise<void> | null = null;
|
||||
private pendingTransition: Promise<void> | null = null;
|
||||
private pendingStop: Promise<RecordedAudio> | null = null;
|
||||
private sound: ReturnType<typeof createSound> | null = null;
|
||||
private progressHandler: ((progress: RecordingProgress) => void) | null =
|
||||
null;
|
||||
private stateHandler: ((snapshot: RecordingRuntimeSnapshot) => void) | null =
|
||||
null;
|
||||
private androidPollGeneration = 0;
|
||||
|
||||
private getSound(): ReturnType<typeof createSound> {
|
||||
if (this.sound !== null) return this.sound;
|
||||
|
||||
const sound = createSound();
|
||||
sound.setSubscriptionDuration(0.1);
|
||||
sound.addRecordBackListener((event: RecordBackType) => {
|
||||
if (this.progressHandler === null) return;
|
||||
|
||||
const durationMs = Number.isFinite(event.currentPosition)
|
||||
? Math.max(0, Math.round(event.currentPosition))
|
||||
: this.progress.durationMs;
|
||||
const meteringDb =
|
||||
typeof event.currentMetering === 'number' &&
|
||||
Number.isFinite(event.currentMetering)
|
||||
? event.currentMetering
|
||||
: null;
|
||||
|
||||
this.progress = { durationMs, meteringDb };
|
||||
this.progressHandler(this.progress);
|
||||
});
|
||||
this.sound = sound;
|
||||
return sound;
|
||||
}
|
||||
|
||||
async requestPermission(): Promise<boolean> {
|
||||
if (Platform.OS !== 'android') {
|
||||
// Nitro Sound requests iOS microphone access through AVAudioSession.
|
||||
return true;
|
||||
}
|
||||
|
||||
const permission = PermissionsAndroid.PERMISSIONS.RECORD_AUDIO;
|
||||
if (await PermissionsAndroid.check(permission)) return true;
|
||||
|
||||
const result = await PermissionsAndroid.request(permission, {
|
||||
title: 'Microphone access',
|
||||
message:
|
||||
'D3RO Voice needs microphone access to record and transcribe audio.',
|
||||
buttonPositive: 'Allow',
|
||||
buttonNegative: 'Cancel',
|
||||
});
|
||||
|
||||
return result === PermissionsAndroid.RESULTS.GRANTED;
|
||||
}
|
||||
|
||||
async start(
|
||||
onProgress: (progress: RecordingProgress) => void,
|
||||
options: StartRecordingOptions = {},
|
||||
): Promise<void> {
|
||||
if (this.state !== 'idle') {
|
||||
throw new Error(`Cannot start recorder while it is ${this.state}`);
|
||||
}
|
||||
|
||||
this.state = 'starting';
|
||||
if (Platform.OS === 'android') {
|
||||
this.progressHandler = onProgress;
|
||||
this.stateHandler = options.onStateChange ?? null;
|
||||
try {
|
||||
const snapshot = parseNativeSnapshot(
|
||||
await androidRecordingModule().start(
|
||||
options.meetingId === undefined
|
||||
? {}
|
||||
: { meetingId: options.meetingId },
|
||||
),
|
||||
);
|
||||
if (snapshot.state !== 'recording' || snapshot.recording === null) {
|
||||
throw new Error('Android foreground recorder did not start');
|
||||
}
|
||||
this.currentPath = snapshot.recording.path;
|
||||
this.progress = {
|
||||
durationMs: snapshot.recording.durationMs,
|
||||
meteringDb: null,
|
||||
};
|
||||
this.state = 'recording';
|
||||
this.stateHandler?.(snapshot);
|
||||
this.startAndroidPolling();
|
||||
return;
|
||||
} catch (error) {
|
||||
this.progressHandler = null;
|
||||
this.stateHandler = null;
|
||||
this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
try {
|
||||
await deleteStaleTemporaryFiles();
|
||||
} catch (error) {
|
||||
this.state = 'idle';
|
||||
throw error;
|
||||
}
|
||||
|
||||
const outputPath = createTemporaryPath();
|
||||
this.currentPath = outputPath;
|
||||
this.progress = { durationMs: 0, meteringDb: null };
|
||||
this.progressHandler = onProgress;
|
||||
const sound = this.getSound();
|
||||
|
||||
const pendingStart = (async () => {
|
||||
try {
|
||||
const startedUri = await sound.startRecorder(
|
||||
outputPath,
|
||||
AUDIO_SETTINGS,
|
||||
true,
|
||||
);
|
||||
const startedPath = pathFromFileUri(startedUri);
|
||||
|
||||
if (startedPath !== outputPath) {
|
||||
throw new Error('Recorder returned an unexpected output URI');
|
||||
}
|
||||
|
||||
this.state = 'recording';
|
||||
} catch (error) {
|
||||
try {
|
||||
await sound.stopRecorder();
|
||||
} catch {
|
||||
// stopRecorder also releases Android's MediaRecorder when stopping fails.
|
||||
}
|
||||
this.progressHandler = null;
|
||||
if (this.currentPath === outputPath) this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
await deleteOwnedTemporaryFile(outputPath);
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
|
||||
this.pendingStart = pendingStart;
|
||||
try {
|
||||
await pendingStart;
|
||||
} finally {
|
||||
if (this.pendingStart === pendingStart) this.pendingStart = null;
|
||||
}
|
||||
}
|
||||
|
||||
async pause(): Promise<void> {
|
||||
if (this.state !== 'recording') {
|
||||
throw new Error(`Cannot pause recorder while it is ${this.state}`);
|
||||
}
|
||||
|
||||
this.state = 'pausing';
|
||||
if (Platform.OS === 'android') {
|
||||
try {
|
||||
const snapshot = parseNativeSnapshot(await androidRecordingModule().pause());
|
||||
if (snapshot.state !== 'paused') throw new Error('Android recorder did not pause');
|
||||
this.state = 'paused';
|
||||
this.stateHandler?.(snapshot);
|
||||
return;
|
||||
} catch (error) {
|
||||
this.state = 'recording';
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const pendingTransition = (async () => {
|
||||
try {
|
||||
await this.getSound().pauseRecorder();
|
||||
this.state = 'paused';
|
||||
} catch (error) {
|
||||
this.state = 'recording';
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
|
||||
this.pendingTransition = pendingTransition;
|
||||
try {
|
||||
await pendingTransition;
|
||||
} finally {
|
||||
if (this.pendingTransition === pendingTransition) {
|
||||
this.pendingTransition = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async resume(): Promise<void> {
|
||||
if (this.state !== 'paused') {
|
||||
throw new Error(`Cannot resume recorder while it is ${this.state}`);
|
||||
}
|
||||
|
||||
this.state = 'resuming';
|
||||
if (Platform.OS === 'android') {
|
||||
try {
|
||||
const snapshot = parseNativeSnapshot(await androidRecordingModule().resume());
|
||||
if (snapshot.state !== 'recording') throw new Error('Android recorder did not resume');
|
||||
this.state = 'recording';
|
||||
this.stateHandler?.(snapshot);
|
||||
return;
|
||||
} catch (error) {
|
||||
this.state = 'paused';
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
const pendingTransition = (async () => {
|
||||
try {
|
||||
await this.getSound().resumeRecorder();
|
||||
this.state = 'recording';
|
||||
} catch (error) {
|
||||
this.state = 'paused';
|
||||
throw error;
|
||||
}
|
||||
})();
|
||||
|
||||
this.pendingTransition = pendingTransition;
|
||||
try {
|
||||
await pendingTransition;
|
||||
} finally {
|
||||
if (this.pendingTransition === pendingTransition) {
|
||||
this.pendingTransition = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async stop(): Promise<RecordedAudio> {
|
||||
if (this.state !== 'recording' && this.state !== 'paused') {
|
||||
throw new Error(`Cannot stop recorder while it is ${this.state}`);
|
||||
}
|
||||
|
||||
const outputPath = this.currentPath;
|
||||
if (outputPath === null) throw new Error('Recorder has no output path');
|
||||
|
||||
const pendingStop = (async (): Promise<RecordedAudio> => {
|
||||
this.state = 'stopping';
|
||||
|
||||
try {
|
||||
if (Platform.OS === 'android') {
|
||||
const snapshot = parseNativeSnapshot(await androidRecordingModule().stop());
|
||||
if (
|
||||
(snapshot.state !== 'stopped' && snapshot.state !== 'recoverable') ||
|
||||
snapshot.recording === null ||
|
||||
snapshot.recording.size <= 44
|
||||
) {
|
||||
throw new Error('Android recorder produced no recoverable audio');
|
||||
}
|
||||
this.stopAndroidPolling();
|
||||
this.currentPath = snapshot.recording.path;
|
||||
this.progress = {
|
||||
durationMs: snapshot.recording.durationMs,
|
||||
meteringDb: null,
|
||||
};
|
||||
this.state = 'stopped';
|
||||
this.stateHandler?.(snapshot);
|
||||
return snapshot.recording;
|
||||
}
|
||||
const stoppedUri = await this.getSound().stopRecorder();
|
||||
const stoppedPath = pathFromFileUri(stoppedUri);
|
||||
|
||||
if (stoppedPath !== outputPath) {
|
||||
throw new Error('Recorder did not return the expected output file');
|
||||
}
|
||||
|
||||
const stat = await FileSystem.stat(outputPath);
|
||||
if (stat.type !== 'file' || stat.size <= 0) {
|
||||
throw new Error('Recorder produced an empty audio file');
|
||||
}
|
||||
|
||||
this.state = 'stopped';
|
||||
return {
|
||||
uri: stoppedUri,
|
||||
path: outputPath,
|
||||
fileName: outputPath.slice(outputPath.lastIndexOf('/') + 1),
|
||||
mimeType: RECORDING_MIME_TYPE,
|
||||
size: stat.size,
|
||||
durationMs: this.progress.durationMs,
|
||||
};
|
||||
} catch (error) {
|
||||
this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
await deleteOwnedTemporaryFile(outputPath);
|
||||
throw error;
|
||||
} finally {
|
||||
this.progressHandler = null;
|
||||
this.stateHandler = null;
|
||||
}
|
||||
})();
|
||||
|
||||
this.pendingStop = pendingStop;
|
||||
try {
|
||||
return await pendingStop;
|
||||
} finally {
|
||||
if (this.pendingStop === pendingStop) this.pendingStop = null;
|
||||
}
|
||||
}
|
||||
|
||||
async cleanup(recording?: Pick<RecordedAudio, 'path'>): Promise<void> {
|
||||
const path = recording?.path ?? this.currentPath;
|
||||
if (Platform.OS === 'android') {
|
||||
if (path !== null) await androidRecordingModule().dispose(path);
|
||||
this.stopAndroidPolling();
|
||||
this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
this.progress = { durationMs: 0, meteringDb: null };
|
||||
this.progressHandler = null;
|
||||
this.stateHandler = null;
|
||||
return;
|
||||
}
|
||||
await deleteOwnedTemporaryFile(path);
|
||||
|
||||
if (path === this.currentPath) {
|
||||
this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
this.progress = { durationMs: 0, meteringDb: null };
|
||||
}
|
||||
}
|
||||
|
||||
async cancel(): Promise<void> {
|
||||
if (Platform.OS === 'android') {
|
||||
this.stopAndroidPolling();
|
||||
await androidRecordingModule().cancel();
|
||||
this.progressHandler = null;
|
||||
this.stateHandler = null;
|
||||
this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
this.progress = { durationMs: 0, meteringDb: null };
|
||||
return;
|
||||
}
|
||||
if (this.state === 'starting' && this.pendingStart !== null) {
|
||||
try {
|
||||
await this.pendingStart;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.state === 'stopping' && this.pendingStop !== null) {
|
||||
try {
|
||||
await this.pendingStop;
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.pendingTransition !== null) {
|
||||
try {
|
||||
await this.pendingTransition;
|
||||
} catch {
|
||||
// Continue cleanup from the stable pre-transition recording state.
|
||||
}
|
||||
}
|
||||
|
||||
const outputPath = this.currentPath;
|
||||
let stopError: unknown = null;
|
||||
|
||||
try {
|
||||
if (this.state === 'recording' || this.state === 'paused') {
|
||||
await this.getSound().stopRecorder();
|
||||
}
|
||||
} catch (error) {
|
||||
stopError = error;
|
||||
} finally {
|
||||
this.progressHandler = null;
|
||||
this.currentPath = null;
|
||||
this.state = 'idle';
|
||||
this.progress = { durationMs: 0, meteringDb: null };
|
||||
await deleteOwnedTemporaryFile(outputPath);
|
||||
}
|
||||
|
||||
if (stopError !== null) throw stopError;
|
||||
}
|
||||
|
||||
async restore(
|
||||
onProgress?: (progress: RecordingProgress) => void,
|
||||
onStateChange?: (snapshot: RecordingRuntimeSnapshot) => void,
|
||||
): Promise<RecordingRuntimeSnapshot> {
|
||||
if (Platform.OS !== 'android') {
|
||||
return {
|
||||
state: 'idle',
|
||||
recording: null,
|
||||
meetingId: null,
|
||||
interruptionReason: null,
|
||||
startedAtMs: 0,
|
||||
};
|
||||
}
|
||||
const snapshot = parseNativeSnapshot(await androidRecordingModule().getStatus());
|
||||
this.stopAndroidPolling();
|
||||
this.progressHandler = onProgress ?? null;
|
||||
this.stateHandler = onStateChange ?? null;
|
||||
this.currentPath = snapshot.recording?.path ?? null;
|
||||
this.progress = {
|
||||
durationMs: snapshot.recording?.durationMs ?? 0,
|
||||
meteringDb: null,
|
||||
};
|
||||
if (snapshot.state === 'recording' || snapshot.state === 'paused') {
|
||||
this.state = snapshot.state;
|
||||
this.startAndroidPolling();
|
||||
} else if (snapshot.state === 'stopped' || snapshot.state === 'recoverable') {
|
||||
this.state = 'stopped';
|
||||
} else {
|
||||
this.state = 'idle';
|
||||
}
|
||||
this.progressHandler?.(this.progress);
|
||||
this.stateHandler?.(snapshot);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
private startAndroidPolling(): void {
|
||||
const generation = ++this.androidPollGeneration;
|
||||
const poll = async (): Promise<void> => {
|
||||
if (generation !== this.androidPollGeneration) return;
|
||||
try {
|
||||
const native = await androidRecordingModule().getStatus();
|
||||
if (generation !== this.androidPollGeneration) return;
|
||||
const snapshot = parseNativeSnapshot(native);
|
||||
const nextProgress = {
|
||||
durationMs: snapshot.recording?.durationMs ?? 0,
|
||||
meteringDb:
|
||||
typeof native.meteringDb === 'number' && Number.isFinite(native.meteringDb)
|
||||
? native.meteringDb
|
||||
: null,
|
||||
};
|
||||
this.progress = nextProgress;
|
||||
this.progressHandler?.(nextProgress);
|
||||
this.stateHandler?.(snapshot);
|
||||
if (snapshot.state === 'recording' || snapshot.state === 'paused') {
|
||||
this.state = snapshot.state;
|
||||
setTimeout(() => { void poll(); }, 250);
|
||||
} else {
|
||||
this.currentPath = snapshot.recording?.path ?? null;
|
||||
this.state = snapshot.state === 'idle' ? 'idle' : 'stopped';
|
||||
}
|
||||
} catch {
|
||||
// A transient React bridge failure must not stop the native foreground capture.
|
||||
if (generation === this.androidPollGeneration) {
|
||||
setTimeout(() => { void poll(); }, 500);
|
||||
}
|
||||
}
|
||||
};
|
||||
void poll();
|
||||
}
|
||||
|
||||
private stopAndroidPolling(): void {
|
||||
this.androidPollGeneration += 1;
|
||||
}
|
||||
}
|
||||
|
||||
export const audioRecorder = new AudioRecorder();
|
||||
Loading…
Add table
Add a link
Reference in a new issue