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, UCHAR tIsRecording, UCHAR isRadio)
24 : vfeed(this), afeed(this)
26 commandMessageQueue = messageQueue;
27 audio = Audio::getInstance();
28 video = Video::getInstance();
29 logger = Log::getInstance();
37 feedMode = MODE_NORMAL;
38 isRecording = tIsRecording;
48 startupBlockSize = 60000;
53 startupBlockSize = 250000;
59 if (initted) shutdown();
64 if (initted) return 0;
66 if (!demuxer.init(this))
68 logger->log("Player", Log::ERR, "Demuxer failed to init");
73 vfeed.init(video->getFD());
74 afeed.init(audio->getFD());
84 int Player::shutdown()
86 if (!initted) return 0;
89 logger->log("Player", Log::DEBUG, "Player shutdown...");
105 logger->log("Player", Log::DEBUG, "Player shutdown done");
110 void Player::resyncAudio()
113 if (!initted) return;
119 void Player::resyncVideo()
122 if (!initted) return;
130 if (!initted) return 0;
132 // If we are just paused, unpause!
139 // If we are fast forwarding, set to normal
146 // If we are fast backwarding, set to normal
149 toggleFastBackward();
153 // If we are already playing, bail // FIXME - resync?
156 logger->log("Player", Log::DEBUG, "DOING RESYNC");
161 // Standard play start
162 logger->log("Player", Log::DEBUG, "Standard play start");
168 // ------------------------ This one works, but doesn't allow any pre-buffering.
178 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
193 // ------------------------------------------------------------------------------------------------
201 if (!initted) return;
202 if (!playing) return;
204 logger->log("Player", Log::DEBUG, "Stop called", streamLength);
211 video->unFastForward();
212 audio->systemMuteOff();
213 feedMode = MODE_NORMAL;
232 void Player::togglePause()
234 if (!initted) return;
235 if (!playing) return;
237 if (ffwd) toggleFastForward();
238 if (fbwd) toggleFastBackward();
254 void Player::setPosition(ULLONG position)
256 feedPosition = position;
259 void Player::setLength(ULLONG length)
261 lastRescan = time(NULL);
262 streamLength = length;
263 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
266 void Player::skipForward(int seconds)
268 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
270 if (paused) togglePause();
272 ULONG wantedFrameNumber = video->timecodeToFrameNumber(getPositionTS() + (seconds * 90000));
273 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
274 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
275 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
283 audio->doMuting(); // ???
285 feedPosition = newPosition;
297 void Player::skipBackward(int seconds)
299 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
301 if (paused) togglePause();
303 ULLONG newPosition = 0;
305 long long newTimeCode = getPositionTS() - (seconds * 90000);
308 ULONG wantedFrameNumber = video->timecodeToFrameNumber((ULLONG)newTimeCode);
309 newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
310 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
311 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
321 audio->doMuting(); // ???
323 feedPosition = newPosition;
335 void Player::toggleFastForward()
337 if (!initted) return;
338 if (!playing) return;
340 if (paused) togglePause();
341 if (fbwd) toggleFastBackward();
346 // video->unFastForward();
367 audio->systemMuteOff();
371 demuxer.flushAudio();
379 audio->systemMuteOff();
386 audio->systemMuteOn();
387 video->fastForward();
391 void Player::toggleFastBackward()
393 if (!initted) return;
394 if (!playing) return;
396 if (paused) togglePause();
397 if (ffwd) toggleFastForward();
403 audio->systemMuteOff();
406 feedMode = MODE_NORMAL;
413 audio->systemMuteOn();
416 feedMode = MODE_BACKWARDS;
424 void Player::jumpToPercent(int percent)
426 if (paused) togglePause();
427 if (ffwd) toggleFastForward();
438 feedPosition = streamLength * percent / 100;
450 ULLONG Player::getPositionTS()
452 if (startup) return 0ULL;
453 long long currentTS = video->getCurrentTimestamp() - startTS;
454 if (currentTS < 0) currentTS += 8589934592ULL;
455 return (ULLONG)currentTS;
458 ULLONG Player::getEndTS()
460 long long rendTS = endTS - startTS;
461 if (rendTS < 0) rendTS += 8589934592ULL;
462 return (ULLONG)rendTS;
465 void Player::call(void* caller)
467 if (caller == &demuxer)
469 logger->log("Player", Log::DEBUG, "Callback from demuxer");
471 if (video->getTVsize() == Video::ASPECT4X3)
473 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
477 int dxCurrentAspect = demuxer.getAspectRatio();
478 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
480 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
481 video->setAspectRatio(Video::ASPECT4X3);
483 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
485 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
486 video->setAspectRatio(Video::ASPECT16X9);
490 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
496 threadSignalNoLock();
500 void Player::doConnectionLost()
502 Message* m = new Message();
503 m->message = Message::CONNECTION_LOST;
505 commandMessageQueue->postMessage(m);
510 void Player::threadMethod()
516 VDR* vdr = VDR::getInstance();
527 // If we havn't rescanned for a while..
528 if (isRecording && ((lastRescan + 60) < time(NULL)))
530 streamLength = vdr->rescanRecording();
531 if (!vdr->isConnected()) { doConnectionLost(); return; }
532 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
533 lastRescan = time(NULL);
537 if (streamLength) // is playing a recording
539 if (feedPosition >= streamLength) break; // finished playback
543 if (startupBlockSize > streamLength)
544 askFor = streamLength; // is a very small recording!
546 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
550 if ((feedPosition + blockSize) > streamLength) // last block of recording
551 askFor = streamLength - feedPosition;
556 else // is playing live
559 askFor = startupBlockSize; // find audio streams sized block
561 askFor = blockSize; // normal
564 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
565 if (!vdr->isConnected())
571 if (!threadBuffer) break;
575 int a_stream = demuxer.scan(threadBuffer, thisRead);
576 demuxer.setAudioStream(a_stream);
577 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
579 setStartTS(thisRead);
581 if (isRecording) setEndTS();
586 if (feedMode == MODE_NORMAL)
588 feedPosition += thisRead;
590 else if (feedMode == MODE_BACKWARDS)
592 if (feedPosition >= blockSize)
594 feedPosition -= blockSize;
599 // got to the start of the recording.. revert to play mode? how?
600 feedPosition += thisRead;
606 while(writeLength < thisRead)
608 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
609 writeLength += thisWrite;
613 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
614 // demuxer is full and cant take anymore
616 threadWaitForSignal();
618 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
630 logger->log("Player", Log::DEBUG, "Recording playback ends");
634 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
635 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
636 else m->message = Message::STREAM_END; // live
637 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
638 commandMessageQueue->postMessage(m);
639 logger->log("Player", Log::DEBUG, "Message posted...");
642 void Player::threadPostStopCleanup()
644 logger->log("Player", Log::DEBUG, "Post stop cleanup 1");
647 logger->log("Player", Log::DEBUG, "Post stop cleanup 2");
651 logger->log("Player", Log::DEBUG, "Post stop cleanup 3");
654 void Player::setStartTS(UINT dataInBuffer)
656 if (isRecording && feedPosition) // (feedPosition != 0)
658 // FIXME find out how much data need to get to find a TS
659 // Need to get the actual start of the recording
662 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
663 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
664 if (!tempBuffer) return;
665 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
670 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
674 void Player::setEndTS()
676 logger->log("Player", Log::DEBUG, "Setting end TS");
679 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
680 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
681 if (!tempBuffer) return;
682 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
684 logger->log("Player", Log::DEBUG, "Set end TS");
690 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
695 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");