From a9db20b2b374b424cca0fc75f06a835bfadeabeb Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 1 Jun 2008 21:04:40 +0000 Subject: [PATCH] Code cleaning --- player.cc | 247 ++++++++----------------------------------------- player.h | 11 +-- playerradio.cc | 189 +++++++------------------------------ playerradio.h | 7 +- vradiorec.cc | 2 +- vvideorec.cc | 2 +- 6 files changed, 78 insertions(+), 380 deletions(-) diff --git a/player.cc b/player.cc index 74ed286..db3e01a 100644 --- a/player.cc +++ b/player.cc @@ -1,5 +1,5 @@ /* - Copyright 2004-2005 Chris Tallon + Copyright 2004-2008 Chris Tallon This file is part of VOMP. @@ -34,7 +34,7 @@ // ----------------------------------- Called from outside, one offs or info funcs -Player::Player(MessageQueue* tmessageQueue, void* tmessageReceiver, bool tIsRecording) +Player::Player(MessageQueue* tmessageQueue, void* tmessageReceiver) : vfeed(this), afeed(this) { messageQueue = tmessageQueue; @@ -51,14 +51,10 @@ Player::Player(MessageQueue* tmessageQueue, void* tmessageReceiver, bool tIsReco ifactor = 4; videoStartup = false; - preBuffering = false; - isRecording = tIsRecording; - threadBuffer = NULL; blockSize = 100000; startupBlockSize = 250000; - preBufferSize = 500000; video->turnVideoOn(); } @@ -76,10 +72,7 @@ int Player::init() mutex=CreateMutex(NULL,FALSE,NULL); #endif - if (isRecording) - demuxer = new DemuxerVDR(); - else - demuxer = new DemuxerTS(); + demuxer = new DemuxerVDR(); if (!demuxer) return 0; if (!demuxer->init(this, audio, video, 2097152, 524288)) @@ -167,31 +160,16 @@ bool* Player::getDemuxerAc3AudioChannels() int Player::getCurrentAudioChannel() { - if (isRecording) return demuxer->getselAudioChannel(); - else - return ((DemuxerTS*)demuxer)->getAID(); } void Player::setAudioChannel(int newChannel, int type) { - if (isRecording) demuxer->setAudioChannel(newChannel); - else - ((DemuxerTS*)demuxer)->setAID(newChannel,type); } // ----------------------------------- Externally called events -void Player::play(ULONG Vpid, ULONG Apid) -{ - // TS mode - DemuxerTS* dts = (DemuxerTS*)demuxer; - dts->setVID(Vpid); - dts->setAID(Apid,0); - play(); -} - void Player::play() { if (!initted) return; @@ -692,33 +670,20 @@ void Player::switchState(UCHAR toState, ULONG jumpFrame) audio->systemMuteOff(); video->reset(); demuxer->reset(); - if (isRecording) - { - // FIXME use restartAtFrame here? - if (currentFrameNumber > lengthFrames) currentFrameNumber = 0; - demuxer->setFrameNum(currentFrameNumber); - } + // FIXME use restartAtFrame here? + if (currentFrameNumber > lengthFrames) currentFrameNumber = 0; + demuxer->setFrameNum(currentFrameNumber); demuxer->seek(); - videoStartup = true; state = S_PLAY; threadStart(); - - if (isRecording) - { - logger->log("Player", Log::DEBUG, "Immediate play"); - afeed.start(); - vfeed.start(); - video->sync(); - audio->sync(); - audio->play(); - video->pause(); - } - else // do prebuffering - { - logger->log("Player", Log::DEBUG, "Prebuffering..."); - preBuffering = true; - } + logger->log("Player", Log::DEBUG, "Immediate play"); + afeed.start(); + vfeed.start(); + video->sync(); + audio->sync(); + audio->play(); + video->pause(); return; } case S_PAUSE_P: // to S_PAUSE_P @@ -917,166 +882,40 @@ void Player::threadMethod() // is farm out to threadFeed Live/Play/Scan // All the guff is to support scan hitting one end - if (isRecording) - { - if ((state == S_FFWD) || (state == S_FBWD)) - { - threadFeedScan(); - // if this returns then scan hit one end - if (state == S_FFWD) // scan hit the end. stop - { - threadCheckExit(); - Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex - m->to = messageReceiver; - m->from = this; - m->message = Message::PLAYER_EVENT; - m->parameter = STOP_PLAYBACK; - logger->log("Player", Log::DEBUG, "Posting message to %p...", messageQueue); - messageQueue->postMessage(m); - logger->log("Player", Log::DEBUG, "Message posted..."); - return; - } - // if execution gets to here, threadFeedScan hit the start, go to play mode - state = S_PLAY; - audio->reset(); - audio->setStreamType(Audio::MPEG2_PES); - demuxer->flush(); - demuxer->seek(); - demuxer->setFrameNum(currentFrameNumber); - videoStartup = true; - afeed.start(); - vfeed.start(); - audio->play(); - audio->sync(); - audio->systemMuteOff(); - audio->doMuting(); - } - - if (state == S_PLAY) threadFeedPlay(); - } - else - { - threadFeedLive(); - } -} - -void Player::threadFeedLive() -{ - UINT thisRead; - UINT writeLength; - UINT thisWrite; - UINT preBufferTotal = 0; - - UINT askFor; - while(1) + if ((state == S_FFWD) || (state == S_FBWD)) { - thisRead = 0; - writeLength = 0; - thisWrite = 0; - - threadCheckExit(); - - if (startup) - askFor = startupBlockSize; // find audio streams sized block - else - askFor = blockSize; // normal - - threadBuffer = vdr->getBlock(0, askFor, &thisRead); - - if (!vdr->isConnected()) - { - doConnectionLost(); - return; - } - - if (!threadBuffer) break; - - if (startup) - { - int a_stream = demuxer->scan(threadBuffer, thisRead); - demuxer->setAudioStream(a_stream); - logger->log("Player", Log::DEBUG, "Startup Audio stream chosen %x", a_stream); - startup = false; - } - - if (preBuffering) - { - preBufferTotal += thisRead; - if (preBufferTotal >= preBufferSize) - { - logger->log("Player", Log::DEBUG, "Got >500K, prebuffering complete"); - - preBuffering = false; - preBufferTotal = 0; - - -// FIXME check the win32 code on the mvp -#ifndef WIN32 - audio->sync(); - audio->play(); - video->sync(); - video->pause(); - afeed.start(); - vfeed.start(); -#else - afeed.start(); - vfeed.start(); - audio->sync(); - audio->play(); - video->sync(); - video->pause(); -#endif - -// unLock(); // thread will be locked by play until here - // FIXME - see if this can segfault because it is starting threads out of the master mutex - } - } - - threadCheckExit(); - - while(writeLength < thisRead) + threadFeedScan(); + // if this returns then scan hit one end + if (state == S_FFWD) // scan hit the end. stop { - thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength); - writeLength += thisWrite; - -// logger->log("Player", Log::DEBUG, "Player put %u to demuxer", thisWrite); - - if (!thisWrite) - { -// logger->log("Player", Log::DEBUG, "Demuxer full"); - // demuxer is full and can't take anymore - threadLock(); - threadWaitForSignal(); - threadUnlock(); - } - threadCheckExit(); + Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex + m->to = messageReceiver; + m->from = this; + m->message = Message::PLAYER_EVENT; + m->parameter = STOP_PLAYBACK; + logger->log("Player", Log::DEBUG, "Posting message to %p...", messageQueue); + messageQueue->postMessage(m); + logger->log("Player", Log::DEBUG, "Message posted..."); + return; } - - free(threadBuffer); - threadBuffer = NULL; - - } - - logger->log("Player", Log::DEBUG, "Live play failed to start or interrupted"); - - if (videoStartup) // oh woe. there never was a stream, I was conned! - { - videoStartup = false; - unLock(); - MILLISLEEP(500); // I think this will solve a race + // if execution gets to here, threadFeedScan hit the start, go to play mode + state = S_PLAY; + audio->reset(); + audio->setStreamType(Audio::MPEG2_PES); + demuxer->flush(); + demuxer->seek(); + demuxer->setFrameNum(currentFrameNumber); + videoStartup = true; + afeed.start(); + vfeed.start(); + audio->play(); + audio->sync(); + audio->systemMuteOff(); + audio->doMuting(); } - threadCheckExit(); - - Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex - m->to = messageReceiver; - m->from = this; - m->message = Message::PLAYER_EVENT; - m->parameter = Player::STREAM_END; - logger->log("Player", Log::DEBUG, "Posting message to %p...", messageQueue); - messageQueue->postMessage(m); - logger->log("Player", Log::DEBUG, "Message posted..."); + if (state == S_PLAY) threadFeedPlay(); } void Player::threadFeedPlay() @@ -1311,14 +1150,10 @@ void Player::threadPostStopCleanup() void Player::test1() { logger->log("Player", Log::DEBUG, "PLAYER TEST 1"); -// video->play(); -// video->setAspectRatio(Video::ASPECT4X3); - jumpToFrameP(37507); } void Player::test2() { logger->log("Player", Log::DEBUG, "PLAYER TEST 2"); - video->setAspectRatio(Video::ASPECT16X9); } #endif diff --git a/player.h b/player.h index b2e8844..51af591 100644 --- a/player.h +++ b/player.h @@ -1,5 +1,5 @@ /* - Copyright 2004-2005 Chris Tallon + Copyright 2004-2008 Chris Tallon This file is part of VOMP. @@ -49,7 +49,7 @@ class Demuxer; class Player : public Thread_TYPE, public Callback { public: - Player(MessageQueue* messageQueue, void* messageReceiver, bool isRecording); + Player(MessageQueue* messageQueue, void* messageReceiver); virtual ~Player(); int init(); @@ -59,8 +59,7 @@ class Player : public Thread_TYPE, public Callback void setLengthFrames(ULONG length); void setAudioChannel(int newChannel, int type); - void play(); // Recording - void play(ULONG vpid, ULONG apid); // Live TV + void play(); void stop(); void pause(); void fastForward(); @@ -112,7 +111,6 @@ class Player : public Thread_TYPE, public Callback private: void switchState(UCHAR newState, ULONG jumpFrame=0); - void threadFeedLive(); void threadFeedPlay(); void threadFeedScan(); @@ -133,8 +131,6 @@ class Player : public Thread_TYPE, public Callback bool initted; bool startup; bool videoStartup; - bool isRecording; - bool preBuffering; #ifndef WIN32 pthread_mutex_t mutex; @@ -149,7 +145,6 @@ class Player : public Thread_TYPE, public Callback ULONG currentFrameNumber; UINT blockSize; UINT startupBlockSize; - UINT preBufferSize; UCHAR* threadBuffer; UCHAR state; UCHAR ifactor; diff --git a/playerradio.cc b/playerradio.cc index 0dd2161..a16ff23 100644 --- a/playerradio.cc +++ b/playerradio.cc @@ -32,7 +32,7 @@ // ----------------------------------- Called from outside, one offs or info funcs -PlayerRadio::PlayerRadio(MessageQueue* tmessageQueue, void* tmessageReceiver, bool tIsRecording) +PlayerRadio::PlayerRadio(MessageQueue* tmessageQueue, void* tmessageReceiver) : afeed(this) { messageQueue = tmessageQueue; @@ -49,13 +49,10 @@ PlayerRadio::PlayerRadio(MessageQueue* tmessageQueue, void* tmessageReceiver, bo startPTS = 0; lengthSeconds = 0; - isRecording = tIsRecording; - threadBuffer = NULL; blockSize = 10000; startupBlockSize = 20000; - preBufferSize = 40000; Video::getInstance()->turnVideoOff(); } @@ -74,10 +71,7 @@ int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthPackets) mutex=CreateMutex(NULL,FALSE,NULL); #endif - if (isRecording) - demuxer = new DemuxerVDR(); - else - demuxer = new DemuxerTS(); + demuxer = new DemuxerVDR(); if (!demuxer) return 0; if (!demuxer->init(this, audio, NULL, 0, 40000)) @@ -98,34 +92,31 @@ int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthPackets) UINT thisRead = 0; int success; - if (isRecording) + UCHAR* buffer = vdr->getBlock(0, 10000, &thisRead); + if (!buffer) { - UCHAR* buffer = vdr->getBlock(0, 10000, &thisRead); - if (!buffer) - { - logger->log("PlayerRadio", Log::ERR, "Failed to get start block"); - shutdown(); - if (!vdr->isConnected()) doConnectionLost(); - return 0; - } - - success = demuxer->findPTS(buffer, thisRead, &startPTS); - if (!success) - { - logger->log("PlayerRadio", Log::ERR, "Failed to get start PTS"); - free(buffer); - shutdown(); - return 0; - } + logger->log("PlayerRadio", Log::ERR, "Failed to get start block"); + shutdown(); + if (!vdr->isConnected()) doConnectionLost(); + return 0; + } + success = demuxer->findPTS(buffer, thisRead, &startPTS); + if (!success) + { + logger->log("PlayerRadio", Log::ERR, "Failed to get start PTS"); free(buffer); + shutdown(); + return 0; + } - if (!setLengthSeconds()) - { - logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds"); - shutdown(); - return 0; - } + free(buffer); + + if (!setLengthSeconds()) + { + logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds"); + shutdown(); + return 0; } initted = true; @@ -205,14 +196,6 @@ ULONG PlayerRadio::getCurrentSeconds() // ----------------------------------- Externally called events -void PlayerRadio::play(ULONG Apid) -{ - // TS mode - DemuxerTS* dts = (DemuxerTS*)demuxer; - dts->setAID(Apid,0/*Radio is always mpeg?*/); - play(); -} - void PlayerRadio::play() { if (!initted) return; @@ -382,27 +365,16 @@ void PlayerRadio::switchState(UCHAR toState, ULONG jumpPacket) audio->setStreamType(Audio::MPEG2_PES); audio->systemMuteOff(); demuxer->reset(); - if (isRecording) - { - // FIXME use restartAtPacket here? - if (currentPacketNumber > lengthPackets) currentPacketNumber = 0; - demuxer->setPacketNum(currentPacketNumber); - } + // FIXME use restartAtPacket here? + if (currentPacketNumber > lengthPackets) currentPacketNumber = 0; + demuxer->setPacketNum(currentPacketNumber); state = S_PLAY; threadStart(); + logger->log("PlayerRadio", Log::DEBUG, "Immediate play"); + afeed.start(); + audio->play(); - if (isRecording) - { - logger->log("PlayerRadio", Log::DEBUG, "Immediate play"); - afeed.start(); - audio->play(); - } - else // do prebuffering - { - logger->log("PlayerRadio", Log::DEBUG, "Prebuffering..."); - preBuffering = true; - } return; } case S_PAUSE_P: // to S_PAUSE_P @@ -484,107 +456,7 @@ void PlayerRadio::call(void* caller) void PlayerRadio::threadMethod() { - if (isRecording) - { - if (state == S_PLAY) threadFeedPlay(); - } - else - { - threadFeedLive(); - } -} - -void PlayerRadio::threadFeedLive() -{ - UINT thisRead; - UINT writeLength; - UINT thisWrite; - UINT preBufferTotal = 0; - - UINT askFor; - while(1) - { - thisRead = 0; - writeLength = 0; - thisWrite = 0; - - threadCheckExit(); - - if (startup) - askFor = startupBlockSize; // find audio streams sized block - else - askFor = blockSize; // normal - - threadBuffer = vdr->getBlock(0, askFor, &thisRead); - - if (!vdr->isConnected()) - { - doConnectionLost(); - return; - } - - if (!threadBuffer) break; - - if (startup) - { - int a_stream = demuxer->scan(threadBuffer, thisRead); - demuxer->setAudioStream(a_stream); - logger->log("PlayerRadio", Log::DEBUG, "Startup Audio stream chosen %x", a_stream); - startup = false; - } - - if (preBuffering) - { - preBufferTotal += thisRead; - if (preBufferTotal >= preBufferSize) - { - logger->log("PlayerRadio", Log::DEBUG, "Got >500K, prebuffering complete"); - - preBuffering = false; - preBufferTotal = 0; - - audio->play(); - afeed.start(); -// unLock(); // thread will be locked by play until here - // FIXME - see if this can segfault because it is starting threads out of the master mutex - } - } - - threadCheckExit(); - - while(writeLength < thisRead) - { - thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength); - writeLength += thisWrite; - - if (!thisWrite) - { - // demuxer is full and can't take anymore - threadLock(); - threadWaitForSignal(); - threadUnlock(); - } - - threadCheckExit(); - } - - free(threadBuffer); - threadBuffer = NULL; - - } - - logger->log("PlayerRadio", Log::DEBUG, "Live play failed to start or interrupted"); - - threadCheckExit(); - - Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex - m->to = messageReceiver; - m->from = this; - m->message = Message::PLAYER_EVENT; - m->parameter = PlayerRadio::STREAM_END; - logger->log("PlayerRadio", Log::DEBUG, "Posting message to %p...", messageQueue); - messageQueue->postMessage(m); - logger->log("PlayerRadio", Log::DEBUG, "Message posted..."); + if (state == S_PLAY) threadFeedPlay(); } void PlayerRadio::threadFeedPlay() @@ -703,3 +575,4 @@ void PlayerRadio::threadPostStopCleanup() threadBuffer = NULL; } } + diff --git a/playerradio.h b/playerradio.h index e7c7a98..3460a8f 100644 --- a/playerradio.h +++ b/playerradio.h @@ -47,7 +47,7 @@ class MessageQueue; class PlayerRadio : public Thread_TYPE, public Callback { public: - PlayerRadio(MessageQueue* messageQueue, void* messageReceiver, bool isRecording); + PlayerRadio(MessageQueue* messageQueue, void* messageReceiver); virtual ~PlayerRadio(); int init(ULLONG lengthBytes, ULONG lengthPackets); @@ -55,7 +55,6 @@ class PlayerRadio : public Thread_TYPE, public Callback void setStartBytes(ULLONG startBytes); void play(); - void play(ULONG apid); // Live radio void stop(); void pause(); void jumpToPercent(double percent); @@ -86,7 +85,6 @@ class PlayerRadio : public Thread_TYPE, public Callback private: void switchState(UCHAR newState, ULONG jumpPacket=0); - void threadFeedLive(); void threadFeedPlay(); void threadFeedScan(); @@ -104,8 +102,6 @@ class PlayerRadio : public Thread_TYPE, public Callback bool initted; bool startup; - bool isRecording; - bool preBuffering; ULLONG startPTS; ULONG lengthSeconds; @@ -124,7 +120,6 @@ class PlayerRadio : public Thread_TYPE, public Callback ULONG currentPacketNumber; UINT blockSize; UINT startupBlockSize; - UINT preBufferSize; UCHAR* threadBuffer; UCHAR state; }; diff --git a/vradiorec.cc b/vradiorec.cc index f0d53cf..5c8737f 100644 --- a/vradiorec.cc +++ b/vradiorec.cc @@ -46,7 +46,7 @@ VRadioRec::VRadioRec(Recording* rec) startMargin = 0; endMargin = 0; - player = new PlayerRadio(Command::getInstance(), this, true); + player = new PlayerRadio(Command::getInstance(), this); char* cstartMargin = vdr->configLoad("Timers", "Start margin"); char* cendMargin = vdr->configLoad("Timers", "End margin"); diff --git a/vvideorec.cc b/vvideorec.cc index f2ee7c0..0475d98 100644 --- a/vvideorec.cc +++ b/vvideorec.cc @@ -45,7 +45,7 @@ VVideoRec::VVideoRec(Recording* rec) vas = NULL; vsummary = NULL; - player = new Player(Command::getInstance(), this, true); + player = new Player(Command::getInstance(), this); player->init(); videoMode = video->getMode(); -- 2.39.5