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::setStartFrame(ULONG startFrame)
133 startFrameNum = startFrame;
136 feedPosition = VDR::getInstance()->positionFromFrameNumber(startFrameNum);
144 void Player::setLengthBytes(ULLONG length)
146 lastRescan = time(NULL);
147 lengthBytes = length;
148 logger->log("Player", Log::DEBUG, "Player has received length bytes of %llu", lengthBytes);
151 void Player::setLengthFrames(ULONG length)
153 lengthFrames = length;
154 logger->log("Player", Log::DEBUG, "Player has received length frames of %lu", lengthFrames);
158 ULLONG Player::getEndTS() // FIXME delme - used only by bar clocks until current frame code is done
160 long long rendTS = endTS - startTS;
161 if (rendTS < 0) rendTS += 8589934592ULL;
162 return (ULLONG)rendTS;
167 hmsf Player::getEndHMSF()
169 return video->framesToHMSF(lengthFrames);
173 ULONG Player::getLengthFrames()
178 ULONG Player::getCurrentFrameNum()
180 if (startup) return 0;
181 return demuxer->getFrameNumFromPTS(video->getCurrentTimestamp());
184 // ----------------------------------- Externally called events
189 bool doUnlock = false;
190 int result = playInt(&doUnlock);
191 if (doUnlock) unLock();
202 void Player::togglePause()
209 void Player::toggleFastForward()
212 toggleFastForwardInt();
216 void Player::toggleFastBackward()
219 toggleFastBackwardInt();
223 void Player::jumpToPercent(int percent)
226 jumpToPercentInt(percent);
227 // unLock(); - let thread unlock this
230 void Player::skipForward(int seconds)
233 skipForwardInt(seconds);
234 // unLock(); - let thread unlock this
237 void Player::skipBackward(int seconds)
240 skipBackwardInt(seconds);
241 // unLock(); - let thread unlock this
244 // ----------------------------------- Implementations called events
247 int Player::playInt(bool* doUnlock)
249 if (!initted) return 0;
251 // If we are just paused, unpause!
259 // If we are fast forwarding, set to normal
262 toggleFastForwardInt();
266 // If we are fast backwarding, set to normal
269 toggleFastBackwardInt();
273 // If we are already playing, ignore. no resyncing to do now!
280 // Standard play start
281 logger->log("Player", Log::DEBUG, "Standard play start");
288 if (startFrameNum > lengthFrames) startFrameNum = 0;
289 demuxer->setFrameNum(startFrameNum);
290 if (!isRadio) demuxer->seek();
297 logger->log("Player", Log::DEBUG, "Immediate play");
305 else // do prebuffering
307 logger->log("Player", Log::DEBUG, "Prebuffering...");
315 void Player::stopInt()
317 if (!initted) return;
318 if (!playing) return;
325 video->unFastForward();
326 audio->systemMuteOff();
327 feedMode = MODE_NORMAL;
346 void Player::togglePauseInt()
348 if (!initted) return;
349 if (!playing) return;
351 if (ffwd) toggleFastForwardInt();
352 if (fbwd) toggleFastBackwardInt();
368 void Player::toggleFastForwardInt()
370 if (!initted) return;
371 if (!playing) return;
373 if (paused) togglePauseInt();
374 if (fbwd) toggleFastBackwardInt();
387 if (!isRadio) demuxer->seek();
397 audio->systemMuteOff();
407 audio->systemMuteOn();
408 video->fastForward();
412 void Player::toggleFastBackwardInt()
414 if (!initted) return;
415 if (!playing) return;
417 if (paused) togglePauseInt();
418 if (ffwd) toggleFastForwardInt();
424 audio->systemMuteOff();
427 feedMode = MODE_NORMAL;
436 audio->systemMuteOn();
439 feedMode = MODE_BACKWARDS;
443 if (!isRadio) demuxer->seek();
449 void Player::skipForwardInt(int seconds)
451 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
452 restartAtFrame(getCurrentFrameNum() + (seconds * video->getFPS()));
455 void Player::skipBackwardInt(int seconds)
457 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
458 long newFrameNum = getCurrentFrameNum() - (seconds * video->getFPS());
459 if (newFrameNum < 0) newFrameNum = 0;
460 restartAtFrame(newFrameNum);
463 void Player::jumpToPercentInt(int percent)
465 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
466 ULONG newFrame = percent * lengthFrames / 100;
467 restartAtFrame(newFrame);
471 // ----------------------------------- Internal functions
476 pthread_mutex_lock(&mutex);
477 logger->log("Player", Log::DEBUG, "LOCKED");
480 WaitForSingleObject(mutex, INFINITE );
484 void Player::unLock()
487 logger->log("Player", Log::DEBUG, "UNLOCKING");
488 pthread_mutex_unlock(&mutex);
494 void Player::restartAt(ULLONG timecode)
496 if (paused) togglePauseInt();
497 if (ffwd) toggleFastForwardInt();
499 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
500 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
501 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
502 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
511 if (!isRadio) demuxer->seek();
512 feedPosition = newPosition;
513 demuxer->setFrameNum(wantedFrameNumber);
521 audio->systemMuteOff();
526 void Player::restartAtFrame(ULONG newFrame)
528 if (paused) togglePauseInt();
529 if (ffwd) toggleFastForwardInt();
531 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(newFrame);
532 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
533 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", newFrame, feedPosition, newPosition);
542 if (!isRadio) demuxer->seek();
543 feedPosition = newPosition;
544 demuxer->setFrameNum(newFrame);
552 audio->systemMuteOff();
558 void Player::setStartTS(UINT dataInBuffer)
561 if (isRecording && feedPosition) // (feedPosition != 0)
563 // FIXME find out how much data need to get to find a TS
564 // Need to get the actual start of the recording
567 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
568 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
569 if (!tempBuffer) return;
570 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
575 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
584 void Player::setEndTS()
586 logger->log("Player", Log::DEBUG, "Setting end TS");
589 UCHAR* tempBuffer = VDR::getInstance()->getBlock((lengthBytes - 100000), 100000, &thisRead);
590 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
591 if (!tempBuffer) return;
592 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
594 #else //The replacement code relias completely on VDRs timecode and not the pts
595 endTS=video->frameNumberToTimecode(
596 VDR::getInstance()->frameNumberFromPosition((lengthBytes - 100000)));
598 logger->log("Player", Log::DEBUG, "Set end TS");
602 void Player::doConnectionLost()
604 Message* m = new Message();
605 m->message = Message::CONNECTION_LOST;
607 commandMessageQueue->postMessage(m);
610 // ----------------------------------- Callback
612 void Player::call(void* caller)
614 if (caller == demuxer)
616 logger->log("Player", Log::DEBUG, "Callback from demuxer");
618 if (video->getTVsize() == Video::ASPECT4X3)
620 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
624 int dxCurrentAspect = demuxer->getAspectRatio();
625 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
627 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
628 video->setAspectRatio(Video::ASPECT4X3);
630 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
632 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
633 video->setAspectRatio(Video::ASPECT16X9);
637 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
645 videoStartup = false;
653 threadSignalNoLock();
657 // ----------------------------------- Feed thread
659 void Player::threadMethod()
664 UINT preBufferTotal = 0;
666 ULLONG currentposition;
669 VDR* vdr = VDR::getInstance();
680 // If we havn't rescanned for a while..
681 if (isRecording && ((lastRescan + 60) < time(NULL)))
683 lengthBytes = vdr->rescanRecording(&lengthFrames);
684 if (!vdr->isConnected()) { doConnectionLost(); return; }
685 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
686 lastRescan = time(NULL);
690 if (lengthBytes) // is playing a recording
692 if (feedPosition >= lengthBytes) break; // finished playback
696 if (startupBlockSize > lengthBytes)
697 askFor = lengthBytes; // is a very small recording!
699 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
703 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
704 askFor = lengthBytes - feedPosition;
709 else // is playing live
712 askFor = startupBlockSize; // find audio streams sized block
714 askFor = blockSize; // normal
717 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
719 currentposition=feedPosition;
721 if (!vdr->isConnected())
727 if (!threadBuffer) break;
731 int a_stream = demuxer->scan(threadBuffer, thisRead);
732 demuxer->setAudioStream(a_stream);
733 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
735 // setStartTS(thisRead);
737 // if (isRecording) setEndTS();
744 preBufferTotal += thisRead;
745 if (preBufferTotal > 500000)
747 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
749 preBuffering = false;
758 // unLock(); // thread will be locked by play until here
759 // FIXME - see if this can segfault because it is starting threads out of the master mutex
763 if (feedMode == MODE_NORMAL)
765 feedPosition += thisRead;
767 else if (feedMode == MODE_BACKWARDS)
769 if (feedPosition >= blockSize)
771 feedPosition -= blockSize;
772 if (!isRadio) demuxer->seek();
776 // got to the start of the recording.. revert to play mode? how?
777 feedPosition += thisRead;
783 while(writeLength < thisRead)
786 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
788 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
789 currentposition+(ULLONG)writeLength);
791 writeLength += thisWrite;
792 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
796 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
797 // demuxer is full and can't take anymore
799 threadWaitForSignal();
801 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
813 logger->log("Player", Log::DEBUG, "Recording playback ends");
817 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
818 if (lengthBytes) m->message = Message::STOP_PLAYBACK; // recording
819 else m->message = Message::STREAM_END; // live
820 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
821 commandMessageQueue->postMessage(m);
822 logger->log("Player", Log::DEBUG, "Message posted...");
825 void Player::threadPostStopCleanup()
834 // ----------------------------------- Dev
839 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
841 // video->setAspectRatio(Video::ASPECT4X3);
846 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
847 video->setAspectRatio(Video::ASPECT16X9);