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;
42 startupBlockSize = 60000;
47 startupBlockSize = 250000;
51 PlayerVideo::~PlayerVideo()
53 if (initted) shutdown();
56 int PlayerVideo::init()
58 if (initted) return 0;
60 video = Video::getInstance();
62 if (!demuxer.init(this))
64 logger->log("Player", Log::ERR, "Demuxer failed to init");
69 vfeed.init(video->getFD());
70 afeed.init(audio->getFD());
81 int PlayerVideo::shutdown()
83 if (!initted) return 0;
86 logger->log("Player", Log::DEBUG, "Player shutdown...");
102 logger->log("Player", Log::DEBUG, "Player shutdown done");
107 void PlayerVideo::resyncAudio()
111 if (!initted) return;
114 usleep(500000); // SYNC
118 int PlayerVideo::play()
120 if (!initted) return 0;
122 // If we are just paused, unpause!
129 // If we are fast forwarding, set to normal
136 // If we are fast backwarding, set to normal
139 toggleFastBackward();
143 // If we are already playing, bail // FIXME - resync?
146 logger->log("Player", Log::DEBUG, "DOING RESYNC");
177 // Standard play start
184 // ------------------------ This one works, but doesn't allow any pre-buffering.
194 usleep(500000); // SYNC
198 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
204 // struct timespec delay;
206 // delay.tv_nsec = 500000000;
207 // nanosleep(&delay, NULL);
217 usleep(500000); // SYNC
223 // ------------------------------------------------------------------------------------------------
229 void PlayerVideo::stop()
231 if (!initted) return;
232 if (!playing) return;
239 video->unFastForward();
240 audio->systemMuteOff();
241 feedMode = MODE_NORMAL;
247 logger->log("PlayerVideo", Log::DEBUG, "Temp 1");
249 logger->log("PlayerVideo", Log::DEBUG, "Temp 2");
251 logger->log("PlayerVideo", Log::DEBUG, "Temp 3");
253 logger->log("PlayerVideo", Log::DEBUG, "Temp 4");
255 logger->log("PlayerVideo", Log::DEBUG, "Temp 5");
257 logger->log("PlayerVideo", Log::DEBUG, "Temp 6");
259 logger->log("PlayerVideo", Log::DEBUG, "Temp 7");
261 logger->log("PlayerVideo", Log::DEBUG, "Temp 8");
263 logger->log("PlayerVideo", Log::DEBUG, "Temp 9");
265 logger->log("PlayerVideo", Log::DEBUG, "Temp 10");
270 void PlayerVideo::togglePause()
272 if (!initted) return;
273 if (!playing) return;
275 if (ffwd) toggleFastForward();
276 if (fbwd) toggleFastBackward();
292 void PlayerVideo::setPosition(ULLONG position)
294 feedPosition = position;
297 void PlayerVideo::setLength(ULLONG length)
299 lastRescan = time(NULL);
300 streamLength = length;
301 logger->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
304 void PlayerVideo::skipForward(int seconds)
306 // skip forward 1 minute
307 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
309 if (paused) togglePause();
311 ULLONG moveBy = seconds * 500000;
319 audio->doMuting(); // ???
321 feedPosition += moveBy;
323 // printf("Audio test %i\n", audio->test());
334 usleep(500000); // SYNC
341 void PlayerVideo::skipBackward(int seconds)
343 // skip forward 1 minute
344 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
346 if (paused) togglePause();
348 ULLONG moveBy = seconds * 500000;
357 audio->doMuting(); // ???
359 if (feedPosition > moveBy) feedPosition -= moveBy;
369 usleep(500000); // SYNC
376 void PlayerVideo::toggleFastForward()
378 if (!initted) return;
379 if (!playing) return;
381 if (paused) togglePause();
382 if (fbwd) toggleFastBackward();
387 // video->unFastForward();
408 audio->systemMuteOff();
411 usleep(500000); // SYNC
417 demuxer.flushAudio();
425 audio->systemMuteOff();
432 audio->systemMuteOn();
433 video->fastForward();
437 void PlayerVideo::toggleFastBackward()
439 if (!initted) return;
440 if (!playing) return;
442 if (paused) togglePause();
443 if (ffwd) toggleFastForward();
449 audio->systemMuteOff();
452 feedMode = MODE_NORMAL;
459 audio->systemMuteOn();
462 feedMode = MODE_BACKWARDS;
470 void PlayerVideo::jumpToPercent(int percent)
472 if (paused) togglePause();
473 if (ffwd) toggleFastForward();
484 feedPosition = streamLength * percent / 100;
494 usleep(500000); // SYNC
501 void PlayerVideo::call(void* caller)
503 if (caller == &demuxer)
505 logger->log("Player", Log::DEBUG, "Callback from demuxer");
507 if (video->getTVsize() == Video::ASPECT4X3)
509 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
513 int dxCurrentAspect = demuxer.getAspectRatio();
514 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
516 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
517 video->setAspectRatio(Video::ASPECT4X3);
519 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
521 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
522 video->setAspectRatio(Video::ASPECT16X9);
526 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
532 threadSignalNoLock();
538 void PlayerVideo::threadMethod()
544 VDR* vdr = VDR::getInstance();
555 // If we havn't rescanned for a while..
556 if (isRecording && ((lastRescan + 60) < time(NULL)))
558 streamLength = vdr->rescanRecording();
559 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
560 lastRescan = time(NULL);
563 if (streamLength) // is playing a recording
565 if (feedPosition >= streamLength) break; // finished playback
569 if (startupBlockSize > streamLength)
570 askFor = streamLength; // is a very small recording!
572 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
576 if ((feedPosition + blockSize) > streamLength) // last block of recording
577 askFor = streamLength - feedPosition;
582 else // is playing live
585 askFor = startupBlockSize; // find audio streams sized block
587 askFor = blockSize; // normal
590 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
591 if (!threadBuffer) break;
595 int a_stream = demuxer.scan(threadBuffer, thisRead);
596 demuxer.setAudioStream(a_stream);
597 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
601 if (feedMode == MODE_NORMAL)
603 feedPosition += thisRead;
605 else if (feedMode == MODE_BACKWARDS)
607 if (feedPosition >= blockSize)
609 feedPosition -= blockSize;
614 // got to the start of the recording.. revert to play mode? how?
615 feedPosition += thisRead;
621 while(writeLength < thisRead)
623 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
624 writeLength += thisWrite;
628 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
629 // demuxer is full and cant take anymore
631 threadWaitForSignal();
633 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
645 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
646 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
647 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
648 else m->message = Message::STREAM_END; // live
649 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
650 commandMessageQueue->postMessage(m);
651 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
654 void PlayerVideo::threadPostStopCleanup()
656 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
659 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");
666 void PlayerVideo::test1()
668 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 1");
671 void PlayerVideo::test2()
673 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 2");