#include "protocolPTZ_ONVIF.h" #include "../../hardware/uart/serialPort.h" #define LOG(X) m_pLog->Log("ONVIF", this, X) #define LOG_ERROR(X) m_pLog->Log("ONVIF", this, _LOG_ERROR, X) protocolPTZ_ONVIF::protocolPTZ_ONVIF(config_setting_t * Settings) : protocolPTZ(), m_mInquiryMutex() { m_pLog = basicLogCollection::getInstance(); m_bLastCommandIssued = CMD_PTZ_NOOP; m_pSettings = Settings; m_bAuthMode = 0; #ifdef USE_PONVIF m_pOnVif = NULL; m_pProfile = NULL; m_bReportsMoveStatus = false; m_bReportsPosition = false; m_bTargetValid = false; m_fTargetPan = 0.0f; m_fTargetTilt = 0.0f; m_fTargetZoom = 0.0f; #else m_pDevice = NULL; m_pPTZ = NULL; #endif memset(m_pDeviceURI, 0, MAX_PATH_LEN); memset(m_pUser, 0, MAX_CRED_LEN); memset(m_pPassword, 0, MAX_CRED_LEN); } protocolPTZ_ONVIF::protocolPTZ_ONVIF(CSTR ip, uint16_t port, CSTR username, CSTR password, uint8_t AuthMode) : protocolPTZ(), m_mInquiryMutex() { m_pLog = basicLogCollection::getInstance(); m_bLastCommandIssued = CMD_PTZ_NOOP; m_pSettings = NULL; #ifdef USE_PONVIF m_pOnVif = NULL; m_pProfile = NULL; m_bReportsMoveStatus = false; m_bReportsPosition = false; m_bTargetValid = false; m_fTargetPan = 0.0f; m_fTargetTilt = 0.0f; m_fTargetZoom = 0.0f; #else m_pDevice = NULL; m_pPTZ = NULL; #endif LOG(ip); LOG(username); LOG(password); m_bAuthMode = AuthMode; sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode); LOG(m_pDeviceURI); if (port == 80) { sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip); } else { sprintf(m_pDeviceURI, "http://%s:%d/onvif/device_service", ip, port); } LOG(m_pDeviceURI); strncpy(m_pUser, username, MAX_CRED_LEN); strncpy(m_pPassword, password, MAX_CRED_LEN); } protocolPTZ_ONVIF::protocolPTZ_ONVIF(CSTR ctl_url, CSTR username, CSTR password, uint8_t AuthMode) : protocolPTZ(), m_mInquiryMutex() { m_pLog = basicLogCollection::getInstance(); m_bLastCommandIssued = CMD_PTZ_NOOP; m_pSettings = NULL; #ifdef USE_PONVIF m_pOnVif = NULL; m_pProfile = NULL; m_bReportsMoveStatus = false; m_bReportsPosition = false; m_bTargetValid = false; m_fTargetPan = 0.0f; m_fTargetTilt = 0.0f; m_fTargetZoom = 0.0f; #else m_pDevice = NULL; m_pPTZ = NULL; #endif LOG(ctl_url); LOG(username); LOG(password); m_bAuthMode = AuthMode; sprintf(m_pDeviceURI, "Auth Mode: [%d]", m_bAuthMode); LOG(m_pDeviceURI); strncpy(m_pDeviceURI, ctl_url, 250); strncpy(m_pUser, username, MAX_CRED_LEN); strncpy(m_pPassword, password, MAX_CRED_LEN); } protocolPTZ_ONVIF::~protocolPTZ_ONVIF() {} bool protocolPTZ_ONVIF::setIPAddress(CSTR ip) { if (ip == NULL) { return false; } if (strlen(ip) == 0) { return false; } sprintf(m_pDeviceURI, "http://%s/onvif/device_service", ip); // strncpy( m_pDeviceURI, ip, MAX_PATH_LEN ); return true; } bool protocolPTZ_ONVIF::setUsername(CSTR username) { if (username == NULL) { return false; } if (strlen(username) == 0) { return false; } strncpy(m_pUser, username, MAX_CRED_LEN); return true; } bool protocolPTZ_ONVIF::setPassword(CSTR password) { if (password == NULL) { return false; } if (strlen(password) == 0) { return false; } strncpy(m_pPassword, password, MAX_CRED_LEN); return true; } bool protocolPTZ_ONVIF::setAuthMode(uint8_t Mode) { m_bAuthMode = Mode; return true; } bool protocolPTZ_ONVIF::create(void) { if (! protocolPTZ::create()) { return false; } if (m_pSettings != NULL) { config_setting_t * pSetting; pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_USER); if (pSetting == NULL) { return false; } sprintf(m_pUser, "%s", config_setting_get_string(pSetting)); pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_PASS); if (pSetting == NULL) { return false; } sprintf(m_pPassword, "%s", config_setting_get_string(pSetting)); pSetting = config_setting_get_member(m_pSettings, PPTZ_TAG_ONVIF_DEV_URI); if (pSetting == NULL) { return false; } sprintf(m_pDeviceURI, "%s", config_setting_get_string(pSetting)); }; #ifdef DEBUG LOG(m_pDeviceURI); #endif if (strlen(m_pDeviceURI) < 4) { LOG_ERROR("No Device URI Is Set!"); return false; }; #ifdef USE_PONVIF m_pOnVif = new pOnvif(); if (m_pOnVif == NULL) { LOG_ERROR("pOnvif NEW FAILURE!"); m_bStatus = 0; return false; }; m_pOnVif->setDeviceUri(m_pDeviceURI); m_pOnVif->setUsername(m_pUser); m_pOnVif->setPassword(m_pPassword); m_pOnVif->setAuthMode(m_bAuthMode); LOG("PROTOCREATE"); if (m_pOnVif->create()) { m_pProfile = m_pOnVif->getProfiles()->at(0); detectStatusSupport(); LOG("DONE"); m_bStatus = 1; return true; } else { LOG_ERROR("pOnvif CREATE FAILURE!"); delete(m_pOnVif); m_pOnVif = NULL; m_bStatus = 0; return false; }; #else std::string A(m_pUser); std::string B(m_pPassword); std::string C(m_pDeviceURI); m_pDevice = new OnvifClientDevice(C, A, B); if (m_pDevice != NULL) { m_pPTZ = new OnvifClientPTZ(*m_pDevice); if (m_pPTZ != NULL) { _tptz__GetNodesResponse tNodes; int RetVal; RetVal = m_pPTZ->GetNodes(tNodes); if (RetVal == 0) { OnvifClientMedia media(*m_pDevice); _trt__GetProfilesResponse profiles; media.GetProfiles(profiles); m_pPTZToken = profiles.Profiles.at(0)->token; // PTZConfiguration->token; // m_pPTZToken = tNodes.PTZNode.at(0)->token; //_tptz__GetConfigurationOptionsResponse COResp; // int RetVal = m_pPTZ->GetConfigurationOptions(COResp, m_pToken); //_tptz__GetConfigurationResponse configResponce; // configResponce.PTZConfiguration = new tt__PTZConfiguration(); // m_pPTZ->GetConfiguration(configResponce); // m_pToken = configResponce.PTZConfiguration->token; setConnectionStatus(CS_CONNECTED); m_bStatus = 1; return true; }; }; }; #endif return false; } #ifndef USE_PONVIF bool protocolPTZ_ONVIF::initCamera(void) { if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if ( m_bLastCommandIssued != CMD_PTZ_NOOP ) return false; m_bLastCommandIssued = CMD_PTZ_INIT; m_pPTZ->Initialize(); return true; } #endif bool protocolPTZ_ONVIF::move(uint32_t MoveMatrix) { // if( m_bStatus == 0 ) return false; #ifdef USE_PONVIF if (m_pProfile == NULL) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; float Speed_x, Speed_y, Speed_z; if (MoveMatrix > 0) { if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0) { Speed_x = 0.0f; } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0) { Speed_x = -m_dPanSpeed; } else { Speed_x = m_dPanSpeed; }; if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0) { Speed_y = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0) { Speed_y = m_dTiltSpeed; } else { Speed_y = -m_dTiltSpeed; }; if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0) { Speed_z = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0) { Speed_z = m_dZoomSpeed; } else { Speed_z = -m_dZoomSpeed; }; bool res = true; if ((MoveMatrix & (PTZ_DRIVE_PAN_MASK | PTZ_DRIVE_TILT_MASK)) != 0) { res = m_pOnVif->ptz_ContinuousMove(m_pProfile->profiletoken, Speed_x, Speed_y); }; if (res) { if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) != 0) { res = m_pOnVif->ptz_ContinuousMoveZoom(m_pProfile->profiletoken, Speed_z); }; }; m_bStatus = (res) ? 1 : 0; } else { m_bStatus = (m_pOnVif->ptz_Stop(m_pProfile->profiletoken, true, true)) ? 1 : 0; }; return (m_bStatus > 0); #else if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; _tptz__ContinuousMoveResponse ContMoveResponse; tt__PTZSpeed tSpeed; tSpeed.PanTilt = new tt__Vector2D(); tSpeed.Zoom = new tt__Vector1D(); LONG64 to = 1000; int RetVal; if ((MoveMatrix & PTZ_DRIVE_PAN_MASK) == 0) { tSpeed.PanTilt->x = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_PAN_LEFT) != 0) { tSpeed.PanTilt->x = -m_dPanSpeed; } else { tSpeed.PanTilt->x = m_dPanSpeed; }; if ((MoveMatrix & PTZ_DRIVE_TILT_MASK) == 0) { tSpeed.PanTilt->y = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_TILT_UP) != 0) { tSpeed.PanTilt->y = m_dTiltSpeed; } else { tSpeed.PanTilt->y = -m_dTiltSpeed; }; if ((MoveMatrix & PTZ_DRIVE_ZOOM_MASK) == 0) { tSpeed.Zoom->x = 0.0; } else if ((MoveMatrix & PTZ_DRIVE_ZOOM_WIDE) != 0) { tSpeed.Zoom->x = m_dZoomSpeed; } else { tSpeed.Zoom->x = -m_dZoomSpeed; }; RetVal = m_pPTZ->ContinuousMove(ContMoveResponse, tSpeed, to, m_pPTZToken); delete(tSpeed.PanTilt); delete(tSpeed.Zoom); if (RetVal) { return false; } return true; #endif } #include #include #ifdef USE_PONVIF static long ptzOnvifNowMs(void) { struct timespec tTS; clock_gettime(CLOCK_MONOTONIC, &tTS); return (long) tTS.tv_sec * 1000L + tTS.tv_nsec / 1000000L; } // Pan in the generic space wraps around (-1 == +1 on 360 degree cameras) static float ptzOnvifPanDistance(float a, float b) { float d = fabsf(a - b); if (d > 1.0f) { d = 2.0f - d; } return d; } /* Finds out once (after create) how the end of a move can be detected for this camera. The PTZ service capabilities (MoveStatus / StatusPosition attributes) are asked first; many cameras don't implement GetServiceCapabilities or leave the attributes out, so a real GetStatus decides. An explicit "false" in the capabilities wins over a GetStatus reply (some cameras send fixed values). */ void protocolPTZ_ONVIF::detectStatusSupport(void) { m_bReportsMoveStatus = false; m_bReportsPosition = false; if (m_pOnVif == NULL || m_pProfile == NULL) { return; } int8_t capMoveStatus = -1; int8_t capStatusPosition = -1; m_pOnVif->ptz_GetServiceCapabilities(&capMoveStatus, &capStatusPosition); ptzStatus tVal; bool tProbe = m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal); m_bReportsMoveStatus = tProbe && tVal.hasMoveStatus && (capMoveStatus != 0); m_bReportsPosition = tProbe && tVal.hasPosition && (capStatusPosition != 0); char tB[256]; snprintf(tB, sizeof(tB), "PTZ status support: MoveStatus=%s Position=%s (GetStatus %s, capabilities MoveStatus=%d StatusPosition=%d) -> %s", m_bReportsMoveStatus ? "yes" : "no", m_bReportsPosition ? "yes" : "no", tProbe ? "ok" : "failed", capMoveStatus, capStatusPosition, m_bReportsMoveStatus ? "wait for IDLE" : (m_bReportsPosition ? "wait for position" : "fixed wait")); LOG(tB); } bool protocolPTZ_ONVIF::fixedMoveWait(long alreadyWaitedMs) { long tRest = PTZ_ONVIF_FIXED_MOVE_WAIT_MS - alreadyWaitedMs; if (tRest > 0) { SLEEP(tRest); } return true; } /* Blocks until the last AbsoluteMove is finished (see the strategy at the top of protocolPTZ_ONVIF.h). Returns true when the move is over (or after the fixed wait / timeout), false only if there is no PTZ connection at all. */ bool protocolPTZ_ONVIF::waitMoveIsDone(void) { if (m_pOnVif == NULL || m_pProfile == NULL) { return false; } if (! m_bReportsMoveStatus && ! m_bReportsPosition) { return fixedMoveWait(0); } long tStart = ptzOnvifNowMs(); int tFailures = 0; bool tSeenMoving = false; bool tHavePrev = false; int tStableCount = 0; float tPrevPan = 0.0f; float tPrevTilt = 0.0f; float tPrevZoom = 0.0f; char tB[256]; for (;;) { long tElapsed = ptzOnvifNowMs() - tStart; if (tElapsed >= PTZ_ONVIF_MOVE_TIMEOUT_MS) { LOG_ERROR("Timeout waiting for the end of the PTZ move!"); return true; }; ptzStatus tVal; if (! m_pOnVif->ptz_GetStatus(m_pProfile->profiletoken, &tVal)) { if (++tFailures >= PTZ_ONVIF_MAX_STATUS_FAILURES) { LOG_ERROR("GetStatus keeps failing, falling back to the fixed wait"); return fixedMoveWait(ptzOnvifNowMs() - tStart); }; SLEEP(PTZ_ONVIF_STATUS_POLL_MS); continue; }; tFailures = 0; bool tNear = false; bool tStable = false; if (m_bReportsPosition && tVal.hasPosition) { tNear = m_bTargetValid && (ptzOnvifPanDistance(tVal.pan, m_fTargetPan) <= PTZ_ONVIF_POS_TOLERANCE) && (fabsf(tVal.tilt - m_fTargetTilt) <= PTZ_ONVIF_POS_TOLERANCE) && (! tVal.hasZoomPosition || fabsf(tVal.zoom - m_fTargetZoom) <= PTZ_ONVIF_POS_TOLERANCE); if (tHavePrev && ptzOnvifPanDistance(tVal.pan, tPrevPan) < PTZ_ONVIF_POS_STABLE_EPS && fabsf(tVal.tilt - tPrevTilt) < PTZ_ONVIF_POS_STABLE_EPS && fabsf(tVal.zoom - tPrevZoom) < PTZ_ONVIF_POS_STABLE_EPS) { tStableCount++; } else { tStableCount = 0; }; tPrevPan = tVal.pan; tPrevTilt = tVal.tilt; tPrevZoom = tVal.zoom; tHavePrev = true; tStable = (tStableCount >= PTZ_ONVIF_POS_STABLE_POLLS); }; bool tAfterGrace = (tElapsed >= PTZ_ONVIF_MOVE_START_GRACE_MS); bool tDone = false; if (m_bReportsMoveStatus && tVal.hasMoveStatus) { // Right after the command a camera may still say IDLE: accept IDLE once it was seen // moving, once the target is reached, or after the start grace time. if (tVal.isMoving) { tSeenMoving = true; } else if (tSeenMoving || tNear || tAfterGrace) { tDone = true; }; } else if (m_bReportsPosition && tVal.hasPosition) { tDone = tNear || (tStable && tAfterGrace); } else { // This reply carries neither MoveStatus nor Position return fixedMoveWait(tElapsed); }; if (tDone) { #ifdef DEBUG_EXTRA snprintf(tB, sizeof(tB), "Finished Move in %ld ms (pos %.3f %.3f %.3f)", ptzOnvifNowMs() - tStart, tVal.pan, tVal.tilt, tVal.zoom); LOG(tB); #else (void) tB; #endif return true; }; SLEEP(PTZ_ONVIF_STATUS_POLL_MS); }; } #else bool protocolPTZ_ONVIF::waitMoveIsDone(void) { return false; } #endif bool protocolPTZ_ONVIF::goAbsolute(float dPan, float dTilt, float dZoom) { #ifdef USE_PONVIF // if (m_bStatus == 0) return false; #ifdef DEBUG_EXTRA LOG("ABS Move"); #endif if (m_pProfile == NULL) { LOG_ERROR("No PTZ Profile!!"); return false; }; if (dZoom < 0.0f) { dZoom = 0.0f; } // Remember the target for waitMoveIsDone() m_fTargetPan = dPan; m_fTargetTilt = dTilt; m_fTargetZoom = dZoom; m_bTargetValid = true; if (m_pOnVif->ptz_AbsoluteMove(m_pProfile->profiletoken, dPan, dTilt, dZoom)) { }; return true; #else if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; _tptz__AbsoluteMoveResponse AbsMoveResponse; tt__PTZSpeed tSpeed; tt__PTZVector tPos; tSpeed.PanTilt = new tt__Vector2D(); tSpeed.Zoom = new tt__Vector1D(); tSpeed.PanTilt->x = 1.f; tSpeed.PanTilt->y = 1.f; tSpeed.Zoom->x = 1.f; tPos.PanTilt = new tt__Vector2D(); tPos.Zoom = new tt__Vector1D(); tPos.PanTilt->x = dPan; tPos.PanTilt->y = dTilt; tPos.Zoom->x = dZoom; int RetVal; RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken); delete(tSpeed.PanTilt); delete(tSpeed.Zoom); delete(tPos.PanTilt); delete(tPos.Zoom); return true; #endif } bool protocolPTZ_ONVIF::getCameraPresets(void) { LOG("GET PRESETS!"); return m_pOnVif->ptz_GetPresets(m_pProfile->profiletoken); } bool protocolPTZ_ONVIF::goPreset(char * PresetToken) { #ifdef USE_PONVIF // if (m_bStatus == 0) return false; #ifdef DEBUG_EXTRA LOG("Preset Move"); #endif if (m_pProfile == NULL) { LOG_ERROR("No PTZ Profile!!"); return false; }; if (! m_pOnVif->ptz_GotoPreset(m_pProfile->profiletoken, PresetToken, 1.0f, 1.0f, 1.0f)) { m_bStatus = 0; } return (m_bStatus > 0); #else if (m_bStatus == 0) { return false; } // if (m_bReplyRequired) if (m_bLastCommandIssued != CMD_PTZ_NOOP) return false; /* _tptz__AbsoluteMoveResponse AbsMoveResponse; tt__PTZSpeed tSpeed; tt__PTZVector tPos; tSpeed.PanTilt = new tt__Vector2D(); tSpeed.Zoom = new tt__Vector1D(); tSpeed.PanTilt->x = 1.f; tSpeed.PanTilt->y = 1.f; tSpeed.Zoom->x = 1.f; tPos.PanTilt = new tt__Vector2D(); tPos.Zoom = new tt__Vector1D(); tPos.PanTilt->x = dPan; tPos.PanTilt->y = dTilt; tPos.Zoom->x = dZoom; int RetVal; RetVal = m_pPTZ->AbsoluteMove(AbsMoveResponse, tSpeed, tPos, m_pPTZToken); delete(tSpeed.PanTilt); delete(tSpeed.Zoom); delete(tPos.PanTilt); delete(tPos.Zoom); */ return true; #endif } bool protocolPTZ_ONVIF::validate() { return (m_bStatus > 0); } #undef LOG #undef LOG_ERROR