video_callback.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 = 1;
  21. static uint32_t video_encode_capture(struct frames_st *frames) {
  22. static int firstEntry = 0;
  23. static int v4l2Fd = -1;
  24. if(!firstEntry) {
  25. firstEntry++;
  26. int err;
  27. const char *v4l2_device_path = "/dev/video1";
  28. fprintf(stderr,"Opening V4L2 device: %s \n", v4l2_device_path);
  29. v4l2Fd = open(v4l2_device_path, O_WRONLY, 0777);
  30. if(v4l2Fd < 0) fprintf(stderr,"Failed to open V4L2 device: %s\n", v4l2_device_path);
  31. struct v4l2_format vid_format;
  32. memset(&vid_format, 0, sizeof(vid_format));
  33. vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  34. vid_format.fmt.pix.width = 1920;
  35. vid_format.fmt.pix.height = 1080;
  36. vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_H264;
  37. vid_format.fmt.pix.sizeimage = 0;
  38. vid_format.fmt.pix.field = V4L2_FIELD_NONE;
  39. vid_format.fmt.pix.bytesperline = 0;
  40. vid_format.fmt.pix.colorspace = V4L2_PIX_FMT_YUV420;
  41. err = ioctl(v4l2Fd, VIDIOC_S_FMT, &vid_format);
  42. if(err < 0) fprintf(stderr,"Unable to set V4L2 device video format: %d\n", err);
  43. err = ioctl(v4l2Fd, VIDIOC_STREAMON, &vid_format);
  44. if(err < 0) fprintf(stderr,"Unable to perform VIDIOC_STREAMON: %d\n", err);
  45. }
  46. if( (v4l2Fd >= 0) && VideoCaptureEnable) {
  47. uint32_t *buf = frames->buf;
  48. int size = write(v4l2Fd, frames->buf, frames->length);
  49. if(size != frames->length) fprintf(stderr,"Stream write error: %s\n", size);
  50. }
  51. return ((framecb)video_encode_cb)(frames);
  52. }
  53. int local_sdk_video_set_encode_frame_callback(int ch, void *callback) {
  54. fprintf(stderr, "local_sdk_video_set_encode_frame_callback streamChId=%d, callback=0x%x\n", ch, callback);
  55. if(ch == 0) {
  56. video_encode_cb = callback;
  57. fprintf(stderr,"enc func injection save video_encode_cb=0x%x\n", video_encode_cb);
  58. callback = video_encode_capture;
  59. }
  60. return real_local_sdk_video_set_encode_frame_callback(ch, callback);
  61. }
  62. static void __attribute ((constructor)) video_callback_init(void) {
  63. real_local_sdk_video_set_encode_frame_callback = dlsym(dlopen("/system/lib/liblocalsdk.so", RTLD_LAZY), "local_sdk_video_set_encode_frame_callback");
  64. }