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 int PlayerVideo::play()
109 if (!initted) return 0;
111 // If we are just paused, unpause!
118 // If we are fast forwarding, set to normal
125 // If we are fast backwarding, set to normal
128 toggleFastBackward();
132 // If we are already playing, bail // FIXME - resync?
135 logger->log("Player", Log::DEBUG, "DOING RESYNC");
166 // Standard play start
173 // ------------------------ This one works, but doesn't allow any pre-buffering.
183 usleep(500000); // SYNC
188 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
195 // struct timespec delay;
197 // delay.tv_nsec = 500000000;
198 // nanosleep(&delay, NULL);
207 // ------------------------------------------------------------------------------------------------
213 void PlayerVideo::stop()
215 if (!initted) return;
216 if (!playing) return;
223 video->unFastForward();
224 audio->systemMuteOff();
225 feedMode = MODE_NORMAL;
231 logger->log("PlayerVideo", Log::DEBUG, "Temp 1");
233 logger->log("PlayerVideo", Log::DEBUG, "Temp 2");
235 logger->log("PlayerVideo", Log::DEBUG, "Temp 3");
237 logger->log("PlayerVideo", Log::DEBUG, "Temp 4");
239 logger->log("PlayerVideo", Log::DEBUG, "Temp 5");
241 logger->log("PlayerVideo", Log::DEBUG, "Temp 6");
243 logger->log("PlayerVideo", Log::DEBUG, "Temp 7");
245 logger->log("PlayerVideo", Log::DEBUG, "Temp 8");
247 logger->log("PlayerVideo", Log::DEBUG, "Temp 9");
249 logger->log("PlayerVideo", Log::DEBUG, "Temp 10");
254 void PlayerVideo::togglePause()
256 if (!initted) return;
257 if (!playing) return;
259 if (ffwd) toggleFastForward();
260 if (fbwd) toggleFastBackward();
276 void PlayerVideo::setPosition(ULLONG position)
278 feedPosition = position;
281 void PlayerVideo::setLength(ULLONG length)
283 lastRescan = time(NULL);
284 streamLength = length;
285 logger->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
288 void PlayerVideo::skipForward(int seconds)
290 // skip forward 1 minute
291 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
293 if (paused) togglePause();
295 ULLONG moveBy = seconds * 500000;
303 audio->doMuting(); // ???
305 feedPosition += moveBy;
307 // printf("Audio test %i\n", audio->test());
318 usleep(500000); // SYNC
325 void PlayerVideo::skipBackward(int seconds)
327 // skip forward 1 minute
328 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
330 if (paused) togglePause();
332 ULLONG moveBy = seconds * 500000;
341 audio->doMuting(); // ???
343 if (feedPosition > moveBy) feedPosition -= moveBy;
353 usleep(500000); // SYNC
360 void PlayerVideo::toggleFastForward()
362 if (!initted) return;
363 if (!playing) return;
365 if (paused) togglePause();
366 if (fbwd) toggleFastBackward();
371 // video->unFastForward();
392 audio->systemMuteOff();
395 usleep(500000); // SYNC
401 demuxer.flushAudio();
409 audio->systemMuteOff();
416 audio->systemMuteOn();
417 video->fastForward();
421 void PlayerVideo::toggleFastBackward()
423 if (!initted) return;
424 if (!playing) return;
426 if (paused) togglePause();
427 if (ffwd) toggleFastForward();
433 audio->systemMuteOff();
436 feedMode = MODE_NORMAL;
443 audio->systemMuteOn();
446 feedMode = MODE_BACKWARDS;
454 void PlayerVideo::jumpToPercent(int percent)
465 feedPosition = streamLength * percent / 100;
475 usleep(500000); // SYNC
482 void PlayerVideo::call(void* caller)
484 if (caller == &demuxer)
486 logger->log("Player", Log::DEBUG, "Callback from demuxer");
488 if (video->getTVsize() == Video::ASPECT4X3)
490 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
494 int dxCurrentAspect = demuxer.getAspectRatio();
495 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
497 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
498 video->setAspectRatio(Video::ASPECT4X3);
500 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
502 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
503 video->setAspectRatio(Video::ASPECT16X9);
507 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
513 threadSignalNoLock();
519 void PlayerVideo::threadMethod()
525 VDR* vdr = VDR::getInstance();
536 // If we havn't rescanned for a while..
537 if (isRecording && ((lastRescan + 60) < time(NULL)))
539 streamLength = vdr->rescanRecording();
540 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
541 lastRescan = time(NULL);
544 if (streamLength) // is playing a recording
546 if (feedPosition >= streamLength) break; // finished playback
550 if (startupBlockSize > streamLength)
551 askFor = streamLength; // is a very small recording!
553 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
557 if ((feedPosition + blockSize) > streamLength) // last block of recording
558 askFor = streamLength - feedPosition;
563 else // is playing live
566 askFor = startupBlockSize; // find audio streams sized block
568 askFor = blockSize; // normal
571 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
572 if (!threadBuffer) break;
576 int a_stream = demuxer.scan(threadBuffer, thisRead);
577 demuxer.setAudioStream(a_stream);
578 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
582 if (feedMode == MODE_NORMAL)
584 feedPosition += thisRead;
586 else if (feedMode == MODE_BACKWARDS)
588 if (feedPosition >= blockSize)
590 feedPosition -= blockSize;
595 // got to the start of the recording.. revert to play mode? how?
596 feedPosition += thisRead;
602 while(writeLength < thisRead)
604 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
605 writeLength += thisWrite;
609 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
610 // demuxer is full and cant take anymore
611 threadWaitForSignal();
612 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
624 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
625 Message* m = new Message();
626 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
627 else m->message = Message::STREAM_END; // live
628 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
629 commandMessageQueue->postMessage(m);
630 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
633 void PlayerVideo::threadPostStopCleanup()
635 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
638 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");
645 void PlayerVideo::test1()
647 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 1");
650 void PlayerVideo::test2()
652 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 2");