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();
39 feedMode = MODE_NORMAL;
46 isRecording = tIsRecording;
54 startupBlockSize = 60000;
55 video->turnVideoOff();
60 startupBlockSize = 250000;
67 if (initted) shutdown();
72 if (initted) return 0;
74 pthread_mutex_init(&mutex, NULL);
76 mutex=CreateMutex(NULL,FALSE,NULL);
79 demuxer = new DemuxerVDR();
80 if (!demuxer) return 0;
82 if (!demuxer->init(this, audio, video))
84 logger->log("Player", Log::ERR, "Demuxer failed to init");
100 int Player::shutdown()
102 if (!initted) return 0;
130 void Player::setPosition(ULLONG position)
132 feedPosition = position;
135 void Player::setLength(ULLONG length)
137 lastRescan = time(NULL);
138 streamLength = length;
139 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
142 ULLONG Player::getEndTS() // used internally (jump to percent)
144 long long rendTS = endTS - startTS;
145 if (rendTS < 0) rendTS += 8589934592ULL;
146 return (ULLONG)rendTS;
149 ULLONG Player::getPositionTS() // used internall (skip fw/bw)
151 if (startup) return 0ULL;
152 long long currentTS = video->getCurrentTimestamp() - startTS;
153 if (currentTS < 0) currentTS += 8589934592ULL;
154 return (ULLONG)currentTS;
157 // ----------------------------------- Externally called events
162 bool doUnlock = false;
163 int result = playInt(&doUnlock);
164 if (doUnlock) unLock();
175 void Player::togglePause()
182 void Player::toggleFastForward()
185 toggleFastForwardInt();
189 void Player::toggleFastBackward()
192 toggleFastBackwardInt();
196 void Player::jumpToPercent(int percent)
199 jumpToPercentInt(percent);
200 // unLock(); - let thread unlock this
203 void Player::skipForward(int seconds)
206 skipForwardInt(seconds);
207 // unLock(); - let thread unlock this
210 void Player::skipBackward(int seconds)
213 skipBackwardInt(seconds);
214 // unLock(); - let thread unlock this
217 // ----------------------------------- Implementations called events
220 int Player::playInt(bool* doUnlock)
222 if (!initted) return 0;
224 // If we are just paused, unpause!
232 // If we are fast forwarding, set to normal
235 toggleFastForwardInt();
239 // If we are fast backwarding, set to normal
242 toggleFastBackwardInt();
246 // If we are already playing, ignore. no resyncing to do now!
253 // Standard play start
254 logger->log("Player", Log::DEBUG, "Standard play start");
261 if (!isRadio) demuxer->seek();
268 logger->log("Player", Log::DEBUG, "Immediate play");
276 else // do prebuffering
278 logger->log("Player", Log::DEBUG, "Prebuffering...");
286 void Player::stopInt()
288 if (!initted) return;
289 if (!playing) return;
296 video->unFastForward();
297 audio->systemMuteOff();
298 feedMode = MODE_NORMAL;
317 void Player::togglePauseInt()
319 if (!initted) return;
320 if (!playing) return;
322 if (ffwd) toggleFastForwardInt();
323 if (fbwd) toggleFastBackwardInt();
339 void Player::toggleFastForwardInt()
341 if (!initted) return;
342 if (!playing) return;
344 if (paused) togglePauseInt();
345 if (fbwd) toggleFastBackwardInt();
358 if (!isRadio) demuxer->seek();
368 audio->systemMuteOff();
378 audio->systemMuteOn();
379 video->fastForward();
383 void Player::toggleFastBackwardInt()
385 if (!initted) return;
386 if (!playing) return;
388 if (paused) togglePauseInt();
389 if (ffwd) toggleFastForwardInt();
395 audio->systemMuteOff();
398 feedMode = MODE_NORMAL;
407 audio->systemMuteOn();
410 feedMode = MODE_BACKWARDS;
414 if (!isRadio) demuxer->seek();
420 void Player::skipForwardInt(int seconds)
422 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
423 restartAt(getPositionTS() + (seconds * 90000));
426 void Player::skipBackwardInt(int seconds)
428 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
429 long long newTimeCode = getPositionTS() - (seconds * 90000);
430 if (newTimeCode < 0) newTimeCode = 0;
431 restartAt(newTimeCode);
434 void Player::jumpToPercentInt(int percent)
436 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
437 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
438 restartAt(newTimeCode);
442 // ----------------------------------- Internal functions
447 pthread_mutex_lock(&mutex);
448 logger->log("Player", Log::DEBUG, "LOCKED");
451 WaitForSingleObject(mutex, INFINITE );
455 void Player::unLock()
458 logger->log("Player", Log::DEBUG, "UNLOCKING");
459 pthread_mutex_unlock(&mutex);
465 void Player::restartAt(ULLONG timecode)
467 if (paused) togglePauseInt();
468 if (ffwd) toggleFastForwardInt();
470 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
471 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
472 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
473 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
482 if (!isRadio) demuxer->seek();
483 feedPosition = newPosition;
491 audio->systemMuteOff();
496 void Player::setStartTS(UINT dataInBuffer)
499 if (isRecording && feedPosition) // (feedPosition != 0)
501 // FIXME find out how much data need to get to find a TS
502 // Need to get the actual start of the recording
505 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
506 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
507 if (!tempBuffer) return;
508 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
513 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
520 void Player::setEndTS()
522 logger->log("Player", Log::DEBUG, "Setting end TS");
525 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
526 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
527 if (!tempBuffer) return;
528 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
530 #else //The replacement code relias completely on VDRs timecode and not the pts
531 endTS=video->frameNumberToTimecode(
532 VDR::getInstance()->frameNumberFromPosition((streamLength - 100000)));
534 logger->log("Player", Log::DEBUG, "Set end TS");
537 void Player::doConnectionLost()
539 Message* m = new Message();
540 m->message = Message::CONNECTION_LOST;
542 commandMessageQueue->postMessage(m);
545 // ----------------------------------- Callback
547 void Player::call(void* caller)
549 if (caller == demuxer)
551 logger->log("Player", Log::DEBUG, "Callback from demuxer");
553 if (video->getTVsize() == Video::ASPECT4X3)
555 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
559 int dxCurrentAspect = demuxer->getAspectRatio();
560 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
562 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
563 video->setAspectRatio(Video::ASPECT4X3);
565 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
567 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
568 video->setAspectRatio(Video::ASPECT16X9);
572 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
580 videoStartup = false;
588 threadSignalNoLock();
592 // ----------------------------------- Feed thread
594 void Player::threadMethod()
599 UINT preBufferTotal = 0;
601 ULLONG currentposition;
604 VDR* vdr = VDR::getInstance();
615 // If we havn't rescanned for a while..
616 if (isRecording && ((lastRescan + 60) < time(NULL)))
618 streamLength = vdr->rescanRecording();
619 if (!vdr->isConnected()) { doConnectionLost(); return; }
620 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
621 lastRescan = time(NULL);
625 if (streamLength) // is playing a recording
627 if (feedPosition >= streamLength) break; // finished playback
631 if (startupBlockSize > streamLength)
632 askFor = streamLength; // is a very small recording!
634 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
638 if ((feedPosition + blockSize) > streamLength) // last block of recording
639 askFor = streamLength - feedPosition;
644 else // is playing live
647 askFor = startupBlockSize; // find audio streams sized block
649 askFor = blockSize; // normal
652 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
654 currentposition=feedPosition;
656 if (!vdr->isConnected())
662 if (!threadBuffer) break;
666 int a_stream = demuxer->scan(threadBuffer, thisRead);
667 demuxer->setAudioStream(a_stream);
668 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
670 setStartTS(thisRead);
672 if (isRecording) setEndTS();
679 preBufferTotal += thisRead;
680 if (preBufferTotal > 500000)
682 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
684 preBuffering = false;
693 // unLock(); // thread will be locked by play until here
694 // FIXME - see if this can segfault because it is starting threads out of the master mutex
698 if (feedMode == MODE_NORMAL)
700 feedPosition += thisRead;
702 else if (feedMode == MODE_BACKWARDS)
704 if (feedPosition >= blockSize)
706 feedPosition -= blockSize;
707 if (!isRadio) demuxer->seek();
711 // got to the start of the recording.. revert to play mode? how?
712 feedPosition += thisRead;
718 while(writeLength < thisRead)
721 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
723 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
724 currentposition+(ULLONG)writeLength);
726 writeLength += thisWrite;
727 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
731 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
732 // demuxer is full and can't take anymore
734 threadWaitForSignal();
736 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
748 logger->log("Player", Log::DEBUG, "Recording playback ends");
752 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
753 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
754 else m->message = Message::STREAM_END; // live
755 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
756 commandMessageQueue->postMessage(m);
757 logger->log("Player", Log::DEBUG, "Message posted...");
760 void Player::threadPostStopCleanup()
769 // ----------------------------------- Dev
774 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
776 // video->setAspectRatio(Video::ASPECT4X3);
781 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
782 video->setAspectRatio(Video::ASPECT16X9);