MppManager.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "MppManager.h"
  2. #include "../DataManager/DataManager.h"
  3. #include "../DataManager/DataPackage.h"
  4. #include "../ThreadGuardian/ThreadGuardian.h"
  5. #include "../RGAColorTransfer/RgaColorTransfer.h"
  6. #include <thread>
  7. #include "../ImageTest/ImageTest.h"
  8. // ...existing code...
  9. void MppManager::updateThreadStatus(int threadIndex)
  10. {
  11. std::string threadName = "mppThread_" + std::to_string(threadIndex);
  12. ThreadGuardian::getInstance().updateThreadHeartbeat(threadName);
  13. }
  14. MppManager::~MppManager()
  15. {
  16. for (int i = 0; i < m_threads.size(); i++)
  17. {
  18. m_bThreadSwitch = false;
  19. if (m_threads[i].joinable())
  20. {
  21. m_threads[i].join();
  22. }
  23. }
  24. }
  25. void MppManager::addMppDecode()
  26. {
  27. m_vThreadSwitch.resize(3);
  28. m_bThreadSwitch = true;
  29. m_threads.emplace_back(&MppManager::decodeThread, this, 0);
  30. int index = m_threads.size() - 1;
  31. ThreadGuardian::getInstance().registerMppThread(index, std::bind(&MppManager::restartDecodeThread, this, index));
  32. m_threads.emplace_back(&MppManager::decodeThread, this, 1);
  33. index = m_threads.size() - 1;
  34. ThreadGuardian::getInstance().registerMppThread(index, std::bind(&MppManager::restartDecodeThread, this, index));
  35. m_threads.emplace_back(&MppManager::decodeThread, this, 2);
  36. index = m_threads.size() - 1;
  37. ThreadGuardian::getInstance().registerMppThread(index, std::bind(&MppManager::restartDecodeThread, this, index));
  38. }
  39. void MppManager::restartDecodeThread(int threadIndex)
  40. {
  41. // 先停止当前线程
  42. // if (m_threads[threadIndex].joinable())
  43. // {
  44. // m_vThreadSwitch[threadIndex] = false;
  45. // m_threads[threadIndex].join();
  46. // }
  47. // 重新启动线程
  48. if (threadIndex >= 0 && threadIndex < m_threads.size())
  49. {
  50. m_threads[threadIndex] = std::thread(&MppManager::decodeThread, this, threadIndex);
  51. std::cout << "MPP线程 " << threadIndex << " 已重新启动" << std::endl;
  52. }
  53. }
  54. void MppManager::decodeThread(int index)
  55. {
  56. MppDecoder decoder;
  57. decoder.init();
  58. RgaColorTransfer colorTransfer;
  59. unsigned char *rgbData = nullptr;
  60. int width = 0, height = 0;
  61. DataManager::getInstance().addDataPipe<DataPackagePtr>("resized" + std::to_string(index));
  62. // test
  63. UsbTest::HighResolutionTimer timer;
  64. while (m_bThreadSwitch)
  65. {
  66. DataPackagePtr dataPackage = nullptr;
  67. // update thread status
  68. updateThreadStatus(index);
  69. if (DataManager::getInstance().popData("uvc" + std::to_string(index), dataPackage))
  70. {
  71. decoder.decodeJpegToRgb((const char *)dataPackage->pJpegData, dataPackage->nJpegSize, (unsigned char **)&dataPackage->pRGBData, &width, &height);
  72. if (width != dataPackage->nWidth || height != dataPackage->nHeight)
  73. {
  74. std::cerr << "the decode result is different from defined !!" << std::endl;
  75. }
  76. std::cout << "Decoded frame " << index << std::endl;
  77. // color transfer
  78. resize_image(dataPackage->pRGBData, width, height, dataPackage->pResizeData, dataPackage->nResizeWidth, dataPackage->nResizeHeight);
  79. // ImageTest::saveImageFromData((unsigned char *)dataPackage->pResizeData, dataPackage->nResizeWidth, dataPackage->nResizeHeight);
  80. std::string logMessage = "Decoded frame " + std::to_string(index) + " degree : " + std::to_string(dataPackage->dDegree) + " " + UsbTest::GlobalResolutionTimer::getInstance().pressStopWatchString();
  81. // UsbTest::TimeRecorder::getInstance().recordTime(logMessage);
  82. // push data to the next pipe
  83. DataManager::getInstance().pushData("resized" + std::to_string(index), dataPackage);
  84. }
  85. // delay 10ms
  86. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  87. }
  88. // 释放资源
  89. if (rgbData)
  90. {
  91. delete[] rgbData;
  92. }
  93. DataManager::getInstance().eraseDataPipe("mpp" + std::to_string(index));
  94. }