RealPlayDemo.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # coding=utf-8
  2. import sys
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QTableWidgetItem
  4. from PyQt5.QtCore import Qt
  5. from ctypes import *
  6. from RealPlayUI import Ui_MainWindow
  7. from NetSDK.NetSDK import NetClient
  8. from NetSDK.SDK_Callback import fDisConnect, fHaveReConnect, fDecCBFun, fRealDataCallBackEx2
  9. from NetSDK.SDK_Enum import SDK_RealPlayType, EM_LOGIN_SPAC_CAP_TYPE, EM_REALDATA_FLAG
  10. from NetSDK.SDK_Struct import C_LLONG, sys_platform, NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY, \
  11. NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY, PLAY_FRAME_INFO
  12. class MyMainWindow(QMainWindow, Ui_MainWindow):
  13. def __init__(self, parent=None):
  14. super(MyMainWindow, self).__init__(parent)
  15. self.setupUi(self)
  16. # 界面初始化
  17. self._init_ui()
  18. # NetSDK用到的相关变量和回调
  19. self.loginID = C_LLONG()
  20. self.playID = C_LLONG()
  21. self.freePort = c_int()
  22. self.m_DisConnectCallBack = fDisConnect(self.DisConnectCallBack)
  23. self.m_ReConnectCallBack = fHaveReConnect(self.ReConnectCallBack)
  24. self.m_DecodingCallBack = fDecCBFun(self.DecodingCallBack)
  25. self.m_RealDataCallBack = fRealDataCallBackEx2(self.RealDataCallBack)
  26. # 获取NetSDK对象并初始化
  27. self.sdk = NetClient()
  28. self.sdk.InitEx(self.m_DisConnectCallBack)
  29. self.sdk.SetAutoReconnect(self.m_ReConnectCallBack)
  30. # 初始化界面
  31. def _init_ui(self):
  32. self.login_btn.setText('登录(Login)')
  33. self.play_btn.setText('预览(Play)')
  34. self.play_btn.setEnabled(False)
  35. self.IP_lineEdit.setText('172.23.8.94')
  36. self.Port_lineEdit.setText('37777')
  37. self.Name_lineEdit.setText('admin')
  38. self.Pwd_lineEdit.setText('admin123')
  39. self.setWindowFlag(Qt.WindowMinimizeButtonHint)
  40. self.setWindowFlag(Qt.WindowCloseButtonHint)
  41. self.setFixedSize(self.width(), self.height())
  42. self.login_btn.clicked.connect(self.login_btn_onclick)
  43. self.play_btn.clicked.connect(self.play_btn_onclick)
  44. self.PlayMode_comboBox.addItem('CallBack')
  45. if sys_platform == 'windows':
  46. self.PlayMode_comboBox.addItem('PlaySDK')
  47. def login_btn_onclick(self):
  48. if not self.loginID:
  49. ip = self.IP_lineEdit.text()
  50. port = int(self.Port_lineEdit.text())
  51. username = self.Name_lineEdit.text()
  52. password = self.Pwd_lineEdit.text()
  53. stuInParam = NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY()
  54. stuInParam.dwSize = sizeof(NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY)
  55. stuInParam.szIP = ip.encode()
  56. stuInParam.nPort = port
  57. stuInParam.szUserName = username.encode()
  58. stuInParam.szPassword = password.encode()
  59. stuInParam.emSpecCap = EM_LOGIN_SPAC_CAP_TYPE.TCP
  60. stuInParam.pCapParam = None
  61. stuOutParam = NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY()
  62. stuOutParam.dwSize = sizeof(NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY)
  63. self.loginID, device_info, error_msg = self.sdk.LoginWithHighLevelSecurity(stuInParam, stuOutParam)
  64. if self.loginID != 0:
  65. self.setWindowTitle('实时预览(RealPlay)-在线(OnLine)')
  66. self.login_btn.setText('登出(Logout)')
  67. self.play_btn.setEnabled(True)
  68. self.play_btn.setText("预览(Play)")
  69. for i in range(int(device_info.nChanNum)):
  70. self.Channel_comboBox.addItem(str(i))
  71. self.StreamTyp_comboBox.setEnabled(True)
  72. self.PlayMode_comboBox.setEnabled(True)
  73. else:
  74. QMessageBox.about(self, '提示(prompt)', error_msg)
  75. else:
  76. if self.playID:
  77. self.sdk.StopRealPlayEx(self.playID)
  78. self.play_btn.setText("预览(Play)")
  79. self.playID = 0
  80. self.PlayWnd.repaint()
  81. if 0 == self.PlayMode_comboBox.currentIndex():
  82. self.sdk.SetDecCallBack(self.freePort, None)
  83. self.sdk.Stop(self.freePort)
  84. self.sdk.CloseStream(self.freePort)
  85. self.sdk.ReleasePort(self.freePort)
  86. result = self.sdk.Logout(self.loginID)
  87. if result:
  88. self.setWindowTitle("实时预览(RealPlay)-离线(OffLine)")
  89. self.login_btn.setText("登录(Login)")
  90. self.loginID = 0
  91. self.play_btn.setEnabled(False)
  92. self.StreamTyp_comboBox.setEnabled(False)
  93. self.PlayMode_comboBox.setEnabled(False)
  94. self.Channel_comboBox.clear()
  95. def play_btn_onclick(self):
  96. if not self.playID:
  97. if 1 == self.PlayMode_comboBox.currentIndex():
  98. channel = self.Channel_comboBox.currentIndex()
  99. if self.StreamTyp_comboBox.currentIndex() == 0:
  100. stream_type = SDK_RealPlayType.Realplay
  101. else:
  102. stream_type = SDK_RealPlayType.Realplay_1
  103. self.playID = self.sdk.RealPlayEx(self.loginID, channel, self.PlayWnd.winId(), stream_type)
  104. if self.playID != 0:
  105. self.play_btn.setText("停止(Stop)")
  106. self.StreamTyp_comboBox.setEnabled(False)
  107. self.PlayMode_comboBox.setEnabled(False)
  108. else:
  109. QMessageBox.about(self, '提示(prompt)', self.sdk.GetLastErrorMessage())
  110. else:
  111. result, self.freePort = self.sdk.GetFreePort()
  112. if not result:
  113. pass
  114. self.sdk.OpenStream(self.freePort)
  115. self.sdk.Play(self.freePort, self.PlayWnd.winId())
  116. channel = self.Channel_comboBox.currentIndex()
  117. if self.StreamTyp_comboBox.currentIndex() == 0:
  118. stream_type = SDK_RealPlayType.Realplay
  119. else:
  120. stream_type = SDK_RealPlayType.Realplay_1
  121. self.playID = self.sdk.RealPlayEx(self.loginID, channel, 0, stream_type)
  122. if self.playID != 0:
  123. self.play_btn.setText("停止(Stop)")
  124. self.StreamTyp_comboBox.setEnabled(False)
  125. self.PlayMode_comboBox.setEnabled(False)
  126. self.sdk.SetRealDataCallBackEx2(self.playID, self.m_RealDataCallBack, None,
  127. EM_REALDATA_FLAG.RAW_DATA)
  128. self.sdk.SetDecCallBack(self.freePort, self.m_DecodingCallBack)
  129. else:
  130. result = self.sdk.StopRealPlayEx(self.playID)
  131. if result:
  132. self.play_btn.setText("预览(Play)")
  133. self.StreamTyp_comboBox.setEnabled(True)
  134. self.PlayMode_comboBox.setEnabled(True)
  135. self.playID = 0
  136. self.PlayWnd.repaint()
  137. if 0 == self.PlayMode_comboBox.currentIndex():
  138. self.sdk.SetDecCallBack(self.freePort, None)
  139. self.sdk.Stop(self.freePort)
  140. self.sdk.CloseStream(self.freePort)
  141. self.sdk.ReleasePort(self.freePort)
  142. # 实现断线回调函数功能
  143. def DisConnectCallBack(self, lLoginID, pchDVRIP, nDVRPort, dwUser):
  144. self.setWindowTitle("实时预览(RealPlay)-离线(OffLine)")
  145. # 实现断线重连回调函数功能
  146. def ReConnectCallBack(self, lLoginID, pchDVRIP, nDVRPort, dwUser):
  147. self.setWindowTitle('实时预览(RealPlay)-在线(OnLine)')
  148. # 拉流回调函数功能
  149. def RealDataCallBack(self, lRealHandle, dwDataType, pBuffer, dwBufSize, param, dwUser):
  150. if lRealHandle == self.playID:
  151. data_buffer = cast(pBuffer, POINTER(c_ubyte * dwBufSize)).contents
  152. with open('./data.dav', 'ab+') as data_file:
  153. data_file.write(data_buffer)
  154. self.sdk.InputData(self.freePort, pBuffer, dwBufSize)
  155. # PLAYSDK解码回调函数功能
  156. def DecodingCallBack(self, nPort, pBuf, nSize, pFrameInfo, pUserData, nReserved2):
  157. # here get YUV data, pBuf is YUV data IYUV/YUV420 ,size is nSize, pFrameInfo is frame info with height, width.
  158. data = cast(pBuf, POINTER(c_ubyte * nSize)).contents
  159. info = pFrameInfo.contents
  160. # info.nType == 3 is YUV data,others ard audio data.
  161. # you can parse YUV420 data to RGB
  162. if info.nType == 3:
  163. pass
  164. # 关闭主窗口时清理资源
  165. def closeEvent(self, event):
  166. event.accept()
  167. if self.loginID:
  168. self.sdk.Logout(self.loginID)
  169. self.sdk.Cleanup()
  170. if __name__ == '__main__':
  171. app = QApplication(sys.argv)
  172. my_wnd = MyMainWindow()
  173. my_wnd.show()
  174. sys.exit(app.exec_())