video_callback.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. char *v4l2_device_path = "/dev/video0";
  56. //Check for this file, which should only exist on the V2 cameras
  57. const char *productv2="/driver/sensor_jxf23.ko";
  58. if( access( productv2, F_OK ) != -1 ) {
  59. v4l2_device_path = "/dev/video6";
  60. fprintf(stderr, "[command] v4l2_device_path = %s\n", v4l2_device_path);
  61. } else {
  62. v4l2_device_path = "/dev/video1";
  63. fprintf(stderr, "[command] v4l2_device_path = %s\n", v4l2_device_path);
  64. }
  65. const char *productf="/configs/.product_db3";
  66. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  67. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  68. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  69. struct v4l2_format vid_format;
  70. memset(&vid_format, 0, sizeof(vid_format));
  71. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  72. if( access( productf, F_OK ) == 0 ) {
  73. /* doorbell resolution */
  74. printf("[command] video product 1728x1296");
  75. vid_format.fmt.pix.width = 1728;
  76. vid_format.fmt.pix.height = 1296;
  77. } else {
  78. /* v3 and panv2 res */
  79. printf("[command] video product 1920x1080");
  80. vid_format.fmt.pix.width = 1920;
  81. vid_format.fmt.pix.height = 1080;
  82. }
  83. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  84. vid_format.fmt.pix.sizeimage = 0;
  85. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  86. vid_format.fmt.pix.bytesperline = 0;
  87. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  88. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  89. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  90. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  91. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  92. }
  93. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  94. uint32_t *buf = frames->buf;
  95. int size = write(v4l2Fd, frames->buf, frames->length);
  96. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  97. }
  98. return ((framecb)video_encode_cb)(frames);
  99. }
  100. //secondary stream 1
  101. static uint32_t video_encode_capture1(struct frames_st *frames) {
  102. static int firstEntry = 0;
  103. static int v4l2Fd = -1;
  104. if(!firstEntry) {
  105. firstEntry++;
  106. int err;
  107. char *v4l2_device_path = "/dev/video0";
  108. //Check for this file, which should only exist on the V2 cameras
  109. const char *productv2="/driver/sensor_jxf23.ko";
  110. if( access( productv2, F_OK ) != -1 ) {
  111. v4l2_device_path = "/dev/video7";
  112. fprintf(stderr, "[command] v4l2_device_path = %s\n", v4l2_device_path);
  113. } else {
  114. v4l2_device_path = "/dev/video2";
  115. fprintf(stderr, "[command] v4l2_device_path = %s\n", v4l2_device_path);
  116. }
  117. const char *productf="/configs/.product_db3";
  118. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  119. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  120. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  121. struct v4l2_format vid_format;
  122. memset(&vid_format, 0, sizeof(vid_format));
  123. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  124. if( access( productf, F_OK ) == 0 ) {
  125. /* doorbell resolution */
  126. printf("[command] video product 640x480");
  127. vid_format.fmt.pix.width = 640;
  128. vid_format.fmt.pix.height = 480;
  129. } else {
  130. /* v3 and panv2 res */
  131. printf("[command] video product 640x320");
  132. vid_format.fmt.pix.width = 640;
  133. vid_format.fmt.pix.height = 320;
  134. }
  135. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  136. vid_format.fmt.pix.sizeimage = 0;
  137. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  138. vid_format.fmt.pix.bytesperline = 0;
  139. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  140. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  141. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  142. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  143. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  144. }
  145. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  146. uint32_t *buf = frames->buf;
  147. int size = write(v4l2Fd, frames->buf, frames->length);
  148. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  149. }
  150. return ((framecb)video_encode_cb1)(frames);
  151. }
  152. int local_sdk_video_set_encode_frame_callback(int ch, void *callback) {
  153. fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  154. static int ch_count = 0;
  155. /* 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. */
  156. //stream 0
  157. if( (ch == 0) && ch_count == 2) {
  158. video_encode_cb = callback;
  159. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
  160. callback = video_encode_capture;
  161. } else if( (ch == 0) && ch_count == 3) {
  162. video_encode_cb = callback;
  163. fprintf(stderr,"RTSP FIRMWARE enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
  164. callback = video_encode_capture;
  165. }
  166. fprintf(stderr,"ch count is %x\n", ch_count);
  167. //stream 1
  168. if( (ch == 1) && ch_count == 1) {
  169. video_encode_cb1 = callback;
  170. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb1);
  171. callback = video_encode_capture1;
  172. }
  173. ch_count=ch_count+1;
  174. return real_local_sdk_video_set_encode_frame_callback(ch, callback);
  175. }
  176. static void __attribute ((constructor)) video_callback_init(void) {
  177. real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
  178. }