| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551 |
- #include "VideoPlayer.h"
- #include "AutoConnect.h"
- #include "Utility.h"
- #include "UserInterfaceUtility.h"
- #include "ark_api.h"
- #include <QTimer>
- #include <QList>
- #include <QFileInfo>
- #include <QDomDocument>
- #include <fcntl.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <string.h>
- #include <QDebug>
- #include <arpa/inet.h> //for in_addr
- #include <linux/rtnetlink.h> //for rtnetlink
- #include <net/if.h> //for IF_NAMESIZ, route_info
- #include <QFile>
- #include <pthread.h>
- #include<sys/types.h>
- #include<sys/stat.h>
- #include<sys/wait.h>
- #include<semaphore.h>
- #include <errno.h>
- #include <iostream>
- #include <sys/time.h>
- #include <termios.h>
- #include "configUtils.h"
- using namespace std;
- static const string SDMultimedia("/data/MultiMediaFile/SDMultimedia");
- static const string SDVideoMillesmial("/data/MultiMediaFile/SDVideoMillesmial");
- static const string SDVideoPathInfo("/data/MultiMediaFile/SDVideoPathInfo");
- static const string USBMultimedia("/data/MultiMediaFile/USBMultimedia");
- static const string USBVideoMillesmial("/data/MultiMediaFile/USBVideoMillesmial");
- static const string USBVideoPathInfo("/data/MultiMediaFile/USBVideoPathInfo");
- int MyClass::m_VideoPlayStatus = 0;
- bool MyClass::m_VideoPlayQuitStatus = false;
- /*
- *功能:获取当前的播放模式
- */
- //int getCrrentvideoPlayMode()
- //{
- // QFile file("/etc/videoplaymode");
- // if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- // {
- // qDebug()<<Q_FUNC_INFO<<"file :/etc/videoplaymode is open failed";
- // }
- // QTextStream in(&file);
- // QString line = in.readLine();
- // qDebug() << Q_FUNC_INFO<<"current video mode ="<<line.toInt();
- // file.flush();
- // file.close();
- // if(line.isEmpty()){
- // return 0;
- // }
- // return line.toInt();
- //}
- class VideoPlayerPrivate
- {
- Q_DISABLE_COPY(VideoPlayerPrivate)
- public:
- explicit VideoPlayerPrivate(VideoPlayer *parent);
- ~VideoPlayerPrivate();
- void initialize();
- void connectAllSlots();
- void setPlayMode(const VideoPlayerPlayMode mode);
- void setPlayStatus(const VideoPlayerPlayStatus status);
- void pauseToggleHandler(const QString& output);
- void endTimePositionHandler(const QString &output);
- void millesimalHandler(const QString& output);
- void timePositionHandler(const QString &output);
- QList<QString>& getPathList(const DeviceWatcherType type);
- void endOfFileHandler();
- void unsupportHandler();
- void undragAndDropHandler();//add by wandz 20190425
- void seekToMillesimal(const int millesimal);
- void createSDFileNamesXml();
- void createUSBFileNamesXml();
- void videoPlayerSetPlay();
- void videoPlayerSetSuspend();
- void videoPlayerSetPause();
- void videoPlayerSetExit(const DeviceWatcherType type);
- void exitVideoPlayer();
- void videoPlayerVisible(const bool flag);
- QString chgform(QString name);
- void writeCmdToMplayer(const char* cmd);
- void quitMpalyer();
- void onVideoPlayerVisible(bool visible);
- void sliderTouchEnable(bool enable);
- void videoPlayerPlayStatus(const int type, const int status);
- ssize_t readLine(int fd, char *buffer, size_t n);
- bool contains(const std::string& str, const std::string& substr);
- int isRunning(int pid);
- int isZombieProcess(int pid);
- void playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal);
- VideoPlayerPlayMode m_PlayMode;
- VideoPlayerPlayStatus m_PlayStatus;
- QString m_SDFileNamesXml;
- QString m_USBFileNamesXml;
- QList<QString> m_SDPathList;
- QList<QString> m_USBPathList;
- DeviceWatcherType m_DiskType;
- static const quint32 GUARD_BEFORE_VALUE = 0xDEADBEEF;
- static const quint32 GUARD_AFTER_VALUE = 0xCAFEBABE;
- quint32 m_guardBefore; // 前哨兵
- int m_PlayIndex;
- quint32 m_guardAfter; // 后哨兵
- // 内存完整性检查方法
- bool checkMemoryIntegrity() const;
- void logMemoryCorruption() const;
- int m_ElapsedTime;
- int m_Millesimal;
- int m_EndTime;
- int m_X;
- int m_Y;
- int m_Width;
- int m_Height;
- bool m_Suspend;
- int m_ReadpThread;
- pthread_t m_VideoPthread;
- int fd[2];
- int m_Fd_w;
- pid_t m_pid1;
- pid_t m_pid2;
- QString m_Process;
- QString m_FilePath;
- bool m_IsCurrentFile;
- bool m_VideoPlayerExitStatus;
- bool m_VideoPlayerReset;
- QMutex m_Mutex;
- QTimer *m_VideoTimer;
- private:
- Q_DECLARE_PUBLIC(VideoPlayer)
- VideoPlayer* const q_ptr;
- };
- static void* quitHanlder(void* arg)
- {
- VideoPlayerPrivate* pThis = (VideoPlayerPrivate*)arg;
- char info[1024] = {0};
- int ret = 0;
- time_t time_pget;
- struct tm *p_time;
- time(&time_pget);
- p_time = localtime(&time_pget);
- int last_sec = p_time->tm_sec;
- int currentpThread = 1;
- //bool currentProcessIsNotRunning = false;
- while (1) {
- if(pThis->m_ReadpThread == 0)
- {
- qDebug()<<"+++[VideoPlayer::quitHanlder:pThis->m_ReadpThread]+++"<<pThis->m_ReadpThread;
- break;//退出线程
- }
- if(currentpThread == 0){
- qDebug()<<"+++[VideoPlayer::quitHanlder:currentpThread]+++"<<currentpThread;
- break;//退出线程
- }
- time(&time_pget);
- p_time = localtime(&time_pget);
- if(last_sec != p_time->tm_sec)
- {
- last_sec= p_time->tm_sec;
- //qDebug()<<"+++[VideoPlayer::Time:"<<p_time->tm_hour<<":"<<p_time->tm_min<<":"<<p_time->tm_sec;
- if(pThis->isRunning(pThis->m_pid2) == 1)
- {
- if(pThis->isZombieProcess(pThis->m_pid2) == 1)//判断进程是否为僵尸进程
- {
- qDebug()<<"==============musice_278";
- currentpThread = 0;
- continue;
- }
- }
- if(MyClass::m_VideoPlayQuitStatus){
- if(!pThis->m_VideoPlayerExitStatus)
- {
- qDebug()<<"####video thread exit mplayer289######:";
- pThis->quitMpalyer();
- }
- }
- }
-
- bzero(info, sizeof(info));
- ret = pThis->readLine(pThis->fd[0], info, sizeof(info));
- while(ret > 0)
- {
- if(MyClass::m_VideoPlayStatus==0)
- {
- MyClass::m_VideoPlayStatus = 1;
- }
- string str(info);
- QString output = QString::fromStdString(str);
- //qDebug()<<"=== mplayer video:"<<output;
- if(output.contains(pThis->m_FilePath))
- {
- pThis->m_IsCurrentFile = true;
- }
- if(!pThis->m_IsCurrentFile)
- {
- if((output.contains(QString("mplayer exit done")))){//mplayer新增退出前下发退出通知
- qDebug()<<"=======videoMplayer exit==177=====";
- currentpThread = 0;
- break;
- }
- bzero(info, sizeof(info));
- ret = pThis->readLine(pThis->fd[0], info, sizeof(info));
- continue;
- }
- if (output.contains(QString("ANS_LENGTH="))) {
- pThis->endTimePositionHandler(output);
- }else if(output.contains("get pipe ready")){//mplayer读取管道准别OK
- if(pThis->m_VideoPlayerExitStatus){
- pThis->quitMpalyer();
- }
- } else if (output.contains(QString("ANS_PERCENT_POSITION="))) {
- pThis->millesimalHandler(output);
- } else if (output.contains(QString("ANS_TIME_POSITION="))) {
- pThis->timePositionHandler(output);
- } else if (output.contains(QString("ANS_pause"))) {
- pThis->pauseToggleHandler(output);
- } else if (output.contains(QString("EOF code: 1"))) {
- pThis->setPlayStatus(VPPS_Stop);
- // usleep(500*1000);
- pThis->endOfFileHandler();
- }
- else if (output.contains(QString("give up video"))) {
- pThis->onVideoPlayerVisible(false);
- } else if (output.contains(QString("take video"))) {
- pThis->setPlayStatus(VPPS_Undefine);
- pThis->onVideoPlayerVisible(true);
- } else if(output.contains(QString("Cannot seek in this file"))) {
- qDebug() << __PRETTY_FUNCTION__<<__LINE__ << "seek failed";
- pThis->sliderTouchEnable(false);//dll add 不可拖动的视频禁止拖动
- pThis->undragAndDropHandler();
- } else if ((output.contains(QString("incorrect streams")))//wandz video
- || (output.contains(QString("No stream found")))
- || (output.contains(QString("Cannot find codec matching selected -vo and video format")))
- || (output.contains(QString("Maybe you are playing a non-interleaved stream/file or the codec failed")))
- ) {
- qDebug()<<"=== unsupport_outpit:"<<output;
- pThis->unsupportHandler();
- usleep(500*1000);
- }/* else if (output.contains(QString("seek-begin"))) {
- } */else if ((output.contains(QString("seek-finish"))
- || (output.contains(QString("seek-error"))))) {
- pThis->setPlayStatus(VPPS_SeekFinish);
- pThis->videoPlayerPlayStatus(pThis->m_DiskType, VPPS_SeekFinish);
- }
- else if((output.contains(QString("Exiting... (Fatal error)"))))
- {
- pThis->videoPlayerPlayStatus(pThis->m_DiskType, VPPS_PlayError);
- }
- else if((output.contains(QString("mplayer exit done")))){//mplayer新增退出前下发退出通知
- qDebug()<<"=======VideoMplayer exit==228=====";
- currentpThread = 0;
- break;
- }
- else {
- if (output != QString("\n")) {
- qDebug() << "output" << output;
- }
- }
- if(MyClass::m_VideoPlayQuitStatus){
- if(!pThis->m_VideoPlayerExitStatus)//应用没有主动发送quit,发送了quit这个变量值为true
- {
- qDebug()<<"####video thread exit mplayer249######";
- pThis->quitMpalyer();//视频进程还在跑,此时要启动音乐进程,发送quit退出,以防应用方没有发送quit退出,导致视屏播放不了
- }
- break;
- }
- bzero(info, sizeof(info));
- ret = pThis->readLine(pThis->fd[0], info, sizeof(info));
- }
- usleep(1000*5);
- }
- QMutexLocker mutexLocker(&pThis->m_Mutex);//加锁,线程结束才主线程才启动新的mplayer
- usleep(1000 * 20);
- pid_t pid = 0;
- if(pThis->isRunning(pThis->m_pid2) == 1) {
- qDebug()<<"##############@287_m_pid2:"<<pThis->m_pid2;
- for(int i= 0;i<100;i++)
- {
- if(pThis->isZombieProcess(pThis->m_pid2)==1)//判断进程是否为僵尸进程
- {
- qDebug()<<"#############i:"<<i;
- pid= waitpid(pThis->m_pid2,NULL,0);//回收固定的进程号
- qDebug()<<"videoPlye:@@@@@@pid=%d@@@@@"<<pid;
- break;
- }
- }
- }
-
- if(pThis->m_Fd_w > 0)
- {
- close(pThis->m_Fd_w);
- pThis->m_Fd_w = -1 ;
- }
- pThis->m_Process.clear();
- MyClass::m_VideoPlayQuitStatus = false;
- close(pThis->fd[0]);
- pThis->m_ReadpThread = 0;
- pThis->m_VideoPthread= 0;
- mutexLocker.unlock();//解锁,要不然主线程启动不了新的mplayer
- if(!pThis->m_VideoPlayerExitStatus)
- {
- pThis->m_VideoPlayerExitStatus = false;
- MyClass::m_VideoPlayStatus = 0;
- if(pid == pThis->m_pid2){
- if ((pThis->getPathList(pThis->m_DiskType).size() - 1) > pThis->m_PlayIndex) {
- ++pThis->m_PlayIndex;
- } else {
- pThis->m_PlayIndex = 0;
- }
- qDebug() << "***********" << __FUNCTION__ << " " << __LINE__ << " m_PlayIndex:" << pThis->m_PlayIndex;
- pThis->playVideoIndex(pThis->m_DiskType, pThis->m_PlayIndex, pThis->m_X, pThis->m_Y, pThis->m_Width, pThis->m_Height, 0);//mpalyer挂掉了,如果while循环里面mplayer还没有成为僵尸进程,重新启动一个新的mplayer
- }
- }
- else{
- pThis->m_VideoPlayerExitStatus = false;
- MyClass::m_VideoPlayStatus = 0;
- qDebug()<<"==============m_VideoPlayerReset_true321"<<pThis->m_VideoPlayerReset;
- if(pThis->m_VideoPlayerReset){
- pThis->m_VideoPlayerReset = false;
- pThis->playVideoIndex(pThis->m_DiskType, pThis->m_PlayIndex, pThis->m_X, pThis->m_Y, pThis->m_Width, pThis->m_Height, pThis->m_Millesimal);
- }
- else{
- pThis->setPlayStatus(VPPS_Exit);
- }
- }
- qDebug()<<"video pthead end";
- return NULL;
- }
- void VideoPlayer::videoPlayerRequestFileNames(const DeviceWatcherType type)
- {
- Q_D(VideoPlayer);
- switch (type) {
- case DWT_SDDisk: {
- emit onVideoPlayerFileNames(DWT_SDDisk, d->m_SDFileNamesXml);
- break;
- }
- case DWT_USBDisk: {
- emit onVideoPlayerFileNames(DWT_USBDisk, d->m_USBFileNamesXml);
- break;
- }
- default: {
- break;
- }
- }
- }
- void VideoPlayer::videoPlayerSetPlayModeToggle()
- {
- Q_D(VideoPlayer);
- switch (d->m_PlayMode) {
- case VPPM_AllRepeat: {
- videoPlayerSetPlayMode(VPPM_Shuffle);
- break;
- }
- case VPPM_Shuffle: {
- videoPlayerSetPlayMode(VPPM_RepeatOnce);
- break;
- }
- case VPPM_RepeatOnce:
- default: {
- videoPlayerSetPlayMode(VPPM_AllRepeat);
- break;
- }
- }
- }
- void VideoPlayer::videoPlayerSetPlayMode(const VideoPlayerPlayMode mode)
- {
- Q_D(VideoPlayer);
- d->setPlayMode(mode);
- }
- void VideoPlayer::videoPlayerSetPlayStatusToggle()
- {
- Q_D(VideoPlayer);
- switch (d->m_PlayStatus) {
- case VPPS_Play: {
- videoPlayerSetPlayStatus(VPPS_Pause);
- break;
- }
- case VPPS_Pause: {
- videoPlayerSetPlayStatus(VPPS_Play);
- break;
- }
- case VPPS_Stop: {
- videoPlayerSetPlayStatus(VPPS_Stop);
- }
- case VPPS_Unsupport: {
- //playVideoIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
- break;
- }
- default: {
- break;
- }
- }
- }
- void VideoPlayer::videoPlayerSetPlayStatus(const VideoPlayerPlayStatus status)
- {
- Q_D(VideoPlayer);
- if(d->m_Process.size() != 0 && (d->isRunning(d->m_pid2)==1)) {
- switch (status) {
- case VPPS_Play: {
- d->videoPlayerSetPlay();
- break;
- }
- case VPPS_Pause: {
- d->videoPlayerSetPause();
- break;
- }
- case VPPS_SuspendToggle: {
- d->videoPlayerSetSuspend();
- break;
- }
- case VPPS_Stop: {
- videoPlayerPlayListViewIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
- break;
- }
- default : {
- break;
- }
- }
- }
- }
- void VideoPlayer::videoPlayerPlayListViewIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
- {
- playVideoIndex(type, index, x, y, width, height, millesimal);
- }
- void VideoPlayer::videoPlayerPlayPreviousListViewIndex()
- {
- Q_D(VideoPlayer);
- // 检查内存完整性
- if (!d->checkMemoryIntegrity()) {
- qCritical() << "Memory corruption detected before videoPlayerPlayPreviousListViewIndex processing!";
- // 可以选择恢复或退出
- return;
- }
- int lastIndex;
- printf("*************%s m_PlayMode:%d m_PlayIndex:%d\n",__FUNCTION__, d->m_PlayMode, d->m_PlayIndex);
- if (MPPM_Shuffle == d->m_PlayMode) {
- lastIndex = QRandomGenerator::global()->bounded(d->getPathList( d->m_DiskType).size());
- } else {
- lastIndex = d->m_PlayIndex;
- QList<QString> temp = d->getPathList(d->m_DiskType);
- if ((lastIndex > 0)
- && (lastIndex <= temp.size() - 1)) {
- --lastIndex;
- } else {
- lastIndex = temp.size() - 1;
- }
- }
- videoPlayerPlayListViewIndex(d->m_DiskType, lastIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
- }
- void VideoPlayer::videoPlayerPlayNextListViewIndex()
- {
- Q_D(VideoPlayer);
- // 检查内存完整性
- if (!d->checkMemoryIntegrity()) {
- qCritical() << "Memory corruption detected before videoPlayerPlayNextListViewIndex processing!";
- // 可以选择恢复或退出
- return;
- }
- int lastIndex;
- printf("*************%s m_PlayMode:%d m_PlayIndex:%d\n",__FUNCTION__, d->m_PlayMode, d->m_PlayIndex);
- if (MPPM_Shuffle == d->m_PlayMode) {
- lastIndex = QRandomGenerator::global()->bounded(d->getPathList( d->m_DiskType).size());
- } else {
- lastIndex = d->m_PlayIndex;
- QList<QString> temp = d->getPathList(d->m_DiskType);
- if (((lastIndex < (temp.size() - 1)))
- && (lastIndex >= 0)) {
- ++lastIndex;
- } else {
- lastIndex = 0;
- }
- }
- videoPlayerPlayListViewIndex(d->m_DiskType, lastIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
- }
- void VideoPlayer::videoPlayerSeekToMillesimal(const int millesimal)
- {
- Q_D(VideoPlayer);
- if(d->isRunning(d->m_pid2)==1){
- d->seekToMillesimal(millesimal);
- }
- }
- void VideoPlayer::videoPlayerExit()
- {
- Q_D(VideoPlayer);
- d->videoPlayerSetExit(d->m_DiskType);
- }
- void VideoPlayer::videoPlayerVisible(const bool flag)
- {
- Q_D(VideoPlayer);
- if(d->isRunning(d->m_pid2)==1){
- d->videoPlayerVisible(flag);
- }
- }
- void VideoPlayer::onDeviceWatcherStatus(const DeviceWatcherType type, const DeviceWatcherStatus status)
- {
- Q_D(VideoPlayer);
- qDebug()<<"[VideoPlayer] onDeviceWatcherStatus ++++++" << status;
- if (DWT_SDDisk == type) {
- switch (status) {
- case DWS_Empty: {
- d->m_SDPathList.clear();
- break;
- }
- case DWS_Busy: {
- d->m_SDPathList.clear();
- break;
- }
- case DWS_Ready: {
- d->createSDFileNamesXml();
- videoPlayerRequestFileNames(DWT_SDDisk);
- break;
- }
- case DWS_Remove: {
- d->m_SDPathList.clear();
- if (DWT_SDDisk == d->m_DiskType) {
- if(d->isRunning(d->m_pid2)==1){
- d->quitMpalyer();
- }
- d->setPlayStatus(VPPS_Exit);
- }
- break;
- }
- default : {
- break;
- }
- }
- } else if (DWT_USBDisk == type) {
- switch (status) {
- case DWS_Empty: {
- d->m_USBPathList.clear();
- break;
- }
- case DWS_Busy: {
- d->m_USBPathList.clear();
- break;
- }
- case DWS_Ready: {
- d->createUSBFileNamesXml();
- videoPlayerRequestFileNames(DWT_USBDisk);
- break;
- }
- case DWS_Remove: {
- d->m_USBPathList.clear();
- if (DWT_USBDisk == d->m_DiskType) {
- if(d->isRunning(d->m_pid2)==1){
- d->quitMpalyer();
- }
- d->setPlayStatus(VPPS_Exit);
- }
- }
- default : {
- break;
- }
- }
- }
- }
- void VideoPlayer::onVideoFilePath(const QString &path, const DeviceWatcherType type)
- {
- Q_D(VideoPlayer);
- if (type == DWT_USBDisk) {
- d->m_USBPathList.append(path);
- } else if (type == DWT_SDDisk) {
- d->m_SDPathList.append(path);
- }
- // emit onVideoPlayerFilePath(type, path);
- }
- VideoPlayer::VideoPlayer(QObject *parent)
- : QObject(parent)
- , d_ptr(new VideoPlayerPrivate(this))
- {
- }
- VideoPlayer::~VideoPlayer()
- {
- }
- void VideoPlayer::writeCmdToMplayer(char* cmd){
- Q_D(VideoPlayer);
- d->writeCmdToMplayer(cmd);
- }
- pthread_t VideoPlayer::getVideoPthreadValue()
- {
- Q_D(VideoPlayer);
- return d->m_VideoPthread;
- }
- VideoPlayerPrivate::VideoPlayerPrivate(VideoPlayer *parent)
- : q_ptr(parent)
- {
- // 先初始化哨兵
- m_guardBefore = GUARD_BEFORE_VALUE;
- m_guardAfter = GUARD_AFTER_VALUE;
- m_PlayMode = VPPM_AllRepeat;
- //m_PlayMode = getCrrentvideoPlayMode();
- m_PlayStatus = VPPS_Exit;
- m_DiskType = DWT_Undefine;
- m_PlayIndex = -1;
- m_ElapsedTime = -1;
- m_Millesimal = 0;
- m_EndTime = -1;
- m_X = 0;
- m_Y = 0;
- m_Width = 800;
- m_Height = 480;
- m_Suspend = false;
- m_ReadpThread = 0;
- m_VideoPthread = 0;
- m_pid2 = 0;
- m_Fd_w = -1;
- m_IsCurrentFile = false;
- m_VideoPlayerExitStatus = false;
- m_VideoPlayerReset = false;
- m_Process.clear();
- m_VideoTimer = NULL;
- initialize();
- connectAllSlots();
- }
- VideoPlayerPrivate::~VideoPlayerPrivate()
- {
- if(isRunning(m_pid2)==1){
- quitMpalyer();
- }
- }
- void VideoPlayerPrivate::initialize()
- {
- Q_Q(VideoPlayer);
- if(m_VideoTimer == NULL) {
- m_VideoTimer = new QTimer(q);
- m_VideoTimer->setInterval(100);
- }
- }
- void VideoPlayerPrivate::connectAllSlots()
- {
- Q_Q(VideoPlayer);
- connectSignalAndSlotByNamesake(g_DeviceWatcher, q, SLOT(onDeviceWatcherStatus(const int, const int)));
- connectSignalAndSlotByNamesake(g_DeviceWatcher, q, SLOT(onVideoFilePath(const QString &, const int)));
- QObject::connect(m_VideoTimer,SIGNAL(timeout()),q,SLOT(onTimeout()));
- }
- void VideoPlayer::onTimeout()
- {
- Q_D(VideoPlayer);
- QTimer* ptr = static_cast<QTimer*>(sender());
- if(ptr == d->m_VideoTimer)
- {
- qDebug()<<"######VideoPlayer::onTimeout()_725_MyClass::m_MusicPlayStatus:"<<MyClass::m_MusicPlayStatus;
- if(MyClass::m_MusicPlayStatus == 0){
- playVideoIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
- }
- }
- }
- void VideoPlayer::videoPlayerDeleteListItem(int index)
- {
- Q_D(VideoPlayer);
- qDebug()<<"[MultimediaService]==>VideoPlayer videoPlayerDeleteListItem enter index:"<<index;
- if(index < d->m_USBPathList.size() && d->m_USBPathList.size() > 0)
- {
- int ret = std::remove(d->m_USBPathList.at(index).toStdString().c_str());
- if(ret == 0){
- qDebug()<<"videoPlayerDeleteListItem success.";
- } else {
- qDebug()<<"videoPlayerDeleteListItem fail.";
- }
- d->m_USBPathList.replace(index,"remove"); //删除list里面的元素方式和APP里面采用方式一样
- }
- }
- void VideoPlayer::startDeleteVideoListItem()
- {
- Q_D(VideoPlayer);
- qDebug()<<"[MultimediaService]==>VideoPlayer startDeleteVideoListItem ";
- //删除QList元素
- for(int i = 0; i < d->m_USBPathList.size(); ++i)
- {
- if("remove"== d->m_USBPathList[i])
- {
- d->m_USBPathList.removeAt(i);
- i--;//将元素删除后链表中元素的排列。删除一个元素后,后面的元素会补到被删元素的位置
- }
- }
- }
- void VideoPlayer::playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
- {
- Q_D(VideoPlayer);
- qDebug() << "***************x: " << x << " y:" << y << " width:" << width << " height:" << height;
- QMutexLocker mutexLocker(&d->m_Mutex);
- if(d->m_VideoPlayerExitStatus){
- return;
- }
- QList<QString> temp = d->getPathList(type);
- if(index < 0)
- {
- return;
- }
- qDebug()<<"###########temp.size()#####"<<temp.size();
- qDebug()<<"###########index740#####"<< index;
- if(temp.size() == 0 ){
- qDebug()<<"###########m_MusicPlayStatus=1#####";
- if(d->m_VideoTimer->isActive())
- {
- d->m_VideoTimer->stop();
- MyClass::m_MusicPlayQuitStatus = false;
- }
- return;
- }
- if ((temp.size() > index) && (QFile(temp.at(index))).exists()) {
- d->m_DiskType = type;
- d->m_ElapsedTime = 0;
- d->m_EndTime = 0;
- d->m_PlayIndex = index;
- d->m_Suspend = false;
- d->m_Millesimal = millesimal;
- d->m_X = x;
- d->m_Y = y;
- d->m_Width = width;
- d->m_Height = height;
- bool _ProcessIsRunning(false);
- if(MyClass::m_MusicPlayStatus == 1){
- qDebug()<<"###########m_MusicPlayStatus=1#####";
- if(!d->m_VideoTimer->isActive())
- {
- MyClass::m_MusicPlayQuitStatus = true;
- d->m_VideoTimer->start();
- }
- return;
- }
- else{
- if(d->m_VideoTimer->isActive())
- d->m_VideoTimer->stop();
- }
- MyClass::m_MusicPlayQuitStatus = false;
- if (0 != millesimal) {//不是从头开始播放,millesimal 表示播放的百分比
- if(d->m_Process.size() != 0){
- if(d->isRunning(d->m_pid2) == 1){
- d->quitMpalyer();
- d->m_VideoPlayerReset = true;
- qDebug()<<"==============m_VideoPlayerReset_true783";
- return;
- }
- }
- }
- d->m_DiskType = type;
- usleep(150*1000);
- if(d->isRunning(d->m_pid2)==1){
- if(d->isZombieProcess(d->m_pid2) == 1)//判断进程是否为僵尸进程
- {
- pid_t pid = waitpid(d->m_pid2,NULL,0);//回收固定的进程号
- qDebug()<<"######pid_800"<<pid;
- if(d->m_ReadpThread == 1){
- d->m_ReadpThread = 0;
- d->m_VideoPlayerReset = true;
- qDebug()<<"==============m_VideoPlayerReset_true798";
- return;
- }
- }
- }
- qDebug()<<"###########d->m_VideoPlayerExitStatus 794#####"<<d->m_VideoPlayerExitStatus;
- if(d->m_VideoPlayerExitStatus){
- return;
- }
- if(d->isRunning(d->m_pid2)==0){
- if(d->m_ReadpThread == 1){
- d->m_ReadpThread = 0;
- d->m_VideoPlayerReset = true;
- qDebug()<<"==============m_VideoPlayerReset_true811";
- return;
- }
- }
- qDebug()<<"##########d->m_Process.size()######"<<d->m_Process.size();
- if(d->m_Process.size() == 0)
- {
- _ProcessIsRunning = true;
- }
- qDebug()<<"##########_ProcessIsRunning######"<<_ProcessIsRunning;
- if (_ProcessIsRunning){
- if(access("/tmp/VideoPipe",F_OK) != 0)
- {
- qDebug()<<"VideoPipe is not exist";
- mkfifo("/tmp/VideoPipe",0777);
- }
- else{
- unlink("/tmp/VideoPipe");
- mkfifo("/tmp/VideoPipe", 0777);
- }
- if(pipe(d->fd) < 0){
- perror("pipe error\n");
- return;
- }
- fcntl(d->fd[0], F_SETFL, O_NONBLOCK); //无名管道设为非阻塞
- d->m_pid1 = vfork();
- qDebug()<<"+++++++++0000video_pid++++++++";
- if(d->m_pid1 < 0){
- perror("fork error\n");
- }
- else if(d->m_pid1 == 0)
- {
- d->m_pid2 = getpid();
- qDebug()<<"[VideoPlayer] child video m_pid2:"<<d->m_pid2;
- close(d->fd[0]);
- dup2(d->fd[1], STDERR_FILENO);
- d->m_FilePath = temp.at(d->m_PlayIndex);
- d->m_IsCurrentFile = false;
- QByteArray _NameByte = temp.at(d->m_PlayIndex).toLocal8Bit();
- const char* _Name = _NameByte.data();
- QString _GeometryStr = QString(QString::number(d->m_Width)
- + QString("x")
- + QString::number(d->m_Height)
- + QString("+")
- + QString::number(d->m_X)
- + QString("+")
- + QString::number(d->m_Y));
- QByteArray _GeometryByte = _GeometryStr.toLocal8Bit();
- const char* _Geometry = _GeometryByte.data();
- if(millesimal == 0){
- execlp("/usr/bin/mplayer", "mplayer", "-slave", "-quiet", "-idle","-geometry",_Geometry,
- "-vo", "customfb","-ao","alsa","-input", "file=/tmp/VideoPipe", _Name,NULL); //命名管道pipe有命令数据,mplayer会自动读取
- }
- else{
- const char* _ChMillesimal = std::to_string(millesimal).c_str();
- execlp("/usr/bin/mplayer", "mplayer", "-slave", "-quiet", "-idle","-geometry",_Geometry,
- "-vo", "customfb","-ao","alsa","-input", "file=/tmp/VideoPipe","-ss",_ChMillesimal, _Name,NULL); //命名管道pipe有命令数据,mplayer会自动读取
- }
- }
- else{
- printf("##########m_VideoTestTimer.start");
- //d->m_VideoTestTimer->start();
- //切換视频时解开进度条拖动..防止不可拖动的视频也能拖动
- emit onSliderTouchEnable(true);
- emit onVideoPlayerElapsedInformation(d->m_ElapsedTime, d->m_Millesimal);
- emit onVideoPlayerInformation(d->m_DiskType,d->m_PlayIndex, QFileInfo(temp.at(d->m_PlayIndex)).fileName(), d->m_EndTime);
- emit onVideoPlayerPlayMode(d->m_PlayMode);
- d->setPlayStatus(VPPS_Start);
- QFileInfo fileInfo(temp.at(d->m_PlayIndex));
- QString filePath = fileInfo.filePath() + QString("/") +
- fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) +
- fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) +
- QString::number(fileInfo.size());
- int value = MT_Video;
- if(d->m_DiskType == DWT_USBDisk){
- SaveConfigString(filePath.toStdString(),USBVideoPathInfo);
- SaveConfigString(value,USBMultimedia);
- }
- else if(d->m_DiskType == DWT_SDDisk){
- SaveConfigString(filePath.toStdString(),SDVideoPathInfo);
- SaveConfigString(value,SDMultimedia);
- }
- d->m_Process = QString("mplayer");
- d->m_ReadpThread = 1;
- close(d->fd[1]);
- // usleep(200*1000);
- int ret = pthread_create(&d->m_VideoPthread, NULL,quitHanlder,d);
- if(ret != 0) {
- qDebug()<<"pthread_create failed!";
- }
-
- ret = pthread_detach(d->m_VideoPthread);
- if (ret != 0) {
- // 线程分离出错处理
- perror("pthread_detach error:");
- return;
- }
- }
- }
- else{
- char pathname[1024] = {0};
- d->m_FilePath = temp.at(d->m_PlayIndex);
- d->m_IsCurrentFile = false;
- QByteArray _NameByte = d->chgform(temp.at(d->m_PlayIndex)).toLocal8Bit();
- char* _Name = _NameByte.data();
- snprintf(pathname, sizeof(pathname),"loadfile %s\n",_Name);
- if(d->m_Fd_w < 0)
- {
- d->m_Fd_w = open("/tmp/VideoPipe", O_WRONLY | O_NONBLOCK | O_TRUNC);
- if(d->m_Fd_w < 0){
- qDebug()<<"video open file error";
- d->m_ReadpThread = 0;
- MyClass::m_VideoPlayQuitStatus = false;
- d->m_Process.clear();
- return;
- }
- }
- //ftruncate(d->m_Fd_w,0);
- //lseek(d->m_Fd_w,0,SEEK_SET);
- tcflush(d->m_Fd_w, TCIOFLUSH);
- if(write(d->m_Fd_w, pathname, strlen(pathname)) == -1) {
- perror("write") ;
- }
- //切換视频时解开进度条拖动..防止不可拖动的视频也能拖动
- emit onSliderTouchEnable(true);
- d->setPlayStatus(VPPS_Start);
- emit onVideoPlayerElapsedInformation(d->m_ElapsedTime, d->m_Millesimal);
- emit onVideoPlayerInformation(d->m_DiskType,d->m_PlayIndex, QFileInfo(temp.at(d->m_PlayIndex)).fileName(), d->m_EndTime);
- emit onVideoPlayerPlayMode(d->m_PlayMode);
- QFileInfo fileInfo(temp.at(d->m_PlayIndex));
- QString filePath = fileInfo.filePath() + QString("/") +
- fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) +
- fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) +
- QString::number(fileInfo.size());
- int value = MT_Video;
- if(d->m_DiskType == DWT_USBDisk){
- SaveConfigString(filePath.toStdString(),USBVideoPathInfo);
- SaveConfigString(value,USBMultimedia);
- }
- else if(d->m_DiskType == DWT_SDDisk){
- SaveConfigString(filePath.toStdString(),SDVideoPathInfo);
- SaveConfigString(value,SDMultimedia);
- }
- }
- }
- mutexLocker.unlock();
- }
- void VideoPlayerPrivate::setPlayMode(const VideoPlayerPlayMode mode)
- {
- Q_Q(VideoPlayer);
- if (mode != m_PlayMode) {
- m_PlayMode = mode;
- }
- emit q->onVideoPlayerPlayMode(m_PlayMode);
- }
- void VideoPlayerPrivate::setPlayStatus(const VideoPlayerPlayStatus status)
- {
- Q_Q(VideoPlayer);
- if (status != m_PlayStatus) {
- m_PlayStatus = status;
- emit q->onVideoPlayerPlayStatus(m_DiskType, status);
- }
- }
- void VideoPlayerPrivate::pauseToggleHandler(const QString &output)
- {
- if (output.contains(QString("yes"))) {
- setPlayStatus(VPPS_Pause);
- } else if (output.contains(QString("no"))) {
- setPlayStatus(VPPS_Play);
- }
- }
- void VideoPlayerPrivate::endTimePositionHandler(const QString &output)
- {
- Q_Q(VideoPlayer);
- QString keyWord("ANS_LENGTH=");
- int startPos = output.indexOf(keyWord);
- int length = keyWord.length();
- QString endTime;
- endTime.clear();
- QList<QString> temp = getPathList(m_DiskType);
- if (temp.size() > m_PlayIndex) {
- m_EndTime = 0;
- if (-1 != startPos) {
- for (int i = 0; i < (output.length() - startPos); ++i) {
- if (QChar('\n') == output.at(startPos + length + i)) {
- m_EndTime = static_cast<int>(endTime.toFloat());
- break;
- }
- endTime += output.at(startPos + length + i);
- }
- }
- //qDebug()<<"+++++++endTimePositionHandler+++++++++++";
- emit q->onVideoPlayerInformation(m_DiskType, m_PlayIndex, QFileInfo(temp.at(m_PlayIndex)).fileName(), m_EndTime);
- setPlayStatus(VPPS_Play);
- } else {
- qCritical() << temp.size() << m_PlayIndex;
- }
- }
- void VideoPlayerPrivate::millesimalHandler(const QString &output)
- {
- int start = output.indexOf(QChar('=')) + 1;
- int end = output.indexOf(QChar('\n'));
- if ((-1 != start)
- && (-1 != end)) {
- bool ok(false);
- int millesimal = static_cast<int>(output.mid(start, end - start).toFloat(&ok) * 1000);
- if (ok) {
- m_Millesimal = millesimal;
- }
- }
- }
- void VideoPlayerPrivate::timePositionHandler(const QString &output)
- {
- Q_Q(VideoPlayer);
- if (output.startsWith(QString("ANS_TIME_POSITION="), Qt::CaseSensitive)) {
- int start = output.indexOf(QChar('=')) + 1;
- int end = output.indexOf(QChar('\n'));
- if ((-1 != start)
- && (-1 != end)) {
- bool ok = true;
- int elapsed = output.mid(start, end - start).toInt(&ok);
- if (ok) {
- if (((qAbs(m_ElapsedTime - elapsed) > 5))
- || (elapsed % 5)) {
- if(m_DiskType == DWT_USBDisk){
- SaveConfigString(elapsed,USBVideoMillesmial);
- }
- else if(m_DiskType == DWT_SDDisk){
- SaveConfigString(elapsed,SDVideoMillesmial);
- }
- }
- m_ElapsedTime = elapsed;
- setPlayStatus(VPPS_Play);
- emit q->onVideoPlayerElapsedInformation(m_ElapsedTime, m_Millesimal);
- }
- }
- }
- }
- QList<QString> &VideoPlayerPrivate::getPathList(const int type)
- {
- switch (type) {
- case DWT_SDDisk: {
- return m_SDPathList;
- break;
- }
- case DWT_USBDisk:
- default: {
- return m_USBPathList;
- break;
- }
- }
- }
- void VideoPlayerPrivate::endOfFileHandler()
- {
- Q_Q(VideoPlayer);
- // 检查内存完整性
- if (!checkMemoryIntegrity()) {
- qCritical() << "Memory corruption detected before endOfFileHandler processing!";
- // 可以选择恢复或退出
- return;
- }
- switch (m_PlayStatus) {
- case VPPS_Start:
- case VPPS_Unsupport:
- case VPPS_Pause:
- case VPPS_Stop:
- case VPPS_Play: {
- qDebug() << "*************" << __FUNCTION__ << " m_PlayMode:" << m_PlayMode << " m_PlayIndex:" << m_PlayIndex;
- switch (m_PlayMode) {
- case VPPM_AllRepeat: {
- if ((getPathList(m_DiskType).size() - 1) > m_PlayIndex) {
- m_PlayIndex++;
- } else {
- m_PlayIndex = 0;
- }
- break;
- }
- case VPPM_RepeatOnce: {
- break;
- }
- case VPPM_Shuffle:
- default : {
- m_PlayIndex = QRandomGenerator::global()->bounded(getPathList( m_DiskType).size());
- break;
- }
- }
- q->playVideoIndex(m_DiskType, m_PlayIndex, m_X, m_Y, m_Width, m_Height, 0);
- break;
- }
- default: {
- break;
- }
- }
- }
- void VideoPlayerPrivate::unsupportHandler()
- {
- Q_Q(VideoPlayer);
- QList<QString> temp = getPathList(m_DiskType);
- if (m_PlayIndex < temp.size()) {
- m_ElapsedTime = 0;
- m_Millesimal = 0;
- emit q->onVideoPlayerElapsedInformation(m_ElapsedTime, m_Millesimal);
- m_EndTime = 0;
- emit q->onVideoPlayerInformation(m_DiskType, m_PlayIndex, QFileInfo(temp.at(m_PlayIndex)).fileName(), m_EndTime);
- setPlayStatus(VPPS_Unsupport);
- endOfFileHandler();
- }
- }
- //handler for can't drag and drop for video,from wandz at 20190425
- void VideoPlayerPrivate::undragAndDropHandler()
- {
- setPlayStatus(VPPS_UndragAndDrop);
- }
- void VideoPlayerPrivate::seekToMillesimal(const int millesimal)
- {
- Q_Q(VideoPlayer);
- if((VPPS_Pause == m_PlayStatus)
- || (VPPS_Play == m_PlayStatus)) {
- //百分比
- // QString cmd("seek %1.%2 1\n");
- // cmd = cmd.arg(millesimal / 10, 3, 10, QChar(' ')).arg(millesimal % 10, 1, 10, QChar('0'));
- // writeCmdToMplayer(cmd.toLocal8Bit().data());
- //秒数
- QString cmd("seek %1 2\n");
- cmd = cmd.arg(millesimal);
- writeCmdToMplayer(cmd.toLocal8Bit().data());
- qDebug()<<"=== seekToMillesimal:"<<cmd;
- }else{
- q->playVideoIndex(m_DiskType, m_PlayIndex, m_X, m_Y, m_Width, m_Height, 0);
- }
- }
- void VideoPlayerPrivate::createSDFileNamesXml()
- {
- QDomDocument domDocument;
- domDocument.clear();
- domDocument.appendChild(domDocument.createElement(QString("VideoPlayer")));
- QDomElement root = domDocument.firstChildElement(QString("VideoPlayer"));
- QDomElement fileNames;
- QDomElement info;
- fileNames = domDocument.createElement(QString("SDFileNames"));
- root.appendChild(fileNames);
- m_SDFileNamesXml.clear();
- string pathInfo;
- LoadConfigString(pathInfo,SDVideoPathInfo);
- QString sdPersistantPathInfo = QString::fromStdString(pathInfo);
- QStringList stringList = sdPersistantPathInfo.split(QChar('/'));
- QString sdPersistantPath = sdPersistantPathInfo.left(sdPersistantPathInfo.length() - stringList.last().length() - 1);
- QString persistantIndex("");
- QFileInfo fileInfo;
- int i;
- for (i = 0; i < m_SDPathList.size(); ++i) {
- info = domDocument.createElement(QString("Index:" + QString::number(i)));
- fileNames.appendChild(info);
- fileInfo.setFile(m_SDPathList.at(i));
- info.appendChild(domDocument.createTextNode(fileInfo.fileName()));
- if (persistantIndex.isEmpty()) {
- if (fileInfo.filePath() == sdPersistantPath) {
- if (sdPersistantPathInfo == (fileInfo.filePath() + QString("/") + fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) + fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) + QString::number(fileInfo.size()))) {
- persistantIndex = QString::number(i);
- }
- }
- }
- }
- QDomElement persistant = domDocument.createElement(QString("SDPersistant"));
- root.appendChild(persistant);
- int mediaType = 0;
- LoadConfigString(mediaType,SDMultimedia);
- unsigned char multimediaType = (unsigned char)mediaType;
- int videoMillesmial = 0;
- if (persistantIndex.isEmpty()) {
- if (0 == i) {
- persistantIndex = QString("-1");
- multimediaType = MT_Idle;
- } else {
- if (0 == multimediaType) {
- multimediaType = MT_Video;
- SaveConfigString(multimediaType,SDMultimedia);
- }
- persistantIndex = QString("0");
- }
- } else {
- LoadConfigString(videoMillesmial,SDVideoMillesmial);
- }
- QString data = persistantIndex + QString("-") + QString::number(videoMillesmial);
- //qDebug()<<__PRETTY_FUNCTION__<<"line:"<<__LINE__<<"sd videoMillesmia"<<data;
- persistant.appendChild(domDocument.createTextNode(data));
- QDomElement type = domDocument.createElement(QString("SDType"));
- root.appendChild(type);
- type.appendChild(domDocument.createTextNode(QString::number(multimediaType)));
- m_SDFileNamesXml = domDocument.toString();
- }
- void VideoPlayerPrivate::createUSBFileNamesXml()
- {
- QDomDocument domDocument;
- domDocument.clear();
- domDocument.appendChild(domDocument.createElement(QString("VideoPlayer")));
- QDomElement root = domDocument.firstChildElement(QString("VideoPlayer"));
- QDomElement fileNames;
- QDomElement info;
- fileNames = domDocument.createElement(QString("USBFileNames"));
- root.appendChild(fileNames);
- m_USBFileNamesXml.clear();
- string pathInfo;
- LoadConfigString(pathInfo,USBVideoPathInfo);
- QString usbPersistantPathInfo = QString::fromStdString(pathInfo);
- QStringList stringList = usbPersistantPathInfo.split(QChar('/'));
- QString usbPersistantPath = usbPersistantPathInfo.left(usbPersistantPathInfo.length() - stringList.last().length() - 1);
- QString persistantIndex("");
- QFileInfo fileInfo;
- int i;
- for (i = 0; i < m_USBPathList.size(); ++i) {
- info = domDocument.createElement(QString("Index:" + QString::number(i)));
- fileNames.appendChild(info);
- fileInfo.setFile(m_USBPathList.at(i));
- info.appendChild(domDocument.createTextNode(fileInfo.fileName()));
- if (persistantIndex.isEmpty()) {
- if (fileInfo.filePath() == usbPersistantPath) {
- if (usbPersistantPathInfo == (fileInfo.filePath() + QString("/") + fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) + fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) + QString::number(fileInfo.size()))) {
- persistantIndex = QString::number(i);
- }
- }
- }
- }
- QDomElement persistant = domDocument.createElement(QString("USBPersistant"));
- root.appendChild(persistant);
- int mediaType = 0;
- LoadConfigString(mediaType,USBMultimedia);
- unsigned char multimediaType = (unsigned char)mediaType;
- int videoMillesmial = 0;
- if (persistantIndex.isEmpty()) {
- if (0 == i) {
- persistantIndex = QString("-1");
- multimediaType = MT_Idle;
- } else {
- if (0 == multimediaType) {
- multimediaType = MT_Video;
- SaveConfigString(multimediaType,USBMultimedia);
- }
- persistantIndex = QString("0");
- }
- } else {
- LoadConfigString(videoMillesmial,USBVideoMillesmial);
- }
- QString data = persistantIndex + QString("-") + QString::number(videoMillesmial);
- //qDebug()<<__PRETTY_FUNCTION__<<"line:"<<__LINE__<<"usb videoMillesmia"<<data;
- persistant.appendChild(domDocument.createTextNode(data));
- QDomElement type = domDocument.createElement(QString("USBType"));
- root.appendChild(type);
- type.appendChild(domDocument.createTextNode(QString::number(multimediaType)));
- m_USBFileNamesXml = domDocument.toString();
- }
- void VideoPlayerPrivate::videoPlayerSetPlay()
- {
- switch (m_PlayStatus) {
- case VPPS_Play: {
- break;
- }
- case VPPS_Pause: {
- const char* cmd = "pause\n";
- writeCmdToMplayer(cmd);
- break;
- }
- default: {
- return;
- break;
- }
- }
- }
- void VideoPlayerPrivate::videoPlayerSetSuspend()
- {
- if (m_Suspend) {
- m_Suspend = false;
- videoPlayerSetPlay();
- } else {
- if ((VPPS_Play == m_PlayStatus)
- || (VPPS_Start == m_PlayStatus)) {
- m_Suspend = true;
- videoPlayerSetPause();
- } else if (VPPS_Pause == m_PlayStatus) {
- const char* cmd = "pause\n";
- writeCmdToMplayer(cmd);
- }
- }
- }
- void VideoPlayerPrivate::videoPlayerSetPause()
- {
- switch (m_PlayStatus) {
- case VPPS_Start:
- case VPPS_Play: {
- const char* cmd = "pause\n";
- writeCmdToMplayer(cmd);
- break;
- }
- case VPPS_Pause: {
- break;
- }
- default: {
- return;
- break;
- }
- }
- }
- void VideoPlayerPrivate::videoPlayerSetExit(const DeviceWatcherType type)
- {
- if (type == m_DiskType) {
- exitVideoPlayer();
- }
- }
- void VideoPlayerPrivate::exitVideoPlayer()
- {
- if(isRunning(m_pid2)==1){
- quitMpalyer();
- setPlayStatus(VPPS_Exit);
- }
- }
- void VideoPlayerPrivate::videoPlayerVisible(const bool flag)
- {
- if(m_Process.size() != 0){
- if(isRunning(m_pid2)==1){
- if (flag) {
- const char* cmd = "takevideo\n";
- writeCmdToMplayer(cmd);
- } else {
- const char* cmd = "giveupvideo\n";
- writeCmdToMplayer(cmd);
- }
- }
- }
- }
- QString VideoPlayerPrivate::chgform(QString name)
- {
- // qDebug()<<"+++[MusicPlayerPrivate::chgform]+++"<< name;
- QString leftStr;
- QString str;
- str.clear();
- while(name.size())
- {
- leftStr= name.left(1);
- if(name.size()>1)
- {
- name = name.right(name.size()-1);
- }
- else{
- name.clear();
- }
- if(leftStr == " ")
- {
- leftStr = "\\ ";
- }
- str += leftStr;
- }
- return str;
- }
- void VideoPlayerPrivate::writeCmdToMplayer(const char* cmd)
- {
- qDebug()<<"+++[VideoPlayerPrivate::writeCmdToMplayer:"<<cmd;
- if(!m_VideoPlayerExitStatus){
- if(m_Fd_w < 0)
- {
- m_Fd_w = open("/tmp/VideoPipe",O_WRONLY | O_NONBLOCK | O_TRUNC);
- if(m_Fd_w < 0)
- {
- qDebug()<<"+++[VideoPlayerPrivate::writeCmdToMplayer:]+++";
- qDebug()<<"video open fifo error";
- m_ReadpThread = 0;
- m_Process.clear();
- return;
- }
- }
- // ftruncate(m_Fd_w,0);
- // lseek(m_Fd_w,0,SEEK_SET);
- tcflush(m_Fd_w, TCIOFLUSH);
- if(write(m_Fd_w,cmd,strlen(cmd)) == -1) {
- perror("write");
- }
- }
- return;
- }
- void VideoPlayerPrivate::quitMpalyer()
- {
- Q_Q(VideoPlayer);
- qDebug()<<"+++[VideoPlayerPrivate::quitMpalyer()]+++";
- const char* cmd = "quit\n";
- writeCmdToMplayer(cmd);
- if(m_ReadpThread != 0){
- m_VideoPlayerExitStatus = true;
- }
- emit q->onVideoPlayerExit();
- }
- ssize_t VideoPlayerPrivate::readLine(int fd, char *buffer, size_t n)
- {
- ssize_t numRead;
- size_t totRead;
- char *buf;
- char ch;
- if (n <= 0 || buffer == NULL) {
- errno = EINVAL;
- return -1;
- }
- buf = buffer;
- totRead = 0;
- for (;;) {
- int flags = fcntl(fd,F_GETFL,0);
- flags |= O_NONBLOCK;
- fcntl(fd,F_SETFL,flags);
- numRead = read(fd, &ch, 1);
- if (-1 == numRead) {
- if (errno == EINTR) {
- continue;
- } else {
- return -1;
- }
- } else if (numRead == 0) {
- if (totRead == 0) {
- return 0;
- } else {
- break;
- }
- } else {
- if (totRead < n - 1) {
- totRead++;
- *buf++ = ch;
- }
- if (ch == '\n') {
- break;
- }
- }
- }
- *buf = '\0';
- return totRead;
- }
- void VideoPlayerPrivate::onVideoPlayerVisible(bool visible)
- {
- Q_Q(VideoPlayer);
- emit q->onVideoPlayerVisible(visible);
- }
- void VideoPlayerPrivate::sliderTouchEnable(bool enable)
- {
- Q_Q(VideoPlayer);
- emit q->onSliderTouchEnable(enable);
- }
- void VideoPlayerPrivate::videoPlayerPlayStatus(const int type, const int status)
- {
- Q_Q(VideoPlayer);
- emit q->onVideoPlayerPlayStatus(type, status);
- }
- int VideoPlayerPrivate::isRunning(int pid)
- {
- QString path = QString("/proc/") + QString::number(pid);
- QDir dir(path);
- if(dir.exists()){
- return 1;
- }
- else{
- return 0;
- }
- }
- bool VideoPlayerPrivate::contains(const std::string& str, const std::string& substr)
- {
- return str.find(substr) != std::string::npos;
- }
- int VideoPlayerPrivate::isZombieProcess(int pid)
- {
- //printf("############%s_%d\n",__func__,__LINE__);
- std::string filename;
- std::string line;
- int result = 0;
- filename = string("/proc/") + std::to_string(pid) + string("/status");
- // printf("############filename:%s\n",filename.c_str());
- if (access(filename.data(), F_OK) == 0)
- {
- // printf("############%s_%d\n",__func__,__LINE__);
- std::ifstream readStream;
- readStream.open(filename);
- if(readStream.is_open()){
- while(getline(readStream, line)){
- //std::cout<<line<<std::endl;
- if(line.size() > 0){
- if(contains(line,std::string("State:"))){
- if(contains(line,std::string("zombie"))){//状态为zombie时,表示进程是僵尸进程
- result = 1;
- }
- break;
- }
- }
- line.clear();
- }
- readStream.close();
- }
- else {
- std::cout << "Unable to open file"<<std::endl;;
- }
- }
- return result;
- }
- void VideoPlayerPrivate::playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
- {
- Q_Q(VideoPlayer);
- q->playVideoIndex(type,index, x, y,width,height,millesimal);
- }
- bool VideoPlayerPrivate::checkMemoryIntegrity() const
- {
- bool beforeOk = (m_guardBefore == GUARD_BEFORE_VALUE);
- bool afterOk = (m_guardAfter == GUARD_AFTER_VALUE);
- if (!beforeOk || !afterOk) {
- logMemoryCorruption();
- return false;
- }
- return true;
- }
- void VideoPlayerPrivate::logMemoryCorruption() const
- {
- qCritical() << "==================================================";
- qCritical() << "MEMORY CORRUPTION DETECTED in VideoPlayerPrivate!";
- qCritical() << "Time:" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
- qCritical() << "Guard Before:" << Qt::hex << m_guardBefore << "(expected:" << GUARD_BEFORE_VALUE << ")";
- qCritical() << "PlayIndex:" << m_PlayIndex;
- qCritical() << "Guard After:" << Qt::hex << m_guardAfter << "(expected:" << GUARD_AFTER_VALUE << ")";
- qCritical() << "PlayMode:" << m_PlayMode << "PlayStatus:" << m_PlayStatus;
- qCritical() << "DiskType:" << m_DiskType;
- qCritical() << "==================================================";
- // 可以在这里添加崩溃或恢复逻辑
- // 比如保存日志、重启应用等
- }
|