video_callback.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #define _GNU_SOURCE
  2. #include <dlfcn.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <stdint.h>
  8. #include <fcntl.h>
  9. #include <linux/videodev2.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/time.h>
  12. #include <pthread.h>
  13. struct frames_st {
  14. void *buf;
  15. size_t length;
  16. };
  17. typedef int (* framecb)(struct frames_st *);
  18. static int (*real_local_sdk_video_set_encode_frame_callback)(int ch, void *callback);
  19. static void *video_encode_cb = NULL;
  20. static int VideoCaptureEnable = 0;
  21. char *VideoCapture(int fd, char *tokenPtr) {
  22. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  23. if(!p) return VideoCaptureEnable ? "on" : "off";
  24. if(!strcmp(p, "on")) {
  25. VideoCaptureEnable = 1;
  26. fprintf(stderr, "[command] video capute on\n", p);
  27. return "ok";
  28. }
  29. if(!strcmp(p, "off")) {
  30. VideoCaptureEnable = 0;
  31. fprintf(stderr, "[command] video capute off\n", p);
  32. return "ok";
  33. }
  34. return "error";
  35. }
  36. static uint32_t video_encode_capture(struct frames_st *frames) {
  37. static int firstEntry = 0;
  38. static int v4l2Fd = -1;
  39. if(!firstEntry) {
  40. firstEntry++;
  41. int err;
  42. const char *v4l2_device_path = "/dev/video1";
  43. const char *productf="/configs/.product_db3";
  44. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  45. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  46. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  47. struct v4l2_format vid_format;
  48. memset(&vid_format, 0, sizeof(vid_format));
  49. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  50. if( access( productf, F_OK ) == 0 ) {
  51. /* doorbell resolution */
  52. printf("[command] video product 1728x1296");
  53. vid_format.fmt.pix.width = 1728;
  54. vid_format.fmt.pix.height = 1296;
  55. } else {
  56. /* v3 and panv2 res */
  57. printf("[command] video product 1920x1080");
  58. vid_format.fmt.pix.width = 1920;
  59. vid_format.fmt.pix.height = 1080;
  60. }
  61. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  62. vid_format.fmt.pix.sizeimage = 0;
  63. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  64. vid_format.fmt.pix.bytesperline = 0;
  65. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  66. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  67. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  68. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  69. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  70. }
  71. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  72. uint32_t *buf = frames->buf;
  73. int size = write(v4l2Fd, frames->buf, frames->length);
  74. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  75. }
  76. return ((framecb)video_encode_cb)(frames);
  77. }
  78. int local_sdk_video_set_encode_frame_callback(int ch, void *callback) {
  79. fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  80. static int ch_count = 0;
  81. /* two callbacks typically detected, unknown what the difference is between them, but if they are both hooked, the app breaks. grab just one of them. */
  82. if( (ch == 0) && ch_count == 2) {
  83. video_encode_cb = callback;
  84. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
  85. callback = video_encode_capture;
  86. }
  87. fprintf(stderr,"ch count is %x\n", ch_count);
  88. ch_count=ch_count+1;
  89. return real_local_sdk_video_set_encode_frame_callback(ch, callback);
  90. }
  91. static void __attribute ((constructor)) video_callback_init(void) {
  92. real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
  93. }