/// <summary> /// Attaches a hardware accelerator to this video component. /// </summary> /// <param name="selectedConfig">The selected configuration.</param> /// <returns> /// Whether or not the hardware accelerator was attached. /// </returns> public bool AttachHardwareDevice(HardwareDeviceInfo selectedConfig) { // Check for no device selection if (selectedConfig == null) { return(false); } try { var accelerator = new HardwareAccelerator(this, selectedConfig); AVBufferRef *devContextRef = null; var initResultCode = ffmpeg.av_hwdevice_ctx_create(&devContextRef, accelerator.DeviceType, null, null, 0); if (initResultCode < 0) { throw new MediaContainerException($"Unable to initialize hardware context for device {accelerator.Name}"); } HardwareDeviceContext = devContextRef; HardwareAccelerator = accelerator; CodecContext->hw_device_ctx = ffmpeg.av_buffer_ref(HardwareDeviceContext); CodecContext->get_format = accelerator.GetFormatCallback; return(true); } catch (Exception ex) { this.LogError(Aspects.Component, "Could not attach hardware decoder.", ex); return(false); } }
/// <summary> /// Initializes a new instance of the <see cref="HardwareAccelerator"/> class. /// </summary> /// <param name="component">The component this accelerator is attached to.</param> /// <param name="selectedConfig">The selected hardware device configuration.</param> public HardwareAccelerator(VideoComponent component, HardwareDeviceInfo selectedConfig) { Component = component; Name = selectedConfig.DeviceTypeName; DeviceType = selectedConfig.DeviceType; PixelFormat = selectedConfig.PixelFormat; GetFormatCallback = GetPixelFormat; }
/// <summary> /// Initializes a new instance of the <see cref="HardwareAccelerator"/> class. /// </summary> /// <param name="component">The component this accelerator is attached to.</param> /// <param name="selectedConfig">The selected hardware device configuration.</param> public HardwareAccelerator(VideoComponent component, HardwareDeviceInfo selectedConfig) { Component = component; Name = selectedConfig.DeviceTypeName; DeviceType = selectedConfig.DeviceType; PixelFormat = selectedConfig.PixelFormat; GetFormatCallback = new AVCodecContext_get_format(GetPixelFormat); }
bool IDeviceCreator.IsCanWorkWith(HardwareDeviceInfo info) { if (info.VID != 0483 || info.PID != 5740) { return(false); } if (String.IsNullOrEmpty(SerialPort.GetPortNames().FirstOrDefault(x => x == info.PortName))) { return(false); } using (SerialPort port = new SerialPort()) { try { port.PortName = info.PortName; port.BaudRate = 115200; port.DataBits = 8; port.Parity = System.IO.Ports.Parity.None; port.StopBits = System.IO.Ports.StopBits.One; port.Open(); port.ReadTimeout = 500; byte[] buf = new byte[15]; port.Write(new byte[] { 1 }, 0, 1); //timeout read int size = port.SafeRead(50, ref buf); //check size and command id if ((size != 5) || (buf[0] != 1)) { port.Close(); return(false); } DeviceInfo deviceInfo = (DeviceInfo)buf; //check UID if ((deviceInfo.UID[0] != 1) || (deviceInfo.UID[1] != 2) || (deviceInfo.UID[2] != 3)) { port.Close(); return(false); } port.Close(); return(true); } catch { port.Close(); return(false); } } }
IDevice IDeviceCreator.CreateInstanceDefault(HardwareDeviceInfo info) { if (!((IDeviceCreator)this).IsCanWorkWith(info)) { return(null); } return(new CANAnalyzerDevice(info.PortName)); }
/// <summary> /// Create new IDevice. /// </summary> /// <param name="info">Information needed for creation IDevice</param> /// <param name="returnDefault">If device isn't supported, then return null or exception. If true - return null or IDevice, if false - return IDevice or throw exceprion.</param> /// <returns>Return IDevice. Before creation checks compatibility. If not compatible, then throw exception or returned null.</returns> IDevice IDeviceCreator.CreateInstance(HardwareDeviceInfo info) { if (!((IDeviceCreator)this).IsCanWorkWith(info)) { throw new ArgumentException("this device cannot work with this hardware device."); } return(new CANHackerDevice(info.PortName)); }
public void UseHW() { _haInfo = null; foreach (var haa in HardwareAccelerator.GetCompatibleDevices(AVCodecID.AV_CODEC_ID_H264)) { if (haa.DeviceType == AVHWDeviceType.AV_HWDEVICE_TYPE_D3D11VA) { _haInfo = haa; } } }
/// <summary> /// Attaches a hardware accelerator to the specified component. /// </summary> /// <param name="component">The component.</param> /// <param name="selectedConfig">The selected configuration.</param> /// <returns> /// Whether or not the hardware accelerator was attached /// </returns> public static bool Attach(VideoComponent component, HardwareDeviceInfo selectedConfig) { try { var result = new HardwareAccelerator { Component = component, Name = selectedConfig.DeviceTypeName, DeviceType = selectedConfig.DeviceType, PixelFormat = selectedConfig.PixelFormat, }; result.InitializeHardwareContext(); return(true); } catch (Exception ex) { component.Container.Parent?.Log(MediaLogMessageType.Error, $"Could not attach hardware decoder. {ex.Message}"); return(false); } }
/// <summary> /// Check on compatability device. /// </summary> /// <param name="info">Information needed for check compatability.</param> /// <returns>Return true if device is compatability, else return false.</returns> bool IDeviceCreator.IsCanWorkWith(HardwareDeviceInfo info) { if (info.VID != 0483 || info.PID != 5740) { return(false); } if (String.IsNullOrEmpty(SerialPort.GetPortNames().FirstOrDefault(x => x == info.PortName))) { return(false); } using (SerialPort port = new SerialPort()) { try { port.PortName = info.PortName; port.BaudRate = 115200; port.DataBits = 8; port.DtrEnable = false; port.RtsEnable = false; port.Parity = System.IO.Ports.Parity.None; port.StopBits = System.IO.Ports.StopBits.One; port.ReadTimeout = 50; port.WriteTimeout = 50; port.Open(); char[] buf = new char[15]; port.Write(new char[] { 'v' }, 0, 1); int size = port.SafeRead(50, ref buf); if (size != 7) { port.Close(); return(false); } if (new string(buf, 0, 7) != "vSTM32\r") { port.Close(); return(false); } port.Write(new char[] { 'V' }, 0, 1); size = port.SafeRead(50, ref buf); if (size != 6) { port.Close(); return(false); } if (new string(buf, 0, 6) != "V0100\r") { port.Close(); return(false); } port.Close(); return(true); } catch { port.Close(); return(false); } } }