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;
120 if (!initted) return 0;
122 // If we are just paused, unpause!
129 // If we are still seeking in from the last jump, ignore
130 if (videoStartup) return 1;
132 // If we are fast forwarding, set to normal
139 // If we are fast backwarding, set to normal
142 toggleFastBackward();
146 // Standard play start
147 logger->log("Player", Log::DEBUG, "Standard play start");
154 if (!isRadio) demuxer.seek();
161 logger->log("Player", Log::DEBUG, "Immediate play");
169 else // do prebuffering
171 logger->log("Player", Log::DEBUG, "Prebuffering...");
175 // ------------------------------------------------------------------------------------------------
183 if (!initted) return;
184 if (!playing) return;
186 // If we are still seeking in from the last jump, ignore
187 if (videoStartup) return;
194 video->unFastForward();
195 audio->systemMuteOff();
196 feedMode = MODE_NORMAL;
215 void Player::togglePause()
217 if (!initted) return;
218 if (!playing) return;
220 if (ffwd) toggleFastForward();
221 if (fbwd) toggleFastBackward();
237 void Player::setPosition(ULLONG position)
239 feedPosition = position;
242 void Player::setLength(ULLONG length)
244 lastRescan = time(NULL);
245 streamLength = length;
246 logger->log("Player", Log::DEBUG, "Player has received length of %llu", streamLength);
249 void Player::restartAt(ULLONG timecode)
251 // If we are still seeking in from the last jump, ignore
252 if (videoStartup) return;
254 if (paused) togglePause();
255 if (ffwd) toggleFastForward();
257 ULONG wantedFrameNumber = video->timecodeToFrameNumber(timecode);
258 ULLONG newPosition = VDR::getInstance()->positionFromFrameNumber(wantedFrameNumber);
259 if (!VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
260 logger->log("Player", Log::DEBUG, "wantedframe %i feedpos %llu goto %llu", wantedFrameNumber, feedPosition, newPosition);
269 if (!isRadio) demuxer.seek();
270 feedPosition = newPosition;
278 audio->systemMuteOff();
283 void Player::skipForward(int seconds)
285 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
286 restartAt(getPositionTS() + (seconds * 90000));
289 void Player::skipBackward(int seconds)
291 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
292 long long newTimeCode = getPositionTS() - (seconds * 90000);
293 if (newTimeCode < 0) newTimeCode = 0;
294 restartAt(newTimeCode);
297 void Player::jumpToPercent(int percent)
299 logger->log("Player", Log::DEBUG, "JUMP TO %i%%", percent);
300 ULLONG newTimeCode = (ULLONG)(getEndTS() * ((float)percent / 100));
301 restartAt(newTimeCode);
304 void Player::toggleFastForward()
306 if (!initted) return;
307 if (!playing) return;
309 // If we are still seeking in from the last jump, ignore
310 if (videoStartup) return;
312 if (paused) togglePause();
313 if (fbwd) toggleFastBackward();
326 if (!isRadio) demuxer.seek();
336 audio->systemMuteOff();
344 audio->systemMuteOn();
345 video->fastForward();
349 void Player::toggleFastBackward()
351 if (!initted) return;
352 if (!playing) return;
354 // If we are still seeking in from the last jump, ignore
355 if (videoStartup) return;
357 if (paused) togglePause();
358 if (ffwd) toggleFastForward();
364 audio->systemMuteOff();
367 feedMode = MODE_NORMAL;
374 audio->systemMuteOn();
377 feedMode = MODE_BACKWARDS;
381 if (!isRadio) demuxer.seek();
387 ULLONG Player::getPositionTS()
389 if (startup) return 0ULL;
390 long long currentTS = video->getCurrentTimestamp() - startTS;
391 if (currentTS < 0) currentTS += 8589934592ULL;
392 return (ULLONG)currentTS;
395 ULLONG Player::getEndTS()
397 long long rendTS = endTS - startTS;
398 if (rendTS < 0) rendTS += 8589934592ULL;
399 return (ULLONG)rendTS;
402 void Player::call(void* caller)
404 if (caller == &demuxer)
406 logger->log("Player", Log::DEBUG, "Callback from demuxer");
408 if (video->getTVsize() == Video::ASPECT4X3)
410 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
414 int dxCurrentAspect = demuxer.getAspectRatio();
415 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
417 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
418 video->setAspectRatio(Video::ASPECT4X3);
420 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
422 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
423 video->setAspectRatio(Video::ASPECT16X9);
427 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
435 videoStartup = false;
442 threadSignalNoLock();
445 void Player::doConnectionLost()
447 Message* m = new Message();
448 m->message = Message::CONNECTION_LOST;
450 commandMessageQueue->postMessage(m);
455 void Player::threadMethod()
462 VDR* vdr = VDR::getInstance();
473 // If we havn't rescanned for a while..
474 if (isRecording && ((lastRescan + 60) < time(NULL)))
476 streamLength = vdr->rescanRecording();
477 if (!vdr->isConnected()) { doConnectionLost(); return; }
478 logger->log("Player", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
479 lastRescan = time(NULL);
483 if (streamLength) // is playing a recording
485 if (feedPosition >= streamLength) break; // finished playback
489 if (startupBlockSize > streamLength)
490 askFor = streamLength; // is a very small recording!
492 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
496 if ((feedPosition + blockSize) > streamLength) // last block of recording
497 askFor = streamLength - feedPosition;
502 else // is playing live
505 askFor = startupBlockSize; // find audio streams sized block
507 askFor = blockSize; // normal
510 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
511 if (!vdr->isConnected())
517 if (!threadBuffer) break;
521 int a_stream = demuxer.scan(threadBuffer, thisRead);
522 demuxer.setAudioStream(a_stream);
523 logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
525 setStartTS(thisRead);
527 if (isRecording) setEndTS();
534 preBufferTotal += thisRead;
535 if (preBufferTotal > 500000)
537 logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete");
539 preBuffering = false;
548 // FIXME - see if this can segfault because it is starting threads out of the master mutex
552 if (feedMode == MODE_NORMAL)
554 feedPosition += thisRead;
556 else if (feedMode == MODE_BACKWARDS)
558 if (feedPosition >= blockSize)
560 feedPosition -= blockSize;
561 if (!isRadio) demuxer.seek();
565 // got to the start of the recording.. revert to play mode? how?
566 feedPosition += thisRead;
572 while(writeLength < thisRead)
574 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
575 writeLength += thisWrite;
576 // logger->log("Player", Log::DEBUG, "Put %i to demuxer", thisWrite);
580 // logger->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
581 // demuxer is full and cant take anymore
583 threadWaitForSignal();
585 // logger->log("Player", Log::DEBUG, "BACK FROM WAIT");
597 logger->log("Player", Log::DEBUG, "Recording playback ends");
601 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
602 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
603 else m->message = Message::STREAM_END; // live
604 logger->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
605 commandMessageQueue->postMessage(m);
606 logger->log("Player", Log::DEBUG, "Message posted...");
609 void Player::threadPostStopCleanup()
618 void Player::setStartTS(UINT dataInBuffer)
620 if (isRecording && feedPosition) // (feedPosition != 0)
622 // FIXME find out how much data need to get to find a TS
623 // Need to get the actual start of the recording
626 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
627 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
628 if (!tempBuffer) return;
629 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
634 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
638 void Player::setEndTS()
640 logger->log("Player", Log::DEBUG, "Setting end TS");
643 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
644 if (!tempBuffer && !VDR::getInstance()->isConnected()) { doConnectionLost(); return; }
645 if (!tempBuffer) return;
646 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
648 logger->log("Player", Log::DEBUG, "Set end TS");
654 logger->log("Player", Log::DEBUG, "PLAYER TEST 1");
656 // video->setAspectRatio(Video::ASPECT4X3);
661 logger->log("Player", Log::DEBUG, "PLAYER TEST 2");
662 video->setAspectRatio(Video::ASPECT16X9);