#Region - TimeStamp
; 2011-09-13 18:57:02   v 0.1
#EndRegion - TimeStamp

#include-once
#include <WindowsConstants.au3>
#include <WebcamConstants.au3>


;===============================================================================
; Description:      Open's a webcam capturing window in your gui
; Syntax:           _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight)
; Parameter(s):     $sHwnd     - The handle of the gui
;                   $sLeft     - Left coord. of the preview screen
;                   $sTop      - Top coord. of the preview screen
;                   $sWidth    - Width of the preview screen
;                   $sHeight   - Height of the preview screen
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns id needed for other controls
;                   On Failure - Returns -1
; Author(s):        Ludocus
; Modified:         BugFix ( bugfix@autoit.de )
; Note(s):          Sets the "fYield" param to 1, that allows user action during capturing (controls not frozen now)
;===============================================================================
Func _WebcamOpen($sHwnd, $sLeft, $sTop, $sWidth, $sHeight)
	Local $cap = DllCall($Dll_avicap, "int", "capCreateCaptureWindow", "str", "cap", "int", BitOR($WS_CHILD,$WS_VISIBLE), "int", $sLeft, "int", $sTop, "int", $sWidth, "int", $sHeight, "hwnd", $sHwnd, "int", 1)
	If @error Then Return -1
	Local $tCAPTUREPARMS = __CAP_GET_SEQUENCE_SETUP($cap[0])
	If @error Then Return -1
	__CAP_SET_SEQUENCE_SETUP($cap[0], $tCAPTUREPARMS)
	If @error Then Return -1
	Return $cap[0]
EndFunc

;===============================================================================
; Description:      Closes the preview screen created with _WebcamOpen
; Syntax:           _WebcamClose($sId)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          None
;===============================================================================
Func _WebcamClose($sId)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEW, "int", 0, "int", 0);ferme le preview
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_CALLBACK_FRAME, "int", 0, "int", 0);ajout
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_END, "int", 0, "int", 0)
	DllClose($Dll_user)
	If @error Then
		Return 0
	Else
		Return 1
	EndIf
EndFunc

;===============================================================================
; Function Name....: _WebcamActive
; Description......: Connects the capture driver for specified webcam-id (default 0) to the capture window (like camera switch on)
;                    shows freeze image
; Parameter(s).....: $sId       - Id (returned from _WebcamOpen)
;                    $WebCamId  - specified webcam-id (default 0)
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _WebcamActive($sId, $WebCamId=0)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_CONNECT, "int", $WebCamId , "int", 0)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_SCALE, "int", 1, "int", 0)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_OVERLAY, "int", 1, "int", 0)
EndFunc

;===============================================================================
; Function Name....: _WebcamInactive
; Description......: Disonnects the capture driver from the capture window (like camera switch off)
; Parameter(s).....: $sId       - Id (returned from _WebcamOpen)
; Author(s)........: BugFix ( bugfix@autoit.de )
;===============================================================================
Func _WebcamInactive($sId)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_DRIVER_DISCONNECT, "int",  0, "int", 0)
EndFunc

;===============================================================================
; Function Name....: _WebcamPreview
; Description......: shows live image in the capture window
; Parameter(s).....: $sId       - Id (returned from _WebcamOpen)
; Requirement(s)...: Run _WebcamActive before
; Author(s)........: Ludocus
;===============================================================================
Func _WebcamPreview($sId)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEW, "int", 1, "int", 0)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEWRATE, "int", 15, "int", 0)
EndFunc

;===============================================================================
; Function Name....: _WebcamStopPreview
; Description......: stops the live image in the capture window
; Parameter(s).....: $sId       - Id (returned from _WebcamOpen)
; Author(s)........: Ludocus
;===============================================================================
Func _WebcamStopPreview($sId)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_PREVIEW, "int", 0, "int", 0)
EndFunc

;===============================================================================
; Description:      Creates a Snapshot from a webcam
; Syntax:           _WebcamSnap($sId, $sFile)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)

; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          None
;===============================================================================
Func _WebcamSnap($sId, $sFile)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GRAB_FRAME_NOSTOP, "int", 0, "int", 0)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SAVEDIBA, "int", 0, "str", $sFile)
	If @error Then
		Return 0
	Else
		Return 1
	EndIf
EndFunc

;===============================================================================
; Description:      Starts recording the webcam to file
; Syntax:           _WebcamRecordStart($sId, $sFile)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
;                   $sFile     - File to save the movie to (*.avi)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Modified:         BugFix ( bugfix@autoit.de )
; Note(s):          Stop recording by: _WebcamRecordStop($Id, $sFile)
;===============================================================================
Func _WebcamRecordStart($sId, $sFile)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_FILE_SET_CAPTURE_FILEA, "int", 0, "str", $sFile)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SEQUENCE, "int", 0, "int", 0)
	If @error Then
		Return 0
	Else
		Return 1
	EndIf
EndFunc

;===============================================================================
; Description:      Stops recording
; Syntax:           _WebcamRecordStop($sId)
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        Ludocus
; Note(s):          None
;===============================================================================
Func _WebcamRecordStop($sId)
	DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_STOP, "int", 0, "int", 0)
	If @error Then
		Return 0
	Else
		Return 1
	EndIf
EndFunc

;===============================================================================
; Description:      De/Activate audio capturing
; Syntax:           _WebcamAudioSet($sId [, $iOn])
; Parameter(s):     $sId       - Id (returned from _WebcamOpen)
;                   $iOn       - 0 or 1 (Default) to switch Off/On
; Requirement(s):   A webcam
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0
; Author(s):        BugFix ( bugfix@autoit.de )
; Note(s):          By default in SEQUENCE_SETUP is capturing active
;===============================================================================
Func _WebcamAudioSet($sId, $iOn=1)
	If $iOn <> 1 Then $iOn = 0
	__CAP_SET_SEQUENCE_SETUP($sId, __CAP_GET_SEQUENCE_SETUP($sId), 'fCaptureAudio=' & $iOn)
	If @error Then Return 0
	Return 1
EndFunc

; == helper functions:
;===============================================================================
; Description:      Reads the values of current capture parameter
; Syntax:           __CAP_GET_SEQUENCE_SETUP($sId)
; Return Value(s):  On Success - Returns the CAPTUREPARMS-structure
;                   On Failure - Returns 0, set error
; Author(s):        BugFix ( bugfix@autoit.de )
;===============================================================================
Func __CAP_GET_SEQUENCE_SETUP($sId)
	Local $aParamStruc[24] = [ _
		'dwRequestMicroSecPerFrame','fMakeUserHitOKToCapture','wPercentDropForError','fYield','dwIndexSize','wChunkGranularity','fUsingDOSMemory','wNumVideoRequested', _
		'fCaptureAudio','wNumAudioRequested','vKeyAbort','fAbortLeftMouse','fAbortRightMouse','fLimitEnabled','wTimeLimit','fMCIControl','fStepMCIDevice','dwMCIStartTime', _
		'dwMCIStopTime','fStepCaptureAt2x','wStepCaptureAverageFrames','dwAudioBufferSize','fDisableWriteCache','AVStreamMaster']
	Local $tCAPTUREPARMS = DllStructCreate($tagCAPTUREPARMS), $pCAPTUREPARMS =  DllStructGetPtr($tCAPTUREPARMS)
	Local $ret = DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_GET_SEQUENCE_SETUP, "WPARAM", DllStructGetSize($tCAPTUREPARMS), "LPARAM", $pCAPTUREPARMS)
	If @error Then
		Return SetError(1,0,0)
	Else
;~ 		For $i = 0 To 23
;~ 			ConsoleWrite($aParamStruc[$i] & @TAB & DllStructGetData($tCAPTUREPARMS, $aParamStruc[$i]) & @CRLF)
;~ 		Next
		Return $tCAPTUREPARMS
	EndIf
EndFunc

;===============================================================================
; Description:      Sets one or more values in current capture parameter
; Syntax:           __CAP_SET_SEQUENCE_SETUP($sId, $tCAPTUREPARMS [, $vParams=0])
; Parameter(s):     $sId            - Id (returned from _WebcamOpen)
;                   $tCAPTUREPARMS  - structure from __CAP_GET_SEQUENCE_SETUP
;                   $vParams        - string: "Param=value"; for more than one param use: "Param1=value|Param2=value|Param3=value|.."
; Return Value(s):  On Success - Returns 1
;                   On Failure - Returns 0, set error
; Author(s):        BugFix ( bugfix@autoit.de )
; Note(s):          Meaning of all parameters: http://msdn.microsoft.com/en-us/library/dd756942(v=VS.85).aspx
;===============================================================================
Func __CAP_SET_SEQUENCE_SETUP($sId, $tCAPTUREPARMS, $vParams=0) ; $vParams: Um nur einen Wert zu ändern "Parametername=Wert" übergeben, mehrere Werte mit "|" abtrennen
	Local $aParamStruc[24] = [ _
		'dwRequestMicroSecPerFrame','fMakeUserHitOKToCapture','wPercentDropForError','fYield','dwIndexSize','wChunkGranularity','fUsingDOSMemory','wNumVideoRequested', _
		'fCaptureAudio','wNumAudioRequested','vKeyAbort','fAbortLeftMouse','fAbortRightMouse','fLimitEnabled','wTimeLimit','fMCIControl','fStepMCIDevice','dwMCIStartTime', _
		'dwMCIStopTime','fStepCaptureAt2x','wStepCaptureAverageFrames','dwAudioBufferSize','fDisableWriteCache','AVStreamMaster']
	If $vParams == 0 Then
		Local $aParam2Set[1] = ['fYield=1'] ; == Default: Erlaubt Nutzereingaben während Capturing
	Else
		Local $aParam2Set = StringSplit($vParams, '|', 2)
	EndIf
	Local $aSplit
	For $i = 0 To UBound($aParam2Set) -1
		$aSplit = StringSplit($aParam2Set[$i], '=')
		If $aSplit[0] <> 2 Then ContinueLoop
		DllStructSetData($tCAPTUREPARMS, $aSplit[1], $aSplit[2])
	Next
	Local $pCAPTUREPARMS =  DllStructGetPtr($tCAPTUREPARMS)
	Local $ret = DllCall($Dll_user, "int", "SendMessage", "hWnd", $sId, "int", $WM_CAP_SET_SEQUENCE_SETUP, "wparam", DllStructGetSize($tCAPTUREPARMS), "lparam", $pCAPTUREPARMS)
	If @error Then
		Return SetError(1,0,0)
	Else
;~ 		For $i = 0 To 23
;~ 			ConsoleWrite($aParamStruc[$i] & @TAB & DllStructGetData($tCAPTUREPARMS, $aParamStruc[$i]) & @CRLF)
;~ 		Next
		Return 1
	EndIf
EndFunc