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 void PlayerVideo::resyncAudio()
111 if (!initted) return;
114 usleep(500000); // SYNC
118 int PlayerVideo::play()
120 if (!initted) return 0;
122 // If we are just paused, unpause!
129 // If we are fast forwarding, set to normal
136 // If we are fast backwarding, set to normal
139 toggleFastBackward();
143 // If we are already playing, bail // FIXME - resync?
146 logger->log("Player", Log::DEBUG, "DOING RESYNC");
177 // Standard play start
184 // ------------------------ This one works, but doesn't allow any pre-buffering.
194 usleep(500000); // SYNC
199 // ------------------------ This one doesn't work, but it should, and would allow for prebuffering.
206 // struct timespec delay;
208 // delay.tv_nsec = 500000000;
209 // nanosleep(&delay, NULL);
218 // ------------------------------------------------------------------------------------------------
224 void PlayerVideo::stop()
226 if (!initted) return;
227 if (!playing) return;
234 video->unFastForward();
235 audio->systemMuteOff();
236 feedMode = MODE_NORMAL;
242 logger->log("PlayerVideo", Log::DEBUG, "Temp 1");
244 logger->log("PlayerVideo", Log::DEBUG, "Temp 2");
246 logger->log("PlayerVideo", Log::DEBUG, "Temp 3");
248 logger->log("PlayerVideo", Log::DEBUG, "Temp 4");
250 logger->log("PlayerVideo", Log::DEBUG, "Temp 5");
252 logger->log("PlayerVideo", Log::DEBUG, "Temp 6");
254 logger->log("PlayerVideo", Log::DEBUG, "Temp 7");
256 logger->log("PlayerVideo", Log::DEBUG, "Temp 8");
258 logger->log("PlayerVideo", Log::DEBUG, "Temp 9");
260 logger->log("PlayerVideo", Log::DEBUG, "Temp 10");
265 void PlayerVideo::togglePause()
267 if (!initted) return;
268 if (!playing) return;
270 if (ffwd) toggleFastForward();
271 if (fbwd) toggleFastBackward();
287 void PlayerVideo::setPosition(ULLONG position)
289 feedPosition = position;
292 void PlayerVideo::setLength(ULLONG length)
294 lastRescan = time(NULL);
295 streamLength = length;
296 logger->log("PlayerVideo", Log::DEBUG, "Player has received length of %llu", streamLength);
299 void PlayerVideo::skipForward(int seconds)
301 // skip forward 1 minute
302 logger->log("Player", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
304 if (paused) togglePause();
306 ULLONG moveBy = seconds * 500000;
314 audio->doMuting(); // ???
316 feedPosition += moveBy;
318 // printf("Audio test %i\n", audio->test());
329 usleep(500000); // SYNC
336 void PlayerVideo::skipBackward(int seconds)
338 // skip forward 1 minute
339 logger->log("Player", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
341 if (paused) togglePause();
343 ULLONG moveBy = seconds * 500000;
352 audio->doMuting(); // ???
354 if (feedPosition > moveBy) feedPosition -= moveBy;
364 usleep(500000); // SYNC
371 void PlayerVideo::toggleFastForward()
373 if (!initted) return;
374 if (!playing) return;
376 if (paused) togglePause();
377 if (fbwd) toggleFastBackward();
382 // video->unFastForward();
403 audio->systemMuteOff();
406 usleep(500000); // SYNC
412 demuxer.flushAudio();
420 audio->systemMuteOff();
427 audio->systemMuteOn();
428 video->fastForward();
432 void PlayerVideo::toggleFastBackward()
434 if (!initted) return;
435 if (!playing) return;
437 if (paused) togglePause();
438 if (ffwd) toggleFastForward();
444 audio->systemMuteOff();
447 feedMode = MODE_NORMAL;
454 audio->systemMuteOn();
457 feedMode = MODE_BACKWARDS;
465 void PlayerVideo::jumpToPercent(int percent)
476 feedPosition = streamLength * percent / 100;
486 usleep(500000); // SYNC
493 void PlayerVideo::call(void* caller)
495 if (caller == &demuxer)
497 logger->log("Player", Log::DEBUG, "Callback from demuxer");
499 if (video->getTVsize() == Video::ASPECT4X3)
501 logger->log("Player", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
505 int dxCurrentAspect = demuxer.getAspectRatio();
506 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
508 logger->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
509 video->setAspectRatio(Video::ASPECT4X3);
511 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
513 logger->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
514 video->setAspectRatio(Video::ASPECT16X9);
518 logger->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
524 threadSignalNoLock();
530 void PlayerVideo::threadMethod()
536 VDR* vdr = VDR::getInstance();
547 // If we havn't rescanned for a while..
548 if (isRecording && ((lastRescan + 60) < time(NULL)))
550 streamLength = vdr->rescanRecording();
551 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
552 lastRescan = time(NULL);
555 if (streamLength) // is playing a recording
557 if (feedPosition >= streamLength) break; // finished playback
561 if (startupBlockSize > streamLength)
562 askFor = streamLength; // is a very small recording!
564 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
568 if ((feedPosition + blockSize) > streamLength) // last block of recording
569 askFor = streamLength - feedPosition;
574 else // is playing live
577 askFor = startupBlockSize; // find audio streams sized block
579 askFor = blockSize; // normal
582 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
583 if (!threadBuffer) break;
587 int a_stream = demuxer.scan(threadBuffer, thisRead);
588 demuxer.setAudioStream(a_stream);
589 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
593 if (feedMode == MODE_NORMAL)
595 feedPosition += thisRead;
597 else if (feedMode == MODE_BACKWARDS)
599 if (feedPosition >= blockSize)
601 feedPosition -= blockSize;
606 // got to the start of the recording.. revert to play mode? how?
607 feedPosition += thisRead;
613 while(writeLength < thisRead)
615 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
616 writeLength += thisWrite;
620 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
621 // demuxer is full and cant take anymore
623 threadWaitForSignal();
625 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
637 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
638 Message* m = new Message();
639 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
640 else m->message = Message::STREAM_END; // live
641 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
642 commandMessageQueue->postMessage(m);
643 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
646 void PlayerVideo::threadPostStopCleanup()
648 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
651 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");
658 void PlayerVideo::test1()
660 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 1");
663 void PlayerVideo::test2()
665 Log::getInstance()->log("Player", Log::DEBUG, "PLAYER TEST 2");