video_callback.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 void *video_encode_cb1 = NULL;
  21. static int VideoCaptureEnable = 0;
  22. static int VideoCaptureEnable1 = 0;
  23. char *VideoCapture(int fd, char *tokenPtr) {
  24. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  25. if(!p) return VideoCaptureEnable ? "on" : "off";
  26. if(!strcmp(p, "on")) {
  27. VideoCaptureEnable = 1;
  28. fprintf(stderr, "[command] video capture ch0 on\n", p);
  29. return "ok";
  30. }
  31. if(!strcmp(p, "on1")) {
  32. VideoCaptureEnable1 = 1;
  33. fprintf(stderr, "[command] video capture ch1 on\n", p);
  34. return "ok";
  35. }
  36. if(!strcmp(p, "off")) {
  37. VideoCaptureEnable = 0;
  38. fprintf(stderr, "[command] video capture ch0 off\n", p);
  39. return "ok";
  40. }
  41. if(!strcmp(p, "off1")) {
  42. VideoCaptureEnable1 = 0;
  43. fprintf(stderr, "[command] video capture ch1 off\n", p);
  44. return "ok";
  45. }
  46. return "error";
  47. }
  48. static uint32_t video_encode_capture(struct frames_st *frames) {
  49. static int firstEntry = 0;
  50. static int v4l2Fd = -1;
  51. //primary stream 0
  52. if(!firstEntry) {
  53. firstEntry++;
  54. int err;
  55. const char *v4l2_device_path = "/dev/video1";
  56. const char *productf="/configs/.product_db3";
  57. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  58. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  59. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  60. struct v4l2_format vid_format;
  61. memset(&vid_format, 0, sizeof(vid_format));
  62. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  63. if( access( productf, F_OK ) == 0 ) {
  64. /* doorbell resolution */
  65. printf("[command] video product 1728x1296");
  66. vid_format.fmt.pix.width = 1728;
  67. vid_format.fmt.pix.height = 1296;
  68. } else {
  69. /* v3 and panv2 res */
  70. printf("[command] video product 1920x1080");
  71. vid_format.fmt.pix.width = 1920;
  72. vid_format.fmt.pix.height = 1080;
  73. }
  74. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  75. vid_format.fmt.pix.sizeimage = 0;
  76. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  77. vid_format.fmt.pix.bytesperline = 0;
  78. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  79. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  80. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  81. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  82. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  83. }
  84. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  85. uint32_t *buf = frames->buf;
  86. int size = write(v4l2Fd, frames->buf, frames->length);
  87. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  88. }
  89. return ((framecb)video_encode_cb)(frames);
  90. }
  91. //secondary stream 1
  92. static uint32_t video_encode_capture1(struct frames_st *frames) {
  93. static int firstEntry = 0;
  94. static int v4l2Fd = -1;
  95. if(!firstEntry) {
  96. firstEntry++;
  97. int err;
  98. const char *v4l2_device_path = "/dev/video2";
  99. const char *productf="/configs/.product_db3";
  100. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  101. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  102. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  103. struct v4l2_format vid_format;
  104. memset(&vid_format, 0, sizeof(vid_format));
  105. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  106. if( access( productf, F_OK ) == 0 ) {
  107. /* doorbell resolution */
  108. printf("[command] video product 640x480");
  109. vid_format.fmt.pix.width = 640;
  110. vid_format.fmt.pix.height = 480;
  111. } else {
  112. /* v3 and panv2 res */
  113. printf("[command] video product 640x320");
  114. vid_format.fmt.pix.width = 640;
  115. vid_format.fmt.pix.height = 320;
  116. }
  117. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  118. vid_format.fmt.pix.sizeimage = 0;
  119. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  120. vid_format.fmt.pix.bytesperline = 0;
  121. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  122. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  123. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  124. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  125. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  126. }
  127. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  128. uint32_t *buf = frames->buf;
  129. int size = write(v4l2Fd, frames->buf, frames->length);
  130. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  131. }
  132. return ((framecb)video_encode_cb1)(frames);
  133. }
  134. int local_sdk_video_set_encode_frame_callback(int ch, void *callback) {
  135. fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  136. static int ch_count = 0;
  137. /* two callbacks for video stream 0 are typically detected, unknown what the difference is between them, but if they are both hooked, the app breaks. grab just one of them. */
  138. //stream 0
  139. if( (ch == 0) && ch_count == 2) {
  140. video_encode_cb = callback;
  141. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
  142. callback = video_encode_capture;
  143. }
  144. fprintf(stderr,"ch count is %x\n", ch_count);
  145. //stream 1
  146. if( (ch == 1) && ch_count == 1) {
  147. video_encode_cb1 = callback;
  148. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb1);
  149. callback = video_encode_capture1;
  150. }
  151. ch_count=ch_count+1;
  152. return real_local_sdk_video_set_encode_frame_callback(ch, callback);
  153. }
  154. static void __attribute ((constructor)) video_callback_init(void) {
  155. real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
  156. }