VideoPlayer.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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 "configUtils.h"
  29. using namespace std;
  30. static const string SDMultimedia("/data/MultiMediaFile/SDMultimedia");
  31. static const string SDVideoMillesmial("/data/MultiMediaFile/SDVideoMillesmial");
  32. static const string SDVideoPathInfo("/data/MultiMediaFile/SDVideoPathInfo");
  33. static const string USBMultimedia("/data/MultiMediaFile/USBMultimedia");
  34. static const string USBVideoMillesmial("/data/MultiMediaFile/USBVideoMillesmial");
  35. static const string USBVideoPathInfo("/data/MultiMediaFile/USBVideoPathInfo");
  36. /*
  37. *功能:获取当前的播放模式
  38. */
  39. static char* getPidFromStr(const char *str)
  40. {
  41. static char sPID[8] = {0};
  42. int tmp = 0;
  43. int pos1 = 0;
  44. int pos2 = 0;
  45. int i = 0;
  46. int j = 0;
  47. for (i=0; i<strlen(str); i++) {
  48. if ( (tmp==0) && (str[i]>='0' && str[i]<='9') ) {
  49. tmp = 1;
  50. pos1 = i;
  51. }
  52. if ( (tmp==1) && (str[i]<'0' || str[i]>'9') ) {
  53. pos2 = i;
  54. break;
  55. }
  56. }
  57. for (j=0,i=pos1; i<pos2; i++,j++) {
  58. sPID[j] = str[i];
  59. }
  60. return sPID;
  61. }
  62. int getCrrentvideoPlayMode()
  63. {
  64. QFile file("/etc/videoplaymode");
  65. if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  66. {
  67. qDebug()<<Q_FUNC_INFO<<"file :/etc/videoplaymode is open failed";
  68. }
  69. QTextStream in(&file);
  70. QString line = in.readLine();
  71. qDebug() << Q_FUNC_INFO<<"current video mode ="<<line.toInt();
  72. file.flush();
  73. file.close();
  74. if(line.isEmpty()){
  75. return 0;
  76. }
  77. return line.toInt();
  78. }
  79. class VideoPlayerPrivate
  80. {
  81. Q_DISABLE_COPY(VideoPlayerPrivate)
  82. public:
  83. enum CommandType {
  84. CT_Undefine = -1,
  85. CT_PlayIndex = 0,
  86. CT_PlayNext = 1,
  87. CT_PlayPrevious = 2,
  88. CT_Pause = 3,
  89. CT_Seek = 4,
  90. CT_Toggle = 5,
  91. };
  92. explicit VideoPlayerPrivate(VideoPlayer *parent);
  93. ~VideoPlayerPrivate();
  94. void initialize();
  95. void connectAllSlots();
  96. void setPlayMode(const VideoPlayerPlayMode mode);
  97. void setPlayStatus(const VideoPlayerPlayStatus status);
  98. void pauseToggleHandler(const QString& output);
  99. void startHandler(const QString& output);
  100. void endTimePositionHandler(const QString &output);
  101. void millesimalHandler(const QString& output);
  102. void timePositionHandler(const QString &output);
  103. QList<QString>& getPathList(const DeviceWatcherType type);
  104. void endOfFileHandler();
  105. void unsupportHandler();
  106. void undragAndDropHandler();//add by wandz 20190425
  107. void seekToMillesimal(const int millesimal);
  108. void createSDFileNamesXml();
  109. void createUSBFileNamesXml();
  110. void videoPlayerSetPlay();
  111. void videoPlayerSetSuspend();
  112. void videoPlayerSetPause();
  113. void videoPlayerSetExit(const DeviceWatcherType type);
  114. void exitVideoPlayer();
  115. void videoPlayerVisible(const bool flag);
  116. int isRunning(char *process);
  117. QString chgform(QString name);
  118. void writeCmdToMplayer(char* cmd);
  119. void quitMpalyer();
  120. void onVideoPlayerVisible(bool visible);
  121. void sliderTouchEnable(bool enable);
  122. void videoPlayerPlayStatus(const int type, const int status);
  123. ssize_t readLine(int fd, char *buffer, size_t n);
  124. VideoPlayerPlayMode m_PlayMode;
  125. VideoPlayerPlayStatus m_PlayStatus;
  126. QString m_SDFileNamesXml;
  127. QString m_USBFileNamesXml;
  128. QList<QString> m_SDPathList;
  129. QList<QString> m_USBPathList;
  130. QProcess* m_VideoPlayer;
  131. DeviceWatcherType m_DiskType;
  132. int m_PlayIndex;
  133. int m_ElapsedTime;
  134. int m_Millesimal;
  135. int m_EndTime;
  136. int m_X;
  137. int m_Y;
  138. int m_Width;
  139. int m_Height;
  140. bool m_Suspend;
  141. VideoPlayerPrivate::CommandType m_LastCommand;
  142. int m_ReadpThread;
  143. int fd[2];
  144. int m_Fd_w;
  145. pid_t m_pid1;
  146. pid_t m_pid2;
  147. QString m_Process;
  148. private:
  149. Q_DECLARE_PUBLIC(VideoPlayer)
  150. VideoPlayer* const q_ptr;
  151. };
  152. static void* quitHanlder(void* arg)
  153. {
  154. VideoPlayerPrivate* pThis = (VideoPlayerPrivate*)arg;
  155. char info[512] = {0};
  156. int ret = 0;
  157. while (1) {
  158. if(pThis->m_ReadpThread == 0)
  159. {
  160. qDebug()<<"+++[VideoPlayer::quitHanlder:pThis->m_ReadpThread]+++"<<pThis->m_ReadpThread;
  161. break;//退出线程
  162. }
  163. bzero(info, 512);
  164. ret = pThis->readLine(pThis->fd[0], info, 512);
  165. while(ret > 0)
  166. {
  167. string str(info);
  168. QString output = QString::fromStdString(str);
  169. //qDebug()<<"=== mplayer video:"<<output;
  170. if (output.contains(QString("ANS_LENGTH="))) {
  171. pThis->endTimePositionHandler(output);
  172. } else if (output.contains(QString("ANS_PERCENT_POSITION="))) {
  173. pThis->millesimalHandler(output);
  174. } else if (output.contains(QString("ANS_TIME_POSITION="))) {
  175. pThis->timePositionHandler(output);
  176. } else if (output.contains(QString("ANS_pause"))) {
  177. pThis->pauseToggleHandler(output);
  178. } else if (output.contains(QString("EOF code: 1"))) {
  179. pThis->setPlayStatus(VPPS_Stop);
  180. sleep(2);
  181. pThis->endOfFileHandler();
  182. }
  183. else if (output.contains(QString("give up video"))) {
  184. pThis->onVideoPlayerVisible(false);
  185. } else if (output.contains(QString("take video"))) {
  186. pThis->onVideoPlayerVisible(true);
  187. } else if(output.contains(QString("Cannot seek in this file"))){
  188. qDebug()<<__PRETTY_FUNCTION__<<__LINE__<<"seek failed";
  189. pThis->sliderTouchEnable(false);//dll add 不可拖动的视频禁止拖动
  190. pThis->undragAndDropHandler();
  191. }else if ((output.contains(QString("incorrect streams")))//wandz video
  192. || (output.contains(QString("No stream found")))
  193. || (output.contains(QString("Cannot find codec matching selected -vo and video format")))
  194. || (output.contains(QString("Maybe you are playing a non-interleaved stream/file or the codec failed")))
  195. ) {
  196. sleep(2);
  197. pThis->unsupportHandler();
  198. }/* else if (output.contains(QString("seek-begin"))) {
  199. } */else if ((output.contains(QString("seek-finish"))
  200. || (output.contains(QString("seek-error"))))) {
  201. pThis->videoPlayerPlayStatus(pThis->m_DiskType, VPPS_SeekFinish);
  202. }
  203. else if((output.contains(QString("Exiting... (Fatal error)"))))
  204. {
  205. pThis->videoPlayerPlayStatus(pThis->m_DiskType, VPPS_PlayError);
  206. }
  207. else {
  208. if (output != QString("\n")) {
  209. qDebug() << "output" << output;
  210. }
  211. }
  212. bzero(info, 512);
  213. ret = pThis->readLine(pThis->fd[0], info, 512);
  214. }
  215. if(ret <= 0){
  216. sleep(1);
  217. }
  218. }
  219. if(pThis->m_Fd_w > 0)
  220. {
  221. close(pThis->m_Fd_w);
  222. pThis->m_Fd_w = -1 ;
  223. }
  224. close(pThis->fd[0]);
  225. pThis->m_pid2 = wait(NULL);//回收mpalyer的进程号,避免mpalyer成为僵尸进程
  226. printf("*********VideoPlayer_pid2= %d \n",pThis->m_pid2);//wait的返回值为子进程的pid
  227. pThis->m_Process.clear();
  228. return NULL;
  229. }
  230. void VideoPlayer::videoPlayerRequestFileNames(const DeviceWatcherType type)
  231. {
  232. Q_D(VideoPlayer);
  233. switch (type) {
  234. case DWT_SDDisk: {
  235. emit onVideoPlayerFileNames(DWT_SDDisk, d->m_SDFileNamesXml);
  236. break;
  237. }
  238. case DWT_USBDisk: {
  239. emit onVideoPlayerFileNames(DWT_USBDisk, d->m_USBFileNamesXml);
  240. break;
  241. }
  242. default: {
  243. break;
  244. }
  245. }
  246. }
  247. void VideoPlayer::videoPlayerSetPlayModeToggle()
  248. {
  249. Q_D(VideoPlayer);
  250. switch (d->m_PlayMode) {
  251. case VPPM_AllRepeat: {
  252. videoPlayerSetPlayMode(VPPM_Shuffle);
  253. break;
  254. }
  255. case VPPM_Shuffle: {
  256. videoPlayerSetPlayMode(VPPM_RepeatOnce);
  257. break;
  258. }
  259. case VPPM_RepeatOnce:
  260. default: {
  261. videoPlayerSetPlayMode(VPPM_AllRepeat);
  262. break;
  263. }
  264. }
  265. }
  266. void VideoPlayer::videoPlayerSetPlayMode(const VideoPlayerPlayMode mode)
  267. {
  268. Q_D(VideoPlayer);
  269. d->setPlayMode(mode);
  270. }
  271. void VideoPlayer::videoPlayerSetPlayStatusToggle()
  272. {
  273. Q_D(VideoPlayer);
  274. switch (d->m_PlayStatus) {
  275. case VPPS_Play: {
  276. videoPlayerSetPlayStatus(VPPS_Pause);
  277. break;
  278. }
  279. case VPPS_Pause: {
  280. videoPlayerSetPlayStatus(VPPS_Play);
  281. break;
  282. }
  283. case VPPS_Stop: {
  284. videoPlayerSetPlayStatus(VPPS_Stop);
  285. }
  286. case VPPS_Unsupport: {
  287. playVideoIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  288. break;
  289. }
  290. default: {
  291. break;
  292. }
  293. }
  294. }
  295. void VideoPlayer::videoPlayerSetPlayStatus(const VideoPlayerPlayStatus status)
  296. {
  297. Q_D(VideoPlayer);
  298. if(d->m_Process.size() != 0){
  299. QByteArray processArray = d->m_Process.toLatin1();
  300. char *process = processArray.data();
  301. if(d->isRunning(process)){
  302. if (VideoPlayerPrivate::CT_Undefine == d->m_LastCommand) {
  303. switch (status) {
  304. case VPPS_Play: {
  305. d->videoPlayerSetPlay();
  306. break;
  307. }
  308. case VPPS_Pause: {
  309. d->videoPlayerSetPause();
  310. break;
  311. }
  312. case VPPS_SuspendToggle: {
  313. d->videoPlayerSetSuspend();
  314. break;
  315. }
  316. case VPPS_Stop: {
  317. videoPlayerPlayListViewIndex(d->m_DiskType, d->m_PlayIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  318. break;
  319. }
  320. default : {
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. }
  328. void VideoPlayer::videoPlayerPlayListViewIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
  329. {
  330. Q_D(VideoPlayer);
  331. if (VideoPlayerPrivate::CT_Undefine == d->m_LastCommand) {
  332. playVideoIndex(type, index, x, y, width, height, millesimal);
  333. }
  334. }
  335. void VideoPlayer::videoPlayerPlayPreviousListViewIndex()
  336. {
  337. Q_D(VideoPlayer);
  338. if (VideoPlayerPrivate::CT_Undefine == d->m_LastCommand) {
  339. int lastIndex;
  340. if (MPPM_Shuffle == d->m_PlayMode) {
  341. lastIndex = qrand() % d->getPathList(d->m_DiskType).size();
  342. } else {
  343. lastIndex = d->m_PlayIndex;
  344. QList<QString> temp = d->getPathList(d->m_DiskType);
  345. if ((lastIndex > 0)
  346. && (lastIndex <= temp.size() - 1)) {
  347. --lastIndex;
  348. } else {
  349. lastIndex = temp.size() - 1;
  350. }
  351. }
  352. videoPlayerPlayListViewIndex(d->m_DiskType, lastIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  353. }
  354. }
  355. void VideoPlayer::videoPlayerPlayNextListViewIndex()
  356. {
  357. Q_D(VideoPlayer);
  358. if (VideoPlayerPrivate::CT_Undefine == d->m_LastCommand) {
  359. int lastIndex;
  360. if (MPPM_Shuffle == d->m_PlayMode) {
  361. lastIndex = qrand() % d->getPathList(d->m_DiskType).size();
  362. } else {
  363. lastIndex = d->m_PlayIndex;
  364. QList<QString> temp = d->getPathList(d->m_DiskType);
  365. if (((lastIndex < (temp.size() - 1)))
  366. && (lastIndex >= 0)) {
  367. ++lastIndex;
  368. } else {
  369. lastIndex = 0;
  370. }
  371. }
  372. videoPlayerPlayListViewIndex(d->m_DiskType, lastIndex, d->m_X, d->m_Y, d->m_Width, d->m_Height, 0);
  373. }
  374. }
  375. void VideoPlayer::videoPlayerSeekToMillesimal(const int millesimal)
  376. {
  377. Q_D(VideoPlayer);
  378. if(d->m_Process.size() != 0){
  379. QByteArray processArray = d->m_Process.toLatin1();
  380. char *process = processArray.data();
  381. if(d->isRunning(process)){
  382. if (VideoPlayerPrivate::CT_Undefine == d->m_LastCommand) {
  383. d->seekToMillesimal(millesimal);
  384. }
  385. }
  386. }
  387. }
  388. void VideoPlayer::videoPlayerExit()
  389. {
  390. Q_D(VideoPlayer);
  391. d->videoPlayerSetExit(d->m_DiskType);
  392. }
  393. void VideoPlayer::videoPlayerVisible(const bool flag)
  394. {
  395. Q_D(VideoPlayer);
  396. if(d->m_Process.size() != 0){
  397. QByteArray processArray = d->m_Process.toLatin1();
  398. char *process = processArray.data();
  399. if(d->isRunning(process)){
  400. d->videoPlayerVisible(flag);
  401. }
  402. }
  403. }
  404. void VideoPlayer::onDeviceWatcherStatus(const DeviceWatcherType type, const DeviceWatcherStatus status)
  405. {
  406. Q_D(VideoPlayer);
  407. if (DWT_SDDisk == type) {
  408. switch (status) {
  409. case DWS_Empty: {
  410. d->m_SDPathList.clear();
  411. break;
  412. }
  413. case DWS_Busy: {
  414. d->m_SDPathList.clear();
  415. break;
  416. }
  417. case DWS_Ready: {
  418. d->createSDFileNamesXml();
  419. videoPlayerRequestFileNames(DWT_SDDisk);
  420. break;
  421. }
  422. case DWS_Remove: {
  423. d->m_SDPathList.clear();
  424. if (DWT_SDDisk == d->m_DiskType) {
  425. if(d->m_Process.size() != 0){
  426. QByteArray processArray = d->m_Process.toLatin1();
  427. char *process = processArray.data();
  428. if(d->isRunning(process)){
  429. d->quitMpalyer();
  430. }
  431. }
  432. d->setPlayStatus(VPPS_Exit);
  433. }
  434. break;
  435. }
  436. default : {
  437. break;
  438. }
  439. }
  440. } else if (DWT_USBDisk == type) {
  441. switch (status) {
  442. case DWS_Empty: {
  443. d->m_USBPathList.clear();
  444. break;
  445. }
  446. case DWS_Busy: {
  447. d->m_USBPathList.clear();
  448. break;
  449. }
  450. case DWS_Ready: {
  451. d->createUSBFileNamesXml();
  452. videoPlayerRequestFileNames(DWT_USBDisk);
  453. break;
  454. }
  455. case DWS_Remove: {
  456. d->m_USBPathList.clear();
  457. if (DWT_USBDisk == d->m_DiskType) {
  458. if(d->m_Process.size() != 0){
  459. QByteArray processArray = d->m_Process.toLatin1();
  460. char *process = processArray.data();
  461. if(d->isRunning(process)){
  462. d->quitMpalyer();
  463. }
  464. }
  465. d->setPlayStatus(VPPS_Exit);
  466. }
  467. }
  468. default : {
  469. break;
  470. }
  471. }
  472. }
  473. }
  474. void VideoPlayer::onVideoFilePath(const QString &path, const DeviceWatcherType type)
  475. {
  476. Q_D(VideoPlayer);
  477. if (type == DWT_USBDisk) {
  478. d->m_USBPathList.append(path);
  479. } else if (type == DWT_SDDisk) {
  480. d->m_SDPathList.append(path);
  481. }
  482. // emit onVideoPlayerFilePath(type, path);
  483. }
  484. VideoPlayer::VideoPlayer(QObject *parent)
  485. : QObject(parent)
  486. , d_ptr(new VideoPlayerPrivate(this))
  487. {
  488. }
  489. VideoPlayer::~VideoPlayer()
  490. {
  491. }
  492. VideoPlayerPrivate::VideoPlayerPrivate(VideoPlayer *parent)
  493. : q_ptr(parent)
  494. {
  495. // m_PlayMode = VPPM_AllRepeat;
  496. m_PlayMode = getCrrentvideoPlayMode();
  497. m_PlayStatus = VPPS_Exit;
  498. m_VideoPlayer = NULL;
  499. m_DiskType = DWT_Undefine;
  500. m_PlayIndex = -1;
  501. m_ElapsedTime = -1;
  502. m_Millesimal = 0;
  503. m_EndTime = -1;
  504. m_X = 0;
  505. m_Y = 0;
  506. m_Width = 800;
  507. m_Height = 480;
  508. m_Suspend = false;
  509. m_LastCommand = VideoPlayerPrivate::CT_Undefine;
  510. m_ReadpThread = 1;
  511. m_Fd_w = -1;
  512. m_Process.clear();
  513. initialize();
  514. connectAllSlots();
  515. }
  516. VideoPlayerPrivate::~VideoPlayerPrivate()
  517. {
  518. Q_Q(VideoPlayer);
  519. if(m_Process.size() != 0){
  520. QByteArray processArray = m_Process.toLatin1();
  521. char *process = processArray.data();
  522. if(isRunning(process)){
  523. quitMpalyer();
  524. }
  525. }
  526. }
  527. void VideoPlayerPrivate::initialize()
  528. {
  529. Q_Q(VideoPlayer);
  530. m_VideoPlayer = new QProcess(q);
  531. m_VideoPlayer->setProcessChannelMode(QProcess::MergedChannels);
  532. }
  533. void VideoPlayerPrivate::connectAllSlots()
  534. {
  535. Q_Q(VideoPlayer);
  536. connectSignalAndSlotByNamesake(g_DeviceWatcher, q, SLOT(onDeviceWatcherStatus(const int, const int)));
  537. connectSignalAndSlotByNamesake(g_DeviceWatcher, q, SLOT(onVideoFilePath(const QString &, const int)));
  538. }
  539. void VideoPlayer::videoPlayerSetGeometry(int x,int y,int width,int height)
  540. {
  541. //不行 花屏
  542. // Q_D(VideoPlayer);
  543. // d->m_Width = width;
  544. // d->m_Height = height;
  545. // d->m_X = x ;
  546. // d->m_Y = y;
  547. // QString _GeometryStr = QString(QString::number(d->m_Width)
  548. // + QString("x")
  549. // + QString::number(d->m_Height)
  550. // + QString("+")
  551. // + QString::number(d->m_X)
  552. // + QString("+")
  553. // + QString::number(d->m_Y));
  554. // QByteArray _GeometryByte = _GeometryStr.toLocal8Bit();
  555. // const char* _Geometry = _GeometryByte.data();
  556. // char _GeometryCmd[512] = {0};
  557. // strcat(_GeometryCmd, "change_geometry ");
  558. // strcat(_GeometryCmd, _Geometry);
  559. // strcat(_GeometryCmd, "\n");
  560. // d->writeCmdToMplayer(_GeometryCmd);
  561. }
  562. void VideoPlayer::playVideoIndex(const DeviceWatcherType type, const int index, const int x, const int y, const int width, const int height, const int millesimal)
  563. {
  564. Q_D(VideoPlayer);
  565. QList<QString> temp = d->getPathList(type);
  566. if ((VideoPlayerPrivate::CT_Undefine == d->m_LastCommand)
  567. && (temp.size() > index)
  568. && (QFile(temp.at(index))).exists()) {
  569. d->m_DiskType = type;
  570. d->m_ElapsedTime = 0;
  571. d->m_EndTime = 0;
  572. d->m_PlayIndex = index;
  573. d->m_Suspend = false;
  574. d->m_Millesimal = millesimal;
  575. d->m_X = x;
  576. d->m_Y = y;
  577. d->m_Width = width;
  578. d->m_Height = height;
  579. bool _ProcessIsRunning(false);
  580. if (0 != millesimal) {
  581. if(d->m_Process.size() != 0){
  582. QByteArray processArray = d->m_Process.toLatin1();
  583. char *process = processArray.data();
  584. if(d->isRunning(process)){
  585. d->quitMpalyer();
  586. usleep(500*1000);
  587. }
  588. }
  589. }
  590. d->m_DiskType = type;
  591. if(d->m_Process.size() != 0){
  592. QByteArray processArray = d->m_Process.toLatin1();
  593. char *process = processArray.data();
  594. sleep(1);
  595. if(!d->isRunning(process)){
  596. _ProcessIsRunning = true;
  597. }
  598. }
  599. if(d->m_Process.size() == 0)
  600. {
  601. _ProcessIsRunning = true;
  602. }
  603. if (_ProcessIsRunning){
  604. if(access("/data/VideoPipe",F_OK) != 0)
  605. {
  606. printf("VideoPipe exist\n");
  607. mkfifo("/data/VideoPipe",0777);
  608. }
  609. else{
  610. unlink("/data/VideoPipe");
  611. mkfifo("/data/VideoPipe", 0777);
  612. }
  613. if(pipe(d->fd) < 0){
  614. perror("pipe error\n");
  615. return;
  616. }
  617. fcntl(d->fd[0], F_SETFL, O_NONBLOCK); //无名管道设为非阻塞
  618. d->m_pid1 = vfork();
  619. printf("+++++++++0000video_pid++++++++\n");
  620. if(d->m_pid1 < 0){
  621. perror("fork error\n");
  622. }
  623. else if(d->m_pid1 == 0)
  624. {
  625. printf("child:video0000%d\n",getpid());
  626. close(d->fd[0]);
  627. dup2(d->fd[1], STDERR_FILENO);
  628. QByteArray _NameByte = temp.at(d->m_PlayIndex).toLocal8Bit();
  629. const char* _Name = _NameByte.data();
  630. QString _GeometryStr = QString(QString::number(d->m_Width)
  631. + QString("x")
  632. + QString::number(d->m_Height)
  633. + QString("+")
  634. + QString::number(d->m_X)
  635. + QString("+")
  636. + QString::number(d->m_Y));
  637. QByteArray _GeometryByte = _GeometryStr.toLocal8Bit();
  638. const char* _Geometry = _GeometryByte.data();
  639. if(millesimal == 0){
  640. execlp("/usr/bin/mplayer", "mplayer", "-slave", "-quiet", "-idle","-geometry",_Geometry,
  641. "-vo", "customfb","-ao","alsa","-input", "file=/data/VideoPipe", _Name,NULL); //命名管道pipe有命令数据,mplayer会自动读取
  642. }
  643. else{
  644. const char* _ChMillesimal = std::to_string(millesimal).c_str();
  645. execlp("/usr/bin/mplayer", "mplayer", "-slave", "-quiet", "-idle","-geometry",_Geometry,
  646. "-vo", "customfb","-ao","alsa","-input", "file=/data/VideoPipe","-ss",_ChMillesimal, _Name,NULL); //命名管道pipe有命令数据,mplayer会自动读取
  647. }
  648. }
  649. else{
  650. //切換视频时解开进度条拖动..防止不可拖动的视频也能拖动
  651. emit onSliderTouchEnable(true);
  652. emit onVideoPlayerElapsedInformation(d->m_ElapsedTime, d->m_Millesimal);
  653. emit onVideoPlayerInformation(d->m_DiskType,d->m_PlayIndex, QFileInfo(temp.at(d->m_PlayIndex)).fileName(), d->m_EndTime);
  654. emit onVideoPlayerPlayMode(d->m_PlayMode);
  655. d->setPlayStatus(VPPS_Start);
  656. QFileInfo fileInfo(temp.at(d->m_PlayIndex));
  657. QString filePath = fileInfo.filePath() + QString("/") +
  658. fileInfo.created().toString(QString("yyyyMMddhhmmss")) +
  659. fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) +
  660. QString::number(fileInfo.size());
  661. int value = MT_Video;
  662. if(d->m_DiskType == DWT_USBDisk){
  663. SaveConfigString(filePath.toStdString(),USBVideoPathInfo);
  664. system("sync");
  665. SaveConfigString(value,USBMultimedia);
  666. system("sync");
  667. }
  668. else if(d->m_DiskType == DWT_SDDisk){
  669. SaveConfigString(filePath.toStdString(),SDVideoPathInfo);
  670. system("sync");
  671. SaveConfigString(value,SDMultimedia);
  672. system("sync");
  673. }
  674. d->m_Process = QString("mplayer");
  675. d->m_ReadpThread = 1;
  676. close(d->fd[1]);
  677. usleep(200*1000);
  678. pthread_t pthead;
  679. int ret = pthread_create(&pthead, NULL,quitHanlder,d);
  680. if(ret != 0) {
  681. printf("pthread_create failed!\n");
  682. }
  683. }
  684. }
  685. else{
  686. char pathname[512] = {0};
  687. strcat(pathname, "loadfile ");
  688. QString _FilePath = d->chgform(temp.at(d->m_PlayIndex));
  689. QByteArray _NameByte = _FilePath.toLocal8Bit();
  690. char* _Name = _NameByte.data();
  691. strcat(pathname, _Name);
  692. strcat(pathname, "\n");
  693. printf("+++++++++++++pathname:%s", pathname);
  694. if(d->m_Fd_w < 0)
  695. {
  696. d->m_Fd_w = open("/data/VideoPipe", O_WRONLY | O_NONBLOCK | O_TRUNC);
  697. if(d->m_Fd_w < 0){
  698. perror("open file error");
  699. d->m_ReadpThread = 0;
  700. d->m_Process.clear();
  701. return;
  702. }
  703. }
  704. ftruncate(d->m_Fd_w,0);
  705. lseek(d->m_Fd_w,0,SEEK_SET);
  706. write(d->m_Fd_w, pathname, strlen(pathname));
  707. //切換视频时解开进度条拖动..防止不可拖动的视频也能拖动
  708. emit onSliderTouchEnable(true);
  709. d->setPlayStatus(VPPS_Start);
  710. emit onVideoPlayerElapsedInformation(d->m_ElapsedTime, d->m_Millesimal);
  711. emit onVideoPlayerInformation(d->m_DiskType,d->m_PlayIndex, QFileInfo(temp.at(d->m_PlayIndex)).fileName(), d->m_EndTime);
  712. emit onVideoPlayerPlayMode(d->m_PlayMode);
  713. QFileInfo fileInfo(temp.at(d->m_PlayIndex));
  714. QString filePath = fileInfo.filePath() + QString("/") +
  715. fileInfo.created().toString(QString("yyyyMMddhhmmss")) +
  716. fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) +
  717. QString::number(fileInfo.size());
  718. int value = MT_Video;
  719. if(d->m_DiskType == DWT_USBDisk){
  720. SaveConfigString(filePath.toStdString(),USBVideoPathInfo);
  721. system("sync");
  722. SaveConfigString(value,USBMultimedia);
  723. system("sync");
  724. }
  725. else if(d->m_DiskType == DWT_SDDisk){
  726. SaveConfigString(filePath.toStdString(),SDVideoPathInfo);
  727. system("sync");
  728. SaveConfigString(value,SDMultimedia);
  729. system("sync");
  730. }
  731. }
  732. }
  733. }
  734. void VideoPlayerPrivate::setPlayMode(const VideoPlayerPlayMode mode)
  735. {
  736. Q_Q(VideoPlayer);
  737. if (mode != m_PlayMode) {
  738. m_PlayMode = mode;
  739. }
  740. emit q->onVideoPlayerPlayMode(m_PlayMode);
  741. }
  742. void VideoPlayerPrivate::setPlayStatus(const VideoPlayerPlayStatus status)
  743. {
  744. Q_Q(VideoPlayer);
  745. if (status != m_PlayStatus) {
  746. m_PlayStatus = status;
  747. emit q->onVideoPlayerPlayStatus(m_DiskType, status);
  748. }
  749. }
  750. void VideoPlayerPrivate::pauseToggleHandler(const QString &output)
  751. {
  752. if (output.contains(QString("yes"))) {
  753. setPlayStatus(VPPS_Pause);
  754. } else if (output.contains(QString("no"))) {
  755. setPlayStatus(VPPS_Play);
  756. if (VideoPlayerPrivate::CT_Seek == m_LastCommand) {
  757. }
  758. }
  759. }
  760. void VideoPlayerPrivate::startHandler(const QString &output)
  761. {
  762. }
  763. void VideoPlayerPrivate::endTimePositionHandler(const QString &output)
  764. {
  765. Q_Q(VideoPlayer);
  766. QString keyWord("ANS_LENGTH=");
  767. int startPos = output.indexOf(keyWord);
  768. int length = keyWord.length();
  769. QString endTime;
  770. endTime.clear();
  771. QList<QString> temp = getPathList(m_DiskType);
  772. if (temp.size() > m_PlayIndex) {
  773. m_EndTime = 0;
  774. if (-1 != startPos) {
  775. for (int i = 0; i < (output.length() - startPos); ++i) {
  776. if (QChar('\n') == output.at(startPos + length + i)) {
  777. m_EndTime = static_cast<int>(endTime.toFloat());
  778. break;
  779. }
  780. endTime += output.at(startPos + length + i);
  781. }
  782. }
  783. //qDebug()<<"+++++++endTimePositionHandler+++++++++++";
  784. emit q->onVideoPlayerInformation(m_DiskType, m_PlayIndex, QFileInfo(temp.at(m_PlayIndex)).fileName(), m_EndTime);
  785. setPlayStatus(VPPS_Play);
  786. } else {
  787. qCritical() << temp.size() << m_PlayIndex;
  788. }
  789. }
  790. void VideoPlayerPrivate::millesimalHandler(const QString &output)
  791. {
  792. int start = output.indexOf(QChar('=')) + 1;
  793. int end = output.indexOf(QChar('\n'));
  794. if ((-1 != start)
  795. && (-1 != end)) {
  796. bool ok(false);
  797. int millesimal = static_cast<int>(output.mid(start, end - start).toFloat(&ok) * 1000);
  798. if (ok) {
  799. m_Millesimal = millesimal;
  800. }
  801. }
  802. }
  803. void VideoPlayerPrivate::timePositionHandler(const QString &output)
  804. {
  805. Q_Q(VideoPlayer);
  806. if (output.startsWith(QString("ANS_TIME_POSITION="), Qt::CaseSensitive)) {
  807. int start = output.indexOf(QChar('=')) + 1;
  808. int end = output.indexOf(QChar('\n'));
  809. if ((-1 != start)
  810. && (-1 != end)) {
  811. bool ok = true;
  812. int elapsed = output.mid(start, end - start).toInt(&ok);
  813. if (ok) {
  814. if (((qAbs(m_ElapsedTime - elapsed) > 5))
  815. || (elapsed % 5)) {
  816. if(m_DiskType == DWT_USBDisk){
  817. SaveConfigString(elapsed,USBVideoMillesmial);
  818. system("sync");
  819. }
  820. else if(m_DiskType == DWT_SDDisk){
  821. SaveConfigString(elapsed,SDVideoMillesmial);
  822. system("sync");
  823. }
  824. }
  825. m_ElapsedTime = elapsed;
  826. emit q->onVideoPlayerElapsedInformation(m_ElapsedTime, m_Millesimal);
  827. }
  828. }
  829. }
  830. }
  831. QList<QString> &VideoPlayerPrivate::getPathList(const int type)
  832. {
  833. switch (type) {
  834. case DWT_SDDisk: {
  835. return m_SDPathList;
  836. break;
  837. }
  838. case DWT_USBDisk:
  839. default: {
  840. return m_USBPathList;
  841. break;
  842. }
  843. }
  844. }
  845. void VideoPlayerPrivate::endOfFileHandler()
  846. {
  847. Q_Q(VideoPlayer);
  848. switch (m_PlayStatus) {
  849. case VPPS_Start:
  850. case VPPS_Unsupport:
  851. case VPPS_Pause:
  852. case VPPS_Stop:
  853. case VPPS_Play: {
  854. switch (m_PlayMode) {
  855. case VPPM_AllRepeat: {
  856. if ((getPathList(m_DiskType).size() - 1) > m_PlayIndex) {
  857. ++m_PlayIndex;
  858. } else {
  859. m_PlayIndex = 0;
  860. }
  861. break;
  862. }
  863. case VPPM_RepeatOnce: {
  864. break;
  865. }
  866. case VPPM_Shuffle:
  867. default : {
  868. m_PlayIndex = qrand() % getPathList(m_DiskType).size();
  869. break;
  870. }
  871. }
  872. q->playVideoIndex(m_DiskType, m_PlayIndex, m_X, m_Y, m_Width, m_Height, 0);
  873. break;
  874. }
  875. default: {
  876. break;
  877. }
  878. }
  879. }
  880. void VideoPlayerPrivate::unsupportHandler()
  881. {
  882. Q_Q(VideoPlayer);
  883. QList<QString> temp = getPathList(m_DiskType);
  884. if (m_PlayIndex < temp.size()) {
  885. m_ElapsedTime = 0;
  886. m_Millesimal = 0;
  887. emit q->onVideoPlayerElapsedInformation(m_ElapsedTime, m_Millesimal);
  888. m_EndTime = 0;
  889. emit q->onVideoPlayerInformation(m_DiskType, m_PlayIndex, QFileInfo(temp.at(m_PlayIndex)).fileName(), m_EndTime);
  890. setPlayStatus(VPPS_Unsupport);
  891. endOfFileHandler();
  892. }
  893. }
  894. //handler for can't drag and drop for video,from wandz at 20190425
  895. void VideoPlayerPrivate::undragAndDropHandler()
  896. {
  897. setPlayStatus(VPPS_UndragAndDrop);
  898. }
  899. void VideoPlayerPrivate::seekToMillesimal(const int millesimal)
  900. {
  901. Q_Q(VideoPlayer);
  902. if ((VPPS_Pause == m_PlayStatus)
  903. || (VPPS_Play == m_PlayStatus)) {
  904. QString cmd("seek %1.%2 1\n");
  905. cmd = cmd.arg(millesimal / 10, 3, 10, QChar(' ')).arg(millesimal % 10, 1, 10, QChar('0'));
  906. writeCmdToMplayer(cmd.toLocal8Bit().data());
  907. } else {
  908. q->playVideoIndex(m_DiskType, m_PlayIndex, m_X, m_Y, m_Width, m_Height, 0);
  909. }
  910. }
  911. void VideoPlayerPrivate::createSDFileNamesXml()
  912. {
  913. QDomDocument domDocument;
  914. domDocument.clear();
  915. domDocument.appendChild(domDocument.createElement(QString("VideoPlayer")));
  916. QDomElement root = domDocument.firstChildElement(QString("VideoPlayer"));
  917. QDomElement fileNames;
  918. QDomElement info;
  919. fileNames = domDocument.createElement(QString("SDFileNames"));
  920. root.appendChild(fileNames);
  921. m_SDFileNamesXml.clear();
  922. string pathInfo;
  923. LoadConfigString(pathInfo,SDVideoPathInfo);
  924. QString sdPersistantPathInfo = QString::fromStdString(pathInfo);
  925. QStringList stringList = sdPersistantPathInfo.split(QChar('/'));
  926. QString sdPersistantPath = sdPersistantPathInfo.left(sdPersistantPathInfo.length() - stringList.last().length() - 1);
  927. QString persistantIndex("");
  928. QFileInfo fileInfo;
  929. int i;
  930. for (i = 0; i < m_SDPathList.size(); ++i) {
  931. info = domDocument.createElement(QString("Index:" + QString::number(i)));
  932. fileNames.appendChild(info);
  933. fileInfo.setFile(m_SDPathList.at(i));
  934. info.appendChild(domDocument.createTextNode(fileInfo.fileName()));
  935. if (persistantIndex.isEmpty()) {
  936. if (fileInfo.filePath() == sdPersistantPath) {
  937. if (sdPersistantPathInfo == (fileInfo.filePath() + QString("/") + fileInfo.created().toString(QString("yyyyMMddhhmmss")) + fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) + QString::number(fileInfo.size()))) {
  938. persistantIndex = QString::number(i);
  939. }
  940. }
  941. }
  942. }
  943. QDomElement persistant = domDocument.createElement(QString("SDPersistant"));
  944. root.appendChild(persistant);
  945. int mediaType = 0;
  946. LoadConfigString(mediaType,SDMultimedia);
  947. unsigned char multimediaType = (unsigned char)mediaType;
  948. int videoMillesmial = 0;
  949. if (persistantIndex.isEmpty()) {
  950. if (0 == i) {
  951. persistantIndex = QString("-1");
  952. multimediaType = MT_Idle;
  953. } else {
  954. if (0 == multimediaType) {
  955. multimediaType = MT_Video;
  956. SaveConfigString(multimediaType,SDMultimedia);
  957. system("sync");
  958. }
  959. persistantIndex = QString("0");
  960. }
  961. } else {
  962. LoadConfigString(videoMillesmial,SDVideoMillesmial);
  963. }
  964. QString data = persistantIndex + QString("-") + QString::number(videoMillesmial);
  965. //qDebug()<<__PRETTY_FUNCTION__<<"line:"<<__LINE__<<"sd videoMillesmia"<<data;
  966. persistant.appendChild(domDocument.createTextNode(data));
  967. QDomElement type = domDocument.createElement(QString("SDType"));
  968. root.appendChild(type);
  969. type.appendChild(domDocument.createTextNode(QString::number(multimediaType)));
  970. m_SDFileNamesXml = domDocument.toString();
  971. }
  972. void VideoPlayerPrivate::createUSBFileNamesXml()
  973. {
  974. QDomDocument domDocument;
  975. domDocument.clear();
  976. domDocument.appendChild(domDocument.createElement(QString("VideoPlayer")));
  977. QDomElement root = domDocument.firstChildElement(QString("VideoPlayer"));
  978. QDomElement fileNames;
  979. QDomElement info;
  980. fileNames = domDocument.createElement(QString("USBFileNames"));
  981. root.appendChild(fileNames);
  982. m_USBFileNamesXml.clear();
  983. string pathInfo;
  984. LoadConfigString(pathInfo,USBVideoPathInfo);
  985. QString usbPersistantPathInfo = QString::fromStdString(pathInfo);
  986. QStringList stringList = usbPersistantPathInfo.split(QChar('/'));
  987. QString usbPersistantPath = usbPersistantPathInfo.left(usbPersistantPathInfo.length() - stringList.last().length() - 1);
  988. QString persistantIndex("");
  989. QFileInfo fileInfo;
  990. int i;
  991. for (i = 0; i < m_USBPathList.size(); ++i) {
  992. info = domDocument.createElement(QString("Index:" + QString::number(i)));
  993. fileNames.appendChild(info);
  994. fileInfo.setFile(m_USBPathList.at(i));
  995. info.appendChild(domDocument.createTextNode(fileInfo.fileName()));
  996. if (persistantIndex.isEmpty()) {
  997. if (fileInfo.filePath() == usbPersistantPath) {
  998. if (usbPersistantPathInfo == (fileInfo.filePath() + QString("/") + fileInfo.created().toString(QString("yyyyMMddhhmmss")) + fileInfo.lastModified().toString(QString("yyyyMMddhhmmss")) + QString::number(fileInfo.size()))) {
  999. persistantIndex = QString::number(i);
  1000. }
  1001. }
  1002. }
  1003. }
  1004. QDomElement persistant = domDocument.createElement(QString("USBPersistant"));
  1005. root.appendChild(persistant);
  1006. int mediaType = 0;
  1007. LoadConfigString(mediaType,USBMultimedia);
  1008. unsigned char multimediaType = (unsigned char)mediaType;
  1009. int videoMillesmial = 0;
  1010. if (persistantIndex.isEmpty()) {
  1011. if (0 == i) {
  1012. persistantIndex = QString("-1");
  1013. multimediaType = MT_Idle;
  1014. } else {
  1015. if (0 == multimediaType) {
  1016. multimediaType = MT_Video;
  1017. SaveConfigString(multimediaType,USBMultimedia);
  1018. system("sync");
  1019. }
  1020. persistantIndex = QString("0");
  1021. }
  1022. } else {
  1023. LoadConfigString(videoMillesmial,USBVideoMillesmial);
  1024. }
  1025. QString data = persistantIndex + QString("-") + QString::number(videoMillesmial);
  1026. //qDebug()<<__PRETTY_FUNCTION__<<"line:"<<__LINE__<<"usb videoMillesmia"<<data;
  1027. persistant.appendChild(domDocument.createTextNode(data));
  1028. QDomElement type = domDocument.createElement(QString("USBType"));
  1029. root.appendChild(type);
  1030. type.appendChild(domDocument.createTextNode(QString::number(multimediaType)));
  1031. m_USBFileNamesXml = domDocument.toString();
  1032. }
  1033. void VideoPlayerPrivate::videoPlayerSetPlay()
  1034. {
  1035. switch (m_PlayStatus) {
  1036. case VPPS_Play: {
  1037. break;
  1038. }
  1039. case VPPS_Pause: {
  1040. char* cmd = "pause\n";
  1041. writeCmdToMplayer(cmd);
  1042. break;
  1043. }
  1044. default: {
  1045. return;
  1046. break;
  1047. }
  1048. }
  1049. }
  1050. void VideoPlayerPrivate::videoPlayerSetSuspend()
  1051. {
  1052. if (m_Suspend) {
  1053. m_Suspend = false;
  1054. videoPlayerSetPlay();
  1055. } else {
  1056. if ((VPPS_Play == m_PlayStatus)
  1057. || (VPPS_Start == m_PlayStatus)) {
  1058. m_Suspend = true;
  1059. videoPlayerSetPause();
  1060. } else if (VPPS_Pause == m_PlayStatus) {
  1061. char* cmd = "pause\n";
  1062. writeCmdToMplayer(cmd);
  1063. }
  1064. }
  1065. }
  1066. void VideoPlayerPrivate::videoPlayerSetPause()
  1067. {
  1068. switch (m_PlayStatus) {
  1069. case VPPS_Start:
  1070. case VPPS_Play: {
  1071. char* cmd = "pause\n";
  1072. writeCmdToMplayer(cmd);
  1073. break;
  1074. }
  1075. case VPPS_Pause: {
  1076. break;
  1077. }
  1078. default: {
  1079. return;
  1080. break;
  1081. }
  1082. }
  1083. }
  1084. void VideoPlayerPrivate::videoPlayerSetExit(const DeviceWatcherType type)
  1085. {
  1086. if (type == m_DiskType) {
  1087. exitVideoPlayer();
  1088. }
  1089. }
  1090. void VideoPlayerPrivate::exitVideoPlayer()
  1091. {
  1092. Q_Q(VideoPlayer);
  1093. if(m_Process.size() != 0){
  1094. QByteArray processArray = m_Process.toLatin1();
  1095. char *process = processArray.data();
  1096. if(isRunning(process)){
  1097. quitMpalyer();
  1098. setPlayStatus(VPPS_Exit);
  1099. }
  1100. }
  1101. }
  1102. void VideoPlayerPrivate::videoPlayerVisible(const bool flag)
  1103. {
  1104. if(m_Process.size() != 0){
  1105. QByteArray processArray = m_Process.toLatin1();
  1106. char *process = processArray.data();
  1107. if(isRunning(process)){
  1108. if (flag) {
  1109. char* cmd = "takevideo\n";
  1110. writeCmdToMplayer(cmd);
  1111. } else {
  1112. char* cmd = "giveupvideo\n";
  1113. writeCmdToMplayer(cmd);
  1114. }
  1115. }
  1116. }
  1117. }
  1118. int VideoPlayerPrivate::isRunning(char *process)
  1119. {
  1120. int ret = 0;
  1121. char sCurrPid[16] = {0};
  1122. sprintf(sCurrPid, "%d", getpid());
  1123. FILE *fstream=NULL;
  1124. char buff[1024] = {0};
  1125. char cmd[128]="";
  1126. strcat(cmd,"ps -e -o pid,comm | grep ");
  1127. strcat(cmd, process);
  1128. strcat(cmd, " | grep -v PID | grep -v grep");
  1129. //printf("=== isRunning cmd: %s\n",cmd);
  1130. if(NULL==(fstream=popen(cmd, "r")))
  1131. {
  1132. printf("execute command failed:");
  1133. return -1;
  1134. }
  1135. while(NULL!= fgets(buff, sizeof(buff), fstream)) {
  1136. char *oldPID = getPidFromStr(buff);
  1137. if ( strcmp(sCurrPid, oldPID) != 0 ) {
  1138. printf("Runing,PID=%s\n", oldPID);
  1139. ret = 1;
  1140. }
  1141. }
  1142. pclose(fstream);
  1143. return ret;
  1144. }
  1145. QString VideoPlayerPrivate::chgform(QString name)
  1146. {
  1147. //printf("+++++++chgform_name:%s\n",name);
  1148. // qDebug()<<"+++[MusicPlayerPrivate::chgform]+++"<< name;
  1149. QString leftStr;
  1150. QString str;
  1151. str.clear();
  1152. while(name.size())
  1153. {
  1154. leftStr= name.left(1);
  1155. if(name.size()>1)
  1156. {
  1157. name = name.right(name.size()-1);
  1158. }
  1159. else{
  1160. name.clear();
  1161. }
  1162. if(leftStr == " ")
  1163. {
  1164. leftStr = "\\ ";
  1165. }
  1166. str += leftStr;
  1167. }
  1168. return str;
  1169. // char buf[512] = {0};
  1170. // qDebug()<<"++1111++";
  1171. // char *p = strtok(name, " ");
  1172. // printf( "%s\n", p );
  1173. // if(p != NULL){
  1174. // strcat(buf, p);
  1175. // strcat(buf, "\\ ");
  1176. // }
  1177. // p = strtok(NULL, " ");
  1178. // while(p != NULL)
  1179. // {
  1180. // if(strcmp(p,"\0")){
  1181. // strcat(buf, "\\ ");
  1182. // }
  1183. // else{
  1184. // strcat(buf, p);
  1185. // strcat(buf, "\\ ");
  1186. // }
  1187. // p = strtok(NULL, " ");
  1188. // }
  1189. }
  1190. void VideoPlayerPrivate::writeCmdToMplayer(char* cmd)
  1191. {
  1192. Q_Q(VideoPlayer);
  1193. printf("+++[MusicPlayerPrivate::writeCmdToMplayer:%s]+++\n",cmd);
  1194. if(m_Fd_w < 0)
  1195. {
  1196. //qDebug()<<"+++++++ffff0000++++++++";
  1197. m_Fd_w = open("/data/VideoPipe",O_WRONLY | O_NONBLOCK | O_TRUNC);
  1198. if(m_Fd_w < 0)
  1199. {
  1200. qDebug()<<"+++[MusicPlayerPrivate::writeCmdToMplayer:]+++";
  1201. perror("open fifo error\n");
  1202. m_ReadpThread = 0;
  1203. m_Process.clear();
  1204. return;
  1205. }
  1206. }
  1207. ftruncate(m_Fd_w,0);
  1208. lseek(m_Fd_w,0,SEEK_SET);
  1209. write(m_Fd_w,cmd,strlen(cmd));
  1210. }
  1211. void VideoPlayerPrivate::quitMpalyer(){
  1212. Q_Q(VideoPlayer);
  1213. qDebug()<<"+++[VideoPlayerPrivate::quitMpalyer()]+++";
  1214. char* cmd = "quit\n";
  1215. writeCmdToMplayer(cmd);
  1216. m_ReadpThread = 0;
  1217. usleep(500*1000);
  1218. emit q->onVideoPlayerExit();
  1219. }
  1220. ssize_t VideoPlayerPrivate::readLine(int fd, char *buffer, size_t n)
  1221. {
  1222. ssize_t numRead;
  1223. size_t totRead;
  1224. char *buf;
  1225. char ch;
  1226. if (n <= 0 || buffer == NULL) {
  1227. errno = EINVAL;
  1228. return -1;
  1229. }
  1230. buf = buffer;
  1231. totRead = 0;
  1232. for (;;) {
  1233. int flags = fcntl(fd,F_GETFL,0);
  1234. flags |= O_NONBLOCK;
  1235. fcntl(fd,F_SETFL,flags);
  1236. numRead = read(fd, &ch, 1);
  1237. if (-1 == numRead) {
  1238. if (errno == EINTR) {
  1239. continue;
  1240. } else {
  1241. return -1;
  1242. }
  1243. } else if (numRead == 0) {
  1244. if (totRead == 0) {
  1245. return 0;
  1246. } else {
  1247. break;
  1248. }
  1249. } else {
  1250. if (totRead < n - 1) {
  1251. totRead++;
  1252. *buf++ = ch;
  1253. }
  1254. if (ch == '\n') {
  1255. break;
  1256. }
  1257. }
  1258. }
  1259. *buf = '\0';
  1260. return totRead;
  1261. }
  1262. void VideoPlayerPrivate::onVideoPlayerVisible(bool visible)
  1263. {
  1264. Q_Q(VideoPlayer);
  1265. emit q->onVideoPlayerVisible(visible);
  1266. }
  1267. void VideoPlayerPrivate::sliderTouchEnable(bool enable){
  1268. Q_Q(VideoPlayer);
  1269. emit q->onSliderTouchEnable(enable);
  1270. }
  1271. void VideoPlayerPrivate::videoPlayerPlayStatus(const int type, const int status){
  1272. Q_Q(VideoPlayer);
  1273. emit q->onVideoPlayerPlayStatus(type, status);
  1274. }