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;
219 video->unFastForward();
220 audio->systemMuteOff();
221 feedMode = MODE_NORMAL;
240 void Player::togglePause()
242 if (!initted) return;
243 if (!playing) return;
245 if (ffwd) toggleFastForward();
246 if (fbwd) toggleFastBackward();
262 void Player::setPosition(ULLONG position)
264 feedPosition = position;
267 void Player::setLength(ULLONG length)
269 lastRescan = time(NULL);
270 streamLength = length;
271 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
274 void Player::skipForward(int seconds)
276 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
278 if (paused) togglePause();
280 ULONG wantedFrameNumber = video->timecodeToFrameNumber(getPositionTS() + (seconds * 90000));
281 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
282 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
290 audio->doMuting(); // ???
292 feedPosition = newPosition;
304 void Player::skipBackward(int seconds)
306 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
308 if (paused) togglePause();
310 ULLONG newPosition = 0;
312 long long newTimeCode = getPositionTS() - (seconds * 90000);
315 ULONG wantedFrameNumber = video->timecodeToFrameNumber((ULLONG)newTimeCode);
316 newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
317 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
327 audio->doMuting(); // ???
329 feedPosition = newPosition;
341 void Player::toggleFastForward()
343 if (!initted) return;
344 if (!playing) return;
346 if (paused) togglePause();
347 if (fbwd) toggleFastBackward();
352 // video->unFastForward();
373 audio->systemMuteOff();
377 demuxer.flushAudio();
385 audio->systemMuteOff();
392 audio->systemMuteOn();
393 video->fastForward();
397 void Player::toggleFastBackward()
399 if (!initted) return;
400 if (!playing) return;
402 if (paused) togglePause();
403 if (ffwd) toggleFastForward();
409 audio->systemMuteOff();
412 feedMode = MODE_NORMAL;
419 audio->systemMuteOn();
422 feedMode = MODE_BACKWARDS;
430 void Player::jumpToPercent(int percent)
432 if (paused) togglePause();
433 if (ffwd) toggleFastForward();
444 feedPosition = streamLength * percent / 100;
456 ULLONG Player::getPositionTS()
458 if (startup) return 0ULL;
459 long long currentTS = video->getCurrentTimestamp() - startTS;
460 if (currentTS < 0) currentTS += 8589934592ULL;
461 return (ULLONG)currentTS;
464 ULLONG Player::getEndTS()
466 long long rendTS = endTS - startTS;
467 if (rendTS < 0) rendTS += 8589934592ULL;
468 return (ULLONG)rendTS;
471 void Player::call(void* caller)
473 if (caller == &demuxer)
475 logger->log("Player", Log::DEBUG, "Callback from demuxer");
477 if (video->getTVsize() == Video::ASPECT4X3)
479 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
483 int dxCurrentAspect = demuxer.getAspectRatio();
484 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
486 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
487 video->setAspectRatio(Video::ASPECT4X3);
489 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
491 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
492 video->setAspectRatio(Video::ASPECT16X9);
496 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
502 threadSignalNoLock();
508 void Player::threadMethod()
514 VDR* vdr = VDR::getInstance();
525 // If we havn't rescanned for a while..
526 if (isRecording && ((lastRescan + 60) < time(NULL)))
528 streamLength = vdr->rescanRecording();
529 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
530 lastRescan = time(NULL);
534 if (streamLength) // is playing a recording
536 if (feedPosition >= streamLength) break; // finished playback
540 if (startupBlockSize > streamLength)
541 askFor = streamLength; // is a very small recording!
543 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
547 if ((feedPosition + blockSize) > streamLength) // last block of recording
548 askFor = streamLength - feedPosition;
553 else // is playing live
556 askFor = startupBlockSize; // find audio streams sized block
558 askFor = blockSize; // normal
561 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
562 if (!threadBuffer) break;
566 int a_stream = demuxer.scan(threadBuffer, thisRead);
567 demuxer.setAudioStream(a_stream);
568 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
570 setStartTS(thisRead);
572 if (isRecording) setEndTS();
577 if (feedMode == MODE_NORMAL)
579 feedPosition += thisRead;
581 else if (feedMode == MODE_BACKWARDS)
583 if (feedPosition >= blockSize)
585 feedPosition -= blockSize;
590 // got to the start of the recording.. revert to play mode? how?
591 feedPosition += thisRead;
597 while(writeLength < thisRead)
599 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
600 writeLength += thisWrite;
604 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
605 // demuxer is full and cant take anymore
607 threadWaitForSignal();
609 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
621 logger->log("Player", Log::DEBUG, "Recording playback ends");
622 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
623 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
624 else m->message = Message::STREAM_END; // live
625 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
626 commandMessageQueue->postMessage(m);
627 logger->log("Player", Log::DEBUG, "Message posted...");
630 void Player::threadPostStopCleanup()
632 logger->log("Player", Log::DEBUG, "Post stop cleanup 1");
635 logger->log("Player", Log::DEBUG, "Post stop cleanup 2");
639 logger->log("Player", Log::DEBUG, "Post stop cleanup 3");
642 void Player::setStartTS(UINT dataInBuffer)
644 if (isRecording && feedPosition) // (feedPosition != 0)
646 // FIXME find out how much data need to get to find a TS
647 // Need to get the actual start of the recording
650 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
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) return;
668 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
670 logger->log("Player", Log::DEBUG, "Set end TS");
676 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
681 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");