video_callback.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  44. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  45. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  46. struct v4l2_format vid_format;
  47. memset(&vid_format, 0, sizeof(vid_format));
  48. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  49. vid_format.fmt.pix.width = 1920;
  50. vid_format.fmt.pix.height = 1080;
  51. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  52. vid_format.fmt.pix.sizeimage = 0;
  53. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  54. vid_format.fmt.pix.bytesperline = 0;
  55. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  56. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  57. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  58. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  59. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  60. }
  61. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  62. uint32_t *buf = frames->buf;
  63. int size = write(v4l2Fd, frames->buf, frames->length);
  64. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  65. }
  66. return ((framecb)video_encode_cb)(frames);
  67. }
  68. int local_sdk_video_set_encode_frame_callback(int ch, void *callback) {
  69. fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  70. if( (ch == 0) && callback == 0x48cc50) {
  71. video_encode_cb = callback;
  72. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
  73. callback = video_encode_capture;
  74. }
  75. return real_local_sdk_video_set_encode_frame_callback(ch, callback);
  76. }
  77. static void __attribute ((constructor)) video_callback_init(void) {
  78. real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
  79. }