wait_motion.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dlfcn.h>
  4. #include <string.h>
  5. #include <sys/time.h>
  6. #include <pthread.h>
  7. #include <errno.h>
  8. #include <math.h>
  9. extern void CommandResponse(int fd, const char *res);
  10. extern int local_sdk_motor_get_position(float *step,float *angle);
  11. extern int MotorFd;
  12. extern struct timeval MotorLastMovedTime;
  13. struct RectInfoSt {
  14. int left;
  15. int right;
  16. int top;
  17. int bottom;
  18. int dummy1;
  19. int dummt2;
  20. };
  21. static int (*original_local_sdk_video_osd_update_rect)(int ch, int display, struct RectInfoSt *rectInfo);
  22. static int WaitMotionFd = -1;
  23. static int Timeout = -1;
  24. static pthread_mutex_t WaitMotionMutex = PTHREAD_MUTEX_INITIALIZER;
  25. static pthread_cond_t WaitMotionCond = PTHREAD_COND_INITIALIZER;
  26. char *WaitMotion(int fd, char *tokenPtr) {
  27. if(WaitMotionFd >= 0) {
  28. fprintf(stderr, "[command] wait motion error %d %d\n", WaitMotionFd, fd);
  29. return "error : wait motion error";
  30. }
  31. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  32. Timeout = p ? atoi(p) : 0;
  33. if(Timeout < 10) {
  34. fprintf(stderr, "[command] wait motion timeout error timeout = %d\n", Timeout);
  35. return "error : wait motion timeout value error";
  36. }
  37. WaitMotionFd = fd;
  38. pthread_mutex_unlock(&WaitMotionMutex);
  39. return NULL;
  40. }
  41. int local_sdk_video_osd_update_rect(int ch, int display, struct RectInfoSt *rectInfo) {
  42. if((WaitMotionFd >= 0) && (MotorFd <= 0) && !ch) {
  43. struct timeval tv;
  44. gettimeofday(&tv, NULL);
  45. timersub(&tv, &MotorLastMovedTime, &tv);
  46. if(tv.tv_sec || (tv.tv_usec >= 500000)) {
  47. if(display) {
  48. float pan; // 0-355
  49. float tilt; // 0-180
  50. int ret = local_sdk_motor_get_position(&pan, &tilt);
  51. static char waitMotionResBuf[256];
  52. if(!ret) {
  53. pan += (rectInfo->left + rectInfo->right - 320 * 2) * 85 / (2 * 640);
  54. if(pan < 0.0) pan = 0.0;
  55. if(pan > 355.0) pan = 355;
  56. tilt -= (rectInfo->top + rectInfo->bottom - 180 * 2) * 55 / (2 * 360);
  57. if(tilt < 45.0) tilt = 45.0;
  58. if(tilt > 180.0) tilt = 180.0;
  59. sprintf(waitMotionResBuf, "detect %d %d %d %d %d %d\n",
  60. rectInfo->left, rectInfo->right, rectInfo->top, rectInfo->bottom, lroundf(pan), lroundf(tilt));
  61. } else {
  62. sprintf(waitMotionResBuf, "detect %d %d %d %d - -\n",
  63. rectInfo->left, rectInfo->right, rectInfo->top, rectInfo->bottom);
  64. }
  65. CommandResponse(WaitMotionFd, waitMotionResBuf);
  66. } else {
  67. CommandResponse(WaitMotionFd, "clear\n");
  68. }
  69. pthread_cond_signal(&WaitMotionCond);
  70. }
  71. }
  72. return original_local_sdk_video_osd_update_rect(ch, display, rectInfo);
  73. }
  74. static void *WaitMotionThread() {
  75. while(1) {
  76. pthread_mutex_lock(&WaitMotionMutex);
  77. if(WaitMotionFd >= 0) {
  78. struct timeval now;
  79. struct timespec timeout;
  80. gettimeofday(&now, NULL);
  81. timeout.tv_sec = now.tv_sec + Timeout;
  82. timeout.tv_nsec = now.tv_usec * 1000;
  83. int ret = pthread_cond_timedwait(&WaitMotionCond, &WaitMotionMutex, &timeout);
  84. if(ret == ETIMEDOUT) CommandResponse(WaitMotionFd, "timeout\n");
  85. }
  86. WaitMotionFd = -1;
  87. }
  88. }
  89. static void __attribute ((constructor)) osd_rect_hook_init(void) {
  90. original_local_sdk_video_osd_update_rect = dlsym(dlopen ("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_osd_update_rect");
  91. pthread_mutex_lock(&WaitMotionMutex);
  92. pthread_t thread;
  93. if(pthread_create(&thread, NULL, WaitMotionThread, NULL)) {
  94. fprintf(stderr, "pthread_create error\n");
  95. pthread_mutex_unlock(&WaitMotionMutex);
  96. return;
  97. }
  98. }