VideoPlayer.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. #include "VideoPlayer.h"
  2. #include "AutoConnect.h"
  3. #include "Utility.h"
  4. #include "UserInterfaceUtility.h"
  5. #include "ark_api.h"
  6. #include <QTimer>
  7. #include <QList>
  8. #include <QFileInfo>
  9. #include <QDomDocument>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <unistd.h>
  13. #include <pthread.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <QDebug>
  17. #include <arpa/inet.h> //for in_addr
  18. #include <linux/rtnetlink.h> //for rtnetlink
  19. #include <net/if.h> //for IF_NAMESIZ, route_info
  20. #include <QFile>
  21. #include <pthread.h>
  22. #include<sys/types.h>
  23. #include<sys/stat.h>
  24. #include<sys/wait.h>
  25. #include<semaphore.h>
  26. #include <errno.h>
  27. #include <iostream>
  28. #include <sys/time.h>
  29. #include <termios.h>
  30. #include "configUtils.h"
  31. using namespace std;
  32. static const string SDMultimedia("/data/MultiMediaFile/SDMultimedia");
  33. static const string SDVideoMillesmial("/data/MultiMediaFile/SDVideoMillesmial");
  34. static const string SDVideoPathInfo("/data/MultiMediaFile/SDVideoPathInfo");
  35. static const string USBMultimedia("/data/MultiMediaFile/USBMultimedia");
  36. static const string USBVideoMillesmial("/data/MultiMediaFile/USBVideoMillesmial");
  37. static const string USBVideoPathInfo("/data/MultiMediaFile/USBVideoPathInfo");
  38. int MyClass::m_VideoPlayStatus = 0;
  39. bool MyClass::m_VideoPlayQuitStatus = false;
  40. /*
  41. *功能:获取当前的播放模式
  42. */
  43. //int getCrrentvideoPlayMode()
  44. //{
  45. // QFile file("/etc/videoplaymode");
  46. // if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  47. // {
  48. // qDebug()<<Q_FUNC_INFO<<"file :/etc/videoplaymode is open failed";
  49. // }
  50. // QTextStream in(&file);
  51. // QString line = in.readLine();
  52. // qDebug() << Q_FUNC_INFO<<"current video mode ="<<line.toInt();
  53. // file.flush();
  54. // file.close();
  55. // if(line.isEmpty()){
  56. // return 0;
  57. // }
  58. // return line.toInt();
  59. //}
  60. class VideoPlayerPrivate
  61. {
  62. Q_DISABLE_COPY(VideoPlayerPrivate)
  63. public:
  64. explicit VideoPlayerPrivate(VideoPlayer *parent);
  65. ~VideoPlayerPrivate();
  66. void initialize();
  67. void connectAllSlots();
  68. void setPlayMode(const VideoPlayerPlayMode mode);
  69. void setPlayStatus(const VideoPlayerPlayStatus status);
  70. void pauseToggleHandler(const QString& output);
  71. void endTimePositionHandler(const QString &output);
  72. void millesimalHandler(const QString& output);
  73. void timePositionHandler(const QString &output);
  74. QList<QString>& getPathList(const DeviceWatcherType type);
  75. void endOfFileHandler();
  76. void unsupportHandler();
  77. void undragAndDropHandler();//add by wandz 20190425
  78. void seekToMillesimal(const int millesimal);
  79. void createSDFileNamesXml();
  80. void createUSBFileNamesXml();
  81. void videoPlayerSetPlay();
  82. void videoPlayerSetSuspend();
  83. void videoPlayerSetPause();
  84. void videoPlayerSetExit(const DeviceWatcherType type);
  85. void exitVideoPlayer();
  86. void videoPlayerVisible(const bool flag);
  87. QString chgform(QString name);
  88. void writeCmdToMplayer(const char* cmd);
  89. void quitMpalyer();
  90. void onVideoPlayerVisible(bool visible);
  91. void sliderTouchEnable(bool enable);
  92. void videoPlayerPlayStatus(const int type, const int status);
  93. ssize_t readLine(int fd, char *buffer, size_t n);
  94. bool contains(const std::string& str, const std::string& substr);
  95. int isRunning(int pid);
  96. int isZombieProcess(int pid);
  97. void playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal);
  98. VideoPlayerPlayMode m_PlayMode;
  99. VideoPlayerPlayStatus m_PlayStatus;
  100. QString m_SDFileNamesXml;
  101. QString m_USBFileNamesXml;
  102. QList<QString> m_SDPathList;
  103. QList<QString> m_USBPathList;
  104. DeviceWatcherType m_DiskType;
  105. static const quint32 GUARD_BEFORE_VALUE = 0xDEADBEEF;
  106. static const quint32 GUARD_AFTER_VALUE = 0xCAFEBABE;
  107. quint32 m_guardBefore; // 前哨兵
  108. int m_PlayIndex;
  109. quint32 m_guardAfter; // 后哨兵
  110. // 内存完整性检查方法
  111. bool checkMemoryIntegrity() const;
  112. void logMemoryCorruption() const;
  113. int m_ElapsedTime;
  114. int m_Millesimal;
  115. int m_EndTime;
  116. int m_X;
  117. int m_Y;
  118. int m_Width;
  119. int m_Height;
  120. bool m_Suspend;
  121. int m_ReadpThread;
  122. pthread_t m_VideoPthread;
  123. int fd[2];
  124. int m_Fd_w;
  125. pid_t m_pid1;
  126. pid_t m_pid2;
  127. QString m_Process;
  128. QString m_FilePath;
  129. bool m_IsCurrentFile;
  130. bool m_VideoPlayerExitStatus;
  131. bool m_VideoPlayerReset;
  132. QMutex m_Mutex;
  133. QTimer *m_VideoTimer;
  134. private:
  135. Q_DECLARE_PUBLIC(VideoPlayer)
  136. VideoPlayer* const q_ptr;
  137. };
  138. static void* quitHanlder(void* arg)
  139. {
  140. VideoPlayerPrivate* pThis = (VideoPlayerPrivate*)arg;
  141. char info[1024] = {0};
  142. int ret = 0;
  143. time_t time_pget;
  144. struct tm *p_time;
  145. time(&time_pget);
  146. p_time = localtime(&time_pget);
  147. int last_sec = p_time->tm_sec;
  148. int currentpThread = 1;
  149. //bool currentProcessIsNotRunning = false;
  150. while (1) {
  151. if(pThis->m_ReadpThread == 0)
  152. {
  153. qDebug()<<"+++[VideoPlayer::quitHanlder:pThis->m_ReadpThread]+++"<<pThis->m_ReadpThread;
  154. break;//退出线程
  155. }
  156. if(currentpThread == 0){
  157. qDebug()<<"+++[VideoPlayer::quitHanlder:currentpThread]+++"<<currentpThread;
  158. break;//退出线程
  159. }
  160. time(&time_pget);
  161. p_time = localtime(&time_pget);
  162. if(last_sec != p_time->tm_sec)
  163. {
  164. last_sec= p_time->tm_sec;
  165. //qDebug()<<"+++[VideoPlayer::Time:"<<p_time->tm_hour<<":"<<p_time->tm_min<<":"<<p_time->tm_sec;
  166. if(pThis->isRunning(pThis->m_pid2) == 1)
  167. {
  168. if(pThis->isZombieProcess(pThis->m_pid2) == 1)//判断进程是否为僵尸进程
  169. {
  170. qDebug()<<"==============musice_278";
  171. currentpThread = 0;
  172. continue;
  173. }
  174. }
  175. if(MyClass::m_VideoPlayQuitStatus){
  176. if(!pThis->m_VideoPlayerExitStatus)
  177. {
  178. qDebug()<<"####video thread exit mplayer289######:";
  179. pThis->quitMpalyer();
  180. }
  181. }
  182. }
  183. bzero(info, sizeof(info));
  184. ret = pThis->readLine(pThis->fd[0], info, sizeof(info));
  185. while(ret > 0)
  186. {
  187. if(MyClass::m_VideoPlayStatus==0)
  188. {
  189. MyClass::m_VideoPlayStatus = 1;
  190. }
  191. string str(info);
  192. QString output = QString::fromStdString(str);
  193. //qDebug()<<"=== mplayer video:"<<output;
  194. if(output.contains(pThis->m_FilePath))
  195. {
  196. pThis->m_IsCurrentFile = true;
  197. }
  198. if(!pThis->m_IsCurrentFile)
  199. {
  200. if((output.contains(QString("mplayer exit done")))){//mplayer新增退出前下发退出通知
  201. qDebug()<<"=======videoMplayer exit==177=====";
  202. currentpThread = 0;
  203. break;
  204. }
  205. bzero(info, sizeof(info));
  206. ret = pThis->readLine(pThis->fd[0], info, sizeof(info));
  207. continue;
  208. }
  209. if (output.contains(QString("ANS_LENGTH="))) {
  210. pThis->endTimePositionHandler(output);
  211. }else if(output.contains("get pipe ready")){//mplayer读取管道准别OK
  212. if(pThis->m_VideoPlayerExitStatus){
  213. pThis->quitMpalyer();
  214. }
  215. } else if (output.contains(QString("ANS_PERCENT_POSITION="))) {
  216. pThis->millesimalHandler(output);
  217. } else if (output.contains(QString("ANS_TIME_POSITION="))) {
  218. pThis->timePositionHandler(output);
  219. } else if (output.contains(QString("ANS_pause"))) {
  220. pThis->pauseToggleHandler(output);
  221. } else if (output.contains(QString("EOF code: 1"))) {
  222. pThis->setPlayStatus(VPPS_Stop);
  223. // usleep(500*1000);
  224. pThis->endOfFileHandler();
  225. }
  226. else if (output.contains(QString("give up video"))) {
  227. pThis->onVideoPlayerVisible(false);
  228. } else if (output.contains(QString("take video"))) {
  229. pThis->setPlayStatus(VPPS_Undefine);
  230. pThis->onVideoPlayerVisible(true);
  231. } else if(output.contains(QString("Cannot seek in this file"))) {
  232. qDebug() << __PRETTY_FUNCTION__<<__LINE__ << "seek failed";
  233. pThis->sliderTouchEnable(false);//dll add 不可拖动的视频禁止拖动
  234. pThis->undragAndDropHandler();
  235. } else if ((output.contains(QString("incorrect streams")))//wandz video
  236. || (output.contains(QString("No stream found")))
  237. || (output.contains(QString("Cannot find codec matching selected -vo and video format")))
  238. || (output.contains(QString("Maybe you are playing a non-interleaved stream/file or the codec failed")))
  239. ) {
  240. qDebug()<<"=== unsupport_outpit:"<<output;
  241. pThis->unsupportHandler();
  242. usleep(500*1000);
  243. }/* else if (output.contains(QString("seek-begin"))) {
  244. } */else if ((output.contains(QString("seek-finish"))
  245. || (output.contains(QString("seek-error"))))) {
  246. pThis->setPlayStatus(VPPS_SeekFinish);
  247. pThis->videoPlayerPlayStatus(pThis->m_DiskType, VPPS_SeekFinish);
  248. }
  249. else if((output.contains(QString("Exiting... (Fatal error)"))))
  250. {
  251. pThis->videoPlayerPlayStatus(pThis->m_DiskType, VPPS_PlayError);
  252. }
  253. else if((output.contains(QString("mplayer exit done")))){//mplayer新增退出前下发退出通知
  254. qDebug()<<"=======VideoMplayer exit==228=====";
  255. currentpThread = 0;
  256. break;
  257. }
  258. else {
  259. if (output != QString("\n")) {
  260. qDebug() << "output" << output;
  261. }
  262. }
  263. if(MyClass::m_VideoPlayQuitStatus){
  264. if(!pThis->m_VideoPlayerExitStatus)//应用没有主动发送quit,发送了quit这个变量值为true
  265. {
  266. qDebug()<<"####video thread exit mplayer249######";
  267. pThis->quitMpalyer();//视频进程还在跑,此时要启动音乐进程,发送quit退出,以防应用方没有发送quit退出,导致视屏播放不了
  268. }
  269. break;
  270. }
  271. bzero(info, sizeof(info));
  272. ret = pThis->readLine(pThis->fd[0], info, sizeof(info));
  273. }
  274. usleep(1000*5);
  275. }
  276. QMutexLocker mutexLocker(&pThis->m_Mutex);//加锁,线程结束才主线程才启动新的mplayer
  277. usleep(1000 * 20);
  278. pid_t pid = 0;
  279. if(pThis->isRunning(pThis->m_pid2) == 1) {
  280. qDebug()<<"##############@287_m_pid2:"<<pThis->m_pid2;
  281. for(int i= 0;i<100;i++)
  282. {
  283. if(pThis->isZombieProcess(pThis->m_pid2)==1)//判断进程是否为僵尸进程
  284. {
  285. qDebug()<<"#############i:"<<i;
  286. pid= waitpid(pThis->m_pid2,NULL,0);//回收固定的进程号
  287. qDebug()<<"videoPlye:@@@@@@pid=%d@@@@@"<<pid;
  288. break;
  289. }
  290. }
  291. }
  292. if(pThis->m_Fd_w > 0)
  293. {
  294. close(pThis->m_Fd_w);
  295. pThis->m_Fd_w = -1 ;
  296. }
  297. pThis->m_Process.clear();
  298. MyClass::m_VideoPlayQuitStatus = false;
  299. close(pThis->fd[0]);
  300. pThis->m_ReadpThread = 0;
  301. pThis->m_VideoPthread= 0;
  302. mutexLocker.unlock();//解锁,要不然主线程启动不了新的mplayer
  303. if(!pThis->m_VideoPlayerExitStatus)
  304. {
  305. pThis->m_VideoPlayerExitStatus = false;
  306. MyClass::m_VideoPlayStatus = 0;
  307. if(pid == pThis->m_pid2){
  308. if ((pThis->getPathList(pThis->m_DiskType).size() - 1) > pThis->m_PlayIndex) {
  309. ++pThis->m_PlayIndex;
  310. } else {
  311. pThis->m_PlayIndex = 0;
  312. }
  313. qDebug() << "***********" << __FUNCTION__ << " " << __LINE__ << " m_PlayIndex:" << pThis->m_PlayIndex;
  314. 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
  315. }
  316. }
  317. else{
  318. pThis->m_VideoPlayerExitStatus = false;
  319. MyClass::m_VideoPlayStatus = 0;
  320. qDebug()<<"==============m_VideoPlayerReset_true321"<<pThis->m_VideoPlayerReset;
  321. if(pThis->m_VideoPlayerReset){
  322. pThis->m_VideoPlayerReset = false;
  323. pThis->playVideoIndex(pThis->m_DiskType, pThis->m_PlayIndex, pThis->m_X, pThis->m_Y, pThis->m_Width, pThis->m_Height, pThis->m_Millesimal);
  324. }
  325. else{
  326. pThis->setPlayStatus(VPPS_Exit);
  327. }
  328. }
  329. qDebug()<<"video pthead end";
  330. return NULL;
  331. }
  332. void VideoPlayer::videoPlayerRequestFileNames(const DeviceWatcherType type)
  333. {
  334. Q_D(VideoPlayer);
  335. switch (type) {
  336. case DWT_SDDisk: {
  337. emit onVideoPlayerFileNames(DWT_SDDisk, d->m_SDFileNamesXml);
  338. break;
  339. }
  340. case DWT_USBDisk: {
  341. emit onVideoPlayerFileNames(DWT_USBDisk, d->m_USBFileNamesXml);
  342. break;
  343. }
  344. default: {
  345. break;
  346. }
  347. }
  348. }
  349. void VideoPlayer::videoPlayerSetPlayModeToggle()
  350. {
  351. Q_D(VideoPlayer);
  352. switch (d->m_PlayMode) {
  353. case VPPM_AllRepeat: {
  354. videoPlayerSetPlayMode(VPPM_Shuffle);
  355. break;
  356. }
  357. case VPPM_Shuffle: {
  358. videoPlayerSetPlayMode(VPPM_RepeatOnce);
  359. break;
  360. }
  361. case VPPM_RepeatOnce:
  362. default: {
  363. videoPlayerSetPlayMode(VPPM_AllRepeat);
  364. break;
  365. }
  366. }
  367. }
  368. void VideoPlayer::videoPlayerSetPlayMode(const VideoPlayerPlayMode mode)
  369. {
  370. Q_D(VideoPlayer);
  371. d->setPlayMode(mode);
  372. }
  373. void VideoPlayer::videoPlayerSetPlayStatusToggle()
  374. {
  375. Q_D(VideoPlayer);
  376. switch (d->m_PlayStatus) {
  377. case VPPS_Play: {
  378. videoPlayerSetPlayStatus(VPPS_Pause);
  379. break;
  380. }
  381. case VPPS_Pause: {
  382. videoPlayerSetPlayStatus(VPPS_Play);
  383. break;
  384. }
  385. case VPPS_Stop: {
  386. videoPlayerSetPlayStatus(VPPS_Stop);
  387. }
  388. case VPPS_Unsupport: {
  389. //playVideoIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  390. break;
  391. }
  392. default: {
  393. break;
  394. }
  395. }
  396. }
  397. void VideoPlayer::videoPlayerSetPlayStatus(const VideoPlayerPlayStatus status)
  398. {
  399. Q_D(VideoPlayer);
  400. if(d->m_Process.size() != 0 && (d->isRunning(d->m_pid2)==1)) {
  401. switch (status) {
  402. case VPPS_Play: {
  403. d->videoPlayerSetPlay();
  404. break;
  405. }
  406. case VPPS_Pause: {
  407. d->videoPlayerSetPause();
  408. break;
  409. }
  410. case VPPS_SuspendToggle: {
  411. d->videoPlayerSetSuspend();
  412. break;
  413. }
  414. case VPPS_Stop: {
  415. videoPlayerPlayListViewIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  416. break;
  417. }
  418. default : {
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. void VideoPlayer::videoPlayerPlayListViewIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
  425. {
  426. playVideoIndex(type, index, x, y, width, height, millesimal);
  427. }
  428. void VideoPlayer::videoPlayerPlayPreviousListViewIndex()
  429. {
  430. Q_D(VideoPlayer);
  431. // 检查内存完整性
  432. if (!d->checkMemoryIntegrity()) {
  433. qCritical() << "Memory corruption detected before videoPlayerPlayPreviousListViewIndex processing!";
  434. // 可以选择恢复或退出
  435. return;
  436. }
  437. int lastIndex;
  438. printf("*************%s m_PlayMode:%d m_PlayIndex:%d\n",__FUNCTION__, d->m_PlayMode, d->m_PlayIndex);
  439. if (MPPM_Shuffle == d->m_PlayMode) {
  440. lastIndex = QRandomGenerator::global()->bounded(d->getPathList( d->m_DiskType).size());
  441. } else {
  442. lastIndex = d->m_PlayIndex;
  443. QList<QString> temp = d->getPathList(d->m_DiskType);
  444. if ((lastIndex > 0)
  445. && (lastIndex <= temp.size() - 1)) {
  446. --lastIndex;
  447. } else {
  448. lastIndex = temp.size() - 1;
  449. }
  450. }
  451. videoPlayerPlayListViewIndex(d->m_DiskType, lastIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  452. }
  453. void VideoPlayer::videoPlayerPlayNextListViewIndex()
  454. {
  455. Q_D(VideoPlayer);
  456. // 检查内存完整性
  457. if (!d->checkMemoryIntegrity()) {
  458. qCritical() << "Memory corruption detected before videoPlayerPlayNextListViewIndex processing!";
  459. // 可以选择恢复或退出
  460. return;
  461. }
  462. int lastIndex;
  463. printf("*************%s m_PlayMode:%d m_PlayIndex:%d\n",__FUNCTION__, d->m_PlayMode, d->m_PlayIndex);
  464. if (MPPM_Shuffle == d->m_PlayMode) {
  465. lastIndex = QRandomGenerator::global()->bounded(d->getPathList( d->m_DiskType).size());
  466. } else {
  467. lastIndex = d->m_PlayIndex;
  468. QList<QString> temp = d->getPathList(d->m_DiskType);
  469. if (((lastIndex < (temp.size() - 1)))
  470. && (lastIndex >= 0)) {
  471. ++lastIndex;
  472. } else {
  473. lastIndex = 0;
  474. }
  475. }
  476. videoPlayerPlayListViewIndex(d->m_DiskType, lastIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  477. }
  478. void VideoPlayer::videoPlayerSeekToMillesimal(const int millesimal)
  479. {
  480. Q_D(VideoPlayer);
  481. if(d->isRunning(d->m_pid2)==1){
  482. d->seekToMillesimal(millesimal);
  483. }
  484. }
  485. void VideoPlayer::videoPlayerExit()
  486. {
  487. Q_D(VideoPlayer);
  488. d->videoPlayerSetExit(d->m_DiskType);
  489. }
  490. void VideoPlayer::videoPlayerVisible(const bool flag)
  491. {
  492. Q_D(VideoPlayer);
  493. if(d->isRunning(d->m_pid2)==1){
  494. d->videoPlayerVisible(flag);
  495. }
  496. }
  497. void VideoPlayer::onDeviceWatcherStatus(const DeviceWatcherType type, const DeviceWatcherStatus status)
  498. {
  499. Q_D(VideoPlayer);
  500. qDebug()<<"[VideoPlayer] onDeviceWatcherStatus ++++++" << status;
  501. if (DWT_SDDisk == type) {
  502. switch (status) {
  503. case DWS_Empty: {
  504. d->m_SDPathList.clear();
  505. break;
  506. }
  507. case DWS_Busy: {
  508. d->m_SDPathList.clear();
  509. break;
  510. }
  511. case DWS_Ready: {
  512. d->createSDFileNamesXml();
  513. videoPlayerRequestFileNames(DWT_SDDisk);
  514. break;
  515. }
  516. case DWS_Remove: {
  517. d->m_SDPathList.clear();
  518. if (DWT_SDDisk == d->m_DiskType) {
  519. if(d->isRunning(d->m_pid2)==1){
  520. d->quitMpalyer();
  521. }
  522. d->setPlayStatus(VPPS_Exit);
  523. }
  524. break;
  525. }
  526. default : {
  527. break;
  528. }
  529. }
  530. } else if (DWT_USBDisk == type) {
  531. switch (status) {
  532. case DWS_Empty: {
  533. d->m_USBPathList.clear();
  534. break;
  535. }
  536. case DWS_Busy: {
  537. d->m_USBPathList.clear();
  538. break;
  539. }
  540. case DWS_Ready: {
  541. d->createUSBFileNamesXml();
  542. videoPlayerRequestFileNames(DWT_USBDisk);
  543. break;
  544. }
  545. case DWS_Remove: {
  546. d->m_USBPathList.clear();
  547. if (DWT_USBDisk == d->m_DiskType) {
  548. if(d->isRunning(d->m_pid2)==1){
  549. d->quitMpalyer();
  550. }
  551. d->setPlayStatus(VPPS_Exit);
  552. }
  553. }
  554. default : {
  555. break;
  556. }
  557. }
  558. }
  559. }
  560. void VideoPlayer::onVideoFilePath(const QString &path, const DeviceWatcherType type)
  561. {
  562. Q_D(VideoPlayer);
  563. if (type == DWT_USBDisk) {
  564. d->m_USBPathList.append(path);
  565. } else if (type == DWT_SDDisk) {
  566. d->m_SDPathList.append(path);
  567. }
  568. // emit onVideoPlayerFilePath(type, path);
  569. }
  570. VideoPlayer::VideoPlayer(QObject *parent)
  571. : QObject(parent)
  572. , d_ptr(new VideoPlayerPrivate(this))
  573. {
  574. }
  575. VideoPlayer::~VideoPlayer()
  576. {
  577. }
  578. void VideoPlayer::writeCmdToMplayer(char* cmd){
  579. Q_D(VideoPlayer);
  580. d->writeCmdToMplayer(cmd);
  581. }
  582. pthread_t VideoPlayer::getVideoPthreadValue()
  583. {
  584. Q_D(VideoPlayer);
  585. return d->m_VideoPthread;
  586. }
  587. VideoPlayerPrivate::VideoPlayerPrivate(VideoPlayer *parent)
  588. : q_ptr(parent)
  589. {
  590. // 先初始化哨兵
  591. m_guardBefore = GUARD_BEFORE_VALUE;
  592. m_guardAfter = GUARD_AFTER_VALUE;
  593. m_PlayMode = VPPM_AllRepeat;
  594. //m_PlayMode = getCrrentvideoPlayMode();
  595. m_PlayStatus = VPPS_Exit;
  596. m_DiskType = DWT_Undefine;
  597. m_PlayIndex = -1;
  598. m_ElapsedTime = -1;
  599. m_Millesimal = 0;
  600. m_EndTime = -1;
  601. m_X = 0;
  602. m_Y = 0;
  603. m_Width = 800;
  604. m_Height = 480;
  605. m_Suspend = false;
  606. m_ReadpThread = 0;
  607. m_VideoPthread = 0;
  608. m_pid2 = 0;
  609. m_Fd_w = -1;
  610. m_IsCurrentFile = false;
  611. m_VideoPlayerExitStatus = false;
  612. m_VideoPlayerReset = false;
  613. m_Process.clear();
  614. m_VideoTimer = NULL;
  615. initialize();
  616. connectAllSlots();
  617. }
  618. VideoPlayerPrivate::~VideoPlayerPrivate()
  619. {
  620. if(isRunning(m_pid2)==1){
  621. quitMpalyer();
  622. }
  623. }
  624. void VideoPlayerPrivate::initialize()
  625. {
  626. Q_Q(VideoPlayer);
  627. if(m_VideoTimer == NULL) {
  628. m_VideoTimer = new QTimer(q);
  629. m_VideoTimer->setInterval(100);
  630. }
  631. }
  632. void VideoPlayerPrivate::connectAllSlots()
  633. {
  634. Q_Q(VideoPlayer);
  635. connectSignalAndSlotByNamesake(g_DeviceWatcher, q, SLOT(onDeviceWatcherStatus(const int, const int)));
  636. connectSignalAndSlotByNamesake(g_DeviceWatcher, q, SLOT(onVideoFilePath(const QString &, const int)));
  637. QObject::connect(m_VideoTimer,SIGNAL(timeout()),q,SLOT(onTimeout()));
  638. }
  639. void VideoPlayer::onTimeout()
  640. {
  641. Q_D(VideoPlayer);
  642. QTimer* ptr = static_cast<QTimer*>(sender());
  643. if(ptr == d->m_VideoTimer)
  644. {
  645. qDebug()<<"######VideoPlayer::onTimeout()_725_MyClass::m_MusicPlayStatus:"<<MyClass::m_MusicPlayStatus;
  646. if(MyClass::m_MusicPlayStatus == 0){
  647. playVideoIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  648. }
  649. }
  650. }
  651. void VideoPlayer::videoPlayerDeleteListItem(int index)
  652. {
  653. Q_D(VideoPlayer);
  654. qDebug()<<"[MultimediaService]==>VideoPlayer videoPlayerDeleteListItem enter index:"<<index;
  655. if(index < d->m_USBPathList.size() && d->m_USBPathList.size() > 0)
  656. {
  657. int ret = std::remove(d->m_USBPathList.at(index).toStdString().c_str());
  658. if(ret == 0){
  659. qDebug()<<"videoPlayerDeleteListItem success.";
  660. } else {
  661. qDebug()<<"videoPlayerDeleteListItem fail.";
  662. }
  663. d->m_USBPathList.replace(index,"remove"); //删除list里面的元素方式和APP里面采用方式一样
  664. }
  665. }
  666. void VideoPlayer::startDeleteVideoListItem()
  667. {
  668. Q_D(VideoPlayer);
  669. qDebug()<<"[MultimediaService]==>VideoPlayer startDeleteVideoListItem ";
  670. //删除QList元素
  671. for(int i = 0; i < d->m_USBPathList.size(); ++i)
  672. {
  673. if("remove"== d->m_USBPathList[i])
  674. {
  675. d->m_USBPathList.removeAt(i);
  676. i--;//将元素删除后链表中元素的排列。删除一个元素后,后面的元素会补到被删元素的位置
  677. }
  678. }
  679. }
  680. void VideoPlayer::playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
  681. {
  682. Q_D(VideoPlayer);
  683. qDebug() << "***************x: " << x << " y:" << y << " width:" << width << " height:" << height;
  684. QMutexLocker mutexLocker(&d->m_Mutex);
  685. if(d->m_VideoPlayerExitStatus){
  686. return;
  687. }
  688. QList<QString> temp = d->getPathList(type);
  689. if(index < 0)
  690. {
  691. return;
  692. }
  693. qDebug()<<"###########temp.size()#####"<<temp.size();
  694. qDebug()<<"###########index740#####"<< index;
  695. if(temp.size() == 0 ){
  696. qDebug()<<"###########m_MusicPlayStatus=1#####";
  697. if(d->m_VideoTimer->isActive())
  698. {
  699. d->m_VideoTimer->stop();
  700. MyClass::m_MusicPlayQuitStatus = false;
  701. }
  702. return;
  703. }
  704. if ((temp.size() > index) && (QFile(temp.at(index))).exists()) {
  705. d->m_DiskType = type;
  706. d->m_ElapsedTime = 0;
  707. d->m_EndTime = 0;
  708. d->m_PlayIndex = index;
  709. d->m_Suspend = false;
  710. d->m_Millesimal = millesimal;
  711. d->m_X = x;
  712. d->m_Y = y;
  713. d->m_Width = width;
  714. d->m_Height = height;
  715. bool _ProcessIsRunning(false);
  716. if(MyClass::m_MusicPlayStatus == 1){
  717. qDebug()<<"###########m_MusicPlayStatus=1#####";
  718. if(!d->m_VideoTimer->isActive())
  719. {
  720. MyClass::m_MusicPlayQuitStatus = true;
  721. d->m_VideoTimer->start();
  722. }
  723. return;
  724. }
  725. else{
  726. if(d->m_VideoTimer->isActive())
  727. d->m_VideoTimer->stop();
  728. }
  729. MyClass::m_MusicPlayQuitStatus = false;
  730. if (0 != millesimal) {//不是从头开始播放,millesimal 表示播放的百分比
  731. if(d->m_Process.size() != 0){
  732. if(d->isRunning(d->m_pid2) == 1){
  733. d->quitMpalyer();
  734. d->m_VideoPlayerReset = true;
  735. qDebug()<<"==============m_VideoPlayerReset_true783";
  736. return;
  737. }
  738. }
  739. }
  740. d->m_DiskType = type;
  741. usleep(150*1000);
  742. if(d->isRunning(d->m_pid2)==1){
  743. if(d->isZombieProcess(d->m_pid2) == 1)//判断进程是否为僵尸进程
  744. {
  745. pid_t pid = waitpid(d->m_pid2,NULL,0);//回收固定的进程号
  746. qDebug()<<"######pid_800"<<pid;
  747. if(d->m_ReadpThread == 1){
  748. d->m_ReadpThread = 0;
  749. d->m_VideoPlayerReset = true;
  750. qDebug()<<"==============m_VideoPlayerReset_true798";
  751. return;
  752. }
  753. }
  754. }
  755. qDebug()<<"###########d->m_VideoPlayerExitStatus 794#####"<<d->m_VideoPlayerExitStatus;
  756. if(d->m_VideoPlayerExitStatus){
  757. return;
  758. }
  759. if(d->isRunning(d->m_pid2)==0){
  760. if(d->m_ReadpThread == 1){
  761. d->m_ReadpThread = 0;
  762. d->m_VideoPlayerReset = true;
  763. qDebug()<<"==============m_VideoPlayerReset_true811";
  764. return;
  765. }
  766. }
  767. qDebug()<<"##########d->m_Process.size()######"<<d->m_Process.size();
  768. if(d->m_Process.size() == 0)
  769. {
  770. _ProcessIsRunning = true;
  771. }
  772. qDebug()<<"##########_ProcessIsRunning######"<<_ProcessIsRunning;
  773. if (_ProcessIsRunning){
  774. if(access("/tmp/VideoPipe",F_OK) != 0)
  775. {
  776. qDebug()<<"VideoPipe is not exist";
  777. mkfifo("/tmp/VideoPipe",0777);
  778. }
  779. else{
  780. unlink("/tmp/VideoPipe");
  781. mkfifo("/tmp/VideoPipe", 0777);
  782. }
  783. if(pipe(d->fd) < 0){
  784. perror("pipe error\n");
  785. return;
  786. }
  787. fcntl(d->fd[0], F_SETFL, O_NONBLOCK); //无名管道设为非阻塞
  788. d->m_pid1 = vfork();
  789. qDebug()<<"+++++++++0000video_pid++++++++";
  790. if(d->m_pid1 < 0){
  791. perror("fork error\n");
  792. }
  793. else if(d->m_pid1 == 0)
  794. {
  795. d->m_pid2 = getpid();
  796. qDebug()<<"[VideoPlayer] child video m_pid2:"<<d->m_pid2;
  797. close(d->fd[0]);
  798. dup2(d->fd[1], STDERR_FILENO);
  799. d->m_FilePath = temp.at(d->m_PlayIndex);
  800. d->m_IsCurrentFile = false;
  801. QByteArray _NameByte = temp.at(d->m_PlayIndex).toLocal8Bit();
  802. const char* _Name = _NameByte.data();
  803. QString _GeometryStr = QString(QString::number(d->m_Width)
  804. + QString("x")
  805. + QString::number(d->m_Height)
  806. + QString("+")
  807. + QString::number(d->m_X)
  808. + QString("+")
  809. + QString::number(d->m_Y));
  810. QByteArray _GeometryByte = _GeometryStr.toLocal8Bit();
  811. const char* _Geometry = _GeometryByte.data();
  812. if(millesimal == 0){
  813. execlp("/usr/bin/mplayer", "mplayer", "-slave", "-quiet", "-idle","-geometry",_Geometry,
  814. "-vo", "customfb","-ao","alsa","-input", "file=/tmp/VideoPipe", _Name,NULL); //命名管道pipe有命令数据,mplayer会自动读取
  815. }
  816. else{
  817. const char* _ChMillesimal = std::to_string(millesimal).c_str();
  818. execlp("/usr/bin/mplayer", "mplayer", "-slave", "-quiet", "-idle","-geometry",_Geometry,
  819. "-vo", "customfb","-ao","alsa","-input", "file=/tmp/VideoPipe","-ss",_ChMillesimal, _Name,NULL); //命名管道pipe有命令数据,mplayer会自动读取
  820. }
  821. }
  822. else{
  823. printf("##########m_VideoTestTimer.start");
  824. //d->m_VideoTestTimer->start();
  825. //切換视频时解开进度条拖动..防止不可拖动的视频也能拖动
  826. emit onSliderTouchEnable(true);
  827. emit onVideoPlayerElapsedInformation(d->m_ElapsedTime, d->m_Millesimal);
  828. emit onVideoPlayerInformation(d->m_DiskType,d->m_PlayIndex, QFileInfo(temp.at(d->m_PlayIndex)).fileName(), d->m_EndTime);
  829. emit onVideoPlayerPlayMode(d->m_PlayMode);
  830. d->setPlayStatus(VPPS_Start);
  831. QFileInfo fileInfo(temp.at(d->m_PlayIndex));
  832. QString filePath = fileInfo.filePath() + QString("/") +
  833. fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) +
  834. fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) +
  835. QString::number(fileInfo.size());
  836. int value = MT_Video;
  837. if(d->m_DiskType == DWT_USBDisk){
  838. SaveConfigString(filePath.toStdString(),USBVideoPathInfo);
  839. SaveConfigString(value,USBMultimedia);
  840. }
  841. else if(d->m_DiskType == DWT_SDDisk){
  842. SaveConfigString(filePath.toStdString(),SDVideoPathInfo);
  843. SaveConfigString(value,SDMultimedia);
  844. }
  845. d->m_Process = QString("mplayer");
  846. d->m_ReadpThread = 1;
  847. close(d->fd[1]);
  848. // usleep(200*1000);
  849. int ret = pthread_create(&d->m_VideoPthread, NULL,quitHanlder,d);
  850. if(ret != 0) {
  851. qDebug()<<"pthread_create failed!";
  852. }
  853. ret = pthread_detach(d->m_VideoPthread);
  854. if (ret != 0) {
  855. // 线程分离出错处理
  856. perror("pthread_detach error:");
  857. return;
  858. }
  859. }
  860. }
  861. else{
  862. char pathname[1024] = {0};
  863. d->m_FilePath = temp.at(d->m_PlayIndex);
  864. d->m_IsCurrentFile = false;
  865. QByteArray _NameByte = d->chgform(temp.at(d->m_PlayIndex)).toLocal8Bit();
  866. char* _Name = _NameByte.data();
  867. snprintf(pathname, sizeof(pathname),"loadfile %s\n",_Name);
  868. if(d->m_Fd_w < 0)
  869. {
  870. d->m_Fd_w = open("/tmp/VideoPipe", O_WRONLY | O_NONBLOCK | O_TRUNC);
  871. if(d->m_Fd_w < 0){
  872. qDebug()<<"video open file error";
  873. d->m_ReadpThread = 0;
  874. MyClass::m_VideoPlayQuitStatus = false;
  875. d->m_Process.clear();
  876. return;
  877. }
  878. }
  879. //ftruncate(d->m_Fd_w,0);
  880. //lseek(d->m_Fd_w,0,SEEK_SET);
  881. tcflush(d->m_Fd_w, TCIOFLUSH);
  882. if(write(d->m_Fd_w, pathname, strlen(pathname)) == -1) {
  883. perror("write") ;
  884. }
  885. //切換视频时解开进度条拖动..防止不可拖动的视频也能拖动
  886. emit onSliderTouchEnable(true);
  887. d->setPlayStatus(VPPS_Start);
  888. emit onVideoPlayerElapsedInformation(d->m_ElapsedTime, d->m_Millesimal);
  889. emit onVideoPlayerInformation(d->m_DiskType,d->m_PlayIndex, QFileInfo(temp.at(d->m_PlayIndex)).fileName(), d->m_EndTime);
  890. emit onVideoPlayerPlayMode(d->m_PlayMode);
  891. QFileInfo fileInfo(temp.at(d->m_PlayIndex));
  892. QString filePath = fileInfo.filePath() + QString("/") +
  893. fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) +
  894. fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) +
  895. QString::number(fileInfo.size());
  896. int value = MT_Video;
  897. if(d->m_DiskType == DWT_USBDisk){
  898. SaveConfigString(filePath.toStdString(),USBVideoPathInfo);
  899. SaveConfigString(value,USBMultimedia);
  900. }
  901. else if(d->m_DiskType == DWT_SDDisk){
  902. SaveConfigString(filePath.toStdString(),SDVideoPathInfo);
  903. SaveConfigString(value,SDMultimedia);
  904. }
  905. }
  906. }
  907. mutexLocker.unlock();
  908. }
  909. void VideoPlayerPrivate::setPlayMode(const VideoPlayerPlayMode mode)
  910. {
  911. Q_Q(VideoPlayer);
  912. if (mode != m_PlayMode) {
  913. m_PlayMode = mode;
  914. }
  915. emit q->onVideoPlayerPlayMode(m_PlayMode);
  916. }
  917. void VideoPlayerPrivate::setPlayStatus(const VideoPlayerPlayStatus status)
  918. {
  919. Q_Q(VideoPlayer);
  920. if (status != m_PlayStatus) {
  921. m_PlayStatus = status;
  922. emit q->onVideoPlayerPlayStatus(m_DiskType, status);
  923. }
  924. }
  925. void VideoPlayerPrivate::pauseToggleHandler(const QString &output)
  926. {
  927. if (output.contains(QString("yes"))) {
  928. setPlayStatus(VPPS_Pause);
  929. } else if (output.contains(QString("no"))) {
  930. setPlayStatus(VPPS_Play);
  931. }
  932. }
  933. void VideoPlayerPrivate::endTimePositionHandler(const QString &output)
  934. {
  935. Q_Q(VideoPlayer);
  936. QString keyWord("ANS_LENGTH=");
  937. int startPos = output.indexOf(keyWord);
  938. int length = keyWord.length();
  939. QString endTime;
  940. endTime.clear();
  941. QList<QString> temp = getPathList(m_DiskType);
  942. if (temp.size() > m_PlayIndex) {
  943. m_EndTime = 0;
  944. if (-1 != startPos) {
  945. for (int i = 0; i < (output.length() - startPos); ++i) {
  946. if (QChar('\n') == output.at(startPos + length + i)) {
  947. m_EndTime = static_cast<int>(endTime.toFloat());
  948. break;
  949. }
  950. endTime += output.at(startPos + length + i);
  951. }
  952. }
  953. //qDebug()<<"+++++++endTimePositionHandler+++++++++++";
  954. emit q->onVideoPlayerInformation(m_DiskType, m_PlayIndex, QFileInfo(temp.at(m_PlayIndex)).fileName(), m_EndTime);
  955. setPlayStatus(VPPS_Play);
  956. } else {
  957. qCritical() << temp.size() << m_PlayIndex;
  958. }
  959. }
  960. void VideoPlayerPrivate::millesimalHandler(const QString &output)
  961. {
  962. int start = output.indexOf(QChar('=')) + 1;
  963. int end = output.indexOf(QChar('\n'));
  964. if ((-1 != start)
  965. && (-1 != end)) {
  966. bool ok(false);
  967. int millesimal = static_cast<int>(output.mid(start, end - start).toFloat(&ok) * 1000);
  968. if (ok) {
  969. m_Millesimal = millesimal;
  970. }
  971. }
  972. }
  973. void VideoPlayerPrivate::timePositionHandler(const QString &output)
  974. {
  975. Q_Q(VideoPlayer);
  976. if (output.startsWith(QString("ANS_TIME_POSITION="), Qt::CaseSensitive)) {
  977. int start = output.indexOf(QChar('=')) + 1;
  978. int end = output.indexOf(QChar('\n'));
  979. if ((-1 != start)
  980. && (-1 != end)) {
  981. bool ok = true;
  982. int elapsed = output.mid(start, end - start).toInt(&ok);
  983. if (ok) {
  984. if (((qAbs(m_ElapsedTime - elapsed) > 5))
  985. || (elapsed % 5)) {
  986. if(m_DiskType == DWT_USBDisk){
  987. SaveConfigString(elapsed,USBVideoMillesmial);
  988. }
  989. else if(m_DiskType == DWT_SDDisk){
  990. SaveConfigString(elapsed,SDVideoMillesmial);
  991. }
  992. }
  993. m_ElapsedTime = elapsed;
  994. setPlayStatus(VPPS_Play);
  995. emit q->onVideoPlayerElapsedInformation(m_ElapsedTime, m_Millesimal);
  996. }
  997. }
  998. }
  999. }
  1000. QList<QString> &VideoPlayerPrivate::getPathList(const int type)
  1001. {
  1002. switch (type) {
  1003. case DWT_SDDisk: {
  1004. return m_SDPathList;
  1005. break;
  1006. }
  1007. case DWT_USBDisk:
  1008. default: {
  1009. return m_USBPathList;
  1010. break;
  1011. }
  1012. }
  1013. }
  1014. void VideoPlayerPrivate::endOfFileHandler()
  1015. {
  1016. Q_Q(VideoPlayer);
  1017. // 检查内存完整性
  1018. if (!checkMemoryIntegrity()) {
  1019. qCritical() << "Memory corruption detected before endOfFileHandler processing!";
  1020. // 可以选择恢复或退出
  1021. return;
  1022. }
  1023. switch (m_PlayStatus) {
  1024. case VPPS_Start:
  1025. case VPPS_Unsupport:
  1026. case VPPS_Pause:
  1027. case VPPS_Stop:
  1028. case VPPS_Play: {
  1029. qDebug() << "*************" << __FUNCTION__ << " m_PlayMode:" << m_PlayMode << " m_PlayIndex:" << m_PlayIndex;
  1030. switch (m_PlayMode) {
  1031. case VPPM_AllRepeat: {
  1032. if ((getPathList(m_DiskType).size() - 1) > m_PlayIndex) {
  1033. m_PlayIndex++;
  1034. } else {
  1035. m_PlayIndex = 0;
  1036. }
  1037. break;
  1038. }
  1039. case VPPM_RepeatOnce: {
  1040. break;
  1041. }
  1042. case VPPM_Shuffle:
  1043. default : {
  1044. m_PlayIndex = QRandomGenerator::global()->bounded(getPathList( m_DiskType).size());
  1045. break;
  1046. }
  1047. }
  1048. q->playVideoIndex(m_DiskType, m_PlayIndex, m_X, m_Y, m_Width, m_Height, 0);
  1049. break;
  1050. }
  1051. default: {
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. void VideoPlayerPrivate::unsupportHandler()
  1057. {
  1058. Q_Q(VideoPlayer);
  1059. QList<QString> temp = getPathList(m_DiskType);
  1060. if (m_PlayIndex < temp.size()) {
  1061. m_ElapsedTime = 0;
  1062. m_Millesimal = 0;
  1063. emit q->onVideoPlayerElapsedInformation(m_ElapsedTime, m_Millesimal);
  1064. m_EndTime = 0;
  1065. emit q->onVideoPlayerInformation(m_DiskType, m_PlayIndex, QFileInfo(temp.at(m_PlayIndex)).fileName(), m_EndTime);
  1066. setPlayStatus(VPPS_Unsupport);
  1067. endOfFileHandler();
  1068. }
  1069. }
  1070. //handler for can't drag and drop for video,from wandz at 20190425
  1071. void VideoPlayerPrivate::undragAndDropHandler()
  1072. {
  1073. setPlayStatus(VPPS_UndragAndDrop);
  1074. }
  1075. void VideoPlayerPrivate::seekToMillesimal(const int millesimal)
  1076. {
  1077. Q_Q(VideoPlayer);
  1078. if((VPPS_Pause == m_PlayStatus)
  1079. || (VPPS_Play == m_PlayStatus)) {
  1080. //百分比
  1081. // QString cmd("seek %1.%2 1\n");
  1082. // cmd = cmd.arg(millesimal / 10, 3, 10, QChar(' ')).arg(millesimal % 10, 1, 10, QChar('0'));
  1083. // writeCmdToMplayer(cmd.toLocal8Bit().data());
  1084. //秒数
  1085. QString cmd("seek %1 2\n");
  1086. cmd = cmd.arg(millesimal);
  1087. writeCmdToMplayer(cmd.toLocal8Bit().data());
  1088. qDebug()<<"=== seekToMillesimal:"<<cmd;
  1089. }else{
  1090. q->playVideoIndex(m_DiskType, m_PlayIndex, m_X, m_Y, m_Width, m_Height, 0);
  1091. }
  1092. }
  1093. void VideoPlayerPrivate::createSDFileNamesXml()
  1094. {
  1095. QDomDocument domDocument;
  1096. domDocument.clear();
  1097. domDocument.appendChild(domDocument.createElement(QString("VideoPlayer")));
  1098. QDomElement root = domDocument.firstChildElement(QString("VideoPlayer"));
  1099. QDomElement fileNames;
  1100. QDomElement info;
  1101. fileNames = domDocument.createElement(QString("SDFileNames"));
  1102. root.appendChild(fileNames);
  1103. m_SDFileNamesXml.clear();
  1104. string pathInfo;
  1105. LoadConfigString(pathInfo,SDVideoPathInfo);
  1106. QString sdPersistantPathInfo = QString::fromStdString(pathInfo);
  1107. QStringList stringList = sdPersistantPathInfo.split(QChar('/'));
  1108. QString sdPersistantPath = sdPersistantPathInfo.left(sdPersistantPathInfo.length() - stringList.last().length() - 1);
  1109. QString persistantIndex("");
  1110. QFileInfo fileInfo;
  1111. int i;
  1112. for (i = 0; i < m_SDPathList.size(); ++i) {
  1113. info = domDocument.createElement(QString("Index:" + QString::number(i)));
  1114. fileNames.appendChild(info);
  1115. fileInfo.setFile(m_SDPathList.at(i));
  1116. info.appendChild(domDocument.createTextNode(fileInfo.fileName()));
  1117. if (persistantIndex.isEmpty()) {
  1118. if (fileInfo.filePath() == sdPersistantPath) {
  1119. if (sdPersistantPathInfo == (fileInfo.filePath() + QString("/") + fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) + fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) + QString::number(fileInfo.size()))) {
  1120. persistantIndex = QString::number(i);
  1121. }
  1122. }
  1123. }
  1124. }
  1125. QDomElement persistant = domDocument.createElement(QString("SDPersistant"));
  1126. root.appendChild(persistant);
  1127. int mediaType = 0;
  1128. LoadConfigString(mediaType,SDMultimedia);
  1129. unsigned char multimediaType = (unsigned char)mediaType;
  1130. int videoMillesmial = 0;
  1131. if (persistantIndex.isEmpty()) {
  1132. if (0 == i) {
  1133. persistantIndex = QString("-1");
  1134. multimediaType = MT_Idle;
  1135. } else {
  1136. if (0 == multimediaType) {
  1137. multimediaType = MT_Video;
  1138. SaveConfigString(multimediaType,SDMultimedia);
  1139. }
  1140. persistantIndex = QString("0");
  1141. }
  1142. } else {
  1143. LoadConfigString(videoMillesmial,SDVideoMillesmial);
  1144. }
  1145. QString data = persistantIndex + QString("-") + QString::number(videoMillesmial);
  1146. //qDebug()<<__PRETTY_FUNCTION__<<"line:"<<__LINE__<<"sd videoMillesmia"<<data;
  1147. persistant.appendChild(domDocument.createTextNode(data));
  1148. QDomElement type = domDocument.createElement(QString("SDType"));
  1149. root.appendChild(type);
  1150. type.appendChild(domDocument.createTextNode(QString::number(multimediaType)));
  1151. m_SDFileNamesXml = domDocument.toString();
  1152. }
  1153. void VideoPlayerPrivate::createUSBFileNamesXml()
  1154. {
  1155. QDomDocument domDocument;
  1156. domDocument.clear();
  1157. domDocument.appendChild(domDocument.createElement(QString("VideoPlayer")));
  1158. QDomElement root = domDocument.firstChildElement(QString("VideoPlayer"));
  1159. QDomElement fileNames;
  1160. QDomElement info;
  1161. fileNames = domDocument.createElement(QString("USBFileNames"));
  1162. root.appendChild(fileNames);
  1163. m_USBFileNamesXml.clear();
  1164. string pathInfo;
  1165. LoadConfigString(pathInfo,USBVideoPathInfo);
  1166. QString usbPersistantPathInfo = QString::fromStdString(pathInfo);
  1167. QStringList stringList = usbPersistantPathInfo.split(QChar('/'));
  1168. QString usbPersistantPath = usbPersistantPathInfo.left(usbPersistantPathInfo.length() - stringList.last().length() - 1);
  1169. QString persistantIndex("");
  1170. QFileInfo fileInfo;
  1171. int i;
  1172. for (i = 0; i < m_USBPathList.size(); ++i) {
  1173. info = domDocument.createElement(QString("Index:" + QString::number(i)));
  1174. fileNames.appendChild(info);
  1175. fileInfo.setFile(m_USBPathList.at(i));
  1176. info.appendChild(domDocument.createTextNode(fileInfo.fileName()));
  1177. if (persistantIndex.isEmpty()) {
  1178. if (fileInfo.filePath() == usbPersistantPath) {
  1179. if (usbPersistantPathInfo == (fileInfo.filePath() + QString("/") + fileInfo.birthTime().toString(QString("yyyyMMddhhmmss")) + fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) + QString::number(fileInfo.size()))) {
  1180. persistantIndex = QString::number(i);
  1181. }
  1182. }
  1183. }
  1184. }
  1185. QDomElement persistant = domDocument.createElement(QString("USBPersistant"));
  1186. root.appendChild(persistant);
  1187. int mediaType = 0;
  1188. LoadConfigString(mediaType,USBMultimedia);
  1189. unsigned char multimediaType = (unsigned char)mediaType;
  1190. int videoMillesmial = 0;
  1191. if (persistantIndex.isEmpty()) {
  1192. if (0 == i) {
  1193. persistantIndex = QString("-1");
  1194. multimediaType = MT_Idle;
  1195. } else {
  1196. if (0 == multimediaType) {
  1197. multimediaType = MT_Video;
  1198. SaveConfigString(multimediaType,USBMultimedia);
  1199. }
  1200. persistantIndex = QString("0");
  1201. }
  1202. } else {
  1203. LoadConfigString(videoMillesmial,USBVideoMillesmial);
  1204. }
  1205. QString data = persistantIndex + QString("-") + QString::number(videoMillesmial);
  1206. //qDebug()<<__PRETTY_FUNCTION__<<"line:"<<__LINE__<<"usb videoMillesmia"<<data;
  1207. persistant.appendChild(domDocument.createTextNode(data));
  1208. QDomElement type = domDocument.createElement(QString("USBType"));
  1209. root.appendChild(type);
  1210. type.appendChild(domDocument.createTextNode(QString::number(multimediaType)));
  1211. m_USBFileNamesXml = domDocument.toString();
  1212. }
  1213. void VideoPlayerPrivate::videoPlayerSetPlay()
  1214. {
  1215. switch (m_PlayStatus) {
  1216. case VPPS_Play: {
  1217. break;
  1218. }
  1219. case VPPS_Pause: {
  1220. const char* cmd = "pause\n";
  1221. writeCmdToMplayer(cmd);
  1222. break;
  1223. }
  1224. default: {
  1225. return;
  1226. break;
  1227. }
  1228. }
  1229. }
  1230. void VideoPlayerPrivate::videoPlayerSetSuspend()
  1231. {
  1232. if (m_Suspend) {
  1233. m_Suspend = false;
  1234. videoPlayerSetPlay();
  1235. } else {
  1236. if ((VPPS_Play == m_PlayStatus)
  1237. || (VPPS_Start == m_PlayStatus)) {
  1238. m_Suspend = true;
  1239. videoPlayerSetPause();
  1240. } else if (VPPS_Pause == m_PlayStatus) {
  1241. const char* cmd = "pause\n";
  1242. writeCmdToMplayer(cmd);
  1243. }
  1244. }
  1245. }
  1246. void VideoPlayerPrivate::videoPlayerSetPause()
  1247. {
  1248. switch (m_PlayStatus) {
  1249. case VPPS_Start:
  1250. case VPPS_Play: {
  1251. const char* cmd = "pause\n";
  1252. writeCmdToMplayer(cmd);
  1253. break;
  1254. }
  1255. case VPPS_Pause: {
  1256. break;
  1257. }
  1258. default: {
  1259. return;
  1260. break;
  1261. }
  1262. }
  1263. }
  1264. void VideoPlayerPrivate::videoPlayerSetExit(const DeviceWatcherType type)
  1265. {
  1266. if (type == m_DiskType) {
  1267. exitVideoPlayer();
  1268. }
  1269. }
  1270. void VideoPlayerPrivate::exitVideoPlayer()
  1271. {
  1272. if(isRunning(m_pid2)==1){
  1273. quitMpalyer();
  1274. setPlayStatus(VPPS_Exit);
  1275. }
  1276. }
  1277. void VideoPlayerPrivate::videoPlayerVisible(const bool flag)
  1278. {
  1279. if(m_Process.size() != 0){
  1280. if(isRunning(m_pid2)==1){
  1281. if (flag) {
  1282. const char* cmd = "takevideo\n";
  1283. writeCmdToMplayer(cmd);
  1284. } else {
  1285. const char* cmd = "giveupvideo\n";
  1286. writeCmdToMplayer(cmd);
  1287. }
  1288. }
  1289. }
  1290. }
  1291. QString VideoPlayerPrivate::chgform(QString name)
  1292. {
  1293. // qDebug()<<"+++[MusicPlayerPrivate::chgform]+++"<< name;
  1294. QString leftStr;
  1295. QString str;
  1296. str.clear();
  1297. while(name.size())
  1298. {
  1299. leftStr= name.left(1);
  1300. if(name.size()>1)
  1301. {
  1302. name = name.right(name.size()-1);
  1303. }
  1304. else{
  1305. name.clear();
  1306. }
  1307. if(leftStr == " ")
  1308. {
  1309. leftStr = "\\ ";
  1310. }
  1311. str += leftStr;
  1312. }
  1313. return str;
  1314. }
  1315. void VideoPlayerPrivate::writeCmdToMplayer(const char* cmd)
  1316. {
  1317. qDebug()<<"+++[VideoPlayerPrivate::writeCmdToMplayer:"<<cmd;
  1318. if(!m_VideoPlayerExitStatus){
  1319. if(m_Fd_w < 0)
  1320. {
  1321. m_Fd_w = open("/tmp/VideoPipe",O_WRONLY | O_NONBLOCK | O_TRUNC);
  1322. if(m_Fd_w < 0)
  1323. {
  1324. qDebug()<<"+++[VideoPlayerPrivate::writeCmdToMplayer:]+++";
  1325. qDebug()<<"video open fifo error";
  1326. m_ReadpThread = 0;
  1327. m_Process.clear();
  1328. return;
  1329. }
  1330. }
  1331. // ftruncate(m_Fd_w,0);
  1332. // lseek(m_Fd_w,0,SEEK_SET);
  1333. tcflush(m_Fd_w, TCIOFLUSH);
  1334. if(write(m_Fd_w,cmd,strlen(cmd)) == -1) {
  1335. perror("write");
  1336. }
  1337. }
  1338. return;
  1339. }
  1340. void VideoPlayerPrivate::quitMpalyer()
  1341. {
  1342. Q_Q(VideoPlayer);
  1343. qDebug()<<"+++[VideoPlayerPrivate::quitMpalyer()]+++";
  1344. const char* cmd = "quit\n";
  1345. writeCmdToMplayer(cmd);
  1346. if(m_ReadpThread != 0){
  1347. m_VideoPlayerExitStatus = true;
  1348. }
  1349. emit q->onVideoPlayerExit();
  1350. }
  1351. ssize_t VideoPlayerPrivate::readLine(int fd, char *buffer, size_t n)
  1352. {
  1353. ssize_t numRead;
  1354. size_t totRead;
  1355. char *buf;
  1356. char ch;
  1357. if (n <= 0 || buffer == NULL) {
  1358. errno = EINVAL;
  1359. return -1;
  1360. }
  1361. buf = buffer;
  1362. totRead = 0;
  1363. for (;;) {
  1364. int flags = fcntl(fd,F_GETFL,0);
  1365. flags |= O_NONBLOCK;
  1366. fcntl(fd,F_SETFL,flags);
  1367. numRead = read(fd, &ch, 1);
  1368. if (-1 == numRead) {
  1369. if (errno == EINTR) {
  1370. continue;
  1371. } else {
  1372. return -1;
  1373. }
  1374. } else if (numRead == 0) {
  1375. if (totRead == 0) {
  1376. return 0;
  1377. } else {
  1378. break;
  1379. }
  1380. } else {
  1381. if (totRead < n - 1) {
  1382. totRead++;
  1383. *buf++ = ch;
  1384. }
  1385. if (ch == '\n') {
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. *buf = '\0';
  1391. return totRead;
  1392. }
  1393. void VideoPlayerPrivate::onVideoPlayerVisible(bool visible)
  1394. {
  1395. Q_Q(VideoPlayer);
  1396. emit q->onVideoPlayerVisible(visible);
  1397. }
  1398. void VideoPlayerPrivate::sliderTouchEnable(bool enable)
  1399. {
  1400. Q_Q(VideoPlayer);
  1401. emit q->onSliderTouchEnable(enable);
  1402. }
  1403. void VideoPlayerPrivate::videoPlayerPlayStatus(const int type, const int status)
  1404. {
  1405. Q_Q(VideoPlayer);
  1406. emit q->onVideoPlayerPlayStatus(type, status);
  1407. }
  1408. int VideoPlayerPrivate::isRunning(int pid)
  1409. {
  1410. QString path = QString("/proc/") + QString::number(pid);
  1411. QDir dir(path);
  1412. if(dir.exists()){
  1413. return 1;
  1414. }
  1415. else{
  1416. return 0;
  1417. }
  1418. }
  1419. bool VideoPlayerPrivate::contains(const std::string& str, const std::string& substr)
  1420. {
  1421. return str.find(substr) != std::string::npos;
  1422. }
  1423. int VideoPlayerPrivate::isZombieProcess(int pid)
  1424. {
  1425. //printf("############%s_%d\n",__func__,__LINE__);
  1426. std::string filename;
  1427. std::string line;
  1428. int result = 0;
  1429. filename = string("/proc/") + std::to_string(pid) + string("/status");
  1430. // printf("############filename:%s\n",filename.c_str());
  1431. if (access(filename.data(), F_OK) == 0)
  1432. {
  1433. // printf("############%s_%d\n",__func__,__LINE__);
  1434. std::ifstream readStream;
  1435. readStream.open(filename);
  1436. if(readStream.is_open()){
  1437. while(getline(readStream, line)){
  1438. //std::cout<<line<<std::endl;
  1439. if(line.size() > 0){
  1440. if(contains(line,std::string("State:"))){
  1441. if(contains(line,std::string("zombie"))){//状态为zombie时,表示进程是僵尸进程
  1442. result = 1;
  1443. }
  1444. break;
  1445. }
  1446. }
  1447. line.clear();
  1448. }
  1449. readStream.close();
  1450. }
  1451. else {
  1452. std::cout << "Unable to open file"<<std::endl;;
  1453. }
  1454. }
  1455. return result;
  1456. }
  1457. void VideoPlayerPrivate::playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
  1458. {
  1459. Q_Q(VideoPlayer);
  1460. q->playVideoIndex(type,index, x, y,width,height,millesimal);
  1461. }
  1462. bool VideoPlayerPrivate::checkMemoryIntegrity() const
  1463. {
  1464. bool beforeOk = (m_guardBefore == GUARD_BEFORE_VALUE);
  1465. bool afterOk = (m_guardAfter == GUARD_AFTER_VALUE);
  1466. if (!beforeOk || !afterOk) {
  1467. logMemoryCorruption();
  1468. return false;
  1469. }
  1470. return true;
  1471. }
  1472. void VideoPlayerPrivate::logMemoryCorruption() const
  1473. {
  1474. qCritical() << "==================================================";
  1475. qCritical() << "MEMORY CORRUPTION DETECTED in VideoPlayerPrivate!";
  1476. qCritical() << "Time:" << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
  1477. qCritical() << "Guard Before:" << Qt::hex << m_guardBefore << "(expected:" << GUARD_BEFORE_VALUE << ")";
  1478. qCritical() << "PlayIndex:" << m_PlayIndex;
  1479. qCritical() << "Guard After:" << Qt::hex << m_guardAfter << "(expected:" << GUARD_AFTER_VALUE << ")";
  1480. qCritical() << "PlayMode:" << m_PlayMode << "PlayStatus:" << m_PlayStatus;
  1481. qCritical() << "DiskType:" << m_DiskType;
  1482. qCritical() << "==================================================";
  1483. // 可以在这里添加崩溃或恢复逻辑
  1484. // 比如保存日志、重启应用等
  1485. }