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 demuxer = new DemuxerVDR();
77 if (!demuxer) return 0;
79 if (!demuxer->init(this))
81 logger->log("Player", Log::ERR, "Demuxer failed to init");
86 vfeed.init(video->getFD());
87 afeed.init(audio->getFD());
101 int Player::shutdown()
103 if (!initted) return 0;
128 void Player::setPosition(ULLONG position)
130 feedPosition = position;
133 void Player::setLength(ULLONG length)
135 lastRescan = time(NULL);
136 streamLength = length;
137 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
140 ULLONG Player::getEndTS() // used internally (jump to percent)
142 long long rendTS = endTS - startTS;
143 if (rendTS < 0) rendTS += 8589934592ULL;
144 return (ULLONG)rendTS;
147 ULLONG Player::getPositionTS() // used internall (skip fw/bw)
149 if (startup) return 0ULL;
150 long long currentTS = video->getCurrentTimestamp() - startTS;
151 if (currentTS < 0) currentTS += 8589934592ULL;
152 return (ULLONG)currentTS;
155 // ----------------------------------- Externally called events
160 bool doUnlock = false;
161 int result = playInt(&doUnlock);
162 if (doUnlock) unLock();
173 void Player::togglePause()
180 void Player::toggleFastForward()
183 toggleFastForwardInt();
187 void Player::toggleFastBackward()
190 toggleFastBackwardInt();
194 void Player::jumpToPercent(int percent)
197 jumpToPercentInt(percent);
198 // unLock(); - let thread unlock this
201 void Player::skipForward(int seconds)
204 skipForwardInt(seconds);
205 // unLock(); - let thread unlock this
208 void Player::skipBackward(int seconds)
211 skipBackwardInt(seconds);
212 // unLock(); - let thread unlock this
215 // ----------------------------------- Implementations called events
218 int Player::playInt(bool* doUnlock)
220 if (!initted) return 0;
222 // If we are just paused, unpause!
230 // If we are fast forwarding, set to normal
233 toggleFastForwardInt();
237 // If we are fast backwarding, set to normal
240 toggleFastBackwardInt();
244 // If we are already playing, ignore. no resyncing to do now!
251 // Standard play start
252 logger->log("Player", Log::DEBUG, "Standard play start");
259 if (!isRadio) demuxer->seek();
266 logger->log("Player", Log::DEBUG, "Immediate play");
274 else // do prebuffering
276 logger->log("Player", Log::DEBUG, "Prebuffering...");
284 void Player::stopInt()
286 if (!initted) return;
287 if (!playing) return;
294 video->unFastForward();
295 audio->systemMuteOff();
296 feedMode = MODE_NORMAL;
315 void Player::togglePauseInt()
317 if (!initted) return;
318 if (!playing) return;
320 if (ffwd) toggleFastForwardInt();
321 if (fbwd) toggleFastBackwardInt();
337 void Player::toggleFastForwardInt()
339 if (!initted) return;
340 if (!playing) return;
342 if (paused) togglePauseInt();
343 if (fbwd) toggleFastBackwardInt();
356 if (!isRadio) demuxer->seek();
366 audio->systemMuteOff();
374 audio->systemMuteOn();
375 video->fastForward();
379 void Player::toggleFastBackwardInt()
381 if (!initted) return;
382 if (!playing) return;
384 if (paused) togglePauseInt();
385 if (ffwd) toggleFastForwardInt();
391 audio->systemMuteOff();
394 feedMode = MODE_NORMAL;
401 audio->systemMuteOn();
404 feedMode = MODE_BACKWARDS;
408 if (!isRadio) demuxer->seek();
414 void Player::skipForwardInt(int seconds)
416 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
417 restartAt(getPositionTS() + (seconds * 90000));
420 void Player::skipBackwardInt(int seconds)
422 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
423 long long newTimeCode = getPositionTS() - (seconds * 90000);
424 if (newTimeCode < 0) newTimeCode = 0;
425 restartAt(newTimeCode);
428 void Player::jumpToPercentInt(int percent)
430 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
431 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
432 restartAt(newTimeCode);
436 // ----------------------------------- Internal functions
441 pthread_mutex_lock(&mutex);
442 logger->log("Player", Log::DEBUG, "LOCKED");
449 void Player::unLock()
452 logger->log("Player", Log::DEBUG, "UNLOCKING");
453 pthread_mutex_unlock(&mutex);
459 void Player::restartAt(ULLONG timecode)
461 if (paused) togglePauseInt();
462 if (ffwd) toggleFastForwardInt();
464 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
465 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
466 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
467 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
476 if (!isRadio) demuxer->seek();
477 feedPosition = newPosition;
485 audio->systemMuteOff();
490 void Player::setStartTS(UINT dataInBuffer)
492 if (isRecording && feedPosition) // (feedPosition != 0)
494 // FIXME find out how much data need to get to find a TS
495 // Need to get the actual start of the recording
498 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
499 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
500 if (!tempBuffer) return;
501 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
506 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
510 void Player::setEndTS()
512 logger->log("Player", Log::DEBUG, "Setting end TS");
515 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
516 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
517 if (!tempBuffer) return;
518 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
520 logger->log("Player", Log::DEBUG, "Set end TS");
523 void Player::doConnectionLost()
525 Message* m = new Message();
526 m->message = Message::CONNECTION_LOST;
528 commandMessageQueue->postMessage(m);
531 // ----------------------------------- Callback
533 void Player::call(void* caller)
535 if (caller == demuxer)
537 logger->log("Player", Log::DEBUG, "Callback from demuxer");
539 if (video->getTVsize() == Video::ASPECT4X3)
541 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
545 int dxCurrentAspect = demuxer->getAspectRatio();
546 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
548 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
549 video->setAspectRatio(Video::ASPECT4X3);
551 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
553 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
554 video->setAspectRatio(Video::ASPECT16X9);
558 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
566 videoStartup = false;
574 threadSignalNoLock();
578 // ----------------------------------- Feed thread
580 void Player::threadMethod()
587 VDR* vdr = VDR::getInstance();
598 // If we havn't rescanned for a while..
599 if (isRecording && ((lastRescan + 60) < time(NULL)))
601 streamLength = vdr->rescanRecording();
602 if (!vdr->isConnected()) { doConnectionLost(); return; }
603 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
604 lastRescan = time(NULL);
608 if (streamLength) // is playing a recording
610 if (feedPosition >= streamLength) break; // finished playback
614 if (startupBlockSize > streamLength)
615 askFor = streamLength; // is a very small recording!
617 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
621 if ((feedPosition + blockSize) > streamLength) // last block of recording
622 askFor = streamLength - feedPosition;
627 else // is playing live
630 askFor = startupBlockSize; // find audio streams sized block
632 askFor = blockSize; // normal
635 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
636 if (!vdr->isConnected())
642 if (!threadBuffer) break;
646 int a_stream = demuxer->scan(threadBuffer, thisRead);
647 demuxer->setAudioStream(a_stream);
648 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
650 setStartTS(thisRead);
652 if (isRecording) setEndTS();
659 preBufferTotal += thisRead;
660 if (preBufferTotal > 500000)
662 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
664 preBuffering = false;
673 // unLock(); // thread will be locked by play until here
674 // FIXME - see if this can segfault because it is starting threads out of the master mutex
678 if (feedMode == MODE_NORMAL)
680 feedPosition += thisRead;
682 else if (feedMode == MODE_BACKWARDS)
684 if (feedPosition >= blockSize)
686 feedPosition -= blockSize;
687 if (!isRadio) demuxer->seek();
691 // got to the start of the recording.. revert to play mode? how?
692 feedPosition += thisRead;
698 while(writeLength < thisRead)
700 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
701 writeLength += thisWrite;
702 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
706 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
707 // demuxer is full and can't take anymore
709 threadWaitForSignal();
711 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
723 logger->log("Player", Log::DEBUG, "Recording playback ends");
727 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
728 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
729 else m->message = Message::STREAM_END; // live
730 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
731 commandMessageQueue->postMessage(m);
732 logger->log("Player", Log::DEBUG, "Message posted...");
735 void Player::threadPostStopCleanup()
744 // ----------------------------------- Dev
749 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
751 // video->setAspectRatio(Video::ASPECT4X3);
756 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
757 video->setAspectRatio(Video::ASPECT16X9);