2 * MP3/MPlayer plugin to VDR (C++)
4 * (C) 2001-2007 Stefan Huelswitt <s.huelswitt@gmx.de>
6 * This code is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This code is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
38 #include <vdr/device.h>
39 #include <vdr/videodir.h>
43 #include "player-mplayer.h"
44 #include "setup-mplayer.h"
48 #define MPLAYER_VOL_STEP 3.0
50 const char *MPlayerCmd = "mplayer.sh";
52 const char *globalResumeDir = 0;
54 // -- cMPlayerStatus -----------------------------------------------------------
56 cMPlayerStatus *status;
58 cMPlayerStatus::cMPlayerStatus(void)
64 bool cMPlayerStatus::GetVolume(int &Volume, bool &Mute)
68 // convert according cDvbDevice::SetVolumeDevice();
69 // take into account that VDR does non-linear changes, while
70 // MPlayer does linear volume changes.
71 Volume=((2*volume-volume*volume/255)*100+128)/256;
78 void cMPlayerStatus::SetVolume(int Volume, bool Absolute)
81 if(Absolute && Volume==0) mute=true;
85 #warning Caution! This code does not work with VDR 1.4.1 and 1.4.1-1. You can ignore this warning if you are using VDR 1.4.1-2 or later.
92 if(volume>0) mute=false;
94 d(printf("status: volume=%d mute=%d\n",volume,mute))
99 // --- cResumeEntry ------------------------------------------------------------
101 class cResumeEntry : public cListObject {
110 cResumeEntry::cResumeEntry(void)
115 cResumeEntry::~cResumeEntry()
120 // --- cMPlayerResume ----------------------------------------------------------
122 #define RESUME_FILE ".mplayer.resume"
123 #define GLOBAL_RESUME_FILE "global.mplayer.resume"
125 class cMPlayerResume : public cList<cResumeEntry> {
128 bool modified, global;
131 bool OpenResume(const cFileObj *file);
132 bool SaveResume(void);
134 cResumeEntry *FindResume(const cFileObj *file);
136 cMPlayerResume(void);
138 void SetResume(const cFileObj *file, float pos);
139 bool GetResume(const cFileObj *file, float &pos);
142 cMPlayerResume::cMPlayerResume(void)
147 cMPlayerResume::~cMPlayerResume()
154 void cMPlayerResume::SetResume(const cFileObj *file, float pos)
156 if(pos<0.001) pos=0.0;
157 else if(pos>99.0) pos=99.0;
159 if(OpenResume(file) && (re=FindResume(file))) {
160 d(printf("resume: setting resume %f (update)\n",pos))
164 re->name=strdup(global ? file->FullPath() : file->Name());
166 d(printf("resume: setting resume %f (new)\n",pos))
172 bool cMPlayerResume::GetResume(const cFileObj *file, float &pos)
175 if(OpenResume(file) && (re=FindResume(file))) {
182 bool cMPlayerResume::OpenResume(const cFileObj *file)
186 modified=global=false;
187 free(resfile); resfile=0;
188 delete resobj; resobj=new cFileObj(file);
190 asprintf(&s,file->Subdir() ? "%s/%s":"%s",file->Source()->BaseDir(),file->Subdir());
191 if(MPlayerSetup.ResumeMode==1 ||
192 (access(s,W_OK) && (errno==EACCES || errno==EROFS))) {
194 resfile=AddPath(globalResumeDir?globalResumeDir:VideoDirectory,GLOBAL_RESUME_FILE);
195 d(printf("resume: using global file\n"))
198 resfile=AddPath(s,RESUME_FILE);
201 d(printf("resume: resume file is '%s'\n",resfile))
202 FILE *f=fopen(resfile,"r");
204 d(printf("resume: successfully opened resume file\n"))
206 while(fgets(line,sizeof(line),f)) {
209 if(sscanf(line,"%f:%511[^\n]",&p,name)==2) {
210 cResumeEntry *re=new cResumeEntry;
211 re->name=strdup(name);
220 d(printf("resume: assuming empty resume file\n"))
227 bool cMPlayerResume::SaveResume(void)
229 if(resfile && modified) {
231 d(printf("resume: saving resume file\n"))
232 cSafeFile f(resfile);
234 for(cResumeEntry *re=First(); re; re=Next(re))
235 fprintf(f,"%06.2f:%s\n",re->pos,re->name);
240 d(printf("resume: failed to save resume file\n"))
245 void cMPlayerResume::Purge(void)
247 d(printf("resume: purging from resume file\n"))
248 for(cResumeEntry *re=First(); re;) {
250 if(re->pos<1.0 || re->pos>99.0) {
252 d(printf("resume: purging due to position: %s\n",re->name))
255 resobj->SetName(re->name);
256 if(access(resobj->FullPath(),F_OK)<0) {
258 d(printf("resume: purging due to access: %s\n",re->name))
262 cResumeEntry *n=Next(re);
272 cResumeEntry *cMPlayerResume::FindResume(const cFileObj *file)
275 d(printf("resume: searching resume position for '%s'\n",file->Name()))
276 const char *s=global ? file->FullPath() : file->Name();
277 for(cResumeEntry *re=First(); re; re=Next(re))
278 if(!strcasecmp(re->name,s)) {
279 d(printf("resume: found resume position %.1f%%\n",re->pos))
283 d(printf("resume: no resume position found\n"))
287 // --- cMPlayerPlayer ----------------------------------------------------------
289 cMPlayerPlayer::cMPlayerPlayer(const cFileObj *File, bool Rewind)
290 :cPlayer(pmExtern_THIS_SHOULD_BE_AVOIDED)
292 started=slave=brokenPipe=false; run=true; pid=-1; pipefl=0;
293 playMode=pmPlay; index=saveIndex=total=-1; nextTime=nextPos=0;
295 file=new cFileObj(File);
297 resume=MPlayerSetup.ResumeMode ? new cMPlayerResume : 0;
300 cMPlayerPlayer::~cMPlayerPlayer()
309 void cMPlayerPlayer::ClosePipe(void)
311 if(pipefl&1) close(inpipe[0]);
312 if(pipefl&2) close(inpipe[1]);
313 if(pipefl&4) close(outpipe[0]);
314 if(pipefl&8) close(outpipe[1]);
318 void cMPlayerPlayer::Activate(bool On)
321 if(file && !started) {
322 if(Fork()) started=true;
329 Play(); // MPlayer ignores "quit" while paused
330 MPlayerControl("quit");
331 int until=time_ms()+3000; // wait some time until MPlayer is gone
332 d(printf("mplayer: waiting for child exit"))
334 if(time_ms()>until) {
335 kill(pid,SIGKILL); // kill it anyways
336 d(printf(" SIGKILL"))
340 d(printf(".")) d(fflush(stdout))
346 d(printf("mplayer: waiting for child exit (non-slave)\n"))
356 bool cMPlayerPlayer::Active(void)
358 return waitpid(pid,0,WNOHANG)==0;
361 bool cMPlayerPlayer::Fork(void)
363 if(MPlayerSetup.SlaveMode) {
364 if(pipe(inpipe)==-1) {
365 esyslog("ERROR: pipe failed for inpipe: (%d) %s",errno,strerror(errno));
369 if(pipe(outpipe)==-1) {
370 esyslog("ERROR: pipe failed for outpipe: (%d) %s",errno,strerror(errno));
379 esyslog("ERROR: fork failed: (%d) %s",errno,strerror(errno));
382 if(pid==0) { // child
383 dsyslog("mplayer: mplayer child started (pid=%d)", getpid());
385 if(MPlayerSetup.SlaveMode) {
386 if(dup2(inpipe[0],STDIN_FILENO)<0 ||
387 dup2(outpipe[1],STDOUT_FILENO)<0 ||
388 dup2(outpipe[1],STDERR_FILENO)<0) {
389 esyslog("ERROR: dup2() failed in MPlayer child: (%d) %s",errno,strerror(errno));
394 int nfd=open("/dev/null",O_RDONLY);
395 if(nfd<0 || dup2(nfd,STDIN_FILENO)<0)
396 esyslog("ERROR: redirect of STDIN failed in MPlayer child: (%d) %s",errno,strerror(errno));
398 for(int i=getdtablesize()-1; i>STDERR_FILENO; i--) close(i);
400 char cmd[64+PATH_MAX*2], aid[20];
401 char *fname=Quote(file->FullPath());
402 if(MPlayerAid>=0) snprintf(aid,sizeof(aid)," AID %d",MPlayerAid);
404 snprintf(cmd,sizeof(cmd),"%s \"%s\" %s%s",MPlayerCmd,fname,MPlayerSetup.SlaveMode?"SLAVE":"",aid);
406 execle("/bin/sh","sh","-c",cmd,(char *)0,environ);
407 esyslog("ERROR: exec failed for %s: (%d) %s",cmd,errno,strerror(errno));
411 if(MPlayerSetup.SlaveMode) {
412 close(inpipe[0]); pipefl&=~1;
413 close(outpipe[1]); pipefl&=~8;
414 fcntl(outpipe[0],F_SETFL,O_NONBLOCK);
416 mpVolume=100; // MPlayer startup defaults
427 void cMPlayerPlayer::Action(void)
429 dsyslog("mplayer: player thread started (pid=%d)", getpid());
431 // set locale for correct parsing of MPlayer output.
432 // I don't know if this affects other parts of VDR.
433 const char * const oldLocale=setlocale(LC_NUMERIC,"C");
436 pfd[0].fd=outpipe[0];
437 pfd[0].events=POLLIN;
439 float curPos=-1.0, resPos=-1.0;
440 if(resume && !rewind) resume->GetResume(file,resPos);
442 char buff[BSIZE+2]; // additional space for fake newline
444 bool force=true, slavePatch=false, trustedTotal=false, playBack=false;
446 if(playMode==pmPlay && playBack) {
449 MPlayerControl("get_time_length");
450 nextTime=t+(total>0 ? TIME_INT : POS_INT);
453 if(!slavePatch) MPlayerControl("get_percent_pos");
459 int r=read(outpipe[0],buff+c,BSIZE-c);
462 buff[c]=0; // make sure buffer is NULL terminated
465 p=strpbrk(buff,"\n\r");
466 if(!p && c==BSIZE) { // Full buffer, but no newline found.
467 p=&buff[c]; // Have to fake one.
468 buff[c]='\n'; c++; buff[c]=0;
475 float ftime=-1.0, fpos=-1.0;
477 if(strncmp(buff,"Starting playback",17)==0 ||
478 strncmp(buff,"Starte Wiedergabe",17)==0) {
482 d(printf("PLAYBACK STARTED\n"))
485 !strcmp(currentName,file->FullPath()) ||
486 !strcmp(currentName,file->Path()))
487 MPlayerControl("seek %.1f 1",resPos);
489 d(printf("mplayer: no resume, seems to be playlist\n"))
493 else if(strncmp(buff,"Playing ",8)==0 ||
494 strncmp(buff,"Spiele ",7)==0) {
496 index=saveIndex=total=-1;
500 currentName=strdup(::index(buff,' ')+1);
502 int l=strlen(currentName);
503 if(currentName[l-1]=='.') currentName[l-1]=0; // skip trailing dot
505 d(printf("PLAYING %s\n",currentName))
507 else if(sscanf(buff,"ANS_LENGTH=%d",&itime)==1) {
509 total=SecondsToFrames(itime);
512 printf("sl: ANS_LENGTH=%s (%s)\n",IndexToHMSF(total),buff);
516 else if(sscanf(buff,"ANS_PERCENT_POSITION=%d",&itime)==1) {
520 index=total*itime/100;
522 printf("sl: ANS_PERCENT_POS=%s (%s)\n",IndexToHMSF(index),buff);
527 else if(sscanf(buff,"SLAVE: time=%f position=%f",&ftime,&fpos)==2) {
529 const float fr=(float)SecondsToFrames(1);
530 itime=(int)(ftime*fr);
531 if(saveIndex<0 || itime>saveIndex) { // prevent index jump-back
532 saveIndex=index=itime;
533 if(!trustedTotal) total=(int)(ftime*fr*100.0/fpos);
535 printf("sl: SLAVE=%s/%s [%d] (%s)\n",IndexToHMSF(index),IndexToHMSF(total),trustedTotal,buff);
538 slavePatch=playBack=true;
541 else printf("%s%c",buff,cc);
549 SetMPlayerVolume(force);
554 if(resume && curPos>=0.0) resume->SetResume(file,curPos);
556 // restore old locale
557 if(oldLocale) setlocale(LC_NUMERIC,oldLocale);
559 dsyslog("mplayer: player thread ended (pid=%d)", getpid());
562 void cMPlayerPlayer::SetMPlayerVolume(bool force)
567 if(status->GetVolume(volume,mute) || force) {
569 if(!mpMute) { MPlayerControl("mute"); mpMute=true; }
572 if(mpMute) { MPlayerControl("mute"); mpMute=false; }
573 if(volume!=mpVolume) {
574 MPlayerControl("volume %d 1",volume);
578 d(printf("mplayer: volume=%d mpVolume=%d mpMute=%d\n",volume,mpVolume,mpMute))
583 void cMPlayerPlayer::MPlayerControl(const char *format, ...)
589 vasprintf(&buff,format,ap);
591 // check for writeable pipe i.e. prevent broken pipe signal
594 pfd.fd=inpipe[1]; pfd.events=POLLOUT; pfd.revents=0;
595 int r=poll(&pfd,1,50);
597 if(pfd.revents & ~POLLOUT) {
598 d(printf("mplayer: %s%s%s%sin MPlayerControl\n",pfd.revents&POLLOUT?"POLLOUT ":"",pfd.revents&POLLERR?"POLLERR ":"",pfd.revents&POLLHUP?"POLLHUP ":"",pfd.revents&POLLNVAL?"POLLNVAL ":""))
601 else if(pfd.revents & POLLOUT) {
602 r=write(inpipe[1],buff,strlen(buff));
604 d(printf("mplayer: pipe write(1) failed: %s\n",strerror(errno)))
608 r=write(inpipe[1],"\n",1);
610 d(printf("mplayer: pipe write(2) failed: %s\n",strerror(errno)))
616 else if(r==0) d(printf("mplayer: poll timed out in MPlayerControl (hugh?)\n"))
617 else d(printf("mplayer: poll failed in MPlayerControl: %s\n",strerror(errno)))
619 else d(printf("mplayer: cmd pipe is broken\n"))
621 d(printf("mplayer: slave cmd: %s\n",buff))
627 void cMPlayerPlayer::Pause(void)
630 if(playMode==pmPaused) Play();
631 else if(playMode==pmPlay) {
633 MPlayerControl("pause");
638 void cMPlayerPlayer::Play(void)
641 if(playMode==pmPaused) {
643 MPlayerControl("pause");
648 void cMPlayerPlayer::Goto(int Index, bool percent, bool still)
651 if(playMode==pmPaused) Play();
652 if(percent) MPlayerControl("seek %d 1",Index);
653 else MPlayerControl("seek %+d 0",Index-(index/SecondsToFrames(1)));
659 void cMPlayerPlayer::SkipSeconds(int secs)
663 if(playMode==pmPaused) { Play(); p=true; }
664 MPlayerControl("seek %+d 0",secs);
670 void cMPlayerPlayer::KeyCmd(const char *cmd)
672 if(slave) MPlayerControl(cmd);
675 bool cMPlayerPlayer::GetIndex(int &Current, int &Total, bool SnapToIFrame)
677 Current=index; Total=total;
681 bool cMPlayerPlayer::GetReplayMode(bool &Play, bool &Forward, int &Speed)
683 Play=(playMode==pmPlay);
689 char *cMPlayerPlayer::GetCurrentName(void)
692 return currentName ? strdup(currentName) : 0;