You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

VideoWidget.cpp 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "VideoWidget.h"
  2. VideoWidget::VideoWidget(QWidget* parent)
  3. : QWidget(parent)
  4. , m_bIsWindMax(false)
  5. , m_iWgtWidth(800)
  6. , m_iWgtHeight(600)
  7. , m_iMaxWidth(1920)
  8. , m_iMaxHeight(1080)
  9. , m_iCurWidth(0) // 当前宽度
  10. , m_iCurHeight(0) // 当前高度
  11. , m_bMoving(false)
  12. , m_bEmbedded(false)
  13. , m_pFfmpegDecodeNew(nullptr)
  14. , m_uiIndex(0)
  15. , m_uiDataSize(0)
  16. , m_bIsPlay(false)
  17. , m_iTimerId(-1)
  18. , m_uiParseStatus(0)
  19. , m_pPixmapItem(nullptr)
  20. {
  21. ui.setupUi(this);
  22. setWindowFlags(Qt::FramelessWindowHint);
  23. connect(ui.pb_popup, &QPushButton::clicked, this, &VideoWidget::popupWidget);
  24. connect(ui.pb_minimize, &QPushButton::clicked, this, &VideoWidget::showMinimized);
  25. connect(ui.pb_resize, &QPushButton::clicked, this, &VideoWidget::resizeWidget);
  26. connect(ui.pb_hide, &QPushButton::clicked, this, &VideoWidget::hideWidget);
  27. connect(ui.pb_videoPlayer, &QPushButton::clicked, this, &VideoWidget::videoPlayer);
  28. connect(ui.cb_videoSpeed, &QComboBox::currentTextChanged, this, &VideoWidget::selectVideoSpeed);
  29. m_wgtRect.m_iMaxWidth = 1920;
  30. m_wgtRect.m_iMaxHeight = 1080;
  31. m_wgtRect.m_iWgtWidth = 800;
  32. m_wgtRect.m_iWgtHeight = 600;
  33. // FFmpeg播放器
  34. ui.gv_videoPlayer->setScene(new QGraphicsScene(this));
  35. ui.gv_videoPlayer->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 关闭水平滚动条
  36. ui.gv_videoPlayer->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // 关闭垂直滚动条
  37. m_pFfmpegDecodeNew = new FfmpegDecodeNew();
  38. // 进度条
  39. ui.hs_videoPlayer->setMaximum(100);
  40. ui.hs_videoPlayer->setValue(0);
  41. connect(ui.hs_videoPlayer, &QSlider::sliderPressed, this,
  42. &VideoWidget::videoSilderPressed); // 按下
  43. connect(ui.hs_videoPlayer, &QSlider::sliderReleased, this,
  44. &VideoWidget::videoSilderReleased); // 释放
  45. }
  46. VideoWidget::~VideoWidget()
  47. {
  48. if (m_pFfmpegDecodeNew) {
  49. m_pFfmpegDecodeNew->stop();
  50. m_pFfmpegDecodeNew->quit();
  51. m_pFfmpegDecodeNew->wait();
  52. delete m_pFfmpegDecodeNew;
  53. m_pFfmpegDecodeNew = nullptr;
  54. }
  55. }
  56. void VideoWidget::setParam(QString title, int count)
  57. {
  58. m_strTitle = title;
  59. m_iCount = count;
  60. ui.lb_videoTitle->setText(title);
  61. }
  62. void VideoWidget::resizeEvent(QResizeEvent* event)
  63. {
  64. QSize size = event->size();
  65. m_iCurWidth = size.width();
  66. m_iCurHeight = size.height();
  67. resizeSubControl(m_iCurWidth, m_iCurHeight);
  68. }
  69. void VideoWidget::mouseMoveEvent(QMouseEvent* event)
  70. {
  71. if (m_bEmbedded == true) {
  72. return;
  73. }
  74. if (event->buttons().testFlag(Qt::LeftButton) && m_bMoving) {
  75. this->setCursor(Qt::OpenHandCursor);
  76. this->move(this->pos() + (event->globalPos() - m_lastMousePos));
  77. m_lastMousePos = event->globalPos();
  78. }
  79. }
  80. void VideoWidget::mouseReleaseEvent(QMouseEvent* event)
  81. {
  82. if (m_bEmbedded == true) {
  83. return;
  84. }
  85. if (event->button() == Qt::LeftButton) {
  86. m_bMoving = false;
  87. this->setCursor(Qt::ArrowCursor);
  88. }
  89. }
  90. void VideoWidget::mousePressEvent(QMouseEvent* event)
  91. {
  92. if (m_bEmbedded == true) {
  93. return;
  94. }
  95. if (event->button() == Qt::LeftButton) {
  96. m_bMoving = true;
  97. m_lastMousePos = event->globalPos();
  98. }
  99. }
  100. void VideoWidget::resizeWidget()
  101. {
  102. if (false == m_bIsWindMax) {
  103. // 窗口最大化
  104. this->setGeometry(0, 0, m_iMaxWidth, m_iMaxHeight);
  105. // resizeSubControl(m_iMaxWidth, m_iMaxHeight);
  106. } else if (true == m_bIsWindMax) {
  107. // 窗口正常化
  108. int iWgtPosX = (m_iMaxWidth - m_iWgtWidth) / 2;
  109. int iWgtPosY = (m_iMaxHeight - m_iWgtHeight) / 2;
  110. this->setGeometry(iWgtPosX, iWgtPosY, m_iWgtWidth, m_iWgtHeight);
  111. // resizeSubControl(m_iWgtWidth,m_iWgtHeight);
  112. }
  113. m_bIsWindMax = !m_bIsWindMax;
  114. }
  115. void VideoWidget::resizeSubControl(int width, int height)
  116. {
  117. // 背景窗口
  118. QRect rect = ui.wgt_video->geometry();
  119. ui.wgt_video->setGeometry(rect.x(), rect.y(), width, height);
  120. // 菜单窗口
  121. QRect rectMenu = ui.wgt_menu->geometry();
  122. ui.wgt_menu->setGeometry(rectMenu.x(), rectMenu.y(), width, rectMenu.height());
  123. rect = ui.wgt_resize->geometry();
  124. ui.wgt_resize->setGeometry(width - 1 - rect.width(), rect.y(), rect.width(), rect.height());
  125. rect = ui.pb_popup->geometry();
  126. ui.pb_popup->setGeometry(width - 100 - rect.width(), rect.y(), rect.width(), rect.height());
  127. // 控制窗口
  128. QRect rectCtrl = ui.wgt_control->geometry();
  129. ui.wgt_control->setGeometry(rectCtrl.x(), height - rectCtrl.height(), width, rectCtrl.height());
  130. // 显示窗口
  131. // FFmpeg播放器
  132. rect = ui.gv_videoPlayer->geometry();
  133. ui.gv_videoPlayer->setGeometry(rect.x(), rect.y(), width,
  134. height - rectMenu.height() - rectCtrl.height());
  135. fitInView();
  136. }
  137. void VideoWidget::fitInView()
  138. {
  139. if (m_pPixmapItem) {
  140. ui.gv_videoPlayer->fitInView(m_pPixmapItem, Qt::KeepAspectRatio);
  141. }
  142. }
  143. void VideoWidget::popupWidget()
  144. {
  145. emit popupVideoWidget(m_strTitle, m_iCount, m_bEmbedded);
  146. }
  147. void VideoWidget::setWidgetEmbedded(bool embedded)
  148. {
  149. m_bEmbedded = embedded;
  150. if (embedded == true) {
  151. ui.pb_minimize->setEnabled(false);
  152. ui.pb_resize->setEnabled(false);
  153. ui.pb_popup->setText("弹出");
  154. } else if (embedded == false) {
  155. ui.pb_minimize->setEnabled(true);
  156. ui.pb_resize->setEnabled(true);
  157. ui.pb_popup->setText("内嵌");
  158. }
  159. }
  160. void VideoWidget::setVideoData(QByteArray array)
  161. {
  162. m_videoArray = array;
  163. }
  164. void VideoWidget::videoPlayer()
  165. {
  166. if (m_uiParseStatus == 0) { // 没有解析任务
  167. return;
  168. }
  169. if (m_bIsPlay == false) {
  170. m_iTimerId = startTimer(20);
  171. ui.pb_videoPlayer->setText("停止");
  172. m_bIsPlay = true; // 播放
  173. } else if (m_bIsPlay == true) {
  174. killTimer(m_iTimerId);
  175. ui.pb_videoPlayer->setText("播放");
  176. m_bIsPlay = false; // 停止
  177. }
  178. emit startPlayer(m_bIsPlay);
  179. }
  180. void VideoWidget::timerEvent(QTimerEvent* event)
  181. {
  182. if (m_pFfmpegDecodeNew->getImage(m_image)) {
  183. m_pixmap = QPixmap::fromImage(m_image);
  184. m_pPixmapItem->setPixmap(m_pixmap);
  185. ui.gv_videoPlayer->fitInView(m_pPixmapItem, Qt::KeepAspectRatio);
  186. }
  187. }
  188. void VideoWidget::playedCount(unsigned int num, unsigned int count)
  189. {
  190. int value = (count * 1.0) / (num * 1.0) * 100;
  191. // 用于显示播放进度
  192. ui.hs_videoPlayer->setValue(value);
  193. if (ui.hs_videoPlayer->value() == 100) {
  194. ui.pb_videoPlayer->setText("播放");
  195. ui.hs_videoPlayer->setValue(0);
  196. }
  197. }
  198. void VideoWidget::stopPlay()
  199. {
  200. m_bIsPlay = true;
  201. videoPlayer();
  202. // 清空缓存
  203. m_pFfmpegDecodeNew->getConstBuffer()->clear();
  204. }
  205. void VideoWidget::setParseStatus(unsigned int status)
  206. {
  207. m_uiParseStatus = status;
  208. if (m_uiParseStatus != 0) {
  209. m_pPixmapItem = new QGraphicsPixmapItem();
  210. ui.gv_videoPlayer->scene()->addItem(m_pPixmapItem);
  211. }
  212. if (m_uiParseStatus == 1) { // 离线解析
  213. ui.hs_videoPlayer->show();
  214. ui.lb_videoSpeed->show();
  215. ui.cb_videoSpeed->show();
  216. } else if (m_uiParseStatus == 2) { // 在线解析
  217. ui.hs_videoPlayer->hide();
  218. ui.lb_videoSpeed->hide();
  219. ui.cb_videoSpeed->hide();
  220. }
  221. }
  222. void VideoWidget::hideWidget()
  223. {
  224. emit hideVideoWidget(m_iCount);
  225. }
  226. // 拖动进度条
  227. void VideoWidget::videoSilderPressed()
  228. {
  229. // 先暂停播放,然后拖动进度条
  230. killTimer(m_iTimerId);
  231. ui.pb_videoPlayer->setText("播放");
  232. m_bIsPlay = false; // 停止
  233. emit startPlayer(m_bIsPlay);
  234. }
  235. // 释放进度条
  236. void VideoWidget::videoSilderReleased()
  237. {
  238. // 从指定位置开始回放
  239. int iPos = ui.hs_videoPlayer->value();
  240. emit setPlayPos(iPos);
  241. m_iTimerId = startTimer(20);
  242. ui.pb_videoPlayer->setText("停止");
  243. m_bIsPlay = true; // 播放
  244. emit startPlayer(m_bIsPlay);
  245. }
  246. void VideoWidget::clearWidget()
  247. {
  248. if (m_pPixmapItem) {
  249. ui.gv_videoPlayer->scene()->removeItem(m_pPixmapItem);
  250. delete m_pPixmapItem;
  251. m_pPixmapItem = nullptr;
  252. }
  253. ui.pb_videoPlayer->setText("播放");
  254. }
  255. void VideoWidget::selectVideoSpeed()
  256. {
  257. QString strSpeed = ui.cb_videoSpeed->currentText();
  258. emit videoSpeed(strSpeed);
  259. }