recorderwidget.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. void showPreview();
  64. void hidePreview();
  65. bool isPreviewVisible() const;
  66. // 获取状态栏
  67. QWidget* statusBar() const { return m_statusBar; }
  68. public slots:
  69. void updatePreview();
  70. void updateStatus();
  71. signals:
  72. void recordingStarted();
  73. void recordingStopped();
  74. void streamingStarted();
  75. void streamingStopped();
  76. void errorOccurred(const QString& error);
  77. private:
  78. void initUI();
  79. void initStatusBar();
  80. void initRecorder();
  81. void releaseRecorder();
  82. void setupCallbacks();
  83. void refreshAudioDevices(); // 改为仅维护内部选择,不再更新UI
  84. void refreshVideoEncoders();
  85. // 回调函数
  86. static void onDurationCallback(uint64_t duration);
  87. static void onErrorCallback(int error);
  88. static void onDeviceChangeCallback(int type);
  89. static void onPreviewYUVCallback(const unsigned char *data, unsigned int size, int width, int height, int type);
  90. static void onPreviewAudioCallback();
  91. private:
  92. // UI组件
  93. QVBoxLayout* m_mainLayout = nullptr;
  94. QOpenGLWidget* m_previewWidget = nullptr;
  95. QComboBox* m_encoderComboBox = nullptr;
  96. QComboBox* m_micComboBox = nullptr; // 将不再在UI中创建
  97. QComboBox* m_speakerComboBox = nullptr; // 将不再在UI中创建
  98. QStatusBar* m_statusBar = nullptr;
  99. QLabel* m_statusLabel = nullptr;
  100. QLabel* m_timeLabel = nullptr;
  101. QLabel* m_encoderLabel = nullptr;
  102. // 定时器
  103. QTimer* m_previewTimer = nullptr;
  104. QTimer* m_statusTimer = nullptr;
  105. // 录制相关
  106. Settings m_settings;
  107. AMRECORDER_SETTING m_recorderSetting;
  108. AMRECORDER_CALLBACK m_callbacks;
  109. // 外部选择的音频设备(若未设置,则在启动时使用系统默认设备)
  110. AMRECORDER_DEVICE m_selectedMicDevice{};
  111. AMRECORDER_DEVICE m_selectedSpeakerDevice{};
  112. // 新增:外部选择的视频编码器 id(默认未选择)
  113. int m_selectedEncoderId = -1;
  114. // 设备信息(保留成员避免大范围改动,但不再用于UI)
  115. AMRECORDER_DEVICE* m_micDevices = nullptr;
  116. AMRECORDER_DEVICE* m_speakerDevices = nullptr;
  117. AMRECORDER_ENCODERS* m_encoders = nullptr;
  118. int m_micCount = 0;
  119. int m_speakerCount = 0;
  120. int m_encoderCount = 0;
  121. // 状态
  122. bool m_isRecording = false;
  123. bool m_isStreaming = false;
  124. bool m_isInitialized = false;
  125. QTime m_recordStartTime;
  126. // 预览相关
  127. QMutex m_previewMutex;
  128. QByteArray m_previewData;
  129. int m_previewWidth = 0;
  130. int m_previewHeight = 0;
  131. // 静态实例指针,用于回调
  132. static RecorderWidget* s_instance;
  133. };