class CameraDevice(Device): def __init__(self, **kwargs): super().__init__(**kwargs) self.playId = C_LLONG() self.m_RealDataCallBack = fRealDataCallBackEx2(self.realDataCallback) self.onRealData = Observable() self.onRealPlay = Observable() self.onRealStop = Observable() def realPlay(self): if not self.playId: streamtype = SDK_RealPlayType.Realplay if self.streamtype == 0 else SDK_RealPlayType.Realplay_1 self.playId = self.client.RealPlayEx(self.loginId, self.channel, 0, streamtype) if self.playId != 0: self.client.SetRealDataCallBackEx2(self.playId, self.m_RealDataCallBack, None, EM_REALDATA_FLAG.RAW_DATA) self.onRealPlay.notify() return True else: msg = self.client.GetLastErrorMessage() self.onError.notify("实时监视失败(RealPlay fail). " + msg) return False def realStop(self): if self.playId: self.client.StopRealPlayEx(self.playId) self.playId = 0 self.onRealStop.notify() # 拉流回调函数功能 def realDataCallback(self, lRealHandle, dwDataType, pBuffer, dwBufSize, param, dwUser): if lRealHandle == self.playId: if dwDataType == 0: self.onRealData.notify(dwBufSize) # print("码流大小(Stream size):" + str(dwBufSize) + ". 码流类型:原始未加密码流(Stream type:original unencrypted stream)") pass def dispose(self): self.playId = 0 super().dispose() self.onRealData.dispose() self.onRealPlay.dispose() self.onRealStop.dispose() self.client.Cleanup()
class Device: def __init__(self, ip='0.0.0.0', port=0, username='', password='', autoReconnect=True): self.loginId = C_LLONG() self.client = NetClient() self.client.InitEx(fDisConnect(self.disconnectCallback)) autoReconnect and self.client.SetAutoReconnect( fHaveReConnect(self.reconnectCallback)) self.clientInfo = None self.ip = ip self.port = port self.username = username self.password = password ## 通道(channel) self.channel = 0 ## 码流类型(0:主码流; 1:辅码流) self.streamtype = 0 self.onConnect = Observable() self.onDisconnect = Observable() self.onReconnect = Observable() self.onError = Observable() def login(self): print('LOGIN[{}]: {}:{}@{}:{}'.format(self.loginId, self.username, self.password, self.ip, self.port)) stuInParam = NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY() stuInParam.dwSize = sizeof(NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY) stuInParam.szIP = self.ip.encode() stuInParam.nPort = self.port stuInParam.szUserName = self.username.encode() stuInParam.szPassword = self.password.encode() stuInParam.emSpecCap = EM_LOGIN_SPAC_CAP_TYPE.TCP stuInParam.pCapParam = None stuOutParam = NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY() stuOutParam.dwSize = sizeof(NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY) self.loginId, self.clientInfo, errorMsg = self.client.LoginWithHighLevelSecurity( stuInParam, stuOutParam) if self.loginId != 0: print('Login Succeed') self.connectCallback() return True else: self.errorCallback(errorMsg) return False def logout(self): if self.loginId: res = self.client.Logout(self.loginId) if res: self.disconnectCallback() return res or 0 def connectCallback(self): self.onConnect.notify(None) def disconnectCallback(self): self.onDisconnect.notify(None) def reconnectCallback(self): self.onReconnect.notify(None) def errorCallback(self, msg): self.onError.notify(msg) def dispose(self): self.loginId = 0 self.clientInfo = None self.onConnect.dispose() self.onDisconnect.dispose() self.onReconnect.dispose()