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");
178 // ------------------------ This one works, but doesn't allow any pre-buffering.
188 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
191 // ------------------------------------------------------------------------------------------------
199 if (!initted) return;
200 if (!playing) return;
202 logger->log("Player", Log::DEBUG, "Stop called", streamLength);
209 video->unFastForward();
210 audio->systemMuteOff();
211 feedMode = MODE_NORMAL;
230 void Player::togglePause()
232 if (!initted) return;
233 if (!playing) return;
235 if (ffwd) toggleFastForward();
236 if (fbwd) toggleFastBackward();
252 void Player::setPosition(ULLONG position)
254 feedPosition = position;
257 void Player::setLength(ULLONG length)
259 lastRescan = time(NULL);
260 streamLength = length;
261 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
264 void Player::restartAt(ULLONG timecode)
266 if (paused) togglePause();
267 if (ffwd) toggleFastForward();
269 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
270 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
271 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
272 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
281 if (!isRadio) demuxer.seek();
282 feedPosition = newPosition;
289 audio->systemMuteOff();
294 void Player::skipForward(int seconds)
296 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
297 restartAt(getPositionTS() + (seconds * 90000));
300 void Player::skipBackward(int seconds)
302 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
303 long long newTimeCode = getPositionTS() - (seconds * 90000);
304 if (newTimeCode < 0) newTimeCode = 0;
305 restartAt(newTimeCode);
308 void Player::jumpToPercent(int percent)
310 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
311 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
312 restartAt(newTimeCode);
315 void Player::toggleFastForward()
317 if (!initted) return;
318 if (!playing) return;
320 if (paused) togglePause();
321 if (fbwd) toggleFastBackward();
334 if (!isRadio) demuxer.seek();
343 audio->systemMuteOff();
351 audio->systemMuteOn();
352 video->fastForward();
356 void Player::toggleFastBackward()
358 if (!initted) return;
359 if (!playing) return;
361 if (paused) togglePause();
362 if (ffwd) toggleFastForward();
368 audio->systemMuteOff();
371 feedMode = MODE_NORMAL;
378 audio->systemMuteOn();
381 feedMode = MODE_BACKWARDS;
385 if (!isRadio) demuxer.seek();
391 ULLONG Player::getPositionTS()
393 if (startup) return 0ULL;
394 long long currentTS = video->getCurrentTimestamp() - startTS;
395 if (currentTS < 0) currentTS += 8589934592ULL;
396 return (ULLONG)currentTS;
399 ULLONG Player::getEndTS()
401 long long rendTS = endTS - startTS;
402 if (rendTS < 0) rendTS += 8589934592ULL;
403 return (ULLONG)rendTS;
406 void Player::call(void* caller)
408 if (caller == &demuxer)
410 logger->log("Player", Log::DEBUG, "Callback from demuxer");
412 if (video->getTVsize() == Video::ASPECT4X3)
414 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
418 int dxCurrentAspect = demuxer.getAspectRatio();
419 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
421 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
422 video->setAspectRatio(Video::ASPECT4X3);
424 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
426 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
427 video->setAspectRatio(Video::ASPECT16X9);
431 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
439 videoStartup = false;
440 logger->log("Player", Log::DEBUG, "Starting VFeed");
447 threadSignalNoLock();
450 void Player::doConnectionLost()
452 Message* m = new Message();
453 m->message = Message::CONNECTION_LOST;
455 commandMessageQueue->postMessage(m);
460 void Player::threadMethod()
466 VDR* vdr = VDR::getInstance();
477 // If we havn't rescanned for a while..
478 if (isRecording && ((lastRescan + 60) < time(NULL)))
480 streamLength = vdr->rescanRecording();
481 if (!vdr->isConnected()) { doConnectionLost(); return; }
482 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
483 lastRescan = time(NULL);
487 if (streamLength) // is playing a recording
489 if (feedPosition >= streamLength) break; // finished playback
493 if (startupBlockSize > streamLength)
494 askFor = streamLength; // is a very small recording!
496 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
500 if ((feedPosition + blockSize) > streamLength) // last block of recording
501 askFor = streamLength - feedPosition;
506 else // is playing live
509 askFor = startupBlockSize; // find audio streams sized block
511 askFor = blockSize; // normal
514 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
515 if (!vdr->isConnected())
521 if (!threadBuffer) break;
525 int a_stream = demuxer.scan(threadBuffer, thisRead);
526 demuxer.setAudioStream(a_stream);
527 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
529 setStartTS(thisRead);
531 if (isRecording) setEndTS();
536 if (feedMode == MODE_NORMAL)
538 feedPosition += thisRead;
540 else if (feedMode == MODE_BACKWARDS)
542 if (feedPosition >= blockSize)
544 feedPosition -= blockSize;
545 if (!isRadio) demuxer.seek();
549 // got to the start of the recording.. revert to play mode? how?
550 feedPosition += thisRead;
556 while(writeLength < thisRead)
558 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
559 writeLength += thisWrite;
563 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
564 // demuxer is full and cant take anymore
566 threadWaitForSignal();
568 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
580 logger->log("Player", Log::DEBUG, "Recording playback ends");
584 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
585 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
586 else m->message = Message::STREAM_END; // live
587 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
588 commandMessageQueue->postMessage(m);
589 logger->log("Player", Log::DEBUG, "Message posted...");
592 void Player::threadPostStopCleanup()
594 logger->log("Player", Log::DEBUG, "Post stop cleanup 1");
597 logger->log("Player", Log::DEBUG, "Post stop cleanup 2");
601 logger->log("Player", Log::DEBUG, "Post stop cleanup 3");
604 void Player::setStartTS(UINT dataInBuffer)
606 if (isRecording && feedPosition) // (feedPosition != 0)
608 // FIXME find out how much data need to get to find a TS
609 // Need to get the actual start of the recording
612 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
613 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
614 if (!tempBuffer) return;
615 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
620 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
624 void Player::setEndTS()
626 logger->log("Player", Log::DEBUG, "Setting end TS");
629 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
630 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
631 if (!tempBuffer) return;
632 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
634 logger->log("Player", Log::DEBUG, "Set end TS");
640 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
641 video->setAspectRatio(Video::ASPECT4X3);
646 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
647 video->setAspectRatio(Video::ASPECT16X9);