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;
214 logger->log("Player", Log::DEBUG, "Stop called", streamLength);
221 video->unFastForward();
222 audio->systemMuteOff();
223 feedMode = MODE_NORMAL;
242 void Player::togglePause()
244 if (!initted) return;
245 if (!playing) return;
247 if (ffwd) toggleFastForward();
248 if (fbwd) toggleFastBackward();
264 void Player::setPosition(ULLONG position)
266 feedPosition = position;
269 void Player::setLength(ULLONG length)
271 lastRescan = time(NULL);
272 streamLength = length;
273 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
276 void Player::skipForward(int seconds)
278 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
280 if (paused) togglePause();
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);
293 audio->doMuting(); // ???
295 feedPosition = newPosition;
307 void Player::skipBackward(int seconds)
309 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
311 if (paused) togglePause();
313 ULLONG newPosition = 0;
315 long long newTimeCode = getPositionTS() - (seconds * 90000);
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);
331 audio->doMuting(); // ???
333 feedPosition = newPosition;
345 void Player::toggleFastForward()
347 if (!initted) return;
348 if (!playing) return;
350 if (paused) togglePause();
351 if (fbwd) toggleFastBackward();
356 // video->unFastForward();
377 audio->systemMuteOff();
381 demuxer.flushAudio();
389 audio->systemMuteOff();
396 audio->systemMuteOn();
397 video->fastForward();
401 void Player::toggleFastBackward()
403 if (!initted) return;
404 if (!playing) return;
406 if (paused) togglePause();
407 if (ffwd) toggleFastForward();
413 audio->systemMuteOff();
416 feedMode = MODE_NORMAL;
423 audio->systemMuteOn();
426 feedMode = MODE_BACKWARDS;
434 void Player::jumpToPercent(int percent)
436 if (paused) togglePause();
437 if (ffwd) toggleFastForward();
448 feedPosition = streamLength * percent / 100;
460 ULLONG Player::getPositionTS()
462 if (startup) return 0ULL;
463 long long currentTS = video->getCurrentTimestamp() - startTS;
464 if (currentTS < 0) currentTS += 8589934592ULL;
465 return (ULLONG)currentTS;
468 ULLONG Player::getEndTS()
470 long long rendTS = endTS - startTS;
471 if (rendTS < 0) rendTS += 8589934592ULL;
472 return (ULLONG)rendTS;
475 void Player::call(void* caller)
477 if (caller == &demuxer)
479 logger->log("Player", Log::DEBUG, "Callback from demuxer");
481 if (video->getTVsize() == Video::ASPECT4X3)
483 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
487 int dxCurrentAspect = demuxer.getAspectRatio();
488 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
490 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
491 video->setAspectRatio(Video::ASPECT4X3);
493 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
495 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
496 video->setAspectRatio(Video::ASPECT16X9);
500 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
506 threadSignalNoLock();
510 void Player::doConnectionLost()
512 Message* m = new Message();
513 m->message = Message::CONNECTION_LOST;
515 commandMessageQueue->postMessage(m);
520 void Player::threadMethod()
526 VDR* vdr = VDR::getInstance();
537 // If we havn't rescanned for a while..
538 if (isRecording && ((lastRescan + 60) < time(NULL)))
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);
547 if (streamLength) // is playing a recording
549 if (feedPosition >= streamLength) break; // finished playback
553 if (startupBlockSize > streamLength)
554 askFor = streamLength; // is a very small recording!
556 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
560 if ((feedPosition + blockSize) > streamLength) // last block of recording
561 askFor = streamLength - feedPosition;
566 else // is playing live
569 askFor = startupBlockSize; // find audio streams sized block
571 askFor = blockSize; // normal
574 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
575 if (!vdr->isConnected())
581 if (!threadBuffer) break;
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);
589 setStartTS(thisRead);
591 if (isRecording) setEndTS();
596 if (feedMode == MODE_NORMAL)
598 feedPosition += thisRead;
600 else if (feedMode == MODE_BACKWARDS)
602 if (feedPosition >= blockSize)
604 feedPosition -= blockSize;
609 // got to the start of the recording.. revert to play mode? how?
610 feedPosition += thisRead;
616 while(writeLength < thisRead)
618 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
619 writeLength += thisWrite;
623 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
624 // demuxer is full and cant take anymore
626 threadWaitForSignal();
628 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
640 logger->log("Player", Log::DEBUG, "Recording playback ends");
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...");
652 void Player::threadPostStopCleanup()
654 logger->log("Player", Log::DEBUG, "Post stop cleanup 1");
657 logger->log("Player", Log::DEBUG, "Post stop cleanup 2");
661 logger->log("Player", Log::DEBUG, "Post stop cleanup 3");
664 void Player::setStartTS(UINT dataInBuffer)
666 if (isRecording && feedPosition) // (feedPosition != 0)
668 // FIXME find out how much data need to get to find a TS
669 // Need to get the actual start of the recording
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);
680 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
684 void Player::setEndTS()
686 logger->log("Player", Log::DEBUG, "Setting end TS");
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);
694 logger->log("Player", Log::DEBUG, "Set end TS");
700 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
705 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");