2 Copyright 2008 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 "playerliveradio.h"
25 #include "demuxerts.h"
27 #include "messagequeue.h"
33 // ----------------------------------- Called from outside, one offs or info funcs
35 PlayerLiveRadio::PlayerLiveRadio(MessageQueue* tmessageQueue, void* tmessageReceiver, ChannelList* tchanList)
38 messageQueue = tmessageQueue;
39 messageReceiver = tmessageReceiver;
42 audio = Audio::getInstance();
43 logger = Log::getInstance();
44 vdr = VDR::getInstance();
49 Video::getInstance()->turnVideoOff();
52 PlayerLiveRadio::~PlayerLiveRadio()
54 if (initted) shutdown();
57 int PlayerLiveRadio::init()
59 if (initted) return 0;
61 demuxer = new DemuxerTS();
62 if (!demuxer) return 0;
64 if (!demuxer->init(this, audio, NULL, NULL, 0, 200000,0))
66 logger->log("PlayerLiveRadio", Log::ERR, "Demuxer failed to init");
78 int PlayerLiveRadio::shutdown()
80 if (!initted) return 0;
91 bool* PlayerLiveRadio::getDemuxerMpegAudioChannels()
93 return demuxer->getmpAudioChannels();
96 bool* PlayerLiveRadio::getDemuxerAc3AudioChannels()
98 return demuxer->getac3AudioChannels();
101 int PlayerLiveRadio::getCurrentAudioChannel()
103 return demuxer->getAID();
106 int *PlayerLiveRadio::getTeletxtSubtitlePages(){
110 int PlayerLiveRadio::getCurrentSubtitleChannel(){
111 return demuxer->getSubID();
114 void PlayerLiveRadio::setAudioChannel(int newChannel, int type,int streamtype)
116 demuxer->setAID(newChannel, type,streamtype,true);
119 void PlayerLiveRadio::setSubtitleChannel(int newChannel)
121 demuxer->setSubID(newChannel);
124 // ----------------------------------- Externally called events
126 void PlayerLiveRadio::go(ULONG index)
128 struct PLInstruction i;
129 i.instruction = I_SETCHANNEL;
130 i.channelIndex = index;
131 instructions.push(i);
135 void PlayerLiveRadio::setChannel(ULONG index)
137 logger->log("PlayerLiveRadio", Log::DEBUG, "setChannel");
138 struct PLInstruction i;
139 i.instruction = I_SETCHANNEL;
140 i.channelIndex = index;
141 instructions.push(i);
142 logger->log("PlayerLiveRadio", Log::DEBUG, "posted setChannel instruction, now %i in queue", instructions.size());
143 threadSignalNoLock();
146 void PlayerLiveRadio::stop()
148 logger->log("PlayerLiveRadio", Log::DEBUG, "stop");
149 struct PLInstruction i;
150 i.instruction = I_STOP;
151 instructions.push(i);
156 // ----------------------------------- Callback
158 void PlayerLiveRadio::call(void* caller)
162 // -----------------------------------
164 void PlayerLiveRadio::streamReceive(ULONG flag, void* data, ULONG len)
167 // 0 = normal stream packet
169 // 2 = connection lost
175 Message* m = new Message();
177 m->to = messageReceiver;
178 m->message = Message::PLAYER_EVENT;
179 m->parameter = PlayerLiveRadio::STREAM_END;
180 messageQueue->postMessageFromOuterSpace(m);
183 if (streamChunks.size() < 11)
188 streamChunks.push(s);
189 threadSignalNoLock();
193 // Too many chunks in streamChunks, drop this chunk
195 logger->log("PlayerLiveRadio", Log::WARN, "Dropped chunk");
199 void PlayerLiveRadio::clearStreamChunks()
201 while(streamChunks.size())
203 logger->log("PlayerLiveRadio", Log::DEBUG, "Dropping chunk from old stream");
204 struct StreamChunk s = streamChunks.front();
210 void PlayerLiveRadio::chunkToDemuxer()
212 StreamChunk s = streamChunks.front();
214 //logger->log("PlayerLiveRadio", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
215 /*int a =*/ demuxer->put((UCHAR*)s.data, s.len);
216 //logger->log("PlayerLiveRadio", Log::DEBUG, "put %i to demuxer", a);
220 void PlayerLiveRadio::switchState(UCHAR newState)
222 logger->log("PlayerLiveRadio", Log::DEBUG, "Switch from state %u to state %u", state, newState);
226 case S_STOP: // FROM S_STOP
235 audio->setStreamType(Audio::MPEG2_PES);
236 audio->systemMuteOff();
249 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
256 case S_PREBUFFERING: // FROM S_PREBUFFERING
268 vdr->stopStreaming();
278 vdr->stopStreaming();
294 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
301 case S_PLAY: // FROM S_PLAY
307 vdr->stopStreaming();
315 case S_PREBUFFERING: // IS THIS HOW IT WORKS?
317 vdr->stopStreaming();
333 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
342 bool PlayerLiveRadio::checkError()
344 if (!vdr->isConnected())
348 Message* m = new Message();
350 m->to = messageReceiver;
351 m->message = Message::PLAYER_EVENT;
352 m->parameter = PlayerLiveRadio::CONNECTION_LOST;
353 messageQueue->postMessageFromOuterSpace(m);
360 void PlayerLiveRadio::optimizeInstructionQueue()
364 // Currently there are only 2 instruction types, so this is a bit overkill...
366 struct PLInstruction i;
367 while(instructions.size() > 1)
369 i = instructions.front();
370 if (i.instruction == I_SETCHANNEL)
372 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
374 else if (i.instruction == I_STOP)
376 return; // return here and ensure the next instruction will be stop
381 void PlayerLiveRadio::threadMethod()
385 while(!instructions.empty())
387 if (instructions.size() > 1)
389 logger->log("PlayerLiveRadio", Log::DEBUG, "Should optimise");
390 optimizeInstructionQueue();
393 struct PLInstruction i = instructions.front();
396 if (i.instruction == I_SETCHANNEL)
398 logger->log("PlayerLiveRadio", Log::DEBUG, "start new stream");
400 switchState(S_PREBUFFERING);
404 Channel* chan = (*chanList)[i.channelIndex];
409 if (chan->numAPids > 0)
412 while (j<chan->numAPids && !found) {
413 if (Audio::getInstance()->streamTypeSupported(chan->apids[j].type)) {
414 demuxer->setAID(chan->apids[j].pid,0,chan->apids[j].type,true);
415 audio->setStreamType(Audio::MPEG2_PES);
416 logger->log("PlayerLiveRadio", Log::DEBUG, "Demuxer pids: %u %u %u", chan->vpid, chan->apids[j].pid,chan->apids[j].type);
425 if (chan->numDPids > 0 && audio->maysupportAc3())
428 while (j<chan->numDPids && !found) {
429 if (Audio::getInstance()->streamTypeSupported(chan->dpids[j].type)) {
430 demuxer->setAID(chan->dpids[j].pid,1,chan->dpids[j].type,true);
431 audio->setStreamType(Audio::MPEG2_PES);
432 logger->log("PlayerLiveRadio", Log::DEBUG, "Demuxer pids: %u %u (ac3) %u", chan->vpid, chan->dpids[j].pid,chan->dpids[j].type);
440 logger->log("PlayerLiveRadio", Log::WARN, "Demuxer no pids!");
446 int streamSuccess = vdr->streamChannel(chan->number, this);
447 if (!checkError() && !streamSuccess)
449 Message* m = new Message();
451 m->to = messageReceiver;
452 m->message = Message::PLAYER_EVENT;
453 m->parameter = PlayerLiveRadio::STREAM_END;
454 messageQueue->postMessageFromOuterSpace(m);
458 else if (i.instruction == I_STOP)
460 logger->log("PlayerLiveRadio", Log::DEBUG, "Stopping");
471 while(streamChunks.size())
475 if (state == S_PREBUFFERING)
478 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
479 logger->log("PlayerLiveRadio", Log::DEBUG, "Prebuffering %lu%%", percentDone);
481 Message* m = new Message();
483 m->to = messageReceiver;
484 m->message = Message::PLAYER_EVENT;
485 m->parameter = PlayerLiveRadio::PREBUFFERING;
486 m->tag = percentDone;
487 messageQueue->postMessageFromOuterSpace(m);
489 if (preBufferCount == preBufferAmount)
498 threadWaitForSignal(); // unlocks and waits for signal
502 logger->log("PlayerLiveRadio", Log::DEBUG, "End of thread");