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))
84 logger->log("Player", Log::ERR, "Demuxer failed to init");
89 vfeed.init(video->getFD());
90 afeed.init(audio->getFD());
104 int Player::shutdown()
106 if (!initted) return 0;
134 void Player::setPosition(ULLONG position)
136 feedPosition = position;
139 void Player::setLength(ULLONG length)
141 lastRescan = time(NULL);
142 streamLength = length;
143 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
146 ULLONG Player::getEndTS() // used internally (jump to percent)
148 long long rendTS = endTS - startTS;
149 if (rendTS < 0) rendTS += 8589934592ULL;
150 return (ULLONG)rendTS;
153 ULLONG Player::getPositionTS() // used internall (skip fw/bw)
155 if (startup) return 0ULL;
156 long long currentTS = video->getCurrentTimestamp() - startTS;
157 if (currentTS < 0) currentTS += 8589934592ULL;
158 return (ULLONG)currentTS;
161 // ----------------------------------- Externally called events
166 bool doUnlock = false;
167 int result = playInt(&doUnlock);
168 if (doUnlock) unLock();
179 void Player::togglePause()
186 void Player::toggleFastForward()
189 toggleFastForwardInt();
193 void Player::toggleFastBackward()
196 toggleFastBackwardInt();
200 void Player::jumpToPercent(int percent)
203 jumpToPercentInt(percent);
204 // unLock(); - let thread unlock this
207 void Player::skipForward(int seconds)
210 skipForwardInt(seconds);
211 // unLock(); - let thread unlock this
214 void Player::skipBackward(int seconds)
217 skipBackwardInt(seconds);
218 // unLock(); - let thread unlock this
221 // ----------------------------------- Implementations called events
224 int Player::playInt(bool* doUnlock)
226 if (!initted) return 0;
228 // If we are just paused, unpause!
236 // If we are fast forwarding, set to normal
239 toggleFastForwardInt();
243 // If we are fast backwarding, set to normal
246 toggleFastBackwardInt();
250 // If we are already playing, ignore. no resyncing to do now!
257 // Standard play start
258 logger->log("Player", Log::DEBUG, "Standard play start");
265 if (!isRadio) demuxer->seek();
272 logger->log("Player", Log::DEBUG, "Immediate play");
280 else // do prebuffering
282 logger->log("Player", Log::DEBUG, "Prebuffering...");
290 void Player::stopInt()
292 if (!initted) return;
293 if (!playing) return;
300 video->unFastForward();
301 audio->systemMuteOff();
302 feedMode = MODE_NORMAL;
321 void Player::togglePauseInt()
323 if (!initted) return;
324 if (!playing) return;
326 if (ffwd) toggleFastForwardInt();
327 if (fbwd) toggleFastBackwardInt();
343 void Player::toggleFastForwardInt()
345 if (!initted) return;
346 if (!playing) return;
348 if (paused) togglePauseInt();
349 if (fbwd) toggleFastBackwardInt();
362 if (!isRadio) demuxer->seek();
372 audio->systemMuteOff();
380 audio->systemMuteOn();
381 video->fastForward();
385 void Player::toggleFastBackwardInt()
387 if (!initted) return;
388 if (!playing) return;
390 if (paused) togglePauseInt();
391 if (ffwd) toggleFastForwardInt();
397 audio->systemMuteOff();
400 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);