]> git.vomp.tv Git - vompclient.git/blob - vvideorec.cc
MVP code removal
[vompclient.git] / vvideorec.cc
1 /*
2     Copyright 2004-2019 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP 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.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include <math.h>
22
23 #include "command.h"
24 #include "osd.h"
25 #include "wsymbol.h"
26 #include "audio.h"
27 #include "vdr.h"
28 #include "video.h"
29 #include "timers.h"
30 #include "player.h"
31 #include "recording.h"
32 #include "vaudioselector.h"
33 #include "message.h"
34 #include "remote.h"
35 #include "boxstack.h"
36 #include "vinfo.h"
37 #include "i18n.h"
38 #include "bitmap.h"
39 #include "recinfo.h"
40 #include "log.h"
41 #include "channel.h"
42 #include "vteletextview.h"
43 #include "vvideorec.h"
44
45 VVideoRec::VVideoRec(Recording* rec, bool ish264)
46 {
47   boxstack = BoxStack::getInstance();
48   vdr = VDR::getInstance();
49   video = Video::getInstance();
50   timers = Timers::getInstance();
51   vas = NULL;
52   vsummary = NULL;
53
54   videoMode = video->getMode();
55   myRec = rec;
56
57   video->seth264mode(ish264);
58
59   player = new Player(Command::getInstance(), this, this);
60   player->init(myRec->IsPesRecording,myRec->recInfo->fps);
61
62   playing = false;
63
64   startMargin = 0;
65   endMargin = 0;
66   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
67   char* cendMargin = vdr->configLoad("Timers", "End margin");
68   if (!cstartMargin)
69   {
70     startMargin = 300; // 5 mins default
71   }
72   else
73   {
74     startMargin = atoi(cstartMargin) * 60;
75     delete[] cstartMargin;
76   }
77
78   if (!cendMargin)
79   {
80     endMargin = 300; // 5 mins default
81   }
82   else
83   {
84     endMargin = atoi(cendMargin) * 60;
85     delete[] cendMargin;
86   }
87
88   Log::getInstance()->log("VVideoRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
89
90   setSize(video->getScreenWidth(), video->getScreenHeight());
91   createBuffer();
92   transparent.set(0, 0, 0, 0);
93   setBackgroundColour(transparent);
94
95   OsdVector* osdv=dynamic_cast<OsdVector*>(Osd::getInstance());
96   if (osdv)
97   {
98           osdv->updateBackgroundColor(DrawStyle::BLACK);
99   }
100
101   barRegion.x = 0;
102   barRegion.y = video->getScreenHeight() - 58;   // FIXME, need to be - 1? and below?
103   barRegion.w = video->getScreenWidth();
104   barRegion.h = 58;
105
106   clocksRegion.x = barRegion.x + 140;
107   clocksRegion.y = barRegion.y + 12;
108   clocksRegion.w = 170;
109   clocksRegion.h = getFontHeight();
110 //  barBlue.set(0, 0, 150, 150);
111   barBlue.set(0, 0, 0, 128);
112
113   barShowing = false;
114   barGenHold = false;
115   barScanHold = false;
116   barVasHold = false;
117
118   vdisplay.mode=Fullscreen;
119   vdisplay.fallbackMode=Fullscreen;
120   vdisplay.x=0;
121   vdisplay.y=0;
122   vdisplay.width=0;
123   vdisplay.height=0;
124
125   lastbar = -1;
126 }
127
128 void VVideoRec::preDelete()
129 {
130   timers->cancelTimer(this, 1);
131   timers->cancelTimer(this, 2);
132
133   if (vas)
134   {
135     boxstack->remove(vas);
136     vas = NULL;
137   }
138
139   if (vsummary) delete vsummary;
140
141   if (playing) stopPlay();
142 }
143
144 VVideoRec::~VVideoRec()
145 {
146   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Entering vvideorec destructor");
147
148   video->setDefaultAspect();
149
150   // kill recInfo in case resumePoint has changed (likely)
151   myRec->dropRecInfo();
152   // FIXME - do this properly - save the resume point back to the server manually and update
153   // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
154
155   OsdVector* osdv=dynamic_cast<OsdVector*>(Osd::getInstance());
156   if (osdv)
157   {
158           osdv->updateBackgroundColor(DrawStyle::WALLPAPER);
159   }
160 }
161
162 void VVideoRec::go(bool resume)
163 {
164   ULONG startFrameNum;
165   if (resume)
166     startFrameNum = myRec->recInfo->resumePoint;
167   else
168     startFrameNum = 0;
169
170   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Starting stream: %s at frame: %lu", myRec->getFileName(), startFrameNum);
171   ULONG lengthFrames = 0;
172   bool isPesRecording;
173   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames, &isPesRecording);
174   myRec->IsPesRecording = isPesRecording;
175   if (lengthBytes)
176   {
177     player->setLengthBytes(lengthBytes);
178     player->setLengthFrames(lengthFrames);
179     player->setStartFrame(startFrameNum);
180     player->play();
181     playing = true;
182     doBar(0);
183   }
184   else
185   {
186     stopPlay(); // clean up
187
188     if (!vdr->isConnected())
189     {
190       Command::getInstance()->connectionLost();
191       return;
192     }
193
194     Message* m = new Message();
195     m->message = Message::CLOSE_ME;
196     m->from = this;
197     m->to = boxstack;
198     Command::getInstance()->postMessageNoLock(m);
199
200     VInfo* vi = new VInfo();
201     vi->setSize(360, 200);
202     vi->createBuffer();
203     if (Video::getInstance()->getFormat() == Video::PAL)
204       vi->setPosition(190, 170);
205     else
206       vi->setPosition(180, 120);
207     vi->setOneLiner(tr("Error playing recording"));
208     vi->setExitable();
209     vi->setBorderOn(1);
210     vi->setTitleBarColour(DrawStyle::DANGER);
211     vi->okButton();
212     vi->draw();
213
214     m = new Message();
215     m->message = Message::ADD_VIEW;
216     m->to = boxstack;
217     m->parameter.num = (ULONG)vi;
218     Command::getInstance()->postMessageNoLock(m);
219   }
220 }
221
222 int VVideoRec::handleCommand(int command)
223 {
224   switch(command)
225   {
226     case Remote::UP:
227     case Remote::PLAY:
228     {
229       player->play();
230       doBar(0);
231       return 2;
232     }
233
234     case Remote::PLAYPAUSE:
235     {
236         player->playpause();
237         doBar(0);
238         return 2;
239     }
240
241     case Remote::BACK:
242     {
243       if (vsummary)
244       {
245         removeSummary();
246         return 2;
247       }
248     } // DROP THROUGH
249     case Remote::STOP:
250     case Remote::MENU:
251     {
252       if (playing) stopPlay();
253
254       return 4;
255     }
256     case Remote::DOWN:
257     case Remote::PAUSE:
258     {
259       player->pause();
260       doBar(0);
261       return 2;
262     }
263     case Remote::SKIPFORWARD:
264     {
265       doBar(3);
266       player->skipForward(60);
267       return 2;
268     }
269     case Remote::SKIPBACK:
270     {
271       doBar(4);
272       player->skipBackward(60);
273       return 2;
274     }
275     case Remote::RIGHT:
276     case Remote::FORWARD:
277     {
278       player->fastForward();
279       doBar(0);
280       return 2;
281     }
282     case Remote::LEFT:
283     case Remote::REVERSE:
284     {
285       player->fastBackward();
286       doBar(0);
287       return 2;
288     }
289     case Remote::RED:
290     {
291       if (vsummary) removeSummary();
292       else doSummary();
293       return 2;
294     }
295     case Remote::GREEN:
296     {
297       doAudioSelector();
298       return 2;
299     }
300     case Remote::YELLOW:
301     {
302       if (myRec->hasMarks())
303       {
304         // skip to previous mark
305         Log* logger = Log::getInstance();
306         int currentFrame = (player->getCurrentFrameNum()); // get current Frame
307         currentFrame -= (int)(5. * myRec->recInfo->fps); // subtrack 5 seconds, else you cannot skip more than once back ..
308
309         int prevMark = myRec->getPrevMark(currentFrame); // find previous Frame
310         if (prevMark)
311         {
312           logger->log("VVideoRec", Log::NOTICE, "jump back from pos %i to mark at %i",currentFrame,prevMark);
313           player->jumpToMark(prevMark);
314         }
315         doBar(4);
316       }
317       else
318       {
319         doBar(2);
320         player->skipBackward(10);
321       }
322       return 2;
323     }
324     case Remote::BLUE:
325     {
326       if (myRec->hasMarks())
327       {
328         // skip to next mark
329         Log* logger = Log::getInstance();
330         int currentFrame = (player->getCurrentFrameNum());
331
332         int nextMark = myRec->getNextMark(currentFrame);
333
334         if (nextMark)
335         {
336           logger->log("VVideoRec", Log::NOTICE, "jump forward from pos %i to mark at %i",currentFrame,nextMark);
337           player->jumpToMark(nextMark);
338         }
339         doBar(3);
340       }
341       else
342       {
343         doBar(1);
344         player->skipForward(10);
345       }
346       return 2;
347     }
348     case Remote::PREVCHANNEL:
349     {
350       player->skipBackward(2);
351       return 2;
352     }
353     case Remote::STAR:
354     {
355       doBar(2);
356       player->skipBackward(10);
357       return 2;
358     }
359     case Remote::HASH:
360     {
361       doBar(1);
362       player->skipForward(10);
363       return 2;
364     }
365     case Remote::FULL:
366     case Remote::TV:
367     {
368       toggleChopSides();
369       return 2;
370     }
371
372     case Remote::OK:
373     {
374       if (vsummary)
375       {
376         removeSummary();
377         return 2;
378       }
379       
380       if (barShowing) removeBar();
381       else doBar(0);
382       return 2;
383     }
384
385     case Remote::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
386     case Remote::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
387     case Remote::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
388     case Remote::THREE: player->jumpToPercent(30); doBar(0);  return 2;
389     case Remote::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
390     case Remote::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
391     case Remote::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
392     case Remote::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
393     case Remote::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
394     case Remote::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
395
396     case Remote::RECORD: player->toggleSubtitles(); return 2;
397 #ifdef DEV
398 //    case Remote::RED:
399 //    {
400       //Don't use RED for anything. It will eventually be recording summary
401
402       //player->test1();
403
404
405       /*
406       // for testing EPG in NTSC with a NTSC test video
407       Video::getInstance()->setMode(Video::QUARTER);
408       Video::getInstance()->setPosition(170, 5);
409       VEpg* vepg = new VEpg(NULL, 0);
410       vepg->draw();
411       BoxStack::getInstance()->add(vepg);
412       BoxStack::getInstance()->update(vepg);
413       */
414
415 //      return 2;
416 //    }
417
418 #endif
419
420   }
421
422   return 1;
423 }
424
425 void VVideoRec::doTeletext()
426 {
427   
428   bool exists=true;
429
430   
431   // Draw the teletxt
432   VTeletextView *vtxv=player->getTeletextDecoder()->getTeletxtView();
433   if (vtxv==NULL) {
434        vtxv= new VTeletextView((player)->getTeletextDecoder(),this,NULL);
435       (player)->getTeletextDecoder()->registerTeletextView(vtxv);
436       exists=false;
437   }
438   vtxv->setSubtitleMode(true);
439   vtxv->draw();
440   draw();
441   
442   if (!exists) {
443       BoxStack::getInstance()->add(vtxv);
444   }
445   BoxStack::getInstance()->update(this);
446   BoxStack::getInstance()->update(vtxv); 
447 }
448
449 void VVideoRec::processMessage(Message* m)
450 {
451   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Message received");
452
453   if (m->message == Message::MOUSE_LBDOWN)
454   {
455     UINT x = (m->parameter.num>>16) - getScreenX();
456     UINT y = (m->parameter.num&0xFFFF) - getScreenY();
457
458     if (!barShowing)
459     {
460       BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
461     }
462     else if (barRegion.x<=x && barRegion.y<=y && (barRegion.x+barRegion.w)>=x && (barRegion.y+barRegion.h)>=y)
463     {
464       int progBarXbase = barRegion.x + 300;
465       if (myRec->hasMarks())
466       {
467         MarkList* markList = myRec->getMarkList();
468         MarkList::iterator i;
469         Mark* loopMark = NULL;
470         int posPix;
471         ULONG lengthFrames;
472         if (myRec->recInfo->timerEnd > time(NULL))
473         {
474           // chasing playback
475           // Work out an approximate length in frames (good to 1s...)
476                         lengthFrames = (ULONG)((double)(myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * myRec->recInfo->fps);
477         }
478         else
479         {
480           lengthFrames = player->getLengthFrames();
481         }
482         for(i = markList->begin(); i != markList->end(); i++)
483         {
484           loopMark = *i;
485           if (loopMark->pos)
486           {
487             posPix = 302 * loopMark->pos / lengthFrames;
488             rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, DrawStyle::DANGER);
489             if (x>=barRegion.x + progBarXbase + 2 + posPix
490                 && x<=barRegion.x + progBarXbase + 2 + posPix+3
491                 && y>=barRegion.y + 12 - 2
492                 && y<=barRegion.y + 12 - 2+28)
493             {
494               player->jumpToMark(loopMark->pos);
495               doBar(3);
496               return;
497             }
498           }
499         }
500       }
501
502       if (x>=barRegion.x + progBarXbase + 24
503           && x<=barRegion.x + progBarXbase + 4 + 302
504           && y>=barRegion.y + 12 - 2
505           && y<=barRegion.y + 12 - 2+28)
506       {
507         int cx=x-(barRegion.x + progBarXbase + 4);
508         double percent=((double)cx)/302.*100.;
509         player->jumpToPercent(percent);
510         doBar(3);
511         return;
512         //  int progressWidth = 302 * currentFrameNum / lengthFrames;
513         //  rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
514       }
515     }
516     else
517     {
518       BoxStack::getInstance()->handleCommand(Remote::OK); //simulate rok press
519     }
520   }
521   else if (m->from == player)
522   {
523     if (m->message != Message::PLAYER_EVENT) return;
524     switch(m->parameter.num)
525     {
526       case Player::CONNECTION_LOST: // connection lost detected
527       {
528         // I can't handle this, send it to command
529         Message* m2 = new Message();
530         m2->to = Command::getInstance();
531         m2->message = Message::CONNECTION_LOST;
532         Command::getInstance()->postMessageNoLock(m2);
533         break;
534       }
535       case Player::STOP_PLAYBACK:
536       {
537         // FIXME Obselete ish - improve this
538         Message* m2 = new Message(); // Must be done after this thread finishes, and must break into master mutex
539         m2->to = Command::getInstance();
540         m2->message = Message::STOP_PLAYBACK;
541         Command::getInstance()->postMessageNoLock(m2);
542         break;
543       }
544       case Player::ASPECT43:
545       {
546         break;
547       }
548       case Player::ASPECT169:
549       {
550         break;
551       }
552     }
553   }
554   else if (m->message == Message::AUDIO_CHANGE_CHANNEL)
555   {
556     Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received change audio channel to %i", m->parameter.num);
557     player->setAudioChannel(m->parameter.num&0xFFFF,(m->parameter.num&0xFF0000)>> 16,(m->parameter.num&0xFF000000)>> 24 );
558   }
559   else if (m->message == Message::SUBTITLE_CHANGE_CHANNEL)
560   {
561       Log::getInstance()->log("VVideoRec", Log::DEBUG, "Received change subtitle channel to %i", m->parameter.num);
562       int type=((m->parameter.num & 0xFF0000)>>16);
563       switch (type) {
564       case 0x10: { //dvbsubtitle
565           player->setSubtitleChannel((m->parameter.num & 0xFFFF));
566           player->turnSubtitlesOn(true);
567           VTeletextView *vtxt=((Player*)player)->getTeletextDecoder()->getTeletxtView();
568           if (vtxt && vtxt->isInSubtitleMode()) {
569               BoxStack::getInstance()->remove(vtxt);
570           }
571                  } break;
572       case 0xFF: { //nosubtitles
573           
574            player->turnSubtitlesOn(false);
575            VTeletextView *vtxt=((Player*)player)->getTeletextDecoder()->getTeletxtView();
576            if (vtxt && vtxt->isInSubtitleMode()) {
577               BoxStack::getInstance()->remove(vtxt);
578            }  
579           
580                  } break;
581       case 0x11: { //videotext
582           player->turnSubtitlesOn(false);
583           doTeletext();
584           ((Player*)player)->getTeletextDecoder()->setPage((m->parameter.num & 0xFFFF));
585                  } break;
586       };
587       if (vas) {
588         BoxStack::getInstance()->update(vas);
589       }
590       BoxStack::getInstance()->update(this);
591
592       
593   } 
594   else if (m->message == Message::CHILD_CLOSE)
595   {
596     if (m->from == vas)
597     {
598       vas = NULL;
599       barVasHold = false;
600       if (!barGenHold && !barScanHold && !barVasHold) removeBar();
601     }
602   }
603 }
604
605 void VVideoRec::stopPlay()
606 {
607   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Pre stopPlay");
608
609   removeBar();
610   Log::getInstance()->log("VVideoRec", Log::DEBUG, "1");
611   player->stop();
612   Log::getInstance()->log("VVideoRec", Log::DEBUG, "2");
613   vdr->stopStreaming();
614   Log::getInstance()->log("VVideoRec", Log::DEBUG, "3");
615   delete player;
616
617   playing = false;
618
619   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
620   Log::getInstance()->log("VVideoRec", Log::DEBUG, "Post stopPlay");
621 }
622
623 void VVideoRec::toggleChopSides()
624 {
625   if (video->getTVsize() == Video::ASPECT16X9) return; // Means nothing for 16:9 TVs
626
627   if (videoMode == Video::NORMAL)
628   {
629     videoMode = Video::LETTERBOX;
630     video->setMode(Video::LETTERBOX);
631   }
632   else
633   {
634     videoMode = Video::NORMAL;
635     video->setMode(Video::NORMAL);
636   }
637 }
638
639 void VVideoRec::doAudioSelector()
640 {
641     int subtitleChannel=player->getCurrentSubtitleChannel();
642     int subtitleType=0x10;
643     if (!(player)->isSubtitlesOn()) {
644         if ((player)->getTeletextDecoder()->getTeletxtView() &&
645             (player)->getTeletextDecoder()->getTeletxtView()->isInSubtitleMode() 
646             ) {
647                 subtitleChannel=(player)->getTeletextDecoder()->getPage();
648                 subtitleType=0x11;
649       
650            } else {
651                 subtitleType=0xFF; //turnedOff
652                 subtitleChannel=0;
653           }
654     }
655     if (player->isPesRecording()) {
656         bool* availableMpegAudioChannels = player->getDemuxerMpegAudioChannels();
657         bool* availableAc3AudioChannels = NULL;
658         bool* availableSubtitleChannels = player->getDemuxerSubtitleChannels();
659         int *availableTTxtpages = player->getTeletxtSubtitlePages();
660         int currentAudioChannel = player->getCurrentAudioChannel();
661         if (Audio::getInstance()->supportsAc3())
662         {
663             availableAc3AudioChannels = player->getDemuxerAc3AudioChannels();
664         }
665         
666         vas = new VAudioSelector(this, availableMpegAudioChannels, availableAc3AudioChannels, currentAudioChannel,availableSubtitleChannels, availableTTxtpages,
667             subtitleChannel, subtitleType, myRec->recInfo);
668     } else {
669         // Draw the selector
670         Channel *temp_channel=player->getDemuxerChannel();
671        // RecInfo *cur_info= myRec->recInfo;
672      /*   unsigned char numchan_recinfo = cur_info->numComponents;
673         unsigned char numchan_subtitles_siz = temp_channel.numSPids;
674         ULONG mp_audcounter = 0;
675         ULONG ac3_counter = 0;
676         int dvb_subcounter = 1;*/
677         ULONG i;
678         
679         /*unsigned char type;
680         char* lang;
681         char* description;
682         for (i = 0; i < numchan_recinfo; i++)
683         {   
684             apid* ac = NULL;
685             type = cur_info->types[i];
686             lang = cur_info->languages[i];
687             description = cur_info->descriptions[i];
688             
689
690             if (cur_info->streams[i] == 2) {
691                 switch (type)
692                 {
693                 case 1: //mpaudio mono
694                 case 3: //mpaudio stereo
695                     if (mp_audcounter < temp_channel.numAPids) ac = &temp_channel.apids[mp_audcounter];
696                     
697                     mp_audcounter++;
698                     break;
699                 case 5: //ac3
700                     if (ac3_counter < temp_channel.numDPids) ac = &temp_channel.dpids[ac3_counter];
701                     ac3_counter++;
702                     break;
703                 }
704             } else if (cur_info->streams[i] == 3){
705                 if (dvb_subcounter < numchan_subtitles_siz) ac = &temp_channel.spids[dvb_subcounter];
706             } else continue; //neither audio nor subtitle
707             if (ac)
708             {
709                 if (description && (strlen(description) > 0))
710                 {
711                     ac->name = new char[strlen(description) + 1];
712                     strcpy(ac->name, description);
713                     
714                 } else if (lang && strlen(lang) > 0)
715                 {
716                     ac->name = new char[strlen(lang) + 1];
717                     strcpy(ac->name, lang);
718                     
719                 }
720             }
721         }*/
722         for (i=0;i<temp_channel->numAPids;i++) {
723             apid *ac=&temp_channel->apids[i];
724             if (ac->desc[0]==0) {
725                 strncpy(ac->desc, tr("unknown"),9);
726             }
727         }
728         for (i=0;i<temp_channel->numDPids;i++) {
729             apid *ac=&temp_channel->dpids[i];
730             if (ac->desc[0]==0) {
731                 strncpy(ac->desc, tr("unknown"),9);
732             }
733         }
734         for (i=0;i<temp_channel->numSPids;i++) {
735             apid *ac=&temp_channel->spids[i];
736             if (ac->desc[0]==0) {
737                 strncpy(ac->desc, tr("unknown"),9);
738             }
739         }
740
741         vas = new VAudioSelector(this,temp_channel , (player)->getCurrentAudioChannel(),
742             subtitleType,subtitleChannel,player->getTeletxtSubtitlePages());  
743       /*   for (i=0;i<temp_channel.numAPids;i++) {
744             apid *ac=&temp_channel.apids[i];
745             delete[] ac->name;
746             ac->name=NULL;
747         }
748         for (i=0;i<temp_channel.numDPids;i++) {
749             apid *ac=&temp_channel.dpids[i];
750             delete[] ac->name;
751             ac->name=NULL;
752         }
753         for (i=0;i<temp_channel.numSPids;i++) {
754             apid *ac=&temp_channel.spids[i];
755             delete[] ac->name;
756             ac->name=NULL;
757         }*/
758     }
759
760
761   vas->setBackgroundColour(barBlue);
762   vas->setPosition(0, barRegion.y - 120);
763
764 // pal 62, ntsc 57
765
766   barVasHold = true;
767   doBar(0);
768
769   vas->draw();
770   boxstack->add(vas);
771   boxstack->update(vas);
772 }
773
774 void VVideoRec::doBar(int action_in)
775 {
776   if (player->isSubtitlesOn()) clearOSD(); // remove dvbsubtitles
777   player->tellSubtitlesOSDVisible(true);
778   barShowing = true;
779
780   int action = action_in;
781   if (action == -1) {
782           action = lastbar;
783   }
784   else {
785           lastbar = action;
786   }
787
788   rectangle(barRegion, barBlue);
789
790   /* Work out what to display - choices:
791
792   Playing  >
793   Paused   ||
794   FFwd     >>
795   FBwd     <<
796
797   Specials, informed by parameter
798
799   Skip forward 10s    >|
800   Skip backward 10s   |<
801   Skip forward 1m     >>|
802   Skip backward 1m    |<<
803
804   */
805
806   WSymbol w;
807   TEMPADD(&w);
808   w.nextSymbol = 0;
809   w.setPosition(barRegion.x + 66, barRegion.y + 16);
810
811   UCHAR playerState = 0;
812
813   if (action)
814   {
815     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
816     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
817     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
818     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
819   }
820   else
821   {
822     playerState = player->getState();
823     if (playerState == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
824     else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
825     else if (playerState == Player::S_FFWD)    w.nextSymbol = WSymbol::FFWD;
826     else if (playerState == Player::S_FBWD)    w.nextSymbol = WSymbol::FBWD;
827     else                                       w.nextSymbol = WSymbol::PLAY;
828   }
829
830   w.draw();
831
832   if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD))
833   {
834     // draw blips to show how fast the scan is
835     UCHAR scanrate = player->getIScanRate();
836     if (scanrate >= 2)
837     {
838       char text[5];
839       SNPRINTF(text, 5, "%ux", scanrate);
840       drawText(text, barRegion.x + 102, barRegion.y + 12, DrawStyle::LIGHTTEXT);
841     }
842   }
843
844   drawBarClocks();
845
846   boxstack->update(this, &barRegion);
847
848   if (action_in != -1) {
849           timers->cancelTimer(this, 1);
850
851
852           if ((playerState == Player::S_FFWD) || (playerState == Player::S_FBWD)) barScanHold = true;
853           else barScanHold = false;
854
855           if (!barGenHold && !barScanHold && !barVasHold) timers->setTimerD(this, 1, 4);
856
857           timers->setTimerD(this, 2, 0, 200000000);
858   }
859 }
860
861 void VVideoRec::timercall(int clientReference)
862 {
863   switch(clientReference)
864   {
865     case 1:
866     {
867       // Remove bar
868       removeBar();
869       break;
870     }
871     case 2:
872     {
873       // Update clock
874       if (!barShowing) break;
875 #ifndef GRADIENT_DRAWING
876       drawBarClocks();
877       boxstack->update(this, &barRegion);
878 #else 
879           doBar(-1);
880 #endif
881       timers->setTimerD(this, 2, 0, 200000000);
882       break;
883     }
884   }
885 }
886
887 void VVideoRec::drawBarClocks()
888 {
889   if (barScanHold)
890   {
891     UCHAR playerState = player->getState();
892     // sticky bar is set if we are in ffwd/fbwd mode
893     // if player has gone to S_PLAY then kill stickyBar, and run doBar(0) which
894     // will repaint all the bar (it will call this function again, but
895     // this section won't run because stickyBarF will then == false)
896
897     if ((playerState != Player::S_FFWD) && (playerState != Player::S_FBWD))
898     {
899       barScanHold = false;
900       doBar(0);
901       return; // doBar will call this function and do the rest
902     }
903   }
904
905   Log* logger = Log::getInstance();
906   logger->log("VVideoRec", Log::DEBUG, "Draw bar clocks");
907
908   // Draw RTC
909   // Blank the area first
910 #ifndef GRADIENT_DRAWING
911   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
912 #endif
913   char timeString[20];
914   time_t t;
915   time(&t);
916   struct tm tms;
917   LOCALTIME_R(&t, &tms);
918   strftime(timeString, 19, "%H:%M", &tms);
919   drawText(timeString, barRegion.x + 624, barRegion.y + 12, DrawStyle::LIGHTTEXT);
920
921   // Draw clocks
922 #ifndef GRADIENT_DRAWING
923   rectangle(clocksRegion, barBlue);
924 #endif
925
926   ULONG currentFrameNum = player->getCurrentFrameNum();
927   ULONG lengthFrames;
928   if (myRec->recInfo->timerEnd > time(NULL))
929   {
930     // chasing playback
931     // Work out an approximate length in frames (good to 1s...)
932     lengthFrames =(ULONG) ((double)(myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * myRec->recInfo->fps);
933   }
934   else
935   {
936     lengthFrames = player->getLengthFrames();
937   }
938
939   hmsf currentFrameHMSF = myRec->recInfo->framesToHMSF(currentFrameNum);
940   hmsf lengthHMSF = myRec->recInfo->framesToHMSF(lengthFrames);
941
942   char buffer[100];
943   if (currentFrameNum >= lengthFrames)
944   {
945     strcpy(buffer, "-:--:-- / -:--:--");
946   }
947   else
948   {
949     SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
950     logger->log("VVideoRec", Log::DEBUG, buffer);
951   }
952
953   drawText(buffer, clocksRegion.x, clocksRegion.y, DrawStyle::LIGHTTEXT);
954
955
956
957
958
959
960
961   // Draw progress bar
962   int progBarXbase = barRegion.x + 300;
963
964   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, DrawStyle::LIGHTTEXT);
965   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
966
967   if (currentFrameNum > lengthFrames) return;
968   if (lengthFrames == 0) return;
969
970   // Draw yellow portion
971   int progressWidth = 302 * currentFrameNum / lengthFrames;
972   rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, DrawStyle::SELECTHIGHLIGHT);
973
974   if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
975   {
976     int nrWidth = (int)(302 * ((double)(lengthFrames - player->getLengthFrames()) / lengthFrames));
977
978     Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
979     Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
980     Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
981     rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, DrawStyle::RED);
982   }
983
984   int posPix;
985   // Now calc position for blips
986
987   if (myRec->hasMarks())
988   {
989     // Draw blips where there are cut marks
990     MarkList* markList = myRec->getMarkList();
991     MarkList::iterator i;
992     Mark* loopMark = NULL;
993
994     for(i = markList->begin(); i != markList->end(); i++)
995     {
996       loopMark = *i;
997       if (loopMark->pos)
998       {
999         logger->log("VVideoRec", Log::DEBUG, "Drawing mark at frame %i", loopMark->pos);
1000         posPix = 302 * loopMark->pos / lengthFrames;
1001         rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 3, 28, DrawStyle::DANGER);
1002       }
1003     }
1004   }
1005   else
1006   {
1007     // Draw blips where start and end margins probably are
1008
1009           posPix =(int) (302. * myRec->recInfo->fps * ((double)startMargin) /((double) lengthFrames));
1010
1011     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, DrawStyle::LIGHTTEXT);
1012     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, DrawStyle::LIGHTTEXT);
1013
1014     posPix = (int)(302. * ((double)lengthFrames - ((double)endMargin) * myRec->recInfo->fps) / ((double)lengthFrames));
1015
1016     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, DrawStyle::LIGHTTEXT);
1017     rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, DrawStyle::LIGHTTEXT);
1018   }
1019 }
1020
1021 void VVideoRec::removeBar()
1022 {
1023   if (!barShowing) return;
1024   timers->cancelTimer(this, 2);
1025   barShowing = false;
1026   barGenHold = false;
1027   barScanHold = false;
1028   barVasHold = false;
1029   rectangle(barRegion, transparent);
1030   boxstack->update(this, &barRegion);
1031   player->tellSubtitlesOSDVisible(false);
1032 }
1033
1034 void VVideoRec::doSummary()
1035 {
1036   clearOSD(); // remove dvbsubtitles
1037   player->tellSubtitlesOSDVisible(true);
1038   vsummary = new VInfo();
1039   vsummary->setTitleText(myRec->getProgName());
1040   vsummary->setBorderOn(1);
1041   vsummary->setExitable();
1042   if (myRec->recInfo->summary) vsummary->setMainText(myRec->recInfo->summary);
1043   else vsummary->setMainText(tr("Summary unavailable"));
1044   if (Video::getInstance()->getFormat() == Video::PAL)
1045   {
1046     vsummary->setPosition(120, 130);
1047   }
1048   else
1049   {
1050     vsummary->setPosition(110, 90);
1051   }
1052   vsummary->setSize(510, 270);
1053   add(vsummary);
1054   vsummary->draw();
1055
1056   BoxStack::getInstance()->update(this);
1057 }
1058
1059 void VVideoRec::removeSummary()
1060 {
1061   if (vsummary)
1062   {
1063     remove(vsummary);
1064     delete vsummary;
1065     vsummary = NULL;
1066     draw();
1067     BoxStack::getInstance()->update(this);
1068     player->tellSubtitlesOSDVisible(false);
1069   }
1070 }
1071
1072 void VVideoRec::drawOSDBitmap(UINT posX, UINT posY, const Bitmap& bm, const DisplayRegion& region)
1073 {
1074   drawBitmap(posX, posY, bm, region);
1075   Region r;
1076   r.x = posX; r.y = posY; r.w = bm.getWidth(); r.h = bm.getHeight();
1077   boxstack->update(this, &r);
1078 }
1079
1080 void VVideoRec::clearOSD()
1081 {
1082   rectangle(area, transparent);
1083   boxstack->update(this, &area);
1084 }
1085
1086 void VVideoRec::clearOSDArea(UINT posX, UINT posY, UINT width, UINT height, const DisplayRegion& region)
1087 {
1088   Region r;
1089   r.x = posX+region.windowx; r.y = posY+region.windowy; r.w = width; r.h = height;
1090   //now convert to our display
1091   float scalex=720.f/((float) (region.framewidth+1));
1092   float scaley=576.f/((float) (region.frameheight+1));
1093   r.x=(UINT)floor(scalex*((float)r.x));
1094   r.y=(UINT)floor(scaley*((float)r.y));
1095   r.w=(UINT)(ceil(scalex*((float)r.w))+1.f);
1096   r.h=(UINT)(ceil(scaley*((float)r.h))+1.f);
1097
1098   rectangle(r, transparent);
1099   boxstack->update(this, &r);
1100 }