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 // ----------------------------------- Called from outside, one offs or info funcs
25 Player::Player(MessageQueue* messageQueue, bool tIsRecording, bool tIsRadio)
26 : vfeed(this), afeed(this)
28 commandMessageQueue = messageQueue;
29 audio = Audio::getInstance();
30 video = Video::getInstance();
31 logger = Log::getInstance();
40 feedMode = MODE_NORMAL;
47 isRecording = tIsRecording;
55 startupBlockSize = 60000;
56 video->turnVideoOff();
61 startupBlockSize = 250000;
68 if (initted) shutdown();
73 if (initted) return 0;
75 pthread_mutex_init(&mutex, NULL);
77 mutex=CreateMutex(NULL,FALSE,NULL);
80 demuxer = new DemuxerVDR();
81 if (!demuxer) return 0;
83 if (!demuxer->init(this, audio, video))
85 logger->log("Player", Log::ERR, "Demuxer failed to init");
101 int Player::shutdown()
103 if (!initted) return 0;
131 void Player::setPosition(ULLONG position)
133 feedPosition = position;
136 void Player::setLengthBytes(ULLONG length)
138 lastRescan = time(NULL);
139 lengthBytes = length;
140 logger->log("Player", Log::DEBUG, "Player has received length bytes of %llu", lengthBytes);
143 void Player::setLengthFrames(ULONG length)
145 lengthFrames = length;
146 logger->log("Player", Log::DEBUG, "Player has received length frames of %lu", lengthFrames);
149 ULLONG Player::getEndTS() // used internally (jump to percent)
151 long long rendTS = endTS - startTS;
152 if (rendTS < 0) rendTS += 8589934592ULL;
153 return (ULLONG)rendTS;
156 hmsf Player::getEndHMSF()
158 return video->framesToHMSF(lengthFrames);
161 ULLONG Player::getPositionTS() // used internall (skip fw/bw)
163 if (startup) return 0ULL;
164 long long currentTS = video->getCurrentTimestamp() - startTS;
165 if (currentTS < 0) currentTS += 8589934592ULL;
166 return (ULLONG)currentTS;
169 // ----------------------------------- Externally called events
174 bool doUnlock = false;
175 int result = playInt(&doUnlock);
176 if (doUnlock) unLock();
187 void Player::togglePause()
194 void Player::toggleFastForward()
197 toggleFastForwardInt();
201 void Player::toggleFastBackward()
204 toggleFastBackwardInt();
208 void Player::jumpToPercent(int percent)
211 jumpToPercentInt(percent);
212 // unLock(); - let thread unlock this
215 void Player::skipForward(int seconds)
218 skipForwardInt(seconds);
219 // unLock(); - let thread unlock this
222 void Player::skipBackward(int seconds)
225 skipBackwardInt(seconds);
226 // unLock(); - let thread unlock this
229 // ----------------------------------- Implementations called events
232 int Player::playInt(bool* doUnlock)
234 if (!initted) return 0;
236 // If we are just paused, unpause!
244 // If we are fast forwarding, set to normal
247 toggleFastForwardInt();
251 // If we are fast backwarding, set to normal
254 toggleFastBackwardInt();
258 // If we are already playing, ignore. no resyncing to do now!
265 // Standard play start
266 logger->log("Player", Log::DEBUG, "Standard play start");
273 if (!isRadio) demuxer->seek();
280 logger->log("Player", Log::DEBUG, "Immediate play");
288 else // do prebuffering
290 logger->log("Player", Log::DEBUG, "Prebuffering...");
298 void Player::stopInt()
300 if (!initted) return;
301 if (!playing) return;
308 video->unFastForward();
309 audio->systemMuteOff();
310 feedMode = MODE_NORMAL;
329 void Player::togglePauseInt()
331 if (!initted) return;
332 if (!playing) return;
334 if (ffwd) toggleFastForwardInt();
335 if (fbwd) toggleFastBackwardInt();
351 void Player::toggleFastForwardInt()
353 if (!initted) return;
354 if (!playing) return;
356 if (paused) togglePauseInt();
357 if (fbwd) toggleFastBackwardInt();
370 if (!isRadio) demuxer->seek();
380 audio->systemMuteOff();
390 audio->systemMuteOn();
391 video->fastForward();
395 void Player::toggleFastBackwardInt()
397 if (!initted) return;
398 if (!playing) return;
400 if (paused) togglePauseInt();
401 if (ffwd) toggleFastForwardInt();
407 audio->systemMuteOff();
410 feedMode = MODE_NORMAL;
419 audio->systemMuteOn();
422 feedMode = MODE_BACKWARDS;
426 if (!isRadio) demuxer->seek();
432 void Player::skipForwardInt(int seconds)
434 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
435 restartAt(getPositionTS() + (seconds * 90000));
438 void Player::skipBackwardInt(int seconds)
440 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
441 long long newTimeCode = getPositionTS() - (seconds * 90000);
442 if (newTimeCode < 0) newTimeCode = 0;
443 restartAt(newTimeCode);
446 void Player::jumpToPercentInt(int percent)
448 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
449 ULONG newFrame = (ULONG)(((float)percent / 100) * lengthFrames);
450 restartAtFrame(newFrame);
454 // ----------------------------------- Internal functions
459 pthread_mutex_lock(&mutex);
460 logger->log("Player", Log::DEBUG, "LOCKED");
463 WaitForSingleObject(mutex, INFINITE );
467 void Player::unLock()
470 logger->log("Player", Log::DEBUG, "UNLOCKING");
471 pthread_mutex_unlock(&mutex);
477 void Player::restartAt(ULLONG timecode)
479 if (paused) togglePauseInt();
480 if (ffwd) toggleFastForwardInt();
482 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
483 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
484 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
485 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
494 if (!isRadio) demuxer->seek();
495 feedPosition = newPosition;
503 audio->systemMuteOff();
508 void Player::restartAtFrame(ULONG newFrame)
510 if (paused) togglePauseInt();
511 if (ffwd) toggleFastForwardInt();
513 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(newFrame);
514 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
515 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", newFrame, feedPosition, newPosition);
524 if (!isRadio) demuxer->seek();
525 feedPosition = newPosition;
533 audio->systemMuteOff();
538 void Player::setStartTS(UINT dataInBuffer)
541 if (isRecording && feedPosition) // (feedPosition != 0)
543 // FIXME find out how much data need to get to find a TS
544 // Need to get the actual start of the recording
547 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
548 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
549 if (!tempBuffer) return;
550 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
555 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
562 void Player::setEndTS()
564 logger->log("Player", Log::DEBUG, "Setting end TS");
567 UCHAR* tempBuffer = VDR::getInstance()->getBlock((lengthBytes - 100000), 100000, &thisRead);
568 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
569 if (!tempBuffer) return;
570 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
572 #else //The replacement code relias completely on VDRs timecode and not the pts
573 endTS=video->frameNumberToTimecode(
574 VDR::getInstance()->frameNumberFromPosition((lengthBytes - 100000)));
576 logger->log("Player", Log::DEBUG, "Set end TS");
579 void Player::doConnectionLost()
581 Message* m = new Message();
582 m->message = Message::CONNECTION_LOST;
584 commandMessageQueue->postMessage(m);
587 // ----------------------------------- Callback
589 void Player::call(void* caller)
591 if (caller == demuxer)
593 logger->log("Player", Log::DEBUG, "Callback from demuxer");
595 if (video->getTVsize() == Video::ASPECT4X3)
597 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
601 int dxCurrentAspect = demuxer->getAspectRatio();
602 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
604 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
605 video->setAspectRatio(Video::ASPECT4X3);
607 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
609 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
610 video->setAspectRatio(Video::ASPECT16X9);
614 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
622 videoStartup = false;
630 threadSignalNoLock();
634 // ----------------------------------- Feed thread
636 void Player::threadMethod()
641 UINT preBufferTotal = 0;
643 ULLONG currentposition;
646 VDR* vdr = VDR::getInstance();
657 // If we havn't rescanned for a while..
658 if (isRecording && ((lastRescan + 60) < time(NULL)))
660 lengthBytes = vdr->rescanRecording(&lengthFrames);
661 if (!vdr->isConnected()) { doConnectionLost(); return; }
662 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
663 lastRescan = time(NULL);
667 if (lengthBytes) // is playing a recording
669 if (feedPosition >= lengthBytes) break; // finished playback
673 if (startupBlockSize > lengthBytes)
674 askFor = lengthBytes; // is a very small recording!
676 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
680 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
681 askFor = lengthBytes - feedPosition;
686 else // is playing live
689 askFor = startupBlockSize; // find audio streams sized block
691 askFor = blockSize; // normal
694 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
696 currentposition=feedPosition;
698 if (!vdr->isConnected())
704 if (!threadBuffer) break;
708 int a_stream = demuxer->scan(threadBuffer, thisRead);
709 demuxer->setAudioStream(a_stream);
710 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
712 setStartTS(thisRead);
714 if (isRecording) setEndTS();
721 preBufferTotal += thisRead;
722 if (preBufferTotal > 500000)
724 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
726 preBuffering = false;
735 // unLock(); // thread will be locked by play until here
736 // FIXME - see if this can segfault because it is starting threads out of the master mutex
740 if (feedMode == MODE_NORMAL)
742 feedPosition += thisRead;
744 else if (feedMode == MODE_BACKWARDS)
746 if (feedPosition >= blockSize)
748 feedPosition -= blockSize;
749 if (!isRadio) demuxer->seek();
753 // got to the start of the recording.. revert to play mode? how?
754 feedPosition += thisRead;
760 while(writeLength < thisRead)
763 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
765 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
766 currentposition+(ULLONG)writeLength);
768 writeLength += thisWrite;
769 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
773 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
774 // demuxer is full and can't take anymore
776 threadWaitForSignal();
778 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
790 logger->log("Player", Log::DEBUG, "Recording playback ends");
794 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
795 if (lengthBytes) m->message = Message::STOP_PLAYBACK; // recording
796 else m->message = Message::STREAM_END; // live
797 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
798 commandMessageQueue->postMessage(m);
799 logger->log("Player", Log::DEBUG, "Message posted...");
802 void Player::threadPostStopCleanup()
811 // ----------------------------------- Dev
816 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
818 // video->setAspectRatio(Video::ASPECT4X3);
823 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
824 video->setAspectRatio(Video::ASPECT16X9);