diff --git a/packages/react-user-media/src/hooks/index.ts b/packages/react-user-media/src/hooks/index.ts index 08a580d..dce09db 100644 --- a/packages/react-user-media/src/hooks/index.ts +++ b/packages/react-user-media/src/hooks/index.ts @@ -3,3 +3,6 @@ export * from "./use-media-devices"; export * from "./use-media-devices-ext"; export * from "./use-media-ext"; export * from "./use-media-recorder"; + +// Worker-based hooks +export * from "../workers"; diff --git a/packages/react-user-media/src/index.ts b/packages/react-user-media/src/index.ts index 9562774..f49331d 100644 --- a/packages/react-user-media/src/index.ts +++ b/packages/react-user-media/src/index.ts @@ -1,5 +1,6 @@ export * from "./components"; export * from "./hooks"; +export * from "./workers"; export function getSupportedConstraints() { return navigator.mediaDevices.getSupportedConstraints(); diff --git a/packages/react-user-media/src/workers/README.md b/packages/react-user-media/src/workers/README.md new file mode 100644 index 0000000..182a5a1 --- /dev/null +++ b/packages/react-user-media/src/workers/README.md @@ -0,0 +1,226 @@ +# Media Workers + +This module provides worker-based abstractions for media operations, allowing for testing without the full web worker stack while maintaining production compatibility. + +## Overview + +The media workers module consists of: + +- **Types**: Core interfaces and type definitions +- **Mock Worker**: Test-friendly implementation that doesn't require actual web workers +- **Web Worker**: Production implementation using actual web workers +- **Factory**: Management and creation of worker instances +- **Hooks**: React hooks for easy integration + +## Key Features + +- **Testable**: Mock implementation for unit testing without web workers +- **Production Ready**: Real web worker implementation for production use +- **Type Safe**: Full TypeScript support with comprehensive type definitions +- **React Integration**: Custom hooks for seamless React integration +- **Flexible**: Configurable worker behavior and lifecycle management + +## Basic Usage + +### Using the Media Worker Hook + +```tsx +import { useMediaWorker } from '@react-user-media/workers'; + +function MyComponent() { + const mediaWorker = useMediaWorker({ + useMockWorker: process.env.NODE_ENV === 'test', // Use mock in tests + autoInitialize: true, + }); + + const handleStartRecording = async () => { + try { + await mediaWorker.startRecording({ + mimeType: 'video/webm', + videoBitsPerSecond: 2500000, + }); + } catch (error) { + console.error('Failed to start recording:', error); + } + }; + + return ( +
Initialized: {mediaWorker.isInitialized ? 'Yes' : 'No'}
+Recording: {mediaWorker.isRecording ? 'Yes' : 'No'}
+ +Duration: {recorder.getDuration() ? Math.round(recorder.getDuration()! / 1000) : 0}s
+Finalized: {recorder.isFinalized ? 'Yes' : 'No'}
+ +Initialized: {mediaWorker.isInitialized ? 'Yes' : 'No'}
+Recording: {recorder.isRecording ? 'Yes' : 'No'}
+Processing: {mediaWorker.isProcessing ? 'Yes' : 'No'}
+Error: {mediaWorker.error || 'None'}
+ {recorder.getDuration() && ( +Duration: {Math.round(recorder.getDuration()! / 1000)}s
+ )} +Loading devices...
+ ) : ( +Count: {segments.length}
+ {segments.length > 0 && ( +
+ {JSON.stringify(mediaWorker.workerState, null, 2)}
+
+ Status: {status}
+Initialized: {mediaWorker.isInitialized ? 'Yes' : 'No'}
+Recording: {recorder.isRecording ? 'Yes' : 'No'}
+Error: {mediaWorker.error || 'None'}
+
+ {JSON.stringify(mediaWorker.workerState, null, 2)}
+
+ Status: {status}
+Initialized: {mediaWorker.isInitialized ? 'Yes' : 'No'}
+Processing: {mediaWorker.isProcessing ? 'Yes' : 'No'}
+Error: {mediaWorker.error || 'None'}
+Codec Capabilities: {mediaWorker.codecCapabilities.length}
+Size: {processedFrame.byteLength} bytes
+