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
21 #include "playervideo.h"
23 PlayerVideo::PlayerVideo(MessageQueue* messageQueue, UCHAR tIsRecording, UCHAR isRadio)
24 : vfeed(this), afeed(this)
26 commandMessageQueue = messageQueue;
28 logger = Log::getInstance();
34 feedMode = MODE_NORMAL;
35 isRecording = tIsRecording;
45 startupBlockSize = 60000;
50 startupBlockSize = 250000;
54 PlayerVideo::~PlayerVideo()
56 if (initted) shutdown();
59 int PlayerVideo::init()
61 if (initted) return 0;
63 video = Video::getInstance();
65 if (!demuxer.init(this))
67 logger->log("Player", Log::ERR, "Demuxer failed to init");
72 vfeed.init(video->getFD());
73 afeed.init(audio->getFD());
83 int PlayerVideo::shutdown()
85 if (!initted) return 0;
88 logger->log("Player", Log::DEBUG, "Player shutdown...");
104 logger->log("Player", Log::DEBUG, "Player shutdown done");
109 void PlayerVideo::resyncAudio()
113 if (!initted) return;
116 usleep(500000); // SYNC
120 int PlayerVideo::play()
122 if (!initted) return 0;
124 // If we are just paused, unpause!
131 // If we are fast forwarding, set to normal
138 // If we are fast backwarding, set to normal
141 toggleFastBackward();
145 // If we are already playing, bail // FIXME - resync?
148 logger->log("Player", Log::DEBUG, "DOING RESYNC");
179 // Standard play start
185 // ------------------------ This one works, but doesn't allow any pre-buffering.
195 usleep(500000); // SYNC
199 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
205 // struct timespec delay;
207 // delay.tv_nsec = 500000000;
208 // nanosleep(&delay, NULL);
218 usleep(500000); // SYNC
224 // ------------------------------------------------------------------------------------------------
230 void PlayerVideo::stop()
232 if (!initted) return;
233 if (!playing) return;
240 video->unFastForward();
241 audio->systemMuteOff();
242 feedMode = MODE_NORMAL;
248 logger->log("PlayerVideo", Log::DEBUG, "Temp 1");
250 logger->log("PlayerVideo", Log::DEBUG, "Temp 2");
252 logger->log("PlayerVideo", Log::DEBUG, "Temp 3");
254 logger->log("PlayerVideo", Log::DEBUG, "Temp 4");
256 logger->log("PlayerVideo", Log::DEBUG, "Temp 5");
258 logger->log("PlayerVideo", Log::DEBUG, "Temp 6");
260 logger->log("PlayerVideo", Log::DEBUG, "Temp 7");
262 logger->log("PlayerVideo", Log::DEBUG, "Temp 8");
264 logger->log("PlayerVideo", Log::DEBUG, "Temp 9");
266 logger->log("PlayerVideo", Log::DEBUG, "Temp 10");
271 void PlayerVideo::togglePause()
273 if (!initted) return;
274 if (!playing) return;
276 if (ffwd) toggleFastForward();
277 if (fbwd) toggleFastBackward();
293 void PlayerVideo::setPosition(ULLONG position)
295 feedPosition = position;
298 void PlayerVideo::setLength(ULLONG length)
300 lastRescan = time(NULL);
301 streamLength = length;
302 logger->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
305 void PlayerVideo::skipForward(int seconds)
307 // skip forward 1 minute
308 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
310 if (paused) togglePause();
312 ULLONG moveBy = seconds * 500000;
320 audio->doMuting(); // ???
322 feedPosition += moveBy;
324 // printf("Audio test %i\n", audio->test());
335 usleep(500000); // SYNC
342 void PlayerVideo::skipBackward(int seconds)
344 // skip forward 1 minute
345 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
347 if (paused) togglePause();
349 ULLONG moveBy = seconds * 500000;
358 audio->doMuting(); // ???
360 if (feedPosition > moveBy) feedPosition -= moveBy;
370 usleep(500000); // SYNC
377 void PlayerVideo::toggleFastForward()
379 if (!initted) return;
380 if (!playing) return;
382 if (paused) togglePause();
383 if (fbwd) toggleFastBackward();
388 // video->unFastForward();
409 audio->systemMuteOff();
412 usleep(500000); // SYNC
418 demuxer.flushAudio();
426 audio->systemMuteOff();
433 audio->systemMuteOn();
434 video->fastForward();
438 void PlayerVideo::toggleFastBackward()
440 if (!initted) return;
441 if (!playing) return;
443 if (paused) togglePause();
444 if (ffwd) toggleFastForward();
450 audio->systemMuteOff();
453 feedMode = MODE_NORMAL;
460 audio->systemMuteOn();
463 feedMode = MODE_BACKWARDS;
471 void PlayerVideo::jumpToPercent(int percent)
473 if (paused) togglePause();
474 if (ffwd) toggleFastForward();
485 feedPosition = streamLength * percent / 100;
495 usleep(500000); // SYNC
501 ULLONG PlayerVideo::getPositionTS()
503 if (startup) return 0ULL;
504 long long currentTS = video->getCurrentTimestamp() - startTS;
505 if (currentTS < 0) currentTS += 8589934592ULL;
506 return (ULLONG)currentTS;
509 ULLONG PlayerVideo::getEndTS()
511 long long rendTS = endTS - startTS;
512 if (rendTS < 0) rendTS += 8589934592ULL;
513 return (ULLONG)rendTS;
516 void PlayerVideo::call(void* caller)
518 if (caller == &demuxer)
520 logger->log("Player", Log::DEBUG, "Callback from demuxer");
522 if (video->getTVsize() == Video::ASPECT4X3)
524 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
528 int dxCurrentAspect = demuxer.getAspectRatio();
529 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
531 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
532 video->setAspectRatio(Video::ASPECT4X3);
534 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
536 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
537 video->setAspectRatio(Video::ASPECT16X9);
541 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
547 threadSignalNoLock();
553 void PlayerVideo::threadMethod()
559 VDR* vdr = VDR::getInstance();
570 // If we havn't rescanned for a while..
571 if (isRecording && ((lastRescan + 60) < time(NULL)))
573 streamLength = vdr->rescanRecording();
574 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
575 lastRescan = time(NULL);
579 if (streamLength) // is playing a recording
581 if (feedPosition >= streamLength) break; // finished playback
585 if (startupBlockSize > streamLength)
586 askFor = streamLength; // is a very small recording!
588 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
592 if ((feedPosition + blockSize) > streamLength) // last block of recording
593 askFor = streamLength - feedPosition;
598 else // is playing live
601 askFor = startupBlockSize; // find audio streams sized block
603 askFor = blockSize; // normal
606 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
607 if (!threadBuffer) break;
611 int a_stream = demuxer.scan(threadBuffer, thisRead);
612 demuxer.setAudioStream(a_stream);
613 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
615 setStartTS(thisRead);
617 if (isRecording) setEndTS();
622 if (feedMode == MODE_NORMAL)
624 feedPosition += thisRead;
626 else if (feedMode == MODE_BACKWARDS)
628 if (feedPosition >= blockSize)
630 feedPosition -= blockSize;
635 // got to the start of the recording.. revert to play mode? how?
636 feedPosition += thisRead;
642 while(writeLength < thisRead)
644 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
645 writeLength += thisWrite;
649 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
650 // demuxer is full and cant take anymore
652 threadWaitForSignal();
654 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
666 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
667 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
668 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
669 else m->message = Message::STREAM_END; // live
670 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
671 commandMessageQueue->postMessage(m);
672 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
675 void PlayerVideo::threadPostStopCleanup()
677 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
680 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");
686 void PlayerVideo::setStartTS(UINT dataInBuffer)
688 if (isRecording && feedPosition) // (feedPosition != 0)
690 // FIXME find out how much data need to get to find a TS
691 // Need to get the actual start of the recording
694 UCHAR* tempBuffer = VDR::getInstance()->getBlock(0, 100000, &thisRead);
695 if (!tempBuffer) return;
696 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &startTS);
701 demuxer.findVideoPTS(threadBuffer, dataInBuffer, &startTS);
705 void PlayerVideo::setEndTS()
707 Log::getInstance()->log("Player", Log::DEBUG, "Setting end TS");
710 UCHAR* tempBuffer = VDR::getInstance()->getBlock((streamLength - 100000), 100000, &thisRead);
711 if (!tempBuffer) return;
712 if (thisRead) demuxer.findVideoPTS(tempBuffer, thisRead, &endTS);
714 Log::getInstance()->log("Player", Log::DEBUG, "Set end TS");
718 void PlayerVideo::test1()
720 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 1");
723 void PlayerVideo::test2()
725 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 2");