1 /*!
2 * @file videoplayer.cpp
3 * Copyright (c) 2018
4 * @brief des
5 * detailed des
6 *
7 * @date 2018
8 * @author lee
9 */
10 #include "videoplayer.h"
11 #include "ui_videoplayer.h"
12 #include "frmmessagebox.h"
13 #include "mediahome.h"
14 #include "audiosink.h"
15
16 #include <QMutex>
17 #include <QMutexLocker>
18 #include <QDebug>
19 #include <QMenu>
20 #include <QAction>
21 #include <QTextCodec>
22
23
24 VideoPlayer *VideoPlayer::self = NULL;
25
26
27 /**************************************
28 Function:
29 Description:
30 ***************************************/
Instance(QWidget * parent)31 VideoPlayer *VideoPlayer::Instance(QWidget *parent)
32 {
33 if (self == NULL) {
34 QMutex mutex;
35 QMutexLocker locker(&mutex);
36 if (self == NULL) {
37 self = new VideoPlayer(parent);
38 }
39 }
40 return self;
41 }
42
43
44 /**************************************
45 Function:
46 Description:
47 ***************************************/
VideoPlayer(QWidget * parent)48 VideoPlayer::VideoPlayer(QWidget *parent) :
49 QWidget(parent),
50 ui(new Ui::VideoPlayer),
51 ap(NULL),
52 m_totalDuration(0),
53 m_curDuration(0),
54 m_bPlayState(STOPPED),
55 m_bVideoListOpen(false),
56 m_curVideoFilePath(""),
57 m_curVideoFileName(""),
58 m_curVideoFileCount(-1),
59 m_bSearchDialogOpen(false),
60 m_pSearchDialog(NULL),
61 m_subtitleNow(1),
62 m_audioNow(1)
63 {
64 qDebug()<<"VideoPlayer init";
65
66 pStaticVideoPlayer=this;
67
68 ui->setupUi(this);
69
70 m_commonFunction = new CommonFunction ;
71
72 ui->UnFullscreenButton->setVisible(false);
73
74 //m_pVideoPlayList = VideoPlayList::Instance(this);
75 //m_pVideoPlayList->move(1024,50);
76 //connect(this, SIGNAL(hidePlayList()), m_pVideoPlayList, SLOT(hidePlayList()));
77 //connect(this,SIGNAL(showPlayList()),m_pVideoPlayList,SLOT(showPlayList()));
78
79 //connect(m_pVideoPlayList,SIGNAL(notifyVideoPlayerToPlay(QList<QFileInfo>,int)),\
80 // this,SLOT(on_notifyVideoPlayerToPlay(QList<QFileInfo>,int)));
81 //connect(m_pVideoPlayList,SIGNAL(notifyVideoPlayerToPlay(QList<QFileInfo>)),\
82 // this,SLOT(on_notifyVideoPlayerListChanged(QList<QFileInfo>)));
83
84 progressSliderTimer = new QTimer(this);
85 connect(progressSliderTimer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
86
87 connect(ui->progressSlider,SIGNAL(sliderMoved(int)),this,SLOT(onProgressSliderMoved(int)));
88 connect(ui->progressSlider,SIGNAL(sliderReleased()),this,SLOT(onProgressSliderReleased()));
89
90 connect(ui->return_btn,SIGNAL(clicked()),this,SLOT(on_return_btn_clicked()));
91
92 connect(ui->nextVideoButton,SIGNAL(clicked()),this,SLOT(on_nextVideoButton_clicked()));
93
94 connect(this, SIGNAL(msgSignal(int,void*,void*,int)),
95 this, SLOT(doMsgCmd(int,void*,void*,int)));
96
97 setAttribute(Qt::WA_TranslucentBackground, true);
98
99 setStyleSheet("background-color: transparent");
100
101 ui->playListButton->hide();
102 ui->nextVideoButton->hide();
103 ui->preVideoButton->hide();
104 ui->subtitleSelectButton->hide();
105 }
106
107
108 /**************************************
109 Function:
110 Description:
111 ***************************************/
~VideoPlayer()112 VideoPlayer::~VideoPlayer()
113 {
114 delete ui;
115 self = NULL;
116 }
117
118 /**************************************
119 Function:
120 Description:
121 ***************************************/
initAutPlayer()122 void VideoPlayer::initAutPlayer()
123 {
124 if(m_curVideoFilePath.isEmpty())
125 {
126 qDebug()<<"file path is emtpy";
127 }
128 if(ap != NULL){
129 delete ap;
130 ap = NULL;
131 }
132 ap = new AUTPlayer();
133 if(ap == NULL){
134 qDebug()<<"new AutPlayer error";
135 return ;
136 }
137 m_bPlayState = STOPPED;
138
139 int ret = ap->setUrl((char*)m_curVideoFilePath.toLatin1().data());
140 if(ret != 0){
141 qDebug()<<"this video set url errror";
142 return;
143 }
144 ui->videoNameLabel->setText(m_curVideoFileName);
145
146 ap->setUserCb(autVideoCb_func, this);
147
148 m_totalDuration = ap->getDuration() / 1000;
149 ui->progressSlider->setRange(0,m_totalDuration * 1000);
150 m_pretime=0;
151 m_curDuration=0;
152
153 ui->totalTimeLabel->setText(getTimeStr(m_totalDuration,m_totalDuration));
154 ui->curTimeLabel->setText(getTimeStr(m_curDuration,m_totalDuration));
155 ret = ap->play();
156 if(ret != 0)
157 qDebug()<<"play bug";
158
159 ui->playButton->setIcon(QIcon(":/images/playerIcons/Pause.png"));
160 ui->playButton->setIconSize(QSize(50,50));
161 m_bPlayState = PLAYING;
162
163 progressSliderTimer->start(1000);
164 }
165
166 /**************************************
167 Function:
168 Description:
169 ***************************************/
startPlay()170 void VideoPlayer::startPlay()
171 {
172 bool isExist = m_commonFunction->fileExistOrNot(m_curVideoFilePath);
173 if(!isExist){
174 qDebug()<<"file exist";
175 frmMessageBox *msg = new frmMessageBox(this);
176 msg->SetMessage(QString("文件不存在!"),0);
177 msg->exec();
178 return;
179 }
180 if(!m_commonFunction->video_support_or_not(this,m_curVideoFilePath)){
181 qDebug()<<"format not support";
182 return ;
183 }
184 initAutPlayer();
185 }
186
187 /**************************************
188 Function:
189 Description:
190 ***************************************/
onProgressSliderMoved(int value)191 void VideoPlayer::onProgressSliderMoved(int value)
192 {
193 qDebug()<<"slider is moved"<<value;
194 progressSliderTimer->stop();
195 if (!ui->progressSlider->isSliderDown()) {
196 ui->progressSlider->setValue(value);
197 }
198 ui->progressSlider->setValue(value);
199 updateDurationInfo(value / 1000);
200 progressSliderTimer->start(1000);
201 }
202
203 /**************************************
204 Function:
205 Description:
206 ***************************************/
onProgressSliderReleased()207 void VideoPlayer::onProgressSliderReleased()
208 {
209 qDebug()<<"slider is released";
210 if(m_bPlayState == PAUSEED)
211 on_playButton_clicked();
212 progressSliderTimer->stop();
213 m_bPlayState = SEEKING;
214 if(ap != NULL){
215 ap->seekto(ui->progressSlider->value() / 1000);
216 }
217 progressSliderTimer->start(1000);
218
219 m_bPlayState = PLAYING;
220
221 }
222
223
224 /**************************************
225 Function:
226 Description:
227 ***************************************/
timerUpdate()228 void VideoPlayer::timerUpdate()
229 {
230 if (ap == NULL)
231 return ;
232 m_curDuration = ap->getCurrentPosition();
233
234 //qDebug()<<"timerUpdate "<<m_curDuration;
235
236 ui->progressSlider->setValue(m_curDuration);
237 updateDurationInfo(m_curDuration / 1000);
238 }
239
240
241
242 /**************************************
243 Function:
244 Description:
245 ***************************************/
updateDurationInfo(qint64 currentInfo)246 void VideoPlayer::updateDurationInfo(qint64 currentInfo)
247 {
248
249 QString tStr;
250
251 if (ap == NULL)
252 return ;
253
254
255 if(currentInfo >= m_totalDuration)
256 {
257 currentInfo = 0;
258 m_bPlayState = STOPPED;
259 ui->playButton->setIcon(QIcon(":/images/playerIcons/Play.png"));
260 ui->playButton->setIconSize(QSize(50,50));
261 progressSliderTimer->stop();
262
263 }
264
265 tStr=getTimeStr(currentInfo,m_totalDuration);
266 ui->curTimeLabel->setText(tStr);
267
268 //qDebug()<<"update the time label" << tStr << " " << tStr.compare("") << " " <<tStr.size();
269 }
270
271 /**************************************
272 Function:
273 Description:
274 ***************************************/
on_return_btn_clicked()275 void VideoPlayer::on_return_btn_clicked()
276 {
277 QString tStr = "00:00";
278
279 this->close();
280
281 if(m_bPlayState == PLAYING){
282 on_playButton_clicked();
283 }
284
285 if(ap != NULL){
286 ap->stop();
287 m_bPlayState = STOPPED;
288 progressSliderTimer->stop();
289 ui->progressSlider->setValue(0);
290 ui->curTimeLabel->setText(tStr);
291 }
292
293 if(m_bSearchDialogOpen == true){
294 m_bSearchDialogOpen = false;
295 m_pSearchDialog->hide();
296 }
297
298 pGlobalMediaHome->setHidden(false);
299 }
300
301
302 /**************************************
303 Function:
304 Description:
305 ***************************************/
on_fullScreenButton_clicked()306 void VideoPlayer::on_fullScreenButton_clicked()
307 {
308 ui->UnFullscreenButton->setVisible(true);
309 ui->controlButtonwidget->setVisible(false);
310 ui->progressSlider->setVisible(false);
311 ui->tilteWidget->setVisible(false);
312 ui->sliderAndTimeLabelWidget->move(0,570);
313 ui->subtitlewidget->move(0,510);
314 ui->videoNameWidget->move(0,5);
315
316 }
317
318 /**************************************
319 Function:
320 Description:
321 ***************************************/
on_UnFullscreenButton_clicked()322 void VideoPlayer::on_UnFullscreenButton_clicked()
323 {
324 ui->UnFullscreenButton->setVisible(false);
325 ui->controlButtonwidget->setVisible(true);
326 ui->progressSlider->setVisible(true);
327 ui->tilteWidget->setVisible(true);
328 ui->sliderAndTimeLabelWidget->move(0,490);
329 ui->subtitlewidget->move(0,430);
330 ui->videoNameWidget->move(0,70);
331 }
332
333
334 /**************************************
335 Function:
336 Description:
337 ***************************************/
stopPlay()338 void VideoPlayer::stopPlay()
339 {
340 if(ap != NULL){
341 ap->stop();
342 }
343 }
344 /**************************************
345 Function:
346 Description:
347 ***************************************/
on_preVideoButton_clicked()348 void VideoPlayer::on_preVideoButton_clicked()
349 {
350 if(m_curVideoFileCount -1 >= 0){
351 m_curVideoFileCount--;
352 m_curVideoFilePath = m_playList.at(m_curVideoFileCount).absoluteFilePath();
353 m_curVideoFileName = m_playList.at(m_curVideoFileCount).completeBaseName();
354 progressSliderTimer->stop();
355 stopPlay();
356 startPlay();
357 }else{
358 return ;
359 }
360 }
361 /**************************************
362 Function:
363 Description:
364 ***************************************/
on_nextVideoButton_clicked()365 void VideoPlayer::on_nextVideoButton_clicked()
366 {
367 qDebug()<<"on_nextVideoButton_clicked ";
368
369 if(m_curVideoFileCount + 1 < m_playList.count()){
370 m_curVideoFileCount++;
371 m_curVideoFilePath = m_playList.at(m_curVideoFileCount).absoluteFilePath();
372 m_curVideoFileName = m_playList.at(m_curVideoFileCount).completeBaseName();
373 progressSliderTimer->stop();
374 stopPlay();
375 startPlay();
376 }else{
377 return ;
378 }
379 }
380 /**************************************
381 Function:
382 Description:
383 ***************************************/
on_playButton_clicked()384 void VideoPlayer::on_playButton_clicked()
385 {
386 int ret=0;
387 if(ap != NULL){
388 ret = ap->pause();
389 if(ret != 0)
390 qDebug()<<"pause bug";
391 if (m_bPlayState == PAUSEED){
392 m_bPlayState = PLAYING;
393 ui->playButton->setIcon(QIcon(":/images/playerIcons/Pause.png"));
394 }
395 else if(m_bPlayState == PLAYING){
396 m_bPlayState = PAUSEED;
397 ui->playButton->setIcon(QIcon(":/images/playerIcons/Play.png"));
398 }
399 else if(m_bPlayState == STOPPED){
400 initAutPlayer();
401 }
402 }
403 }
404
405 /**************************************
406 Function:
407 Description:
408 ***************************************/
409 /*/
410 void VideoPlayer::on_playListButton_clicked()
411 {
412 qDebug()<<"on_playListButton_clicked "<<m_bVideoListOpen;
413
414 if(m_pVideoPlayList == NULL){
415 m_pVideoPlayList = VideoPlayList::Instance(this);
416 m_pVideoPlayList->move(1024,50);
417 }
418 if(m_bVideoListOpen == false){
419 emit showPlayList();
420 m_bVideoListOpen = true;
421 } else {
422 emit hidePlayList();
423 m_bVideoListOpen = false;
424 }
425
426 }
427 */
428 /**************************************
429 Function:
430 Description:
431 ***************************************/
on_searchButton_clicked()432 void VideoPlayer::on_searchButton_clicked()
433 {
434 if(m_pSearchDialog == NULL){
435 m_pSearchDialog = new searchFileDialog(1,this);
436
437 connect(m_pSearchDialog,SIGNAL(notifyVideoPlayerAddFile(QString)),\
438 this,SLOT(on_notifyVideoPlayerAddFile(QString)));
439
440 connect(m_pSearchDialog,SIGNAL(notifyVideoCancelSearch()),\
441 this,SLOT(on_notifyVideoCancelSearch()));
442 }
443
444 if(m_bSearchDialogOpen == false){
445 m_bSearchDialogOpen = true;
446 m_pSearchDialog->showD();
447 } else {
448 m_bSearchDialogOpen = false;
449 m_pSearchDialog->hide();
450
451 }
452 }
453
454 /**************************************
455 Function:
456 Description:
457 ***************************************/
on_notifyVideoPlayerToPlay(QList<QFileInfo> playList,int row)458 void VideoPlayer::on_notifyVideoPlayerToPlay(QList<QFileInfo> playList,int row)
459 {
460 //hide the playlist
461 emit hidePlayList();
462 m_bVideoListOpen = 0;
463
464 m_playList = playList;
465 if(m_playList.count() < row){
466 qDebug()<<"playlist is wrong";
467 return;
468 }
469 m_curVideoFileName = m_playList.at(row).completeBaseName();
470 m_curVideoFilePath = m_playList.at(row).absoluteFilePath();
471 m_curVideoFileCount = row;
472
473 stopPlay();
474 startPlay();
475 }
476
477
478 /**************************************
479 Function:
480 Description:
481 ***************************************/
on_notifyVideoPlayerListChanged(QList<QFileInfo> playList)482 void VideoPlayer::on_notifyVideoPlayerListChanged(QList<QFileInfo> playList)
483 {
484 m_playList = playList;
485 }
486
487
488 /**************************************
489 Function:
490 Description:
491 ***************************************/
getNumOfAudioAndSubtitle(int & subtitleNum,int & audioNum)492 void VideoPlayer::getNumOfAudioAndSubtitle(int& subtitleNum,int& audioNum)
493 {
494 if(ap != NULL){
495 MediaInfo* mediaInfo = ap->getMediaInfo();
496 subtitleNum = mediaInfo->nSubtitleStreamNum;
497 audioNum = mediaInfo->nAudioStreamNum;
498 }else{
499 frmMessageBox* msg = new frmMessageBox();
500 msg->SetMessage("播放器没有在运行!!",0);
501 msg->show();
502 }
503 }
504
505 /**************************************
506 Function:
507 Description:
508 ***************************************/
on_subtitleSelectButton_clicked()509 void VideoPlayer::on_subtitleSelectButton_clicked()
510 {
511
512
513 QFileDialog *fileDialog = new QFileDialog(this);
514
515 fileDialog->setStyleSheet("font: bold; font-size:20px; color: rgb(0,0,0); background-color: rgb(170, 255, 255)");
516 fileDialog->setFileMode(QFileDialog::Directory);
517 fileDialog->setDirectory("/mnt/");
518
519 if(fileDialog->exec() == QDialog::Accepted)
520 {
521 str_SelectPath = fileDialog->getExistingDirectory();
522 qDebug()<<"the path is "<< str_SelectPath;
523
524 }else{
525
526 }
527 }
528
529
530 /**************************************
531 Function:
532 Description:
533 ***************************************/
setSubtitleNum(QAction * pAction)534 void VideoPlayer::setSubtitleNum(QAction* pAction)
535 {
536 QString file = pAction->text();
537 pAction->setCheckable(true);
538 pAction->setChecked(true);
539 qDebug()<<"triggered the "<<file;
540 QString tmp;
541 for(int i=0;i<file.size();i++){
542 if(file[i].isDigit()){
543 tmp += file[i];
544 }
545 }
546 int num = tmp.toInt();
547 m_subtitleNow = num;
548 qDebug()<<"the subtitle "<<num<<" will be set";
549 ap->switchSubtitle(num);
550
551 }
552
553 /**************************************
554 Function:
555 Description:
556 ***************************************/
setAudioNum(QAction * pAction)557 void VideoPlayer::setAudioNum(QAction* pAction)
558 {
559 QString file = pAction->text();
560 pAction->setCheckable(true);
561 pAction->setChecked(true);
562 qDebug()<<"triggered the "<<file;
563 QString tmp;
564 for(int i = 0;i < file.size();i++){
565 if(file[i].isDigit()){
566 tmp += file[i];
567 }
568 }
569 int num = tmp.toInt();
570 m_audioNow = num;
571 qDebug()<<"the audio "<<num<<" will be set";
572 ap->switchAudio(num);
573
574 }
575
576 /**************************************
577 Function:
578 Description:
579 ***************************************/
doMsgCmd(int msgType,void * user,void * data,int len)580 void VideoPlayer::doMsgCmd(int msgType, void* user,void* data,int len)
581 {
582 switch(msgType)
583 {
584
585 case 128:
586 break;
587 case SUBCTRL_SUBTITLE_AVAILABLE:
588 break;
589 case SUBCTRL_SUBTITLE_EXPIRED:
590 break;
591 case AWPLAYER_MEDIA_PREPARED:
592 break;
593 case AWPLAYER_MEDIA_PLAYBACK_COMPLETE:
594 on_nextVideoButton_clicked();
595 break;
596 case AWPLAYER_MEDIA_SEEK_COMPLETE:
597
598 break;
599 case AWPLAYER_MEDIA_STARTED:
600 break;
601 case AWPLAYER_MEDIA_LOG_RECORDER:
602 break;
603 case AWPLAYER_MEDIA_BUFFERING_UPDATE:
604 break;
605 case AWPLAYER_MEDIA_ERROR:
606 break;
607 case AWPLAYER_MEDIA_INFO:
608 break;
609 case AWPLAYER_MEDIA_SET_VIDEO_SIZE:
610 break;
611 default:
612 break;
613 }
614 }
615
616 /**************************************
617 Function:
618 Description:
619 ***************************************/
autVideoCb_func(int msgType,void * user,void * data,int len)620 int VideoPlayer::autVideoCb_func(int msgType, void* user,void* data,int len)
621 {
622 emit pStaticVideoPlayer->msgSignal(msgType,user,data,len);
623 }
624
625
626 /**************************************
627 Function:
628 Description:
629 ***************************************/
on_notifyVideoPlayerAddFile(QString strpath)630 void VideoPlayer::on_notifyVideoPlayerAddFile(QString strpath)
631 {
632 m_curVideoFilePath = strpath;
633 QFileInfo fileInfo(strpath);
634 m_curVideoFileName = fileInfo.fileName();
635
636 if(m_bSearchDialogOpen == true){
637 m_bSearchDialogOpen = false;
638 m_pSearchDialog->hide();
639 }
640
641 qDebug()<<"on_notifyVideoPlayerAddFile "<< m_curVideoFilePath << " "<< m_curVideoFileName;
642
643 AudioSink sink;
644 sink.setOutType(AudioSink::WM8960_OUT);
645
646 stopPlay();
647 startPlay();
648
649 }
650
651 /**************************************
652 Function:
653 Description:
654 ***************************************/
on_notifyVideoCancelSearch()655 void VideoPlayer::on_notifyVideoCancelSearch()
656 {
657
658 qDebug()<<"on_notifyVideoCancelSearch ";
659
660 if(m_bSearchDialogOpen == true){
661 m_bSearchDialogOpen = false;
662 m_pSearchDialog->hide();
663 }
664 }
665
666