recorderwidget.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #pragma once
  2. #include <QWidget>
  3. #include <QVBoxLayout>
  4. #include <QHBoxLayout>
  5. #include <QPushButton>
  6. #include <QLabel>
  7. #include <QCheckBox>
  8. #include <QComboBox>
  9. #include <QGroupBox>
  10. #include <QTimer>
  11. #include <QTime>
  12. #include <QStatusBar>
  13. #include <QOpenGLWidget>
  14. #include <QOpenGLFunctions>
  15. #include <QOpenGLShaderProgram>
  16. #include <QOpenGLTexture>
  17. #include <QMutex>
  18. #include <QThread>
  19. #include <QFuture>
  20. #include <QtConcurrent>
  21. // 引入新的Recorder库
  22. #include "../libs/Recorder/export.h"
  23. #include "../libs/Recorder/ultra_low_latency_recorder_config.h"
  24. /**
  25. * 基于新Recorder库的录制推流组件
  26. * 用于替换原有的AvRecorder实现
  27. */
  28. class RecorderWidget : public QWidget
  29. {
  30. Q_OBJECT
  31. public:
  32. struct Settings {
  33. QString liveUrl; // 将通过NetworkConfig动态获取
  34. std::string liveName = "stream";
  35. std::string outputDir = ".";
  36. int videoBitRate = UltraLowLatencyRecorderConfig::ULTRA_VIDEO_BITRATE; // 使用超低延迟配置
  37. int videoFrameRate = UltraLowLatencyRecorderConfig::ULTRA_VIDEO_FRAMERATE; // 30fps
  38. int videoQuality = 100;
  39. int audioBitRate = UltraLowLatencyRecorderConfig::ULTRA_AUDIO_BITRATE; // 使用超低延迟配置
  40. };
  41. public:
  42. explicit RecorderWidget(QWidget *parent = nullptr);
  43. ~RecorderWidget();
  44. // 设置录制推流参数
  45. void setSettings(const Settings& settings);
  46. // 新增:外部设置音频设备
  47. void setMicDevice(const AMRECORDER_DEVICE& device);
  48. void setSpeakerDevice(const AMRECORDER_DEVICE& device);
  49. // 新增:外部设置视频编码器(由 MainPanel 控制)
  50. void setVideoEncoderId(int encId);
  51. // 开始录制
  52. bool startRecording();
  53. // 停止录制
  54. void stopRecording();
  55. // 开始推流
  56. bool startStreaming();
  57. // 停止推流
  58. void stopStreaming();
  59. // 便捷方法:设置推流流名(用于以房间ID作为流名)
  60. void setLiveName(const QString& name);
  61. // 新增:供MainPanel调用的按钮功能
  62. void onRecordButtonClicked();
  63. void onStreamButtonClicked();
  64. void onSettingsButtonClicked();
  65. // 预览显示控制
  66. void showPreview();
  67. void hidePreview();
  68. bool isPreviewVisible() const;
  69. // 获取状态栏
  70. QWidget* statusBar() const { return m_statusBar; }
  71. public slots:
  72. void updatePreview();
  73. void updateStatus();
  74. signals:
  75. void recordingStarted();
  76. void recordingStopped();
  77. void streamingStarted();
  78. void streamingStopped();
  79. void errorOccurred(const QString& error);
  80. private:
  81. void initUI();
  82. void initStatusBar();
  83. void initRecorder();
  84. void releaseRecorder();
  85. void setupCallbacks();
  86. void refreshAudioDevices(); // 改为仅维护内部选择,不再更新UI
  87. void refreshVideoEncoders();
  88. // 回调函数
  89. static void onDurationCallback(uint64_t duration);
  90. static void onErrorCallback(int error);
  91. static void onDeviceChangeCallback(int type);
  92. static void onPreviewYUVCallback(const unsigned char *data, unsigned int size, int width, int height, int type);
  93. static void onPreviewAudioCallback();
  94. private:
  95. // UI组件
  96. QVBoxLayout* m_mainLayout = nullptr;
  97. QOpenGLWidget* m_previewWidget = nullptr;
  98. QComboBox* m_encoderComboBox = nullptr;
  99. QComboBox* m_micComboBox = nullptr; // 将不再在UI中创建
  100. QComboBox* m_speakerComboBox = nullptr; // 将不再在UI中创建
  101. QStatusBar* m_statusBar = nullptr;
  102. QLabel* m_statusLabel = nullptr;
  103. QLabel* m_timeLabel = nullptr;
  104. QLabel* m_encoderLabel = nullptr;
  105. QLabel* m_vrbLabel = nullptr; // 新增:视频环形缓冲区统计
  106. QLabel* m_arbLabel = nullptr; // 新增:音频环形缓冲区统计
  107. // 定时器
  108. QTimer* m_previewTimer = nullptr;
  109. QTimer* m_statusTimer = nullptr;
  110. // 录制相关
  111. Settings m_settings;
  112. AMRECORDER_SETTING m_recorderSetting;
  113. AMRECORDER_CALLBACK m_callbacks;
  114. // 外部选择的音频设备(若未设置,则在启动时使用系统默认设备)
  115. AMRECORDER_DEVICE m_selectedMicDevice{};
  116. AMRECORDER_DEVICE m_selectedSpeakerDevice{};
  117. // 新增:外部选择的视频编码器 id(默认未选择)
  118. int m_selectedEncoderId = -1;
  119. // 设备信息(保留成员避免大范围改动,但不再用于UI)
  120. AMRECORDER_DEVICE* m_micDevices = nullptr;
  121. AMRECORDER_DEVICE* m_speakerDevices = nullptr;
  122. AMRECORDER_ENCODERS* m_encoders = nullptr;
  123. int m_micCount = 0;
  124. int m_speakerCount = 0;
  125. int m_encoderCount = 0;
  126. // 状态
  127. bool m_isRecording = false;
  128. bool m_isStreaming = false;
  129. bool m_isInitialized = false;
  130. QTime m_recordStartTime;
  131. // 预览相关
  132. QMutex m_previewMutex;
  133. QByteArray m_previewData;
  134. int m_previewWidth = 0;
  135. int m_previewHeight = 0;
  136. // 静态实例指针,用于回调
  137. static RecorderWidget* s_instance;
  138. };