|
| 1 | +# Video Capture |
| 2 | + |
| 3 | +The `VideoCapture` class represents a controllable video capture device and coordinates device selection, capability negotiation, frame delivery to a sink, and lifecycle/resource management. |
| 4 | + |
| 5 | +API: `dev.onvoid.webrtc.media.video.VideoCapture` |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +- Purpose: Control a physical or virtual video input device and deliver frames to a sink. |
| 10 | +- Typical workflow: set device ➜ set capability ➜ set sink ➜ start ➜ stop ➜ dispose. |
| 11 | +- Resource management: Always call `dispose()` to release native resources. |
| 12 | +- Threading: Methods are not guaranteed to be thread‑safe; synchronize externally if accessed from multiple threads. |
| 13 | + |
| 14 | +Key methods: |
| 15 | +- `void setVideoCaptureDevice(VideoDevice device)` – select the camera/device to bind. |
| 16 | +- `void setVideoCaptureCapability(VideoCaptureCapability capability)` – configure desired resolution, frame rate, pixel format, etc. |
| 17 | +- `void setVideoSink(VideoTrackSink sink)` – register/replace the sink that will receive frames. |
| 18 | +- `void start()` – begin asynchronous capture and frame delivery. |
| 19 | +- `void stop()` – stop capture (idempotent). |
| 20 | +- `void dispose()` – release native resources; implicitly stops if running. |
| 21 | + |
| 22 | +Related types: |
| 23 | +- `VideoDevice` – describes a capturable device (enumerate via your platform/device utilities). |
| 24 | +- `VideoCaptureCapability` – resolution, FPS, and pixel format preferences. |
| 25 | +- `VideoTrackSink` – consumer that receives `VideoFrame` callbacks. |
| 26 | + |
| 27 | +## Typical usage |
| 28 | + |
| 29 | +```java |
| 30 | +import dev.onvoid.webrtc.media.video.*; |
| 31 | + |
| 32 | +// 1) Create capture |
| 33 | +VideoCapture capture = new VideoCapture(); |
| 34 | + |
| 35 | +try { |
| 36 | + // 2) Select a device (obtained from your device enumeration logic) |
| 37 | + VideoDevice device = /* obtain a VideoDevice */; |
| 38 | + capture.setVideoCaptureDevice(device); |
| 39 | + |
| 40 | + // 3) Configure capability |
| 41 | + VideoCaptureCapability cap = new VideoCaptureCapability(); |
| 42 | + cap.width = 1280; |
| 43 | + cap.height = 720; |
| 44 | + cap.maxFPS = 30; |
| 45 | + // cap.pixelFormat = ... // if applicable in your build |
| 46 | + capture.setVideoCaptureCapability(cap); |
| 47 | + |
| 48 | + // 4) Provide a sink to receive frames |
| 49 | + VideoTrackSink sink = frame -> { |
| 50 | + // Consume VideoFrame |
| 51 | + // e.g., render, encode, or forward to a WebRTC VideoSource/Track |
| 52 | + }; |
| 53 | + capture.setVideoSink(sink); |
| 54 | + |
| 55 | + // 5) Start capture |
| 56 | + capture.start(); |
| 57 | + |
| 58 | + // ... capture is running, frames delivered to sink ... |
| 59 | + |
| 60 | + // 6) Stop capture |
| 61 | + capture.stop(); |
| 62 | +} |
| 63 | +finally { |
| 64 | + // 7) Cleanup |
| 65 | + capture.dispose(); |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Notes: |
| 70 | +- Call `setVideoCaptureDevice` and `setVideoCaptureCapability` before `start()`. |
| 71 | +- You may call `setVideoSink` before or after `start()`; passing `null` detaches the sink (if supported), dropping frames until a new sink is set. |
| 72 | +- Some capability values may be negotiated to the nearest supported values by the underlying platform. |
| 73 | + |
| 74 | +## Integration with WebRTC tracks |
| 75 | + |
| 76 | +While `VideoCapture` handles raw capture, most WebRTC pipelines use a `VideoSource`/`VideoTrack` abstraction. If you need to feed frames into a `VideoTrack`, use or implement a sink that forwards frames to your `VideoSource` (or use a higher‑level helper provided by this project/examples). |
| 77 | + |
| 78 | +Example sketch: |
| 79 | + |
| 80 | +```java |
| 81 | +VideoTrackSink sink = frame -> { |
| 82 | + // Convert/forward frame into your WebRTC VideoSource or renderer |
| 83 | +}; |
| 84 | + |
| 85 | +capture.setVideoSink(sink); |
| 86 | +capture.start(); |
| 87 | +``` |
| 88 | + |
| 89 | +If you need pixel format conversion, see `VideoBufferConverter`. |
| 90 | + |
| 91 | +## Error handling and lifecycle |
| 92 | + |
| 93 | +- Constructor: Initializes native resources; failures may surface as runtime exceptions from native code. |
| 94 | +- `IllegalStateException`: Thrown if methods are invoked after disposal or when prerequisites are missing. |
| 95 | +- Idempotency: `start()` and `stop()` are intended to be safe to call multiple times (implementation‑dependent no‑op if already in that state). |
| 96 | +- Always call `dispose()` in a `finally` block to avoid leaking native resources. |
| 97 | + |
| 98 | +## Tips |
| 99 | + |
| 100 | +- Device changes (plug/unplug cameras) may require re‑enumeration and re‑selection via `setVideoCaptureDevice`. |
| 101 | +- If frames need scaling or color conversion, use `VideoBufferConverter` before feeding frames to encoders or renderers. |
| 102 | +- Synchronize access if multiple threads change device, capability, or sink while capturing. |
| 103 | + |
| 104 | +## Related guides |
| 105 | + |
| 106 | +- [Camera Capture](guide/camera_capture.md) |
| 107 | +- [Custom Video Source](guide/custom_video_source.md) |
| 108 | +- [Screen Capturer](guide/screen_capturer.md) |
| 109 | +- [Window Capturer](guide/window_capturer.md) |
| 110 | +- [Power Management](guide/power_management.md) |
0 commit comments