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)
24 : vfeed(this), afeed(this)
26 commandMessageQueue = messageQueue;
33 feedMode = MODE_NORMAL;
34 isRecording = tIsRecording;
39 PlayerVideo::~PlayerVideo()
41 if (initted) shutdown();
44 int PlayerVideo::init()
46 if (initted) return 0;
48 video = Video::getInstance();
50 if (demuxer.init()) // inverted
52 Log::getInstance()->log("Player", Log::ERR, "Demuxer failed to init");
57 vfeed.init(video->getFD());
58 afeed.init(audio->getFD());
69 int PlayerVideo::shutdown()
71 if (!initted) return 0;
74 Log::getInstance()->log("Player", Log::DEBUG, "Player shutdown...");
94 int PlayerVideo::play()
96 if (!initted) return 0;
98 // If we are just paused, unpause!
105 // If we are fast forwarding, set to normal
112 // If we are fast backwarding, set to normal
115 toggleFastBackward();
119 // If we are already playing, bail // FIXME - resync?
122 Log::getInstance()->log("Player", Log::DEBUG, "DOING RESYNC");
153 // Standard play start
160 // ------------------------ This one works, but doesn't allow any pre-buffering.
170 usleep(500000); // SYNC
175 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
182 // struct timespec delay;
184 // delay.tv_nsec = 500000000;
185 // nanosleep(&delay, NULL);
194 // ------------------------------------------------------------------------------------------------
200 void PlayerVideo::stop()
202 if (!initted) return;
203 if (!playing) return;
210 video->unFastForward();
211 audio->systemMuteOff();
212 feedMode = MODE_NORMAL;
231 void PlayerVideo::togglePause()
233 if (!initted) return;
234 if (!playing) return;
236 if (ffwd) toggleFastForward();
237 if (fbwd) toggleFastBackward();
253 void PlayerVideo::test()
255 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
261 static int flipflop = 0;
264 if (flipflop) a = video->setAspectRatio(Video::ASPECT16X9);
265 else a = video->setAspectRatio(Video::ASPECT4X3);
267 flipflop = !flipflop;
269 printf("A = %i\n", a);
273 void PlayerVideo::test2()
275 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
280 void PlayerVideo::setPosition(ULLONG position)
282 feedPosition = position;
285 void PlayerVideo::setLength(ULLONG length)
287 lastRescan = time(NULL);
288 streamLength = length;
289 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
292 void PlayerVideo::skipForward(int seconds)
294 // skip forward 1 minute
295 Log::getInstance()->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
297 if (paused) togglePause();
299 ULLONG moveBy = seconds * 500000;
307 audio->doMuting(); // ???
309 feedPosition += moveBy;
311 // printf("Audio test %i\n", audio->test());
322 usleep(500000); // SYNC
329 void PlayerVideo::skipBackward(int seconds)
331 // skip forward 1 minute
332 Log::getInstance()->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
334 if (paused) togglePause();
336 ULLONG moveBy = seconds * 500000;
345 audio->doMuting(); // ???
347 if (feedPosition > moveBy) feedPosition -= moveBy;
357 usleep(500000); // SYNC
364 void PlayerVideo::toggleFastForward()
366 if (!initted) return;
367 if (!playing) return;
369 if (paused) togglePause();
370 if (fbwd) toggleFastBackward();
375 // video->unFastForward();
396 audio->systemMuteOff();
399 usleep(500000); // SYNC
405 demuxer.flushAudio();
413 audio->systemMuteOff();
420 audio->systemMuteOn();
421 video->fastForward();
425 void PlayerVideo::toggleFastBackward()
427 if (!initted) return;
428 if (!playing) return;
430 if (paused) togglePause();
431 if (ffwd) toggleFastForward();
437 audio->systemMuteOff();
440 feedMode = MODE_NORMAL;
447 audio->systemMuteOn();
450 feedMode = MODE_BACKWARDS;
458 void PlayerVideo::jumpToPercent(int percent)
469 feedPosition = streamLength * percent / 100;
479 usleep(500000); // SYNC
486 void PlayerVideo::call()
488 threadSignalNoLock();
493 void PlayerVideo::threadMethod()
499 VDR* vdr = VDR::getInstance();
510 // If we havn't rescanned for a while..
511 if (isRecording && ((lastRescan + 60) < time(NULL)))
513 streamLength = vdr->rescanRecording();
514 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
515 lastRescan = time(NULL);
518 if (streamLength) // is playing a recording
520 if (feedPosition >= streamLength) break; // finished playback
524 if (startupBlockSize > streamLength)
525 askFor = streamLength; // is a very small recording!
527 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
531 if ((feedPosition + blockSize) > streamLength) // last block of recording
532 askFor = streamLength - feedPosition;
537 else // is playing live
540 askFor = startupBlockSize; // find audio streams sized block
542 askFor = blockSize; // normal
545 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
546 if (!threadBuffer) break;
550 int a_stream = demuxer.scan(threadBuffer, thisRead);
551 demuxer.setAudioStream(a_stream);
552 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
556 if (feedMode == MODE_NORMAL)
558 feedPosition += thisRead;
560 else if (feedMode == MODE_BACKWARDS)
562 if (feedPosition >= blockSize)
564 feedPosition -= blockSize;
569 // got to the start of the recording.. revert to play mode? how?
570 feedPosition += thisRead;
576 while(writeLength < thisRead)
578 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
579 writeLength += thisWrite;
583 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
584 // demuxer is full and cant take anymore
585 threadWaitForSignal();
586 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
598 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
599 Message* m = new Message();
600 m->message = Message::STOP_PLAYBACK;
601 Log::getInstance()->log("Player", Log::DEBUG, "Posting message...");
602 commandMessageQueue->postMessage(m);
603 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
608 void PlayerVideo::threadPostStopCleanup()
610 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
613 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");