2 Copyright 2004-2006 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 "playerradio.h"
23 // ----------------------------------- Called from outside, one offs or info funcs
25 PlayerRadio::PlayerRadio(MessageQueue* tmessageQueue, void* tmessageReceiver, bool tIsRecording)
28 messageQueue = tmessageQueue;
29 messageReceiver = tmessageReceiver;
30 audio = Audio::getInstance();
31 logger = Log::getInstance();
32 vdr = VDR::getInstance();
42 isRecording = tIsRecording;
47 startupBlockSize = 20000;
48 preBufferSize = 20000;
50 Video::getInstance()->turnVideoOff();
53 PlayerRadio::~PlayerRadio()
55 if (initted) shutdown();
58 int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthPackets)
60 if (initted) return 0;
62 pthread_mutex_init(&mutex, NULL);
64 mutex=CreateMutex(NULL,FALSE,NULL);
68 demuxer = new DemuxerVDR();
70 demuxer = new DemuxerTS();
71 if (!demuxer) return 0;
73 if (!demuxer->init(this, audio, NULL, 0, 40000))
75 logger->log("PlayerRadio", Log::ERR, "Demuxer failed to init");
83 lengthBytes = tlengthBytes;
84 lengthPackets = tlengthPackets;
86 logger->log("PlayerRadio", Log::DEBUG, "PlayerRadio has received length bytes of %llu", lengthBytes);
93 UCHAR* buffer = vdr->getBlock(0, 10000, &thisRead);
96 logger->log("PlayerRadio", Log::ERR, "Failed to get start block");
101 success = demuxer->findPTS(buffer, thisRead, &startPTS);
104 logger->log("PlayerRadio", Log::ERR, "Failed to get start PTS");
112 if (!setLengthSeconds())
114 logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds");
124 bool PlayerRadio::setLengthSeconds()
129 UCHAR* buffer = vdr->getBlock(lengthBytes - 10000, 10000, &thisRead);
132 logger->log("PlayerRadio", Log::ERR, "Failed to get end block");
136 success = demuxer->findPTS(buffer, thisRead, &endPTS);
139 logger->log("PlayerRadio", Log::ERR, "Failed to get end PTS");
144 if (startPTS < endPTS)
146 lengthSeconds = (endPTS - startPTS) / 90000;
150 lengthSeconds = (startPTS - endPTS) / 90000;
156 int PlayerRadio::shutdown()
158 if (!initted) return 0;
173 void PlayerRadio::setStartBytes(ULLONG startBytes)
175 streamPos = startBytes;
178 ULONG PlayerRadio::getLengthSeconds()
180 return lengthSeconds;
183 ULONG PlayerRadio::getCurrentSeconds()
185 if (startup) return 0;
187 long long currentPTS = demuxer->getAudioPTS();
188 currentPTS -= startPTS;
189 if (currentPTS < 0) currentPTS += 8589934592ULL;
190 ULONG ret = currentPTS / 90000;
194 // ----------------------------------- Externally called events
196 void PlayerRadio::play(ULONG Apid)
199 DemuxerTS* dts = (DemuxerTS*)demuxer;
204 void PlayerRadio::play()
206 if (!initted) return;
207 if (state == S_PLAY) return;
213 void PlayerRadio::stop()
215 if (!initted) return;
216 if (state == S_STOP) return;
218 logger->log("PlayerRadio", Log::DEBUG, "Stop called lock");
223 void PlayerRadio::pause()
225 if (!initted) return;
228 if (state == S_PAUSE_P)
234 switchState(S_PAUSE_P);
240 void PlayerRadio::jumpToPercent(int percent)
243 logger->log("PlayerRadio", Log::DEBUG, "JUMP TO %i%%", percent);
244 ULONG newPacket = percent * lengthPackets / 100;
245 switchState(S_JUMP, newPacket);
249 void PlayerRadio::skipForward(int seconds)
252 logger->log("PlayerRadio", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
253 ULONG currentSeconds = getCurrentSeconds();
254 ULONG currentPacket = demuxer->getPacketNum();
256 if (currentSeconds == 0) { unLock(); return; } // div by zero
257 if (currentPacket == 0) { unLock(); return; } // Current pos from demuxer is not valid
259 ULONG newPacket = demuxer->getPacketNum() / currentSeconds * (currentSeconds + seconds);
260 if (newPacket > lengthPackets) { switchState(S_PLAY); unLock(); }
261 else switchState(S_JUMP, newPacket);
265 void PlayerRadio::skipBackward(int seconds) // FIXME why are these signed?!
268 logger->log("PlayerRadio", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
270 ULONG currentSeconds = getCurrentSeconds();
271 ULONG currentPacket = demuxer->getPacketNum();
273 if (currentSeconds == 0) { unLock(); return; } // div by zero
274 if (currentPacket == 0) { unLock(); return; } // Current pos from demuxer is not valid
277 if ((UINT)seconds > currentSeconds)
280 newPacket = demuxer->getPacketNum() / currentSeconds * (currentSeconds - seconds);
282 switchState(S_JUMP, newPacket);
286 // ----------------------------------- Implementations called events
288 void PlayerRadio::switchState(UCHAR toState, ULONG jumpPacket)
290 if (!initted) return;
292 logger->log("PlayerRadio", Log::DEBUG, "Switch state from %u to %u", state, toState);
294 switch(state) // current state selector
296 case S_PLAY: // from S_PLAY -----------------------------------
300 case S_PLAY: // to S_PLAY
304 case S_PAUSE_P: // to S_PAUSE_P
310 case S_STOP: // to S_STOP
320 case S_JUMP: // to S_JUMP
322 restartAtPacket(jumpPacket);
327 case S_PAUSE_P: // from S_PAUSE_P -----------------------------------
331 case S_PLAY: // to S_PLAY
337 case S_PAUSE_P: // to S_PAUSE_P
341 case S_STOP: // to S_STOP
348 audio->systemMuteOff();
352 case S_JUMP: // to S_JUMP
356 restartAtPacket(jumpPacket);
361 case S_STOP: // from S_STOP -----------------------------------
365 case S_PLAY: // to S_PLAY
370 audio->systemMuteOff();
374 // FIXME use restartAtPacket here?
375 if (currentPacketNumber > lengthPackets) currentPacketNumber = 0;
376 demuxer->setPacketNum(currentPacketNumber);
384 logger->log("PlayerRadio", Log::DEBUG, "Immediate play");
388 else // do prebuffering
390 logger->log("PlayerRadio", Log::DEBUG, "Prebuffering...");
395 case S_PAUSE_P: // to S_PAUSE_P
399 case S_STOP: // to S_STOP
403 case S_JUMP: // to S_JUMP
409 // case S_JUMP cannot be selected as a start state because it auto flips to play
413 // ----------------------------------- Internal functions
415 void PlayerRadio::lock()
418 pthread_mutex_lock(&mutex);
419 logger->log("PlayerRadio", Log::DEBUG, "LOCKED");
422 WaitForSingleObject(mutex, INFINITE);
426 void PlayerRadio::unLock()
429 logger->log("PlayerRadio", Log::DEBUG, "UNLOCKING");
430 pthread_mutex_unlock(&mutex);
436 void PlayerRadio::restartAtPacket(ULONG newPacket)
442 currentPacketNumber = newPacket;
443 demuxer->setPacketNum(newPacket);
447 audio->systemMuteOff();
451 void PlayerRadio::doConnectionLost()
453 logger->log("PlayerRadio", Log::DEBUG, "Connection lost, sending message");
454 Message* m = new Message();
455 m->to = messageReceiver;
457 m->message = Message::PLAYER_EVENT;
458 m->parameter = PlayerRadio::CONNECTION_LOST;
459 messageQueue->postMessage(m);
462 // ----------------------------------- Callback
464 void PlayerRadio::call(void* caller)
466 threadSignalNoLock();
469 // ----------------------------------- Feed thread
471 void PlayerRadio::threadMethod()
475 if (state == S_PLAY) threadFeedPlay();
483 void PlayerRadio::threadFeedLive()
488 UINT preBufferTotal = 0;
500 askFor = startupBlockSize; // find audio streams sized block
502 askFor = blockSize; // normal
504 threadBuffer = vdr->getBlock(0, askFor, &thisRead);
506 if (!vdr->isConnected())
512 if (!threadBuffer) break;
516 int a_stream = demuxer->scan(threadBuffer, thisRead);
517 demuxer->setAudioStream(a_stream);
518 logger->log("PlayerRadio", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
524 preBufferTotal += thisRead;
525 if (preBufferTotal >= preBufferSize)
527 logger->log("PlayerRadio", Log::DEBUG, "Got >500K, prebuffering complete");
529 preBuffering = false;
534 // unLock(); // thread will be locked by play until here
535 // FIXME - see if this can segfault because it is starting threads out of the master mutex
541 while(writeLength < thisRead)
543 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
544 writeLength += thisWrite;
548 // demuxer is full and can't take anymore
550 threadWaitForSignal();
562 logger->log("PlayerRadio", Log::DEBUG, "Live play failed to start or interrupted");
566 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
567 m->to = messageReceiver;
569 m->message = Message::PLAYER_EVENT;
570 m->parameter = PlayerRadio::STREAM_END;
571 logger->log("PlayerRadio", Log::DEBUG, "Posting message to %p...", messageQueue);
572 messageQueue->postMessage(m);
573 logger->log("PlayerRadio", Log::DEBUG, "Message posted...");
576 void PlayerRadio::threadFeedPlay()
579 UINT thisRead, writeLength, thisWrite, askFor;
580 time_t lastRescan = time(NULL);
582 feedPosition = vdr->positionFromFrameNumber(currentPacketNumber);
583 if (!vdr->isConnected()) { doConnectionLost(); return; }
584 logger->log("PlayerRadio", Log::DEBUG, "startFeedPlay: wantedPacket %i goto %llu", currentPacketNumber, feedPosition);
595 // If we havn't rescanned for a while..
596 if ((lastRescan + 60) < time(NULL))
598 lengthBytes = vdr->rescanRecording(&lengthPackets);
599 if (!vdr->isConnected()) { doConnectionLost(); return; }
600 logger->log("PlayerRadio", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
601 lastRescan = time(NULL);
603 if (!setLengthSeconds())
605 logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds in thread");
610 if (feedPosition >= lengthBytes) break; // finished playback
614 if (startupBlockSize > lengthBytes)
615 askFor = lengthBytes; // is a very small recording!
617 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
621 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
622 askFor = lengthBytes - feedPosition;
627 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
628 feedPosition += thisRead;
630 if (!vdr->isConnected())
636 if (!threadBuffer) break;
640 int a_stream = demuxer->scan(threadBuffer, thisRead);
641 demuxer->setAudioStream(a_stream);
642 logger->log("PlayerRadio", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
648 while(writeLength < thisRead)
650 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
651 writeLength += thisWrite;
655 // demuxer is full and can't take anymore
657 threadWaitForSignal();
670 logger->log("PlayerRadio", Log::DEBUG, "Recording playback ends");
675 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
676 m->to = messageReceiver;
678 m->message = Message::PLAYER_EVENT;
679 m->parameter = PlayerRadio::STOP_PLAYBACK;
680 logger->log("PlayerRadio", Log::DEBUG, "Posting message to %p...", messageQueue);
681 messageQueue->postMessage(m);
684 void PlayerRadio::threadPostStopCleanup()