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
23 Player::Player(MessageQueue* messageQueue, UCHAR tIsRecording, UCHAR isRadio)
24 : vfeed(this), afeed(this)
26 commandMessageQueue = messageQueue;
27 audio = Audio::getInstance();
28 video = Video::getInstance();
29 logger = Log::getInstance();
37 feedMode = MODE_NORMAL;
38 isRecording = tIsRecording;
48 startupBlockSize = 60000;
53 startupBlockSize = 250000;
59 if (initted) shutdown();
64 if (initted) return 0;
66 if (!demuxer.init(this))
68 logger->log("Player", Log::ERR, "Demuxer failed to init");
73 vfeed.init(video->getFD());
74 afeed.init(audio->getFD());
84 int Player::shutdown()
86 if (!initted) return 0;
89 logger->log("Player", Log::DEBUG, "Player shutdown...");
105 logger->log("Player", Log::DEBUG, "Player shutdown done");
110 void Player::resyncAudio()
113 if (!initted) return;
119 void Player::resyncVideo()
122 if (!initted) return;
130 if (!initted) return 0;
132 // If we are just paused, unpause!
139 // If we are fast forwarding, set to normal
146 // If we are fast backwarding, set to normal
149 toggleFastBackward();
153 // If we are already playing, bail // FIXME - resync?
156 logger->log("Player", Log::DEBUG, "DOING RESYNC");
161 // Standard play start
162 logger->log("Player", Log::DEBUG, "Standard play start");
168 // ------------------------ This one works, but doesn't allow any pre-buffering.
178 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
184 // struct timespec delay;
186 // delay.tv_nsec = 500000000;
187 // nanosleep(&delay, NULL);
197 usleep(500000); // SYNC
203 // ------------------------------------------------------------------------------------------------
211 if (!initted) return;
212 if (!playing) return;
219 video->unFastForward();
220 audio->systemMuteOff();
221 feedMode = MODE_NORMAL;
240 void Player::togglePause()
242 if (!initted) return;
243 if (!playing) return;
245 if (ffwd) toggleFastForward();
246 if (fbwd) toggleFastBackward();
262 void Player::setPosition(ULLONG position)
264 feedPosition = position;
267 void Player::setLength(ULLONG length)
269 lastRescan = time(NULL);
270 streamLength = length;
271 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
274 void Player::skipForward(int seconds)
276 // skip forward 1 minute
277 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
279 if (paused) togglePause();
281 ULLONG moveBy = seconds * 500000;
289 audio->doMuting(); // ???
291 feedPosition += moveBy;
303 void Player::skipBackward(int seconds)
305 // skip forward 1 minute
306 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
308 if (paused) togglePause();
310 ULLONG moveBy = seconds * 500000;
319 audio->doMuting(); // ???
321 if (feedPosition > moveBy) feedPosition -= moveBy;
333 void Player::toggleFastForward()
335 if (!initted) return;
336 if (!playing) return;
338 if (paused) togglePause();
339 if (fbwd) toggleFastBackward();
344 // video->unFastForward();
365 audio->systemMuteOff();
369 demuxer.flushAudio();
377 audio->systemMuteOff();
384 audio->systemMuteOn();
385 video->fastForward();
389 void Player::toggleFastBackward()
391 if (!initted) return;
392 if (!playing) return;
394 if (paused) togglePause();
395 if (ffwd) toggleFastForward();
401 audio->systemMuteOff();
404 feedMode = MODE_NORMAL;
411 audio->systemMuteOn();
414 feedMode = MODE_BACKWARDS;
422 void Player::jumpToPercent(int percent)
424 if (paused) togglePause();
425 if (ffwd) toggleFastForward();
436 feedPosition = streamLength * percent / 100;
448 ULLONG Player::getPositionTS()
450 if (startup) return 0ULL;
451 long long currentTS = video->getCurrentTimestamp() - startTS;
452 if (currentTS < 0) currentTS += 8589934592ULL;
453 return (ULLONG)currentTS;
456 ULLONG Player::getEndTS()
458 long long rendTS = endTS - startTS;
459 if (rendTS < 0) rendTS += 8589934592ULL;
460 return (ULLONG)rendTS;
463 void Player::call(void* caller)
465 if (caller == &demuxer)
467 logger->log("Player", Log::DEBUG, "Callback from demuxer");
469 if (video->getTVsize() == Video::ASPECT4X3)
471 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
475 int dxCurrentAspect = demuxer.getAspectRatio();
476 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
478 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
479 video->setAspectRatio(Video::ASPECT4X3);
481 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
483 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
484 video->setAspectRatio(Video::ASPECT16X9);
488 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
494 threadSignalNoLock();
500 void Player::threadMethod()
506 VDR* vdr = VDR::getInstance();
517 // If we havn't rescanned for a while..
518 if (isRecording && ((lastRescan + 60) < time(NULL)))
520 streamLength = vdr->rescanRecording();
521 Log::getInstance()->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
522 lastRescan = time(NULL);
526 if (streamLength) // is playing a recording
528 if (feedPosition >= streamLength) break; // finished playback
532 if (startupBlockSize > streamLength)
533 askFor = streamLength; // is a very small recording!
535 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
539 if ((feedPosition + blockSize) > streamLength) // last block of recording
540 askFor = streamLength - feedPosition;
545 else // is playing live
548 askFor = startupBlockSize; // find audio streams sized block
550 askFor = blockSize; // normal
553 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
554 if (!threadBuffer) break;
558 int a_stream = demuxer.scan(threadBuffer, thisRead);
559 demuxer.setAudioStream(a_stream);
560 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
562 setStartTS(thisRead);
564 if (isRecording) setEndTS();
569 if (feedMode == MODE_NORMAL)
571 feedPosition += thisRead;
573 else if (feedMode == MODE_BACKWARDS)
575 if (feedPosition >= blockSize)
577 feedPosition -= blockSize;
582 // got to the start of the recording.. revert to play mode? how?
583 feedPosition += thisRead;
589 while(writeLength < thisRead)
591 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
592 writeLength += thisWrite;
596 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
597 // demuxer is full and cant take anymore
599 threadWaitForSignal();
601 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
613 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
614 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
615 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
616 else m->message = Message::STREAM_END; // live
617 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
618 commandMessageQueue->postMessage(m);
619 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
622 void Player::threadPostStopCleanup()
624 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
627 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");
631 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 3");
634 void Player::setStartTS(UINT dataInBuffer)
636 if (isRecording && feedPosition) // (feedPosition != 0)
638 // FIXME find out how much data need to get to find a TS
639 // Need to get the actual start of the recording
642 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
643 if (!tempBuffer) return;
644 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
649 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
653 void Player::setEndTS()
655 Log::getInstance()->log("Player", Log::DEBUG, "Setting end TS");
658 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
659 if (!tempBuffer) return;
660 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
662 Log::getInstance()->log("Player", Log::DEBUG, "Set end TS");
668 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 1");
673 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 2");