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 Player::Player(MessageQueue* messageQueue, bool tIsRecording, bool tIsRadio)
24 : vfeed(this), afeed(this)
26 commandMessageQueue = messageQueue;
27 audio = Audio::getInstance();
28 video = Video::getInstance();
29 logger = Log::getInstance();
37 feedMode = MODE_NORMAL;
44 isRecording = tIsRecording;
52 startupBlockSize = 60000;
53 video->turnVideoOff();
58 startupBlockSize = 250000;
65 if (initted) shutdown();
70 if (initted) return 0;
72 if (!demuxer.init(this))
74 logger->log("Player", Log::ERR, "Demuxer failed to init");
79 vfeed.init(video->getFD());
80 afeed.init(audio->getFD());
94 int Player::shutdown()
96 if (!initted) return 0;
99 logger->log("Player", Log::DEBUG, "Player shutdown...");
115 logger->log("Player", Log::DEBUG, "Player shutdown done");
120 void Player::resyncAudio()
123 if (!initted) return;
129 void Player::resyncVideo()
132 if (!initted) return;
140 if (!initted) return 0;
142 // If we are just paused, unpause!
149 // If we are fast forwarding, set to normal
156 // If we are fast backwarding, set to normal
159 toggleFastBackward();
163 // If we are already playing, bail // FIXME - resync?
166 logger->log("Player", Log::DEBUG, "DOING RESYNC");
171 // Standard play start
172 logger->log("Player", Log::DEBUG, "Standard play start");
179 if (!isRadio) demuxer.seek();
181 // ------------------------ This one works, but doesn't allow any pre-buffering.
192 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
199 logger->log("Player", Log::DEBUG, "Immediate play");
206 else // do prebuffering
208 logger->log("Player", Log::DEBUG, "Prebuffering...");
212 // ------------------------------------------------------------------------------------------------
220 if (!initted) return;
221 if (!playing) return;
223 logger->log("Player", Log::DEBUG, "Stop called", streamLength);
230 video->unFastForward();
231 audio->systemMuteOff();
232 feedMode = MODE_NORMAL;
251 void Player::togglePause()
253 if (!initted) return;
254 if (!playing) return;
256 if (ffwd) toggleFastForward();
257 if (fbwd) toggleFastBackward();
273 void Player::setPosition(ULLONG position)
275 feedPosition = position;
278 void Player::setLength(ULLONG length)
280 lastRescan = time(NULL);
281 streamLength = length;
282 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
285 void Player::restartAt(ULLONG timecode)
287 if (paused) togglePause();
288 if (ffwd) toggleFastForward();
290 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
291 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
292 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
293 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
302 if (!isRadio) demuxer.seek();
303 feedPosition = newPosition;
310 audio->systemMuteOff();
315 void Player::skipForward(int seconds)
317 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
318 restartAt(getPositionTS() + (seconds * 90000));
321 void Player::skipBackward(int seconds)
323 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
324 long long newTimeCode = getPositionTS() - (seconds * 90000);
325 if (newTimeCode < 0) newTimeCode = 0;
326 restartAt(newTimeCode);
329 void Player::jumpToPercent(int percent)
331 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
332 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
333 restartAt(newTimeCode);
336 void Player::toggleFastForward()
338 if (!initted) return;
339 if (!playing) return;
341 if (paused) togglePause();
342 if (fbwd) toggleFastBackward();
355 if (!isRadio) demuxer.seek();
364 audio->systemMuteOff();
372 audio->systemMuteOn();
373 video->fastForward();
377 void Player::toggleFastBackward()
379 if (!initted) return;
380 if (!playing) return;
382 if (paused) togglePause();
383 if (ffwd) toggleFastForward();
389 audio->systemMuteOff();
392 feedMode = MODE_NORMAL;
399 audio->systemMuteOn();
402 feedMode = MODE_BACKWARDS;
406 if (!isRadio) demuxer.seek();
412 ULLONG Player::getPositionTS()
414 if (startup) return 0ULL;
415 long long currentTS = video->getCurrentTimestamp() - startTS;
416 if (currentTS < 0) currentTS += 8589934592ULL;
417 return (ULLONG)currentTS;
420 ULLONG Player::getEndTS()
422 long long rendTS = endTS - startTS;
423 if (rendTS < 0) rendTS += 8589934592ULL;
424 return (ULLONG)rendTS;
427 void Player::call(void* caller)
429 if (caller == &demuxer)
431 logger->log("Player", Log::DEBUG, "Callback from demuxer");
433 if (video->getTVsize() == Video::ASPECT4X3)
435 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
439 int dxCurrentAspect = demuxer.getAspectRatio();
440 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
442 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
443 video->setAspectRatio(Video::ASPECT4X3);
445 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
447 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
448 video->setAspectRatio(Video::ASPECT16X9);
452 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
460 videoStartup = false;
461 logger->log("Player", Log::DEBUG, "Starting VFeed");
468 threadSignalNoLock();
471 void Player::doConnectionLost()
473 Message* m = new Message();
474 m->message = Message::CONNECTION_LOST;
476 commandMessageQueue->postMessage(m);
481 void Player::threadMethod()
488 VDR* vdr = VDR::getInstance();
499 // If we havn't rescanned for a while..
500 if (isRecording && ((lastRescan + 60) < time(NULL)))
502 streamLength = vdr->rescanRecording();
503 if (!vdr->isConnected()) { doConnectionLost(); return; }
504 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
505 lastRescan = time(NULL);
509 if (streamLength) // is playing a recording
511 if (feedPosition >= streamLength) break; // finished playback
515 if (startupBlockSize > streamLength)
516 askFor = streamLength; // is a very small recording!
518 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
522 if ((feedPosition + blockSize) > streamLength) // last block of recording
523 askFor = streamLength - feedPosition;
528 else // is playing live
531 askFor = startupBlockSize; // find audio streams sized block
533 askFor = blockSize; // normal
536 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
537 if (!vdr->isConnected())
543 if (!threadBuffer) break;
547 int a_stream = demuxer.scan(threadBuffer, thisRead);
548 demuxer.setAudioStream(a_stream);
549 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
551 setStartTS(thisRead);
553 if (isRecording) setEndTS();
560 preBufferTotal += thisRead;
561 if (preBufferTotal > 500000)
563 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
565 preBuffering = false;
576 if (feedMode == MODE_NORMAL)
578 feedPosition += thisRead;
580 else if (feedMode == MODE_BACKWARDS)
582 if (feedPosition >= blockSize)
584 feedPosition -= blockSize;
585 if (!isRadio) demuxer.seek();
589 // got to the start of the recording.. revert to play mode? how?
590 feedPosition += thisRead;
596 while(writeLength < thisRead)
598 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
599 writeLength += thisWrite;
603 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
604 // demuxer is full and cant take anymore
606 threadWaitForSignal();
608 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
620 logger->log("Player", Log::DEBUG, "Recording playback ends");
624 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
625 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
626 else m->message = Message::STREAM_END; // live
627 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
628 commandMessageQueue->postMessage(m);
629 logger->log("Player", Log::DEBUG, "Message posted...");
632 void Player::threadPostStopCleanup()
641 void Player::setStartTS(UINT dataInBuffer)
643 if (isRecording && feedPosition) // (feedPosition != 0)
645 // FIXME find out how much data need to get to find a TS
646 // Need to get the actual start of the recording
649 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
650 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
651 if (!tempBuffer) return;
652 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
657 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
661 void Player::setEndTS()
663 logger->log("Player", Log::DEBUG, "Setting end TS");
666 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
667 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
668 if (!tempBuffer) return;
669 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
671 logger->log("Player", Log::DEBUG, "Set end TS");
677 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
678 video->setAspectRatio(Video::ASPECT4X3);
683 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
684 video->setAspectRatio(Video::ASPECT16X9);