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();
376 audio->systemMuteOn();
377 video->fastForward();
381 void Player::toggleFastBackwardInt()
383 if (!initted) return;
384 if (!playing) return;
386 if (paused) togglePauseInt();
387 if (ffwd) toggleFastForwardInt();
393 audio->systemMuteOff();
396 feedMode = MODE_NORMAL;
403 audio->systemMuteOn();
406 feedMode = MODE_BACKWARDS;
410 if (!isRadio) demuxer->seek();
416 void Player::skipForwardInt(int seconds)
418 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
419 restartAt(getPositionTS() + (seconds * 90000));
422 void Player::skipBackwardInt(int seconds)
424 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
425 long long newTimeCode = getPositionTS() - (seconds * 90000);
426 if (newTimeCode < 0) newTimeCode = 0;
427 restartAt(newTimeCode);
430 void Player::jumpToPercentInt(int percent)
432 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
433 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
434 restartAt(newTimeCode);
438 // ----------------------------------- Internal functions
443 pthread_mutex_lock(&mutex);
444 logger->log("Player", Log::DEBUG, "LOCKED");
447 WaitForSingleObject(mutex, INFINITE );
451 void Player::unLock()
454 logger->log("Player", Log::DEBUG, "UNLOCKING");
455 pthread_mutex_unlock(&mutex);
461 void Player::restartAt(ULLONG timecode)
463 if (paused) togglePauseInt();
464 if (ffwd) toggleFastForwardInt();
466 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
467 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
468 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
469 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
478 if (!isRadio) demuxer->seek();
479 feedPosition = newPosition;
487 audio->systemMuteOff();
492 void Player::setStartTS(UINT dataInBuffer)
495 if (isRecording && feedPosition) // (feedPosition != 0)
497 // FIXME find out how much data need to get to find a TS
498 // Need to get the actual start of the recording
501 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
502 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
503 if (!tempBuffer) return;
504 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &startTS);
509 demuxer->findVideoPTS(threadBuffer, dataInBuffer, &startTS);
516 void Player::setEndTS()
518 logger->log("Player", Log::DEBUG, "Setting end TS");
521 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
522 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
523 if (!tempBuffer) return;
524 if (thisRead) demuxer->findVideoPTS(tempBuffer, thisRead, &endTS);
526 #else //The replacement code relias completely on VDRs timecode and not the pts
527 endTS=video->frameNumberToTimecode(
528 VDR::getInstance()->frameNumberFromPosition((streamLength - 100000)));
530 logger->log("Player", Log::DEBUG, "Set end TS");
533 void Player::doConnectionLost()
535 Message* m = new Message();
536 m->message = Message::CONNECTION_LOST;
538 commandMessageQueue->postMessage(m);
541 // ----------------------------------- Callback
543 void Player::call(void* caller)
545 if (caller == demuxer)
547 logger->log("Player", Log::DEBUG, "Callback from demuxer");
549 if (video->getTVsize() == Video::ASPECT4X3)
551 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
555 int dxCurrentAspect = demuxer->getAspectRatio();
556 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
558 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
559 video->setAspectRatio(Video::ASPECT4X3);
561 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
563 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
564 video->setAspectRatio(Video::ASPECT16X9);
568 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
576 videoStartup = false;
584 threadSignalNoLock();
588 // ----------------------------------- Feed thread
590 void Player::threadMethod()
595 UINT preBufferTotal = 0;
597 ULLONG currentposition;
600 VDR* vdr = VDR::getInstance();
611 // If we havn't rescanned for a while..
612 if (isRecording && ((lastRescan + 60) < time(NULL)))
614 streamLength = vdr->rescanRecording();
615 if (!vdr->isConnected()) { doConnectionLost(); return; }
616 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
617 lastRescan = time(NULL);
621 if (streamLength) // is playing a recording
623 if (feedPosition >= streamLength) break; // finished playback
627 if (startupBlockSize > streamLength)
628 askFor = streamLength; // is a very small recording!
630 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
634 if ((feedPosition + blockSize) > streamLength) // last block of recording
635 askFor = streamLength - feedPosition;
640 else // is playing live
643 askFor = startupBlockSize; // find audio streams sized block
645 askFor = blockSize; // normal
648 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
650 currentposition=feedPosition;
652 if (!vdr->isConnected())
658 if (!threadBuffer) break;
662 int a_stream = demuxer->scan(threadBuffer, thisRead);
663 demuxer->setAudioStream(a_stream);
664 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
666 setStartTS(thisRead);
668 if (isRecording) setEndTS();
675 preBufferTotal += thisRead;
676 if (preBufferTotal > 500000)
678 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
680 preBuffering = false;
689 // unLock(); // thread will be locked by play until here
690 // FIXME - see if this can segfault because it is starting threads out of the master mutex
694 if (feedMode == MODE_NORMAL)
696 feedPosition += thisRead;
698 else if (feedMode == MODE_BACKWARDS)
700 if (feedPosition >= blockSize)
702 feedPosition -= blockSize;
703 if (!isRadio) demuxer->seek();
707 // got to the start of the recording.. revert to play mode? how?
708 feedPosition += thisRead;
714 while(writeLength < thisRead)
717 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
719 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
720 currentposition+(ULLONG)writeLength);
722 writeLength += thisWrite;
723 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
727 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
728 // demuxer is full and can't take anymore
730 threadWaitForSignal();
732 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
744 logger->log("Player", Log::DEBUG, "Recording playback ends");
748 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
749 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
750 else m->message = Message::STREAM_END; // live
751 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
752 commandMessageQueue->postMessage(m);
753 logger->log("Player", Log::DEBUG, "Message posted...");
756 void Player::threadPostStopCleanup()
765 // ----------------------------------- Dev
770 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
772 // video->setAspectRatio(Video::ASPECT4X3);
777 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
778 video->setAspectRatio(Video::ASPECT16X9);