IP Camera stream + record v1.0


SUBMITTED BY: Guest

DATE: May 29, 2013, 7:11 a.m.

FORMAT: Text only

SIZE: 6.8 kB

HITS: 1001

  1. #include <GUIConstants.au3>
  2. #include <WindowsConstants.au3>
  3. #include <StaticConstants.au3>
  4. #include <WinAPI.au3>
  5. #include <GDIPlus.au3>
  6. #include <Memory.au3>
  7. #region Global Vars
  8. Global Const $sProgramTitle = "IP Camera stream + record"
  9. ;HERE YOU GO:
  10. Global Const $iIPAddress = "192.168.1.99", $iPort = 99, $shtauth = "yourauth=="
  11. Global Const $STM_SETIMAGE = 0x0172
  12. Global $blRecording = False, $blGUIMinimized = False
  13. Global Const $sRecordDir = @ScriptDir & "\ip_camera_stream"
  14. Global $bRecvtmp = Binary(""), $bStream = $bRecvtmp
  15. Global $iImgLen = 0, $iStreamLen = 0, $iWritten = 0, $iEOH = 0, $iContLenPos = 0, $hImgFile = 0, $pBuffer = 0, $iImgCount = 0
  16. Global Const $iContLengthLen = StringLen("Content-Length: ")
  17. Global $sStream = "", $sTrim2ContLen = ""
  18. Global $hBMP = 0, $hGraphics = 0, $hHBITMAP2 = 0, $hFamily = 0, $hFont = 0, $tLayout = "", $hFormat = 0, $hBrush = 0
  19. #endregion Global Vars
  20. TCPStartup()
  21. Global $iSocket = TCPConnect($iIPAddress, $iPort)
  22. If @error Then
  23. MsgBox(16, $sProgramTitle, "Could not connect !")
  24. Exit -1
  25. EndIf
  26. TCPSend($iSocket, _
  27. "GET /videostream.cgi HTTP/1.1" & @CRLF & _
  28. "Host: " & $iIPAddress & ":" & $iPort & @CRLF & _
  29. "Connection: keep-alive" & @CRLF & _
  30. "Authorization: Basic " & $shtauth & @CRLF & @CRLF)
  31. #region GUI
  32. Global $hGUI = 0, $pPic = 0, $hPic = 0, $btnRecord = 0
  33. $hGUI = GUICreate($sProgramTitle, 640, 525)
  34. $pPic = GUICtrlCreatePic("", 0, 0, 640, 480, $SS_BITMAP)
  35. GUICtrlSetState($pPic, $GUI_DISABLE)
  36. $hPic = GUICtrlGetHandle($pPic)
  37. $btnRecord = GUICtrlCreateButton("Record", 10, 490, 80, 26)
  38. GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")
  39. GUISetState(@SW_SHOW, $hGUI)
  40. #endregion GUI
  41. _GDIPlus_Startup()
  42. $hFamily = _GDIPlus_FontFamilyCreate("Arial")
  43. $hFont = _GDIPlus_FontCreate($hFamily, 17)
  44. $tLayout = _GDIPlus_RectFCreate(10, 10, 100, 40)
  45. $hFormat = _GDIPlus_StringFormatCreate()
  46. $hBrush = _GDIPlus_BrushCreateSolid(0xAFFF0000)
  47. While 1
  48. Switch GUIGetMsg()
  49. Case $GUI_EVENT_CLOSE
  50. ExitLoop
  51. Case $btnRecord
  52. If $blRecording Then
  53. GUICtrlSetData($btnRecord, "Record")
  54. Else
  55. If Not FileExists($sRecordDir) Then DirCreate($sRecordDir)
  56. GUICtrlSetData($btnRecord, "Stop recording")
  57. EndIf
  58. $blRecording = Not $blRecording
  59. EndSwitch
  60. $bRecvtmp = TCPRecv($iSocket, 4096, 1) ;4kb
  61. If @error Then ExitLoop
  62. If Not BinaryLen($bRecvtmp) Then ContinueLoop
  63. $bStream &= $bRecvtmp
  64. If $iImgLen = 0 Then
  65. $sStream = BinaryToString($bStream)
  66. $iContLenPos = StringInStr($sStream, "Content-Length: ", 2)
  67. $iEOH = StringInStr($sStream, @CRLF & @CRLF, 2, 1, $iContLenPos)
  68. If $iEOH = 0 Or $iContLenPos = 0 Then ContinueLoop
  69. $sTrim2ContLen = StringTrimLeft($sStream, $iContLenPos + $iContLengthLen - 1)
  70. $iImgLen = Number(StringLeft($sTrim2ContLen, StringInStr($sTrim2ContLen, @CR, 2) - 1))
  71. $bStream = BinaryMid($bStream, $iEOH + 4)
  72. EndIf
  73. If $iImgLen = 0 Then ContinueLoop
  74. $iStreamLen = BinaryLen($bStream)
  75. If $iStreamLen < $iImgLen Then ContinueLoop
  76. If Not $blGUIMinimized Then
  77. $hBMP = Load_BMP_From_Mem($bStream)
  78. If $blRecording Then
  79. $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBMP)
  80. _GDIPlus_GraphicsDrawStringEx($hGraphics, "[•REC]", $hFont, $tLayout, $hFormat, $hBrush)
  81. EndIf
  82. $hHBITMAP2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP)
  83. _WinAPI_DeleteObject(_SendMessage($hPic, $STM_SETIMAGE, 0, $hHBITMAP2))
  84. _GDIPlus_ImageDispose($hBMP)
  85. If $blRecording Then _GDIPlus_GraphicsDispose($hGraphics)
  86. _WinAPI_DeleteObject($hHBITMAP2)
  87. EndIf
  88. If $blRecording Then
  89. $pBuffer = DllStructCreate("byte[" & $iImgLen & "]")
  90. If $iStreamLen > $iImgLen Then
  91. DllStructSetData($pBuffer, 1, BinaryMid($bStream, 1, $iImgLen))
  92. $bStream = BinaryMid($bStream, $iImgLen)
  93. Else
  94. DllStructSetData($pBuffer, 1, $bStream)
  95. $bStream = Binary("")
  96. EndIf
  97. $hImgFile = _WinAPI_CreateFile($sRecordDir & "\snap_" & StringFormat("%.4d", $iImgCount) & ".jpg", 3, 4, 4)
  98. _WinAPI_WriteFile($hImgFile, DllStructGetPtr($pBuffer), $iImgLen, $iWritten)
  99. _WinAPI_CloseHandle($hImgFile)
  100. $iImgCount += 1
  101. EndIf
  102. $iImgLen = 0
  103. WEnd
  104. _GDIPlus_FontDispose($hFont)
  105. _GDIPlus_FontFamilyDispose($hFamily)
  106. _GDIPlus_StringFormatDispose($hFormat)
  107. _GDIPlus_BrushDispose($hBrush)
  108. _GDIPlus_Shutdown()
  109. TCPCloseSocket($iSocket)
  110. TCPShutdown()
  111. Func WM_SYSCOMMAND($hWnd, $iMsg, $wParam, $lParam)
  112. Local Const $SC_MINIMIZE = 0xF020, $SC_RESTORE = 0xF120
  113. Switch BitAND($wParam, 0xFFF0)
  114. Case $SC_MINIMIZE, $SC_RESTORE
  115. $blGUIMinimized = Not $blGUIMinimized
  116. EndSwitch
  117. Return $GUI_RUNDEFMSG
  118. EndFunc ;==>WM_SYSCOMMAND
  119. Func Load_BMP_From_Mem($mem_image)
  120. ;author: UEZ
  121. ;Additional Code: thanks to progandy for the MemGlobalAlloc and tVARIANT lines
  122. If Not IsBinary($mem_image) Then Return SetError(1, 0, 0)
  123. Local Const $memBitmap = Binary($mem_image)
  124. Local Const $len = BinaryLen($memBitmap)
  125. Local Const $hData = _MemGlobalAlloc($len, $GMEM_MOVEABLE)
  126. Local Const $pData = _MemGlobalLock($hData)
  127. Local $tMem = DllStructCreate("byte[" & $len & "]", $pData)
  128. DllStructSetData($tMem, 1, $memBitmap)
  129. _MemGlobalUnlock($hData)
  130. Local $hStream = DllCall("ole32.dll", "int", "CreateStreamOnHGlobal", "handle", $pData, "int", True, "ptr*", 0)
  131. $hStream = $hStream[3]
  132. Local $hBitmap = DllCall($ghGDIPDll, "uint", "GdipCreateBitmapFromStream", "ptr", $hStream, "int*", 0)
  133. $hBitmap = $hBitmap[2]
  134. Local Const $tVARIANT = DllStructCreate("word vt;word r1;word r2;word r3;ptr data; ptr")
  135. DllCall("oleaut32.dll", "long", "DispCallFunc", "ptr", $hStream, "dword", 8 + 8 * @AutoItX64, _
  136. "dword", 4, "dword", 23, "dword", 0, "ptr", 0, "ptr", 0, "ptr", DllStructGetPtr($tVARIANT))
  137. $tMem = 0
  138. Return $hBitmap
  139. EndFunc ;==>Load_BMP_From_Mem

comments powered by Disqus