SimplePlayerWindow.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "SimplePlayerWindow.h"
  2. #include <QFileDialog>
  3. #include <QMessageBox>
  4. #include <QDebug>
  5. #include <QFileInfo>
  6. SimplePlayerWindow::SimplePlayerWindow(QWidget *parent)
  7. : QMainWindow(parent)
  8. , m_openGLVideoRenderer(nullptr)
  9. {
  10. qDebug() << "Initializing SimplePlayerWindow...";
  11. try {
  12. // 创建播放器适配器
  13. m_playerAdapter = PlayerAdapterFactory::create(this);
  14. if (!m_playerAdapter) {
  15. throw std::runtime_error("Failed to create PlayerAdapter");
  16. }
  17. qDebug() << "PlayerAdapter created successfully";
  18. setupUI();
  19. connectSignals();
  20. updateUI();
  21. // 设置OpenGL渲染器
  22. if (m_openGLVideoRenderer && m_playerAdapter) {
  23. qDebug() << "Setting OpenGL video renderer...";
  24. m_playerAdapter->setOpenGLVideoRenderer(m_openGLVideoRenderer);
  25. } else {
  26. qWarning() << "No video renderer available";
  27. }
  28. setWindowTitle("Simple Media Player (OpenGL)");
  29. resize(800, 600);
  30. qDebug() << "SimplePlayerWindow initialized successfully";
  31. } catch (const std::exception& e) {
  32. qCritical() << "Exception during SimplePlayerWindow initialization:" << e.what();
  33. QMessageBox::critical(this, "Initialization Error",
  34. QString("Failed to initialize player window: %1").arg(e.what()));
  35. throw;
  36. } catch (...) {
  37. qCritical() << "Unknown exception during SimplePlayerWindow initialization";
  38. QMessageBox::critical(this, "Initialization Error",
  39. "Unknown error during player window initialization");
  40. throw;
  41. }
  42. }
  43. SimplePlayerWindow::~SimplePlayerWindow()
  44. {
  45. if (m_playerAdapter) {
  46. m_playerAdapter->stop();
  47. }
  48. }
  49. void SimplePlayerWindow::openFile()
  50. {
  51. qDebug() << "Opening file dialog...";
  52. QString filename = QFileDialog::getOpenFileName(
  53. this,
  54. "Open Media File",
  55. "",
  56. "Media Files (*.mp4 *.avi *.mkv *.mov *.wmv *.flv *.webm *.mp3 *.wav *.aac *.flac *.ogg);;All Files (*.*)"
  57. );
  58. if (!filename.isEmpty()) {
  59. qDebug() << "Selected file:" << filename;
  60. // 检查文件是否存在
  61. QFileInfo fileInfo(filename);
  62. if (!fileInfo.exists()) {
  63. QMessageBox::critical(this, "Error", "File does not exist: " + filename);
  64. return;
  65. }
  66. if (!fileInfo.isReadable()) {
  67. QMessageBox::critical(this, "Error", "File is not readable: " + filename);
  68. return;
  69. }
  70. // 显示加载状态
  71. m_stateLabel->setText("State: Loading...");
  72. m_fileLabel->setText("Loading: " + fileInfo.fileName());
  73. // 尝试打开文件
  74. av::ErrorCode result = m_playerAdapter->openFile(filename);
  75. if (result != av::ErrorCode::SUCCESS) {
  76. QString errorMsg = "Failed to open file: " + filename;
  77. // 根据错误代码提供更具体的错误信息
  78. switch (result) {
  79. case av::ErrorCode::FILE_OPEN_FAILED:
  80. errorMsg = "Could not open file. Please check if the file is corrupted or in an unsupported format.";
  81. break;
  82. case av::ErrorCode::CODEC_OPEN_FAILED:
  83. errorMsg = "Could not initialize codecs. The file format may not be supported.";
  84. break;
  85. case av::ErrorCode::NOT_INITIALIZED:
  86. errorMsg = "Player not properly initialized. Please restart the application.";
  87. break;
  88. case av::ErrorCode::INVALID_STATE:
  89. errorMsg = "Player is in an invalid state. Please try again.";
  90. break;
  91. default:
  92. errorMsg = "Unknown error occurred while opening the file.";
  93. break;
  94. }
  95. QMessageBox::critical(this, "Error", errorMsg);
  96. qCritical() << "Failed to open file:" << filename << "Error code:" << static_cast<int>(result);
  97. // 重置状态
  98. m_stateLabel->setText("State: Idle");
  99. m_fileLabel->setText("No file loaded");
  100. } else {
  101. m_fileLabel->setText("File: " + fileInfo.fileName());
  102. qDebug() << "File opened successfully:" << filename;
  103. }
  104. } else {
  105. qDebug() << "No file selected";
  106. }
  107. }
  108. void SimplePlayerWindow::playPause()
  109. {
  110. if (!m_playerAdapter) {
  111. qWarning() << "PlayerAdapter is null";
  112. return;
  113. }
  114. PlayerState state = m_playerAdapter->getState();
  115. qDebug() << "Current player state:" << static_cast<int>(state);
  116. if (state == PlayerState::Playing) {
  117. qDebug() << "Pausing playback...";
  118. av::ErrorCode result = m_playerAdapter->pause();
  119. if (result != av::ErrorCode::SUCCESS) {
  120. qWarning() << "Failed to pause playback, error code:" << static_cast<int>(result);
  121. }
  122. } else if (state == PlayerState::Paused || state == PlayerState::Stopped) {
  123. qDebug() << "Starting playback...";
  124. av::ErrorCode result = m_playerAdapter->play();
  125. if (result != av::ErrorCode::SUCCESS) {
  126. qWarning() << "Failed to start playback, error code:" << static_cast<int>(result);
  127. QMessageBox::warning(this, "Playback Error",
  128. "Failed to start playback. Please check if a file is loaded.");
  129. }
  130. } else if (state == PlayerState::Idle) {
  131. QMessageBox::information(this, "Information",
  132. "Please open a media file first.");
  133. } else {
  134. qDebug() << "Player is in state:" << static_cast<int>(state) << "- cannot play/pause";
  135. }
  136. }
  137. void SimplePlayerWindow::stop()
  138. {
  139. m_playerAdapter->stop();
  140. }
  141. void SimplePlayerWindow::seek()
  142. {
  143. if (m_seeking) {
  144. qint64 duration = m_playerAdapter->getDuration();
  145. qint64 position = (duration * m_positionSlider->value()) / 1000;
  146. m_playerAdapter->seek(position);
  147. }
  148. }
  149. void SimplePlayerWindow::setVolume()
  150. {
  151. double volume = m_volumeSlider->value() / 100.0;
  152. m_playerAdapter->setVolume(volume);
  153. m_volumeLabel->setText(QString("Volume: %1%").arg(m_volumeSlider->value()));
  154. }
  155. void SimplePlayerWindow::toggleMute()
  156. {
  157. bool muted = m_playerAdapter->isMuted();
  158. m_playerAdapter->setMuted(!muted);
  159. }
  160. void SimplePlayerWindow::setPlaybackSpeed()
  161. {
  162. double speed = m_speedSlider->value() / 100.0;
  163. m_playerAdapter->setPlaybackSpeed(speed);
  164. m_speedLabel->setText(QString("Speed: %1x").arg(speed, 0, 'f', 2));
  165. }
  166. void SimplePlayerWindow::onStateChanged(PlayerState state)
  167. {
  168. QString stateText;
  169. switch (state) {
  170. case PlayerState::Idle:
  171. stateText = "Idle";
  172. break;
  173. // case PlayerState::Loading:
  174. // stateText = "Loading";
  175. // break;
  176. case PlayerState::Playing:
  177. stateText = "Playing";
  178. break;
  179. case PlayerState::Paused:
  180. stateText = "Paused";
  181. break;
  182. case PlayerState::Stopped:
  183. stateText = "Stopped";
  184. break;
  185. case PlayerState::Error:
  186. stateText = "Error";
  187. break;
  188. }
  189. m_stateLabel->setText("State: " + stateText);
  190. updateUI();
  191. }
  192. void SimplePlayerWindow::onMediaInfoChanged(const MediaInfo& info)
  193. {
  194. QString infoText = QString("Duration: %1 | Video: %2x%3 | Audio: %4 Hz")
  195. .arg(formatTime(info.duration))
  196. .arg(info.width)
  197. .arg(info.height)
  198. .arg(info.audioSampleRate);
  199. m_infoLabel->setText(infoText);
  200. }
  201. void SimplePlayerWindow::onPositionChanged(qint64 position)
  202. {
  203. if (!m_seeking) {
  204. qint64 duration = m_playerAdapter->getDuration();
  205. if (duration > 0) {
  206. int sliderValue = (position * 1000) / duration;
  207. m_positionSlider->setValue(sliderValue);
  208. }
  209. QString timeText = QString("%1 / %2")
  210. .arg(formatTime(position))
  211. .arg(formatTime(duration));
  212. m_timeLabel->setText(timeText);
  213. }
  214. }
  215. void SimplePlayerWindow::onVolumeChanged(double volume)
  216. {
  217. int volumePercent = static_cast<int>(volume * 100);
  218. m_volumeSlider->setValue(volumePercent);
  219. m_volumeLabel->setText(QString("Volume: %1%").arg(volumePercent));
  220. }
  221. void SimplePlayerWindow::onMutedChanged(bool muted)
  222. {
  223. m_muteButton->setText(muted ? "Unmute" : "Mute");
  224. }
  225. void SimplePlayerWindow::onPlaybackSpeedChanged(double speed)
  226. {
  227. int speedPercent = static_cast<int>(speed * 100);
  228. m_speedSlider->setValue(speedPercent);
  229. m_speedLabel->setText(QString("Speed: %1x").arg(speed, 0, 'f', 2));
  230. }
  231. void SimplePlayerWindow::onErrorOccurred(const QString& error)
  232. {
  233. QMessageBox::critical(this, "Player Error", error);
  234. qDebug() << "Player error:" << error;
  235. }
  236. void SimplePlayerWindow::onStatsUpdated(const PlaybackStats& stats)
  237. {
  238. QString statsText = QString("Packets: %1 | Video Frames: %2 | Audio Frames: %3")
  239. .arg(stats.queuedPackets)
  240. .arg(stats.queuedVideoFrames)
  241. .arg(stats.queuedAudioFrames);
  242. m_statsLabel->setText(statsText);
  243. }
  244. void SimplePlayerWindow::setupUI()
  245. {
  246. auto* centralWidget = new QWidget(this);
  247. setCentralWidget(centralWidget);
  248. auto* mainLayout = new QVBoxLayout(centralWidget);
  249. // 文件信息
  250. m_fileLabel = new QLabel("No file loaded");
  251. m_stateLabel = new QLabel("State: Idle");
  252. m_infoLabel = new QLabel("Media info will appear here");
  253. mainLayout->addWidget(m_fileLabel);
  254. mainLayout->addWidget(m_stateLabel);
  255. mainLayout->addWidget(m_infoLabel);
  256. // 视频渲染器 - 使用OpenGL渲染器
  257. m_openGLVideoRenderer = new OpenGLVideoRenderer(this);
  258. m_openGLVideoRenderer->setMinimumSize(640, 480);
  259. m_openGLVideoRenderer->setStyleSheet("border: 1px solid gray;");
  260. mainLayout->addWidget(m_openGLVideoRenderer);
  261. // 控制按钮
  262. auto* buttonLayout = new QHBoxLayout();
  263. m_openButton = new QPushButton("Open File");
  264. m_playPauseButton = new QPushButton("Play");
  265. m_stopButton = new QPushButton("Stop");
  266. buttonLayout->addWidget(m_openButton);
  267. buttonLayout->addWidget(m_playPauseButton);
  268. buttonLayout->addWidget(m_stopButton);
  269. buttonLayout->addStretch();
  270. mainLayout->addLayout(buttonLayout);
  271. // 进度控制
  272. auto* progressLayout = new QHBoxLayout();
  273. m_timeLabel = new QLabel("00:00 / 00:00");
  274. m_positionSlider = new QSlider(Qt::Horizontal);
  275. m_positionSlider->setRange(0, 1000);
  276. progressLayout->addWidget(m_timeLabel);
  277. progressLayout->addWidget(m_positionSlider);
  278. mainLayout->addLayout(progressLayout);
  279. // 音量控制
  280. auto* volumeLayout = new QHBoxLayout();
  281. m_volumeLabel = new QLabel("Volume: 100%");
  282. m_volumeSlider = new QSlider(Qt::Horizontal);
  283. m_volumeSlider->setRange(0, 100);
  284. m_volumeSlider->setValue(100);
  285. m_muteButton = new QPushButton("Mute");
  286. volumeLayout->addWidget(m_volumeLabel);
  287. volumeLayout->addWidget(m_volumeSlider);
  288. volumeLayout->addWidget(m_muteButton);
  289. mainLayout->addLayout(volumeLayout);
  290. // 播放速度控制
  291. auto* speedLayout = new QHBoxLayout();
  292. m_speedLabel = new QLabel("Speed: 1.00x");
  293. m_speedSlider = new QSlider(Qt::Horizontal);
  294. m_speedSlider->setRange(25, 400); // 0.25x to 4.0x
  295. m_speedSlider->setValue(100);
  296. speedLayout->addWidget(m_speedLabel);
  297. speedLayout->addWidget(m_speedSlider);
  298. mainLayout->addLayout(speedLayout);
  299. // 统计信息
  300. m_statsLabel = new QLabel("Statistics will appear here");
  301. mainLayout->addWidget(m_statsLabel);
  302. mainLayout->addStretch();
  303. }
  304. void SimplePlayerWindow::connectSignals()
  305. {
  306. // UI信号
  307. connect(m_openButton, &QPushButton::clicked, this, &SimplePlayerWindow::openFile);
  308. connect(m_playPauseButton, &QPushButton::clicked, this, &SimplePlayerWindow::playPause);
  309. connect(m_stopButton, &QPushButton::clicked, this, &SimplePlayerWindow::stop);
  310. connect(m_positionSlider, &QSlider::sliderPressed, [this]() { m_seeking = true; });
  311. connect(m_positionSlider, &QSlider::sliderReleased, [this]() {
  312. seek();
  313. m_seeking = false;
  314. });
  315. connect(m_volumeSlider, &QSlider::valueChanged, this, &SimplePlayerWindow::setVolume);
  316. connect(m_muteButton, &QPushButton::clicked, this, &SimplePlayerWindow::toggleMute);
  317. connect(m_speedSlider, &QSlider::valueChanged, this, &SimplePlayerWindow::setPlaybackSpeed);
  318. // 播放器信号
  319. connect(m_playerAdapter.get(),
  320. &PlayerAdapter::stateChanged,
  321. this,
  322. &SimplePlayerWindow::onStateChanged);
  323. connect(m_playerAdapter.get(),
  324. &PlayerAdapter::mediaInfoChanged,
  325. this,
  326. &SimplePlayerWindow::onMediaInfoChanged);
  327. connect(m_playerAdapter.get(),
  328. &PlayerAdapter::positionChanged,
  329. this,
  330. &SimplePlayerWindow::onPositionChanged);
  331. connect(m_playerAdapter.get(),
  332. &PlayerAdapter::volumeChanged,
  333. this,
  334. &SimplePlayerWindow::onVolumeChanged);
  335. connect(m_playerAdapter.get(),
  336. &PlayerAdapter::mutedChanged,
  337. this,
  338. &SimplePlayerWindow::onMutedChanged);
  339. connect(m_playerAdapter.get(),
  340. &PlayerAdapter::playbackSpeedChanged,
  341. this,
  342. &SimplePlayerWindow::onPlaybackSpeedChanged);
  343. connect(m_playerAdapter.get(),
  344. &PlayerAdapter::errorOccurred,
  345. this,
  346. &SimplePlayerWindow::onErrorOccurred);
  347. connect(m_playerAdapter.get(),
  348. &PlayerAdapter::statsUpdated,
  349. this,
  350. &SimplePlayerWindow::onStatsUpdated);
  351. // 渲染器相关信号
  352. connect(m_playerAdapter.get(),
  353. &PlayerAdapter::rendererTypeChanged,
  354. this,
  355. &SimplePlayerWindow::onRendererTypeChanged);
  356. connect(m_playerAdapter.get(),
  357. &PlayerAdapter::openGLRendererInitialized,
  358. this,
  359. &SimplePlayerWindow::onOpenGLRendererInitialized);
  360. }
  361. void SimplePlayerWindow::updateUI()
  362. {
  363. PlayerState state = m_playerAdapter->getState();
  364. // 更新播放/暂停按钮
  365. if (state == PlayerState::Playing) {
  366. m_playPauseButton->setText("Pause");
  367. } else {
  368. m_playPauseButton->setText("Play");
  369. }
  370. // 更新按钮可用状态
  371. bool hasMedia = (state != PlayerState::Idle);
  372. m_playPauseButton->setEnabled(hasMedia);
  373. m_stopButton->setEnabled(hasMedia);
  374. m_positionSlider->setEnabled(hasMedia);
  375. }
  376. void SimplePlayerWindow::onRendererTypeChanged(const QString& type)
  377. {
  378. qDebug() << "Renderer type changed to:" << type;
  379. // 更新窗口标题显示当前渲染器类型
  380. QString title = QString("Simple Media Player (%1)").arg(type);
  381. setWindowTitle(title);
  382. }
  383. void SimplePlayerWindow::onOpenGLRendererInitialized()
  384. {
  385. qDebug() << "OpenGL renderer initialized successfully";
  386. // 可以在这里添加OpenGL渲染器初始化完成后的逻辑
  387. // 比如显示一些OpenGL特定的信息
  388. }
  389. QString SimplePlayerWindow::formatTime(qint64 microseconds)
  390. {
  391. qint64 seconds = microseconds / 1000000;
  392. qint64 minutes = seconds / 60;
  393. seconds %= 60;
  394. return QString("%1:%2").arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
  395. }