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() // FIXME delme - used only by bar clocks until current frame code is done
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 demuxer->setFrameNum(0);
274 if (!isRadio) demuxer->seek();
281 logger->log("Player", Log::DEBUG, "Immediate play");
289 else // do prebuffering
291 logger->log("Player", Log::DEBUG, "Prebuffering...");
299 void Player::stopInt()
301 if (!initted) return;
302 if (!playing) return;
309 video->unFastForward();
310 audio->systemMuteOff();
311 feedMode = MODE_NORMAL;
330 void Player::togglePauseInt()
332 if (!initted) return;
333 if (!playing) return;
335 if (ffwd) toggleFastForwardInt();
336 if (fbwd) toggleFastBackwardInt();
352 void Player::toggleFastForwardInt()
354 if (!initted) return;
355 if (!playing) return;
357 if (paused) togglePauseInt();
358 if (fbwd) toggleFastBackwardInt();
371 if (!isRadio) demuxer->seek();
381 audio->systemMuteOff();
391 audio->systemMuteOn();
392 video->fastForward();
396 void Player::toggleFastBackwardInt()
398 if (!initted) return;
399 if (!playing) return;
401 if (paused) togglePauseInt();
402 if (ffwd) toggleFastForwardInt();
408 audio->systemMuteOff();
411 feedMode = MODE_NORMAL;
420 audio->systemMuteOn();
423 feedMode = MODE_BACKWARDS;
427 if (!isRadio) demuxer->seek();
433 void Player::skipForwardInt(int seconds)
435 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
436 restartAt(getPositionTS() + (seconds * 90000));
439 void Player::skipBackwardInt(int seconds)
441 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
442 long long newTimeCode = getPositionTS() - (seconds * 90000);
443 if (newTimeCode < 0) newTimeCode = 0;
444 restartAt(newTimeCode);
447 void Player::jumpToPercentInt(int percent)
449 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
450 ULONG newFrame = (ULONG)(((float)percent / 100) * lengthFrames);
451 restartAtFrame(newFrame);
455 // ----------------------------------- Internal functions
460 pthread_mutex_lock(&mutex);
461 logger->log("Player", Log::DEBUG, "LOCKED");
464 WaitForSingleObject(mutex, INFINITE );
468 void Player::unLock()
471 logger->log("Player", Log::DEBUG, "UNLOCKING");
472 pthread_mutex_unlock(&mutex);
478 void Player::restartAt(ULLONG timecode)
480 if (paused) togglePauseInt();
481 if (ffwd) toggleFastForwardInt();
483 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
484 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
485 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
486 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
495 if (!isRadio) demuxer->seek();
496 feedPosition = newPosition;
497 demuxer->setFrameNum(wantedFrameNumber);
505 audio->systemMuteOff();
510 void Player::restartAtFrame(ULONG newFrame)
512 if (paused) togglePauseInt();
513 if (ffwd) toggleFastForwardInt();
515 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(newFrame);
516 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
517 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", newFrame, feedPosition, newPosition);
526 if (!isRadio) demuxer->seek();
527 feedPosition = newPosition;
528 demuxer->setFrameNum(newFrame);
536 audio->systemMuteOff();
541 void Player::setStartTS(UINT dataInBuffer)
544 if (isRecording && feedPosition) // (feedPosition != 0)
546 // FIXME find out how much data need to get to find a TS
547 // Need to get the actual start of the recording
550 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
551 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
552 if (!tempBuffer) return;
553 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
558 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
565 void Player::setEndTS()
567 logger->log("Player", Log::DEBUG, "Setting end TS");
570 UCHAR* tempBuffer = VDR::getInstance()->getBlock((lengthBytes - 100000), 100000, &thisRead);
571 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
572 if (!tempBuffer) return;
573 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
575 #else //The replacement code relias completely on VDRs timecode and not the pts
576 endTS=video->frameNumberToTimecode(
577 VDR::getInstance()->frameNumberFromPosition((lengthBytes - 100000)));
579 logger->log("Player", Log::DEBUG, "Set end TS");
582 void Player::doConnectionLost()
584 Message* m = new Message();
585 m->message = Message::CONNECTION_LOST;
587 commandMessageQueue->postMessage(m);
590 // ----------------------------------- Callback
592 void Player::call(void* caller)
594 if (caller == demuxer)
596 logger->log("Player", Log::DEBUG, "Callback from demuxer");
598 if (video->getTVsize() == Video::ASPECT4X3)
600 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
604 int dxCurrentAspect = demuxer->getAspectRatio();
605 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
607 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
608 video->setAspectRatio(Video::ASPECT4X3);
610 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
612 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
613 video->setAspectRatio(Video::ASPECT16X9);
617 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
625 videoStartup = false;
633 threadSignalNoLock();
637 // ----------------------------------- Feed thread
639 void Player::threadMethod()
644 UINT preBufferTotal = 0;
646 ULLONG currentposition;
649 VDR* vdr = VDR::getInstance();
660 // If we havn't rescanned for a while..
661 if (isRecording && ((lastRescan + 60) < time(NULL)))
663 lengthBytes = vdr->rescanRecording(&lengthFrames);
664 if (!vdr->isConnected()) { doConnectionLost(); return; }
665 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
666 lastRescan = time(NULL);
670 if (lengthBytes) // is playing a recording
672 if (feedPosition >= lengthBytes) break; // finished playback
676 if (startupBlockSize > lengthBytes)
677 askFor = lengthBytes; // is a very small recording!
679 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
683 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
684 askFor = lengthBytes - feedPosition;
689 else // is playing live
692 askFor = startupBlockSize; // find audio streams sized block
694 askFor = blockSize; // normal
697 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
699 currentposition=feedPosition;
701 if (!vdr->isConnected())
707 if (!threadBuffer) break;
711 int a_stream = demuxer->scan(threadBuffer, thisRead);
712 demuxer->setAudioStream(a_stream);
713 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
715 setStartTS(thisRead);
717 if (isRecording) setEndTS();
724 preBufferTotal += thisRead;
725 if (preBufferTotal > 500000)
727 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
729 preBuffering = false;
738 // unLock(); // thread will be locked by play until here
739 // FIXME - see if this can segfault because it is starting threads out of the master mutex
743 if (feedMode == MODE_NORMAL)
745 feedPosition += thisRead;
747 else if (feedMode == MODE_BACKWARDS)
749 if (feedPosition >= blockSize)
751 feedPosition -= blockSize;
752 if (!isRadio) demuxer->seek();
756 // got to the start of the recording.. revert to play mode? how?
757 feedPosition += thisRead;
763 while(writeLength < thisRead)
766 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
768 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
769 currentposition+(ULLONG)writeLength);
771 writeLength += thisWrite;
772 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
776 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
777 // demuxer is full and can't take anymore
779 threadWaitForSignal();
781 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
793 logger->log("Player", Log::DEBUG, "Recording playback ends");
797 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
798 if (lengthBytes) m->message = Message::STOP_PLAYBACK; // recording
799 else m->message = Message::STREAM_END; // live
800 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
801 commandMessageQueue->postMessage(m);
802 logger->log("Player", Log::DEBUG, "Message posted...");
805 void Player::threadPostStopCleanup()
814 // ----------------------------------- Dev
819 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
821 // video->setAspectRatio(Video::ASPECT4X3);
826 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
827 video->setAspectRatio(Video::ASPECT16X9);