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, bool isPesRecording)
67 if (initted) return 0;
69 pthread_mutex_init(&mutex, NULL);
71 mutex=CreateMutex(NULL,FALSE,NULL);
75 demuxer = new DemuxerVDR();
77 demuxer = new DemuxerTS();
78 if (!demuxer) return 0;
80 if (!demuxer->init(this, audio, NULL, NULL, 0, 40000, 0))
82 logger->log("PlayerRadio", Log::ERR, "Demuxer failed to init");
90 lengthBytes = tlengthBytes;
91 lengthPackets = tlengthPackets;
93 logger->log("PlayerRadio", Log::DEBUG, "PlayerRadio has received length bytes of %llu", lengthBytes);
98 UCHAR* buffer = vdr->getBlock(0, 10000, &thisRead);
101 logger->log("PlayerRadio", Log::ERR, "Failed to get start block");
103 if (!vdr->isConnected()) doConnectionLost();
107 success = demuxer->findPTS(buffer, thisRead, &startPTS);
110 logger->log("PlayerRadio", Log::ERR, "Failed to get start PTS");
118 if (!setLengthSeconds())
120 logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds");
129 bool PlayerRadio::setLengthSeconds()
134 UCHAR* buffer = vdr->getBlock(lengthBytes - 10000, 10000, &thisRead);
137 logger->log("PlayerRadio", Log::ERR, "Failed to get end block");
138 if (!vdr->isConnected()) doConnectionLost();
142 success = demuxer->findPTS(buffer, thisRead, &endPTS);
146 logger->log("PlayerRadio", Log::ERR, "Failed to get end PTS");
150 if (startPTS < endPTS)
152 lengthSeconds = (endPTS - startPTS) / 90000;
156 lengthSeconds = (startPTS - endPTS) / 90000;
162 int PlayerRadio::shutdown()
164 if (!initted) return 0;
179 void PlayerRadio::setStartBytes(ULLONG startBytes)
181 streamPos = startBytes;
184 ULONG PlayerRadio::getLengthSeconds()
186 return lengthSeconds;
189 ULONG PlayerRadio::getCurrentSeconds()
191 if (startup) return 0;
193 long long currentPTS = demuxer->getAudioPTS();
194 currentPTS -= startPTS;
195 if (currentPTS < 0) currentPTS += 8589934592ULL;
196 ULONG ret = currentPTS / 90000;
200 // ----------------------------------- Externally called events
202 void PlayerRadio::play()
204 if (!initted) return;
205 if (state == S_PLAY) return;
212 void PlayerRadio::playpause()
214 if (!initted) return;
217 switchState(S_PAUSE_P);
224 void PlayerRadio::stop()
226 if (!initted) return;
227 if (state == S_STOP) return;
229 logger->log("PlayerRadio", Log::DEBUG, "Stop called lock");
234 void PlayerRadio::pause()
236 if (!initted) return;
239 if (state == S_PAUSE_P)
245 switchState(S_PAUSE_P);
251 void PlayerRadio::jumpToPercent(double percent)
254 logger->log("PlayerRadio", Log::DEBUG, "JUMP TO %i%%", percent);
255 ULONG newPacket = (ULONG)(percent * lengthPackets / 100);
256 switchState(S_JUMP, newPacket);
260 void PlayerRadio::skipForward(UINT seconds)
263 logger->log("PlayerRadio", Log::DEBUG, "SKIP FORWARD %i SECONDS", seconds);
264 ULONG currentSeconds = getCurrentSeconds();
265 ULONG currentPacket = demuxer->getPacketNum();
267 if (currentSeconds == 0) { unLock(); return; } // div by zero
268 if (currentPacket == 0) { unLock(); return; } // Current pos from demuxer is not valid
270 ULONG newPacket = currentPacket + (currentPacket * seconds / currentSeconds);
271 if (newPacket > lengthPackets) { switchState(S_PLAY); unLock(); }
272 else switchState(S_JUMP, newPacket);
276 void PlayerRadio::skipBackward(UINT seconds)
279 logger->log("PlayerRadio", Log::DEBUG, "SKIP BACKWARD %i SECONDS", seconds);
281 ULONG currentSeconds = getCurrentSeconds();
282 ULONG currentPacket = demuxer->getPacketNum();
284 if (currentSeconds == 0) { unLock(); return; } // div by zero
285 if (currentPacket == 0) { unLock(); return; } // Current pos from demuxer is not valid
288 if ((UINT)seconds > currentSeconds)
291 newPacket = currentPacket - (currentPacket * seconds / currentSeconds);
293 switchState(S_JUMP, newPacket);
297 // ----------------------------------- Implementations called events
299 void PlayerRadio::switchState(UCHAR toState, ULONG jumpPacket)
301 if (!initted) return;
303 logger->log("PlayerRadio", Log::DEBUG, "Switch state from %u to %u", state, toState);
305 switch(state) // current state selector
307 case S_PLAY: // from S_PLAY -----------------------------------
311 case S_PLAY: // to S_PLAY
315 case S_PAUSE_P: // to S_PAUSE_P
321 case S_STOP: // to S_STOP
331 case S_JUMP: // to S_JUMP
333 restartAtPacket(jumpPacket);
338 case S_PAUSE_P: // from S_PAUSE_P -----------------------------------
342 case S_PLAY: // to S_PLAY
348 case S_PAUSE_P: // to S_PAUSE_P
352 case S_STOP: // to S_STOP
359 audio->systemMuteOff();
363 case S_JUMP: // to S_JUMP
367 restartAtPacket(jumpPacket);
372 case S_STOP: // from S_STOP -----------------------------------
376 case S_PLAY: // to S_PLAY
381 audio->setStreamType(Audio::MPEG2_PES);
382 audio->systemMuteOff();
385 // FIXME use restartAtPacket here?
386 if (currentPacketNumber > lengthPackets) currentPacketNumber = 0;
387 demuxer->setPacketNum(currentPacketNumber);
390 logger->log("PlayerRadio", Log::DEBUG, "Immediate play");
396 case S_PAUSE_P: // to S_PAUSE_P
400 case S_STOP: // to S_STOP
404 case S_JUMP: // to S_JUMP
410 // case S_JUMP cannot be selected as a start state because it auto flips to play
414 // ----------------------------------- Internal functions
416 void PlayerRadio::lock()
419 pthread_mutex_lock(&mutex);
420 logger->log("PlayerRadio", Log::DEBUG, "LOCKED");
423 WaitForSingleObject(mutex, INFINITE);
427 void PlayerRadio::unLock()
430 logger->log("PlayerRadio", Log::DEBUG, "UNLOCKING");
431 pthread_mutex_unlock(&mutex);
437 void PlayerRadio::restartAtPacket(ULONG newPacket)
442 audio->setStreamType(Audio::MPEG2_PES);
444 currentPacketNumber = newPacket;
445 demuxer->setPacketNum(newPacket);
449 audio->systemMuteOff();
453 void PlayerRadio::doConnectionLost()
455 logger->log("PlayerRadio", Log::DEBUG, "Connection lost, sending message");
456 Message* m = new Message();
457 m->to = messageReceiver;
459 m->message = Message::PLAYER_EVENT;
460 m->parameter = PlayerRadio::CONNECTION_LOST;
461 messageQueue->postMessage(m);
464 // ----------------------------------- Callback
466 void PlayerRadio::call(void* caller)
468 threadSignalNoLock();
471 // ----------------------------------- Feed thread
473 void PlayerRadio::threadMethod()
475 if (state == S_PLAY) threadFeedPlay();
478 void PlayerRadio::threadFeedPlay()
481 UINT thisRead, writeLength, thisWrite, askFor;
482 time_t lastRescan = time(NULL);
484 feedPosition = vdr->positionFromFrameNumber(currentPacketNumber);
485 if (!vdr->isConnected()) { doConnectionLost(); return; }
486 logger->log("PlayerRadio", Log::DEBUG, "startFeedPlay: wantedPacket %i goto %llu", currentPacketNumber, feedPosition);
497 // If we havn't rescanned for a while..
498 if ((lastRescan + 60) < time(NULL))
500 lengthBytes = vdr->rescanRecording(&lengthPackets);
501 if (!vdr->isConnected()) { doConnectionLost(); return; }
502 logger->log("PlayerRadio", Log::DEBUG, "Rescanned and reset length: %llu", lengthBytes);
503 lastRescan = time(NULL);
505 if (!setLengthSeconds())
507 logger->log("PlayerRadio", Log::ERR, "Failed to setLengthSeconds in thread");
512 if (feedPosition >= lengthBytes) break; // finished playback
516 if (startupBlockSize > lengthBytes)
517 askFor = lengthBytes; // is a very small recording!
519 askFor = startupBlockSize; // normal, but a startup sized block to detect all the audio streams
523 if ((feedPosition + blockSize) > lengthBytes) // last block of recording
524 askFor = lengthBytes - feedPosition;
529 threadBuffer = vdr->getBlock(feedPosition, askFor, &thisRead);
530 feedPosition += thisRead;
532 if (!vdr->isConnected())
538 if (!threadBuffer) break;
542 int a_stream = demuxer->scan(threadBuffer, thisRead);
543 demuxer->setAudioStream(a_stream);
544 logger->log("PlayerRadio", Log::DEBUG, "Startup Audio stream chosen %x", a_stream);
550 while(writeLength < thisRead)
552 thisWrite = demuxer->put(threadBuffer + writeLength, thisRead - writeLength);
553 writeLength += thisWrite;
557 // demuxer is full and can't take anymore
559 threadWaitForSignal();
572 logger->log("PlayerRadio", Log::DEBUG, "Recording playback ends");
577 Message* m = new Message(); // Must be done after this thread finishes, and must break into master mutex
578 m->to = messageReceiver;
580 m->message = Message::PLAYER_EVENT;
581 m->parameter = PlayerRadio::STOP_PLAYBACK;
582 logger->log("PlayerRadio", Log::DEBUG, "Posting message to %p...", messageQueue);
583 messageQueue->postMessage(m);
586 void PlayerRadio::threadPostStopCleanup()