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();
50 if (!demuxer.init(this))
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(void* caller)
493 if (caller == &demuxer)
495 Log* temp = Log::getInstance();
496 temp->log("Player", Log::DEBUG, "Callback from demuxer");
497 if (video->getAspectRatio() == Video::ASPECT16X9)
499 temp->log("Player", Log::DEBUG, "TV is 16:9, so will try to do a switch");
501 int dxCurrentAspect = demuxer.getAspectRatio();
502 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
504 temp->log("Player", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
505 video->setMode(Video::LETTERBOX); // swap these lines over
507 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
509 temp->log("Player", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
510 video->setMode(Video::NORMAL); // if these are the wrong way around
514 temp->log("Player", Log::DEBUG, "Demuxer said video is something else... ignoring");
520 threadSignalNoLock();
526 void PlayerVideo::threadMethod()
532 VDR* vdr = VDR::getInstance();
543 // If we havn't rescanned for a while..
544 if (isRecording && ((lastRescan + 60) < time(NULL)))
546 streamLength = vdr->rescanRecording();
547 Log::getInstance()->log("PlayerVideo", Log::DEBUG, "Rescanned and reset length: %llu", streamLength);
548 lastRescan = time(NULL);
551 if (streamLength) // is playing a recording
553 if (feedPosition >= streamLength) break; // finished playback
557 if (startupBlockSize > streamLength)
558 askFor = streamLength; // is a very small recording!
560 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
564 if ((feedPosition + blockSize) > streamLength) // last block of recording
565 askFor = streamLength - feedPosition;
570 else // is playing live
573 askFor = startupBlockSize; // find audio streams sized block
575 askFor = blockSize; // normal
578 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
579 if (!threadBuffer) break;
583 int a_stream = demuxer.scan(threadBuffer, thisRead);
584 demuxer.setAudioStream(a_stream);
585 Log::getInstance()->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
589 if (feedMode == MODE_NORMAL)
591 feedPosition += thisRead;
593 else if (feedMode == MODE_BACKWARDS)
595 if (feedPosition >= blockSize)
597 feedPosition -= blockSize;
602 // got to the start of the recording.. revert to play mode? how?
603 feedPosition += thisRead;
609 while(writeLength < thisRead)
611 thisWrite = demuxer.put(threadBuffer + writeLength, thisRead - writeLength);
612 writeLength += thisWrite;
616 // Log::getInstance()->log("Player", Log::DEBUG, "DEMUXER FULL!!!");
617 // demuxer is full and cant take anymore
618 threadWaitForSignal();
619 // Log::getInstance()->log("Player", Log::DEBUG, "BACK FROM WAIT");
631 Log::getInstance()->log("Player", Log::DEBUG, "Recording playback ends");
632 Message* m = new Message();
633 if (streamLength) m->message = Message::STOP_PLAYBACK; // recording
634 else m->message = Message::STREAM_END; // live
635 Log::getInstance()->log("Player", Log::DEBUG, "Posting message to %p...", commandMessageQueue);
636 commandMessageQueue->postMessage(m);
637 Log::getInstance()->log("Player", Log::DEBUG, "Message posted...");
640 void PlayerVideo::threadPostStopCleanup()
642 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 1");
645 Log::getInstance()->log("Player", Log::DEBUG, "Post stop cleanup 2");