]> git.vomp.tv Git - vompclient.git/blob - vradiorec.cc
Update for windows
[vompclient.git] / vradiorec.cc
1 /*
2     Copyright 2004-2006 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "vradiorec.h"
22
23 VRadioRec::VRadioRec(Recording* rec)
24 {
25   vdr = VDR::getInstance();
26   video = Video::getInstance();
27   timers = Timers::getInstance();
28   myRec = rec;
29   playing = false;
30   startMargin = 0;
31   endMargin = 0;
32
33   player = new PlayerRadio(Command::getInstance(), this, true);
34
35   char* cstartMargin = vdr->configLoad("Timers", "Start margin");
36   char* cendMargin = vdr->configLoad("Timers", "End margin");
37   if (!cstartMargin)
38   {
39     startMargin = 300; // 5 mins default
40   }
41   else
42   {
43     startMargin = atoi(cstartMargin) * 60;
44     delete[] cstartMargin;
45   }
46
47   if (!cendMargin)
48   {
49     endMargin = 300; // 5 mins default
50   }
51   else
52   {
53     endMargin = atoi(cendMargin) * 60;
54     delete[] cendMargin;
55   }
56
57   Log::getInstance()->log("VRadioRec", Log::DEBUG, "SM: %u EM: %u", startMargin, endMargin);
58
59   create(video->getScreenWidth(), video->getScreenHeight());
60   setBackgroundColour(Colour::BLACK);
61
62   barRegion.x = 0;
63   barRegion.y = video->getScreenHeight() - 58;   // FIXME, need to be - 1? and below?
64   barRegion.w = video->getScreenWidth();
65   barRegion.h = 58;
66
67   clocksRegion.x = barRegion.x + 140;
68   clocksRegion.y = barRegion.y + 12;
69   clocksRegion.w = 170;
70   clocksRegion.h = surface->getFontHeight();
71
72
73   barBlue.set(0, 0, 150, 150);
74
75   barShowing = false;
76 }
77
78 VRadioRec::~VRadioRec()
79 {
80   if (playing) stopPlay();
81
82   timers->cancelTimer(this, 1);
83   timers->cancelTimer(this, 2);
84
85   // kill recInfo in case resumePoint has changed (likely)
86   myRec->dropRecInfo();
87   // FIXME - do this properly - save the resume point back to the server manually and update
88   // rec->recInfo->resumePoint - this will fix the ~10s offset problem as well
89 }
90
91 void VRadioRec::draw()
92 {
93   View::draw();
94 }
95
96 void VRadioRec::go()
97 {
98   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Starting stream: %s", myRec->getFileName());
99   ULONG lengthFrames = 0;
100   ULLONG lengthBytes = vdr->streamRecording(myRec->getFileName(), &lengthFrames);
101
102   bool cantStart = false;
103
104   if (!lengthBytes) cantStart = true;
105   else if (!player->init(lengthBytes, lengthFrames)) cantStart = true;
106   else
107   {
108     doBar(0);
109   //  player->setStartBytes(startBytes);
110     player->play();
111     playing = true;
112   }
113
114   if (cantStart)
115   {
116     stopPlay(); // clean up
117
118     if (!vdr->isConnected())
119     {
120       Command::getInstance()->connectionLost();
121       return;
122     }
123
124     ViewMan* viewman = ViewMan::getInstance();
125
126     Message* m = new Message();
127     m->message = Message::CLOSE_ME;
128     m->from = this;
129     m->to = viewman;
130     Command::getInstance()->postMessageNoLock(m);
131
132     VInfo* vi = new VInfo();
133     vi->create(400, 150);
134     if (video->getFormat() == Video::PAL)
135       vi->setScreenPos(170, 200);
136     else
137       vi->setScreenPos(160, 150);
138     vi->setExitable();
139     vi->setBorderOn(1);
140     vi->setTitleBarOn(0);
141     vi->setOneLiner(tr("Error playing recording"));
142     vi->draw();
143
144     m = new Message();
145     m->message = Message::ADD_VIEW;
146     m->to = viewman;
147     m->parameter = (ULONG)vi;
148     Command::getInstance()->postMessageNoLock(m);
149   }
150 }
151
152 int VRadioRec::handleCommand(int command)
153 {
154   switch(command)
155   {
156     case Remote::PLAY:
157     {
158       player->play();
159       doBar(0);
160       return 2;
161     }
162
163     case Remote::STOP:
164     case Remote::BACK:
165     case Remote::MENU:
166     {
167       if (playing) stopPlay();
168       return 4;
169     }
170     case Remote::PAUSE:
171     {
172       player->pause();
173       doBar(0);
174       return 2;
175     }
176     case Remote::SKIPFORWARD:
177     {
178       doBar(3);
179       player->skipForward(60);
180       return 2;
181     }
182     case Remote::SKIPBACK:
183     {
184       doBar(4);
185       player->skipBackward(60);
186       return 2;
187     }
188     case Remote::YELLOW:
189     {
190       doBar(2);
191       player->skipBackward(10);
192       return 2;
193     }
194     case Remote::BLUE:
195     {
196       doBar(1);
197       player->skipForward(10);
198       return 2;
199     }
200     case Remote::OK:
201     {
202       if (barShowing) removeBar();
203       else doBar(0);
204       return 2;
205     }
206
207     case Remote::ZERO:  player->jumpToPercent(0);  doBar(0);  return 2;
208     case Remote::ONE:   player->jumpToPercent(10); doBar(0);  return 2;
209     case Remote::TWO:   player->jumpToPercent(20); doBar(0);  return 2;
210     case Remote::THREE: player->jumpToPercent(30); doBar(0);  return 2;
211     case Remote::FOUR:  player->jumpToPercent(40); doBar(0);  return 2;
212     case Remote::FIVE:  player->jumpToPercent(50); doBar(0);  return 2;
213     case Remote::SIX:   player->jumpToPercent(60); doBar(0);  return 2;
214     case Remote::SEVEN: player->jumpToPercent(70); doBar(0);  return 2;
215     case Remote::EIGHT: player->jumpToPercent(80); doBar(0);  return 2;
216     case Remote::NINE:  player->jumpToPercent(90); doBar(0);  return 2;
217
218 #ifdef DEV
219     case Remote::RED:
220     {
221       //player->test1();
222
223
224       /*
225       // for testing EPG in NTSC with a NTSC test video
226       Video::getInstance()->setMode(Video::QUARTER);
227       Video::getInstance()->setPosition(170, 5);
228       VEpg* vepg = new VEpg(NULL, 0);
229       vepg->draw();
230       ViewMan::getInstance()->add(vepg);
231       ViewMan::getInstance()->updateView(vepg);
232       */
233
234       return 2;
235     }
236     case Remote::GREEN:
237     {
238       //player->test2();
239       return 2;
240     }
241 #endif
242
243   }
244
245   return 1;
246 }
247
248 void VRadioRec::processMessage(Message* m)
249 {
250   if (m->from != player) return;
251   if (m->message != Message::PLAYER_EVENT) return;
252
253   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Message received");
254
255   switch(m->parameter)
256   {
257     case Player::CONNECTION_LOST: // connection lost detected
258     {
259       // I can't handle this, send it to command
260       Message* m = new Message();
261       m->to = Command::getInstance();
262       m->message = Message::CONNECTION_LOST;
263       Command::getInstance()->postMessageNoLock(m);
264       break;
265     }
266     case Player::STOP_PLAYBACK:
267     {
268       // FIXME Obselete ish - improve this
269       Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
270       m->to = Command::getInstance();
271       m->message = Message::STOP_PLAYBACK;
272       Command::getInstance()->postMessageNoLock(m);
273       break;
274     }
275   }
276 }
277
278 void VRadioRec::stopPlay()
279 {
280   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Pre stopPlay");
281
282   // FIXME work out a better soln for this
283   // Fix a problem to do with thread sync here
284   // because the bar gets a timer every 0.2s and it seems to take up to 0.1s,
285   // (or maybe just the wrong thread being selected?) for the main loop to lock and process
286   // the video stop message it is possible for a bar message to stack up after a stop message
287   // when the bar message is finally processed the prog crashes because this is deleted by then
288   removeBar();
289   //
290
291   player->stop();
292   vdr->stopStreaming();
293   delete player;
294
295   playing = false;
296
297   if (!vdr->isConnected()) { Command::getInstance()->connectionLost(); return; }
298   Log::getInstance()->log("VRadioRec", Log::DEBUG, "Post stopPlay");
299 }
300
301 void VRadioRec::doBar(int action)
302 {
303   barShowing = true;
304
305   rectangle(barRegion, barBlue);
306
307   /* Work out what to display - choices:
308
309   Playing  >
310   Paused   ||
311
312   Specials, informed by parameter
313
314   Skip forward 10s    >|
315   Skip backward 10s   |<
316   Skip forward 1m     >>|
317   Skip backward 1m    |<<
318
319   */
320
321   WSymbol w;
322   w.setSurface(surface);
323   w.nextSymbol = 0;
324   w.setSurfaceOffset(barRegion.x + 66, barRegion.y + 16);
325
326   UCHAR playerState = 0;
327
328   if (action)
329   {
330     if (action == 1)       w.nextSymbol = WSymbol::SKIPFORWARD;
331     else if (action == 2)  w.nextSymbol = WSymbol::SKIPBACK;
332     else if (action == 3)  w.nextSymbol = WSymbol::SKIPFORWARD2;
333     else if (action == 4)  w.nextSymbol = WSymbol::SKIPBACK2;
334   }
335   else
336   {
337     playerState = player->getState();
338     if (playerState == Player::S_PAUSE_P)      w.nextSymbol = WSymbol::PAUSE;
339     else if (playerState == Player::S_PAUSE_I) w.nextSymbol = WSymbol::PAUSE;
340     else                                       w.nextSymbol = WSymbol::PLAY;
341   }
342
343   w.draw();
344
345   drawBarClocks();
346
347   ViewMan::getInstance()->updateView(this, &barRegion);
348
349   timers->setTimerD(this, 1, 4); // only set the getridofbar timer if not ffwd/fbwd
350   timers->setTimerD(this, 2, 0, 200000000);
351 }
352
353 void VRadioRec::timercall(int clientReference)
354 {
355   switch(clientReference)
356   {
357     case 1:
358     {
359       // Remove bar
360       removeBar();
361       break;
362     }
363     case 2:
364     {
365       // Update clock
366       if (!barShowing) break;
367       drawBarClocks();
368       ViewMan::getInstance()->updateView(this, &barRegion);
369       timers->setTimerD(this, 2, 0, 200000000);
370       break;
371     }
372   }
373 }
374
375 void VRadioRec::drawBarClocks()
376 {
377   Log* logger = Log::getInstance();
378   logger->log("VRadioRec", Log::DEBUG, "Draw bar clocks");
379
380   // Draw RTC
381   // Blank the area first
382   rectangle(barRegion.x + 624, barRegion.y + 12, 60, 30, barBlue);
383   char timeString[20];
384   time_t t;
385   time(&t);
386   struct tm* tms = localtime(&t);
387   strftime(timeString, 19, "%H:%M", tms);
388   drawText(timeString, barRegion.x + 624, barRegion.y + 12, Colour::LIGHTTEXT);
389
390   // Draw clocks
391
392   rectangle(clocksRegion, barBlue);
393
394 /*
395   ULONG currentFrameNum = player->getCurrentFrameNum();
396   ULONG lengthFrames;
397   if (myRec->recInfo->timerEnd > time(NULL))
398   {
399     // chasing playback
400     // Work out an approximate length in frames (good to 1s...)
401     lengthFrames = (myRec->recInfo->timerEnd - myRec->recInfo->timerStart) * video->getFPS();
402   }
403   else
404   {
405 //    lengthFrames = player->getLengthFrames();
406     lengthFrames = 0;
407   }
408
409   hmsf currentFrameHMSF = video->framesToHMSF(currentFrameNum);
410   hmsf lengthHMSF = video->framesToHMSF(lengthFrames);
411
412   char buffer[100];
413   if (currentFrameNum >= lengthFrames)
414   {
415     strcpy(buffer, "-:--:-- / -:--:--");
416   }
417   else
418   {
419     SNPRINTF(buffer, 99, "%01i:%02i:%02i / %01i:%02i:%02i", currentFrameHMSF.hours, currentFrameHMSF.minutes, currentFrameHMSF.seconds, lengthHMSF.hours, lengthHMSF.minutes, lengthHMSF.seconds);
420     logger->log("VRadioRec", Log::DEBUG, buffer);
421   }
422 */
423
424   ULONG currentSeconds = player->getCurrentSeconds();
425   ULONG lengthSeconds = player->getLengthSeconds();
426   char buffer[100];
427
428   if (lengthSeconds && (currentSeconds < lengthSeconds))
429   {
430     ULONG dcurrentSeconds = currentSeconds;
431     ULONG dlengthSeconds = lengthSeconds;
432
433     ULONG currentHours = dcurrentSeconds / 3600;
434     dcurrentSeconds %= 3600;
435     ULONG currentMinutes = dcurrentSeconds / 60;
436     dcurrentSeconds %= 60;
437
438     ULONG lengthHours = dlengthSeconds / 3600;
439     dlengthSeconds %= 3600;
440     ULONG lengthMinutes = dlengthSeconds / 60;
441     dlengthSeconds %= 60;
442
443     SNPRINTF(buffer, 99, "%01lu:%02lu:%02lu / %01lu:%02lu:%02lu", currentHours, currentMinutes, dcurrentSeconds, lengthHours, lengthMinutes, dlengthSeconds);
444     logger->log("VRadioRec", Log::DEBUG, buffer);
445   }
446   else
447   {
448     strcpy(buffer, "-:--:-- / -:--:--");
449   }
450
451   drawText(buffer, clocksRegion.x, clocksRegion.y, Colour::LIGHTTEXT);
452
453
454
455
456   // Draw progress bar
457   int progBarXbase = barRegion.x + 300;
458
459   rectangle(barRegion.x + progBarXbase, barRegion.y + 12, 310, 24, Colour::LIGHTTEXT);
460   rectangle(barRegion.x + progBarXbase + 2, barRegion.y + 14, 306, 20, barBlue);
461
462   if (currentSeconds > lengthSeconds) return;
463   if (lengthSeconds == 0) return;
464
465   // Draw yellow portion
466   int progressWidth = 302 * currentSeconds / lengthSeconds;
467   rectangle(barRegion.x + progBarXbase + 4, barRegion.y + 16, progressWidth, 16, Colour::SELECTHIGHLIGHT);
468 /*
469
470   if (myRec->recInfo->timerEnd > time(NULL)) // if chasing
471   {
472     int nrWidth = (int)(302 * ((double)(lengthFrames - 0) / lengthFrames)); // 0 inserted instead of getlengthframes
473
474     Log::getInstance()->log("GVASDF", Log::DEBUG, "Length Frames: %lu", lengthFrames);
475 //    Log::getInstance()->log("GVASDF", Log::DEBUG, "Player lf: %lu", player->getLengthFrames());
476     Log::getInstance()->log("GVASDF", Log::DEBUG, "NR WDITH: %i", nrWidth);
477     rectangle(barRegion.x + progBarXbase + 4 + 302 - nrWidth, barRegion.y + 16, nrWidth, 16, Colour::RED);
478   }
479 */
480
481   logger->log("VRadioRec", Log::DEBUG, "blips");
482
483   // Now calc position for start margin blips
484   int posPix;
485
486   posPix = 302 * startMargin / lengthSeconds;
487   logger->log("VRadioRec", Log::DEBUG, "posPix %i", posPix);
488
489   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
490   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
491
492   posPix = 302 * (lengthSeconds - endMargin) / lengthSeconds;
493
494   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 - 2, 2, 2, Colour::LIGHTTEXT);
495   rectangle(barRegion.x + progBarXbase + 2 + posPix, barRegion.y + 12 + 24, 2, 2, Colour::LIGHTTEXT);
496 }
497
498 void VRadioRec::removeBar()
499 {
500   if (!barShowing) return;
501   timers->cancelTimer(this, 2);
502   barShowing = false;
503   rectangle(barRegion, Colour::BLACK);
504   ViewMan::getInstance()->updateView(this, &barRegion);
505 }