test_player_with_ui.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. #include <chrono>
  2. #include <string>
  3. #include <thread>
  4. #include <iomanip>
  5. #include "code/base/logger.h"
  6. #include "code/player/player_core_v2.h"
  7. #include <QApplication>
  8. #include <QWidget>
  9. #include <QVBoxLayout>
  10. #include <QHBoxLayout>
  11. #include <QPushButton>
  12. #include <QLabel>
  13. #include <QSlider>
  14. #include <QFileDialog>
  15. #include <QMessageBox>
  16. #include <QTimer>
  17. #include <QProgressBar>
  18. #include <QGroupBox>
  19. #include <QSpinBox>
  20. #include <QCheckBox>
  21. #include <QComboBox>
  22. #include <QTextEdit>
  23. #include <QSplitter>
  24. #include <QDebug>
  25. #include <QDateTime>
  26. #include <QFile>
  27. #include <QTextStream>
  28. using namespace av::player;
  29. using namespace av::utils;
  30. // 播放器事件回调类
  31. class UIPlayerCallback : public PlayerEventCallback {
  32. public:
  33. UIPlayerCallback(QWidget* parent) : m_parent(parent) {}
  34. void onStateChanged(PlayerState newState) override {
  35. std::string stateStr;
  36. switch (newState) {
  37. case PlayerState::Idle: stateStr = "Idle"; break;
  38. case PlayerState::Opening: stateStr = "Opening"; break;
  39. case PlayerState::Stopped: stateStr = "Stopped"; break;
  40. case PlayerState::Playing: stateStr = "Playing"; break;
  41. case PlayerState::Paused: stateStr = "Paused"; break;
  42. case PlayerState::Seeking: stateStr = "Seeking"; break;
  43. case PlayerState::Error: stateStr = "Error"; break;
  44. }
  45. Logger::instance().info("[EVENT] State changed to: " + stateStr);
  46. // 发送信号到UI线程
  47. QMetaObject::invokeMethod(m_parent, "onPlayerStateChanged", Qt::QueuedConnection,
  48. Q_ARG(int, static_cast<int>(newState)));
  49. }
  50. void onPositionChanged(int64_t position) override {
  51. // 发送位置更新到UI线程
  52. QMetaObject::invokeMethod(m_parent, "onPlayerPositionChanged", Qt::QueuedConnection,
  53. Q_ARG(qint64, position));
  54. }
  55. void onMediaInfoChanged(const MediaInfo& info) override {
  56. Logger::instance().info("[EVENT] Media info changed:");
  57. Logger::instance().info(" File: " + info.filename);
  58. Logger::instance().info(" Duration: " + std::to_string(info.duration / 1000000.0) + "s");
  59. Logger::instance().info(" Has Video: " + std::string(info.hasVideo ? "Yes" : "No"));
  60. Logger::instance().info(" Has Audio: " + std::string(info.hasAudio ? "Yes" : "No"));
  61. if (info.hasVideo) {
  62. Logger::instance().info(" Video: " + std::to_string(info.width) + "x" + std::to_string(info.height) + " @ " + std::to_string(info.fps) + " fps");
  63. }
  64. if (info.hasAudio) {
  65. Logger::instance().info(" Audio: " + std::to_string(info.sampleRate) + " Hz, " + std::to_string(info.channels) + " channels");
  66. }
  67. // 发送媒体信息到UI线程
  68. QMetaObject::invokeMethod(m_parent, "onMediaInfoChanged", Qt::QueuedConnection);
  69. }
  70. void onErrorOccurred(const std::string& error) override {
  71. Logger::instance().error("[ERROR] " + error);
  72. QMetaObject::invokeMethod(m_parent, "onPlayerError", Qt::QueuedConnection,
  73. Q_ARG(QString, QString::fromStdString(error)));
  74. }
  75. void onFrameDropped(int64_t totalDropped) override {
  76. Logger::instance().warning("[WARNING] Frame dropped, total: " + std::to_string(totalDropped));
  77. }
  78. void onSyncError(double error, const std::string& reason) override {
  79. Logger::instance().warning("[WARNING] Sync error: " + std::to_string(error * 1000) + "ms, reason: " + reason);
  80. }
  81. void onEndOfFile() override {
  82. Logger::instance().info("[EVENT] End of file reached");
  83. QMetaObject::invokeMethod(m_parent, "onPlayerEndOfFile", Qt::QueuedConnection);
  84. }
  85. private:
  86. QWidget* m_parent;
  87. };
  88. // 主播放器窗口类
  89. class PlayerWindow : public QWidget {
  90. Q_OBJECT
  91. public:
  92. PlayerWindow(QWidget* parent = nullptr) : QWidget(parent) {
  93. setupUI();
  94. setupPlayer();
  95. connectSignals();
  96. // 设置窗口属性
  97. setWindowTitle("AV Player Test with UI");
  98. resize(1280, 900); // 增加窗口大小以适应更多统计信息
  99. // 启动更新定时器
  100. m_updateTimer = new QTimer(this);
  101. connect(m_updateTimer, &QTimer::timeout, this, &PlayerWindow::updateUI);
  102. m_updateTimer->start(100); // 100ms更新一次
  103. m_player->openFile("C:/Users/zhuizhu/Videos/2.mp4");
  104. // playPause();
  105. }
  106. ~PlayerWindow() {
  107. if (m_player) {
  108. m_player->stop();
  109. }
  110. }
  111. public slots:
  112. void onPlayerStateChanged(int state) {
  113. PlayerState playerState = static_cast<PlayerState>(state);
  114. switch (playerState) {
  115. case PlayerState::Idle:
  116. m_stateLabel->setText("状态: 空闲");
  117. m_playButton->setText("播放");
  118. m_playButton->setEnabled(false);
  119. break;
  120. case PlayerState::Opening:
  121. m_stateLabel->setText("状态: 打开中...");
  122. m_playButton->setEnabled(false);
  123. break;
  124. case PlayerState::Stopped:
  125. m_stateLabel->setText("状态: 已停止");
  126. m_playButton->setText("播放");
  127. m_playButton->setEnabled(true);
  128. break;
  129. case PlayerState::Playing:
  130. m_stateLabel->setText("状态: 播放中");
  131. m_playButton->setText("暂停");
  132. m_playButton->setEnabled(true);
  133. break;
  134. case PlayerState::Paused:
  135. m_stateLabel->setText("状态: 已暂停");
  136. m_playButton->setText("播放");
  137. m_playButton->setEnabled(true);
  138. break;
  139. case PlayerState::Seeking:
  140. m_stateLabel->setText("状态: 跳转中...");
  141. break;
  142. case PlayerState::Error:
  143. m_stateLabel->setText("状态: 错误");
  144. m_playButton->setEnabled(false);
  145. break;
  146. }
  147. }
  148. void onPlayerPositionChanged(qint64 position) {
  149. if (!m_seeking) {
  150. // 更新位置标签(秒数显示)
  151. double seconds = position / 1000000.0;
  152. m_positionLabel->setText(QString("位置: %1s").arg(seconds, 0, 'f', 2));
  153. // 我们不在这里更新进度条和时间标签,而是在updateUI中统一更新
  154. // 这样可以避免UI更新过于频繁
  155. }
  156. }
  157. void onMediaInfoChanged() {
  158. if (m_player) {
  159. auto info = m_player->getMediaInfo();
  160. m_duration = info.duration;
  161. QString infoText = QString("文件: %1\n")
  162. .arg(QString::fromStdString(info.filename));
  163. if (info.duration > 0) {
  164. double durationSec = info.duration / 1000000.0;
  165. infoText += QString("时长: %1s\n").arg(durationSec, 0, 'f', 2);
  166. // 更新时间标签显示总时长
  167. m_timeLabel->setText(QString("00:00:00 / %1").arg(formatTime(info.duration)));
  168. }
  169. // 更新视频流复选框状态
  170. m_videoStreamCheckBox->setEnabled(info.hasVideo);
  171. if (!info.hasVideo) {
  172. m_videoStreamCheckBox->setChecked(false);
  173. }
  174. if (info.hasVideo) {
  175. infoText += QString("视频: %1x%2 @ %3fps\n")
  176. .arg(info.width).arg(info.height).arg(info.fps, 0, 'f', 2);
  177. // 初始化视频渲染器
  178. // if (m_videoRenderer && !m_videoRenderer->isInitialized()) {
  179. // m_videoRenderer->initialize(info.width, info.height, AV_PIX_FMT_YUV420P, info.fps);
  180. // }
  181. }
  182. // 更新音频流复选框状态
  183. m_audioStreamCheckBox->setEnabled(info.hasAudio);
  184. if (!info.hasAudio) {
  185. m_audioStreamCheckBox->setChecked(false);
  186. }
  187. if (info.hasAudio) {
  188. infoText += QString("音频: %1Hz, %2ch\n")
  189. .arg(info.sampleRate).arg(info.channels);
  190. }
  191. m_infoLabel->setText(infoText);
  192. m_playButton->setEnabled(true);
  193. }
  194. }
  195. void onPlayerError(const QString& error) {
  196. QMessageBox::critical(this, "播放器错误", error);
  197. }
  198. void onPlayerEndOfFile() {
  199. m_playButton->setText("播放");
  200. // 重置进度条和时间显示
  201. m_progressSlider->setValue(0);
  202. // 如果有持续时间,显示0/总时长;否则显示0/0
  203. if (m_duration > 0) {
  204. m_timeLabel->setText(QString("00:00:00 / %1").arg(formatTime(m_duration)));
  205. } else {
  206. m_timeLabel->setText("00:00:00 / 00:00:00");
  207. }
  208. }
  209. private slots:
  210. void openFile() {
  211. QString fileName = QFileDialog::getOpenFileName(this,
  212. "选择媒体文件", "",
  213. "媒体文件 (*.mp4 *.avi *.mkv *.mov *.wmv *.flv *.webm *.mp3 *.wav *.aac *.flac);;所有文件 (*.*)");
  214. if (!fileName.isEmpty()) {
  215. loadFile(fileName.toStdString());
  216. }
  217. }
  218. void refreshStats() {
  219. if (m_player) {
  220. // 调用播放器的dumpStats方法输出详细统计信息到日志
  221. m_player->dumpStats();
  222. // 强制更新UI显示
  223. updateUI();
  224. // 显示提示信息
  225. QMessageBox::information(this, "统计信息", "统计信息已刷新,详细信息已写入日志文件。");
  226. }
  227. }
  228. void exportStats() {
  229. if (!m_player) return;
  230. // 获取保存文件名
  231. QString fileName = QFileDialog::getSaveFileName(this,
  232. "导出统计信息", "player_stats.txt",
  233. "文本文件 (*.txt);;所有文件 (*.*)");
  234. if (fileName.isEmpty()) return;
  235. // 获取统计信息
  236. auto stats = m_player->getStats();
  237. auto mediaInfo = m_player->getMediaInfo();
  238. auto debugInfo = QString::fromStdString(m_player->getDebugInfo());
  239. // 创建文件
  240. QFile file(fileName);
  241. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
  242. QMessageBox::critical(this, "错误", "无法创建文件: " + fileName);
  243. return;
  244. }
  245. QTextStream out(&file);
  246. // 写入时间戳
  247. auto now = QDateTime::currentDateTime();
  248. out << "播放器统计信息导出 - " << now.toString("yyyy-MM-dd HH:mm:ss") << "\n\n";
  249. // 写入媒体信息
  250. out << "=== 媒体信息 ===\n";
  251. out << "文件: " << QString::fromStdString(mediaInfo.filename) << "\n";
  252. out << "时长: " << (mediaInfo.duration / 1000000.0) << " 秒\n";
  253. if (mediaInfo.hasVideo) {
  254. out << "视频: " << mediaInfo.width << "x" << mediaInfo.height << " @ " << mediaInfo.fps << " fps\n";
  255. }
  256. if (mediaInfo.hasAudio) {
  257. out << "音频: " << mediaInfo.sampleRate << " Hz, " << mediaInfo.channels << " 通道\n";
  258. }
  259. out << "比特率: " << (mediaInfo.bitrate / 1000.0) << " kbps\n\n";
  260. // 写入播放统计
  261. out << "=== 播放统计 ===\n";
  262. out << "当前位置: " << (stats.currentTime / 1000000.0) << " 秒\n";
  263. out << "总帧数: " << stats.totalFrames << "\n";
  264. out << "丢帧数: " << stats.droppedFrames;
  265. if (stats.totalFrames > 0) {
  266. out << " (" << (stats.droppedFrames * 100.0 / stats.totalFrames) << "%)";
  267. }
  268. out << "\n";
  269. out << "重复帧数: " << stats.duplicatedFrames << "\n";
  270. out << "播放速度: " << stats.playbackSpeed << "x\n\n";
  271. // 写入队列状态
  272. out << "=== 队列状态 ===\n";
  273. out << "视频帧队列: " << stats.queuedVideoFrames << "\n";
  274. out << "音频帧队列: " << stats.queuedAudioFrames << "\n";
  275. out << "数据包队列: " << stats.queuedPackets << "\n\n";
  276. // 写入同步状态
  277. out << "=== 同步状态 ===\n";
  278. out << "当前同步误差: " << (stats.syncError * 1000) << " ms\n";
  279. out << "平均同步误差: " << (stats.avgSyncError * 1000) << " ms\n";
  280. out << "最大同步误差: " << (stats.maxSyncError * 1000) << " ms\n\n";
  281. // 写入性能统计
  282. out << "=== 性能统计 ===\n";
  283. out << "CPU使用率: " << stats.cpuUsage << "%\n";
  284. out << "内存使用: " << stats.memoryUsage << " MB\n";
  285. out << "比特率: " << stats.bitrate << " kbps\n\n";
  286. // 写入调试信息
  287. out << "=== 调试信息 ===\n";
  288. out << debugInfo << "\n";
  289. file.close();
  290. QMessageBox::information(this, "导出成功", "统计信息已成功导出到: " + fileName);
  291. }
  292. void playPause() {
  293. if (!m_player) return;
  294. auto state = m_player->getState();
  295. if (state == PlayerState::Playing) {
  296. m_player->pause();
  297. } else if (state == PlayerState::Paused || state == PlayerState::Stopped) {
  298. m_player->play();
  299. }
  300. }
  301. void stop() {
  302. if (m_player) {
  303. m_player->stop();
  304. }
  305. }
  306. void onProgressSliderPressed() {
  307. m_seeking = true;
  308. }
  309. void onProgressSliderReleased() {
  310. if (m_player && m_duration > 0) {
  311. int value = m_progressSlider->value();
  312. int64_t position = (m_duration * value) / 100;
  313. m_player->seek(position);
  314. // 添加一个延迟后再设置m_seeking为false
  315. // 这样可以让seek操作有足够时间完成初始化和缓冲
  316. QTimer::singleShot(500, this, [this]() {
  317. m_seeking = false;
  318. });
  319. return; // 不要立即设置m_seeking为false
  320. }
  321. m_seeking = false;
  322. }
  323. void onProgressSliderMoved(int value) {
  324. if (m_player && m_duration > 0) {
  325. // 计算预览位置
  326. int64_t previewPosition = (m_duration * value) / 100;
  327. // 更新时间标签显示预览时间
  328. QString timeText = QString("%1 / %2")
  329. .arg(formatTime(previewPosition))
  330. .arg(formatTime(m_duration));
  331. m_timeLabel->setText(timeText);
  332. }
  333. }
  334. void onVolumeChanged(int value) {
  335. if (m_player) {
  336. double volume = value / 100.0;
  337. m_player->setVolume(volume);
  338. m_volumeLabel->setText(QString("音量: %1%").arg(value));
  339. }
  340. }
  341. // 将微秒转换为时间字符串 (HH:MM:SS)
  342. QString formatTime(int64_t timeUs) {
  343. int totalSeconds = static_cast<int>(timeUs / 1000000);
  344. int hours = totalSeconds / 3600;
  345. int minutes = (totalSeconds % 3600) / 60;
  346. int seconds = totalSeconds % 60;
  347. return QString("%1:%2:%3")
  348. .arg(hours, 2, 10, QChar('0'))
  349. .arg(minutes, 2, 10, QChar('0'))
  350. .arg(seconds, 2, 10, QChar('0'));
  351. }
  352. void updateUI() {
  353. if (m_player) {
  354. // 更新统计信息
  355. auto stats = m_player->getStats();
  356. // 基本播放统计
  357. QString statsText = QString("帧数: %1\n丢帧: %2 (%3%)\n重复帧: %4\n速度: %5x")
  358. .arg(stats.totalFrames)
  359. .arg(stats.droppedFrames)
  360. .arg(stats.totalFrames > 0 ? (stats.droppedFrames * 100.0 / stats.totalFrames) : 0, 0, 'f', 1)
  361. .arg(stats.duplicatedFrames)
  362. .arg(stats.playbackSpeed, 0, 'f', 2);
  363. // 队列状态
  364. statsText += QString("\n\n队列状态:\n视频帧: %1\n音频帧: %2\n数据包: %3")
  365. .arg(stats.queuedVideoFrames)
  366. .arg(stats.queuedAudioFrames)
  367. .arg(stats.queuedPackets);
  368. // 同步统计
  369. statsText += QString("\n\n同步状态:\n当前误差: %1 ms\n平均误差: %2 ms\n最大误差: %3 ms")
  370. .arg(stats.syncError * 1000, 0, 'f', 1)
  371. .arg(stats.avgSyncError * 1000, 0, 'f', 1)
  372. .arg(stats.maxSyncError * 1000, 0, 'f', 1);
  373. // 性能统计
  374. statsText += QString("\n\n性能:\nCPU: %1%\n内存: %2 MB\n比特率: %3 kbps")
  375. .arg(stats.cpuUsage, 0, 'f', 1)
  376. .arg(stats.memoryUsage, 0, 'f', 1)
  377. .arg(stats.bitrate, 0, 'f', 0);
  378. m_statsLabel->setText(statsText);
  379. // 更新时间显示和进度条
  380. if (!m_seeking && m_duration > 0) {
  381. int64_t currentPosition = m_player->getCurrentTime();
  382. // 更新进度条
  383. int progress = static_cast<int>((currentPosition * 100) / m_duration);
  384. m_progressSlider->setValue(progress);
  385. // 更新时间标签
  386. QString timeText = QString("%1 / %2")
  387. .arg(formatTime(currentPosition))
  388. .arg(formatTime(m_duration));
  389. m_timeLabel->setText(timeText);
  390. }
  391. }
  392. }
  393. void onVideoStreamToggled(bool enabled) {
  394. if (m_player) {
  395. m_player->enableVideoStream(enabled);
  396. Logger::instance().info(QString("视频流%1").arg(enabled ? "启用" : "禁用").toStdString());
  397. }
  398. }
  399. void onAudioStreamToggled(bool enabled) {
  400. if (m_player) {
  401. m_player->enableAudioStream(enabled);
  402. Logger::instance().info(QString("音频流%1").arg(enabled ? "启用" : "禁用").toStdString());
  403. }
  404. }
  405. private:
  406. void setupUI() {
  407. auto* mainLayout = new QHBoxLayout(this);
  408. // 左侧视频区域
  409. auto* leftWidget = new QWidget;
  410. auto* leftLayout = new QVBoxLayout(leftWidget);
  411. // 视频渲染器
  412. m_videoRenderer = new OpenGLVideoWidget;
  413. m_videoRenderer->setMinimumSize(640, 480);
  414. leftLayout->addWidget(m_videoRenderer, 1);
  415. // 控制按钮
  416. auto* controlLayout = new QHBoxLayout;
  417. m_openButton = new QPushButton("打开文件");
  418. m_playButton = new QPushButton("播放");
  419. m_stopButton = new QPushButton("停止");
  420. m_playButton->setEnabled(false);
  421. controlLayout->addWidget(m_openButton);
  422. controlLayout->addWidget(m_playButton);
  423. controlLayout->addWidget(m_stopButton);
  424. controlLayout->addStretch();
  425. leftLayout->addLayout(controlLayout);
  426. // 进度条和时间显示
  427. auto* progressLayout = new QHBoxLayout;
  428. m_progressSlider = new QSlider(Qt::Horizontal);
  429. m_progressSlider->setRange(0, 100);
  430. m_timeLabel = new QLabel("00:00:00 / 00:00:00");
  431. progressLayout->addWidget(m_progressSlider);
  432. progressLayout->addWidget(m_timeLabel);
  433. leftLayout->addLayout(progressLayout);
  434. // 音量控制
  435. auto* volumeLayout = new QHBoxLayout;
  436. volumeLayout->addWidget(new QLabel("音量:"));
  437. m_volumeSlider = new QSlider(Qt::Horizontal);
  438. m_volumeSlider->setRange(0, 100);
  439. m_volumeSlider->setValue(100);
  440. m_volumeSlider->setMaximumWidth(200);
  441. m_volumeLabel = new QLabel("音量: 100%");
  442. volumeLayout->addWidget(m_volumeSlider);
  443. volumeLayout->addWidget(m_volumeLabel);
  444. volumeLayout->addStretch();
  445. leftLayout->addLayout(volumeLayout);
  446. // 流控制
  447. auto* streamControlLayout = new QHBoxLayout;
  448. m_videoStreamCheckBox = new QCheckBox("启用视频");
  449. m_audioStreamCheckBox = new QCheckBox("启用音频");
  450. m_videoStreamCheckBox->setChecked(true);
  451. m_audioStreamCheckBox->setChecked(true);
  452. streamControlLayout->addWidget(m_videoStreamCheckBox);
  453. streamControlLayout->addWidget(m_audioStreamCheckBox);
  454. streamControlLayout->addStretch();
  455. leftLayout->addLayout(streamControlLayout);
  456. mainLayout->addWidget(leftWidget, 2);
  457. // 右侧信息面板
  458. auto* rightWidget = new QWidget;
  459. auto* rightLayout = new QVBoxLayout(rightWidget);
  460. rightWidget->setMinimumWidth(300);
  461. rightWidget->setMaximumWidth(350);
  462. // 状态信息
  463. auto* statusGroup = new QGroupBox("状态信息");
  464. auto* statusLayout = new QVBoxLayout(statusGroup);
  465. m_stateLabel = new QLabel("状态: 空闲");
  466. m_positionLabel = new QLabel("位置: 0s");
  467. statusLayout->addWidget(m_stateLabel);
  468. statusLayout->addWidget(m_positionLabel);
  469. rightLayout->addWidget(statusGroup);
  470. // 媒体信息
  471. auto* infoGroup = new QGroupBox("媒体信息");
  472. auto* infoLayout = new QVBoxLayout(infoGroup);
  473. m_infoLabel = new QLabel("未加载文件");
  474. m_infoLabel->setWordWrap(true);
  475. infoLayout->addWidget(m_infoLabel);
  476. rightLayout->addWidget(infoGroup);
  477. // 统计信息
  478. auto* statsGroup = new QGroupBox("播放统计");
  479. auto* statsLayout = new QVBoxLayout(statsGroup);
  480. // 添加刷新和导出按钮
  481. auto* refreshLayout = new QHBoxLayout();
  482. m_refreshStatsButton = new QPushButton("刷新统计");
  483. m_exportStatsButton = new QPushButton("导出统计");
  484. refreshLayout->addWidget(m_refreshStatsButton);
  485. refreshLayout->addWidget(m_exportStatsButton);
  486. refreshLayout->addStretch();
  487. statsLayout->addLayout(refreshLayout);
  488. m_statsLabel = new QLabel("帧数: 0\n丢帧: 0 (0.0%)\n重复帧: 0\n速度: 1.0x\n\n队列状态:\n视频帧: 0\n音频帧: 0\n数据包: 0\n\n同步状态:\n当前误差: 0.0 ms\n平均误差: 0.0 ms\n最大误差: 0.0 ms\n\n性能:\nCPU: 0.0%\n内存: 0.0 MB\n比特率: 0 kbps");
  489. m_statsLabel->setAlignment(Qt::AlignLeft | Qt::AlignTop);
  490. m_statsLabel->setWordWrap(true);
  491. m_statsLabel->setMinimumHeight(300); // 设置最小高度以显示所有统计信息
  492. statsLayout->addWidget(m_statsLabel);
  493. rightLayout->addWidget(statsGroup);
  494. rightLayout->addStretch();
  495. mainLayout->addWidget(rightWidget);
  496. }
  497. void setupPlayer() {
  498. // 创建同步配置
  499. SyncConfigV2 syncConfig;
  500. syncConfig.strategy = SyncStrategy::VIDEO_MASTER;
  501. syncConfig.syncThresholdMin = 0.040;
  502. syncConfig.syncThresholdMax = 0.100;
  503. syncConfig.frameDropThreshold = 0.100;
  504. syncConfig.frameDupThreshold = 0.100;
  505. syncConfig.audioDiffThreshold = 0.100;
  506. syncConfig.audioDiffAvgCoef = 0.01;
  507. syncConfig.audioDiffAvgCount = 20;
  508. syncConfig.clockSpeedMin = 0.9;
  509. syncConfig.clockSpeedMax = 1.1;
  510. syncConfig.clockSpeedStep = 0.001;
  511. syncConfig.minFramesForSync = 2;
  512. syncConfig.maxFramesForSync = 10;
  513. syncConfig.noSyncThreshold = 10.0;
  514. // 创建播放器实例
  515. m_player = std::make_unique<PlayerCoreV2>(syncConfig);
  516. // 设置视频渲染器
  517. m_player->setOpenGLVideoRenderer(m_videoRenderer);
  518. // 设置初始音量
  519. m_player->setVolume(m_volumeSlider->value() / 100.0);
  520. // 创建事件回调
  521. m_callback = std::make_unique<UIPlayerCallback>(this);
  522. m_player->setEventCallback(m_callback.get());
  523. m_duration = 0;
  524. m_seeking = false;
  525. }
  526. void connectSignals() {
  527. connect(m_openButton, &QPushButton::clicked, this, &PlayerWindow::openFile);
  528. connect(m_playButton, &QPushButton::clicked, this, &PlayerWindow::playPause);
  529. connect(m_stopButton, &QPushButton::clicked, this, &PlayerWindow::stop);
  530. // 进度条信号连接
  531. connect(m_progressSlider, &QSlider::sliderPressed, this, &PlayerWindow::onProgressSliderPressed);
  532. connect(m_progressSlider, &QSlider::sliderReleased, this, &PlayerWindow::onProgressSliderReleased);
  533. connect(m_progressSlider, &QSlider::sliderMoved, this, &PlayerWindow::onProgressSliderMoved);
  534. // 添加进度条点击跳转功能
  535. connect(m_progressSlider, &QSlider::valueChanged, [this](int value) {
  536. if (m_seeking && m_player && m_duration > 0) {
  537. onProgressSliderMoved(value);
  538. }
  539. });
  540. connect(m_volumeSlider, &QSlider::valueChanged, this, &PlayerWindow::onVolumeChanged);
  541. // 连接流控制复选框信号
  542. connect(m_videoStreamCheckBox, &QCheckBox::toggled, this, &PlayerWindow::onVideoStreamToggled);
  543. connect(m_audioStreamCheckBox, &QCheckBox::toggled, this, &PlayerWindow::onAudioStreamToggled);
  544. // 连接统计按钮信号
  545. connect(m_refreshStatsButton, &QPushButton::clicked, this, &PlayerWindow::refreshStats);
  546. connect(m_exportStatsButton, &QPushButton::clicked, this, &PlayerWindow::exportStats);
  547. }
  548. void loadFile(const std::string& filename) {
  549. if (!m_player) return;
  550. Logger::instance().info("Loading file: " + filename);
  551. // 在打开文件前,根据复选框状态设置流启用状态
  552. m_player->enableVideoStream(m_videoStreamCheckBox->isChecked());
  553. m_player->enableAudioStream(m_audioStreamCheckBox->isChecked());
  554. auto result = m_player->openFile(filename);
  555. if (result != ErrorCode::SUCCESS) {
  556. QString error = QString("无法打开文件: %1\n错误代码: %2")
  557. .arg(QString::fromStdString(filename))
  558. .arg(static_cast<int>(result));
  559. QMessageBox::critical(this, "文件打开失败", error);
  560. Logger::instance().error("Failed to open file: " + std::to_string(static_cast<int>(result)));
  561. }
  562. }
  563. private:
  564. // UI组件
  565. OpenGLVideoWidget* m_videoRenderer;
  566. QPushButton* m_openButton;
  567. QPushButton* m_playButton;
  568. QPushButton* m_stopButton;
  569. QPushButton* m_refreshStatsButton;
  570. QPushButton* m_exportStatsButton;
  571. QSlider* m_progressSlider;
  572. QSlider* m_volumeSlider;
  573. QLabel* m_stateLabel;
  574. QLabel* m_positionLabel;
  575. QLabel* m_infoLabel;
  576. QLabel* m_statsLabel;
  577. QLabel* m_volumeLabel;
  578. QLabel* m_timeLabel;
  579. QCheckBox* m_videoStreamCheckBox;
  580. QCheckBox* m_audioStreamCheckBox;
  581. QTimer* m_updateTimer;
  582. // 播放器相关
  583. std::unique_ptr<PlayerCoreV2> m_player;
  584. std::unique_ptr<UIPlayerCallback> m_callback;
  585. int64_t m_duration;
  586. bool m_seeking;
  587. };
  588. int main(int argc, char* argv[]) {
  589. QApplication app(argc, argv);
  590. // 初始化日志系统
  591. Logger::instance().initialize("test.log", LogLevel::DEBUG, false, false);
  592. Logger::instance().info("PlayerCoreV2 Example Started");
  593. try {
  594. // 创建播放器窗口
  595. PlayerWindow window;
  596. window.show();
  597. // 如果有命令行参数,自动加载文件
  598. // if (argc > 1) {
  599. // QTimer::singleShot(500, [&window, argv]() {
  600. // QMetaObject::invokeMethod(&window, "loadFile", Qt::QueuedConnection,
  601. // Q_ARG(std::string, std::string(argv[1])));
  602. // });
  603. // } else {
  604. // // 默认加载测试文件
  605. // QTimer::singleShot(500, [&window]() {
  606. // std::string testFile = "C:/Users/zhuizhu/Videos/2.mp4";
  607. // QMetaObject::invokeMethod(&window, "loadFile", Qt::QueuedConnection,
  608. // Q_ARG(std::string, testFile));
  609. // });
  610. // }
  611. return app.exec();
  612. } catch (const std::exception& e) {
  613. Logger::instance().error("Exception: " + std::string(e.what()));
  614. QMessageBox::critical(nullptr, "错误", QString("发生异常: %1").arg(e.what()));
  615. return 1;
  616. } catch (...) {
  617. Logger::instance().error("Unknown exception occurred");
  618. QMessageBox::critical(nullptr, "错误", "发生未知异常");
  619. return 1;
  620. }
  621. }
  622. #include "test_player_with_ui.moc"