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");
290 if (startFrameNum > lengthFrames) startFrameNum = 0;
291 demuxer->setFrameNum(startFrameNum);
293 if (!isRadio) demuxer->seek();
300 logger->log("Player", Log::DEBUG, "Immediate play");
308 else // do prebuffering
310 logger->log("Player", Log::DEBUG, "Prebuffering...");
318 void Player::stopInt()
320 if (!initted) return;
321 if (!playing) return;
328 video->unFastForward();
329 audio->systemMuteOff();
330 feedMode = MODE_NORMAL;
349 void Player::togglePauseInt()
351 if (!initted) return;
352 if (!playing) return;
354 if (ffwd) toggleFastForwardInt();
355 if (fbwd) toggleFastBackwardInt();
371 void Player::toggleFastForwardInt()
373 if (!initted) return;
374 if (!playing) return;
376 if (paused) togglePauseInt();
377 if (fbwd) toggleFastBackwardInt();
390 if (!isRadio) demuxer->seek();
400 audio->systemMuteOff();
410 audio->systemMuteOn();
411 video->fastForward();
415 void Player::toggleFastBackwardInt()
417 if (!initted) return;
418 if (!playing) return;
420 if (paused) togglePauseInt();
421 if (ffwd) toggleFastForwardInt();
427 audio->systemMuteOff();
430 feedMode = MODE_NORMAL;
439 audio->systemMuteOn();
442 feedMode = MODE_BACKWARDS;
446 if (!isRadio) demuxer->seek();
452 void Player::skipForwardInt(int seconds)
454 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
455 restartAtFrame(getCurrentFrameNum() + (seconds * video->getFPS()));
458 void Player::skipBackwardInt(int seconds)
460 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
461 long newFrameNum = getCurrentFrameNum() - (seconds * video->getFPS());
462 if (newFrameNum < 0) newFrameNum = 0;
463 restartAtFrame(newFrameNum);
466 void Player::jumpToPercentInt(int percent)
468 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
469 ULONG newFrame = percent * lengthFrames / 100;
470 restartAtFrame(newFrame);
474 // ----------------------------------- Internal functions
479 pthread_mutex_lock(&mutex);
480 logger->log("Player", Log::DEBUG, "LOCKED");
483 WaitForSingleObject(mutex, INFINITE );
487 void Player::unLock()
490 logger->log("Player", Log::DEBUG, "UNLOCKING");
491 pthread_mutex_unlock(&mutex);
498 void Player::restartAt(ULLONG timecode)
500 if (paused) togglePauseInt();
501 if (ffwd) toggleFastForwardInt();
503 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
504 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
505 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
506 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
515 if (!isRadio) demuxer->seek();
516 feedPosition = newPosition;
517 demuxer->setFrameNum(wantedFrameNumber);
525 audio->systemMuteOff();
531 void Player::restartAtFrame(ULONG newFrame)
533 if (paused) togglePauseInt();
534 if (ffwd) toggleFastForwardInt();
536 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(newFrame);
537 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
538 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", newFrame, feedPosition, newPosition);
547 if (!isRadio) demuxer->seek();
548 feedPosition = newPosition;
549 demuxer->setFrameNum(newFrame);
557 audio->systemMuteOff();
563 void Player::setStartTS(UINT dataInBuffer)
566 if (isRecording && feedPosition) // (feedPosition != 0)
568 // FIXME find out how much data need to get to find a TS
569 // Need to get the actual start of the recording
572 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
573 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
574 if (!tempBuffer) return;
575 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
580 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
589 void Player::setEndTS()
591 logger->log("Player", Log::DEBUG, "Setting end TS");
594 UCHAR* tempBuffer = VDR::getInstance()->getBlock((lengthBytes - 100000), 100000, &thisRead);
595 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
596 if (!tempBuffer) return;
597 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
599 #else //The replacement code relias completely on VDRs timecode and not the pts
600 endTS=video->frameNumberToTimecode(
601 VDR::getInstance()->frameNumberFromPosition((lengthBytes - 100000)));
603 logger->log("Player", Log::DEBUG, "Set end TS");
607 void Player::doConnectionLost()
609 Message* m = new Message();
610 m->message = Message::CONNECTION_LOST;
612 commandMessageQueue->postMessage(m);
615 // ----------------------------------- Callback
617 void Player::call(void* caller)
619 if (caller == demuxer)
621 logger->log("Player", Log::DEBUG, "Callback from demuxer");
623 if (video->getTVsize() == Video::ASPECT4X3)
625 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
629 int dxCurrentAspect = demuxer->getAspectRatio();
630 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
632 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
633 video->setAspectRatio(Video::ASPECT4X3);
635 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
637 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
638 video->setAspectRatio(Video::ASPECT16X9);
642 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
650 videoStartup = false;
658 threadSignalNoLock();
662 // ----------------------------------- Feed thread
664 void Player::threadMethod()
669 UINT preBufferTotal = 0;
671 ULLONG currentposition;
674 VDR* vdr = VDR::getInstance();
685 // If we havn't rescanned for a while..
686 if (isRecording && ((lastRescan + 60) < time(NULL)))
688 lengthBytes = vdr->rescanRecording(&lengthFrames);
689 if (!vdr->isConnected()) { doConnectionLost(); return; }
690 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
691 lastRescan = time(NULL);
695 if (lengthBytes) // is playing a recording
697 if (feedPosition >= lengthBytes) break; // finished playback
701 if (startupBlockSize > lengthBytes)
702 askFor = lengthBytes; // is a very small recording!
704 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
708 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
709 askFor = lengthBytes - feedPosition;
714 else // is playing live
717 askFor = startupBlockSize; // find audio streams sized block
719 askFor = blockSize; // normal
722 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
724 currentposition=feedPosition;
726 if (!vdr->isConnected())
732 if (!threadBuffer) break;
736 int a_stream = demuxer->scan(threadBuffer, thisRead);
737 demuxer->setAudioStream(a_stream);
738 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
740 // setStartTS(thisRead);
742 // if (isRecording) setEndTS();
749 preBufferTotal += thisRead;
750 if (preBufferTotal > 500000)
752 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
754 preBuffering = false;
763 // unLock(); // thread will be locked by play until here
764 // FIXME - see if this can segfault because it is starting threads out of the master mutex
768 if (feedMode == MODE_NORMAL)
770 feedPosition += thisRead;
772 else if (feedMode == MODE_BACKWARDS)
774 if (feedPosition >= blockSize)
776 feedPosition -= blockSize;
777 if (!isRadio) demuxer->seek();
781 // got to the start of the recording.. revert to play mode? how?
782 feedPosition += thisRead;
788 while(writeLength < thisRead)
791 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
793 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
794 currentposition+(ULLONG)writeLength);
796 writeLength += thisWrite;
797 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
801 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
802 // demuxer is full and can't take anymore
804 threadWaitForSignal();
806 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
818 logger->log("Player", Log::DEBUG, "Recording playback ends");
822 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
823 if (lengthBytes) m->message = Message::STOP_PLAYBACK; // recording
824 else m->message = Message::STREAM_END; // live
825 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
826 commandMessageQueue->postMessage(m);
827 logger->log("Player", Log::DEBUG, "Message posted...");
830 void Player::threadPostStopCleanup()
839 // ----------------------------------- Dev
844 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
846 // video->setAspectRatio(Video::ASPECT4X3);
851 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
852 video->setAspectRatio(Video::ASPECT16X9);