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;
38 PlayerVideo::~PlayerVideo()
40 if (initted) shutdown();
43 int PlayerVideo::init()
45 if (initted) return 0;
47 video = Video::getInstance();
49 if (demuxer.init()) // inverted
51 Log::getInstance()->log("Player", Log::ERR, "Demuxer failed to init");
56 vfeed.init(video->getFD());
57 afeed.init(audio->getFD());
68 int PlayerVideo::shutdown()
70 if (!initted) return 0;
73 Log::getInstance()->log("Player", Log::DEBUG, "Player shutdown...");
93 int PlayerVideo::play()
95 if (!initted) return 0;
97 // If we are just paused, unpause!
104 // If we are fast forwarding, set to normal
111 // If we are fast backwarding, set to normal
114 toggleFastBackward();
118 // If we are already playing, bail // FIXME - resync?
121 Log::getInstance()->log("Player", Log::DEBUG, "DOING RESYNC");
152 // Standard play start
159 // ------------------------ This one works, but doesn't allow any pre-buffering.
169 usleep(500000); // SYNC
174 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
181 // struct timespec delay;
183 // delay.tv_nsec = 500000000;
184 // nanosleep(&delay, NULL);
193 // ------------------------------------------------------------------------------------------------
199 void PlayerVideo::stop()
201 if (!initted) return;
202 if (!playing) return;
209 video->unFastForward();
210 audio->systemMuteOff();
211 feedMode = MODE_NORMAL;
230 void PlayerVideo::togglePause()
232 if (!initted) return;
233 if (!playing) return;
235 if (ffwd) toggleFastForward();
236 if (fbwd) toggleFastBackward();
252 void PlayerVideo::test()
254 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
260 static int flipflop = 0;
263 if (flipflop) a = video->setAspectRatio(Video::ASPECT16X9);
264 else a = video->setAspectRatio(Video::ASPECT4X3);
266 flipflop = !flipflop;
268 printf("A = %i\n", a);
272 void PlayerVideo::test2()
274 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
279 void PlayerVideo::setPosition(ULLONG position)
281 feedPosition = position;
284 void PlayerVideo::setLength(ULLONG length)
286 lastRescan = time(NULL);
287 streamLength = length;
288 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
291 void PlayerVideo::skipForward(int seconds)
293 // skip forward 1 minute
294 Log::getInstance()->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
296 if (paused) togglePause();
298 ULLONG moveBy = seconds * 500000;
306 audio->doMuting(); // ???
308 feedPosition += moveBy;
310 // printf("Audio test %i\n", audio->test());
321 usleep(500000); // SYNC
328 void PlayerVideo::skipBackward(int seconds)
330 // skip forward 1 minute
331 Log::getInstance()->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
333 if (paused) togglePause();
335 ULLONG moveBy = seconds * 500000;
344 audio->doMuting(); // ???
346 if (feedPosition > moveBy) feedPosition -= moveBy;
356 usleep(500000); // SYNC
363 void PlayerVideo::toggleFastForward()
365 if (!initted) return;
366 if (!playing) return;
368 if (paused) togglePause();
369 if (fbwd) toggleFastBackward();
374 // video->unFastForward();
395 audio->systemMuteOff();
398 usleep(500000); // SYNC
404 demuxer.flushAudio();
412 audio->systemMuteOff();
419 audio->systemMuteOn();
420 video->fastForward();
424 void PlayerVideo::toggleFastBackward()
426 if (!initted) return;
427 if (!playing) return;
429 if (paused) togglePause();
430 if (ffwd) toggleFastForward();
436 audio->systemMuteOff();
439 feedMode = MODE_NORMAL;
446 audio->systemMuteOn();
449 feedMode = MODE_BACKWARDS;
457 void PlayerVideo::jumpToPercent(int percent)
468 feedPosition = streamLength * percent / 100;
478 usleep(500000); // SYNC
485 void PlayerVideo::call()
487 threadSignalNoLock();
492 void PlayerVideo::threadMethod()
494 UCHAR buf[blockSize];
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 // a bit hackey. this needs to be split to live and rec players
519 if (streamLength && (feedPosition >= streamLength)) break;
521 if (streamLength && ((feedPosition + blockSize) > streamLength))
523 askFor = streamLength - feedPosition;
525 thisRead = vdr->getBlock(buf, feedPosition, askFor);
528 int a_stream = demuxer.scan(buf, thisRead);
529 demuxer.setAudioStream(a_stream);
530 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
534 if (feedMode == MODE_NORMAL)
536 feedPosition += thisRead;
538 else if (feedMode == MODE_BACKWARDS)
540 if (feedPosition >= 100000)
542 feedPosition -= 100000;
547 // got to the start of the recording.. revert to play mode? how?
548 feedPosition += thisRead;
554 while(writeLength < thisRead)
556 thisWrite = demuxer.put(buf + writeLength, thisRead - writeLength);
557 writeLength += thisWrite;
561 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
562 // demuxer is full and cant take anymore
563 threadWaitForSignal();
564 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
572 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
573 Message* m = new Message();
574 m->message = Message::STOP_PLAYBACK;
575 Log::getInstance()->log("Player", Log::DEBUG, "Posting message...");
576 commandMessageQueue->postMessage(m);
577 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");