Jump to content

WebcamDS (DirectShow webcam)


frank10
 Share

Recommended Posts

  • 1 month later...
  • 7 months later...
  • 2 weeks later...
  • 3 months later...

Hello Frank, Thanks for sharing us best script. Yeah, your script have many advantages compare with Webcam UDF. I would like to adopt more option to make our WebcamDS more completely, what i need is adopt options to adjust video source, format...especially, i am get in trouble with color system option. I tried to get video signal from output by video capture card then script just show black and white instead of color, because color system from out put is PAL while notebook use NTSC, unfortunately, our script was not adopted function to adjust color system....Could you help me to fix it?. 

[topic='27925']WebcamExample[/topic] || [topic='68866']WebcamUDF[/topic] || [topic='138222']DSShowCaptureFilter[/topic] || [topic='20121']ScreenResolutionChange[/topic] || [topic='139426']WebcamDS[/topic] || [topic='156613']ObfuscatorUTF8[/topic] || [topic='156219']VideoColorSystemPAL/NTSC[/topic] || [topic='31963']ExcelWindowsInGUI[/topic] || [topic='110473']4Webcam[/topic] || [topic='83763']RichEdit[/topic] || [topic='83763']RichEditUDF[/topic] || [topic='142384']RichEditLabel[/topic] ||[topic='96986']GUITFLabel[/topic] || [topic='32144']AccessCom[/topic] || [topic='38353']BetaPad[/topic] || [topic='31963']EmbeddedExcel[/topic]

Link to comment
Share on other sites

  • 2 months later...

Hi

About take snapshots:

Anyone devise a way to maintain the camera minimized, and, even on that state, to take a snapshot?

It makes sense if we need a camera for automation, like, if a light turns on or someone passes in front camera, or even some kind of movement, to trigger some action.

On that case, camera image is not needed, in fact, disturb the screen.

At the past I did something like that, but using 2 monitors, one external (under the desk) where the camera was shown, and other the one that was really used.

Thanks

Jose

Link to comment
Share on other sites

  • 4 years later...

I love this UDF, but I'm having a huge trouble: I use it in an Intel NUC with a problem with the built-in soundcard.

I've been trying to modify the UDF to force it to dismiss the audio part, as it always throws the same error when I'm trying to run a script with a webcam stream:

==> Variable must be of type "Object".:
While $oEnum.Next(1, $pMoniker, 0) = $S_OK
While $oEnum^ ERROR

I have modified the WebcamDS_UDF deleting all the audio and microphone parts, but I'm getting the same error over and over.

#include-once
#include <WindowsConstants.au3>

; #INDEX# =======================================================================================================================
; Title .........: WebcamDS
; AutoIt Version : from  3.3.9.1 beta
; Language ......: English
; Description ...: handle webcams with Direct Show
; Author(s) .....: frank10
;
; ------------------------ CREDITS: -------------------------
;
; thanks to trancexx and ProgAndy for the beginning tasks with DirectShow objects
; ===============================================================================================================================

; #VARIABLES# ===================================================================================================================
Global $aWebcamList[9]
Global $aCompressorList[50]
;~ Global $aAuCompressorList[50]
Global $aMicList[9]
Global $iWebcam = 1, $iWebAudio = 0, $iWebRescale = 1, $iWebX, $iWebY, $iWebBpp, $aZoomPos[4]
$aZoomPos[0] = 1
$aZoomPos[1] = 0
$aZoomPos[2] = 1
$aZoomPos[3] = 0

Global $oBasicAudio, $oBasicVideo, $oBuild, $oCapture[9], $oCaptureAudio, $oGraph, $oIBaseFilter, $oMediaControl, $oDevEnum, $oEnum, $oICreateDevEnum, $oIEnumMoniker
Global $oVideoWindow, $oVmr9, $oCompressor, $oStreamConfig
Global $Vmr9 = 1
; ===============================================================================================================================

; #CONSTANTS# ===================================================================================================================
#include "DirectShow_Interfaces.au3"
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_WebcamDS_ReleaseObjects
;_WebcamDS_RenderWebcam
;_WebcamDS_SaveFile
;_WebcamDS_WebcamInit
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
;_WebcamDS_EnumerateDevices
;_WebcamDS_Reset
;_WebcamDS_SelectRect
; ===============================================================================================================================


; #FUNCTION# ====================================================================================================================
;
; Name...........: _WebcamDS_ReleaseObjects
; Description ...: release Objects created during script and at the end of it
; Syntax.........: _WebcamDS_ReleaseObjects($iArg = 0)
; Parameters ....: $iArg   - use 0 if you want to delete only the current capture obj, otherwise (ie: at the end) put it to 1
; Return values .: Success - it deletes the objects created
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Author ........: Frank10
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
;
; ===============================================================================================================================
Func _WebcamDS_ReleaseObjects($iArg = 0)
    ;$oMediaControl.Stop()
    $oMediaControl = 0
    $oVmr9 = 0
    $oCapture[$iWebcam] = 0
    If $iArg Then $oCapture = 0
    $oCaptureAudio = 0
    $oBasicAudio = 0
    $oBasicVideo = 0
    $oVideoWindow = 0
    $oGraph = 0
    $oBuild = 0
    $oCompressor = 0
EndFunc   ;==>_WebcamDS_ReleaseObjects



; #FUNCTION# ====================================================================================================================
;
; Name...........: _WebcamDS_RenderWebcam
; Description ...: call it to set which webcam to display, audio preview, resolution, scale to GUI, zoom
; Syntax.........: _WebcamDS_RenderWebcam($iNumberWebcam=1,$iAudioMic=0,$hGuiWin[,$iScale=1[,$xVideo=640[,$yVideo=480[,$ibpp=16[,$iZoomX=0[,$iZoomY=0[,$iZoomW=0[,$iZoomH=0]]]]]]]])
; Parameters ....: $iNumberWebcam - The webcam to display (from 1)
;                  $iAudioMic   - If you want to preview the audio from the mic of the webcam set it to the corresponding
;                                   position of the mic
;                               - see the position in the Mic list in the array $aMicList filled by _WebcamInit (the position of
;                                   the Mic isn't always equal to the webcam position!)
;                               - it's possible to view one webcam and listen to the mic of another one!
;                               - if you don't want audio preview, set it to 0
;                  $hGuiWin     - The GUI you create to display the webcam
;                  $iScale      - if you want to scale the webcam resolution to the res of the GUI, set it to 1, otherwise to 0
;                  $xVideo      - The webcam Width
;                  $yVideo      - The webcam Height
;                  $ibpp        - bitplane (if you put strange values it doesn't seem to change it..) normal: 12-16-24
;                  $iZoomX      - rectangle Zoom offsetX (0 = noZoom)
;                               - if you use it manually, be careful to put values that are inside the original width-height of
;                                   webcam otherwise it doesn't work
;                               - it's better to use the Setrect func drawing the rectangle with mouse directly on the GUI
;                  $iZoomY      - rectangle Zoom offsetY (0 = noZoom)
;                  $iZoomW      - rectangle Zoom Width (0 = noZoom)
;                  $iZoomH      - rectangle Zoom Heigth (0 = noZoom)
; Return values .: Success - it displays the webcam selected
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Author ........: Frank10
; Modified.......:
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; Yes
;
; ===============================================================================================================================
;~ Func _RenderWebcam(ByRef $iWebcam,$hGuiWin=$hGUI,ByRef $iScale,$xVideo='640',$yVideo='480',$ibpp='16',$iZoomX=0,$iZoomY=0,$iZoomW=0,$iZoomH=0)
;~ Func _RenderWebcam($iWebcam,$hGuiWin=$hGUI,$iScale,$xVideo='640',$yVideo='480',$ibpp='16',$iZoomX=0,$iZoomY=0,$iZoomW=0,$iZoomH=0)
Func _WebcamDS_RenderWebcam($iNumberWebcam, $iAudioMic, $hGuiWin, $iScale = 1, $xVideo = 640, $yVideo = 480, $ibpp = 16, $iZoomX = 0, $iZoomY = 0, $iZoomW = 0, $iZoomH = 0)
    Local $hr
    $iWebcam = $iNumberWebcam
    $iWebAudio = $iAudioMic
    $iWebRescale = $iScale
    $iWebX = $xVideo
    $iWebY = $yVideo

    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory, $iNumberWebcam)
    ;----------------------------------------------------------------------------------------------------
    ;-----------------------------------  change resolution original video: --------------
    Local $_GUID = "DWORD Data1;" & _
            "WORD  Data2;" & _
            "WORD  Data3;" & _
            "BYTE  Data4[8];"
;~  Local $tGuid = DllStructCreate($_GUID)

    Local $_SIZE = "LONG cx;" & _
            "LONG cy;"
;~  Local $tSize = DllStructCreate($_SIZE)

    Local $_VIDEO_STREAM_CONFIG_CAPS = $_GUID & _
            "ULONG    VideoStandard;" & _
            $_SIZE & _
            $_SIZE & _
            $_SIZE & _
            "int      CropGranularityX;" & _
            "int      CropGranularityY;" & _
            "int      CropAlignX;" & _
            "int      CropAlignY;" & _
            $_SIZE & _
            $_SIZE & _
            "int      OutputGranularityX;" & _
            "int      OutputGranularityY;" & _
            "int      StretchTapsX;" & _
            "int      StretchTapsY;" & _
            "int      ShrinkTapsX;" & _
            "int      ShrinkTapsY;" & _
            "int64  MinFrameInterval;" & _
            "int64   MaxFrameInterval;" & _
            "LONG     MinBitsPerSecond;" & _
            "LONG     MaxBitsPerSecond;"
    Local $tVideoStream = DllStructCreate($_VIDEO_STREAM_CONFIG_CAPS)

    Local $_AM_MEDIA = $_GUID & _
            $_GUID & _
            'BOOL  bFixedSizeSamples;' & _
            'BOOL  bTemporalCompression;' & _
            'ULONG    lSampleSize;' & _
            $_GUID & _
            'ptr pUnk;' & _
            'ULONG    cbFormat;' & _
            'ptr  pbFormat;'
    Local $t_AM_MEDIA = DllStructCreate($_AM_MEDIA)

    Local $_RECT = 'LONG left;' & _
            'LONG top;' & _
            'LONG right;' & _
            'LONG bottom;'
    Local $_tagBITMAPINFOHEADER = 'DWORD biSize;' & _
            'LONG  biWidth;' & _
            'LONG  biHeight;' & _
            'WORD  biPlanes;' & _
            'WORD  biBitCount;' & _
            'DWORD biCompression;' & _
            'DWORD biSizeImage;' & _
            'LONG  biXPelsPerMeter;' & _
            'LONG  biYPelsPerMeter;' & _
            'DWORD biClrUsed;' & _
            'DWORD biClrImportant;'
    Local $_tagVIDEOINFOHEADER = $_RECT & _
            $_RECT & _
            'DWORD            dwBitRate;' & _
            'DWORD            dwBitErrorRate;' & _
            'int64   AvgTimePerFrame;' & _
            $_tagBITMAPINFOHEADER

;~  Local $MAX_PIN_NAME = 128
    Local $_PinInfo = 'ptr   pFilter;' & _
            'int64    dir;' & _
            'wchar    achName[128];'
;~      'wchar    achName['&$MAX_PIN_NAME&'];'


    Local $pConfig
    $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], $sIID_IAMStreamConfig, $pConfig)
    $oStreamConfig = ObjCreateInterface($pConfig, $sIID_IAMStreamConfig, $tagIAMStreamConfig)

    Local $iCount = 0, $iSize = 0
    $oStreamConfig.GetNumberOfCapabilities($iCount, $iSize)

    ; Check the size to make sure we pass in the correct structure.
    If DllStructGetSize($tVideoStream) == $iSize Then
        For $iFormat = 0 To $iCount - 1 Step 1
            Local $scc = DllStructGetPtr($tVideoStream)
            Local $pmtConfig = DllStructGetPtr($t_AM_MEDIA)

            $hr = $oStreamConfig.GetStreamCaps($iFormat, $pmtConfig, $scc) ;
            Local $t_AM_MEDIA_TYPE = DllStructCreate($_AM_MEDIA, $pmtConfig)

            Local $pbFormat = DllStructGetData($t_AM_MEDIA_TYPE, "pbFormat")
            Local $t_VIDEOINFOHEADER = DllStructCreate($_tagVIDEOINFOHEADER, $pbFormat)

            Local $videoWidth = DllStructGetData($t_VIDEOINFOHEADER, 'biWidth')
            Local $videoHeight = DllStructGetData($t_VIDEOINFOHEADER, 'biHeight')
            Local $bitCount = DllStructGetData($t_VIDEOINFOHEADER, 'biBitCount')
            Local $dwBitRate = DllStructGetData($t_VIDEOINFOHEADER, 'dwBitRate')
            Local $biCompression = DllStructGetData($t_VIDEOINFOHEADER, 'biCompression')
            Local $biPlanes = DllStructGetData($t_VIDEOINFOHEADER, 'biPlanes')
            Local $biSize = DllStructGetData($t_VIDEOINFOHEADER, 'biSizeImage')
            Local $AvgframeRate = Round(10000000 / DllStructGetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame'))

            ; identify FOURCC compression:
            Local $sText = ''
            For $i = 7 To 0 Step -2
                If $biCompression <> 0 Then $sText = $sText & Chr(Dec(StringMid(Hex($biCompression, 8), $i, 2)))
            Next
;~          if $videoWidth <> 0 Then ConsoleWrite('----- iFormat:'&$iFormat& '  VIDEOINFOHEADER:'&$videoWidth&'x'&$videoHeight&','&$bitCount & ', FrameRate:' &$AvgframeRate&', bitRate(MBs):' & Round($dwBitRate/8/1024/1024,1) & ', Compression:' & Hex($biCompression,8)& '-' _
;~              & $sText & ', biSize(bytes):'  & $biSize  & @CRLF)

            Local $FrameRate = 30 ;GUICtrlRead($FrameInput)
            DllStructSetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame', 10000000 / $FrameRate)
            If $videoWidth = $xVideo And $videoHeight = $yVideo And $bitCount = $ibpp Then
                $hr = $oStreamConfig.SetFormat(DllStructGetPtr($t_AM_MEDIA_TYPE)) ;
                ExitLoop
            EndIf

            ; if there is no match with the desired res, I create one personalized:
            If $iFormat = $iCount - 1 Then
                DllStructSetData($t_VIDEOINFOHEADER, 'biWidth', $xVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biHeight', $yVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biBitCount', $ibpp)
                $hr = $oStreamConfig.SetFormat(DllStructGetPtr($t_AM_MEDIA_TYPE)) ;
            EndIf
        Next

        $oStreamConfig = 0
    EndIf ; end change resolution

    ;----------------------------------------------------------------------------------------------------
    ;------------------------------------------------------AUDIO preview: -------------------------------

    If $iAudioMic <> 0 Then
        _WebcamDS_EnumerateDevices($sCLSID_AudioInputDeviceCategory, $iAudioMic)
        $pConfig = ''
        $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Audio, $oCaptureAudio, $sIID_IAMStreamConfig, $pConfig)
        Global $oAStreamConfig = ObjCreateInterface($pConfig, $sIID_IAMStreamConfig, $tagIAMStreamConfig)
    EndIf

;~  if IsObj($oCaptureAudio) Then
;~          local $pEnum
;~          $hr = $oCaptureAudio.EnumPins($pEnum)
;~          $oEnum =ObjCreateInterface($pEnum, $sIID_IEnumPins, $tagIEnumPins)

;~          Local $pPin,$pCompress,$pInfo,$oPin
;~          While $oEnum.Next(1, $pPin, 0) = $S_OK
;~              $oPin = ObjCreateInterface($pPin, $sIID_IPin, $tagIPin)

;~              local $pPinInfo
;~              $hr = $oPin.QueryPinInfo($pPinInfo)
;~              Local $t_PinInfo = DLLStructCreate($_PinInfo, $pPinInfo)
;~              ConsoleWrite('--PinName:--'& @error &' struct:'&DLLStructGetData($t_PinInfo, 3)  &@CRLF)

;~              Local $pAudioMixer, $oAudioMixer
;~              $oPin = 0
;~              $i +=1
;~              ConsoleWrite('---i:'&$i&' hr:'&$hr&@CRLF)
;~          WEnd
;~          $oEnum = 0
;~          $oPin = 0
;~  EndIf


    ;---------------------------------------------------------------------------------------
    ;-----------------------------------  VMR7 or VMR9 renderer   --------------------------
    If $Vmr9 = 0 Then
        ;  Build the preview part of the graph.
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, 0)
        If IsObj($oCaptureAudio) Then $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Audio, $oCaptureAudio, 0, 0)
    Else
        $oVmr9 = ObjCreateInterface($sCLSID_VideoMixingRenderer9, $sIID_IBaseFilter, $tagIBaseFilter)

        ; Call IFilterGraph::AddFilter on the Filter Graph Manager to add the VMR-9 to the filter graph:
        $hr = $oGraph.AddFilter($oVmr9, "VMR9")

        ; Call the ICaptureGraphBuilder2::RenderStream method to render the video stream to the VMR and eventually the audio:
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, $oVmr9)
        If IsObj($oCaptureAudio) Then $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Audio, $oCaptureAudio, 0, 0)
        ;       ConsoleWrite(">>RenderstreamPrev VMR9 hr = " & Hex($hr) & @CRLF)    ;>>RenderstreamPrev VMR9 hr = 0004027E  OK: Preview was rendered throught the Smart Tee filter,
        $oVmr9 = 0
    EndIf

    ; set position e dimensions of webcam inside GUI, eventually rescale it
    Local $iX = 0, $iY = 0 ; line of buttons above

    Local $aClientSize = WinGetClientSize($hGuiWin)
    If $iScale = 1 Then
        $xVideo = $aClientSize[0]
        $yVideo = $aClientSize[1]
    EndIf

    If $iZoomW <> 0 Then
        $hr = $oBasicVideo.SetSourcePosition($iZoomX, $iZoomY, $iZoomW, $iZoomH)
    EndIf
    ;$hr = $oVideoWindow.SetWindowPosition($iX, $iY, $xVideo, $yVideo)
    $hr = $oVideoWindow.SetWindowPosition($aClientSize[0] - $xVideo, $iY, $xVideo, $yVideo)

    ; connect video to GUI
    $hr = $oVideoWindow.put_Owner($hGuiWin)

    ; borderless
    $oVideoWindow.put_WindowStyle(BitOR($WS_CHILD, $WS_CLIPCHILDREN))

    ; start graph
    $oMediaControl.Run()


EndFunc   ;==>_WebcamDS_RenderWebcam


; #FUNCTION# ====================================================================================================================
;
; Name...........: _WebcamDS_Init
; Description ...: Create Direct Show objects and enumerate webcams, compressors, micAudio
; Syntax.........: _WebcamDS_Init()
; Parameters ....: none
; Return values .: Success - it fills 3 array  $aWebcamList, $aCompressorList, $aMicList
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Author ........: Frank10
; Modified.......:
; Remarks .......: call it at the beginning of the script
; Related .......:
; Link ..........;
; Example .......; Yes
;
; ===============================================================================================================================
Func _WebcamDS_Init()
    Local $hr, $aCall
;~  / / Create the FGM.
    $oGraph = ObjCreateInterface($sCLSID_FilterGraph, $sIID_IGraphBuilder, $tagIGraphBuilder)

;~  / / Create the capture graph builder helper object
    $oBuild = ObjCreateInterface($sCLSID_CaptureGraphBuilder2, $sIID_ICaptureGraphBuilder2, $tagICaptureGraphBuilder2)

;~  / / Tell the capture graph builder about the FGM.
    $oBuild.SetFiltergraph($oGraph)

    Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)

    Local $pVideoWindow
    $oGraph.QueryInterface($sIID_IVideoWindow, $pVideoWindow)
    $oVideoWindow = ObjCreateInterface($pVideoWindow, $sIID_IVideoWindow, $tagIVideoWindow)

    Local $pBasicVideo
    $oGraph.QueryInterface($sIID_IBasicVideo, $pBasicVideo)
    $oBasicVideo = ObjCreateInterface($pBasicVideo, $sIID_IBasicVideo, $tagIBasicVideo)

    If $aWebcamList[1] = '' Then
        _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory)

        _WebcamDS_EnumerateDevices($sCLSID_VideoCompressorCategory)

        _WebcamDS_EnumerateDevices($sCLSID_AudioInputDeviceCategory)
    EndIf
EndFunc   ;==>_WebcamDS_Init



; =====================================================================================================================
;
; #INTERNAL_NO_DOC# ===================================================================================================
;
; =====================================================================================================================
Func _WebcamDS_EnumerateDevices($CLSID_category, $argument = 99)
    ; Create a helper object To find the capture device.
    $oDevEnum = ObjCreateInterface($sCLSID_SystemDeviceEnum, $sIID_ICreateDevEnum, $tagICreateDevEnum)

    Local $pEnum = 0
    $oDevEnum.CreateClassEnumerator($CLSID_category, $pEnum, 0)
    $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumMoniker, $tagIEnumMoniker)
    Local $iPosition = '', $arg = ''
    If StringInStr($CLSID_category, "860bb310") Or StringInStr($CLSID_category, "33d9a762") Then ; VideoInputDevice or Audio
        $iPosition = $argument
        $arg = ''
    Else ; data for compressor
        $iPosition = ''
        $arg = $argument
    EndIf

    Local $pMoniker, $oMoniker, $hr
    Local $i = 1, $pBindObj[99]
    ;If $oEnum.Next(1, $pMoniker, 0) = 0 Then
    While $oEnum.Next(1, $pMoniker, 0) = $S_OK
        Local $pPropBag, $oPropBag
        $oMoniker = ObjCreateInterface($pMoniker, $sIID_IMoniker, $tagIMoniker)
        $oMoniker.BindToObject(0, 0, $sIID_IBaseFilter, $pBindObj[$i])

        $hr = $oMoniker.BindToStorage(0, 0, $sIID_IPropertyBag, $pPropBag)
        $oPropBag = ObjCreateInterface($pPropBag, $sIID_IPropertyBag, $tagIPropertyBag)
        Local $var = ''
        $oPropBag.Read("FriendlyName", $var, 0)
        ;       ConsoleWrite('>> FriendlyMoniker' & $i & ': ' &$var& @CRLF)

        If $iPosition = 99 Then
            If StringInStr($CLSID_category, "33d9a762") Then ; list mics
                Sleep(10)
            Else ; list webcams
                If $var <> '' Then
                    $aWebcamList[$i] = $var
                EndIf
            EndIf
            Local $oCaptureTemp = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
            $oCaptureTemp = 0
            ; create oCaptureTemp otherwise with some webcams (Philips) gives error at the end of script ????
            ; ---------------------------
            ; ASSERT Failed
            ; ---------------------------
            ; Executable: autoit3.exe  Pid 17dc  Tid 768. Module CamExt40V32.ax, 2 objects left active!
            ; At line 320 of f:\dev64\dsdll\dllentry.cpp
            ; Continue? (Cancel to debug)
            ; ---------------------------

        EndIf
        If $iPosition = '' And $arg = 99 Then
            ; list of compressor
            If $var <> '' Then
                $aCompressorList[$i] = $var
            EndIf
        EndIf

        If ($iPosition <> '' And $iPosition = $i) Or ($arg <> '' And $var = $arg) Then
            ;           ConsoleWrite('------found'&@CRLF)
            If ($iPosition = '' And $arg = $var) Then ;
                If Not IsObj($oCompressor) Then
                    $oCompressor = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                EndIf
                ; Add the compressor filter To the filter graph
                $hr = $oGraph.AddFilter($oCompressor, "Compressor" & $iWebcam)
                ExitLoop
            EndIf

;~          If ($iPosition <> '' And $iPosition = $i) Then
;~              If StringInStr($CLSID_category, "33d9a762") Then ; Ë l'audio

;~                  If Not IsObj($oCaptureAudio) Then
;~                      $oCaptureAudio = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
;~                      ConsoleWrite($iWebAudio & 'capAu:' & $i & @CRLF)
;~                  EndIf
;~                  ;  Add the captureAudio filter To the filter graph
;~                  $hr = $oGraph.AddFilter($oCaptureAudio, $var)
;~              Else
;~                  If Not IsObj($oCapture[$iPosition]) Then
;~                      $oCapture[$iPosition] = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
;~                  EndIf
;~                  ;  Add the captureVideo filter To the filter graph
;~                  $hr = $oGraph.AddFilter($oCapture[$iPosition], "CaptureFilter" & $iWebcam)
;~                  ;WinSetTitle($hGUI, '', $var)
;~              EndIf
;~              ExitLoop
;~          EndIf
        EndIf
        $oPropBag = 0
        $i += 1
    WEnd
    ; _ArrayDisplay($aCompressorList)
;~  If $iPosition = 99 And StringInStr($CLSID_category, "33d9a762") Then $aMicList[0] = $i - 1
    If $iPosition = 99 And StringInStr($CLSID_category, "860bb310") Then $aWebcamList[0] = $i - 1
    If $iPosition = '' And $arg = 99 Then
        $aCompressorList[$i] = 'Uncompressed'
        $aCompressorList[0] = $i - 1
    EndIf
    $oMoniker = 0
    $oEnum = 0
    $oDevEnum = 0
EndFunc   ;==>_WebcamDS_EnumerateDevices

Func _WebcamDS_Reset()
    _WebcamDS_ReleaseObjects()
    _WebcamDS_Init()
EndFunc   ;==>_WebcamDS_Reset

Func _WebcamDS_SelectRect($hGuiWin)
    Sleep(5)
EndFunc   ;==>_WebcamDS_SelectRect

I use the webcam as a QR reader. I have some other alternatives to show the webcam stream on screen (one is using another UDF, but it turns the main GUI unresponsible).

Another alternative is to use a Silverlight or a HTML based ActiveX, but they do require the user to click on a button to turn on the camera.

Could someone give me a clue about what should I remove from the original UDF to make it not soundcard dependant?

Greets from Barcelona

Edited by Qwerty212
Link to comment
Share on other sites

  • 6 months later...

@Qwerty212 I took out the audio and video capturing functions and added the photo taping function I had not before, should now work without depending on the sound card.

#include <GuiComboBox.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

; #VARIABLES#
Global Const $WM_CAP_START = 0x400
Global Const $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
Global Const $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
Global Const $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
Global Const $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
Global Const $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
Global Const $WM_CAP_SET_SCALE = $WM_CAP_START + 53
Global Const $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51

Global $iWebcam = 1, $FrameRate, $Vmr9 = 1, $oVideoWindow, $oVmr9, $aWebcamList[9]
Global $oBasicVideo, $oBuild, $oCapture[9], $oGraph, $oMediaControl, $oDevEnum, $oEnum
Global Const $S_OK = 0
Global Const $sIID_IMediaControl = '{56A868B1-0AD4-11CE-B03A-0020AF0BA770}'
Global Const $sCLSID_VideoInputDeviceCategory = '{860bb310-5d01-11d0-bd3b-00a0c911ce86}'
Global Const $PIN_CATEGORY_CAPTURE = '{fb6c4281-0353-11d1-905f-0000c0cc16ba}'
Global Const $PIN_CATEGORY_PREVIEW = '{fb6c4282-0353-11d1-905f-0000c0cc16ba}'
Global Const $MEDIATYPE_Video = '{73646976-0000-0010-8000-00aa00389b71}'
Global Const $sCLSID_VideoMixingRenderer9 = '{51b4abf3-748f-4e3b-a276-c828330e926a}'
Global Const $sCLSID_FilterGraph = "{E436EBB3-524F-11CE-9F53-0020AF0BA770}"
Global Const $sIID_IGraphBuilder = "{56A868A9-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIGraphBuilder = "AddFilter hresult(ptr;wstr);" & "RemoveFilter hresult(ptr);" & "EnumFilters hresult(ptr*);" & "FindFilterByName hresult(wstr;ptr*);" & "ConnectDirect hresult(ptr;ptr;ptr);" & "Reconnect hresult(ptr);" & "Disconnect hresult(ptr);" & "SetDefaultSyncSource hresult();" & "Connect hresult(ptr;ptr);" & "Render hresult(ptr);" & "RenderFile hresult(wstr;ptr);" & "AddSourceFilter hresult(wstr;wstr;ptr*);" & "SetLogFile hresult(dword_ptr);" & "Abort hresult();" & "ShouldOperationContinue hresult();"
Global Const $sCLSID_CaptureGraphBuilder2 = '{BF87B6E1-8C27-11D0-B3F0-00AA003761C5}'
Global Const $sIID_ICaptureGraphBuilder2 = '{93e5a4e0-2d50-11d2-abfa-00a0c9c6e38d}'
Global Const $tagICaptureGraphBuilder2 = "SetFiltergraph hresult(ptr);" & "GetFiltergraph hresult(ptr*);" & "SetOutputFileName hresult(clsid;wstr;ptr*;ptr*);" & "FindInterface hresult(clsid;clsid;ptr;clsid;ptr*);" & "RenderStream hresult(clsid;clsid;ptr;ptr;ptr);" & "ControlStream hresult(clsid;clsid;ptr;ptr;ptr;word;word);" & "AllocCapFile hresult(wstr;int64);" & "CopyCaptureFile hresult(wstr;wstr;int;ptr*);" & "FindPin hresult(ptr;int;clsid;clsid;bool;int;ptr*);"
Global Const $sCLSID_SystemDeviceEnum = '{62BE5D10-60EB-11d0-BD3B-00A0C911CE86}'
Global Const $sIID_ICreateDevEnum = '{29840822-5b84-11d0-bd3b-00a0c911ce86}'
Global Const $tagICreateDevEnum = "CreateClassEnumerator hresult( clsid ; ptr*; dword );"
Global Const $sIID_IEnumMoniker = '{00000102-0000-0000-c000-000000000046}'
Global Const $tagIEnumMoniker = "Next hresult(dword;ptr*;dword*);" & "Skip hresult(dword);" & "Reset hresult();" & "Clone hresult(ptr*);"
Global Const $sIID_IBaseFilter = '{56a86895-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIBaseFilter = "GetClassID hresult(ptr*);" & "Stop hresult();" & "Pause hresult();" & "Run hresult(int64);" & "GetState hresult(dword;dword*);" & "SetSyncSource hresult(ptr);" & "GetSyncSource hresult(ptr*);" & "EnumPins hresult(ptr*);" & "FindPin hresult(wstr;ptr*);" & "QueryFilterInfo hresult(ptr*);" & "JoinFilterGraph hresult(ptr;wstr);" & "QueryVendorInfo hresult(wstr*);"
Global Const $sIID_IMoniker = '{0000000f-0000-0000-C000-000000000046}'
Global Const $tagIMoniker = "GetClassID hresult( clsid )" & "IsDirty hresult(  );" & "Load hresult( ptr );" & "Save hresult( ptr, bool );" & "GetSizeMax hresult( uint64 );" & "BindToObject hresult( ptr;ptr;clsid;ptr*);" & "BindToStorage hresult( ptr;ptr;clsid;ptr*);" & "Reduce hresult( ptr;dword;ptr*;ptr*);" & "ComposeWith hresult( ptr;bool;ptr*);" & "Enum hresult( bool;ptr*);" & "IsEqual hresult( ptr);" & "Hash hresult( dword*);" & "IsRunning hresult( ptr;ptr;ptr);" & "GetTimeOfLastChange hresult( ptr;ptr;int64*);" & "Inverse hresult( ptr*);" & "CommonPrefixWith hresult( ptr;ptr*);" & "RelativePathTo hresult( ptr;ptr*);" & "GetDisplayName hresult( ptr;ptr;wstr*);" & "ParseDisplayName hresult( ptr;ptr;wstr;ulong*;ptr*);" & "IsSystemMoniker hresult( dword*);"
Global Const $tagIDispatch = "GetTypeInfoCount hresult(dword*);" & "GetTypeInfo hresult(dword;dword;ptr*);" & "GetIDsOfNames hresult(clsid;ptr;dword;dword;ptr);" & "Invoke hresult(dword;clsid;dword;word;ptr;ptr;ptr;dword*);"
Global Const $sIID_IVideoWindow = "{56A868B4-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIVideoWindow = $tagIDispatch & "put_Caption hresult(bstr);" & "get_Caption hresult(bstr*);" & "put_WindowStyle hresult(long);" & "get_WindowStyle hresult(long*);" & "put_WindowStyleEx hresult(long);" & "put_WindowStyleEx hresult(long*);" & "put_AutoShow hresult(long);" & "get_AutoShow hresult(long*);" & "put_WindowState hresult(long);" & "get_WindowState hresult(long*);" & "put_BackgroundPalette hresult(long);" & "get_BackgroundPalette hresult(long*);" & "put_Visible hresult(long);" & "get_Visible hresult(long*);" & "put_Left hresult(long);" & "get_Left hresult(long*);" & "put_Width hresult(long);" & "get_Width hresult(long*);" & "put_Top hresult(long);" & "get_Top hresult(long*);" & "put_Height hresult(long);" & "get_Height hresult(long*);" & "put_Owner hresult(long_ptr);" & "get_Owner hresult(long_ptr*);" & "put_MessageDrain hresult(long_ptr);" & "get_MessageDrain hresult(long_ptr*);" & "get_BorderColor hresult(long*);" & "put_BorderColor hresult(long);" & "get_FullScreenMode hresult(long*);" & "put_FullScreenMode hresult(long);" & "SetWindowForeground hresult(long);" & "NotifyOwnerMessage hresult(long_ptr;long;long_ptr;long_ptr);" & "SetWindowPosition hresult(long;long;long;long);" & "GetWindowPosition hresult(long*;long*;long*;long*);" & "GetMinIdealImageSize hresult(long*;long*);" & "GetMaxIdealImageSize hresult(long*;long*);" & "GetRestorePosition hresult(long*;long*;long*;long*);" & "HideCursor hresult(long);" & "IsCursorHidden hresult(long*);"
Global Const $sIID_IBasicVideo = "{56A868B5-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIBasicVideo = $tagIDispatch & "get_AvgTimePerFrame hresult(double*);" & "get_BitRate hresult(long*);" & "get_BitErrorRate hresult(long*);" & "get_VideoWidth hresult(long*);" & "get_VideoHeight hresult(long*);" & "put_SourceLeft hresult(long);" & "get_SourceLeft hresult(long*);" & "put_SourceWidth hresult(long);" & "get_SourceWidth hresult(long*);" & "put_SourceTop hresult(long);" & "get_SourceTop hresult(long*);" & "put_SourceHeight hresult(long);" & "get_SourceHeight hresult(long*);" & "put_DestinationLeft hresult(long);" & "get_DestinationLeft hresult(long*);" & "put_DestinationWidth hresult(long);" & "get_DestinationWidth hresult(long*);" & "put_DestinationTop hresult(long);" & "get_DestinationTop hresult(long*);" & "put_DestinationHeight hresult(long);" & "get_DestinationHeight hresult(long*);" & "SetSourcePosition hresult(long;long;long;long);" & "GetSourcePosition hresult(long*;long*;long*;long*);" & "SetDefaultSourcePosition hresult();" & "SetDestinationPosition hresult(long;long;long;long);" & "GetDestinationPosition hresult(long*;long*;long*;long*);" & "SetDefaultDestinationPosition hresult();" & "GetVideoSize hresult(long*;long*);" & "GetVideoPaletteEntries hresult(long;long;long*;long*);" & "GetCurrentImage hresult(long*;long*);" & "IsUsingDefaultSource hresult();" & "IsUsingDefaultDestination hresult();"
Global Const $sIID_IPropertyBag = '{55272a00-42cb-11ce-8135-00aa004bb851}'
Global Const $tagIPropertyBag = 'Read hresult(wstr;variant*;ptr*);' & 'Write hresult(wstr;variant);'
Global Const $tagIVMRWindowlessControl = 'GetNativeVideoSize hresult(long*;long*;long*;long*);' & 'GetMinIdealVideoSize hresult(long*;long*);' & 'GetMaxIdealVideoSize hresult(long*;long*);' & 'SetVideoPosition hresult(ptr;ptr);' & 'GetVideoPosition hresult(ptr*;ptr*);' & 'GetAspectRatioMode hresult(dword*);' & 'SetAspectRatioMode hresult(dword);' & 'SetVideoClippingWindow hresult(hwnd);' & 'RepaintVideo hresult(hwnd;handle);' & 'DisplayModeChanged hresult();' & 'GetCurrentImage hresult(byte*);' & 'SetBorderColor hresult(clr);' & 'GetBorderColor hresult(clr*);' & 'SetColorKey hresult(clr);' & 'GetColorKey hresult(clr*);'
Global Const $tagIVMRFilterConfig = 'SetImageCompositor hresult(ptr);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRFilterConfig9 = '{5a804648-4f66-4867-9c43-4f5c822cf1b8}'
Global Const $tagIVMRFilterConfig9 = 'SetImageCompositor hresult(ptr);' & 'SetNumberOfStreams hresult(dword);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRMixerControl9 = '{1a777eaa-47c8-4930-b2c9-8fee1c1b0f3b}'
Global Const $tagIVMRMixerControl9 = 'SetAlpha(dword;float);' & 'GetAlpha(dword;ptr*) ;' & 'SetZOrder(dword;dword) ;' & 'GetZOrder(dword;ptr*) ;' & 'SetOutputRect(dword;ptr) ;' & 'GetOutputRect(dword;ptr*) ;' & 'SetBackgroundClr(dword) ;' & 'GetBackgroundClr(ptr) ;' & 'SetMixingPrefs(dword) ;' & 'GetMixingPrefs(ptr*) ;' & 'SetProcAmpControl(dword;ptr) ;' & 'GetProcAmpControl(dword;ptr*) ;' & 'GetProcAmpControlRange(dword;ptr*);'
Global Const $sIID_IAMStreamConfig = '{c6e13340-30ac-11d0-a18c-00a0c9118956}'
Global $FrameRate = 30
; #VARIABLES#

; ================================================================================================== EXAMPLE START
Global $msg, $hGUI, $sId_snap, $idCam, $hComboCam = '', $but_snap, $sFile, $isCamera = 0, $pos_control
_WebcamDS_Init()

If @error <> 0 Or @extended <> 0 Then Exit
_CreateGui(740, 580)

_WebcamDS_RenderWebcam(1, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])

If @error <> 0 Or @extended <> 0 Then Exit

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $hComboCam
            _WebcamDS_Reset()
            $iWebcam = _GUICtrlComboBox_GetCurSel($hComboCam) + 1
            _WebcamDS_RenderWebcam($iWebcam, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])
        Case $but_snap
            ;$sFile = FileSaveDialog("Save", @ScriptDir & "\", "Image (*.bmp)", 1 + 4)
            $sFile = @ScriptDir & "\test.bmp"
            _WebcamSnap($sFile, $hGUI)
        Case $GUI_EVENT_CLOSE
            _WebcamDS_ReleaseObjects(1)
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd

Func _CreateGui($w = 640, $h = 480)
    $hGUI = GUICreate("Test Capture", $w, $h, -1, -1, $WS_POPUP)
    GUICtrlCreateLabel("Select webcam:", 115, 12, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    local $pic_image = GUICtrlCreatePic("", 45, 45, 640, 480)
    GUICtrlSetBkColor(-1, 0xff0000)
    $but_snap = GUICtrlCreateButton("Webcam snap", 585, 9, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    $pos_control = ControlGetPos("Test Capture", '', $pic_image)
    If $hComboCam = '' Then
        For $i = 1 To $aWebcamList[0]
            If $hComboCam = '' Then
                $hComboCam = GUICtrlCreateCombo($aWebcamList[$i], 220, 10, 200, 25)
            Else
                If $aWebcamList[$i] <> '' Then GUICtrlSetData($hComboCam, $aWebcamList[$i])
            EndIf
        Next
    EndIf
    GUISetState()
EndFunc   ;==>_CreateGui
; ================================================================================================== EXAMPLE END

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamSnapt
; Description ...: Save current webcam image
; Syntax.........: _WebcamSnap(Image save address, $Gui_id)
; Parameters ....: none
; Return values .: none
; Date...........: 2019/05/11
; Author ........: Belini
; Remarks .......: Call to capture image
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamSnap($snap = "", $hWnd = "")
    Local $x = $pos_control[0]
    Local $y = $pos_control[1]
    Local $w = $pos_control[2]
    Local $h = $pos_control[3]
  _ScreenCapture_CaptureWnd($snap, $hWnd,$x, $y, $w, $h, false)
EndFunc   ;==>_WebcamSnap

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_Init
; Description ...: Create Direct Show objects and enumerate webcams
; Syntax.........: _WebcamDS_Init()
; Parameters ....: none
; Return values .: Success - it fills 3 array  $aWebcamList
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Remarks .......: call it at the beginning of the script
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_Init()
    Local $hr, $aCall
    $oGraph = ObjCreateInterface($sCLSID_FilterGraph, $sIID_IGraphBuilder, $tagIGraphBuilder)
    If Not IsObj($oGraph) Then Return SetError(1, 0, 0)
    $oBuild = ObjCreateInterface($sCLSID_CaptureGraphBuilder2, $sIID_ICaptureGraphBuilder2, $tagICaptureGraphBuilder2)
    If Not IsObj($oBuild) Then Return SetError(2, 0, 0)
    $hr = $oBuild.SetFiltergraph($oGraph)
    If $hr < 0 Then Return SetError(0, 1, 0)
    Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)
    If Not IsObj($oMediaControl) Then Return SetError(3, 0, 0)
    Local $pVideoWindow
    $oGraph.QueryInterface($sIID_IVideoWindow, $pVideoWindow)
    $oVideoWindow = ObjCreateInterface($pVideoWindow, $sIID_IVideoWindow, $tagIVideoWindow)
    If Not IsObj($oVideoWindow) Then Return SetError(4, 0, 0)
    Local $pBasicVideo
    $oGraph.QueryInterface($sIID_IBasicVideo, $pBasicVideo)
    $oBasicVideo = ObjCreateInterface($pBasicVideo, $sIID_IBasicVideo, $tagIBasicVideo)
    If Not IsObj($oBasicVideo) Then Return SetError(5, 0, 0)
    If $aWebcamList[1] = '' Then
        _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory)
    EndIf
EndFunc   ;==>_WebcamDS_Init

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_ReleaseObjects
; Description ...: release Objects created during script and at the end of it
; Syntax.........: _WebcamDS_ReleaseObjects($iArg = 0)
; Parameters ....: $iArg   - use 0 if you want to delete only the current capture obj, otherwise (ie: at the end) put it to 1
; Return values .: Success - it deletes the objects created
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_ReleaseObjects($iArg = 0)
    $oMediaControl.Stop()
    $oMediaControl = 0
    $oVmr9 = 0
    $oCapture[$iWebcam] = 0
    If $iArg Then $oCapture = 0
    $oBasicVideo = 0
    $oVideoWindow = 0
    $oGraph = 0
    $oBuild = 0
EndFunc   ;==>_WebcamDS_ReleaseObjects

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_RenderWebcam
; Description ...: call it to set which webcam to display, audio preview, resolution, scale to GUI, zoom
; Syntax.........: _WebcamDS_RenderWebcam($iNumberWebcam=1,$iAudioMic=0,$hGuiWin[,$iScale=1[,$xVideo=640[,$yVideo=480[,$ibpp=16[,$iZoomX=0[,$iZoomY=0[,$iZoomW=0[,$iZoomH=0]]]]]]]])
; Parameters ....: $iNumberWebcam - The webcam to display (from 1)
;                  $hGuiWin     - The GUI you create to display the webcam
;                  $iScale      - if you want to scale the webcam resolution to the res of the GUI, set it to 1, otherwise to 0
;                  $xVideo      - The webcam Width
;                  $yVideo      - The webcam Height
;                  $left        - The left side of image in Gui
;                  $top         - The top side of image in Gui
; Return values .: Success - it displays the webcam selected
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_RenderWebcam($iNumberWebcam, $hGuiWin, $iScale = 1, $xVideo = 640, $yVideo = 480, $left = 0, $top = 0)
    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory, $iNumberWebcam)
    Local $_GUID = "DWORD Data1;" & "WORD  Data2;" & "WORD  Data3;" & "BYTE  Data4[8];"
    Local $_SIZE = "LONG cx;" & "LONG cy;"
    Local $_VIDEO_STREAM_CONFIG_CAPS = $_GUID & "ULONG    VideoStandard;" & $_SIZE & $_SIZE & $_SIZE & "int      CropGranularityX;" & "int      CropGranularityY;" & "int      CropAlignX;" & "int      CropAlignY;" & $_SIZE & $_SIZE & "int      OutputGranularityX;" & "int      OutputGranularityY;" & "int      StretchTapsX;" & "int      StretchTapsY;" & "int      ShrinkTapsX;" & "int      ShrinkTapsY;" & "int64     MinFrameInterval;" & "int64  MaxFrameInterval;" & "LONG     MinBitsPerSecond;" & "LONG     MaxBitsPerSecond;"
    Local $tVideoStream = DllStructCreate($_VIDEO_STREAM_CONFIG_CAPS)
    Local $_AM_MEDIA = $_GUID & $_GUID & 'BOOL  bFixedSizeSamples;' & 'BOOL  bTemporalCompression;' & 'ULONG    lSampleSize;' & $_GUID & 'ptr pUnk;' & 'ULONG    cbFormat;' & 'ptr  pbFormat;'
    Local $t_AM_MEDIA = DllStructCreate($_AM_MEDIA)
    Local $_RECT = 'LONG left;' & 'LONG top;' & 'LONG right;' & 'LONG bottom;'
    Local $_tagBITMAPINFOHEADER = 'DWORD biSize;' & 'LONG  biWidth;' & 'LONG  biHeight;' & 'WORD  biPlanes;' & 'WORD  biBitCount;' & 'DWORD biCompression;' & 'DWORD biSizeImage;' & 'LONG  biXPelsPerMeter;' & 'LONG  biYPelsPerMeter;' & 'DWORD biClrUsed;' & 'DWORD biClrImportant;'
    Local $_tagVIDEOINFOHEADER = $_RECT & $_RECT & 'DWORD            dwBitRate;' & 'DWORD            dwBitErrorRate;' & 'int64   AvgTimePerFrame;' & $_tagBITMAPINFOHEADER
    Local $_PinInfo = 'ptr   pFilter;' & 'int64    dir;' & 'wchar    achName[128];'
    Local $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], $sIID_IAMStreamConfig, 0)
    Local $iCount = 0, $iSize = 0
    If $hr < 0 Then Return SetError(1, 7, 0)
    If DllStructGetSize($tVideoStream) == $iSize Then
        For $iFormat = 0 To $iCount - 1 Step 1
            Local $scc = DllStructGetPtr($tVideoStream)
            Local $pmtConfig = DllStructGetPtr($t_AM_MEDIA)
            Local $t_AM_MEDIA_TYPE = DllStructCreate($_AM_MEDIA, $pmtConfig)
            Local $pbFormat = DllStructGetData($t_AM_MEDIA_TYPE, "pbFormat")
            Local $t_VIDEOINFOHEADER = DllStructCreate($_tagVIDEOINFOHEADER, $pbFormat)
            Local $videoWidth = DllStructGetData($t_VIDEOINFOHEADER, 'biWidth')
            Local $videoHeight = DllStructGetData($t_VIDEOINFOHEADER, 'biHeight')
            Local $bitCount = DllStructGetData($t_VIDEOINFOHEADER, 'biBitCount')
            Local $dwBitRate = DllStructGetData($t_VIDEOINFOHEADER, 'dwBitRate')
            Local $biCompression = DllStructGetData($t_VIDEOINFOHEADER, 'biCompression')
            Local $biPlanes = DllStructGetData($t_VIDEOINFOHEADER, 'biPlanes')
            Local $biSize = DllStructGetData($t_VIDEOINFOHEADER, 'biSizeImage')
            Local $AvgframeRate = Round(10000000 / DllStructGetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame'))
            Local $sText = ''
            For $i = 7 To 0 Step -2
                If $biCompression <> 0 Then $sText = $sText & Chr(Dec(StringMid(Hex($biCompression, 8), $i, 2)))
            Next
            DllStructSetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame', 10000000 / $FrameRate)
            If $iFormat = $iCount - 1 Then
                DllStructSetData($t_VIDEOINFOHEADER, 'biWidth', $xVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biHeight', $yVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biBitCount', 16)
                If $hr < 0 Then Return SetError(1, 9, 0)
            EndIf
        Next
    EndIf
    If $Vmr9 = 0 Then
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, 0)
        If $hr < 0 Then Return SetError(1, 10, 0)
    Else
        $oVmr9 = ObjCreateInterface($sCLSID_VideoMixingRenderer9, $sIID_IBaseFilter, $tagIBaseFilter)
        If Not IsObj($oVmr9) Then Return SetError(14, 0, 0)
        $hr = $oGraph.AddFilter($oVmr9, "VMR9")
        If $hr < 0 Then Return SetError(1, 11, 0)
        Local $pConfig9, $oConfig9
        $hr = $oVmr9.QueryInterface($sIID_IVMRFilterConfig9, $pConfig9)
        $oConfig9 = ObjCreateInterface($pConfig9, $sIID_IVMRFilterConfig9, $tagIVMRFilterConfig9)
        If Not IsObj($oConfig9) Then Return SetError(15, 0, 0)
        ConsoleWrite('Filterconf hr:' & $hr & '  ' & IsObj($oConfig9) & @CRLF)
        Local $str = ''
        $hr = $oConfig9.SetNumberOfStreams(4)
        If $hr < 0 Then Return SetError(1, 12, 0)
        Local $pMixControl9, $oMixControl9
        $hr = $oVmr9.QueryInterface($sIID_IVMRMixerControl9, $pMixControl9)
        $oMixControl9 = ObjCreateInterface($pMixControl9, $sIID_IVMRMixerControl9, $tagIVMRMixerControl9)
        If Not IsObj($oMixControl9) Then Return SetError(16, 0, 0)
        Local $t_VMR9rect = DllStructCreate($_RECT)
        Local $l = 0, $t = 0, $r = $l + $xVideo, $b = $t + $yVideo
        DllStructSetData($t_VMR9rect, 'left', $l)
        DllStructSetData($t_VMR9rect, 'top', $t)
        DllStructSetData($t_VMR9rect, 'right', $r)
        DllStructSetData($t_VMR9rect, 'bottom', $b)
        Local $streamID = 0
        $hr = $oMixControl9.SetOutputRect($streamID, DllStructGetPtr($t_VMR9rect))
        If $hr < 0 Then Return SetError(1, 13, 0)
        $oConfig9 = 0
        $oMixControl9 = 0
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, $oVmr9)
        If $hr < 0 Then Return SetError(1, 14, 0)
        $oVmr9 = 0
    EndIf
    Local $iX = $left, $iY = $top
    Local $aClientSize = WinGetClientSize($hGuiWin)
    If $iScale = 1 Then
        $xVideo = $aClientSize[0]
        $yVideo = $aClientSize[1]
    EndIf
    $hr = $oVideoWindow.SetWindowPosition($iX, $iY, $xVideo, $yVideo)
    If $hr < 0 Then Return SetError(1, 16, 0)
    $hr = $oVideoWindow.put_Owner($hGuiWin)
    If $hr < 0 Then Return SetError(1, 17, 0)
    $oVideoWindow.put_WindowStyle(BitOR($WS_CHILD, $WS_CLIPCHILDREN))
    If $hr < 0 Then Return SetError(1, 18, 0)
    $hr = $oMediaControl.Run()
    If $hr < 0 Then Return SetError(1, 19, 0)
EndFunc   ;==>_WebcamDS_RenderWebcam

; #INTERNAL_NO_DOC# ===================================================================================================
Func _WebcamDS_EnumerateDevices($CLSID_category, $argument = 99)
    $oDevEnum = ObjCreateInterface($sCLSID_SystemDeviceEnum, $sIID_ICreateDevEnum, $tagICreateDevEnum)

    If Not IsObj($oDevEnum) Then Return SetError(6, 0, 0)
    Local $pEnum = 0
    $oDevEnum.CreateClassEnumerator($CLSID_category, $pEnum, 0)
    $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumMoniker, $tagIEnumMoniker)

    If Not IsObj($oEnum) Then Return SetError(7, 0, 0)
    Local $iPosition = '', $arg = ''
    If StringInStr($CLSID_category, "860bb310") Or StringInStr($CLSID_category, "33d9a762") Then
        $iPosition = $argument
        $arg = ''
    Else
        $iPosition = ''
        $arg = $argument
    EndIf
    Local $pMoniker, $oMoniker, $hr
    Local $i = 1, $pBindObj[99]
    While $oEnum.Next(1, $pMoniker, 0) = $S_OK
        Local $pPropBag, $oPropBag
        $oMoniker = ObjCreateInterface($pMoniker, $sIID_IMoniker, $tagIMoniker)
        If Not IsObj($oMoniker) Then Return SetError(8, 0, 0)
        $hr = $oMoniker.BindToObject(0, 0, $sIID_IBaseFilter, $pBindObj[$i])
        If $hr < 0 Then ExitLoop
        $hr = $oMoniker.BindToStorage(0, 0, $sIID_IPropertyBag, $pPropBag)
        If $hr < 0 Then Return SetError(1, 2, 0)
        $oPropBag = ObjCreateInterface($pPropBag, $sIID_IPropertyBag, $tagIPropertyBag)
        If Not IsObj($oPropBag) Then Return SetError(9, 0, 0)
        Local $var = ''
        $hr = $oPropBag.Read("FriendlyName", $var, 0)
        If $hr < 0 Then Return SetError(1, 3, 0)
        If $iPosition = 99 Then
            If $var <> '' Then
                $aWebcamList[$i] = $var
            EndIf
            Local $oCaptureTemp = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
            $oCaptureTemp = 0
        EndIf
        If ($iPosition <> '' And $iPosition = $i) Or ($arg <> '' And $var = $arg) Then
            If ($iPosition <> '' And $iPosition = $i) Then
                If Not IsObj($oCapture[$iPosition]) Then
                    $oCapture[$iPosition] = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                    If Not IsObj($oCapture[$iPosition]) Then Return SetError(12, 0, 0)
                EndIf
                $hr = $oGraph.AddFilter($oCapture[$iPosition], "CaptureFilter" & $iWebcam)
                If $hr < 0 Then Return SetError(1, 6, 0)
                WinSetTitle($hGUI, '', $var)
                ExitLoop
            EndIf
        EndIf
        $oPropBag = 0
        $i += 1
    WEnd
    If $iPosition = 99 And StringInStr($CLSID_category, "860bb310") Then $aWebcamList[0] = $i - 1
    $oMoniker = 0
    $oEnum = 0
    $oDevEnum = 0
EndFunc   ;==>_WebcamDS_EnumerateDevices

Func _WebcamDS_Reset()
    _WebcamDS_ReleaseObjects()
    _WebcamDS_Init()
EndFunc   ;==>_WebcamDS_Reset
; #INTERNAL_NO_DOC# ===================================================================================================

 

Edited by Belini
Link to comment
Share on other sites

On 5/12/2019 at 4:46 AM, Belini said:

@Qwerty212 I took out the audio and video capturing functions and added the photo taping function I had not before, should now work without depending on the sound card.

#include <GuiComboBox.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

; #VARIABLES#
Global Const $WM_CAP_START = 0x400
Global Const $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
Global Const $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
Global Const $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
Global Const $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
Global Const $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
Global Const $WM_CAP_SET_SCALE = $WM_CAP_START + 53
Global Const $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51

Global $iWebcam = 1, $FrameRate, $Vmr9 = 1, $oVideoWindow, $oVmr9, $aWebcamList[9]
Global $oBasicVideo, $oBuild, $oCapture[9], $oGraph, $oMediaControl, $oDevEnum, $oEnum
Global Const $S_OK = 0
Global Const $sIID_IMediaControl = '{56A868B1-0AD4-11CE-B03A-0020AF0BA770}'
Global Const $sCLSID_VideoInputDeviceCategory = '{860bb310-5d01-11d0-bd3b-00a0c911ce86}'
Global Const $PIN_CATEGORY_CAPTURE = '{fb6c4281-0353-11d1-905f-0000c0cc16ba}'
Global Const $PIN_CATEGORY_PREVIEW = '{fb6c4282-0353-11d1-905f-0000c0cc16ba}'
Global Const $MEDIATYPE_Video = '{73646976-0000-0010-8000-00aa00389b71}'
Global Const $sCLSID_VideoMixingRenderer9 = '{51b4abf3-748f-4e3b-a276-c828330e926a}'
Global Const $sCLSID_FilterGraph = "{E436EBB3-524F-11CE-9F53-0020AF0BA770}"
Global Const $sIID_IGraphBuilder = "{56A868A9-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIGraphBuilder = "AddFilter hresult(ptr;wstr);" & "RemoveFilter hresult(ptr);" & "EnumFilters hresult(ptr*);" & "FindFilterByName hresult(wstr;ptr*);" & "ConnectDirect hresult(ptr;ptr;ptr);" & "Reconnect hresult(ptr);" & "Disconnect hresult(ptr);" & "SetDefaultSyncSource hresult();" & "Connect hresult(ptr;ptr);" & "Render hresult(ptr);" & "RenderFile hresult(wstr;ptr);" & "AddSourceFilter hresult(wstr;wstr;ptr*);" & "SetLogFile hresult(dword_ptr);" & "Abort hresult();" & "ShouldOperationContinue hresult();"
Global Const $sCLSID_CaptureGraphBuilder2 = '{BF87B6E1-8C27-11D0-B3F0-00AA003761C5}'
Global Const $sIID_ICaptureGraphBuilder2 = '{93e5a4e0-2d50-11d2-abfa-00a0c9c6e38d}'
Global Const $tagICaptureGraphBuilder2 = "SetFiltergraph hresult(ptr);" & "GetFiltergraph hresult(ptr*);" & "SetOutputFileName hresult(clsid;wstr;ptr*;ptr*);" & "FindInterface hresult(clsid;clsid;ptr;clsid;ptr*);" & "RenderStream hresult(clsid;clsid;ptr;ptr;ptr);" & "ControlStream hresult(clsid;clsid;ptr;ptr;ptr;word;word);" & "AllocCapFile hresult(wstr;int64);" & "CopyCaptureFile hresult(wstr;wstr;int;ptr*);" & "FindPin hresult(ptr;int;clsid;clsid;bool;int;ptr*);"
Global Const $sCLSID_SystemDeviceEnum = '{62BE5D10-60EB-11d0-BD3B-00A0C911CE86}'
Global Const $sIID_ICreateDevEnum = '{29840822-5b84-11d0-bd3b-00a0c911ce86}'
Global Const $tagICreateDevEnum = "CreateClassEnumerator hresult( clsid ; ptr*; dword );"
Global Const $sIID_IEnumMoniker = '{00000102-0000-0000-c000-000000000046}'
Global Const $tagIEnumMoniker = "Next hresult(dword;ptr*;dword*);" & "Skip hresult(dword);" & "Reset hresult();" & "Clone hresult(ptr*);"
Global Const $sIID_IBaseFilter = '{56a86895-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIBaseFilter = "GetClassID hresult(ptr*);" & "Stop hresult();" & "Pause hresult();" & "Run hresult(int64);" & "GetState hresult(dword;dword*);" & "SetSyncSource hresult(ptr);" & "GetSyncSource hresult(ptr*);" & "EnumPins hresult(ptr*);" & "FindPin hresult(wstr;ptr*);" & "QueryFilterInfo hresult(ptr*);" & "JoinFilterGraph hresult(ptr;wstr);" & "QueryVendorInfo hresult(wstr*);"
Global Const $sIID_IMoniker = '{0000000f-0000-0000-C000-000000000046}'
Global Const $tagIMoniker = "GetClassID hresult( clsid )" & "IsDirty hresult(  );" & "Load hresult( ptr );" & "Save hresult( ptr, bool );" & "GetSizeMax hresult( uint64 );" & "BindToObject hresult( ptr;ptr;clsid;ptr*);" & "BindToStorage hresult( ptr;ptr;clsid;ptr*);" & "Reduce hresult( ptr;dword;ptr*;ptr*);" & "ComposeWith hresult( ptr;bool;ptr*);" & "Enum hresult( bool;ptr*);" & "IsEqual hresult( ptr);" & "Hash hresult( dword*);" & "IsRunning hresult( ptr;ptr;ptr);" & "GetTimeOfLastChange hresult( ptr;ptr;int64*);" & "Inverse hresult( ptr*);" & "CommonPrefixWith hresult( ptr;ptr*);" & "RelativePathTo hresult( ptr;ptr*);" & "GetDisplayName hresult( ptr;ptr;wstr*);" & "ParseDisplayName hresult( ptr;ptr;wstr;ulong*;ptr*);" & "IsSystemMoniker hresult( dword*);"
Global Const $tagIDispatch = "GetTypeInfoCount hresult(dword*);" & "GetTypeInfo hresult(dword;dword;ptr*);" & "GetIDsOfNames hresult(clsid;ptr;dword;dword;ptr);" & "Invoke hresult(dword;clsid;dword;word;ptr;ptr;ptr;dword*);"
Global Const $sIID_IVideoWindow = "{56A868B4-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIVideoWindow = $tagIDispatch & "put_Caption hresult(bstr);" & "get_Caption hresult(bstr*);" & "put_WindowStyle hresult(long);" & "get_WindowStyle hresult(long*);" & "put_WindowStyleEx hresult(long);" & "put_WindowStyleEx hresult(long*);" & "put_AutoShow hresult(long);" & "get_AutoShow hresult(long*);" & "put_WindowState hresult(long);" & "get_WindowState hresult(long*);" & "put_BackgroundPalette hresult(long);" & "get_BackgroundPalette hresult(long*);" & "put_Visible hresult(long);" & "get_Visible hresult(long*);" & "put_Left hresult(long);" & "get_Left hresult(long*);" & "put_Width hresult(long);" & "get_Width hresult(long*);" & "put_Top hresult(long);" & "get_Top hresult(long*);" & "put_Height hresult(long);" & "get_Height hresult(long*);" & "put_Owner hresult(long_ptr);" & "get_Owner hresult(long_ptr*);" & "put_MessageDrain hresult(long_ptr);" & "get_MessageDrain hresult(long_ptr*);" & "get_BorderColor hresult(long*);" & "put_BorderColor hresult(long);" & "get_FullScreenMode hresult(long*);" & "put_FullScreenMode hresult(long);" & "SetWindowForeground hresult(long);" & "NotifyOwnerMessage hresult(long_ptr;long;long_ptr;long_ptr);" & "SetWindowPosition hresult(long;long;long;long);" & "GetWindowPosition hresult(long*;long*;long*;long*);" & "GetMinIdealImageSize hresult(long*;long*);" & "GetMaxIdealImageSize hresult(long*;long*);" & "GetRestorePosition hresult(long*;long*;long*;long*);" & "HideCursor hresult(long);" & "IsCursorHidden hresult(long*);"
Global Const $sIID_IBasicVideo = "{56A868B5-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIBasicVideo = $tagIDispatch & "get_AvgTimePerFrame hresult(double*);" & "get_BitRate hresult(long*);" & "get_BitErrorRate hresult(long*);" & "get_VideoWidth hresult(long*);" & "get_VideoHeight hresult(long*);" & "put_SourceLeft hresult(long);" & "get_SourceLeft hresult(long*);" & "put_SourceWidth hresult(long);" & "get_SourceWidth hresult(long*);" & "put_SourceTop hresult(long);" & "get_SourceTop hresult(long*);" & "put_SourceHeight hresult(long);" & "get_SourceHeight hresult(long*);" & "put_DestinationLeft hresult(long);" & "get_DestinationLeft hresult(long*);" & "put_DestinationWidth hresult(long);" & "get_DestinationWidth hresult(long*);" & "put_DestinationTop hresult(long);" & "get_DestinationTop hresult(long*);" & "put_DestinationHeight hresult(long);" & "get_DestinationHeight hresult(long*);" & "SetSourcePosition hresult(long;long;long;long);" & "GetSourcePosition hresult(long*;long*;long*;long*);" & "SetDefaultSourcePosition hresult();" & "SetDestinationPosition hresult(long;long;long;long);" & "GetDestinationPosition hresult(long*;long*;long*;long*);" & "SetDefaultDestinationPosition hresult();" & "GetVideoSize hresult(long*;long*);" & "GetVideoPaletteEntries hresult(long;long;long*;long*);" & "GetCurrentImage hresult(long*;long*);" & "IsUsingDefaultSource hresult();" & "IsUsingDefaultDestination hresult();"
Global Const $sIID_IPropertyBag = '{55272a00-42cb-11ce-8135-00aa004bb851}'
Global Const $tagIPropertyBag = 'Read hresult(wstr;variant*;ptr*);' & 'Write hresult(wstr;variant);'
Global Const $tagIVMRWindowlessControl = 'GetNativeVideoSize hresult(long*;long*;long*;long*);' & 'GetMinIdealVideoSize hresult(long*;long*);' & 'GetMaxIdealVideoSize hresult(long*;long*);' & 'SetVideoPosition hresult(ptr;ptr);' & 'GetVideoPosition hresult(ptr*;ptr*);' & 'GetAspectRatioMode hresult(dword*);' & 'SetAspectRatioMode hresult(dword);' & 'SetVideoClippingWindow hresult(hwnd);' & 'RepaintVideo hresult(hwnd;handle);' & 'DisplayModeChanged hresult();' & 'GetCurrentImage hresult(byte*);' & 'SetBorderColor hresult(clr);' & 'GetBorderColor hresult(clr*);' & 'SetColorKey hresult(clr);' & 'GetColorKey hresult(clr*);'
Global Const $tagIVMRFilterConfig = 'SetImageCompositor hresult(ptr);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRFilterConfig9 = '{5a804648-4f66-4867-9c43-4f5c822cf1b8}'
Global Const $tagIVMRFilterConfig9 = 'SetImageCompositor hresult(ptr);' & 'SetNumberOfStreams hresult(dword);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRMixerControl9 = '{1a777eaa-47c8-4930-b2c9-8fee1c1b0f3b}'
Global Const $tagIVMRMixerControl9 = 'SetAlpha(dword;float);' & 'GetAlpha(dword;ptr*) ;' & 'SetZOrder(dword;dword) ;' & 'GetZOrder(dword;ptr*) ;' & 'SetOutputRect(dword;ptr) ;' & 'GetOutputRect(dword;ptr*) ;' & 'SetBackgroundClr(dword) ;' & 'GetBackgroundClr(ptr) ;' & 'SetMixingPrefs(dword) ;' & 'GetMixingPrefs(ptr*) ;' & 'SetProcAmpControl(dword;ptr) ;' & 'GetProcAmpControl(dword;ptr*) ;' & 'GetProcAmpControlRange(dword;ptr*);'
Global Const $sIID_IAMStreamConfig = '{c6e13340-30ac-11d0-a18c-00a0c9118956}'
Global $avicap = DllOpen("avicap32.dll"), $UserDLL = DllOpen("user32.dll")
Global $FrameRate = 30
; #VARIABLES#

; ================================================================================================== EXAMPLE START
Global $msg, $hGUI, $sId_snap, $idCam, $hComboCam = '', $but_snap, $sFile, $isCamera = 0, $pos_control
_WebcamDS_Init()

If @error <> 0 Or @extended <> 0 Then Exit
_CreateGui(740, 580)

_WebcamDS_RenderWebcam(1, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])

If @error <> 0 Or @extended <> 0 Then Exit

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $hComboCam
            _WebcamDS_Reset()
            $iWebcam = _GUICtrlComboBox_GetCurSel($hComboCam) + 1
            _WebcamDS_RenderWebcam($iWebcam, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])
        Case $but_snap
            ;$sFile = FileSaveDialog("Save", @ScriptDir & "\", "Image (*.bmp)", 1 + 4)
            $sFile = @ScriptDir & "\test.bmp"
            _WebcamSnap($sFile, $hGUI)
        Case $GUI_EVENT_CLOSE
            _WebcamDS_ReleaseObjects(1)
            DllClose($UserDLL)
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd

Func _CreateGui($w = 640, $h = 480)
    $hGUI = GUICreate("Test Capture", $w, $h, -1, -1, $WS_POPUP)
    GUICtrlCreateLabel("Select webcam:", 115, 12, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    local $pic_image = GUICtrlCreatePic("", 45, 45, 640, 480)
    GUICtrlSetBkColor(-1, 0xff0000)
    $but_snap = GUICtrlCreateButton("Webcam snap", 585, 9, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    $pos_control = ControlGetPos("Test Capture", '', $pic_image)
    If $hComboCam = '' Then
        For $i = 1 To $aWebcamList[0]
            If $hComboCam = '' Then
                $hComboCam = GUICtrlCreateCombo($aWebcamList[$i], 220, 10, 200, 25)
            Else
                If $aWebcamList[$i] <> '' Then GUICtrlSetData($hComboCam, $aWebcamList[$i])
            EndIf
        Next
    EndIf
    GUISetState()
EndFunc   ;==>_CreateGui
; ================================================================================================== EXAMPLE END

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamSnapt
; Description ...: Save current webcam image
; Syntax.........: _WebcamSnap(Image save address, $Gui_id)
; Parameters ....: none
; Return values .: none
; Date...........: 2019/05/11
; Author ........: Belini
; Remarks .......: Call to capture image
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamSnap($snap = "", $hWnd = "")
    Local $x = $pos_control[0]
    Local $y = $pos_control[1]
    Local $w = $pos_control[2]
    Local $h = $pos_control[3]
  _ScreenCapture_CaptureWnd($snap, $hWnd,$x, $y, $w, $h, false)
EndFunc   ;==>_WebcamSnap

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_Init
; Description ...: Create Direct Show objects and enumerate webcams
; Syntax.........: _WebcamDS_Init()
; Parameters ....: none
; Return values .: Success - it fills 3 array  $aWebcamList
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Remarks .......: call it at the beginning of the script
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_Init()
    Local $hr, $aCall
    $oGraph = ObjCreateInterface($sCLSID_FilterGraph, $sIID_IGraphBuilder, $tagIGraphBuilder)
    If Not IsObj($oGraph) Then Return SetError(1, 0, 0)
    $oBuild = ObjCreateInterface($sCLSID_CaptureGraphBuilder2, $sIID_ICaptureGraphBuilder2, $tagICaptureGraphBuilder2)
    If Not IsObj($oBuild) Then Return SetError(2, 0, 0)
    $hr = $oBuild.SetFiltergraph($oGraph)
    If $hr < 0 Then Return SetError(0, 1, 0)
    Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)
    If Not IsObj($oMediaControl) Then Return SetError(3, 0, 0)
    Local $pVideoWindow
    $oGraph.QueryInterface($sIID_IVideoWindow, $pVideoWindow)
    $oVideoWindow = ObjCreateInterface($pVideoWindow, $sIID_IVideoWindow, $tagIVideoWindow)
    If Not IsObj($oVideoWindow) Then Return SetError(4, 0, 0)
    Local $pBasicVideo
    $oGraph.QueryInterface($sIID_IBasicVideo, $pBasicVideo)
    $oBasicVideo = ObjCreateInterface($pBasicVideo, $sIID_IBasicVideo, $tagIBasicVideo)
    If Not IsObj($oBasicVideo) Then Return SetError(5, 0, 0)
    If $aWebcamList[1] = '' Then
        _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory)
    EndIf
EndFunc   ;==>_WebcamDS_Init

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_ReleaseObjects
; Description ...: release Objects created during script and at the end of it
; Syntax.........: _WebcamDS_ReleaseObjects($iArg = 0)
; Parameters ....: $iArg   - use 0 if you want to delete only the current capture obj, otherwise (ie: at the end) put it to 1
; Return values .: Success - it deletes the objects created
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_ReleaseObjects($iArg = 0)
    $oMediaControl.Stop()
    $oMediaControl = 0
    $oVmr9 = 0
    $oCapture[$iWebcam] = 0
    If $iArg Then $oCapture = 0
    $oBasicVideo = 0
    $oVideoWindow = 0
    $oGraph = 0
    $oBuild = 0
EndFunc   ;==>_WebcamDS_ReleaseObjects

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_RenderWebcam
; Description ...: call it to set which webcam to display, audio preview, resolution, scale to GUI, zoom
; Syntax.........: _WebcamDS_RenderWebcam($iNumberWebcam=1,$iAudioMic=0,$hGuiWin[,$iScale=1[,$xVideo=640[,$yVideo=480[,$ibpp=16[,$iZoomX=0[,$iZoomY=0[,$iZoomW=0[,$iZoomH=0]]]]]]]])
; Parameters ....: $iNumberWebcam - The webcam to display (from 1)
;                  $hGuiWin     - The GUI you create to display the webcam
;                  $iScale      - if you want to scale the webcam resolution to the res of the GUI, set it to 1, otherwise to 0
;                  $xVideo      - The webcam Width
;                  $yVideo      - The webcam Height
;                  $left        - The left side of image in Gui
;                  $top         - The top side of image in Gui
; Return values .: Success - it displays the webcam selected
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_RenderWebcam($iNumberWebcam, $hGuiWin, $iScale = 1, $xVideo = 640, $yVideo = 480, $left = 0, $top = 0)
    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory, $iNumberWebcam)
    Local $_GUID = "DWORD Data1;" & "WORD  Data2;" & "WORD  Data3;" & "BYTE  Data4[8];"
    Local $_SIZE = "LONG cx;" & "LONG cy;"
    Local $_VIDEO_STREAM_CONFIG_CAPS = $_GUID & "ULONG    VideoStandard;" & $_SIZE & $_SIZE & $_SIZE & "int      CropGranularityX;" & "int      CropGranularityY;" & "int      CropAlignX;" & "int      CropAlignY;" & $_SIZE & $_SIZE & "int      OutputGranularityX;" & "int      OutputGranularityY;" & "int      StretchTapsX;" & "int      StretchTapsY;" & "int      ShrinkTapsX;" & "int      ShrinkTapsY;" & "int64     MinFrameInterval;" & "int64  MaxFrameInterval;" & "LONG     MinBitsPerSecond;" & "LONG     MaxBitsPerSecond;"
    Local $tVideoStream = DllStructCreate($_VIDEO_STREAM_CONFIG_CAPS)
    Local $_AM_MEDIA = $_GUID & $_GUID & 'BOOL  bFixedSizeSamples;' & 'BOOL  bTemporalCompression;' & 'ULONG    lSampleSize;' & $_GUID & 'ptr pUnk;' & 'ULONG    cbFormat;' & 'ptr  pbFormat;'
    Local $t_AM_MEDIA = DllStructCreate($_AM_MEDIA)
    Local $_RECT = 'LONG left;' & 'LONG top;' & 'LONG right;' & 'LONG bottom;'
    Local $_tagBITMAPINFOHEADER = 'DWORD biSize;' & 'LONG  biWidth;' & 'LONG  biHeight;' & 'WORD  biPlanes;' & 'WORD  biBitCount;' & 'DWORD biCompression;' & 'DWORD biSizeImage;' & 'LONG  biXPelsPerMeter;' & 'LONG  biYPelsPerMeter;' & 'DWORD biClrUsed;' & 'DWORD biClrImportant;'
    Local $_tagVIDEOINFOHEADER = $_RECT & $_RECT & 'DWORD            dwBitRate;' & 'DWORD            dwBitErrorRate;' & 'int64   AvgTimePerFrame;' & $_tagBITMAPINFOHEADER
    Local $_PinInfo = 'ptr   pFilter;' & 'int64    dir;' & 'wchar    achName[128];'
    Local $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], $sIID_IAMStreamConfig, 0)
    Local $iCount = 0, $iSize = 0
    If $hr < 0 Then Return SetError(1, 7, 0)
    If DllStructGetSize($tVideoStream) == $iSize Then
        For $iFormat = 0 To $iCount - 1 Step 1
            Local $scc = DllStructGetPtr($tVideoStream)
            Local $pmtConfig = DllStructGetPtr($t_AM_MEDIA)
            Local $t_AM_MEDIA_TYPE = DllStructCreate($_AM_MEDIA, $pmtConfig)
            Local $pbFormat = DllStructGetData($t_AM_MEDIA_TYPE, "pbFormat")
            Local $t_VIDEOINFOHEADER = DllStructCreate($_tagVIDEOINFOHEADER, $pbFormat)
            Local $videoWidth = DllStructGetData($t_VIDEOINFOHEADER, 'biWidth')
            Local $videoHeight = DllStructGetData($t_VIDEOINFOHEADER, 'biHeight')
            Local $bitCount = DllStructGetData($t_VIDEOINFOHEADER, 'biBitCount')
            Local $dwBitRate = DllStructGetData($t_VIDEOINFOHEADER, 'dwBitRate')
            Local $biCompression = DllStructGetData($t_VIDEOINFOHEADER, 'biCompression')
            Local $biPlanes = DllStructGetData($t_VIDEOINFOHEADER, 'biPlanes')
            Local $biSize = DllStructGetData($t_VIDEOINFOHEADER, 'biSizeImage')
            Local $AvgframeRate = Round(10000000 / DllStructGetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame'))
            Local $sText = ''
            For $i = 7 To 0 Step -2
                If $biCompression <> 0 Then $sText = $sText & Chr(Dec(StringMid(Hex($biCompression, 8), $i, 2)))
            Next
            DllStructSetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame', 10000000 / $FrameRate)
            If $iFormat = $iCount - 1 Then
                DllStructSetData($t_VIDEOINFOHEADER, 'biWidth', $xVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biHeight', $yVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biBitCount', 16)
                If $hr < 0 Then Return SetError(1, 9, 0)
            EndIf
        Next
    EndIf
    If $Vmr9 = 0 Then
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, 0)
        If $hr < 0 Then Return SetError(1, 10, 0)
    Else
        $oVmr9 = ObjCreateInterface($sCLSID_VideoMixingRenderer9, $sIID_IBaseFilter, $tagIBaseFilter)
        If Not IsObj($oVmr9) Then Return SetError(14, 0, 0)
        $hr = $oGraph.AddFilter($oVmr9, "VMR9")
        If $hr < 0 Then Return SetError(1, 11, 0)
        Local $pConfig9, $oConfig9
        $hr = $oVmr9.QueryInterface($sIID_IVMRFilterConfig9, $pConfig9)
        $oConfig9 = ObjCreateInterface($pConfig9, $sIID_IVMRFilterConfig9, $tagIVMRFilterConfig9)
        If Not IsObj($oConfig9) Then Return SetError(15, 0, 0)
        ConsoleWrite('Filterconf hr:' & $hr & '  ' & IsObj($oConfig9) & @CRLF)
        Local $str = ''
        $hr = $oConfig9.SetNumberOfStreams(4)
        If $hr < 0 Then Return SetError(1, 12, 0)
        Local $pMixControl9, $oMixControl9
        $hr = $oVmr9.QueryInterface($sIID_IVMRMixerControl9, $pMixControl9)
        $oMixControl9 = ObjCreateInterface($pMixControl9, $sIID_IVMRMixerControl9, $tagIVMRMixerControl9)
        If Not IsObj($oMixControl9) Then Return SetError(16, 0, 0)
        Local $t_VMR9rect = DllStructCreate($_RECT)
        Local $l = 0, $t = 0, $r = $l + $xVideo, $b = $t + $yVideo
        DllStructSetData($t_VMR9rect, 'left', $l)
        DllStructSetData($t_VMR9rect, 'top', $t)
        DllStructSetData($t_VMR9rect, 'right', $r)
        DllStructSetData($t_VMR9rect, 'bottom', $b)
        Local $streamID = 0
        $hr = $oMixControl9.SetOutputRect($streamID, DllStructGetPtr($t_VMR9rect))
        If $hr < 0 Then Return SetError(1, 13, 0)
        $oConfig9 = 0
        $oMixControl9 = 0
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, $oVmr9)
        If $hr < 0 Then Return SetError(1, 14, 0)
        $oVmr9 = 0
    EndIf
    Local $iX = $left, $iY = $top
    Local $aClientSize = WinGetClientSize($hGuiWin)
    If $iScale = 1 Then
        $xVideo = $aClientSize[0]
        $yVideo = $aClientSize[1]
    EndIf
    $hr = $oVideoWindow.SetWindowPosition($iX, $iY, $xVideo, $yVideo)
    If $hr < 0 Then Return SetError(1, 16, 0)
    $hr = $oVideoWindow.put_Owner($hGuiWin)
    If $hr < 0 Then Return SetError(1, 17, 0)
    $oVideoWindow.put_WindowStyle(BitOR($WS_CHILD, $WS_CLIPCHILDREN))
    If $hr < 0 Then Return SetError(1, 18, 0)
    $hr = $oMediaControl.Run()
    If $hr < 0 Then Return SetError(1, 19, 0)
EndFunc   ;==>_WebcamDS_RenderWebcam

; #INTERNAL_NO_DOC# ===================================================================================================
Func _WebcamDS_EnumerateDevices($CLSID_category, $argument = 99)
    $oDevEnum = ObjCreateInterface($sCLSID_SystemDeviceEnum, $sIID_ICreateDevEnum, $tagICreateDevEnum)

    If Not IsObj($oDevEnum) Then Return SetError(6, 0, 0)
    Local $pEnum = 0
    $oDevEnum.CreateClassEnumerator($CLSID_category, $pEnum, 0)
    $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumMoniker, $tagIEnumMoniker)

    If Not IsObj($oEnum) Then Return SetError(7, 0, 0)
    Local $iPosition = '', $arg = ''
    If StringInStr($CLSID_category, "860bb310") Or StringInStr($CLSID_category, "33d9a762") Then
        $iPosition = $argument
        $arg = ''
    Else
        $iPosition = ''
        $arg = $argument
    EndIf
    Local $pMoniker, $oMoniker, $hr
    Local $i = 1, $pBindObj[99]
    While $oEnum.Next(1, $pMoniker, 0) = $S_OK
        Local $pPropBag, $oPropBag
        $oMoniker = ObjCreateInterface($pMoniker, $sIID_IMoniker, $tagIMoniker)
        If Not IsObj($oMoniker) Then Return SetError(8, 0, 0)
        $hr = $oMoniker.BindToObject(0, 0, $sIID_IBaseFilter, $pBindObj[$i])
        If $hr < 0 Then ExitLoop
        $hr = $oMoniker.BindToStorage(0, 0, $sIID_IPropertyBag, $pPropBag)
        If $hr < 0 Then Return SetError(1, 2, 0)
        $oPropBag = ObjCreateInterface($pPropBag, $sIID_IPropertyBag, $tagIPropertyBag)
        If Not IsObj($oPropBag) Then Return SetError(9, 0, 0)
        Local $var = ''
        $hr = $oPropBag.Read("FriendlyName", $var, 0)
        If $hr < 0 Then Return SetError(1, 3, 0)
        If $iPosition = 99 Then
            If $var <> '' Then
                $aWebcamList[$i] = $var
            EndIf
            Local $oCaptureTemp = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
            $oCaptureTemp = 0
        EndIf
        If ($iPosition <> '' And $iPosition = $i) Or ($arg <> '' And $var = $arg) Then
            If ($iPosition <> '' And $iPosition = $i) Then
                If Not IsObj($oCapture[$iPosition]) Then
                    $oCapture[$iPosition] = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                    If Not IsObj($oCapture[$iPosition]) Then Return SetError(12, 0, 0)
                EndIf
                $hr = $oGraph.AddFilter($oCapture[$iPosition], "CaptureFilter" & $iWebcam)
                If $hr < 0 Then Return SetError(1, 6, 0)
                WinSetTitle($hGUI, '', $var)
                ExitLoop
            EndIf
        EndIf
        $oPropBag = 0
        $i += 1
    WEnd
    If $iPosition = 99 And StringInStr($CLSID_category, "860bb310") Then $aWebcamList[0] = $i - 1
    $oMoniker = 0
    $oEnum = 0
    $oDevEnum = 0
EndFunc   ;==>_WebcamDS_EnumerateDevices

Func _WebcamDS_Reset()
    _WebcamDS_ReleaseObjects()
    _WebcamDS_Init()
EndFunc   ;==>_WebcamDS_Reset
; #INTERNAL_NO_DOC# ===================================================================================================

 

Oh, cheers! Can't wait to test it. I will come back with some reports.

 

Greets from Barcelona

Link to comment
Share on other sites

I have now modified to write to .ini file the camera that will be used and also to inform when there is no camera connected.

#include <GuiComboBox.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

; #VARIABLES#
Global Const $WM_CAP_START = 0x400
Global Const $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
Global Const $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
Global Const $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
Global Const $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
Global Const $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
Global Const $WM_CAP_SET_SCALE = $WM_CAP_START + 53
Global Const $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51

Global $iWebcam = 1, $FrameRate, $Vmr9 = 1, $oVideoWindow, $oVmr9, $aWebcamList[9]
Global $oBasicVideo, $oBuild, $oCapture[9], $oGraph, $oMediaControl, $oDevEnum, $oEnum
Global Const $S_OK = 0
Global Const $sIID_IMediaControl = '{56A868B1-0AD4-11CE-B03A-0020AF0BA770}'
Global Const $sCLSID_VideoInputDeviceCategory = '{860bb310-5d01-11d0-bd3b-00a0c911ce86}'
Global Const $PIN_CATEGORY_CAPTURE = '{fb6c4281-0353-11d1-905f-0000c0cc16ba}'
Global Const $PIN_CATEGORY_PREVIEW = '{fb6c4282-0353-11d1-905f-0000c0cc16ba}'
Global Const $MEDIATYPE_Video = '{73646976-0000-0010-8000-00aa00389b71}'
Global Const $sCLSID_VideoMixingRenderer9 = '{51b4abf3-748f-4e3b-a276-c828330e926a}'
Global Const $sCLSID_FilterGraph = "{E436EBB3-524F-11CE-9F53-0020AF0BA770}"
Global Const $sIID_IGraphBuilder = "{56A868A9-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIGraphBuilder = "AddFilter hresult(ptr;wstr);" & "RemoveFilter hresult(ptr);" & "EnumFilters hresult(ptr*);" & "FindFilterByName hresult(wstr;ptr*);" & "ConnectDirect hresult(ptr;ptr;ptr);" & "Reconnect hresult(ptr);" & "Disconnect hresult(ptr);" & "SetDefaultSyncSource hresult();" & "Connect hresult(ptr;ptr);" & "Render hresult(ptr);" & "RenderFile hresult(wstr;ptr);" & "AddSourceFilter hresult(wstr;wstr;ptr*);" & "SetLogFile hresult(dword_ptr);" & "Abort hresult();" & "ShouldOperationContinue hresult();"
Global Const $sCLSID_CaptureGraphBuilder2 = '{BF87B6E1-8C27-11D0-B3F0-00AA003761C5}'
Global Const $sIID_ICaptureGraphBuilder2 = '{93e5a4e0-2d50-11d2-abfa-00a0c9c6e38d}'
Global Const $tagICaptureGraphBuilder2 = "SetFiltergraph hresult(ptr);" & "GetFiltergraph hresult(ptr*);" & "SetOutputFileName hresult(clsid;wstr;ptr*;ptr*);" & "FindInterface hresult(clsid;clsid;ptr;clsid;ptr*);" & "RenderStream hresult(clsid;clsid;ptr;ptr;ptr);" & "ControlStream hresult(clsid;clsid;ptr;ptr;ptr;word;word);" & "AllocCapFile hresult(wstr;int64);" & "CopyCaptureFile hresult(wstr;wstr;int;ptr*);" & "FindPin hresult(ptr;int;clsid;clsid;bool;int;ptr*);"
Global Const $sCLSID_SystemDeviceEnum = '{62BE5D10-60EB-11d0-BD3B-00A0C911CE86}'
Global Const $sIID_ICreateDevEnum = '{29840822-5b84-11d0-bd3b-00a0c911ce86}'
Global Const $tagICreateDevEnum = "CreateClassEnumerator hresult( clsid ; ptr*; dword );"
Global Const $sIID_IEnumMoniker = '{00000102-0000-0000-c000-000000000046}'
Global Const $tagIEnumMoniker = "Next hresult(dword;ptr*;dword*);" & "Skip hresult(dword);" & "Reset hresult();" & "Clone hresult(ptr*);"
Global Const $sIID_IBaseFilter = '{56a86895-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIBaseFilter = "GetClassID hresult(ptr*);" & "Stop hresult();" & "Pause hresult();" & "Run hresult(int64);" & "GetState hresult(dword;dword*);" & "SetSyncSource hresult(ptr);" & "GetSyncSource hresult(ptr*);" & "EnumPins hresult(ptr*);" & "FindPin hresult(wstr;ptr*);" & "QueryFilterInfo hresult(ptr*);" & "JoinFilterGraph hresult(ptr;wstr);" & "QueryVendorInfo hresult(wstr*);"
Global Const $sIID_IMoniker = '{0000000f-0000-0000-C000-000000000046}'
Global Const $tagIMoniker = "GetClassID hresult( clsid )" & "IsDirty hresult(  );" & "Load hresult( ptr );" & "Save hresult( ptr, bool );" & "GetSizeMax hresult( uint64 );" & "BindToObject hresult( ptr;ptr;clsid;ptr*);" & "BindToStorage hresult( ptr;ptr;clsid;ptr*);" & "Reduce hresult( ptr;dword;ptr*;ptr*);" & "ComposeWith hresult( ptr;bool;ptr*);" & "Enum hresult( bool;ptr*);" & "IsEqual hresult( ptr);" & "Hash hresult( dword*);" & "IsRunning hresult( ptr;ptr;ptr);" & "GetTimeOfLastChange hresult( ptr;ptr;int64*);" & "Inverse hresult( ptr*);" & "CommonPrefixWith hresult( ptr;ptr*);" & "RelativePathTo hresult( ptr;ptr*);" & "GetDisplayName hresult( ptr;ptr;wstr*);" & "ParseDisplayName hresult( ptr;ptr;wstr;ulong*;ptr*);" & "IsSystemMoniker hresult( dword*);"
Global Const $tagIDispatch = "GetTypeInfoCount hresult(dword*);" & "GetTypeInfo hresult(dword;dword;ptr*);" & "GetIDsOfNames hresult(clsid;ptr;dword;dword;ptr);" & "Invoke hresult(dword;clsid;dword;word;ptr;ptr;ptr;dword*);"
Global Const $sIID_IVideoWindow = "{56A868B4-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIVideoWindow = $tagIDispatch & "put_Caption hresult(bstr);" & "get_Caption hresult(bstr*);" & "put_WindowStyle hresult(long);" & "get_WindowStyle hresult(long*);" & "put_WindowStyleEx hresult(long);" & "put_WindowStyleEx hresult(long*);" & "put_AutoShow hresult(long);" & "get_AutoShow hresult(long*);" & "put_WindowState hresult(long);" & "get_WindowState hresult(long*);" & "put_BackgroundPalette hresult(long);" & "get_BackgroundPalette hresult(long*);" & "put_Visible hresult(long);" & "get_Visible hresult(long*);" & "put_Left hresult(long);" & "get_Left hresult(long*);" & "put_Width hresult(long);" & "get_Width hresult(long*);" & "put_Top hresult(long);" & "get_Top hresult(long*);" & "put_Height hresult(long);" & "get_Height hresult(long*);" & "put_Owner hresult(long_ptr);" & "get_Owner hresult(long_ptr*);" & "put_MessageDrain hresult(long_ptr);" & "get_MessageDrain hresult(long_ptr*);" & "get_BorderColor hresult(long*);" & "put_BorderColor hresult(long);" & "get_FullScreenMode hresult(long*);" & "put_FullScreenMode hresult(long);" & "SetWindowForeground hresult(long);" & "NotifyOwnerMessage hresult(long_ptr;long;long_ptr;long_ptr);" & "SetWindowPosition hresult(long;long;long;long);" & "GetWindowPosition hresult(long*;long*;long*;long*);" & "GetMinIdealImageSize hresult(long*;long*);" & "GetMaxIdealImageSize hresult(long*;long*);" & "GetRestorePosition hresult(long*;long*;long*;long*);" & "HideCursor hresult(long);" & "IsCursorHidden hresult(long*);"
Global Const $sIID_IBasicVideo = "{56A868B5-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIBasicVideo = $tagIDispatch & "get_AvgTimePerFrame hresult(double*);" & "get_BitRate hresult(long*);" & "get_BitErrorRate hresult(long*);" & "get_VideoWidth hresult(long*);" & "get_VideoHeight hresult(long*);" & "put_SourceLeft hresult(long);" & "get_SourceLeft hresult(long*);" & "put_SourceWidth hresult(long);" & "get_SourceWidth hresult(long*);" & "put_SourceTop hresult(long);" & "get_SourceTop hresult(long*);" & "put_SourceHeight hresult(long);" & "get_SourceHeight hresult(long*);" & "put_DestinationLeft hresult(long);" & "get_DestinationLeft hresult(long*);" & "put_DestinationWidth hresult(long);" & "get_DestinationWidth hresult(long*);" & "put_DestinationTop hresult(long);" & "get_DestinationTop hresult(long*);" & "put_DestinationHeight hresult(long);" & "get_DestinationHeight hresult(long*);" & "SetSourcePosition hresult(long;long;long;long);" & "GetSourcePosition hresult(long*;long*;long*;long*);" & "SetDefaultSourcePosition hresult();" & "SetDestinationPosition hresult(long;long;long;long);" & "GetDestinationPosition hresult(long*;long*;long*;long*);" & "SetDefaultDestinationPosition hresult();" & "GetVideoSize hresult(long*;long*);" & "GetVideoPaletteEntries hresult(long;long;long*;long*);" & "GetCurrentImage hresult(long*;long*);" & "IsUsingDefaultSource hresult();" & "IsUsingDefaultDestination hresult();"
Global Const $sIID_IPropertyBag = '{55272a00-42cb-11ce-8135-00aa004bb851}'
Global Const $tagIPropertyBag = 'Read hresult(wstr;variant*;ptr*);' & 'Write hresult(wstr;variant);'
Global Const $tagIVMRWindowlessControl = 'GetNativeVideoSize hresult(long*;long*;long*;long*);' & 'GetMinIdealVideoSize hresult(long*;long*);' & 'GetMaxIdealVideoSize hresult(long*;long*);' & 'SetVideoPosition hresult(ptr;ptr);' & 'GetVideoPosition hresult(ptr*;ptr*);' & 'GetAspectRatioMode hresult(dword*);' & 'SetAspectRatioMode hresult(dword);' & 'SetVideoClippingWindow hresult(hwnd);' & 'RepaintVideo hresult(hwnd;handle);' & 'DisplayModeChanged hresult();' & 'GetCurrentImage hresult(byte*);' & 'SetBorderColor hresult(clr);' & 'GetBorderColor hresult(clr*);' & 'SetColorKey hresult(clr);' & 'GetColorKey hresult(clr*);'
Global Const $tagIVMRFilterConfig = 'SetImageCompositor hresult(ptr);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRFilterConfig9 = '{5a804648-4f66-4867-9c43-4f5c822cf1b8}'
Global Const $tagIVMRFilterConfig9 = 'SetImageCompositor hresult(ptr);' & 'SetNumberOfStreams hresult(dword);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRMixerControl9 = '{1a777eaa-47c8-4930-b2c9-8fee1c1b0f3b}'
Global Const $tagIVMRMixerControl9 = 'SetAlpha(dword;float);' & 'GetAlpha(dword;ptr*) ;' & 'SetZOrder(dword;dword) ;' & 'GetZOrder(dword;ptr*) ;' & 'SetOutputRect(dword;ptr) ;' & 'GetOutputRect(dword;ptr*) ;' & 'SetBackgroundClr(dword) ;' & 'GetBackgroundClr(ptr) ;' & 'SetMixingPrefs(dword) ;' & 'GetMixingPrefs(ptr*) ;' & 'SetProcAmpControl(dword;ptr) ;' & 'GetProcAmpControl(dword;ptr*) ;' & 'GetProcAmpControlRange(dword;ptr*);'
Global Const $sIID_IAMStreamConfig = '{c6e13340-30ac-11d0-a18c-00a0c9118956}'
Global $FrameRate = 30
; #VARIABLES#

; ================================================================================================== EXAMPLE START
Global $msg, $hGUI, $sId_snap, $idCam, $hComboCam = '', $but_snap, $sFile, $isCamera = 0, $pos_control

Global $iWebcam = IniRead(@ScriptDir & "\Config.ini", "WEBCAM", "opt", "1")

_WebcamDS_Init()

If @error <> 0 Or @extended <> 0 Then Exit
_CreateGui(740, 580)

_WebcamDS_RenderWebcam($iWebcam, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])

If @error <> 0 Or @extended <> 0 Then Exit

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $hComboCam
            _WebcamDS_Reset()
            $iWebcam = _GUICtrlComboBox_GetCurSel($hComboCam) + 1
            IniWrite(@ScriptDir & "\Config.ini", "WEBCAM", "opt", $iWebcam)
            _WebcamDS_RenderWebcam($iWebcam, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])
        Case $but_snap
            ;$sFile = FileSaveDialog("Save", @ScriptDir & "\", "Image (*.bmp)", 1 + 4)
            $sFile = @ScriptDir & "\test.bmp"
            _WebcamSnap($sFile, $hGUI)
        Case $GUI_EVENT_CLOSE
            _WebcamDS_ReleaseObjects(1)
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd

Func _CreateGui($w = 640, $h = 480)
    $hGUI = GUICreate("Test Capture", $w, $h, -1, -1, $WS_POPUP)
    GUICtrlCreateLabel("Select webcam:", 115, 12, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    Local $pic_image = GUICtrlCreatePic("", 45, 45, 640, 480)
    GUICtrlSetBkColor(-1, 0xff0000)
    $but_snap = GUICtrlCreateButton("Webcam snap", 585, 9, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    $pos_control = ControlGetPos("Test Capture", '', $pic_image)
    If $hComboCam = '' Then
        For $i = 1 To $aWebcamList[0]
            If $hComboCam = '' Then
                $hComboCam = GUICtrlCreateCombo($aWebcamList[$i], 220, 10, 200, 25)
            Else
                If $aWebcamList[$i] <> '' Then GUICtrlSetData($hComboCam, $aWebcamList[$i])
            EndIf
        Next
    EndIf
    GUISetState()
EndFunc   ;==>_CreateGui
; ================================================================================================== EXAMPLE END

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamSnapt
; Description ...: Save current webcam image
; Syntax.........: _WebcamSnap(Image save address, $Gui_id)
; Parameters ....: none
; Return values .: none
; Date...........: 2019/05/11
; Author ........: Belini
; Remarks .......: Call to capture image
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamSnap($snap = "", $hWnd = "")
    Local $x = $pos_control[0]
    Local $y = $pos_control[1]
    Local $w = $pos_control[2]
    Local $h = $pos_control[3]
    _ScreenCapture_CaptureWnd($snap, $hWnd, $x, $y, $w, $h, False)
EndFunc   ;==>_WebcamSnap

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_Init
; Description ...: Create Direct Show objects and enumerate webcams
; Syntax.........: _WebcamDS_Init()
; Parameters ....: none
; Return values .: Success - it fills 3 array  $aWebcamList
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Remarks .......: call it at the beginning of the script
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_Init()
    Local $hr, $aCall
    $oGraph = ObjCreateInterface($sCLSID_FilterGraph, $sIID_IGraphBuilder, $tagIGraphBuilder)
    If Not IsObj($oGraph) Then Return SetError(1, 0, 0)
    $oBuild = ObjCreateInterface($sCLSID_CaptureGraphBuilder2, $sIID_ICaptureGraphBuilder2, $tagICaptureGraphBuilder2)
    If Not IsObj($oBuild) Then Return SetError(2, 0, 0)
    $hr = $oBuild.SetFiltergraph($oGraph)
    If $hr < 0 Then Return SetError(0, 1, 0)
    Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)
    If Not IsObj($oMediaControl) Then Return SetError(3, 0, 0)
    Local $pVideoWindow
    $oGraph.QueryInterface($sIID_IVideoWindow, $pVideoWindow)
    $oVideoWindow = ObjCreateInterface($pVideoWindow, $sIID_IVideoWindow, $tagIVideoWindow)
    If Not IsObj($oVideoWindow) Then Return SetError(4, 0, 0)
    Local $pBasicVideo
    $oGraph.QueryInterface($sIID_IBasicVideo, $pBasicVideo)
    $oBasicVideo = ObjCreateInterface($pBasicVideo, $sIID_IBasicVideo, $tagIBasicVideo)
    If Not IsObj($oBasicVideo) Then Return SetError(5, 0, 0)

    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory)
    If $aWebcamList[0] > 0 Then
        If $aWebcamList[0] < $iWebcam Or $aWebcamList[0] = 1 Then $iWebcam = 1
    Else
        $iWebcam = 0
        MsgBox(4096, "ALERT", "No camera found", 5)
        exit
    EndIf
EndFunc   ;==>_WebcamDS_Init

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_ReleaseObjects
; Description ...: release Objects created during script and at the end of it
; Syntax.........: _WebcamDS_ReleaseObjects($iArg = 0)
; Parameters ....: $iArg   - use 0 if you want to delete only the current capture obj, otherwise (ie: at the end) put it to 1
; Return values .: Success - it deletes the objects created
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_ReleaseObjects($iArg = 0)
    $oMediaControl.Stop()
    $oMediaControl = 0
    $oVmr9 = 0
    $oCapture[$iWebcam] = 0
    If $iArg Then $oCapture = 0
    $oBasicVideo = 0
    $oVideoWindow = 0
    $oGraph = 0
    $oBuild = 0
EndFunc   ;==>_WebcamDS_ReleaseObjects

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_RenderWebcam
; Description ...: call it to set which webcam to display, audio preview, resolution, scale to GUI, zoom
; Syntax.........: _WebcamDS_RenderWebcam($iNumberWebcam=1,$iAudioMic=0,$hGuiWin[,$iScale=1[,$xVideo=640[,$yVideo=480[,$ibpp=16[,$iZoomX=0[,$iZoomY=0[,$iZoomW=0[,$iZoomH=0]]]]]]]])
; Parameters ....: $iNumberWebcam - The webcam to display (from 1)
;                  $hGuiWin     - The GUI you create to display the webcam
;                  $iScale      - if you want to scale the webcam resolution to the res of the GUI, set it to 1, otherwise to 0
;                  $xVideo      - The webcam Width
;                  $yVideo      - The webcam Height
;                  $left        - The left side of image in Gui
;                  $top         - The top side of image in Gui
; Return values .: Success - it displays the webcam selected
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_RenderWebcam($iNumberWebcam, $hGuiWin, $iScale = 1, $xVideo = 640, $yVideo = 480, $left = 0, $top = 0)
    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory, $iNumberWebcam)
    Local $_GUID = "DWORD Data1;" & "WORD  Data2;" & "WORD  Data3;" & "BYTE  Data4[8];"
    Local $_SIZE = "LONG cx;" & "LONG cy;"
    Local $_VIDEO_STREAM_CONFIG_CAPS = $_GUID & "ULONG    VideoStandard;" & $_SIZE & $_SIZE & $_SIZE & "int      CropGranularityX;" & "int      CropGranularityY;" & "int      CropAlignX;" & "int      CropAlignY;" & $_SIZE & $_SIZE & "int      OutputGranularityX;" & "int      OutputGranularityY;" & "int      StretchTapsX;" & "int      StretchTapsY;" & "int      ShrinkTapsX;" & "int      ShrinkTapsY;" & "int64     MinFrameInterval;" & "int64  MaxFrameInterval;" & "LONG     MinBitsPerSecond;" & "LONG     MaxBitsPerSecond;"
    Local $tVideoStream = DllStructCreate($_VIDEO_STREAM_CONFIG_CAPS)
    Local $_AM_MEDIA = $_GUID & $_GUID & 'BOOL  bFixedSizeSamples;' & 'BOOL  bTemporalCompression;' & 'ULONG    lSampleSize;' & $_GUID & 'ptr pUnk;' & 'ULONG    cbFormat;' & 'ptr  pbFormat;'
    Local $t_AM_MEDIA = DllStructCreate($_AM_MEDIA)
    Local $_RECT = 'LONG left;' & 'LONG top;' & 'LONG right;' & 'LONG bottom;'
    Local $_tagBITMAPINFOHEADER = 'DWORD biSize;' & 'LONG  biWidth;' & 'LONG  biHeight;' & 'WORD  biPlanes;' & 'WORD  biBitCount;' & 'DWORD biCompression;' & 'DWORD biSizeImage;' & 'LONG  biXPelsPerMeter;' & 'LONG  biYPelsPerMeter;' & 'DWORD biClrUsed;' & 'DWORD biClrImportant;'
    Local $_tagVIDEOINFOHEADER = $_RECT & $_RECT & 'DWORD            dwBitRate;' & 'DWORD            dwBitErrorRate;' & 'int64   AvgTimePerFrame;' & $_tagBITMAPINFOHEADER
    Local $_PinInfo = 'ptr   pFilter;' & 'int64    dir;' & 'wchar    achName[128];'
    Local $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], $sIID_IAMStreamConfig, 0)
    Local $iCount = 0, $iSize = 0
    If $hr < 0 Then Return SetError(1, 7, 0)
    If DllStructGetSize($tVideoStream) == $iSize Then
        For $iFormat = 0 To $iCount - 1 Step 1
            Local $scc = DllStructGetPtr($tVideoStream)
            Local $pmtConfig = DllStructGetPtr($t_AM_MEDIA)
            Local $t_AM_MEDIA_TYPE = DllStructCreate($_AM_MEDIA, $pmtConfig)
            Local $pbFormat = DllStructGetData($t_AM_MEDIA_TYPE, "pbFormat")
            Local $t_VIDEOINFOHEADER = DllStructCreate($_tagVIDEOINFOHEADER, $pbFormat)
            Local $videoWidth = DllStructGetData($t_VIDEOINFOHEADER, 'biWidth')
            Local $videoHeight = DllStructGetData($t_VIDEOINFOHEADER, 'biHeight')
            Local $bitCount = DllStructGetData($t_VIDEOINFOHEADER, 'biBitCount')
            Local $dwBitRate = DllStructGetData($t_VIDEOINFOHEADER, 'dwBitRate')
            Local $biCompression = DllStructGetData($t_VIDEOINFOHEADER, 'biCompression')
            Local $biPlanes = DllStructGetData($t_VIDEOINFOHEADER, 'biPlanes')
            Local $biSize = DllStructGetData($t_VIDEOINFOHEADER, 'biSizeImage')
            Local $AvgframeRate = Round(10000000 / DllStructGetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame'))
            Local $sText = ''
            For $i = 7 To 0 Step -2
                If $biCompression <> 0 Then $sText = $sText & Chr(Dec(StringMid(Hex($biCompression, 8), $i, 2)))
            Next
            DllStructSetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame', 10000000 / $FrameRate)
            If $iFormat = $iCount - 1 Then
                DllStructSetData($t_VIDEOINFOHEADER, 'biWidth', $xVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biHeight', $yVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biBitCount', 16)
                If $hr < 0 Then Return SetError(1, 9, 0)
            EndIf
        Next
    EndIf
    If $Vmr9 = 0 Then
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, 0)
        If $hr < 0 Then Return SetError(1, 10, 0)
    Else
        $oVmr9 = ObjCreateInterface($sCLSID_VideoMixingRenderer9, $sIID_IBaseFilter, $tagIBaseFilter)
        If Not IsObj($oVmr9) Then Return SetError(14, 0, 0)
        $hr = $oGraph.AddFilter($oVmr9, "VMR9")
        If $hr < 0 Then Return SetError(1, 11, 0)
        Local $pConfig9, $oConfig9
        $hr = $oVmr9.QueryInterface($sIID_IVMRFilterConfig9, $pConfig9)
        $oConfig9 = ObjCreateInterface($pConfig9, $sIID_IVMRFilterConfig9, $tagIVMRFilterConfig9)
        If Not IsObj($oConfig9) Then Return SetError(15, 0, 0)
        ConsoleWrite('Filterconf hr:' & $hr & '  ' & IsObj($oConfig9) & @CRLF)
        Local $str = ''
        $hr = $oConfig9.SetNumberOfStreams(4)
        If $hr < 0 Then Return SetError(1, 12, 0)
        Local $pMixControl9, $oMixControl9
        $hr = $oVmr9.QueryInterface($sIID_IVMRMixerControl9, $pMixControl9)
        $oMixControl9 = ObjCreateInterface($pMixControl9, $sIID_IVMRMixerControl9, $tagIVMRMixerControl9)
        If Not IsObj($oMixControl9) Then Return SetError(16, 0, 0)
        Local $t_VMR9rect = DllStructCreate($_RECT)
        Local $l = 0, $t = 0, $r = $l + $xVideo, $b = $t + $yVideo
        DllStructSetData($t_VMR9rect, 'left', $l)
        DllStructSetData($t_VMR9rect, 'top', $t)
        DllStructSetData($t_VMR9rect, 'right', $r)
        DllStructSetData($t_VMR9rect, 'bottom', $b)
        Local $streamID = 0
        $hr = $oMixControl9.SetOutputRect($streamID, DllStructGetPtr($t_VMR9rect))
        If $hr < 0 Then Return SetError(1, 13, 0)
        $oConfig9 = 0
        $oMixControl9 = 0
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, $oVmr9)
        If $hr < 0 Then Return SetError(1, 14, 0)
        $oVmr9 = 0
    EndIf
    Local $iX = $left, $iY = $top
    Local $aClientSize = WinGetClientSize($hGuiWin)
    If $iScale = 1 Then
        $xVideo = $aClientSize[0]
        $yVideo = $aClientSize[1]
    EndIf
    $hr = $oVideoWindow.SetWindowPosition($iX, $iY, $xVideo, $yVideo)
    If $hr < 0 Then Return SetError(1, 16, 0)
    $hr = $oVideoWindow.put_Owner($hGuiWin)
    If $hr < 0 Then Return SetError(1, 17, 0)
    $oVideoWindow.put_WindowStyle(BitOR($WS_CHILD, $WS_CLIPCHILDREN))
    If $hr < 0 Then Return SetError(1, 18, 0)
    $hr = $oMediaControl.Run()
    If $hr < 0 Then Return SetError(1, 19, 0)
EndFunc   ;==>_WebcamDS_RenderWebcam

; #INTERNAL_NO_DOC# ===================================================================================================
Func _WebcamDS_EnumerateDevices($CLSID_category, $argument = 99)
    $oDevEnum = ObjCreateInterface($sCLSID_SystemDeviceEnum, $sIID_ICreateDevEnum, $tagICreateDevEnum)

    If Not IsObj($oDevEnum) Then Return SetError(6, 0, 0)
    Local $pEnum = 0
    $oDevEnum.CreateClassEnumerator($CLSID_category, $pEnum, 0)
    $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumMoniker, $tagIEnumMoniker)

    If Not IsObj($oEnum) Then Return SetError(7, 0, 0)
    Local $iPosition = '', $arg = ''
    If StringInStr($CLSID_category, "860bb310") Or StringInStr($CLSID_category, "33d9a762") Then
        $iPosition = $argument
        $arg = ''
    Else
        $iPosition = ''
        $arg = $argument
    EndIf
    Local $pMoniker, $oMoniker, $hr
    Local $i = 1, $pBindObj[99]
    While $oEnum.Next(1, $pMoniker, 0) = $S_OK
        Local $pPropBag, $oPropBag
        $oMoniker = ObjCreateInterface($pMoniker, $sIID_IMoniker, $tagIMoniker)
        If Not IsObj($oMoniker) Then Return SetError(8, 0, 0)
        $hr = $oMoniker.BindToObject(0, 0, $sIID_IBaseFilter, $pBindObj[$i])
        If $hr < 0 Then ExitLoop
        $hr = $oMoniker.BindToStorage(0, 0, $sIID_IPropertyBag, $pPropBag)
        If $hr < 0 Then Return SetError(1, 2, 0)
        $oPropBag = ObjCreateInterface($pPropBag, $sIID_IPropertyBag, $tagIPropertyBag)
        If Not IsObj($oPropBag) Then Return SetError(9, 0, 0)
        Local $var = ''
        $hr = $oPropBag.Read("FriendlyName", $var, 0)
        If $hr < 0 Then Return SetError(1, 3, 0)
        If $iPosition = 99 Then
            If $var <> '' Then
                $aWebcamList[$i] = $var
            EndIf
            Local $oCaptureTemp = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
            $oCaptureTemp = 0
        EndIf
        If ($iPosition <> '' And $iPosition = $i) Or ($arg <> '' And $var = $arg) Then
            If ($iPosition <> '' And $iPosition = $i) Then
                If Not IsObj($oCapture[$iPosition]) Then
                    $oCapture[$iPosition] = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                    If Not IsObj($oCapture[$iPosition]) Then Return SetError(12, 0, 0)
                EndIf
                $hr = $oGraph.AddFilter($oCapture[$iPosition], "CaptureFilter" & $iWebcam)
                If $hr < 0 Then Return SetError(1, 6, 0)
                WinSetTitle($hGUI, '', $var)
                ExitLoop
            EndIf
        EndIf
        $oPropBag = 0
        $i += 1
    WEnd
    If $iPosition = 99 And StringInStr($CLSID_category, "860bb310") Then $aWebcamList[0] = $i - 1
    $oMoniker = 0
    $oEnum = 0
    $oDevEnum = 0
EndFunc   ;==>_WebcamDS_EnumerateDevices

Func _WebcamDS_Reset()
    _WebcamDS_ReleaseObjects()
    _WebcamDS_Init()
EndFunc   ;==>_WebcamDS_Reset
; #INTERNAL_NO_DOC# ===================================================================================================

 

Edited by Belini
Link to comment
Share on other sites

5 hours ago, argumentum said:

Cool. Use "#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7"

I had forgotten to put

#include <GuiComboBox.au3>
#include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

; #VARIABLES#
Global Const $S_OK = 0
Global Const $WM_CAP_START = 0x400
Global Const $WM_CAP_GRAB_FRAME_NOSTOP = $WM_CAP_START + 61
Global Const $WM_CAP_FILE_SAVEDIBA = $WM_CAP_START + 25
Global Const $WM_CAP_SET_PREVIEW = $WM_CAP_START + 50
Global Const $WM_CAP_SET_PREVIEWRATE = $WM_CAP_START + 52
Global Const $WM_CAP_DRIVER_CONNECT = $WM_CAP_START + 10
Global Const $WM_CAP_SET_SCALE = $WM_CAP_START + 53
Global Const $WM_CAP_SET_OVERLAY = $WM_CAP_START + 51
Global $Vmr9 = 1, $oVideoWindow, $oVmr9, $aWebcamList[9]
Global $oBasicVideo, $oBuild, $oCapture[9], $oGraph, $oMediaControl, $oDevEnum, $oEnum
Global Const $sIID_IMediaControl = '{56A868B1-0AD4-11CE-B03A-0020AF0BA770}'
Global Const $sCLSID_VideoInputDeviceCategory = '{860bb310-5d01-11d0-bd3b-00a0c911ce86}'
Global Const $PIN_CATEGORY_CAPTURE = '{fb6c4281-0353-11d1-905f-0000c0cc16ba}'
Global Const $PIN_CATEGORY_PREVIEW = '{fb6c4282-0353-11d1-905f-0000c0cc16ba}'
Global Const $MEDIATYPE_Video = '{73646976-0000-0010-8000-00aa00389b71}'
Global Const $sCLSID_VideoMixingRenderer9 = '{51b4abf3-748f-4e3b-a276-c828330e926a}'
Global Const $sCLSID_FilterGraph = "{E436EBB3-524F-11CE-9F53-0020AF0BA770}"
Global Const $sIID_IGraphBuilder = "{56A868A9-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIGraphBuilder = "AddFilter hresult(ptr;wstr);" & "RemoveFilter hresult(ptr);" & "EnumFilters hresult(ptr*);" & "FindFilterByName hresult(wstr;ptr*);" & "ConnectDirect hresult(ptr;ptr;ptr);" & "Reconnect hresult(ptr);" & "Disconnect hresult(ptr);" & "SetDefaultSyncSource hresult();" & "Connect hresult(ptr;ptr);" & "Render hresult(ptr);" & "RenderFile hresult(wstr;ptr);" & "AddSourceFilter hresult(wstr;wstr;ptr*);" & "SetLogFile hresult(dword_ptr);" & "Abort hresult();" & "ShouldOperationContinue hresult();"
Global Const $sCLSID_CaptureGraphBuilder2 = '{BF87B6E1-8C27-11D0-B3F0-00AA003761C5}'
Global Const $sIID_ICaptureGraphBuilder2 = '{93e5a4e0-2d50-11d2-abfa-00a0c9c6e38d}'
Global Const $tagICaptureGraphBuilder2 = "SetFiltergraph hresult(ptr);" & "GetFiltergraph hresult(ptr*);" & "SetOutputFileName hresult(clsid;wstr;ptr*;ptr*);" & "FindInterface hresult(clsid;clsid;ptr;clsid;ptr*);" & "RenderStream hresult(clsid;clsid;ptr;ptr;ptr);" & "ControlStream hresult(clsid;clsid;ptr;ptr;ptr;word;word);" & "AllocCapFile hresult(wstr;int64);" & "CopyCaptureFile hresult(wstr;wstr;int;ptr*);" & "FindPin hresult(ptr;int;clsid;clsid;bool;int;ptr*);"
Global Const $sCLSID_SystemDeviceEnum = '{62BE5D10-60EB-11d0-BD3B-00A0C911CE86}'
Global Const $sIID_ICreateDevEnum = '{29840822-5b84-11d0-bd3b-00a0c911ce86}'
Global Const $tagICreateDevEnum = "CreateClassEnumerator hresult( clsid ; ptr*; dword );"
Global Const $sIID_IEnumMoniker = '{00000102-0000-0000-c000-000000000046}'
Global Const $tagIEnumMoniker = "Next hresult(dword;ptr*;dword*);" & "Skip hresult(dword);" & "Reset hresult();" & "Clone hresult(ptr*);"
Global Const $sIID_IBaseFilter = '{56a86895-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIBaseFilter = "GetClassID hresult(ptr*);" & "Stop hresult();" & "Pause hresult();" & "Run hresult(int64);" & "GetState hresult(dword;dword*);" & "SetSyncSource hresult(ptr);" & "GetSyncSource hresult(ptr*);" & "EnumPins hresult(ptr*);" & "FindPin hresult(wstr;ptr*);" & "QueryFilterInfo hresult(ptr*);" & "JoinFilterGraph hresult(ptr;wstr);" & "QueryVendorInfo hresult(wstr*);"
Global Const $sIID_IMoniker = '{0000000f-0000-0000-C000-000000000046}'
Global Const $tagIMoniker = "GetClassID hresult( clsid )" & "IsDirty hresult(  );" & "Load hresult( ptr );" & "Save hresult( ptr, bool );" & "GetSizeMax hresult( uint64 );" & "BindToObject hresult( ptr;ptr;clsid;ptr*);" & "BindToStorage hresult( ptr;ptr;clsid;ptr*);" & "Reduce hresult( ptr;dword;ptr*;ptr*);" & "ComposeWith hresult( ptr;bool;ptr*);" & "Enum hresult( bool;ptr*);" & "IsEqual hresult( ptr);" & "Hash hresult( dword*);" & "IsRunning hresult( ptr;ptr;ptr);" & "GetTimeOfLastChange hresult( ptr;ptr;int64*);" & "Inverse hresult( ptr*);" & "CommonPrefixWith hresult( ptr;ptr*);" & "RelativePathTo hresult( ptr;ptr*);" & "GetDisplayName hresult( ptr;ptr;wstr*);" & "ParseDisplayName hresult( ptr;ptr;wstr;ulong*;ptr*);" & "IsSystemMoniker hresult( dword*);"
Global Const $tagIDispatch = "GetTypeInfoCount hresult(dword*);" & "GetTypeInfo hresult(dword;dword;ptr*);" & "GetIDsOfNames hresult(clsid;ptr;dword;dword;ptr);" & "Invoke hresult(dword;clsid;dword;word;ptr;ptr;ptr;dword*);"
Global Const $sIID_IVideoWindow = "{56A868B4-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIVideoWindow = $tagIDispatch & "put_Caption hresult(bstr);" & "get_Caption hresult(bstr*);" & "put_WindowStyle hresult(long);" & "get_WindowStyle hresult(long*);" & "put_WindowStyleEx hresult(long);" & "put_WindowStyleEx hresult(long*);" & "put_AutoShow hresult(long);" & "get_AutoShow hresult(long*);" & "put_WindowState hresult(long);" & "get_WindowState hresult(long*);" & "put_BackgroundPalette hresult(long);" & "get_BackgroundPalette hresult(long*);" & "put_Visible hresult(long);" & "get_Visible hresult(long*);" & "put_Left hresult(long);" & "get_Left hresult(long*);" & "put_Width hresult(long);" & "get_Width hresult(long*);" & "put_Top hresult(long);" & "get_Top hresult(long*);" & "put_Height hresult(long);" & "get_Height hresult(long*);" & "put_Owner hresult(long_ptr);" & "get_Owner hresult(long_ptr*);" & "put_MessageDrain hresult(long_ptr);" & "get_MessageDrain hresult(long_ptr*);" & "get_BorderColor hresult(long*);" & "put_BorderColor hresult(long);" & "get_FullScreenMode hresult(long*);" & "put_FullScreenMode hresult(long);" & "SetWindowForeground hresult(long);" & "NotifyOwnerMessage hresult(long_ptr;long;long_ptr;long_ptr);" & "SetWindowPosition hresult(long;long;long;long);" & "GetWindowPosition hresult(long*;long*;long*;long*);" & "GetMinIdealImageSize hresult(long*;long*);" & "GetMaxIdealImageSize hresult(long*;long*);" & "GetRestorePosition hresult(long*;long*;long*;long*);" & "HideCursor hresult(long);" & "IsCursorHidden hresult(long*);"
Global Const $sIID_IBasicVideo = "{56A868B5-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIBasicVideo = $tagIDispatch & "get_AvgTimePerFrame hresult(double*);" & "get_BitRate hresult(long*);" & "get_BitErrorRate hresult(long*);" & "get_VideoWidth hresult(long*);" & "get_VideoHeight hresult(long*);" & "put_SourceLeft hresult(long);" & "get_SourceLeft hresult(long*);" & "put_SourceWidth hresult(long);" & "get_SourceWidth hresult(long*);" & "put_SourceTop hresult(long);" & "get_SourceTop hresult(long*);" & "put_SourceHeight hresult(long);" & "get_SourceHeight hresult(long*);" & "put_DestinationLeft hresult(long);" & "get_DestinationLeft hresult(long*);" & "put_DestinationWidth hresult(long);" & "get_DestinationWidth hresult(long*);" & "put_DestinationTop hresult(long);" & "get_DestinationTop hresult(long*);" & "put_DestinationHeight hresult(long);" & "get_DestinationHeight hresult(long*);" & "SetSourcePosition hresult(long;long;long;long);" & "GetSourcePosition hresult(long*;long*;long*;long*);" & "SetDefaultSourcePosition hresult();" & "SetDestinationPosition hresult(long;long;long;long);" & "GetDestinationPosition hresult(long*;long*;long*;long*);" & "SetDefaultDestinationPosition hresult();" & "GetVideoSize hresult(long*;long*);" & "GetVideoPaletteEntries hresult(long;long;long*;long*);" & "GetCurrentImage hresult(long*;long*);" & "IsUsingDefaultSource hresult();" & "IsUsingDefaultDestination hresult();"
Global Const $sIID_IPropertyBag = '{55272a00-42cb-11ce-8135-00aa004bb851}'
Global Const $tagIPropertyBag = 'Read hresult(wstr;variant*;ptr*);' & 'Write hresult(wstr;variant);'
Global Const $tagIVMRWindowlessControl = 'GetNativeVideoSize hresult(long*;long*;long*;long*);' & 'GetMinIdealVideoSize hresult(long*;long*);' & 'GetMaxIdealVideoSize hresult(long*;long*);' & 'SetVideoPosition hresult(ptr;ptr);' & 'GetVideoPosition hresult(ptr*;ptr*);' & 'GetAspectRatioMode hresult(dword*);' & 'SetAspectRatioMode hresult(dword);' & 'SetVideoClippingWindow hresult(hwnd);' & 'RepaintVideo hresult(hwnd;handle);' & 'DisplayModeChanged hresult();' & 'GetCurrentImage hresult(byte*);' & 'SetBorderColor hresult(clr);' & 'GetBorderColor hresult(clr*);' & 'SetColorKey hresult(clr);' & 'GetColorKey hresult(clr*);'
Global Const $tagIVMRFilterConfig = 'SetImageCompositor hresult(ptr);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRFilterConfig9 = '{5a804648-4f66-4867-9c43-4f5c822cf1b8}'
Global Const $tagIVMRFilterConfig9 = 'SetImageCompositor hresult(ptr);' & 'SetNumberOfStreams hresult(dword);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRMixerControl9 = '{1a777eaa-47c8-4930-b2c9-8fee1c1b0f3b}'
Global Const $tagIVMRMixerControl9 = 'SetAlpha(dword;float);' & 'GetAlpha(dword;ptr*) ;' & 'SetZOrder(dword;dword) ;' & 'GetZOrder(dword;ptr*) ;' & 'SetOutputRect(dword;ptr) ;' & 'GetOutputRect(dword;ptr*) ;' & 'SetBackgroundClr(dword) ;' & 'GetBackgroundClr(ptr) ;' & 'SetMixingPrefs(dword) ;' & 'GetMixingPrefs(ptr*) ;' & 'SetProcAmpControl(dword;ptr) ;' & 'GetProcAmpControl(dword;ptr*) ;' & 'GetProcAmpControlRange(dword;ptr*);'
Global Const $sIID_IAMStreamConfig = '{c6e13340-30ac-11d0-a18c-00a0c9118956}'
Global $FrameRate = 30
; #VARIABLES#

; ================================================================================================== EXAMPLE START
Global $msg, $hGUI, $sId_snap, $idCam, $hComboCam = '', $but_snap, $sFile, $isCamera = 0, $pos_control

Global $iWebcam = IniRead(@ScriptDir & "\Config.ini", "WEBCAM", "opt", "1")

_WebcamDS_Init()

If @error <> 0 Or @extended <> 0 Then Exit
_CreateGui(740, 580)

_WebcamDS_RenderWebcam($iWebcam, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])

If @error <> 0 Or @extended <> 0 Then Exit

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $hComboCam
            _WebcamDS_Reset()
            $iWebcam = _GUICtrlComboBox_GetCurSel($hComboCam) + 1
            IniWrite(@ScriptDir & "\Config.ini", "WEBCAM", "opt", $iWebcam)
            _WebcamDS_RenderWebcam($iWebcam, $hGUI, 0, $pos_control[2], $pos_control[3], $pos_control[0], $pos_control[1])
        Case $but_snap
            ;$sFile = FileSaveDialog("Save", @ScriptDir & "\", "Image (*.bmp)", 1 + 4)
            $sFile = @ScriptDir & "\test.bmp"
            _WebcamSnap($sFile, $hGUI)
        Case $GUI_EVENT_CLOSE
            _WebcamDS_ReleaseObjects(1)
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd

Func _CreateGui($w = 640, $h = 480)
    $hGUI = GUICreate("Test Capture", $w, $h, -1, -1, $WS_POPUP)
    GUICtrlCreateLabel("Select webcam:", 115, 12, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    Local $pic_image = GUICtrlCreatePic("", 45, 45, 640, 480)
    GUICtrlSetBkColor(-1, 0xff0000)
    $but_snap = GUICtrlCreateButton("Webcam snap", 585, 9, 100, 25)
    GUICtrlSetFont(-1, 10, 400, Default, 'arial')
    $pos_control = ControlGetPos("Test Capture", '', $pic_image)
    If $hComboCam = '' Then
        For $i = 1 To $aWebcamList[0]
            If $hComboCam = '' Then
                $hComboCam = GUICtrlCreateCombo($aWebcamList[$i], 220, 10, 200, 25)
            Else
                If $aWebcamList[$i] <> '' Then GUICtrlSetData($hComboCam, $aWebcamList[$i])
            EndIf
        Next
    EndIf
    GUISetState()
EndFunc   ;==>_CreateGui
; ================================================================================================== EXAMPLE END

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamSnapt
; Description ...: Save current webcam image
; Syntax.........: _WebcamSnap(Image save address, $Gui_id)
; Parameters ....: none
; Return values .: none
; Date...........: 2019/05/11
; Author ........: Belini
; Remarks .......: Call to capture image
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamSnap($snap = "", $hWnd = "")
    Local $x = $pos_control[0]
    Local $y = $pos_control[1]
    Local $w = $pos_control[2]
    Local $h = $pos_control[3]
    _ScreenCapture_CaptureWnd($snap, $hWnd, $x, $y, $w, $h, False)
EndFunc   ;==>_WebcamSnap

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_Init
; Description ...: Create Direct Show objects and enumerate webcams
; Syntax.........: _WebcamDS_Init()
; Parameters ....: none
; Return values .: Success - it fills 3 array  $aWebcamList
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Remarks .......: call it at the beginning of the script
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_Init()
    Local $hr
    $oGraph = ObjCreateInterface($sCLSID_FilterGraph, $sIID_IGraphBuilder, $tagIGraphBuilder)
    If Not IsObj($oGraph) Then Return SetError(1, 0, 0)
    $oBuild = ObjCreateInterface($sCLSID_CaptureGraphBuilder2, $sIID_ICaptureGraphBuilder2, $tagICaptureGraphBuilder2)
    If Not IsObj($oBuild) Then Return SetError(2, 0, 0)
    $hr = $oBuild.SetFiltergraph($oGraph)
    If $hr < 0 Then Return SetError(0, 1, 0)
    Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)
    If Not IsObj($oMediaControl) Then Return SetError(3, 0, 0)
    Local $pVideoWindow
    $oGraph.QueryInterface($sIID_IVideoWindow, $pVideoWindow)
    $oVideoWindow = ObjCreateInterface($pVideoWindow, $sIID_IVideoWindow, $tagIVideoWindow)
    If Not IsObj($oVideoWindow) Then Return SetError(4, 0, 0)
    Local $pBasicVideo
    $oGraph.QueryInterface($sIID_IBasicVideo, $pBasicVideo)
    $oBasicVideo = ObjCreateInterface($pBasicVideo, $sIID_IBasicVideo, $tagIBasicVideo)
    If Not IsObj($oBasicVideo) Then Return SetError(5, 0, 0)

    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory)
    If $aWebcamList[0] > 0 Then
        If $aWebcamList[0] < $iWebcam Or $aWebcamList[0] = 1 Then $iWebcam = 1
    Else
        $iWebcam = 0
        MsgBox(4096, "ALERT", "No camera found", 5)
        exit
    EndIf
EndFunc   ;==>_WebcamDS_Init

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_ReleaseObjects
; Description ...: release Objects created during script and at the end of it
; Syntax.........: _WebcamDS_ReleaseObjects($iArg = 0)
; Parameters ....: $iArg   - use 0 if you want to delete only the current capture obj, otherwise (ie: at the end) put it to 1
; Return values .: Success - it deletes the objects created
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_ReleaseObjects($iArg = 0)
    $oMediaControl.Stop()
    $oMediaControl = 0
    $oVmr9 = 0
    $oCapture[$iWebcam] = 0
    If $iArg Then $oCapture = 0
    $oBasicVideo = 0
    $oVideoWindow = 0
    $oGraph = 0
    $oBuild = 0
EndFunc   ;==>_WebcamDS_ReleaseObjects

; #FUNCTION# ====================================================================================================================
; Name...........: _WebcamDS_RenderWebcam
; Description ...: call it to set which webcam to display, audio preview, resolution, scale to GUI, zoom
; Syntax.........: _WebcamDS_RenderWebcam($iNumberWebcam=1,$iAudioMic=0,$hGuiWin[,$iScale=1[,$xVideo=640[,$yVideo=480[,$ibpp=16[,$iZoomX=0[,$iZoomY=0[,$iZoomW=0[,$iZoomH=0]]]]]]]])
; Parameters ....: $iNumberWebcam - The webcam to display (from 1)
;                  $hGuiWin     - The GUI you create to display the webcam
;                  $iScale      - if you want to scale the webcam resolution to the res of the GUI, set it to 1, otherwise to 0
;                  $xVideo      - The webcam Width
;                  $yVideo      - The webcam Height
;                  $left        - The left side of image in Gui
;                  $top         - The top side of image in Gui
; Return values .: Success - it displays the webcam selected
;                  Failure - Returns 0 and Sets @Error:
;                  |0 - No error.
; Date...........: 2019/05/11
; Author ........: Frank10
; Modified.......: Belini
; Example .......; Yes
; ===============================================================================================================================
Func _WebcamDS_RenderWebcam($iNumberWebcam, $hGuiWin, $iScale = 1, $xVideo = 640, $yVideo = 480, $left = 0, $top = 0)
    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory, $iNumberWebcam)
    Local $_GUID = "DWORD Data1;" & "WORD  Data2;" & "WORD  Data3;" & "BYTE  Data4[8];"
    Local $_SIZE = "LONG cx;" & "LONG cy;"
    Local $_VIDEO_STREAM_CONFIG_CAPS = $_GUID & "ULONG    VideoStandard;" & $_SIZE & $_SIZE & $_SIZE & "int      CropGranularityX;" & "int      CropGranularityY;" & "int      CropAlignX;" & "int      CropAlignY;" & $_SIZE & $_SIZE & "int      OutputGranularityX;" & "int      OutputGranularityY;" & "int      StretchTapsX;" & "int      StretchTapsY;" & "int      ShrinkTapsX;" & "int      ShrinkTapsY;" & "int64     MinFrameInterval;" & "int64  MaxFrameInterval;" & "LONG     MinBitsPerSecond;" & "LONG     MaxBitsPerSecond;"
    Local $tVideoStream = DllStructCreate($_VIDEO_STREAM_CONFIG_CAPS)
    Local $_AM_MEDIA = $_GUID & $_GUID & 'BOOL  bFixedSizeSamples;' & 'BOOL  bTemporalCompression;' & 'ULONG    lSampleSize;' & $_GUID & 'ptr pUnk;' & 'ULONG    cbFormat;' & 'ptr  pbFormat;'
    Local $t_AM_MEDIA = DllStructCreate($_AM_MEDIA)
    Local $_RECT = 'LONG left;' & 'LONG top;' & 'LONG right;' & 'LONG bottom;'
    Local $_tagBITMAPINFOHEADER = 'DWORD biSize;' & 'LONG  biWidth;' & 'LONG  biHeight;' & 'WORD  biPlanes;' & 'WORD  biBitCount;' & 'DWORD biCompression;' & 'DWORD biSizeImage;' & 'LONG  biXPelsPerMeter;' & 'LONG  biYPelsPerMeter;' & 'DWORD biClrUsed;' & 'DWORD biClrImportant;'
    Local $_tagVIDEOINFOHEADER = $_RECT & $_RECT & 'DWORD            dwBitRate;' & 'DWORD            dwBitErrorRate;' & 'int64   AvgTimePerFrame;' & $_tagBITMAPINFOHEADER
    Local $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], $sIID_IAMStreamConfig, 0)
    Local $iCount = 0, $iSize = 0
    If $hr < 0 Then Return SetError(1, 7, 0)
    If DllStructGetSize($tVideoStream) == $iSize Then
        For $iFormat = 0 To $iCount - 1 Step 1
            Local $pmtConfig = DllStructGetPtr($t_AM_MEDIA)
            Local $t_AM_MEDIA_TYPE = DllStructCreate($_AM_MEDIA, $pmtConfig)
            Local $pbFormat = DllStructGetData($t_AM_MEDIA_TYPE, "pbFormat")
            Local $t_VIDEOINFOHEADER = DllStructCreate($_tagVIDEOINFOHEADER, $pbFormat)
            Local $biCompression = DllStructGetData($t_VIDEOINFOHEADER, 'biCompression')
            Local $sText = ''
            For $i = 7 To 0 Step -2
                If $biCompression <> 0 Then $sText = $sText & Chr(Dec(StringMid(Hex($biCompression, 8), $i, 2)))
            Next
            DllStructSetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame', 10000000 / $FrameRate)
            If $iFormat = $iCount - 1 Then
                DllStructSetData($t_VIDEOINFOHEADER, 'biWidth', $xVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biHeight', $yVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biBitCount', 16)
                If $hr < 0 Then Return SetError(1, 9, 0)
            EndIf
        Next
    EndIf
    If $Vmr9 = 0 Then
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, 0)
        If $hr < 0 Then Return SetError(1, 10, 0)
    Else
        $oVmr9 = ObjCreateInterface($sCLSID_VideoMixingRenderer9, $sIID_IBaseFilter, $tagIBaseFilter)
        If Not IsObj($oVmr9) Then Return SetError(14, 0, 0)
        $hr = $oGraph.AddFilter($oVmr9, "VMR9")
        If $hr < 0 Then Return SetError(1, 11, 0)
        Local $pConfig9, $oConfig9
        $hr = $oVmr9.QueryInterface($sIID_IVMRFilterConfig9, $pConfig9)
        $oConfig9 = ObjCreateInterface($pConfig9, $sIID_IVMRFilterConfig9, $tagIVMRFilterConfig9)
        If Not IsObj($oConfig9) Then Return SetError(15, 0, 0)
        ConsoleWrite('Filterconf hr:' & $hr & '  ' & IsObj($oConfig9) & @CRLF)
        $hr = $oConfig9.SetNumberOfStreams(4)
        If $hr < 0 Then Return SetError(1, 12, 0)
        Local $pMixControl9, $oMixControl9
        $hr = $oVmr9.QueryInterface($sIID_IVMRMixerControl9, $pMixControl9)
        $oMixControl9 = ObjCreateInterface($pMixControl9, $sIID_IVMRMixerControl9, $tagIVMRMixerControl9)
        If Not IsObj($oMixControl9) Then Return SetError(16, 0, 0)
        Local $t_VMR9rect = DllStructCreate($_RECT)
        Local $l = 0, $t = 0, $r = $l + $xVideo, $b = $t + $yVideo
        DllStructSetData($t_VMR9rect, 'left', $l)
        DllStructSetData($t_VMR9rect, 'top', $t)
        DllStructSetData($t_VMR9rect, 'right', $r)
        DllStructSetData($t_VMR9rect, 'bottom', $b)
        Local $streamID = 0
        $hr = $oMixControl9.SetOutputRect($streamID, DllStructGetPtr($t_VMR9rect))
        If $hr < 0 Then Return SetError(1, 13, 0)
        $oConfig9 = 0
        $oMixControl9 = 0
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, $oVmr9)
        If $hr < 0 Then Return SetError(1, 14, 0)
        $oVmr9 = 0
    EndIf
    Local $iX = $left, $iY = $top
    Local $aClientSize = WinGetClientSize($hGuiWin)
    If $iScale = 1 Then
        $xVideo = $aClientSize[0]
        $yVideo = $aClientSize[1]
    EndIf
    $hr = $oVideoWindow.SetWindowPosition($iX, $iY, $xVideo, $yVideo)
    If $hr < 0 Then Return SetError(1, 16, 0)
    $hr = $oVideoWindow.put_Owner($hGuiWin)
    If $hr < 0 Then Return SetError(1, 17, 0)
    $oVideoWindow.put_WindowStyle(BitOR($WS_CHILD, $WS_CLIPCHILDREN))
    If $hr < 0 Then Return SetError(1, 18, 0)
    $hr = $oMediaControl.Run()
    If $hr < 0 Then Return SetError(1, 19, 0)
EndFunc   ;==>_WebcamDS_RenderWebcam

; #INTERNAL_NO_DOC# ===================================================================================================
Func _WebcamDS_EnumerateDevices($CLSID_category, $argument = 99)
    $oDevEnum = ObjCreateInterface($sCLSID_SystemDeviceEnum, $sIID_ICreateDevEnum, $tagICreateDevEnum)

    If Not IsObj($oDevEnum) Then Return SetError(6, 0, 0)
    Local $pEnum = 0
    $oDevEnum.CreateClassEnumerator($CLSID_category, $pEnum, 0)
    $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumMoniker, $tagIEnumMoniker)

    If Not IsObj($oEnum) Then Return SetError(7, 0, 0)
    Local $iPosition = '', $arg = ''
    If StringInStr($CLSID_category, "860bb310") Or StringInStr($CLSID_category, "33d9a762") Then
        $iPosition = $argument
        $arg = ''
    Else
        $iPosition = ''
        $arg = $argument
    EndIf
    Local $pMoniker, $oMoniker, $hr
    Local $i = 1, $pBindObj[99]
    While $oEnum.Next(1, $pMoniker, 0) = $S_OK
        Local $pPropBag, $oPropBag
        $oMoniker = ObjCreateInterface($pMoniker, $sIID_IMoniker, $tagIMoniker)
        If Not IsObj($oMoniker) Then Return SetError(8, 0, 0)
        $hr = $oMoniker.BindToObject(0, 0, $sIID_IBaseFilter, $pBindObj[$i])
        If $hr < 0 Then ExitLoop
        $hr = $oMoniker.BindToStorage(0, 0, $sIID_IPropertyBag, $pPropBag)
        If $hr < 0 Then Return SetError(1, 2, 0)
        $oPropBag = ObjCreateInterface($pPropBag, $sIID_IPropertyBag, $tagIPropertyBag)
        If Not IsObj($oPropBag) Then Return SetError(9, 0, 0)
        Local $var = ''
        $hr = $oPropBag.Read("FriendlyName", $var, 0)
        If $hr < 0 Then Return SetError(1, 3, 0)
        If $iPosition = 99 Then
            If $var <> '' Then
                $aWebcamList[$i] = $var
            EndIf
        EndIf
        If ($iPosition <> '' And $iPosition = $i) Or ($arg <> '' And $var = $arg) Then
            If ($iPosition <> '' And $iPosition = $i) Then
                If Not IsObj($oCapture[$iPosition]) Then
                    $oCapture[$iPosition] = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                    If Not IsObj($oCapture[$iPosition]) Then Return SetError(12, 0, 0)
                EndIf
                $hr = $oGraph.AddFilter($oCapture[$iPosition], "CaptureFilter" & $iWebcam)
                If $hr < 0 Then Return SetError(1, 6, 0)
                WinSetTitle($hGUI, '', $var)
                ExitLoop
            EndIf
        EndIf
        $oPropBag = 0
        $i += 1
    WEnd
    If $iPosition = 99 And StringInStr($CLSID_category, "860bb310") Then $aWebcamList[0] = $i - 1
    $oMoniker = 0
    $oEnum = 0
    $oDevEnum = 0
EndFunc   ;==>_WebcamDS_EnumerateDevices

Func _WebcamDS_Reset()
    _WebcamDS_ReleaseObjects()
    _WebcamDS_Init()
EndFunc   ;==>_WebcamDS_Reset
; #INTERNAL_NO_DOC# ===================================================================================================

 

Link to comment
Share on other sites

Several compile errors when I "test compiled" the udf:

Local $FrameRate = GUICtrlRead($FrameInput)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Users\whiggs\Downloads\WebcamDS_UDF.au3"(559,23) : warning: $hGUI: possibly used before declaration.
                    WinSetTitle($hGUI,
                    ~~~~~~~~~~~~~~~~~^
"C:\Users\whiggs\Downloads\WebcamDS_UDF.au3"(593,33) : warning: $UserDLL: possibly used before declaration.
    While _IsPressed("01", $UserDLL)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Users\whiggs\Downloads\WebcamDS_UDF.au3"(241,46) : error: $FrameInput: undeclared global variable.
            Local $FrameRate = GUICtrlRead($FrameInput)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"C:\Users\whiggs\Downloads\WebcamDS_UDF.au3"(559,23) : error: $hGUI: undeclared global variable.
                    WinSetTitle($hGUI,
                    ~~~~~~~~~~~~~~~~~^
"C:\Users\whiggs\Downloads\WebcamDS_UDF.au3"(593,33) : error: $UserDLL: undeclared global variable.
    While _IsPressed("01", $UserDLL)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

I mention this because, when I run the example script in the original post, while I am able to hear the input from the microphone, there is no video, which I assume their should be since my integrated webcam light turns on.  Attempting to change the codec results in the whole application crashing.  But when I do exit the example udf (I have been launching it directly from Scite for autoit), scite will automatically open the "WebcamDS_UDF.au3" file and immediately reference these compile errors.  Great start, but not usable.

 

Edit: Adding #include <Misc.au3> at the beggining of the udf got rid of the undefined function compile error, but the above errors still remain.

Edited by MattHiggs
Link to comment
Share on other sites

  • 4 years later...

@Belini asked for support.

I think that 

Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)

This ObjCreateInterface needs TAG structure

I'm not Windows API magican. so as for now I was able only to 

Global Const $tagIMediaControl = $tagIDispatch & _
        "Run hresult( );" & _
        "Stop hresult( );" & _
        "Pause hresult( );" & _
        ""

and

Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl, $tagIMediaControl)


Help needed in properly TAG structure creation.
Doc: https://learn.microsoft.com/en-us/windows/win32/api/control/nn-control-imediacontrol

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

modified verision:

 

WebcamDS_UDF.zip

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

@mLipok the UDF you modified opens the interface but does not show the camera image and returns several errors "NULL Pointer assignment" and "Variable must be of type 'Object'." Then I tested with the UDF without changes, it opens the camera image without errors.

WebcamDS_ExampleUDF.au3 (490) : ==> COM Error intercepted !
    err.number is:      0x00000003
    err.windescription: NULL Pointer assignment
    err.description is:     
    err.source is:      
    err.helpfile is:    
    err.helpcontext is:     
    err.lastdllerror is:    0
    err.scriptline is:  490
    err.retcode is:     0x00000000

WebcamDS_ExampleUDF.au3 (504) : ==> COM Error intercepted !
    err.number is:      0x000000A9
    err.windescription: Variable must be of type 'Object'.
    err.description is:     
    err.source is:      
    err.helpfile is:    
    err.helpcontext is:     
    err.lastdllerror is:    0
    err.scriptline is:  504
    err.retcode is:     0x00000000

WebcamDS_ExampleUDF.au3 (490) : ==> COM Error intercepted !
    err.number is:      0x00000003
    err.windescription: NULL Pointer assignment
    err.description is:     
    err.source is:      
    err.helpfile is:    
    err.helpcontext is:     
    err.lastdllerror is:    0
    err.scriptline is:  490
    err.retcode is:     0x00000000

WebcamDS_ExampleUDF.au3 (504) : ==> COM Error intercepted !
    err.number is:      0x000000A9
    err.windescription: Variable must be of type 'Object'.
    err.description is:     
    err.source is:      
    err.helpfile is:    
    err.helpcontext is:     
    err.lastdllerror is:    0
    err.scriptline is:  504
    err.retcode is:     0x00000000

WebcamDS_ExampleUDF.au3 (274) : ==> COM Error intercepted !
    err.number is:      0x00000003
    err.windescription: NULL Pointer assignment
    err.description is:     
    err.source is:      
    err.helpfile is:    
    err.helpcontext is:     
    err.lastdllerror is:    0
    err.scriptline is:  274
    err.retcode is:     0x00000000

+>16:41:49 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 13.331

 

Link to comment
Share on other sites

I tested with the _WebcamDS_SaveFile() function that @mLipok changed and I kept the rest done by @frank10 and the recording worked, but sometimes the audio and video are out of sync.

UDF made by Frank10 with changes made to SaveFile() by mLipok (Recording working)

#include <Misc.au3>
#include <WinAPI.au3>
#include <GuiComboBox.au3>
#include <SendMessage.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

; ========================================================================================================================= VARIABLES
Global $AudioPrevBox, $AudioCapBox, $FrameInput, $SaveButton, $StopButton, $ZoomButton, $FullButton
Global $hComboCam = '', $hComboComp = '', $hComboMic, $ZoomInput = ''
Global $aWebcamList[9]
Global $aCompressorList[50]
Global $aMicList[9]
Global $iWebcam = 1, $iWebAudio = 0, $iWebRescale = 1, $iWebX, $iWebY, $iWebBpp, $aZoomPos[4]
Global $oBasicAudio, $oBasicVideo, $oBuild, $oCapture[9], $oCaptureAudio, $oGraph, $oIBaseFilter, $oMediaControl, $oDevEnum, $oEnum, $oICreateDevEnum, $oIEnumMoniker
Global $oVideoWindow, $oVmr9, $oCompressor, $oStreamConfig
Global $Vmr9 = 1, $iWebXinside = 0, $iWebYinside = 0
Global Const $CompressionCaps_CanKeyFrame = 0x04
Global Const $sIID_IMediaControl = '{56A868B1-0AD4-11CE-B03A-0020AF0BA770}'
Global Const $sCLSID_VideoInputDeviceCategory = '{860bb310-5d01-11d0-bd3b-00a0c911ce86}'
Global Const $sCLSID_AudioInputDeviceCategory = '{33d9a762-90c8-11d0-bd43-00a0c911ce86}'
Global Const $sCLSID_VideoCompressorCategory = '{33d9a760-90c8-11d0-bd43-00a0c911ce86}'
Global Const $S_OK = 0
Global Const $PIN_CATEGORY_CAPTURE = '{fb6c4281-0353-11d1-905f-0000c0cc16ba}'
Global Const $PIN_CATEGORY_PREVIEW = '{fb6c4282-0353-11d1-905f-0000c0cc16ba}'
Global Const $MEDIASUBTYPE_Avi = '{e436eb88-524f-11ce-9f53-0020af0ba770}'
Global Const $MEDIATYPE_Video = '{73646976-0000-0010-8000-00aa00389b71}'
Global Const $MEDIATYPE_Audio = '{73647561-0000-0010-8000-00aa00389b71}'
Global Const $sCLSID_VideoMixingRenderer9 = '{51b4abf3-748f-4e3b-a276-c828330e926a}'
Global Const $sCLSID_FilterGraph = "{E436EBB3-524F-11CE-9F53-0020AF0BA770}"
Global Const $sIID_IGraphBuilder = "{56A868A9-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIGraphBuilder = "AddFilter hresult(ptr;wstr);" & "RemoveFilter hresult(ptr);" & "EnumFilters hresult(ptr*);" & "FindFilterByName hresult(wstr;ptr*);" & "ConnectDirect hresult(ptr;ptr;ptr);" & "Reconnect hresult(ptr);" & "Disconnect hresult(ptr);" & "SetDefaultSyncSource hresult();" & "Connect hresult(ptr;ptr);" & "Render hresult(ptr);" & "RenderFile hresult(wstr;ptr);" & "AddSourceFilter hresult(wstr;wstr;ptr*);" & "SetLogFile hresult(dword_ptr);" & "Abort hresult();" & "ShouldOperationContinue hresult();"
Global Const $sCLSID_CaptureGraphBuilder2 = '{BF87B6E1-8C27-11D0-B3F0-00AA003761C5}'
Global Const $sIID_ICaptureGraphBuilder2 = '{93e5a4e0-2d50-11d2-abfa-00a0c9c6e38d}'
Global Const $tagICaptureGraphBuilder2 = "SetFiltergraph hresult(ptr);" & "GetFiltergraph hresult(ptr*);" & "SetOutputFileName hresult(clsid;wstr;ptr*;ptr*);" & "FindInterface hresult(clsid;clsid;ptr;clsid;ptr*);" & "RenderStream hresult(clsid;clsid;ptr;ptr;ptr);" & "ControlStream hresult(clsid;clsid;ptr;ptr;ptr;word;word);" & "AllocCapFile hresult(wstr;int64);" & "CopyCaptureFile hresult(wstr;wstr;int;ptr*);" & "FindPin hresult(ptr;int;clsid;clsid;bool;int;ptr*);"
Global Const $sCLSID_SystemDeviceEnum = '{62BE5D10-60EB-11d0-BD3B-00A0C911CE86}'
Global Const $sIID_ICreateDevEnum = '{29840822-5b84-11d0-bd3b-00a0c911ce86}'
Global Const $tagICreateDevEnum = "CreateClassEnumerator hresult( clsid ; ptr*; dword );"
Global Const $sIID_IEnumMoniker = '{00000102-0000-0000-c000-000000000046}'
Global Const $tagIEnumMoniker = "Next hresult(dword;ptr*;dword*);" & "Skip hresult(dword);" & "Reset hresult();" & "Clone hresult(ptr*);"
Global Const $sIID_IBaseFilter = '{56a86895-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIBaseFilter = "GetClassID hresult(ptr*);" & "Stop hresult();" & "Pause hresult();" & "Run hresult(int64);" & "GetState hresult(dword;dword*);" & "SetSyncSource hresult(ptr);" & "GetSyncSource hresult(ptr*);" & "EnumPins hresult(ptr*);" & "FindPin hresult(wstr;ptr*);" & "QueryFilterInfo hresult(ptr*);" & "JoinFilterGraph hresult(ptr;wstr);" & "QueryVendorInfo hresult(wstr*);"
Global Const $sIID_IMoniker = '{0000000f-0000-0000-C000-000000000046}'
Global Const $tagIMoniker = "GetClassID hresult( clsid )" & "IsDirty hresult(  );" & "Load hresult( ptr );" & "Save hresult( ptr, bool );" & "GetSizeMax hresult( uint64 );" & "BindToObject hresult( ptr;ptr;clsid;ptr*);" & "BindToStorage hresult( ptr;ptr;clsid;ptr*);" & "Reduce hresult( ptr;dword;ptr*;ptr*);" & "ComposeWith hresult( ptr;bool;ptr*);" & "Enum hresult( bool;ptr*);" & "IsEqual hresult( ptr);" & "Hash hresult( dword*);" & "IsRunning hresult( ptr;ptr;ptr);" & "GetTimeOfLastChange hresult( ptr;ptr;int64*);" & "Inverse hresult( ptr*);" & "CommonPrefixWith hresult( ptr;ptr*);" & "RelativePathTo hresult( ptr;ptr*);" & "GetDisplayName hresult( ptr;ptr;wstr*);" & "ParseDisplayName hresult( ptr;ptr;wstr;ulong*;ptr*);" & "IsSystemMoniker hresult( dword*);"
Global Const $tagIDispatch = "GetTypeInfoCount hresult(dword*);" & "GetTypeInfo hresult(dword;dword;ptr*);" & "GetIDsOfNames hresult(clsid;ptr;dword;dword;ptr);" & "Invoke hresult(dword;clsid;dword;word;ptr;ptr;ptr;dword*);"
Global Const $sIID_IVideoWindow = "{56A868B4-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIVideoWindow = $tagIDispatch & "put_Caption hresult(bstr);" & "get_Caption hresult(bstr*);" & "put_WindowStyle hresult(long);" & "get_WindowStyle hresult(long*);" & "put_WindowStyleEx hresult(long);" & "put_WindowStyleEx hresult(long*);" & "put_AutoShow hresult(long);" & "get_AutoShow hresult(long*);" & "put_WindowState hresult(long);" & "get_WindowState hresult(long*);" & "put_BackgroundPalette hresult(long);" & "get_BackgroundPalette hresult(long*);" & "put_Visible hresult(long);" & "get_Visible hresult(long*);" & "put_Left hresult(long);" & "get_Left hresult(long*);" & "put_Width hresult(long);" & "get_Width hresult(long*);" & "put_Top hresult(long);" & "get_Top hresult(long*);" & "put_Height hresult(long);" & "get_Height hresult(long*);" & "put_Owner hresult(long_ptr);" & "get_Owner hresult(long_ptr*);" & "put_MessageDrain hresult(long_ptr);" & "get_MessageDrain hresult(long_ptr*);" & "get_BorderColor hresult(long*);" & "put_BorderColor hresult(long);" & "get_FullScreenMode hresult(long*);" & "put_FullScreenMode hresult(long);" & "SetWindowForeground hresult(long);" & "NotifyOwnerMessage hresult(long_ptr;long;long_ptr;long_ptr);" & "SetWindowPosition hresult(long;long;long;long);" & "GetWindowPosition hresult(long*;long*;long*;long*);" & "GetMinIdealImageSize hresult(long*;long*);" & "GetMaxIdealImageSize hresult(long*;long*);" & "GetRestorePosition hresult(long*;long*;long*;long*);" & "HideCursor hresult(long);" & "IsCursorHidden hresult(long*);"
Global Const $sIID_IBasicVideo = "{56A868B5-0AD4-11CE-B03A-0020AF0BA770}"
Global Const $tagIBasicVideo = $tagIDispatch & "get_AvgTimePerFrame hresult(double*);" & "get_BitRate hresult(long*);" & "get_BitErrorRate hresult(long*);" & "get_VideoWidth hresult(long*);" & "get_VideoHeight hresult(long*);" & "put_SourceLeft hresult(long);" & "get_SourceLeft hresult(long*);" & "put_SourceWidth hresult(long);" & "get_SourceWidth hresult(long*);" & "put_SourceTop hresult(long);" & "get_SourceTop hresult(long*);" & "put_SourceHeight hresult(long);" & "get_SourceHeight hresult(long*);" & "put_DestinationLeft hresult(long);" & "get_DestinationLeft hresult(long*);" & "put_DestinationWidth hresult(long);" & "get_DestinationWidth hresult(long*);" & "put_DestinationTop hresult(long);" & "get_DestinationTop hresult(long*);" & "put_DestinationHeight hresult(long);" & "get_DestinationHeight hresult(long*);" & "SetSourcePosition hresult(long;long;long;long);" & "GetSourcePosition hresult(long*;long*;long*;long*);" & "SetDefaultSourcePosition hresult();" & "SetDestinationPosition hresult(long;long;long;long);" & "GetDestinationPosition hresult(long*;long*;long*;long*);" & "SetDefaultDestinationPosition hresult();" & "GetVideoSize hresult(long*;long*);" & "GetVideoPaletteEntries hresult(long;long;long*;long*);" & "GetCurrentImage hresult(long*;long*);" & "IsUsingDefaultSource hresult();" & "IsUsingDefaultDestination hresult();"
Global Const $sIID_IPropertyBag = '{55272a00-42cb-11ce-8135-00aa004bb851}'
Global Const $tagIPropertyBag = 'Read hresult(wstr;variant*;ptr*);' & 'Write hresult(wstr;variant);'
Global Const $tagIVMRWindowlessControl = 'GetNativeVideoSize hresult(long*;long*;long*;long*);' & 'GetMinIdealVideoSize hresult(long*;long*);' & 'GetMaxIdealVideoSize hresult(long*;long*);' & 'SetVideoPosition hresult(ptr;ptr);' & 'GetVideoPosition hresult(ptr*;ptr*);' & 'GetAspectRatioMode hresult(dword*);' & 'SetAspectRatioMode hresult(dword);' & 'SetVideoClippingWindow hresult(hwnd);' & 'RepaintVideo hresult(hwnd;handle);' & 'DisplayModeChanged hresult();' & 'GetCurrentImage hresult(byte*);' & 'SetBorderColor hresult(clr);' & 'GetBorderColor hresult(clr*);' & 'SetColorKey hresult(clr);' & 'GetColorKey hresult(clr*);'
Global Const $tagIVMRFilterConfig = 'SetImageCompositor hresult(ptr);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRFilterConfig9 = '{5a804648-4f66-4867-9c43-4f5c822cf1b8}'
Global Const $tagIVMRFilterConfig9 = 'SetImageCompositor hresult(ptr);' & 'SetNumberOfStreams hresult(dword);' & 'GetNumberOfStreams hresult(dword*);' & 'SetRenderingPrefs hresult(dword);' & 'GetRenderingPrefs hresult(dword*);' & 'SetRenderingMode hresult(dword);' & 'GetrenderingMode hresult(dword*);'
Global Const $sIID_IVMRMixerControl9 = '{1a777eaa-47c8-4930-b2c9-8fee1c1b0f3b}'
Global Const $tagIVMRMixerControl9 = 'SetAlpha(dword;float);' & 'GetAlpha(dword;ptr*) ;' & 'SetZOrder(dword;dword) ;' & 'GetZOrder(dword;ptr*) ;' & 'SetOutputRect(dword;ptr) ;' & 'GetOutputRect(dword;ptr*) ;' & 'SetBackgroundClr(dword) ;' & 'GetBackgroundClr(ptr) ;' & 'SetMixingPrefs(dword) ;' & 'GetMixingPrefs(ptr*) ;' & 'SetProcAmpControl(dword;ptr) ;' & 'GetProcAmpControl(dword;ptr*) ;' & 'GetProcAmpControlRange(dword;ptr*);'
Global Const $sIID_IAMStreamConfig = '{c6e13340-30ac-11d0-a18c-00a0c9118956}'
Global Const $tagIAMStreamConfig = 'SetFormat hresult(ptr);' & 'GetFormat hresult(ptr*);' & 'GetNumberOfCapabilities hresult(int*;int*);' & 'GetStreamCaps hresult(int;ptr*;struct*);'
Global Const $sIID_IAMVideoCompression = '{c6e13343-30ac-11d0-a18c-00a0c9118956}'
Global Const $tagIAMVideoCompression = 'put_KeyFrameRate hresult(int);' & 'get_KeyFrameRate hresult(int*);' & 'put_PFramesPerKeyFrame hresult(int);' & 'get_PFramesPerKeyFrame hresult(int*);' & 'put_Quality hresult(double);' & 'get_Quality hresult(double*);' & 'put_WindowSize hresult();' & 'get_WindowSize hresult(uint64*);' & 'getInfo hresult(ptr*;int;ptr*;int*;int*;int*;double*;int*);' & 'OverrideKeyFrame hresult(int);' & 'OverrideFrameSize hresult(int;int);'
Global Const $sIID_IEnumPins = '{56a86892-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIEnumPins = 'Next hresult(dword;ptr*;dword*);' & 'Skip hresult(dword);' & 'Reset hresult();' & 'Clone hresult(ptr*);'
Global Const $sIID_IPin = '{56a86891-0ad4-11ce-b03a-0020af0ba770}'
Global Const $tagIPin = 'Connect hresult(ptr;ptr);' & 'ReceiveConnection hresult(ptr;ptr);' & 'Disconnect hresult();' & 'ConnectedTo hresult(ptr*);' & "ConnectionMediaType hresult(ptr*);" & "QueryPinInfo hresult(ptr*);" & "QueryDirection hresult(int*);" & "QueryId hresult(wstr*);" & "QueryAccept hresult(ptr);" & "EnumMediaTypes hresult(ptr*);" & "QueryInternalConnections hresult(ptr*;dword*);" & "EndOfStream hresult();" & "BeginFlush hresult();" & "EndFlush hresult();" & "NewSegment hresult(int64;int64;double);"
Global Const $tagIAMAudioInputMixer = 'put_Enable hresult(bool);' & 'get_Enable hresult(bool*);' & 'put_Mono hresult(bool);' & 'get_Mono hresult(bool*);' & 'put_MixLevel hresult(double);' & 'get_MixLevel hresult(double*);' & 'put_Pan hresult(double);' & 'get_Pan hresult(double*);' & 'put_Loudness hresult(bool);' & 'get_Loudness hresult(bool*);' & 'put_Treble hresult(double);' & 'get_Treble hresult(double*);' & 'get_TrebleRange hresult(double*);' & 'put_Bass hresult(double);' & 'get_Bass hresult(double*);' & 'get_BassRange hresult(double*);'
$aZoomPos[0] = 1
$aZoomPos[1] = 0
$aZoomPos[2] = 1
$aZoomPos[3] = 0
; ========================================================================================================================= VARIABLES

; =========================================================================================================================== EXAMPLE
Global $UserDLL = DllOpen("user32.dll")
Global $hGUI = GUICreate("DirectShow Capture", 1280, 720, -1, -1, -1)

Opt("MustDeclareVars", 1)
OnAutoItExitRegister("_onExit")

Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc")
$iWebXinside = 0
$iWebYinside = 26

_WebcamDS_Init()
If @error <> 0 Or @extended <> 0 Then Exit
_CreateGui()
_WebcamDS_RenderWebcam(1, 0, $hGUI, 0, 640, 480, 16)

If @error <> 0 Or @extended <> 0 Then Exit
If $iWebAudio <> 0 And GUICtrlRead($AudioPrevBox) = $GUI_UNCHECKED Then GUICtrlSetState($AudioPrevBox, $GUI_CHECKED)

Global $msg
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $SaveButton
            Local $audio = 0, $comp
            If GUICtrlRead($AudioCapBox) = $GUI_CHECKED Then $audio = _GUICtrlComboBox_GetCurSel($hComboMic) + 1
            $comp = GUICtrlRead($hComboComp)
            _WebcamDS_SaveFile(@ScriptDir & '\test.avi', $comp, $audio)
        Case $msg = $StopButton
            _WebcamDS_Reset()
            _WebcamDS_RenderWebcam($iWebcam, $iWebAudio, $hGUI, $iWebRescale, $iWebX, $iWebY, 16)
        Case $msg = $hComboMic
            If GUICtrlRead($AudioPrevBox) = $GUI_CHECKED Then
                $iWebAudio = _GUICtrlComboBox_GetCurSel($hComboMic) + 1
                If $iWebAudio = 3 Then $iWebAudio = 4
            EndIf
        Case $msg = $hComboCam
            _WebcamDS_Reset()
            $aZoomPos[0] = 1
            $aZoomPos[1] = 0
            $aZoomPos[2] = 1
            $aZoomPos[3] = 0
            $iWebcam = _GUICtrlComboBox_GetCurSel($hComboCam) + 1
            _WebcamDS_RenderWebcam($iWebcam, $iWebAudio, $hGUI, $iWebRescale, $iWebX, $iWebY, 16)
        Case $msg = $ZoomButton
            _WebcamDS_Reset()
            If $ZoomInput <> '' Then
                Local $aZoom = StringSplit($ZoomInput, ',')
                _WebcamDS_RenderWebcam($iWebcam, $iWebAudio, $hGUI, $iWebRescale, $iWebX, $iWebY, 16, $aZoom[1], $aZoom[2], $aZoom[3], $aZoom[4])
                $ZoomInput = ''
            Else
                _WebcamDS_RenderWebcam($iWebcam, $iWebAudio, $hGUI, $iWebRescale, $iWebX, $iWebY, 16)
                $aZoomPos[0] = 1
                $aZoomPos[1] = 0
                $aZoomPos[2] = 1
                $aZoomPos[3] = 0
            EndIf
        Case $msg = $AudioPrevBox
            If GUICtrlRead($AudioPrevBox) = $GUI_CHECKED Then
                $iWebAudio = _GUICtrlComboBox_GetCurSel($hComboMic) + 1
                If $iWebAudio = 3 Then $iWebAudio = 4
            Else
                $iWebAudio = 0
            EndIf
        Case $msg = $FullButton
            $oVideoWindow.put_FullScreenMode(-1)
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
    EndSelect
    If _IsPressed('70', $UserDLL) Then
        Local $full
        $oVideoWindow.get_FullScreenMode($full)
        If $full = -1 Then
            $oVideoWindow.put_FullScreenMode(0)
            _WebcamDS_Reset()
        EndIf
    EndIf
    If _IsPressed("01", $UserDLL) Then
        Local $aMouse_Pos = MouseGetPos()
        Local $iX1 = $aMouse_Pos[0]
        Local $iY1 = $aMouse_Pos[1]
        Local $aGUIPos = WinGetPos($hGUI)
        If WinGetState($hGUI) = 15 And $iX1 >= $aGUIPos[0] + 2 And $iX1 < $aGUIPos[0] + $aGUIPos[2] And $iY1 >= $aGUIPos[1] + 50 And $iY1 < $aGUIPos[1] + $aGUIPos[3] Then
            Local $sZoom = _WebcamDS_SelectRect($hGUI)
            If StringLen($sZoom) > 2 Then
                $ZoomInput = $sZoom
                ControlClick($hGUI, "", $ZoomButton)
            EndIf
        EndIf
    EndIf
WEnd
Func _CreateGui()
    Global $hGUI = GUICreate("DirectShow Capture", 1280, 720, -1, -1, -1)
    Global $AudioPrevBox = GUICtrlCreateCheckbox("AuPrev", 420, 0, 50, 25)
    Global $AudioCapBox = GUICtrlCreateCheckbox("AuCap", 795, 0, 50, 25)
    Global $FrameInput = GUICtrlCreateInput("30", 850, 0, 40, 25)
    Global $SaveButton = GUICtrlCreateButton("Save", 875, 0, 60, 25)
    Global $StopButton = GUICtrlCreateButton("Stop", 940, 0, 60, 25)
    Global $ZoomButton = GUICtrlCreateButton("Reset", 1120, 0, 70, 25)
    Global $FullButton = GUICtrlCreateButton("FullScreen", 1200, 0, 60, 25)
    GUISetState()
    If $hComboCam = '' Then
        For $i = 1 To $aWebcamList[0]
            If $hComboCam = '' Then
                $hComboCam = GUICtrlCreateCombo($aWebcamList[$i], 0, 0, 200, 25)
            Else
                If $aWebcamList[$i] <> '' Then GUICtrlSetData($hComboCam, $aWebcamList[$i])
            EndIf
        Next
    EndIf
    If $hComboComp = '' Then
        For $i = 1 To $aCompressorList[0] + 1
            If $hComboComp = '' Then
                $hComboComp = GUICtrlCreateCombo($aCompressorList[$i], 475, 0, 200, 25)
            Else
                GUICtrlSetData($hComboComp, $aCompressorList[$i])
            EndIf
            ConsoleWrite($aCompressorList[$i] & @CRLF)
        Next
    EndIf
    If $hComboMic = '' Then
        For $i = 1 To $aMicList[0]
            If $hComboMic = '' Then
                $hComboMic = GUICtrlCreateCombo($aMicList[$i], 210, 0, 200, 25)
            Else
                GUICtrlSetData($hComboMic, $aMicList[$i])
            EndIf
        Next
    EndIf
EndFunc   ;==>_CreateGui
Func _onExit()
    ConsoleWrite('exit @error:' & @error & '@ext:' & @extended & @CRLF)
    _WebcamDS_ReleaseObjects(1)
    DllClose($UserDLL)
EndFunc   ;==>_onExit
; =========================================================================================================================== EXAMPLE

; ============================================================================================================ FUNCTIONS _WEBCAMDS
Func _WebcamDS_Init()
    Local $hr, $aCall
    $oGraph = ObjCreateInterface($sCLSID_FilterGraph, $sIID_IGraphBuilder, $tagIGraphBuilder)
    If Not IsObj($oGraph) Then Return SetError(1, 0, 0)
    $oBuild = ObjCreateInterface($sCLSID_CaptureGraphBuilder2, $sIID_ICaptureGraphBuilder2, $tagICaptureGraphBuilder2)
    If Not IsObj($oBuild) Then Return SetError(2, 0, 0)
    $hr = $oBuild.SetFiltergraph($oGraph)
    If $hr < 0 Then Return SetError(0, 1, 0)
    Local $pMediaControl
    $oGraph.QueryInterface($sIID_IMediaControl, $pMediaControl)
    $oMediaControl = ObjCreateInterface($pMediaControl, $sIID_IMediaControl)
    If Not IsObj($oMediaControl) Then Return SetError(3, 0, 0)
    Local $pVideoWindow
    $oGraph.QueryInterface($sIID_IVideoWindow, $pVideoWindow)
    $oVideoWindow = ObjCreateInterface($pVideoWindow, $sIID_IVideoWindow, $tagIVideoWindow)
    If Not IsObj($oVideoWindow) Then Return SetError(4, 0, 0)
    Local $pBasicVideo
    $oGraph.QueryInterface($sIID_IBasicVideo, $pBasicVideo)
    $oBasicVideo = ObjCreateInterface($pBasicVideo, $sIID_IBasicVideo, $tagIBasicVideo)
    If Not IsObj($oBasicVideo) Then Return SetError(5, 0, 0)
    If $aWebcamList[1] = '' Then
        _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory)
        _WebcamDS_EnumerateDevices($sCLSID_VideoCompressorCategory)
        _WebcamDS_EnumerateDevices($sCLSID_AudioInputDeviceCategory)
    EndIf
EndFunc   ;==>_WebcamDS_Init
Func _WebcamDS_EnumerateDevices($CLSID_category, $argument = 99)
    $oDevEnum = ObjCreateInterface($sCLSID_SystemDeviceEnum, $sIID_ICreateDevEnum, $tagICreateDevEnum)
    If Not IsObj($oDevEnum) Then Return SetError(6, 0, 0)
    Local $pEnum = 0
    $oDevEnum.CreateClassEnumerator($CLSID_category, $pEnum, 0)
    $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumMoniker, $tagIEnumMoniker)
    If Not IsObj($oEnum) Then Return SetError(7, 0, 0)
    Local $iPosition = '', $arg = ''
    If StringInStr($CLSID_category, "860bb310") Or StringInStr($CLSID_category, "33d9a762") Then
        $iPosition = $argument
        $arg = ''
    Else
        $iPosition = ''
        $arg = $argument
    EndIf
    Local $pMoniker, $oMoniker, $hr
    Local $i = 1, $pBindObj[99]
    While $oEnum.Next(1, $pMoniker, 0) = $S_OK
        Local $pPropBag, $oPropBag
        $oMoniker = ObjCreateInterface($pMoniker, $sIID_IMoniker, $tagIMoniker)
        If Not IsObj($oMoniker) Then Return SetError(8, 0, 0)
        $hr = $oMoniker.BindToObject(0, 0, $sIID_IBaseFilter, $pBindObj[$i])
        If $hr < 0 Then ExitLoop
        $hr = $oMoniker.BindToStorage(0, 0, $sIID_IPropertyBag, $pPropBag)
        If $hr < 0 Then Return SetError(1, 2, 0)
        $oPropBag = ObjCreateInterface($pPropBag, $sIID_IPropertyBag, $tagIPropertyBag)
        If Not IsObj($oPropBag) Then Return SetError(9, 0, 0)
        Local $var = ''
        $hr = $oPropBag.Read("FriendlyName", $var, 0)
        If $hr < 0 Then Return SetError(1, 3, 0)
        If $iPosition = 99 Then
            If StringInStr($CLSID_category, "33d9a762") Then
                If $var <> '' Then
                    $aMicList[$i] = $var
                EndIf
            Else
                If $var <> '' Then
                    $aWebcamList[$i] = $var
                EndIf
            EndIf
            Local $oCaptureTemp = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
            $oCaptureTemp = 0
        EndIf
        If $iPosition = '' And $arg = 99 Then
            If $var <> '' Then
                $aCompressorList[$i] = $var
            EndIf
        EndIf
        If ($iPosition <> '' And $iPosition = $i) Or ($arg <> '' And $var = $arg) Then
            If ($iPosition = '' And $arg = $var) Then
                If Not IsObj($oCompressor) Then
                    $oCompressor = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                    If Not IsObj($oCompressor) Then Return SetError(10, 0, 0)
                EndIf
                $hr = $oGraph.AddFilter($oCompressor, "Compressor" & $iWebcam)
                If $hr < 0 Then Return SetError(1, 4, 0)
                ExitLoop
            EndIf
            If ($iPosition <> '' And $iPosition = $i) Then
                If StringInStr($CLSID_category, "33d9a762") Then
                    If Not IsObj($oCaptureAudio) Then
                        $oCaptureAudio = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                        If Not IsObj($oCaptureAudio) Then Return SetError(11, 0, 0)
                        ConsoleWrite($iWebAudio & 'capAu:' & $i & @CRLF)
                    EndIf
                    $hr = $oGraph.AddFilter($oCaptureAudio, $var)
                    If $hr < 0 Then Return SetError(1, 5, 0)
                Else
                    If Not IsObj($oCapture[$iPosition]) Then
                        $oCapture[$iPosition] = ObjCreateInterface($pBindObj[$i], $sIID_IBaseFilter, $tagIBaseFilter)
                        If Not IsObj($oCapture[$iPosition]) Then Return SetError(12, 0, 0)
                    EndIf
                    $hr = $oGraph.AddFilter($oCapture[$iPosition], "CaptureFilter" & $iWebcam)
                    If $hr < 0 Then Return SetError(1, 6, 0)
                    WinSetTitle($hGUI, '', $var)
                EndIf
                ExitLoop
            EndIf
        EndIf
        $oPropBag = 0
        $i += 1
    WEnd
    If $iPosition = 99 And StringInStr($CLSID_category, "33d9a762") Then $aMicList[0] = $i - 1
    If $iPosition = 99 And StringInStr($CLSID_category, "860bb310") Then $aWebcamList[0] = $i - 1
    If $iPosition = '' And $arg = 99 Then
        $aCompressorList[$i] = 'Uncompressed'
        $aCompressorList[0] = $i - 1
    EndIf
    $oMoniker = 0
    $oEnum = 0
    $oDevEnum = 0
EndFunc   ;==>_WebcamDS_EnumerateDevices
Func _WebcamDS_RenderWebcam($iNumberWebcam, $iAudioMic, $hGuiWin, $iScale = 1, $xVideo = 640, $yVideo = 480, $ibpp = 16, $iZoomX = 0, $iZoomY = 0, $iZoomW = 0, $iZoomH = 0)
    Local $hr
    $iWebcam = $iNumberWebcam
    $iWebAudio = $iAudioMic
    $iWebRescale = $iScale
    $iWebX = $xVideo
    $iWebY = $yVideo
    _WebcamDS_EnumerateDevices($sCLSID_VideoInputDeviceCategory, $iNumberWebcam)
    Local $_GUID = "DWORD Data1;" & "WORD  Data2;" & "WORD  Data3;" & "BYTE  Data4[8];"
    Local $_SIZE = "LONG cx;" & "LONG cy;"
    Local $_VIDEO_STREAM_CONFIG_CAPS = $_GUID & "ULONG    VideoStandard;" & $_SIZE & $_SIZE & $_SIZE & "int      CropGranularityX;" & "int      CropGranularityY;" & "int      CropAlignX;" & "int      CropAlignY;" & $_SIZE & $_SIZE & "int      OutputGranularityX;" & "int      OutputGranularityY;" & "int      StretchTapsX;" & "int      StretchTapsY;" & "int      ShrinkTapsX;" & "int      ShrinkTapsY;" & "int64     MinFrameInterval;" & "int64  MaxFrameInterval;" & "LONG     MinBitsPerSecond;" & "LONG     MaxBitsPerSecond;"
    Local $tVideoStream = DllStructCreate($_VIDEO_STREAM_CONFIG_CAPS)
    Local $_AM_MEDIA = $_GUID & $_GUID & 'BOOL  bFixedSizeSamples;' & 'BOOL  bTemporalCompression;' & 'ULONG    lSampleSize;' & $_GUID & 'ptr pUnk;' & 'ULONG    cbFormat;' & 'ptr  pbFormat;'
    Local $t_AM_MEDIA = DllStructCreate($_AM_MEDIA)
    Local $_RECT = 'LONG left;' & 'LONG top;' & 'LONG right;' & 'LONG bottom;'
    Local $_tagBITMAPINFOHEADER = 'DWORD biSize;' & 'LONG  biWidth;' & 'LONG  biHeight;' & 'WORD  biPlanes;' & 'WORD  biBitCount;' & 'DWORD biCompression;' & 'DWORD biSizeImage;' & 'LONG  biXPelsPerMeter;' & 'LONG  biYPelsPerMeter;' & 'DWORD biClrUsed;' & 'DWORD biClrImportant;'
    Local $_tagVIDEOINFOHEADER = $_RECT & $_RECT & 'DWORD            dwBitRate;' & 'DWORD            dwBitErrorRate;' & 'int64   AvgTimePerFrame;' & $_tagBITMAPINFOHEADER
    Local $_PinInfo = 'ptr   pFilter;' & 'int64    dir;' & 'wchar    achName[128];'
    Local $pConfig
    $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], $sIID_IAMStreamConfig, $pConfig)
    $oStreamConfig = ObjCreateInterface($pConfig, $sIID_IAMStreamConfig, $tagIAMStreamConfig)
    If Not IsObj($oStreamConfig) Then Return SetError(13, 0, 0)
    Local $iCount = 0, $iSize = 0
    $hr = $oStreamConfig.GetNumberOfCapabilities($iCount, $iSize)
    If $hr < 0 Then Return SetError(1, 7, 0)
    If DllStructGetSize($tVideoStream) == $iSize Then
        For $iFormat = 0 To $iCount - 1 Step 1
            Local $scc = DllStructGetPtr($tVideoStream)
            Local $pmtConfig = DllStructGetPtr($t_AM_MEDIA)
            $hr = $oStreamConfig.GetStreamCaps($iFormat, $pmtConfig, $scc)
            Local $t_AM_MEDIA_TYPE = DllStructCreate($_AM_MEDIA, $pmtConfig)
            Local $pbFormat = DllStructGetData($t_AM_MEDIA_TYPE, "pbFormat")
            Local $t_VIDEOINFOHEADER = DllStructCreate($_tagVIDEOINFOHEADER, $pbFormat)
            Local $videoWidth = DllStructGetData($t_VIDEOINFOHEADER, 'biWidth')
            Local $videoHeight = DllStructGetData($t_VIDEOINFOHEADER, 'biHeight')
            Local $bitCount = DllStructGetData($t_VIDEOINFOHEADER, 'biBitCount')
            Local $dwBitRate = DllStructGetData($t_VIDEOINFOHEADER, 'dwBitRate')
            Local $biCompression = DllStructGetData($t_VIDEOINFOHEADER, 'biCompression')
            Local $biPlanes = DllStructGetData($t_VIDEOINFOHEADER, 'biPlanes')
            Local $biSize = DllStructGetData($t_VIDEOINFOHEADER, 'biSizeImage')
            Local $AvgframeRate = Round(10000000 / DllStructGetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame'))
            Local $sText = ''
            For $i = 7 To 0 Step -2
                If $biCompression <> 0 Then $sText = $sText & Chr(Dec(StringMid(Hex($biCompression, 8), $i, 2)))
            Next
            Local $FrameRate = GUICtrlRead($FrameInput)
            DllStructSetData($t_VIDEOINFOHEADER, 'AvgTimePerFrame', 10000000 / $FrameRate)
            If $videoWidth = $xVideo And $videoHeight = $yVideo And $bitCount = $ibpp Then
                $hr = $oStreamConfig.SetFormat(DllStructGetPtr($t_AM_MEDIA_TYPE))
                If $hr < 0 Then Return SetError(1, 8, 0)
                ExitLoop
            EndIf
            If $iFormat = $iCount - 1 Then
                DllStructSetData($t_VIDEOINFOHEADER, 'biWidth', $xVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biHeight', $yVideo)
                DllStructSetData($t_VIDEOINFOHEADER, 'biBitCount', $ibpp)
                $hr = $oStreamConfig.SetFormat(DllStructGetPtr($t_AM_MEDIA_TYPE))
                If $hr < 0 Then Return SetError(1, 9, 0)
            EndIf
        Next
        WinSetTitle($hGuiWin, '', WinGetTitle($hGuiWin) & ' - ' & $xVideo & 'x' & $yVideo & 'x' & $ibpp)
        $oStreamConfig = 0
    EndIf
    If $iAudioMic <> 0 Then
        _WebcamDS_EnumerateDevices($sCLSID_AudioInputDeviceCategory, $iAudioMic)
        $pConfig = ''
        $hr = $oBuild.FindInterface($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Audio, $oCaptureAudio, $sIID_IAMStreamConfig, $pConfig)
        Global $oAStreamConfig = ObjCreateInterface($pConfig, $sIID_IAMStreamConfig, $tagIAMStreamConfig)
    EndIf
    If $Vmr9 = 0 Then
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, 0)
        If $hr < 0 Then Return SetError(1, 10, 0)
        If IsObj($oCaptureAudio) Then $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Audio, $oCaptureAudio, 0, 0)
    Else
        $oVmr9 = ObjCreateInterface($sCLSID_VideoMixingRenderer9, $sIID_IBaseFilter, $tagIBaseFilter)
        If Not IsObj($oVmr9) Then Return SetError(14, 0, 0)
        $hr = $oGraph.AddFilter($oVmr9, "VMR9")
        If $hr < 0 Then Return SetError(1, 11, 0)
        Local $pConfig9, $oConfig9
        $hr = $oVmr9.QueryInterface($sIID_IVMRFilterConfig9, $pConfig9)
        $oConfig9 = ObjCreateInterface($pConfig9, $sIID_IVMRFilterConfig9, $tagIVMRFilterConfig9)
        If Not IsObj($oConfig9) Then Return SetError(15, 0, 0)
        ConsoleWrite('Filterconf hr:' & $hr & '  ' & IsObj($oConfig9) & @CRLF)
        Local $str = ''
        $hr = $oConfig9.SetNumberOfStreams(4)
        If $hr < 0 Then Return SetError(1, 12, 0)
        ConsoleWrite('NumberStream hr:' & $hr & '  Get:' & $str & @CRLF)
        Local $pMixControl9, $oMixControl9
        $hr = $oVmr9.QueryInterface($sIID_IVMRMixerControl9, $pMixControl9)
        ConsoleWrite('Mixercontrol9 hr:' & $hr & @CRLF)
        $oMixControl9 = ObjCreateInterface($pMixControl9, $sIID_IVMRMixerControl9, $tagIVMRMixerControl9)
        If Not IsObj($oMixControl9) Then Return SetError(16, 0, 0)
        Local $t_VMR9rect = DllStructCreate($_RECT)
        Local $l = 0, $t = 0, $r = $l + $xVideo, $b = $t + $yVideo
        DllStructSetData($t_VMR9rect, 'left', $l)
        DllStructSetData($t_VMR9rect, 'top', $t)
        DllStructSetData($t_VMR9rect, 'right', $r)
        DllStructSetData($t_VMR9rect, 'bottom', $b)
        Local $streamID = 0
        $hr = $oMixControl9.SetOutputRect($streamID, DllStructGetPtr($t_VMR9rect))
        ConsoleWrite('Output rect0 hr:' & $hr & @CRLF)
        If $hr < 0 Then Return SetError(1, 13, 0)
        $oConfig9 = 0
        $oMixControl9 = 0
        $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Video, $oCapture[$iNumberWebcam], 0, $oVmr9)
        If $hr < 0 Then Return SetError(1, 14, 0)
        If IsObj($oCaptureAudio) Then $hr = $oBuild.RenderStream($PIN_CATEGORY_PREVIEW, $MEDIATYPE_Audio, $oCaptureAudio, 0, 0)
        $oVmr9 = 0
    EndIf
    Local $iX = $iWebXinside, $iY = $iWebYinside
    Local $aClientSize = WinGetClientSize($hGuiWin)
    If $iScale = 1 Then
        $xVideo = $aClientSize[0]
        $yVideo = $aClientSize[1]
    EndIf
    If $iZoomW <> 0 Then
        $hr = $oBasicVideo.SetSourcePosition($iZoomX, $iZoomY, $iZoomW, $iZoomH)
        If $hr < 0 Then Return SetError(1, 15, 0)
    EndIf
    $hr = $oVideoWindow.SetWindowPosition($iX, $iY, $xVideo, $yVideo)
    If $hr < 0 Then Return SetError(1, 16, 0)
    $hr = $oVideoWindow.put_Owner($hGuiWin)
    If $hr < 0 Then Return SetError(1, 17, 0)
    $oVideoWindow.put_WindowStyle(BitOR($WS_CHILD, $WS_CLIPCHILDREN))
    If $hr < 0 Then Return SetError(1, 18, 0)
    $hr = $oMediaControl.Run()
    If $hr < 0 Then Return SetError(1, 19, 0)
EndFunc   ;==>_WebcamDS_RenderWebcam
Func _WebcamDS_SaveFile($sFileName, $sCompress = 'Uncompressed', $iAudioMic = 0)
    Local $hr
    $hr = $oMediaControl.Stop()
    _WebcamDS_EnumerateDevices($sCLSID_VideoCompressorCategory, $sCompress)
    If $sCompress <> 'Uncompressed' Then
        Local $i = 0, $pEnum, $oCompress, $oPin
        $hr = $oCompressor.EnumPins($pEnum)
        $oEnum = ObjCreateInterface($pEnum, $sIID_IEnumPins, $tagIEnumPins)
        Local $pPin, $pCompress
        While $oEnum.Next(1, $pPin, 0) = $S_OK
            $oPin = ObjCreateInterface($pPin, $sIID_IPin, $tagIPin)
            $hr = $oPin.QueryInterface($sIID_IAMVideoCompression, $pCompress)
            $oCompress = ObjCreateInterface($pCompress, $sIID_IAMVideoCompression, $tagIAMVideoCompression)
            $oPin = 0
            $i += 1
            If $hr >= 0 Then
                ExitLoop
            EndIf
            $oCompress = 0
        WEnd
        $oEnum = 0
        $oPin = 0
        If $hr >= 0 Then
            Local $Cap, $KeyFrameDef, $PFrameDef, $QualityDef, $KeyFrame, $PFrame, $m_Quality
            $hr = $oCompress.GetInfo(0, 0, 0, 0, $KeyFrameDef, $PFrameDef, $QualityDef, $Cap)
            If $hr >= 0 Then
                If BitAND($Cap, $CompressionCaps_CanKeyFrame) Then
                    $hr = $oCompress.get_KeyFrameRate($KeyFrame)
                    If $hr < 0 Or $KeyFrame < 0 Then $KeyFrame = $KeyFrameDef
                EndIf
            EndIf
        EndIf
    Else
        $oCompressor = 0
    EndIf
    Local $pMux, $oMux
    $hr = $oBuild.SetOutputFileName($MEDIASUBTYPE_Avi, $sFileName, $pMux, 0)
    $oMux = ObjCreateInterface($pMux, $sIID_IBaseFilter, $tagIBaseFilter)
    If Not IsObj($oMux) Then Return SetError(19, 0, 0)
    $oCompressor = "Microsoft Video 1"
    $hr = $oBuild.RenderStream($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Video, $oCapture[$iWebcam], $oCompressor, $oMux)
    If $iAudioMic <> 0 Then
        _WebcamDS_EnumerateDevices($sCLSID_AudioInputDeviceCategory, $iAudioMic)
        $hr = $oBuild.RenderStream($PIN_CATEGORY_CAPTURE, $MEDIATYPE_Audio, $oCaptureAudio, 0, $oMux)
    EndIf
    $oMux = 0
    $oMediaControl.Run()
EndFunc   ;==>_WebcamDS_SaveFile
Func _WebcamDS_ReleaseObjects($iArg = 0)
    $oMediaControl.Stop()
    $oMediaControl = 0
    $oVmr9 = 0
    $oCapture[$iWebcam] = 0
    If $iArg Then $oCapture = 0
    $oCaptureAudio = 0
    $oBasicAudio = 0
    $oBasicVideo = 0
    $oVideoWindow = 0
    $oGraph = 0
    $oBuild = 0
    $oCompressor = 0
EndFunc   ;==>_WebcamDS_ReleaseObjects
Func _WebcamDS_SelectRect($hGuiWin)
    Local $aMouse_Pos, $hMask, $hMaster_Mask, $iTemp
    $aMouse_Pos = MouseGetPos()
    Local $iX1 = $aMouse_Pos[0]
    Local $iY1 = $aMouse_Pos[1]
    While _IsPressed("01", $UserDLL)
        $aMouse_Pos = MouseGetPos()
        Sleep(10)
    WEnd
    Local $iX2 = $aMouse_Pos[0]
    Local $iY2 = $aMouse_Pos[1]
    If $iX2 < $iX1 Then
        $iTemp = $iX1
        $iX1 = $iX2
        $iX2 = $iTemp
    EndIf
    If $iY2 < $iY1 Then
        $iTemp = $iY1
        $iY1 = $iY2
        $iY2 = $iTemp
    EndIf
    Local $aGUIPos = WinGetPos($hGuiWin)
    Local $aGUISize = WinGetClientSize($hGuiWin)
    Local $sZoom
    If $iX1 <> $iX2 Then
        If $iWebRescale = 1 And $aZoomPos[0] = 1 Then
            $aZoomPos[0] = $aGUISize[0] / $iWebX
            $aZoomPos[2] = $aGUISize[1] / $iWebY
        EndIf
        $sZoom = ($iX1 - $aGUIPos[0]) / $aZoomPos[0] + $aZoomPos[1] & "," & ($iY1 - ($aGUIPos[1] + 50)) / $aZoomPos[2] + $aZoomPos[3] & "," & ($iX2 - $iX1) / $aZoomPos[0] & "," & ($iY2 - $iY1) / $aZoomPos[2]
        Local $aZoomPosTemp[4], $iSizeX, $iSizeY
        If $iWebRescale = 1 Then
            $iSizeX = $aGUISize[0]
            $iSizeY = $aGUISize[1]
        Else
            $iSizeX = $iWebX
            $iSizeY = $iWebY
        EndIf
        $aZoomPosTemp[0] = $iSizeX / (($iX2 - $iX1) / $aZoomPos[0])
        $aZoomPosTemp[2] = $iSizeY / (($iY2 - $iY1) / $aZoomPos[2])
        $aZoomPosTemp[1] = ($iX1 - $aGUIPos[0]) / $aZoomPos[0] + $aZoomPos[1]
        $aZoomPosTemp[3] = ($iY1 - ($aGUIPos[1] + 50)) / $aZoomPos[2] + $aZoomPos[3]
        $aZoomPos[0] = $aZoomPosTemp[0]
        $aZoomPos[1] = $aZoomPosTemp[1]
        $aZoomPos[2] = $aZoomPosTemp[2]
        $aZoomPos[3] = $aZoomPosTemp[3]
        Return $sZoom
    EndIf
EndFunc   ;==>_WebcamDS_SelectRect
Func _WebcamDS_Reset()
    _WebcamDS_ReleaseObjects()
    _WebcamDS_Init()
EndFunc   ;==>_WebcamDS_Reset
; ============================================================================================================ FUNCTIONS _WEBCAMDS
Edited by Belini
Link to comment
Share on other sites

3 hours ago, Belini said:

but sometimes the audio and video are out of sync.

How old is your computer ?
Which codec you choose ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

On 11/4/2023 at 8:50 PM, Belini said:

does not show the camera image and returns several errors "NULL Pointer assignment" and "Variable must be of type 'Object'."

I had no such problems.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...