recorderwidget.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. bool startRecording();
  47. // 停止录制
  48. void stopRecording();
  49. // 开始推流
  50. bool startStreaming();
  51. // 停止推流
  52. void stopStreaming();
  53. // 获取状态栏
  54. QWidget* statusBar() const { return m_statusBar; }
  55. public slots:
  56. void onRecordButtonClicked();
  57. void onStreamButtonClicked();
  58. void onSettingsButtonClicked();
  59. void onAudioDeviceButtonClicked();
  60. void updatePreview();
  61. void updateStatus();
  62. signals:
  63. void recordingStarted();
  64. void recordingStopped();
  65. void streamingStarted();
  66. void streamingStopped();
  67. void errorOccurred(const QString& error);
  68. private:
  69. void initUI();
  70. void initStatusBar();
  71. void initRecorder();
  72. void releaseRecorder();
  73. void setupCallbacks();
  74. void refreshAudioDevices();
  75. void refreshVideoEncoders();
  76. // 回调函数
  77. static void onDurationCallback(uint64_t duration);
  78. static void onErrorCallback(int error);
  79. static void onDeviceChangeCallback(int type);
  80. static void onPreviewYUVCallback(const unsigned char *data, unsigned int size, int width, int height, int type);
  81. static void onPreviewAudioCallback();
  82. private:
  83. // UI组件
  84. QVBoxLayout* m_mainLayout = nullptr;
  85. QOpenGLWidget* m_previewWidget = nullptr;
  86. QPushButton* m_recordButton = nullptr;
  87. QPushButton* m_streamButton = nullptr;
  88. QPushButton* m_settingsButton = nullptr;
  89. QPushButton* m_audioDeviceButton = nullptr;
  90. QCheckBox* m_drawCursorCheckBox = nullptr;
  91. QCheckBox* m_syncRecordCheckBox = nullptr;
  92. QComboBox* m_encoderComboBox = nullptr;
  93. QComboBox* m_micComboBox = nullptr;
  94. QComboBox* m_speakerComboBox = nullptr;
  95. QStatusBar* m_statusBar = nullptr;
  96. QLabel* m_statusLabel = nullptr;
  97. QLabel* m_timeLabel = nullptr;
  98. QLabel* m_encoderLabel = nullptr;
  99. // 定时器
  100. QTimer* m_previewTimer = nullptr;
  101. QTimer* m_statusTimer = nullptr;
  102. // 录制相关
  103. Settings m_settings;
  104. AMRECORDER_SETTING m_recorderSetting;
  105. AMRECORDER_CALLBACK m_callbacks;
  106. // 设备信息
  107. AMRECORDER_DEVICE* m_micDevices = nullptr;
  108. AMRECORDER_DEVICE* m_speakerDevices = nullptr;
  109. AMRECORDER_ENCODERS* m_encoders = nullptr;
  110. int m_micCount = 0;
  111. int m_speakerCount = 0;
  112. int m_encoderCount = 0;
  113. // 状态
  114. bool m_isRecording = false;
  115. bool m_isStreaming = false;
  116. bool m_isInitialized = false;
  117. QTime m_recordStartTime;
  118. // 预览相关
  119. QMutex m_previewMutex;
  120. QByteArray m_previewData;
  121. int m_previewWidth = 0;
  122. int m_previewHeight = 0;
  123. // 静态实例指针,用于回调
  124. static RecorderWidget* s_instance;
  125. };