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