recorderwidget.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  24. * 基于新Recorder库的录制推流组件
  25. * 用于替换原有的AvRecorder实现
  26. */
  27. class RecorderWidget : public QWidget
  28. {
  29. Q_OBJECT
  30. public:
  31. struct Settings {
  32. std::string liveUrl = "rtmp://127.0.0.1:1935/stream/V1";
  33. std::string liveName = "stream";
  34. std::string outputDir = ".";
  35. int videoBitRate = 8000000; // 8Mbps
  36. int videoFrameRate = 30;
  37. int videoQuality = 100;
  38. int audioBitRate = 128000; // 128kbps
  39. };
  40. public:
  41. explicit RecorderWidget(QWidget *parent = nullptr);
  42. ~RecorderWidget();
  43. // 设置录制推流参数
  44. void setSettings(const Settings& settings);
  45. // 新增:外部设置音频设备
  46. void setMicDevice(const AMRECORDER_DEVICE& device);
  47. void setSpeakerDevice(const AMRECORDER_DEVICE& device);
  48. // 新增:外部设置视频编码器(由 MainPanel 控制)
  49. void setVideoEncoderId(int encId);
  50. // 开始录制
  51. bool startRecording();
  52. // 停止录制
  53. void stopRecording();
  54. // 开始推流
  55. bool startStreaming();
  56. // 停止推流
  57. void stopStreaming();
  58. // 新增:供MainPanel调用的按钮功能
  59. void onRecordButtonClicked();
  60. void onStreamButtonClicked();
  61. void onSettingsButtonClicked();
  62. // 获取状态栏
  63. QWidget* statusBar() const { return m_statusBar; }
  64. public slots:
  65. void updatePreview();
  66. void updateStatus();
  67. signals:
  68. void recordingStarted();
  69. void recordingStopped();
  70. void streamingStarted();
  71. void streamingStopped();
  72. void errorOccurred(const QString& error);
  73. private:
  74. void initUI();
  75. void initStatusBar();
  76. void initRecorder();
  77. void releaseRecorder();
  78. void setupCallbacks();
  79. void refreshAudioDevices(); // 改为仅维护内部选择,不再更新UI
  80. void refreshVideoEncoders();
  81. // 回调函数
  82. static void onDurationCallback(uint64_t duration);
  83. static void onErrorCallback(int error);
  84. static void onDeviceChangeCallback(int type);
  85. static void onPreviewYUVCallback(const unsigned char *data, unsigned int size, int width, int height, int type);
  86. static void onPreviewAudioCallback();
  87. private:
  88. // UI组件
  89. QVBoxLayout* m_mainLayout = nullptr;
  90. QOpenGLWidget* m_previewWidget = nullptr;
  91. QComboBox* m_encoderComboBox = nullptr;
  92. QComboBox* m_micComboBox = nullptr; // 将不再在UI中创建
  93. QComboBox* m_speakerComboBox = nullptr; // 将不再在UI中创建
  94. QStatusBar* m_statusBar = nullptr;
  95. QLabel* m_statusLabel = nullptr;
  96. QLabel* m_timeLabel = nullptr;
  97. QLabel* m_encoderLabel = nullptr;
  98. // 定时器
  99. QTimer* m_previewTimer = nullptr;
  100. QTimer* m_statusTimer = nullptr;
  101. // 录制相关
  102. Settings m_settings;
  103. AMRECORDER_SETTING m_recorderSetting;
  104. AMRECORDER_CALLBACK m_callbacks;
  105. // 外部选择的音频设备(若未设置,则在启动时使用系统默认设备)
  106. AMRECORDER_DEVICE m_selectedMicDevice{};
  107. AMRECORDER_DEVICE m_selectedSpeakerDevice{};
  108. // 新增:外部选择的视频编码器 id(默认未选择)
  109. int m_selectedEncoderId = -1;
  110. // 设备信息(保留成员避免大范围改动,但不再用于UI)
  111. AMRECORDER_DEVICE* m_micDevices = nullptr;
  112. AMRECORDER_DEVICE* m_speakerDevices = nullptr;
  113. AMRECORDER_ENCODERS* m_encoders = nullptr;
  114. int m_micCount = 0;
  115. int m_speakerCount = 0;
  116. int m_encoderCount = 0;
  117. // 状态
  118. bool m_isRecording = false;
  119. bool m_isStreaming = false;
  120. bool m_isInitialized = false;
  121. QTime m_recordStartTime;
  122. // 预览相关
  123. QMutex m_previewMutex;
  124. QByteArray m_previewData;
  125. int m_previewWidth = 0;
  126. int m_previewHeight = 0;
  127. // 静态实例指针,用于回调
  128. static RecorderWidget* s_instance;
  129. };