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 ULLONG Player::getPositionTS() // used internall (skip fw/bw)
158 if (startup) return 0ULL;
159 long long currentTS = video->getCurrentTimestamp() - startTS;
160 if (currentTS < 0) currentTS += 8589934592ULL;
161 return (ULLONG)currentTS;
164 // ----------------------------------- Externally called events
169 bool doUnlock = false;
170 int result = playInt(&doUnlock);
171 if (doUnlock) unLock();
182 void Player::togglePause()
189 void Player::toggleFastForward()
192 toggleFastForwardInt();
196 void Player::toggleFastBackward()
199 toggleFastBackwardInt();
203 void Player::jumpToPercent(int percent)
206 jumpToPercentInt(percent);
207 // unLock(); - let thread unlock this
210 void Player::skipForward(int seconds)
213 skipForwardInt(seconds);
214 // unLock(); - let thread unlock this
217 void Player::skipBackward(int seconds)
220 skipBackwardInt(seconds);
221 // unLock(); - let thread unlock this
224 // ----------------------------------- Implementations called events
227 int Player::playInt(bool* doUnlock)
229 if (!initted) return 0;
231 // If we are just paused, unpause!
239 // If we are fast forwarding, set to normal
242 toggleFastForwardInt();
246 // If we are fast backwarding, set to normal
249 toggleFastBackwardInt();
253 // If we are already playing, ignore. no resyncing to do now!
260 // Standard play start
261 logger->log("Player", Log::DEBUG, "Standard play start");
268 if (!isRadio) demuxer->seek();
275 logger->log("Player", Log::DEBUG, "Immediate play");
283 else // do prebuffering
285 logger->log("Player", Log::DEBUG, "Prebuffering...");
293 void Player::stopInt()
295 if (!initted) return;
296 if (!playing) return;
303 video->unFastForward();
304 audio->systemMuteOff();
305 feedMode = MODE_NORMAL;
324 void Player::togglePauseInt()
326 if (!initted) return;
327 if (!playing) return;
329 if (ffwd) toggleFastForwardInt();
330 if (fbwd) toggleFastBackwardInt();
346 void Player::toggleFastForwardInt()
348 if (!initted) return;
349 if (!playing) return;
351 if (paused) togglePauseInt();
352 if (fbwd) toggleFastBackwardInt();
365 if (!isRadio) demuxer->seek();
375 audio->systemMuteOff();
385 audio->systemMuteOn();
386 video->fastForward();
390 void Player::toggleFastBackwardInt()
392 if (!initted) return;
393 if (!playing) return;
395 if (paused) togglePauseInt();
396 if (ffwd) toggleFastForwardInt();
402 audio->systemMuteOff();
405 feedMode = MODE_NORMAL;
414 audio->systemMuteOn();
417 feedMode = MODE_BACKWARDS;
421 if (!isRadio) demuxer->seek();
427 void Player::skipForwardInt(int seconds)
429 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
430 restartAt(getPositionTS() + (seconds * 90000));
433 void Player::skipBackwardInt(int seconds)
435 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
436 long long newTimeCode = getPositionTS() - (seconds * 90000);
437 if (newTimeCode < 0) newTimeCode = 0;
438 restartAt(newTimeCode);
441 void Player::jumpToPercentInt(int percent)
443 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
444 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
445 restartAt(newTimeCode);
449 // ----------------------------------- Internal functions
454 pthread_mutex_lock(&mutex);
455 logger->log("Player", Log::DEBUG, "LOCKED");
458 WaitForSingleObject(mutex, INFINITE );
462 void Player::unLock()
465 logger->log("Player", Log::DEBUG, "UNLOCKING");
466 pthread_mutex_unlock(&mutex);
472 void Player::restartAt(ULLONG timecode)
474 if (paused) togglePauseInt();
475 if (ffwd) toggleFastForwardInt();
477 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
478 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
479 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
480 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
489 if (!isRadio) demuxer->seek();
490 feedPosition = newPosition;
498 audio->systemMuteOff();
503 void Player::setStartTS(UINT dataInBuffer)
506 if (isRecording && feedPosition) // (feedPosition != 0)
508 // FIXME find out how much data need to get to find a TS
509 // Need to get the actual start of the recording
512 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
513 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
514 if (!tempBuffer) return;
515 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
520 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
527 void Player::setEndTS()
529 logger->log("Player", Log::DEBUG, "Setting end TS");
532 UCHAR* tempBuffer = VDR::getInstance()->getBlock((lengthBytes - 100000), 100000, &thisRead);
533 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
534 if (!tempBuffer) return;
535 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
537 #else //The replacement code relias completely on VDRs timecode and not the pts
538 endTS=video->frameNumberToTimecode(
539 VDR::getInstance()->frameNumberFromPosition((lengthBytes - 100000)));
541 logger->log("Player", Log::DEBUG, "Set end TS");
544 void Player::doConnectionLost()
546 Message* m = new Message();
547 m->message = Message::CONNECTION_LOST;
549 commandMessageQueue->postMessage(m);
552 // ----------------------------------- Callback
554 void Player::call(void* caller)
556 if (caller == demuxer)
558 logger->log("Player", Log::DEBUG, "Callback from demuxer");
560 if (video->getTVsize() == Video::ASPECT4X3)
562 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
566 int dxCurrentAspect = demuxer->getAspectRatio();
567 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
569 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
570 video->setAspectRatio(Video::ASPECT4X3);
572 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
574 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
575 video->setAspectRatio(Video::ASPECT16X9);
579 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
587 videoStartup = false;
595 threadSignalNoLock();
599 // ----------------------------------- Feed thread
601 void Player::threadMethod()
606 UINT preBufferTotal = 0;
608 ULLONG currentposition;
611 VDR* vdr = VDR::getInstance();
622 // If we havn't rescanned for a while..
623 if (isRecording && ((lastRescan + 60) < time(NULL)))
625 lengthBytes = vdr->rescanRecording(&lengthFrames);
626 if (!vdr->isConnected()) { doConnectionLost(); return; }
627 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
628 lastRescan = time(NULL);
632 if (lengthBytes) // is playing a recording
634 if (feedPosition >= lengthBytes) break; // finished playback
638 if (startupBlockSize > lengthBytes)
639 askFor = lengthBytes; // is a very small recording!
641 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
645 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
646 askFor = lengthBytes - feedPosition;
651 else // is playing live
654 askFor = startupBlockSize; // find audio streams sized block
656 askFor = blockSize; // normal
659 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
661 currentposition=feedPosition;
663 if (!vdr->isConnected())
669 if (!threadBuffer) break;
673 int a_stream = demuxer->scan(threadBuffer, thisRead);
674 demuxer->setAudioStream(a_stream);
675 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
677 setStartTS(thisRead);
679 if (isRecording) setEndTS();
686 preBufferTotal += thisRead;
687 if (preBufferTotal > 500000)
689 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
691 preBuffering = false;
700 // unLock(); // thread will be locked by play until here
701 // FIXME - see if this can segfault because it is starting threads out of the master mutex
705 if (feedMode == MODE_NORMAL)
707 feedPosition += thisRead;
709 else if (feedMode == MODE_BACKWARDS)
711 if (feedPosition >= blockSize)
713 feedPosition -= blockSize;
714 if (!isRadio) demuxer->seek();
718 // got to the start of the recording.. revert to play mode? how?
719 feedPosition += thisRead;
725 while(writeLength < thisRead)
728 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
730 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
731 currentposition+(ULLONG)writeLength);
733 writeLength += thisWrite;
734 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
738 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
739 // demuxer is full and can't take anymore
741 threadWaitForSignal();
743 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
755 logger->log("Player", Log::DEBUG, "Recording playback ends");
759 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
760 if (lengthBytes) m->message = Message::STOP_PLAYBACK; // recording
761 else m->message = Message::STREAM_END; // live
762 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
763 commandMessageQueue->postMessage(m);
764 logger->log("Player", Log::DEBUG, "Message posted...");
767 void Player::threadPostStopCleanup()
776 // ----------------------------------- Dev
781 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
783 // video->setAspectRatio(Video::ASPECT4X3);
788 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
789 video->setAspectRatio(Video::ASPECT16X9);