MCPcopy Create free account
hub / github.com/RustCV/RustCV / negotiate_format

Function negotiate_format

rustcv-backend-msmf/src/device.rs:282–328  ·  view source on GitHub ↗

Negotiates and selects the best video format from available options This function: 1. Gets the presentation descriptor from the MediaSource 2. Extracts the primary video stream descriptor 3. Iterates through all available media types 4. Parses each media type and calculates a match score 5. Selects the format with the highest score # Arguments `source` - The IMFMediaSource to query `config` - Ca

(source: &IMFMediaSource, config: &CameraConfig)

Source from the content-addressed store, hash-verified

280/// # Returns
281/// A `NegotiatedFormat` struct with selected resolution, format, and frame rate
282fn negotiate_format(source: &IMFMediaSource, config: &CameraConfig) -> Result<NegotiatedFormat> {
283 unsafe {
284 // Create presentation descriptor to access stream information
285 let pd = source
286 .CreatePresentationDescriptor()
287 .map_err(hresult_to_camera_error)?;
288 let mut selected = false.into();
289 let mut sd = None;
290
291 // Get the primary video stream descriptor (index 0)
292 pd.GetStreamDescriptorByIndex(0, &mut selected, &mut sd)
293 .map_err(hresult_to_camera_error)?;
294 let sd =
295 sd.ok_or_else(|| CameraError::Io(std::io::Error::other("No video stream found")))?;
296
297 // Get media type handler for format enumeration
298 let handler = sd.GetMediaTypeHandler().map_err(hresult_to_camera_error)?;
299 let count = handler
300 .GetMediaTypeCount()
301 .map_err(hresult_to_camera_error)?;
302
303 // Early exit if no media types available
304 if count == 0 {
305 return Err(CameraError::FormatNotSupported);
306 }
307
308 // Iterate through media types, tracking best format to avoid unnecessary clones
309 let mut best_format: Option<(i32, NegotiatedFormat)> = None;
310
311 for index in 0..count {
312 if let Ok(mt) = handler.GetMediaTypeByIndex(index) {
313 if let Some((score, fmt)) = parse_media_type(&mt, config) {
314 if best_format
315 .as_ref()
316 .is_none_or(|(best_score, _)| score > *best_score)
317 {
318 best_format = Some((score, fmt));
319 }
320 }
321 }
322 }
323
324 best_format
325 .map(|(_, fmt)| fmt)
326 .ok_or(CameraError::FormatNotSupported)
327 }
328}
329
330/// Parses a Media Foundation media type and calculates format score
331///

Callers 1

openFunction · 0.70

Calls 1

parse_media_typeFunction · 0.85

Tested by

no test coverage detected