2 Copyright 2004-2005 Chris Tallon
4 This file is part of VOMP.
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.
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.
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
21 #include "playervideo.h"
23 PlayerVideo::PlayerVideo(MessageQueue* messageQueue, UCHAR tIsRecording, UCHAR isRadio)
24 : vfeed(this), afeed(this)
26 commandMessageQueue = messageQueue;
28 logger = Log::getInstance();
34 feedMode = MODE_NORMAL;
35 isRecording = tIsRecording;
44 startupBlockSize = 60000;
49 startupBlockSize = 250000;
53 PlayerVideo::~PlayerVideo()
55 if (initted) shutdown();
58 int PlayerVideo::init()
60 if (initted) return 0;
62 video = Video::getInstance();
64 if (!demuxer.init(this))
66 logger->log("Player", Log::ERR, "Demuxer failed to init");
71 vfeed.init(video->getFD());
72 afeed.init(audio->getFD());
83 int PlayerVideo::shutdown()
85 if (!initted) return 0;
88 logger->log("Player", Log::DEBUG, "Player shutdown...");
104 logger->log("Player", Log::DEBUG, "Player shutdown done");
109 void PlayerVideo::resyncAudio()
113 if (!initted) return;
116 usleep(500000); // SYNC
120 int PlayerVideo::play()
122 if (!initted) return 0;
124 // If we are just paused, unpause!
131 // If we are fast forwarding, set to normal
138 // If we are fast backwarding, set to normal
141 toggleFastBackward();
145 // If we are already playing, bail // FIXME - resync?
148 logger->log("Player", Log::DEBUG, "DOING RESYNC");
179 // Standard play start
186 // ------------------------ This one works, but doesn't allow any pre-buffering.
196 usleep(500000); // SYNC
200 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
206 // struct timespec delay;
208 // delay.tv_nsec = 500000000;
209 // nanosleep(&delay, NULL);
219 usleep(500000); // SYNC
225 // ------------------------------------------------------------------------------------------------
231 void PlayerVideo::stop()
233 if (!initted) return;
234 if (!playing) return;
241 video->unFastForward();
242 audio->systemMuteOff();
243 feedMode = MODE_NORMAL;
249 logger->log("PlayerVideo", Log::DEBUG, "Temp 1");
251 logger->log("PlayerVideo", Log::DEBUG, "Temp 2");
253 logger->log("PlayerVideo", Log::DEBUG, "Temp 3");
255 logger->log("PlayerVideo", Log::DEBUG, "Temp 4");
257 logger->log("PlayerVideo", Log::DEBUG, "Temp 5");
259 logger->log("PlayerVideo", Log::DEBUG, "Temp 6");
261 logger->log("PlayerVideo", Log::DEBUG, "Temp 7");
263 logger->log("PlayerVideo", Log::DEBUG, "Temp 8");
265 logger->log("PlayerVideo", Log::DEBUG, "Temp 9");
267 logger->log("PlayerVideo", Log::DEBUG, "Temp 10");
272 void PlayerVideo::togglePause()
274 if (!initted) return;
275 if (!playing) return;
277 if (ffwd) toggleFastForward();
278 if (fbwd) toggleFastBackward();
294 void PlayerVideo::setPosition(ULLONG position)
296 feedPosition = position;
299 void PlayerVideo::setLength(ULLONG length)
301 lastRescan = time(NULL);
302 streamLength = length;
303 logger->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
306 void PlayerVideo::skipForward(int seconds)
308 // skip forward 1 minute
309 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
311 if (paused) togglePause();
313 ULLONG moveBy = seconds * 500000;
321 audio->doMuting(); // ???
323 feedPosition += moveBy;
325 // printf("Audio test %i\n", audio->test());
336 usleep(500000); // SYNC
343 void PlayerVideo::skipBackward(int seconds)
345 // skip forward 1 minute
346 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
348 if (paused) togglePause();
350 ULLONG moveBy = seconds * 500000;
359 audio->doMuting(); // ???
361 if (feedPosition > moveBy) feedPosition -= moveBy;
371 usleep(500000); // SYNC
378 void PlayerVideo::toggleFastForward()
380 if (!initted) return;
381 if (!playing) return;
383 if (paused) togglePause();
384 if (fbwd) toggleFastBackward();
389 // video->unFastForward();
410 audio->systemMuteOff();
413 usleep(500000); // SYNC
419 demuxer.flushAudio();
427 audio->systemMuteOff();
434 audio->systemMuteOn();
435 video->fastForward();
439 void PlayerVideo::toggleFastBackward()
441 if (!initted) return;
442 if (!playing) return;
444 if (paused) togglePause();
445 if (ffwd) toggleFastForward();
451 audio->systemMuteOff();
454 feedMode = MODE_NORMAL;
461 audio->systemMuteOn();
464 feedMode = MODE_BACKWARDS;
472 void PlayerVideo::jumpToPercent(int percent)
474 if (paused) togglePause();
475 if (ffwd) toggleFastForward();
486 feedPosition = streamLength * percent / 100;
496 usleep(500000); // SYNC
502 ULLONG PlayerVideo::getPositionTS()
504 long long currentTS = video->getCurrentTimestamp() - startTS;
505 if (currentTS < 0) currentTS += 7776000000ULL;
506 return (ULLONG)currentTS;
509 ULLONG PlayerVideo::getEndTS()
511 long long rendTS = endTS - startTS;
512 if (rendTS < 0) rendTS += 7776000000ULL;
513 return (ULLONG)rendTS;
516 void PlayerVideo::call(void* caller)
518 if (caller == &demuxer)
520 logger->log("Player", Log::DEBUG, "Callback from demuxer");
522 if (video->getTVsize() == Video::ASPECT4X3)
524 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
528 int dxCurrentAspect = demuxer.getAspectRatio();
529 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
531 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
532 video->setAspectRatio(Video::ASPECT4X3);
534 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
536 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
537 video->setAspectRatio(Video::ASPECT16X9);
541 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
547 threadSignalNoLock();
553 void PlayerVideo::threadMethod()
559 VDR* vdr = VDR::getInstance();
570 // If we havn't rescanned for a while..
571 if (isRecording && ((lastRescan + 60) < time(NULL)))
573 streamLength = vdr->rescanRecording();
574 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
575 lastRescan = time(NULL);
579 if (streamLength) // is playing a recording
581 if (feedPosition >= streamLength) break; // finished playback
585 if (startupBlockSize > streamLength)
586 askFor = streamLength; // is a very small recording!
588 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
592 if ((feedPosition + blockSize) > streamLength) // last block of recording
593 askFor = streamLength - feedPosition;
598 else // is playing live
601 askFor = startupBlockSize; // find audio streams sized block
603 askFor = blockSize; // normal
606 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
607 if (!threadBuffer) break;
611 int a_stream = demuxer.scan(threadBuffer, thisRead);
612 demuxer.setAudioStream(a_stream);
613 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
615 demuxer.findVideoPTS(threadBuffer, thisRead, &startTS);
616 if (isRecording) setEndTS();
621 if (feedMode == MODE_NORMAL)
623 feedPosition += thisRead;
625 else if (feedMode == MODE_BACKWARDS)
627 if (feedPosition >= blockSize)
629 feedPosition -= blockSize;
634 // got to the start of the recording.. revert to play mode? how?
635 feedPosition += thisRead;
641 while(writeLength < thisRead)
643 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
644 writeLength += thisWrite;
648 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
649 // demuxer is full and cant take anymore
651 threadWaitForSignal();
653 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
665 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
666 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
667 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
668 else m->message = Message::STREAM_END; // live
669 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
670 commandMessageQueue->postMessage(m);
671 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
674 void PlayerVideo::threadPostStopCleanup()
676 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
679 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");
685 void PlayerVideo::setEndTS()
687 Log::getInstance()->log("Player", Log::DEBUG, "Setting end TS");
690 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
691 if (!tempBuffer) return;
692 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
694 Log::getInstance()->log("Player", Log::DEBUG, "Set end TS");
698 void PlayerVideo::test1()
700 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 1");
703 void PlayerVideo::test2()
705 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 2");