]> git.vomp.tv Git - vompclient-marten.git/blob - player.cc
Connection failure detection
[vompclient-marten.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   logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
285
286   vfeed.stop();
287   afeed.stop();
288   threadStop();
289   video->stop();
290   video->reset();
291   audio->reset();
292   audio->doMuting();  // ???
293   demuxer.flush();
294   feedPosition = newPosition;
295   vfeed.start();
296   afeed.start();
297   threadStart();
298   audio->play();
299   video->play();
300   video->sync();
301   audio->sync();
302
303   resyncVideo();
304 }
305
306 void Player::skipBackward(int seconds)
307 {
308   logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
309
310   if (paused) togglePause();
311
312   ULLONG newPosition = 0;
313
314   long long newTimeCode = getPositionTS() - (seconds * 90000);
315   if (newTimeCode > 0)
316   {
317     ULONG wantedFrameNumber = video->timecodeToFrameNumber((ULLONG)newTimeCode);
318     newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
319     logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
320   }
321
322   vfeed.stop();
323   afeed.stop();
324   threadStop();
325   video->stop();
326   audio->stop();
327   video->reset();
328   audio->reset();
329   audio->doMuting(); // ???
330   demuxer.flush();
331   feedPosition = newPosition;
332   vfeed.start();
333   afeed.start();
334   threadStart();
335   audio->play();
336   video->play();
337   video->sync();
338   audio->sync();
339
340   resyncVideo();
341 }
342
343 void Player::toggleFastForward()
344 {
345   if (!initted) return;
346   if (!playing) return;
347
348   if (paused) togglePause();
349   if (fbwd) toggleFastBackward();
350
351   if (ffwd)
352   {
353     ffwd = false;
354 //    video->unFastForward();
355
356
357     vfeed.stop();
358     afeed.stop();
359     threadStop();
360     video->stop();
361     audio->stop();
362     video->reset();
363     audio->reset();
364     demuxer.flush();
365 //    demuxer.seek();
366     vfeed.start();
367     afeed.enable();
368     afeed.start();
369     threadStart();
370     video->play();
371     audio->play();
372     video->sync();
373     audio->sync();
374
375     audio->systemMuteOff();
376
377     resyncVideo();
378 /*
379     demuxer.flushAudio();
380     audio->reset();
381     afeed.enable();
382     //video->reset();
383     audio->play();
384     video->play();
385     video->sync();
386     audio->sync();
387     audio->systemMuteOff();
388 */
389   }
390   else
391   {
392     ffwd = true;
393     afeed.disable();
394     audio->systemMuteOn();
395     video->fastForward();
396   }
397 }
398
399 void Player::toggleFastBackward()
400 {
401   if (!initted) return;
402   if (!playing) return;
403
404   if (paused) togglePause();
405   if (ffwd) toggleFastForward();
406
407   if (fbwd)
408   {
409     fbwd = false;
410     afeed.enable();
411     audio->systemMuteOff();
412
413 //    threadStop();
414     feedMode = MODE_NORMAL;
415 //    threadStart();
416   }
417   else
418   {
419     fbwd = false;
420     afeed.disable();
421     audio->systemMuteOn();
422
423     threadStop();
424     feedMode = MODE_BACKWARDS;
425     video->reset();
426     video->play();
427     demuxer.flush();
428     threadStart();
429   }
430 }
431
432 void Player::jumpToPercent(int percent)
433 {
434   if (paused) togglePause();
435   if (ffwd) toggleFastForward();
436
437   vfeed.stop();
438   afeed.stop();
439   threadStop();
440   video->stop();
441   audio->stop();
442   video->reset();
443   audio->reset();
444   demuxer.flush();
445   demuxer.seek();
446   feedPosition = streamLength * percent / 100;
447   vfeed.start();
448   afeed.start();
449   threadStart();
450   audio->play();
451   video->play();
452   video->sync();
453   audio->sync();
454
455   resyncVideo();
456 }
457
458 ULLONG Player::getPositionTS()
459 {
460   if (startup) return 0ULL;
461   long long currentTS = video->getCurrentTimestamp() - startTS;
462   if (currentTS < 0) currentTS += 8589934592ULL;
463   return (ULLONG)currentTS;
464 }
465
466 ULLONG Player::getEndTS()
467 {
468   long long rendTS = endTS - startTS;
469   if (rendTS < 0) rendTS += 8589934592ULL;
470   return (ULLONG)rendTS;
471 }
472
473 void Player::call(void* caller)
474 {
475   if (caller == &demuxer)
476   {
477     logger->log("Player", Log::DEBUG, "Callback from demuxer");
478
479     if (video->getTVsize() == Video::ASPECT4X3)
480     {
481       logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
482       return;
483     }
484
485     int dxCurrentAspect = demuxer.getAspectRatio();
486     if (dxCurrentAspect == Demuxer::ASPECT_4_3)
487     {
488       logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
489       video->setAspectRatio(Video::ASPECT4X3);
490     }
491     else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
492     {
493       logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
494       video->setAspectRatio(Video::ASPECT16X9);
495     }
496     else
497     {
498       logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
499     }
500
501   }
502   else
503   {
504     threadSignalNoLock();
505   }
506 }
507
508 // Feed thread
509
510 void Player::threadMethod()
511 {
512   UINT thisRead;
513   UINT writeLength;
514   UINT thisWrite;
515
516   VDR* vdr = VDR::getInstance();
517
518   UINT askFor;
519   while(1)
520   {
521     thisRead = 0;
522     writeLength = 0;
523     thisWrite = 0;
524
525     threadCheckExit();
526
527     // If we havn't rescanned for a while..
528     if (isRecording && ((lastRescan + 60) < time(NULL)))
529     {
530       streamLength = vdr->rescanRecording();
531       if (!vdr->isConnected())
532       {
533         Message* m = new Message();
534         m->message = Message::CONNECTION_LOST;
535         m->to = this;
536         commandMessageQueue->postMessage(m);
537         return;
538       }
539       logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
540       lastRescan = time(NULL);
541       setEndTS();
542     }
543
544     if (streamLength) // is playing a recording
545     {
546       if (feedPosition >= streamLength) break;  // finished playback
547
548       if (startup)
549       {
550         if (startupBlockSize > streamLength)
551           askFor = streamLength; // is a very small recording!
552         else
553           askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
554       }
555       else
556       {
557         if ((feedPosition + blockSize) > streamLength) // last block of recording
558           askFor = streamLength - feedPosition;
559         else // normal
560           askFor = blockSize;
561       }
562     }
563     else // is playing live
564     {
565       if (startup)
566         askFor = startupBlockSize; // find audio streams sized block
567       else
568         askFor = blockSize; // normal
569     }
570
571     threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
572     if (!vdr->isConnected())
573     {
574       Message* m = new Message();
575       m->message = Message::CONNECTION_LOST;
576       m->to = this;
577       commandMessageQueue->postMessage(m);
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) return;
674     if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
675     free(tempBuffer);
676   }
677   else
678   {
679     demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
680   }
681 }
682
683 void Player::setEndTS()
684 {
685   logger->log("Player", Log::DEBUG, "Setting end TS");
686
687   UINT thisRead;
688   UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
689   if (!tempBuffer) return;
690   if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
691   free(tempBuffer);
692   logger->log("Player", Log::DEBUG, "Set end TS");
693 }
694
695 #ifdef DEV
696 void Player::test1()
697 {
698   logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
699 }
700
701 void Player::test2()
702 {
703   logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
704 }
705 #endif