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::setStartFrame(ULONG startFrame)
133 startFrameNum = startFrame;
136 feedPosition = VDR::getInstance()->positionFromFrameNumber(startFrameNum);
144 void Player::setLengthBytes(ULLONG length)
146 lastRescan = time(NULL);
147 lengthBytes = length;
148 logger->log("Player", Log::DEBUG, "Player has received length bytes of %llu", lengthBytes);
151 void Player::setLengthFrames(ULONG length)
153 lengthFrames = length;
154 logger->log("Player", Log::DEBUG, "Player has received length frames of %lu", lengthFrames);
157 ULONG Player::getLengthFrames()
162 ULONG Player::getCurrentFrameNum()
164 if (startup) return 0;
165 return demuxer->getFrameNumFromPTS(video->getCurrentTimestamp());
168 // ----------------------------------- Externally called events
173 bool doUnlock = false;
174 int result = playInt(&doUnlock);
175 if (doUnlock) unLock();
186 void Player::togglePause()
193 void Player::toggleFastForward()
196 toggleFastForwardInt();
200 void Player::toggleFastBackward()
203 toggleFastBackwardInt();
207 void Player::jumpToPercent(int percent)
210 jumpToPercentInt(percent);
211 // unLock(); - let thread unlock this
214 void Player::skipForward(int seconds)
217 skipForwardInt(seconds);
218 // unLock(); - let thread unlock this
221 void Player::skipBackward(int seconds)
224 skipBackwardInt(seconds);
225 // unLock(); - let thread unlock this
228 // ----------------------------------- Implementations called events
231 int Player::playInt(bool* doUnlock)
233 if (!initted) return 0;
235 // If we are just paused, unpause!
243 // If we are fast forwarding, set to normal
246 toggleFastForwardInt();
250 // If we are fast backwarding, set to normal
253 toggleFastBackwardInt();
257 // If we are already playing, ignore. no resyncing to do now!
264 // Standard play start
265 logger->log("Player", Log::DEBUG, "Standard play start");
274 if (startFrameNum > lengthFrames) startFrameNum = 0;
275 demuxer->setFrameNum(startFrameNum);
277 if (!isRadio) demuxer->seek();
284 logger->log("Player", Log::DEBUG, "Immediate play");
292 else // do prebuffering
294 logger->log("Player", Log::DEBUG, "Prebuffering...");
302 void Player::stopInt()
304 if (!initted) return;
305 if (!playing) return;
312 video->unFastForward();
313 audio->systemMuteOff();
314 feedMode = MODE_NORMAL;
333 void Player::togglePauseInt()
335 if (!initted) return;
336 if (!playing) return;
338 if (ffwd) toggleFastForwardInt();
339 if (fbwd) toggleFastBackwardInt();
355 void Player::toggleFastForwardInt()
357 if (!initted) return;
358 if (!playing) return;
360 if (paused) togglePauseInt();
361 if (fbwd) toggleFastBackwardInt();
374 if (!isRadio) demuxer->seek();
384 audio->systemMuteOff();
394 audio->systemMuteOn();
395 video->fastForward();
399 void Player::toggleFastBackwardInt()
401 if (!initted) return;
402 if (!playing) return;
404 if (paused) togglePauseInt();
405 if (ffwd) toggleFastForwardInt();
411 audio->systemMuteOff();
414 feedMode = MODE_NORMAL;
423 audio->systemMuteOn();
426 feedMode = MODE_BACKWARDS;
430 if (!isRadio) demuxer->seek();
436 void Player::skipForwardInt(int seconds)
438 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
439 restartAtFrame(getCurrentFrameNum() + (seconds * video->getFPS()));
442 void Player::skipBackwardInt(int seconds)
444 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
445 long newFrameNum = getCurrentFrameNum() - (seconds * video->getFPS());
446 if (newFrameNum < 0) newFrameNum = 0;
447 restartAtFrame(newFrameNum);
450 void Player::jumpToPercentInt(int percent)
452 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
453 ULONG newFrame = percent * lengthFrames / 100;
454 restartAtFrame(newFrame);
458 // ----------------------------------- Internal functions
463 pthread_mutex_lock(&mutex);
464 logger->log("Player", Log::DEBUG, "LOCKED");
467 WaitForSingleObject(mutex, INFINITE );
471 void Player::unLock()
474 logger->log("Player", Log::DEBUG, "UNLOCKING");
475 pthread_mutex_unlock(&mutex);
481 void Player::restartAtFrame(ULONG newFrame)
483 if (paused) togglePauseInt();
484 if (ffwd) toggleFastForwardInt();
486 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(newFrame);
487 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
488 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", newFrame, feedPosition, newPosition);
497 if (!isRadio) demuxer->seek();
498 feedPosition = newPosition;
499 demuxer->setFrameNum(newFrame);
507 audio->systemMuteOff();
512 void Player::doConnectionLost()
514 Message* m = new Message();
515 m->message = Message::CONNECTION_LOST;
517 commandMessageQueue->postMessage(m);
520 // ----------------------------------- Callback
522 void Player::call(void* caller)
524 if (caller == demuxer)
526 logger->log("Player", Log::DEBUG, "Callback from demuxer");
528 if (video->getTVsize() == Video::ASPECT4X3)
530 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
534 int dxCurrentAspect = demuxer->getAspectRatio();
535 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
537 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
538 video->setAspectRatio(Video::ASPECT4X3);
540 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
542 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
543 video->setAspectRatio(Video::ASPECT16X9);
547 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
555 videoStartup = false;
563 threadSignalNoLock();
567 // ----------------------------------- Feed thread
569 void Player::threadMethod()
574 UINT preBufferTotal = 0;
576 ULLONG currentposition;
579 VDR* vdr = VDR::getInstance();
590 // If we havn't rescanned for a while..
591 if (isRecording && ((lastRescan + 60) < time(NULL)))
593 lengthBytes = vdr->rescanRecording(&lengthFrames);
594 if (!vdr->isConnected()) { doConnectionLost(); return; }
595 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
596 lastRescan = time(NULL);
600 if (lengthBytes) // is playing a recording
602 if (feedPosition >= lengthBytes) break; // finished playback
606 if (startupBlockSize > lengthBytes)
607 askFor = lengthBytes; // is a very small recording!
609 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
613 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
614 askFor = lengthBytes - feedPosition;
619 else // is playing live
622 askFor = startupBlockSize; // find audio streams sized block
624 askFor = blockSize; // normal
627 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
629 currentposition=feedPosition;
631 if (!vdr->isConnected())
637 if (!threadBuffer) break;
641 int a_stream = demuxer->scan(threadBuffer, thisRead);
642 demuxer->setAudioStream(a_stream);
643 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
645 // setStartTS(thisRead);
647 // if (isRecording) setEndTS();
654 preBufferTotal += thisRead;
655 if (preBufferTotal > 500000)
657 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
659 preBuffering = false;
668 // unLock(); // thread will be locked by play until here
669 // FIXME - see if this can segfault because it is starting threads out of the master mutex
673 if (feedMode == MODE_NORMAL)
675 feedPosition += thisRead;
677 else if (feedMode == MODE_BACKWARDS)
679 if (feedPosition >= blockSize)
681 feedPosition -= blockSize;
682 if (!isRadio) demuxer->seek();
686 // got to the start of the recording.. revert to play mode? how?
687 feedPosition += thisRead;
693 while(writeLength < thisRead)
696 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
698 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength,
699 currentposition+(ULLONG)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 (lengthBytes) 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);