2424#include < optional>
2525#include < stdexcept>
2626#include < string>
27+ #include < vector>
2728
2829#include " livekit/ffi_handle.h"
2930#include " livekit/room_event_types.h"
@@ -35,6 +36,7 @@ namespace livekit {
3536namespace proto {
3637class NewCaptureSourceRequest ;
3738class OwnedCaptureSource ;
39+ class CaptureDeviceList ;
3840} // namespace proto
3941
4042// / Error raised when capture source creation or control fails.
@@ -112,6 +114,195 @@ struct DemoVideoSourceConfig {
112114 std::uint32_t framerate_fps = 0 ;
113115};
114116
117+ // / Frame format delivered by a capture device.
118+ enum class DeviceFrameFormat {
119+ // / Planar I420/YUV420P.
120+ I420 = 0 ,
121+ // / Biplanar NV12.
122+ Nv12 = 1 ,
123+ // / Packed BGRA.
124+ Bgra = 2 ,
125+ // / Packed RGB24.
126+ Rgb24 = 3 ,
127+ // / Packed BGR24.
128+ Bgr24 = 4 ,
129+ // / Packed YUYV/YUY2.
130+ Yuyv = 5 ,
131+ // / Packed UYVY.
132+ Uyvy = 6 ,
133+ // / Single-plane 8-bit luma.
134+ Grey = 7 ,
135+ // / Encoded MJPEG frames.
136+ Mjpeg = 8 ,
137+ };
138+
139+ // / Capture format offered by or requested from a device.
140+ // /
141+ // / All three components are required. When building a request, name the frame
142+ // / format explicitly: which formats a device can be asked for is
143+ // / platform-specific, so there is no portable default.
144+ struct DeviceFormat {
145+ // / Frame dimensions.
146+ CaptureResolution resolution;
147+
148+ // / Frame rate in frames per second.
149+ std::uint32_t framerate_fps = 0 ;
150+
151+ // / Frame format.
152+ DeviceFrameFormat frame_format = DeviceFrameFormat::Nv12;
153+
154+ DeviceFormat () = default ;
155+
156+ DeviceFormat (CaptureResolution resolution, std::uint32_t framerate_fps, DeviceFrameFormat frame_format)
157+ : resolution(resolution), framerate_fps(framerate_fps), frame_format(frame_format) {}
158+ };
159+
160+ // / How a capture device should pick the format it delivers.
161+ // /
162+ // / The device negotiates the delivered format; the created source reports the
163+ // / negotiated resolution through @ref CaptureSource::width and
164+ // / @ref CaptureSource::height. The negotiated frame rate and frame format are
165+ // / not reported back.
166+ // /
167+ // / @note Only resolution and frame rate participate in format selection.
168+ // / A @c frame_format is validated and then treated as a preference the backend
169+ // / may substitute, so the delivered format can differ from the one requested:
170+ // /
171+ // / - macOS accepts only @ref DeviceFrameFormat::I420,
172+ // / @ref DeviceFrameFormat::Nv12, and @ref DeviceFrameFormat::Bgra --
173+ // / requesting any other value fails creation -- and then delivers NV12
174+ // / regardless of which of the three was asked for.
175+ // / - V4L2 rejects @ref DeviceFrameFormat::I420 and @ref DeviceFrameFormat::Bgra.
176+ // / For @ref exact and @ref closest it tries the requested format first and
177+ // / then falls back through the formats it supports; for @ref highestFramerate
178+ // / and @ref highestResolution the constraint selects the format outright, with
179+ // / no fallback.
180+ // /
181+ // / @ref DeviceFrameFormat::Nv12 is the only value both backends accept, which
182+ // / is why it is @ref DeviceFormat's default.
183+ class LIVEKIT_API DeviceFormatRequest {
184+ public:
185+ // / Optional constraints for @ref highestFramerate.
186+ struct HighestFramerateConstraint {
187+ // / Only consider formats with this resolution.
188+ std::optional<CaptureResolution> resolution;
189+ // / Only consider formats with this frame format.
190+ std::optional<DeviceFrameFormat> frame_format;
191+ };
192+
193+ // / Optional constraints for @ref highestResolution.
194+ struct HighestResolutionConstraint {
195+ // / Only consider formats with this frame rate.
196+ std::optional<std::uint32_t > framerate_fps;
197+ // / Only consider formats with this frame format.
198+ std::optional<DeviceFrameFormat> frame_format;
199+ };
200+
201+ // / Let the device choose its default format.
202+ DeviceFormatRequest () = default ;
203+
204+ // / Require the resolution to match exactly; creation fails if the device
205+ // / offers no such format.
206+ // /
207+ // / Frame-rate matching is rounding-tolerant, because devices commonly
208+ // / advertise near-integral rates: a request for 30 fps is satisfied by a
209+ // / device advertising 30.00003 fps, and the device is then driven at its
210+ // / advertised rate.
211+ static DeviceFormatRequest exact (DeviceFormat format);
212+
213+ // / Use the device's closest supported resolution and frame rate.
214+ static DeviceFormatRequest closest (DeviceFormat format);
215+
216+ // / Prefer the highest frame rate, optionally constrained.
217+ static DeviceFormatRequest highestFramerate (HighestFramerateConstraint constraint = {});
218+
219+ // / Prefer the highest resolution, optionally constrained.
220+ static DeviceFormatRequest highestResolution (HighestResolutionConstraint constraint = {});
221+
222+ private:
223+ friend class CaptureSource ;
224+
225+ // / Which selection strategy this request carries.
226+ enum class Kind { Default, Exact, Closest, HighestFramerate, HighestResolution };
227+
228+ Kind kind_ = Kind::Default;
229+ DeviceFormat format_;
230+ HighestFramerateConstraint highest_framerate_;
231+ HighestResolutionConstraint highest_resolution_;
232+ };
233+
234+ // / Selects which video device a capture source opens.
235+ class LIVEKIT_API DeviceSelector {
236+ public:
237+ // / The platform default video device.
238+ DeviceSelector () = default ;
239+
240+ // / The device at this position in the platform's own enumeration order.
241+ // /
242+ // / The meaning is platform-defined and is not necessarily the position of
243+ // / the device in @ref CaptureSource::listDevices: on macOS it indexes the
244+ // / AVFoundation video-device list, on Linux it is the V4L2 node number
245+ // / (@c /dev/videoN). Either way the value can shift as devices are attached
246+ // / and removed. Prefer @ref id, which is stable on macOS.
247+ static DeviceSelector index (std::uint32_t device_index);
248+
249+ // / A platform-stable identifier, as reported by @ref CaptureDeviceInfo::id.
250+ static DeviceSelector id (std::string device_id);
251+
252+ private:
253+ friend class CaptureSource ;
254+
255+ // / Which form of device selection this carries.
256+ enum class Kind { Default, Index, Id };
257+
258+ Kind kind_ = Kind::Default;
259+ std::uint32_t index_ = 0 ;
260+ std::string id_;
261+ };
262+
263+ // / Configuration for camera device capture using the platform's native
264+ // / capture stack.
265+ struct DeviceVideoSourceConfig {
266+ // / Device to capture from; the platform default device when unset.
267+ DeviceSelector device;
268+
269+ // / Format requested from the device; the device default when unset.
270+ DeviceFormatRequest format;
271+ };
272+
273+ // / A video capture device discovered by @ref CaptureSource::listDevices.
274+ struct CaptureDeviceInfo {
275+ // / Device identifier; pass to @ref DeviceSelector::id.
276+ // /
277+ // / Stable across reboots and re-plugging on macOS, where it is the
278+ // / AVFoundation unique id. On V4L2 it is the node number, so it can change
279+ // / as devices are attached and removed; re-enumerate rather than persisting
280+ // / it there.
281+ std::string id;
282+
283+ // / Human-readable device name.
284+ std::string name;
285+
286+ // / Device model identifier, when available.
287+ std::optional<std::string> model_id;
288+
289+ // / Device manufacturer, when available.
290+ std::optional<std::string> manufacturer;
291+
292+ // / Capture formats reported by the device.
293+ // /
294+ // / Empty unless @ref formats_complete is true. Do not use this to decide
295+ // / what to request: ask for a format and let the device negotiate.
296+ std::vector<DeviceFormat> formats;
297+
298+ // / Whether @ref formats is a complete list; some platforms do not
299+ // / enumerate formats up front.
300+ // /
301+ // / False on macOS, where AVFoundation formats are not enumerated; true on
302+ // / V4L2.
303+ bool formats_complete = false ;
304+ };
305+
115306// / Why a capture ended without error.
116307enum class CaptureExit {
117308 // / Stopped via @ref CaptureSource::stop (or source destruction).
@@ -162,6 +353,23 @@ class LIVEKIT_API CaptureSource {
162353 // / Create the built-in demo capture source (solid cycling colors).
163354 static std::future<std::shared_ptr<CaptureSource>> create (DemoVideoSourceConfig config);
164355
356+ // / Create a capture source from a camera device.
357+ // /
358+ // / Completes asynchronously: construction opens the device and negotiates
359+ // / the capture format, so @ref width and @ref height report the negotiated
360+ // / resolution before the first frame is pumped. Errors (missing device,
361+ // / unsatisfiable
362+ // / format request, unsupported platform, missing capture feature) are
363+ // / thrown from the future as @ref CaptureSourceError.
364+ static std::future<std::shared_ptr<CaptureSource>> create (DeviceVideoSourceConfig config);
365+
366+ // / List the video capture devices available on this machine.
367+ // /
368+ // / Completes asynchronously: enumeration queries the platform capture stack
369+ // / and may block briefly. Throws @ref CaptureSourceError from the future on
370+ // / failure, including on platforms without a capture backend.
371+ static std::future<std::vector<CaptureDeviceInfo>> listDevices ();
372+
165373 ~CaptureSource ();
166374
167375 CaptureSource (const CaptureSource&) = delete ;
0 commit comments