BaseRecordThread.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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::threadTask()
  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. /* 执行任务 */
  24. task();
  25. m_threadInfo.threadState = EThreadState::State_Stopped;
  26. }
  27. /* 停止线程 */
  28. void BaseRecordThread::stopThread()
  29. {
  30. m_isRunning.store(false);
  31. }
  32. /* 获取频率ID */
  33. const RecordThreadInfo_t& BaseRecordThread::getThreadInfo()
  34. {
  35. return m_threadInfo;
  36. }
  37. /* 根据文件大小计算时间长度,单位ms */
  38. QDateTime BaseRecordThread::previTime(QDateTime& currentTime, int64_t dataSize)
  39. {
  40. if(dataSize <= 0)
  41. {
  42. return currentTime; // 如果数据大小为0,返回当前时间
  43. }
  44. int64_t offsetMS = dataSize * 1000 / m_oneSecondSize;
  45. return currentTime.addMSecs(-offsetMS);
  46. }
  47. /*根据当前时间和数据大小向后推算时间 */
  48. QDateTime BaseRecordThread::nextTime(QDateTime& currentTime, int64_t dataSize)
  49. {
  50. if(dataSize <= 0)
  51. {
  52. return currentTime; // 如果数据大小为0,返回当前时间
  53. }
  54. int64_t offsetMS = dataSize * 1000 / m_oneSecondSize;
  55. return currentTime.addMSecs(offsetMS);
  56. }
  57. /* 计算出到下一个整点秒需要去掉多少个字节 */
  58. int32_t BaseRecordThread::calculateOffsetToNextSecond(QDateTime& currentTime)
  59. {
  60. QDateTime nextSecond = currentTime.addSecs(1);
  61. nextSecond = nextSecond.addMSecs(-nextSecond.time().msec()); // 去掉毫秒部分
  62. int64_t offsetMS = nextSecond.toMSecsSinceEpoch() - currentTime.toMSecsSinceEpoch();
  63. return static_cast<int32_t>(offsetMS * m_oneSecondSize / 1000); // 转换为字节数
  64. }