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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "playerradio.h"
26 #include "demuxervdr.h"
27 #include "demuxerts.h"
31 #include "messagequeue.h"
33 // ----------------------------------- Called from outside, one offs or info funcs
35 PlayerRadio::PlayerRadio(MessageQueue* tmessageQueue, void* tmessageReceiver)
38 messageQueue = tmessageQueue;
39 messageReceiver = tmessageReceiver;
40 audio = Audio::getInstance();
41 logger = Log::getInstance();
42 vdr = VDR::getInstance();
55 startupBlockSize = 20000;
57 Video::getInstance()->turnVideoOff();
60 PlayerRadio::~PlayerRadio()
62 if (initted) shutdown();
65 int PlayerRadio::init(ULLONG tlengthBytes, ULONG tlengthPackets)
67 if (initted) return 0;
69 pthread_mutex_init(&mutex, NULL);
71 mutex=CreateMutex(NULL,FALSE,NULL);
74 demuxer = new DemuxerVDR();
75 if (!demuxer) return 0;
77 if (!demuxer->init(this, audio, NULL, 0, 40000))
79 logger->log("PlayerRadio", Log::ERR, "Demuxer failed to init");
87 lengthBytes = tlengthBytes;
88 lengthPackets = tlengthPackets;
90 logger->log("PlayerRadio", Log::DEBUG, "PlayerRadio has received length bytes of %llu", lengthBytes);
95 UCHAR* buffer = vdr->getBlock(0, 10000, &thisRead);
98 logger->log("PlayerRadio", Log::ERR, "Failed to get start block");
100 if (!vdr->isConnected()) doConnectionLost();
104 success = demuxer->findPTS(buffer, thisRead, &startPTS);
107 logger->log("PlayerRadio", Log::ERR, "Failed to get start PTS");
115 if (!setLengthSeconds())
117 logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds");
126 bool PlayerRadio::setLengthSeconds()
131 UCHAR* buffer = vdr->getBlock(lengthBytes - 10000, 10000, &thisRead);
134 logger->log("PlayerRadio", Log::ERR, "Failed to get end block");
135 if (!vdr->isConnected()) doConnectionLost();
139 success = demuxer->findPTS(buffer, thisRead, &endPTS);
143 logger->log("PlayerRadio", Log::ERR, "Failed to get end PTS");
147 if (startPTS < endPTS)
149 lengthSeconds = (endPTS - startPTS) / 90000;
153 lengthSeconds = (startPTS - endPTS) / 90000;
159 int PlayerRadio::shutdown()
161 if (!initted) return 0;
176 void PlayerRadio::setStartBytes(ULLONG startBytes)
178 streamPos = startBytes;
181 ULONG PlayerRadio::getLengthSeconds()
183 return lengthSeconds;
186 ULONG PlayerRadio::getCurrentSeconds()
188 if (startup) return 0;
190 long long currentPTS = demuxer->getAudioPTS();
191 currentPTS -= startPTS;
192 if (currentPTS < 0) currentPTS += 8589934592ULL;
193 ULONG ret = currentPTS / 90000;
197 // ----------------------------------- Externally called events
199 void PlayerRadio::play()
201 if (!initted) return;
202 if (state == S_PLAY) return;
208 void PlayerRadio::stop()
210 if (!initted) return;
211 if (state == S_STOP) return;
213 logger->log("PlayerRadio", Log::DEBUG, "Stop called lock");
218 void PlayerRadio::pause()
220 if (!initted) return;
223 if (state == S_PAUSE_P)
229 switchState(S_PAUSE_P);
235 void PlayerRadio::jumpToPercent(double percent)
238 logger->log("PlayerRadio", Log::DEBUG, "JUMP TO %i%%", percent);
239 ULONG newPacket = (ULONG)(percent * lengthPackets / 100);
240 switchState(S_JUMP, newPacket);
244 void PlayerRadio::skipForward(UINT seconds)
247 logger->log("PlayerRadio", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
248 ULONG currentSeconds = getCurrentSeconds();
249 ULONG currentPacket = demuxer->getPacketNum();
251 if (currentSeconds == 0) { unLock(); return; } // div by zero
252 if (currentPacket == 0) { unLock(); return; } // Current pos from demuxer is not valid
254 ULONG newPacket = currentPacket + (currentPacket * seconds / currentSeconds);
255 if (newPacket > lengthPackets) { switchState(S_PLAY); unLock(); }
256 else switchState(S_JUMP, newPacket);
260 void PlayerRadio::skipBackward(UINT seconds)
263 logger->log("PlayerRadio", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
265 ULONG currentSeconds = getCurrentSeconds();
266 ULONG currentPacket = demuxer->getPacketNum();
268 if (currentSeconds == 0) { unLock(); return; } // div by zero
269 if (currentPacket == 0) { unLock(); return; } // Current pos from demuxer is not valid
272 if ((UINT)seconds > currentSeconds)
275 newPacket = currentPacket - (currentPacket * seconds / currentSeconds);
277 switchState(S_JUMP, newPacket);
281 // ----------------------------------- Implementations called events
283 void PlayerRadio::switchState(UCHAR toState, ULONG jumpPacket)
285 if (!initted) return;
287 logger->log("PlayerRadio", Log::DEBUG, "Switch state from %u to %u", state, toState);
289 switch(state) // current state selector
291 case S_PLAY: // from S_PLAY -----------------------------------
295 case S_PLAY: // to S_PLAY
299 case S_PAUSE_P: // to S_PAUSE_P
305 case S_STOP: // to S_STOP
315 case S_JUMP: // to S_JUMP
317 restartAtPacket(jumpPacket);
322 case S_PAUSE_P: // from S_PAUSE_P -----------------------------------
326 case S_PLAY: // to S_PLAY
332 case S_PAUSE_P: // to S_PAUSE_P
336 case S_STOP: // to S_STOP
343 audio->systemMuteOff();
347 case S_JUMP: // to S_JUMP
351 restartAtPacket(jumpPacket);
356 case S_STOP: // from S_STOP -----------------------------------
360 case S_PLAY: // to S_PLAY
365 audio->setStreamType(Audio::MPEG2_PES);
366 audio->systemMuteOff();
369 // FIXME use restartAtPacket here?
370 if (currentPacketNumber > lengthPackets) currentPacketNumber = 0;
371 demuxer->setPacketNum(currentPacketNumber);
374 logger->log("PlayerRadio", Log::DEBUG, "Immediate play");
380 case S_PAUSE_P: // to S_PAUSE_P
384 case S_STOP: // to S_STOP
388 case S_JUMP: // to S_JUMP
394 // case S_JUMP cannot be selected as a start state because it auto flips to play
398 // ----------------------------------- Internal functions
400 void PlayerRadio::lock()
403 pthread_mutex_lock(&mutex);
404 logger->log("PlayerRadio", Log::DEBUG, "LOCKED");
407 WaitForSingleObject(mutex, INFINITE);
411 void PlayerRadio::unLock()
414 logger->log("PlayerRadio", Log::DEBUG, "UNLOCKING");
415 pthread_mutex_unlock(&mutex);
421 void PlayerRadio::restartAtPacket(ULONG newPacket)
426 audio->setStreamType(Audio::MPEG2_PES);
428 currentPacketNumber = newPacket;
429 demuxer->setPacketNum(newPacket);
433 audio->systemMuteOff();
437 void PlayerRadio::doConnectionLost()
439 logger->log("PlayerRadio", Log::DEBUG, "Connection lost, sending message");
440 Message* m = new Message();
441 m->to = messageReceiver;
443 m->message = Message::PLAYER_EVENT;
444 m->parameter = PlayerRadio::CONNECTION_LOST;
445 messageQueue->postMessage(m);
448 // ----------------------------------- Callback
450 void PlayerRadio::call(void* caller)
452 threadSignalNoLock();
455 // ----------------------------------- Feed thread
457 void PlayerRadio::threadMethod()
459 if (state == S_PLAY) threadFeedPlay();
462 void PlayerRadio::threadFeedPlay()
465 UINT thisRead, writeLength, thisWrite, askFor;
466 time_t lastRescan = time(NULL);
468 feedPosition = vdr->positionFromFrameNumber(currentPacketNumber);
469 if (!vdr->isConnected()) { doConnectionLost(); return; }
470 logger->log("PlayerRadio", Log::DEBUG, "startFeedPlay: wantedPacket %i goto %llu", currentPacketNumber, feedPosition);
481 // If we havn't rescanned for a while..
482 if ((lastRescan + 60) < time(NULL))
484 lengthBytes = vdr->rescanRecording(&lengthPackets);
485 if (!vdr->isConnected()) { doConnectionLost(); return; }
486 logger->log("PlayerRadio", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
487 lastRescan = time(NULL);
489 if (!setLengthSeconds())
491 logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds in thread");
496 if (feedPosition >= lengthBytes) break; // finished playback
500 if (startupBlockSize > lengthBytes)
501 askFor = lengthBytes; // is a very small recording!
503 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
507 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
508 askFor = lengthBytes - feedPosition;
513 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
514 feedPosition += thisRead;
516 if (!vdr->isConnected())
522 if (!threadBuffer) break;
526 int a_stream = demuxer->scan(threadBuffer, thisRead);
527 demuxer->setAudioStream(a_stream);
528 logger->log("PlayerRadio", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
534 while(writeLength < thisRead)
536 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
537 writeLength += thisWrite;
541 // demuxer is full and can't take anymore
543 threadWaitForSignal();
556 logger->log("PlayerRadio", Log::DEBUG, "Recording playback ends");
561 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
562 m->to = messageReceiver;
564 m->message = Message::PLAYER_EVENT;
565 m->parameter = PlayerRadio::STOP_PLAYBACK;
566 logger->log("PlayerRadio", Log::DEBUG, "Posting message to %p...", messageQueue);
567 messageQueue->postMessage(m);
570 void PlayerRadio::threadPostStopCleanup()