UVCCallBack.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7. #include <memory>
  8. #include <filesystem>
  9. #include "../libuvc/libuvc.h"
  10. #include "UVCDeviceManager.h"
  11. #include "../DataManager/DataManager.h"
  12. #include "../DataManager/DataPackage.h"
  13. #include <opencv4/opencv2/opencv.hpp>
  14. #include <opencv4/opencv2/imgcodecs.hpp>
  15. #include "../ImageTest/ImageTest.h"
  16. struct timeval pre_frame_time;
  17. /* This callback function runs once per frame. Use it to perform any
  18. * quick processing you need, or have it put the frame into your application's
  19. * input queue. If this function takes too long, you'll start losing frames. */
  20. void cbSaveToLocal(uvc_frame_t *frame, void *ptr)
  21. {
  22. uvc_error_t ret;
  23. int *pindex = (int *)ptr;
  24. int index = *pindex;
  25. FILE *fp;
  26. static int jpeg_count[3] = {0};
  27. static int jpeg_group[3] = {0};
  28. static const char *H264_FILE = "iOSDevLog.h264";
  29. static const char *MJPEG_FILE = ".jpeg";
  30. char filename[264];
  31. std::string FileSavePath = "";
  32. std::fstream log_file("CallBackLog.txt", std::ios::app);
  33. DataPackagePtr dataPackage = nullptr;
  34. // static MemoryPool<DataPackage> dataPool(BUFFER_SIZE, 3840, 2160);
  35. std::shared_ptr<DataPipe<DataPackage>> pipe;
  36. switch (frame->frame_format)
  37. {
  38. case UVC_FRAME_FORMAT_H264:
  39. /* use `ffplay H264_FILE` to play */
  40. fp = fopen(H264_FILE, "a");
  41. fwrite(frame->data, 1, frame->data_bytes, fp);
  42. fclose(fp);
  43. break;
  44. case UVC_COLOR_FORMAT_MJPEG:
  45. if (!g_gpioExplorer->getFailingStatus(index))
  46. {
  47. //std::cout << "Pass this wave in channel " << index << std::endl;
  48. break;
  49. }
  50. g_gpioExplorer->resetFailingStatus(index);
  51. // if(!UsbTest::SimulateTrigger::getInstance().isTriggering(index))
  52. // {
  53. // break;
  54. // }
  55. if (jpeg_count[index] / 23 >= 1)
  56. jpeg_group[index]++;
  57. jpeg_count[index] = jpeg_count[index] % 23;
  58. std::cout << "Current File Device " << index << " UVC Index : " << jpeg_count[index] << std::endl;
  59. // 在 data/ 文件夹下创建文件夹,文件夹名称为设备索引
  60. // if (!std::filesystem::exists("./data/" + std::to_string(index)))
  61. // {
  62. // std::filesystem::create_directory("./data/" + std::to_string(index));
  63. // }
  64. // FileSavePath = "./data/" + std::to_string(index) + "/" + std::to_string(jpeg_group[index]) + "-" + std::to_string(jpeg_count[index]++) + MJPEG_FILE;
  65. // fp = fopen(FileSavePath.c_str(), "wb");
  66. // if (fp == NULL)
  67. // {
  68. // std::cerr << "Failed to open file: " << FileSavePath << std::endl;
  69. // return;
  70. // }
  71. // fwrite(frame->data, 1, frame->data_bytes, fp);
  72. // fclose(fp);
  73. // sprintf(filename, "./data/%d/%d-%d%s", index, jpeg_group[index], jpeg_count[index]++, MJPEG_FILE);
  74. dataPackage = DataManager::getInstance().acquireDataBuffer<DataPackage>();
  75. memcpy(dataPackage->pJpegData, frame->data, frame->data_bytes);
  76. dataPackage->nJpegSize = frame->data_bytes;
  77. dataPackage->nWidth = frame->width;
  78. dataPackage->nHeight = frame->height;
  79. dataPackage->nTimeStamp = frame->capture_time_finished.tv_sec * 1000 + frame->capture_time_finished.tv_nsec / 1000000;
  80. dataPackage->nCameraID = UVCManager::getInstance().getDeviceNumber(index);
  81. dataPackage->dDegree = jpeg_count[index]++;
  82. DataManager::getInstance().pushData<DataPackage>("uvc" + std::to_string(index), dataPackage);
  83. break;
  84. case UVC_COLOR_FORMAT_YUYV:
  85. break;
  86. default:
  87. break;
  88. }
  89. if (frame->sequence % 30 == 0)
  90. {
  91. printf(" * got image %u\n", frame->sequence);
  92. }
  93. }
  94. // call back function
  95. void cbDoNothing(uvc_frame_t *frame, void *ptr)
  96. {
  97. printf("callback! frame_format = %d, width = %d, height = %d, length = %lu, ptr = %p\n",
  98. frame->frame_format, frame->width, frame->height, frame->data_bytes, ptr);
  99. }