]> git.vomp.tv Git - vompclient.git/blob - player.cc
Connection failure detection
[vompclient.git] / player.cc
1 /*
2     Copyright 2004-2005 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 "player.h"
22
23 Player::Player(MessageQueue* messageQueue, UCHAR tIsRecording, UCHAR isRadio)
24 : vfeed(this), afeed(this)
25 {
26   commandMessageQueue = messageQueue;
27   audio = Audio::getInstance();
28   video = Video::getInstance();
29   logger = Log::getInstance();
30   initted = 0;
31   paused = false;
32   playing = false;
33   ffwd = false;
34   fbwd = false;
35   streamLength = 0;
36   feedPosition = 0;
37   feedMode = MODE_NORMAL;
38   isRecording = tIsRecording;
39   lastRescan = 0;
40   startTS = 0;
41   endTS = 0;
42   startup = 1;
43   threadBuffer = NULL;
44
45   if (isRadio)
46   {
47     blockSize = 20000;
48     startupBlockSize = 60000;
49   }
50   else
51   {
52     blockSize = 100000;
53     startupBlockSize = 250000;
54   }
55 }
56
57 Player::~Player()
58 {
59   if (initted) shutdown();
60 }
61
62 int Player::init()
63 {
64   if (initted) return 0;
65
66   if (!demuxer.init(this))
67   {
68     logger->log("Player", Log::ERR, "Demuxer failed to init");
69     shutdown();
70     return 0;
71   }
72
73   vfeed.init(video->getFD());
74   afeed.init(audio->getFD());
75
76   video->stop();
77   video->blank();
78   audio->stop();
79
80   initted = 1;
81   return 1;
82 }
83
84 int Player::shutdown()
85 {
86   if (!initted) return 0;
87   initted = 0;
88
89   logger->log("Player", Log::DEBUG, "Player shutdown...");
90
91   // copy of stop
92   if (playing)
93   {
94     playing = false;
95     threadStop();
96     video->stop();
97     video->blank();
98     audio->stop();
99     vfeed.stop();
100     afeed.stop();
101     video->reset();
102     demuxer.reset();
103     feedPosition = 0;
104   }
105   logger->log("Player", Log::DEBUG, "Player shutdown done");
106
107   return 1;
108 }
109
110 void Player::resyncAudio()
111 {
112   // Temp hopefully
113   if (!initted) return;
114   audio->pause();
115   usleep(500000);
116   audio->unPause();
117 }
118
119 void Player::resyncVideo()
120 {
121   // Temp hopefully
122   if (!initted) return;
123   video->pause();
124   usleep(500000);
125   video->unPause();
126 }
127
128 int Player::play()
129 {
130   if (!initted) return 0;
131
132   // If we are just paused, unpause!
133   if (paused)
134   {
135     togglePause();
136     return 1;
137   }
138
139   // If we are fast forwarding, set to normal
140   if (ffwd)
141   {
142     toggleFastForward();
143     return 1;
144   }
145
146   // If we are fast backwarding, set to normal
147   if (fbwd)
148   {
149     toggleFastBackward();
150     return 1;
151   }
152
153   // If we are already playing, bail // FIXME - resync?
154   if (playing)
155   {
156     logger->log("Player", Log::DEBUG, "DOING RESYNC");
157     resyncVideo();
158     return 1;
159   }
160
161   // Standard play start
162   logger->log("Player", Log::DEBUG, "Standard play start");
163
164   audio->reset();
165   video->reset();
166   demuxer.reset();
167
168 // ------------------------ This one works, but doesn't allow any pre-buffering.
169   threadStart();
170   vfeed.start();
171   afeed.start();
172   audio->play();
173   video->play();
174   video->sync();
175   audio->sync();
176
177   resyncVideo();
178 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
179
180 /*
181   threadStart();
182 //  sleep(1);
183
184 //  struct timespec delay;
185 //  delay.tv_sec = 1;
186 //  delay.tv_nsec = 500000000;
187 //  nanosleep(&delay, NULL);
188
189   vfeed.start();
190   afeed.start();
191   video->play();
192   audio->play();
193   video->sync();
194   audio->sync();
195
196   video->pause();
197       usleep(500000); // SYNC
198   video->sync();
199   video->unPause();
200   video->sync();
201 */
202
203 // ------------------------------------------------------------------------------------------------
204
205   playing = true;
206   return 1;
207 }
208
209 void Player::stop()
210 {
211   if (!initted) return;
212   if (!playing) return;
213
214   logger->log("Player", Log::DEBUG, "Stop called", streamLength);
215
216   if (ffwd || fbwd)
217   {
218     ffwd = false;
219     fbwd = false;
220     afeed.enable();
221     video->unFastForward();
222     audio->systemMuteOff();
223     feedMode = MODE_NORMAL;
224   }
225
226   playing = false;
227   paused = false;
228
229   vfeed.stop();
230   afeed.stop();
231   threadStop();
232   video->stop();
233   video->blank();
234   audio->stop();
235   audio->unPause();
236   video->reset();
237   demuxer.reset();
238
239   feedPosition = 0;
240 }
241
242 void Player::togglePause()
243 {
244   if (!initted) return;
245   if (!playing) return;
246
247   if (ffwd) toggleFastForward();
248   if (fbwd) toggleFastBackward();
249
250   if (paused)
251   {
252     video->unPause();
253     audio->unPause();
254     paused = false;
255   }
256   else
257   {
258     video->pause();
259     audio->pause();
260     paused = true;
261   }
262 }
263
264 void Player::setPosition(ULLONG position)
265 {
266   feedPosition = position;
267 }
268
269 void Player::setLength(ULLONG length)
270 {
271   lastRescan = time(NULL);
272   streamLength = length;
273   logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
274 }
275
276 void Player::skipForward(int seconds)
277 {
278   logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
279
280   if (paused) togglePause();
281
282   ULONG wantedFrameNumber = video->timecodeToFrameNumber(getPositionTS() + (seconds * 90000));
283   ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
284   if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
285   logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
286
287   vfeed.stop();
288   afeed.stop();
289   threadStop();
290   video->stop();
291   video->reset();
292   audio->reset();
293   audio->doMuting();  // ???
294   demuxer.flush();
295   feedPosition = newPosition;
296   vfeed.start();
297   afeed.start();
298   threadStart();
299   audio->play();
300   video->play();
301   video->sync();
302   audio->sync();
303
304   resyncVideo();
305 }
306
307 void Player::skipBackward(int seconds)
308 {
309   logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
310
311   if (paused) togglePause();
312
313   ULLONG newPosition = 0;
314
315   long long newTimeCode = getPositionTS() - (seconds * 90000);
316   if (newTimeCode > 0)
317   {
318     ULONG wantedFrameNumber = video->timecodeToFrameNumber((ULLONG)newTimeCode);
319     newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
320     if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
321     logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
322   }
323
324   vfeed.stop();
325   afeed.stop();
326   threadStop();
327   video->stop();
328   audio->stop();
329   video->reset();
330   audio->reset();
331   audio->doMuting(); // ???
332   demuxer.flush();
333   feedPosition = newPosition;
334   vfeed.start();
335   afeed.start();
336   threadStart();
337   audio->play();
338   video->play();
339   video->sync();
340   audio->sync();
341
342   resyncVideo();
343 }
344
345 void Player::toggleFastForward()
346 {
347   if (!initted) return;
348   if (!playing) return;
349
350   if (paused) togglePause();
351   if (fbwd) toggleFastBackward();
352
353   if (ffwd)
354   {
355     ffwd = false;
356 //    video->unFastForward();
357
358
359     vfeed.stop();
360     afeed.stop();
361     threadStop();
362     video->stop();
363     audio->stop();
364     video->reset();
365     audio->reset();
366     demuxer.flush();
367 //    demuxer.seek();
368     vfeed.start();
369     afeed.enable();
370     afeed.start();
371     threadStart();
372     video->play();
373     audio->play();
374     video->sync();
375     audio->sync();
376
377     audio->systemMuteOff();
378
379     resyncVideo();
380 /*
381     demuxer.flushAudio();
382     audio->reset();
383     afeed.enable();
384     //video->reset();
385     audio->play();
386     video->play();
387     video->sync();
388     audio->sync();
389     audio->systemMuteOff();
390 */
391   }
392   else
393   {
394     ffwd = true;
395     afeed.disable();
396     audio->systemMuteOn();
397     video->fastForward();
398   }
399 }
400
401 void Player::toggleFastBackward()
402 {
403   if (!initted) return;
404   if (!playing) return;
405
406   if (paused) togglePause();
407   if (ffwd) toggleFastForward();
408
409   if (fbwd)
410   {
411     fbwd = false;
412     afeed.enable();
413     audio->systemMuteOff();
414
415 //    threadStop();
416     feedMode = MODE_NORMAL;
417 //    threadStart();
418   }
419   else
420   {
421     fbwd = false;
422     afeed.disable();
423     audio->systemMuteOn();
424
425     threadStop();
426     feedMode = MODE_BACKWARDS;
427     video->reset();
428     video->play();
429     demuxer.flush();
430     threadStart();
431   }
432 }
433
434 void Player::jumpToPercent(int percent)
435 {
436   if (paused) togglePause();
437   if (ffwd) toggleFastForward();
438
439   vfeed.stop();
440   afeed.stop();
441   threadStop();
442   video->stop();
443   audio->stop();
444   video->reset();
445   audio->reset();
446   demuxer.flush();
447   demuxer.seek();
448   feedPosition = streamLength * percent / 100;
449   vfeed.start();
450   afeed.start();
451   threadStart();
452   audio->play();
453   video->play();
454   video->sync();
455   audio->sync();
456
457   resyncVideo();
458 }
459
460 ULLONG Player::getPositionTS()
461 {
462   if (startup) return 0ULL;
463   long long currentTS = video->getCurrentTimestamp() - startTS;
464   if (currentTS < 0) currentTS += 8589934592ULL;
465   return (ULLONG)currentTS;
466 }
467
468 ULLONG Player::getEndTS()
469 {
470   long long rendTS = endTS - startTS;
471   if (rendTS < 0) rendTS += 8589934592ULL;
472   return (ULLONG)rendTS;
473 }
474
475 void Player::call(void* caller)
476 {
477   if (caller == &demuxer)
478   {
479     logger->log("Player", Log::DEBUG, "Callback from demuxer");
480
481     if (video->getTVsize() == Video::ASPECT4X3)
482     {
483       logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
484       return;
485     }
486
487     int dxCurrentAspect = demuxer.getAspectRatio();
488     if (dxCurrentAspect == Demuxer::ASPECT_4_3)
489     {
490       logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
491       video->setAspectRatio(Video::ASPECT4X3);
492     }
493     else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
494     {
495       logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
496       video->setAspectRatio(Video::ASPECT16X9);
497     }
498     else
499     {
500       logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
501     }
502
503   }
504   else
505   {
506     threadSignalNoLock();
507   }
508 }
509
510 void Player::doConnectionLost()
511 {
512   Message* m = new Message();
513   m->message = Message::CONNECTION_LOST;
514   m->to = this;
515   commandMessageQueue->postMessage(m);
516 }
517
518 // Feed thread
519
520 void Player::threadMethod()
521 {
522   UINT thisRead;
523   UINT writeLength;
524   UINT thisWrite;
525
526   VDR* vdr = VDR::getInstance();
527
528   UINT askFor;
529   while(1)
530   {
531     thisRead = 0;
532     writeLength = 0;
533     thisWrite = 0;
534
535     threadCheckExit();
536
537     // If we havn't rescanned for a while..
538     if (isRecording && ((lastRescan + 60) < time(NULL)))
539     {
540       streamLength = vdr->rescanRecording();
541       if (!vdr->isConnected()) { doConnectionLost(); return; }
542       logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
543       lastRescan = time(NULL);
544       setEndTS();
545     }
546
547     if (streamLength) // is playing a recording
548     {
549       if (feedPosition >= streamLength) break;  // finished playback
550
551       if (startup)
552       {
553         if (startupBlockSize > streamLength)
554           askFor = streamLength; // is a very small recording!
555         else
556           askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
557       }
558       else
559       {
560         if ((feedPosition + blockSize) > streamLength) // last block of recording
561           askFor = streamLength - feedPosition;
562         else // normal
563           askFor = blockSize;
564       }
565     }
566     else // is playing live
567     {
568       if (startup)
569         askFor = startupBlockSize; // find audio streams sized block
570       else
571         askFor = blockSize; // normal
572     }
573
574     threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
575     if (!vdr->isConnected())
576     {
577       doConnectionLost();
578       return;
579     }
580
581     if (!threadBuffer) break;
582
583     if (startup)
584     {
585       int a_stream = demuxer.scan(threadBuffer, thisRead);
586       demuxer.setAudioStream(a_stream);
587       logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
588
589       setStartTS(thisRead);
590
591       if (isRecording) setEndTS();
592
593       startup = 0;
594     }
595
596     if (feedMode == MODE_NORMAL)
597     {
598       feedPosition += thisRead;
599     }
600     else if (feedMode == MODE_BACKWARDS)
601     {
602       if (feedPosition >= blockSize)
603       {
604         feedPosition -= blockSize;
605         demuxer.seek();
606       }
607       else
608       {
609         // got to the start of the recording.. revert to play mode? how?
610         feedPosition += thisRead;
611       }
612     }
613
614     threadCheckExit();
615
616     while(writeLength < thisRead)
617     {
618       thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
619       writeLength += thisWrite;
620
621       if (!thisWrite)
622       {
623 //        logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
624         // demuxer is full and cant take anymore
625         threadLock();
626         threadWaitForSignal();
627         threadUnlock();
628 //        logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
629       }
630
631       threadCheckExit();
632     }
633
634     free(threadBuffer);
635     threadBuffer = NULL;
636
637   }
638
639   // end of recording
640   logger->log("Player", Log::DEBUG, "Recording playback ends");
641
642   threadCheckExit();
643
644   Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
645   if (streamLength) m->message = Message::STOP_PLAYBACK;  // recording
646   else m->message = Message::STREAM_END;                  // live
647   logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
648   commandMessageQueue->postMessage(m);
649   logger->log("Player", Log::DEBUG, "Message posted...");
650 }
651
652 void Player::threadPostStopCleanup()
653 {
654   logger->log("Player", Log::DEBUG, "Post stop cleanup 1");
655   if (threadBuffer)
656   {
657     logger->log("Player", Log::DEBUG, "Post stop cleanup 2");
658     free(threadBuffer);
659     threadBuffer = NULL;
660   }
661   logger->log("Player", Log::DEBUG, "Post stop cleanup 3");
662 }
663
664 void Player::setStartTS(UINT dataInBuffer)
665 {
666   if (isRecording && feedPosition) // (feedPosition != 0)
667   {
668     // FIXME find out how much data need to get to find a TS
669     // Need to get the actual start of the recording
670
671     UINT thisRead;
672     UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
673     if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
674     if (!tempBuffer) return;
675     if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
676     free(tempBuffer);
677   }
678   else
679   {
680     demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
681   }
682 }
683
684 void Player::setEndTS()
685 {
686   logger->log("Player", Log::DEBUG, "Setting end TS");
687
688   UINT thisRead;
689   UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
690   if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
691   if (!tempBuffer) return;
692   if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
693   free(tempBuffer);
694   logger->log("Player", Log::DEBUG, "Set end TS");
695 }
696
697 #ifdef DEV
698 void Player::test1()
699 {
700   logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
701 }
702
703 void Player::test2()
704 {
705   logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
706 }
707 #endif