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();
254 void PlayerVideo::test()
256 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
264 static int flipflop = 0;
267 if (flipflop) a = video->setAspectRatio(Video::ASPECT16X9);
268 else a = video->setAspectRatio(Video::ASPECT4X3);
270 flipflop = !flipflop;
272 printf("A = %i\n", a);
276 void PlayerVideo::test2()
278 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST");
283 void PlayerVideo::setPosition(ULLONG position)
285 feedPosition = position;
288 void PlayerVideo::setLength(ULLONG length)
290 lastRescan = time(NULL);
291 streamLength = length;
292 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
295 void PlayerVideo::skipForward(int seconds)
297 // skip forward 1 minute
298 Log::getInstance()->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
300 if (paused) togglePause();
302 ULLONG moveBy = seconds * 500000;
310 audio->doMuting(); // ???
312 feedPosition += moveBy;
314 // printf("Audio test %i\n", audio->test());
325 usleep(500000); // SYNC
332 void PlayerVideo::skipBackward(int seconds)
334 // skip forward 1 minute
335 Log::getInstance()->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
337 if (paused) togglePause();
339 ULLONG moveBy = seconds * 500000;
348 audio->doMuting(); // ???
350 if (feedPosition > moveBy) feedPosition -= moveBy;
360 usleep(500000); // SYNC
367 void PlayerVideo::toggleFastForward()
369 if (!initted) return;
370 if (!playing) return;
372 if (paused) togglePause();
373 if (fbwd) toggleFastBackward();
378 // video->unFastForward();
399 audio->systemMuteOff();
402 usleep(500000); // SYNC
408 demuxer.flushAudio();
416 audio->systemMuteOff();
423 audio->systemMuteOn();
424 video->fastForward();
428 void PlayerVideo::toggleFastBackward()
430 if (!initted) return;
431 if (!playing) return;
433 if (paused) togglePause();
434 if (ffwd) toggleFastForward();
440 audio->systemMuteOff();
443 feedMode = MODE_NORMAL;
450 audio->systemMuteOn();
453 feedMode = MODE_BACKWARDS;
461 void PlayerVideo::jumpToPercent(int percent)
472 feedPosition = streamLength * percent / 100;
482 usleep(500000); // SYNC
489 void PlayerVideo::call()
491 threadSignalNoLock();
496 void PlayerVideo::threadMethod()
502 VDR* vdr = VDR::getInstance();
513 // If we havn't rescanned for a while..
514 if (isRecording && ((lastRescan + 60) < time(NULL)))
516 streamLength = vdr->rescanRecording();
517 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
518 lastRescan = time(NULL);
521 if (streamLength) // is playing a recording
523 if (feedPosition >= streamLength) break; // finished playback
527 if (startupBlockSize > streamLength)
528 askFor = streamLength; // is a very small recording!
530 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
534 if ((feedPosition + blockSize) > streamLength) // last block of recording
535 askFor = streamLength - feedPosition;
540 else // is playing live
543 askFor = startupBlockSize; // find audio streams sized block
545 askFor = blockSize; // normal
548 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
549 if (!threadBuffer) break;
553 int a_stream = demuxer.scan(threadBuffer, thisRead);
554 demuxer.setAudioStream(a_stream);
555 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
559 if (feedMode == MODE_NORMAL)
561 feedPosition += thisRead;
563 else if (feedMode == MODE_BACKWARDS)
565 if (feedPosition >= blockSize)
567 feedPosition -= blockSize;
572 // got to the start of the recording.. revert to play mode? how?
573 feedPosition += thisRead;
579 while(writeLength < thisRead)
581 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
582 writeLength += thisWrite;
586 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
587 // demuxer is full and cant take anymore
588 threadWaitForSignal();
589 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
601 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
602 Message* m = new Message();
603 m->message = Message::STOP_PLAYBACK;
604 Log::getInstance()->log("Player", Log::DEBUG, "Posting message...");
605 commandMessageQueue->postMessage(m);
606 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
609 void PlayerVideo::threadPostStopCleanup()
611 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
614 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");