Skip to content

Commit e273065

Browse files
committed
docs: documentation for MediaStreamTrack, VideoBufferConverter, and VideoCapture
1 parent 0643be2 commit e273065

File tree

3 files changed

+164
-7
lines changed

3 files changed

+164
-7
lines changed

webrtc/src/main/java/dev/onvoid/webrtc/media/MediaStreamTrack.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
/**
2222
* The MediaStreamTrack represents media of a single type that originates from
23-
* one media source, e.g. video produced by a web camera.
23+
* one media source, e.g., video produced by a web camera.
2424
*
25-
* @link https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack
25+
* @link <a href="https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack">...</a>
2626
*
2727
* @author Alex Andres
2828
*/
@@ -100,7 +100,7 @@ public void removeTrackEndedListener(MediaStreamTrackEndedListener listener) {
100100

101101
/**
102102
* When a MediaStreamTrack is created, the application must generate an
103-
* identifier string, and must initialize the object's id attribute to that
103+
* identifier string. It must initialize the object's id attribute to that
104104
* string, unless the object is created as part of a special purpose
105105
* algorithm that specifies how the stream id must be initialized.
106106
*
@@ -119,7 +119,7 @@ public void removeTrackEndedListener(MediaStreamTrackEndedListener listener) {
119119
* Controls the enabled state for the media track. A disabled track will
120120
* produce silence (if audio) or black frames (if video). After a
121121
* MediaStreamTrack has ended, its enabled attribute still changes value
122-
* when set; it just doesn't do anything wit that new value.
122+
* when set; it just doesn't do anything with that new value.
123123
*
124124
* @param enable The new value.
125125
*/

webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoBufferConverter.java

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,33 @@
1616

1717
package dev.onvoid.webrtc.media.video;
1818

19-
import dev.onvoid.webrtc.media.FourCC;
20-
2119
import java.nio.ByteBuffer;
2220

21+
import dev.onvoid.webrtc.media.FourCC;
22+
23+
/**
24+
* Utility methods to convert between I420 video frame buffers and other pixel
25+
* formats represented by a FourCC. Delegates actual conversion work to native
26+
* methods for performance.
27+
*
28+
* @author Alex Andres
29+
*/
2330
public final class VideoBufferConverter {
2431

32+
/**
33+
* Convert an arbitrary {@link VideoFrameBuffer} to the specified pixel format
34+
* (given by {@code fourCC}) and write the result into a destination byte array.
35+
* <p>
36+
* The source buffer is first converted (if necessary) to an intermediate
37+
* I420 layout via {@link VideoFrameBuffer#toI420()} and then transformed.
38+
*
39+
* @param src Source video frame buffer (will be converted to I420 if not already).
40+
* @param dst Destination byte array expected to be large enough to hold the converted frame.
41+
* @param fourCC Target FourCC format identifier.
42+
*
43+
* @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
44+
* @throws Exception if the native conversion fails.
45+
*/
2546
public static void convertFromI420(VideoFrameBuffer src, byte[] dst, FourCC fourCC) throws Exception {
2647
if (src == null) {
2748
throw new NullPointerException("Source buffer must not be null");
@@ -40,7 +61,21 @@ public static void convertFromI420(VideoFrameBuffer src, byte[] dst, FourCC four
4061
i420.getWidth(), i420.getHeight(),
4162
fourCC.value());
4263
}
43-
64+
65+
/**
66+
* Convert an arbitrary {@link VideoFrameBuffer} to the specified pixel format
67+
* and write the result into a {@link ByteBuffer}. If the destination buffer
68+
* is a direct buffer, a native direct path is used; otherwise its backing
69+
* array (or a temporary array) is employed.
70+
*
71+
* @param src Source video frame buffer.
72+
* @param dst Writable destination {@link ByteBuffer}. Must not be read-only.
73+
* @param fourCC Target FourCC format identifier.
74+
*
75+
* @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
76+
* @throws IllegalArgumentException if {@code dst} is read-only.
77+
* @throws Exception if the native conversion fails.
78+
*/
4479
public static void convertFromI420(VideoFrameBuffer src, ByteBuffer dst, FourCC fourCC) throws Exception {
4580
if (src == null) {
4681
throw new NullPointerException("Source buffer must not be null");
@@ -84,6 +119,17 @@ public static void convertFromI420(VideoFrameBuffer src, ByteBuffer dst, FourCC
84119
}
85120
}
86121

122+
/**
123+
* Convert a source frame stored in a byte array (encoded in the format indicated
124+
* by {@code fourCC}) to I420 and write the planes into the provided {@link I420Buffer}.
125+
*
126+
* @param src Source pixel data in the specified FourCC format.
127+
* @param dst Destination I420 buffer (pre-allocated).
128+
* @param fourCC FourCC describing the layout of {@code src}.
129+
*
130+
* @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
131+
* @throws Exception if the native conversion fails.
132+
*/
87133
public static void convertToI420(byte[] src, I420Buffer dst, FourCC fourCC) throws Exception {
88134
if (src == null) {
89135
throw new NullPointerException("Source buffer must not be null");
@@ -101,6 +147,19 @@ public static void convertToI420(byte[] src, I420Buffer dst, FourCC fourCC) thro
101147
fourCC.value());
102148
}
103149

150+
/**
151+
* Convert a source frame stored in a {@link ByteBuffer} (encoded in the format
152+
* specified by {@code fourCC}) to I420 and write the result into the provided
153+
* {@link I420Buffer}. Uses a direct native path for direct buffers; otherwise
154+
* reads from the backing or a temporary array.
155+
*
156+
* @param src Source pixel data buffer.
157+
* @param dst Destination I420 buffer (pre-allocated).
158+
* @param fourCC FourCC describing the layout of {@code src}.
159+
*
160+
* @throws NullPointerException if {@code src} or {@code dst} is {@code null}.
161+
* @throws Exception if the native conversion fails.
162+
*/
104163
public static void convertToI420(ByteBuffer src, I420Buffer dst, FourCC fourCC) throws Exception {
105164
if (src == null) {
106165
throw new NullPointerException("Source buffer must not be null");

webrtc/src/main/java/dev/onvoid/webrtc/media/video/VideoCapture.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,122 @@
1818

1919
import dev.onvoid.webrtc.internal.NativeObject;
2020

21+
/**
22+
* A class representing a video capture device that can be controlled to capture video.
23+
* Extends NativeObject to interact with native code implementations.
24+
* <pre>
25+
* VideoCapture capture = new VideoCapture();
26+
* capture.setVideoCaptureDevice(device);
27+
* capture.setVideoCaptureCapability(capability);
28+
* capture.setVideoSink(sink);
29+
* capture.start();
30+
* // ... consume frames via sink ...
31+
* capture.stop();
32+
* capture.dispose();
33+
* </pre>
34+
* Notes:
35+
* <li>Always call dispose() to release native resources.</li>
36+
* <li>Methods are not guaranteed to be thread-safe unless externally synchronized.</li>
37+
*
38+
* @author Alex Andres
39+
*/
2140
public class VideoCapture extends NativeObject {
2241

42+
/**
43+
* Constructs a new VideoCapture and initializes underlying native resources.
44+
* Initialization failures are typically surfaced as runtime exceptions
45+
* originating from native code.
46+
*/
2347
public VideoCapture() {
2448
initialize();
2549
}
2650

51+
/**
52+
* Selects the physical (or virtual) video input device to capture from.
53+
* <p>
54+
* Constraints / Lifecycle:
55+
* <li>Must be invoked before {@link #start()}.</li>
56+
* <li>Re-setting the device after capture has started may require an
57+
* internal restart (implementation-dependent).</li>
58+
*
59+
* @param device Non-null device descriptor to bind. Passing {@code null}
60+
* is invalid and may raise a {@link NullPointerException}.
61+
*
62+
* @throws IllegalStateException if called after disposal.
63+
*/
2764
public native void setVideoCaptureDevice(VideoDevice device);
2865

66+
/**
67+
* Defines desired capture parameters (resolution, frame rate, pixel format, etc.).
68+
* <p>
69+
* Constraints:
70+
* <li>Should be called after selecting the device and before {@link #start()}.</li>
71+
* <li>Some capabilities may be adjusted (e.g., negotiated to the nearest supported values).</li>
72+
*
73+
* @param capability Desired capture capability (must be non-null).
74+
*
75+
* @throws IllegalArgumentException if unsupported or invalid.
76+
* @throws IllegalStateException if called after disposal.
77+
*/
2978
public native void setVideoCaptureCapability(VideoCaptureCapability capability);
3079

80+
/**
81+
* Registers (or replaces) the sink that will receive decoded/raw video frames.
82+
* <p>
83+
* Behavior:
84+
* <li>May be called before or after {@link #start()}.</li>
85+
* <li>Passing {@code null} (if supported) detaches the current sink and
86+
* frames will be dropped until a new sink is set.</li>
87+
*
88+
* @param sink The consumer of captured frames.
89+
*
90+
* @throws IllegalStateException if called after disposal.
91+
*/
3192
public native void setVideoSink(VideoTrackSink sink);
3293

94+
/**
95+
* Begins asynchronous frame capture and delivery to the configured sink.
96+
* <p>
97+
* Idempotency:
98+
* Calling start() while already started should be a no-op (implementation-dependent).
99+
*
100+
* @throws IllegalStateException if prerequisites (device/capability) are missing
101+
* or the instance is disposed.
102+
*/
33103
public native void start();
34104

105+
/**
106+
* Stops frame capture.
107+
* <p>
108+
* Behavior:
109+
* <li>Drains or discards in-flight frames (implementation-dependent).</li>
110+
* <li>Safe to call multiple times (idempotent).</li>
111+
*
112+
* @throws IllegalStateException if the instance is disposed.
113+
*/
35114
public native void stop();
36115

116+
/**
117+
* Releases native resources associated with this capture instance.
118+
* <p>
119+
* Lifecycle:
120+
* <li>Implicitly stops capture if currently running.</li>
121+
* <li>After disposal, further method calls (other than additional dispose attempts)
122+
* are invalid and may throw {@link IllegalStateException}.</li>
123+
* <p>
124+
* Best Practice:
125+
* Always invoke in a finally block or use a higher-level resource management
126+
* construct to avoid native leaks.
127+
*/
37128
public native void dispose();
38129

130+
/**
131+
* Internal native initialization hook invoked exactly once by the constructor.
132+
* Not intended for direct external use.
133+
* <p>
134+
* Failure Handling:
135+
* Should raise a runtime exception if initialization fails.
136+
*/
39137
private native void initialize();
40138

41139
}

0 commit comments

Comments
 (0)