/// <summary> /// Attaches a hardware device context to the specified video component. /// </summary> /// <param name="component">The component.</param> /// <exception cref="Exception">Throws when unable to initialize the hardware device</exception> public void AttachDevice(VideoComponent component) { var result = 0; fixed(AVBufferRef **devContextRef = &component.HardwareDeviceContext) { result = ffmpeg.av_hwdevice_ctx_create(devContextRef, DeviceType, null, null, 0); if (result < 0) { throw new Exception($"Unable to initialize hardware context for device {Name}"); } } component.HardwareAccelerator = this; component.CodecContext->hw_device_ctx = ffmpeg.av_buffer_ref(component.HardwareDeviceContext); component.CodecContext->get_format = GetFormatCallback; }
/// <summary> /// Detaches and disposes the hardware device context from the specified video component /// </summary> /// <param name="component">The component.</param> public void DetachDevice(VideoComponent component) { // TODO: Check the blow code in the future because I am not sure // how to uninitialize the hardware device context if (component.CodecContext != null) { ffmpeg.av_buffer_unref(&component.CodecContext->hw_device_ctx); component.CodecContext->hw_device_ctx = null; } if (component.HardwareDeviceContext != null) { fixed(AVBufferRef **hwdc = &component.HardwareDeviceContext) { ffmpeg.av_buffer_unref(hwdc); component.HardwareDeviceContext = null; component.HardwareAccelerator = null; } } }