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.
184 // struct timespec delay;
186 // delay.tv_nsec = 500000000;
187 // nanosleep(&delay, NULL);
197 usleep(500000); // SYNC
203 // ------------------------------------------------------------------------------------------------
211 if (!initted) return;
212 if (!playing) return;
214 logger->log("Player", Log::DEBUG, "Stop called", streamLength);
221 video->unFastForward();
222 audio->systemMuteOff();
223 feedMode = MODE_NORMAL;
242 void Player::togglePause()
244 if (!initted) return;
245 if (!playing) return;
247 if (ffwd) toggleFastForward();
248 if (fbwd) toggleFastBackward();
264 void Player::setPosition(ULLONG position)
266 feedPosition = position;
269 void Player::setLength(ULLONG length)
271 lastRescan = time(NULL);
272 streamLength = length;
273 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
276 void Player::skipForward(int seconds)
278 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
280 if (paused) togglePause();
282 ULONG wantedFrameNumber = video->timecodeToFrameNumber(getPositionTS() + (seconds * 90000));
283 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
284 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
292 audio->doMuting(); // ???
294 feedPosition = newPosition;
306 void Player::skipBackward(int seconds)
308 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
310 if (paused) togglePause();
312 ULLONG newPosition = 0;
314 long long newTimeCode = getPositionTS() - (seconds * 90000);
317 ULONG wantedFrameNumber = video->timecodeToFrameNumber((ULLONG)newTimeCode);
318 newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
319 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
329 audio->doMuting(); // ???
331 feedPosition = newPosition;
343 void Player::toggleFastForward()
345 if (!initted) return;
346 if (!playing) return;
348 if (paused) togglePause();
349 if (fbwd) toggleFastBackward();
354 // video->unFastForward();
375 audio->systemMuteOff();
379 demuxer.flushAudio();
387 audio->systemMuteOff();
394 audio->systemMuteOn();
395 video->fastForward();
399 void Player::toggleFastBackward()
401 if (!initted) return;
402 if (!playing) return;
404 if (paused) togglePause();
405 if (ffwd) toggleFastForward();
411 audio->systemMuteOff();
414 feedMode = MODE_NORMAL;
421 audio->systemMuteOn();
424 feedMode = MODE_BACKWARDS;
432 void Player::jumpToPercent(int percent)
434 if (paused) togglePause();
435 if (ffwd) toggleFastForward();
446 feedPosition = streamLength * percent / 100;
458 ULLONG Player::getPositionTS()
460 if (startup) return 0ULL;
461 long long currentTS = video->getCurrentTimestamp() - startTS;
462 if (currentTS < 0) currentTS += 8589934592ULL;
463 return (ULLONG)currentTS;
466 ULLONG Player::getEndTS()
468 long long rendTS = endTS - startTS;
469 if (rendTS < 0) rendTS += 8589934592ULL;
470 return (ULLONG)rendTS;
473 void Player::call(void* caller)
475 if (caller == &demuxer)
477 logger->log("Player", Log::DEBUG, "Callback from demuxer");
479 if (video->getTVsize() == Video::ASPECT4X3)
481 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
485 int dxCurrentAspect = demuxer.getAspectRatio();
486 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
488 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
489 video->setAspectRatio(Video::ASPECT4X3);
491 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
493 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
494 video->setAspectRatio(Video::ASPECT16X9);
498 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
504 threadSignalNoLock();
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())
533 Message* m = new Message();
534 m->message = Message::CONNECTION_LOST;
536 commandMessageQueue->postMessage(m);
539 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
540 lastRescan = time(NULL);
544 if (streamLength) // is playing a recording
546 if (feedPosition >= streamLength) break; // finished playback
550 if (startupBlockSize > streamLength)
551 askFor = streamLength; // is a very small recording!
553 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
557 if ((feedPosition + blockSize) > streamLength) // last block of recording
558 askFor = streamLength - feedPosition;
563 else // is playing live
566 askFor = startupBlockSize; // find audio streams sized block
568 askFor = blockSize; // normal
571 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
572 if (!vdr->isConnected())
574 Message* m = new Message();
575 m->message = Message::CONNECTION_LOST;
577 commandMessageQueue->postMessage(m);
581 if (!threadBuffer) break;
585 int a_stream = demuxer.scan(threadBuffer, thisRead);
586 demuxer.setAudioStream(a_stream);
587 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
589 setStartTS(thisRead);
591 if (isRecording) setEndTS();
596 if (feedMode == MODE_NORMAL)
598 feedPosition += thisRead;
600 else if (feedMode == MODE_BACKWARDS)
602 if (feedPosition >= blockSize)
604 feedPosition -= blockSize;
609 // got to the start of the recording.. revert to play mode? how?
610 feedPosition += thisRead;
616 while(writeLength < thisRead)
618 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
619 writeLength += thisWrite;
623 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
624 // demuxer is full and cant take anymore
626 threadWaitForSignal();
628 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
640 logger->log("Player", Log::DEBUG, "Recording playback ends");
644 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
645 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
646 else m->message = Message::STREAM_END; // live
647 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
648 commandMessageQueue->postMessage(m);
649 logger->log("Player", Log::DEBUG, "Message posted...");
652 void Player::threadPostStopCleanup()
654 logger->log("Player", Log::DEBUG, "Post stop cleanup 1");
657 logger->log("Player", Log::DEBUG, "Post stop cleanup 2");
661 logger->log("Player", Log::DEBUG, "Post stop cleanup 3");
664 void Player::setStartTS(UINT dataInBuffer)
666 if (isRecording && feedPosition) // (feedPosition != 0)
668 // FIXME find out how much data need to get to find a TS
669 // Need to get the actual start of the recording
672 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
673 if (!tempBuffer) return;
674 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
679 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
683 void Player::setEndTS()
685 logger->log("Player", Log::DEBUG, "Setting end TS");
688 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
689 if (!tempBuffer) return;
690 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
692 logger->log("Player", Log::DEBUG, "Set end TS");
698 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
703 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");