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();
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...");
90 Log::getInstance()->log("Player", Log::DEBUG, "Player shutdown done");
95 int PlayerVideo::play()
97 if (!initted) return 0;
99 // If we are just paused, unpause!
106 // If we are fast forwarding, set to normal
113 // If we are fast backwarding, set to normal
116 toggleFastBackward();
120 // If we are already playing, bail // FIXME - resync?
123 Log::getInstance()->log("Player", Log::DEBUG, "DOING RESYNC");
154 // Standard play start
161 // ------------------------ This one works, but doesn't allow any pre-buffering.
171 usleep(500000); // SYNC
176 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
183 // struct timespec delay;
185 // delay.tv_nsec = 500000000;
186 // nanosleep(&delay, NULL);
195 // ------------------------------------------------------------------------------------------------
201 void PlayerVideo::stop()
203 if (!initted) return;
204 if (!playing) return;
211 video->unFastForward();
212 audio->systemMuteOff();
213 feedMode = MODE_NORMAL;
232 void PlayerVideo::togglePause()
234 if (!initted) return;
235 if (!playing) return;
237 if (ffwd) toggleFastForward();
238 if (fbwd) toggleFastBackward();
255 void PlayerVideo::test()
257 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
265 static int flipflop = 0;
268 if (flipflop) a = video->setAspectRatio(Video::ASPECT16X9);
269 else a = video->setAspectRatio(Video::ASPECT4X3);
271 flipflop = !flipflop;
273 printf("A = %i\n", a);
277 void PlayerVideo::test2()
279 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
285 void PlayerVideo::setPosition(ULLONG position)
287 feedPosition = position;
290 void PlayerVideo::setLength(ULLONG length)
292 lastRescan = time(NULL);
293 streamLength = length;
294 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
297 void PlayerVideo::skipForward(int seconds)
299 // skip forward 1 minute
300 Log::getInstance()->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
302 if (paused) togglePause();
304 ULLONG moveBy = seconds * 500000;
312 audio->doMuting(); // ???
314 feedPosition += moveBy;
316 // printf("Audio test %i\n", audio->test());
327 usleep(500000); // SYNC
334 void PlayerVideo::skipBackward(int seconds)
336 // skip forward 1 minute
337 Log::getInstance()->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
339 if (paused) togglePause();
341 ULLONG moveBy = seconds * 500000;
350 audio->doMuting(); // ???
352 if (feedPosition > moveBy) feedPosition -= moveBy;
362 usleep(500000); // SYNC
369 void PlayerVideo::toggleFastForward()
371 if (!initted) return;
372 if (!playing) return;
374 if (paused) togglePause();
375 if (fbwd) toggleFastBackward();
380 // video->unFastForward();
401 audio->systemMuteOff();
404 usleep(500000); // SYNC
410 demuxer.flushAudio();
418 audio->systemMuteOff();
425 audio->systemMuteOn();
426 video->fastForward();
430 void PlayerVideo::toggleFastBackward()
432 if (!initted) return;
433 if (!playing) return;
435 if (paused) togglePause();
436 if (ffwd) toggleFastForward();
442 audio->systemMuteOff();
445 feedMode = MODE_NORMAL;
452 audio->systemMuteOn();
455 feedMode = MODE_BACKWARDS;
463 void PlayerVideo::jumpToPercent(int percent)
474 feedPosition = streamLength * percent / 100;
484 usleep(500000); // SYNC
491 void PlayerVideo::call()
493 threadSignalNoLock();
498 void PlayerVideo::threadMethod()
504 VDR* vdr = VDR::getInstance();
515 // If we havn't rescanned for a while..
516 if (isRecording && ((lastRescan + 60) < time(NULL)))
518 streamLength = vdr->rescanRecording();
519 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
520 lastRescan = time(NULL);
523 if (streamLength) // is playing a recording
525 if (feedPosition >= streamLength) break; // finished playback
529 if (startupBlockSize > streamLength)
530 askFor = streamLength; // is a very small recording!
532 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
536 if ((feedPosition + blockSize) > streamLength) // last block of recording
537 askFor = streamLength - feedPosition;
542 else // is playing live
545 askFor = startupBlockSize; // find audio streams sized block
547 askFor = blockSize; // normal
550 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
551 if (!threadBuffer) break;
555 int a_stream = demuxer.scan(threadBuffer, thisRead);
556 demuxer.setAudioStream(a_stream);
557 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
561 if (feedMode == MODE_NORMAL)
563 feedPosition += thisRead;
565 else if (feedMode == MODE_BACKWARDS)
567 if (feedPosition >= blockSize)
569 feedPosition -= blockSize;
574 // got to the start of the recording.. revert to play mode? how?
575 feedPosition += thisRead;
581 while(writeLength < thisRead)
583 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
584 writeLength += thisWrite;
588 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
589 // demuxer is full and cant take anymore
590 threadWaitForSignal();
591 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
603 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
604 Message* m = new Message();
605 m->message = Message::STOP_PLAYBACK;
606 Log::getInstance()->log("Player", Log::DEBUG, "Posting message...");
607 commandMessageQueue->postMessage(m);
608 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
611 void PlayerVideo::threadPostStopCleanup()
613 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
616 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");