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