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, 0, 200000))
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 void PlayerLiveRadio::setAudioChannel(int newChannel, int type)
108 return demuxer->setAID(newChannel, type);
111 // ----------------------------------- Externally called events
113 void PlayerLiveRadio::go(ULONG index)
115 struct PLInstruction i;
116 i.instruction = I_SETCHANNEL;
117 i.channelIndex = index;
118 instructions.push(i);
122 void PlayerLiveRadio::setChannel(ULONG index)
124 logger->log("PlayerLiveRadio", Log::DEBUG, "setChannel");
125 struct PLInstruction i;
126 i.instruction = I_SETCHANNEL;
127 i.channelIndex = index;
128 instructions.push(i);
129 logger->log("PlayerLiveRadio", Log::DEBUG, "posted setChannel instruction, now %i in queue", instructions.size());
130 threadSignalNoLock();
133 void PlayerLiveRadio::stop()
135 logger->log("PlayerLiveRadio", Log::DEBUG, "stop");
136 struct PLInstruction i;
137 i.instruction = I_STOP;
138 instructions.push(i);
143 // ----------------------------------- Callback
145 void PlayerLiveRadio::call(void* caller)
149 // -----------------------------------
151 void PlayerLiveRadio::streamReceive(ULONG flag, void* data, ULONG len)
154 // 0 = normal stream packet
156 // 2 = connection lost
162 Message* m = new Message();
164 m->to = messageReceiver;
165 m->message = Message::PLAYER_EVENT;
166 m->parameter = PlayerLiveRadio::STREAM_END;
167 messageQueue->postMessageFromOuterSpace(m);
170 if (streamChunks.size() < 11)
175 streamChunks.push(s);
176 threadSignalNoLock();
180 // Too many chunks in streamChunks, drop this chunk
182 logger->log("PlayerLiveRadio", Log::WARN, "Dropped chunk");
186 void PlayerLiveRadio::clearStreamChunks()
188 while(streamChunks.size())
190 logger->log("PlayerLiveRadio", Log::DEBUG, "Dropping chunk from old stream");
191 struct StreamChunk s = streamChunks.front();
197 void PlayerLiveRadio::chunkToDemuxer()
199 StreamChunk s = streamChunks.front();
201 //logger->log("PlayerLiveRadio", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
202 /*int a =*/ demuxer->put((UCHAR*)s.data, s.len);
203 //logger->log("PlayerLiveRadio", Log::DEBUG, "put %i to demuxer", a);
207 void PlayerLiveRadio::switchState(UCHAR newState)
209 logger->log("PlayerLiveRadio", Log::DEBUG, "Switch from state %u to state %u", state, newState);
213 case S_STOP: // FROM S_STOP
222 audio->setStreamType(Audio::MPEG2_PES);
223 audio->systemMuteOff();
236 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
243 case S_PREBUFFERING: // FROM S_PREBUFFERING
255 vdr->stopStreaming();
265 vdr->stopStreaming();
281 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
288 case S_PLAY: // FROM S_PLAY
294 vdr->stopStreaming();
302 case S_PREBUFFERING: // IS THIS HOW IT WORKS?
304 vdr->stopStreaming();
320 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
329 bool PlayerLiveRadio::checkError()
331 if (!vdr->isConnected())
335 Message* m = new Message();
337 m->to = messageReceiver;
338 m->message = Message::PLAYER_EVENT;
339 m->parameter = PlayerLiveRadio::CONNECTION_LOST;
340 messageQueue->postMessageFromOuterSpace(m);
347 void PlayerLiveRadio::optimizeInstructionQueue()
351 // Currently there are only 2 instruction types, so this is a bit overkill...
353 struct PLInstruction i;
354 while(instructions.size() > 1)
356 i = instructions.front();
357 if (i.instruction == I_SETCHANNEL)
359 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
361 else if (i.instruction == I_STOP)
363 return; // return here and ensure the next instruction will be stop
368 void PlayerLiveRadio::threadMethod()
372 while(!instructions.empty())
374 if (instructions.size() > 1)
376 logger->log("PlayerLiveRadio", Log::DEBUG, "Should optimise");
377 optimizeInstructionQueue();
380 struct PLInstruction i = instructions.front();
383 if (i.instruction == I_SETCHANNEL)
385 logger->log("PlayerLiveRadio", Log::DEBUG, "start new stream");
387 switchState(S_PREBUFFERING);
391 Channel* chan = (*chanList)[i.channelIndex];
394 if (chan->numAPids > 0)
396 demuxer->setAID(chan->apids[0].pid,0);
397 logger->log("PlayerLiveRadio", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
401 logger->log("PlayerLiveRadio", Log::WARN, "Demuxer no pids!");
404 int streamSuccess = vdr->streamChannel(chan->number, this);
405 if (!checkError() && !streamSuccess)
407 Message* m = new Message();
409 m->to = messageReceiver;
410 m->message = Message::PLAYER_EVENT;
411 m->parameter = PlayerLiveRadio::STREAM_END;
412 messageQueue->postMessageFromOuterSpace(m);
416 else if (i.instruction == I_STOP)
418 logger->log("PlayerLiveRadio", Log::DEBUG, "Stopping");
429 while(streamChunks.size())
433 if (state == S_PREBUFFERING)
436 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
437 logger->log("PlayerLiveRadio", Log::DEBUG, "Prebuffering %lu%%", percentDone);
439 Message* m = new Message();
441 m->to = messageReceiver;
442 m->message = Message::PLAYER_EVENT;
443 m->parameter = PlayerLiveRadio::PREBUFFERING;
444 m->tag = percentDone;
445 messageQueue->postMessageFromOuterSpace(m);
447 if (preBufferCount == preBufferAmount)
456 threadWaitForSignal(); // unlocks and waits for signal
460 logger->log("PlayerLiveRadio", Log::DEBUG, "End of thread");