BaseRecordThread.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "BaseRecordThread.h"
  2. #include <QDateTime>
  3. BaseRecordThread::BaseRecordThread(RecordThreadInfo_t& threadInfo)
  4. : m_threadInfo(threadInfo)
  5. {
  6. m_logger = spdlog::get("RecordAudio");
  7. if(m_logger == nullptr)
  8. {
  9. fmt::print("RecordThread: RecordAudio Logger not found.\n");
  10. return;
  11. }
  12. }
  13. BaseRecordThread::~BaseRecordThread()
  14. {
  15. }
  16. /* 线程任务函数,子类需要实现 */
  17. void BaseRecordThread::thread_task()
  18. {
  19. m_isRunning.store(true);
  20. m_logBase = fmt::format("录音通道: {}:{}", m_threadInfo.cardRoadInfo.strSoundCardName,
  21. m_threadInfo.cardRoadInfo.pcmInfo.strPCMName);
  22. m_threadInfo.threadState = EThreadState::State_Running;
  23. m_isRunning.store(true);
  24. m_isStoped.store(false);
  25. /* 执行任务 */
  26. task();
  27. m_threadInfo.threadState = EThreadState::State_Stopped;
  28. m_isRunning.store(false);
  29. /* m_isStoped必须放在最后 */
  30. m_isStoped.store(true);
  31. }
  32. /* 停止线程 */
  33. void BaseRecordThread::thread_stop()
  34. {
  35. m_isRunning.store(false);
  36. }
  37. /* 阻塞停止线程 */
  38. void BaseRecordThread::thread_stop_block()
  39. {
  40. if(m_isRunning.load())
  41. {
  42. m_isRunning.store(false);
  43. }
  44. /* 等待退出 */
  45. while(m_isStoped.load() == false)
  46. {
  47. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  48. }
  49. }
  50. /* 获取频率ID */
  51. const RecordThreadInfo_t& BaseRecordThread::getThreadInfo()
  52. {
  53. return m_threadInfo;
  54. }
  55. /* 根据文件大小计算时间长度,单位ms */
  56. QDateTime BaseRecordThread::previTime(QDateTime& currentTime, int64_t dataSize)
  57. {
  58. if(dataSize <= 0)
  59. {
  60. return currentTime; // 如果数据大小为0,返回当前时间
  61. }
  62. int64_t offsetMS = dataSize * 1000 / m_oneSecondSize;
  63. return currentTime.addMSecs(-offsetMS);
  64. }
  65. /*根据当前时间和数据大小向后推算时间 */
  66. QDateTime BaseRecordThread::nextTime(QDateTime& currentTime, int64_t dataSize)
  67. {
  68. if(dataSize <= 0)
  69. {
  70. return currentTime; // 如果数据大小为0,返回当前时间
  71. }
  72. int64_t offsetMS = dataSize * 1000 / m_oneSecondSize;
  73. return currentTime.addMSecs(offsetMS);
  74. }
  75. /* 计算出到下一个整点秒需要去掉多少个字节 */
  76. int32_t BaseRecordThread::calculateOffsetToNextSecond(QDateTime& currentTime)
  77. {
  78. QDateTime nextSecond = currentTime.addSecs(1);
  79. nextSecond = nextSecond.addMSecs(-nextSecond.time().msec()); // 去掉毫秒部分
  80. int64_t offsetMS = nextSecond.toMSecsSinceEpoch() - currentTime.toMSecsSinceEpoch();
  81. return static_cast<int32_t>(offsetMS * m_oneSecondSize / 1000); // 转换为字节数
  82. }