Contents

MAFlashingLightsProcessor

A class that processes a framebuffer object to detect and dim sequences of flashing lights.

Declaration

class MAFlashingLightsProcessor

Overview

A device with the Dim Flashing Lights setting on automatically dims the brightness of flashing effect sequences when it detects them in video content. If your app performs custom video drawing instead of using AVFoundation APIs, you can use the MAFlashingLightsProcessor class to detect and mitigate sequences of flashing effects in your video content.

The following example shows how you might incorporate MAFlashingLightsProcessor into code that uses Core Video APIs.

import MediaAccessibility
import CoreVideo
import OSLog

private let processor = MAFlashingLightsProcessor()
private let logger = Logger()

func readVideoBuffer() {
    
    // Confirm that the Dim Flashing Lights setting is on before processing video.
    if !MADimFlashingLightsEnabled() { return }

    // Retrieve the CVPixelBuffer from your video content.
    // ...
    
    // Get the IOSurface that backs the pixel buffer.
    guard let inSurface = CVPixelBufferGetIOSurface(pixelBuffer)?.takeUnretainedValue() else {
        logger.debug("Can't initialize input surface.")
        return
    }
    
    // Use the properties of the pixel buffer to initialize an output IOSurface.
    guard var outSurface = IOSurface(properties: [
        .width: CVPixelBufferGetWidth(pixelBuffer),
        .height: CVPixelBufferGetHeight(pixelBuffer),
        .bytesPerRow: CVPixelBufferGetWidth(pixelBuffer) * 4,
        .bytesPerElement: 4,
        .pixelFormat: CVPixelBufferGetPixelFormatType(pixelBuffer)
    ]) as IOSurfaceRef? else {
        logger.debug("Can't initialize output surface.")
        return
    }
    
    // Verify that the input IOSurface is compatible with the flashing lights processor.
    if processor.canProcessSurface(inSurface) {
        
        // Analyze input IOSurface for flashing light sequences
        // and write mitigated content to output IOSurface.
        let result = processor.processSurface(inSurface, outSurface: &outSurface,
                                              timestamp: CFAbsoluteTimeGetCurrent())
        
        if result.surfaceProcessed {
            logger.debug("""
            Processed content with flashing lights intensity \(result.intensityLevel)
            and mitigated output with mitigation level \(result.mitigationLevel).
            """)
            
            // Convert the mitigated output surface back to CVPixelBuffer
            // and draw the video content.
            // ...
            
        } else {
            logger.debug("Can't process input surface.")
        }
    }
}

For more information, see Flashing lights.

Topics

Checking compatibility

Processing video content

See Also

Dim flashing lights