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
161 Message* m = new Message();
163 m->to = messageReceiver;
164 m->message = Message::PLAYER_EVENT;
165 m->parameter = PlayerLiveRadio::STREAM_END;
166 messageQueue->postMessageFromOuterSpace(m);
169 if (streamChunks.size() < 11)
174 streamChunks.push(s);
175 threadSignalNoLock();
179 // Too many chunks in streamChunks, drop this chunk
181 logger->log("PlayerLiveRadio", Log::WARN, "Dropped chunk");
185 void PlayerLiveRadio::clearStreamChunks()
187 while(streamChunks.size())
189 logger->log("PlayerLiveRadio", Log::DEBUG, "Dropping chunk from old stream");
190 struct StreamChunk s = streamChunks.front();
196 void PlayerLiveRadio::chunkToDemuxer()
198 StreamChunk s = streamChunks.front();
200 //logger->log("PlayerLiveRadio", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
201 /*int a =*/ demuxer->put((UCHAR*)s.data, s.len);
202 //logger->log("PlayerLiveRadio", Log::DEBUG, "put %i to demuxer", a);
206 void PlayerLiveRadio::switchState(UCHAR newState)
208 logger->log("PlayerLiveRadio", Log::DEBUG, "Switch from state %u to state %u", state, newState);
212 case S_STOP: // FROM S_STOP
221 audio->setStreamType(Audio::MPEG2_PES);
222 audio->systemMuteOff();
235 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
242 case S_PREBUFFERING: // FROM S_PREBUFFERING
254 vdr->stopStreaming();
264 vdr->stopStreaming();
280 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
287 case S_PLAY: // FROM S_PLAY
293 vdr->stopStreaming();
301 case S_PREBUFFERING: // IS THIS HOW IT WORKS?
303 vdr->stopStreaming();
319 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
328 bool PlayerLiveRadio::checkError()
330 if (!vdr->isConnected())
334 Message* m = new Message();
336 m->to = messageReceiver;
337 m->message = Message::PLAYER_EVENT;
338 m->parameter = PlayerLiveRadio::CONNECTION_LOST;
339 messageQueue->postMessageFromOuterSpace(m);
346 void PlayerLiveRadio::optimizeInstructionQueue()
350 // Currently there are only 2 instruction types, so this is a bit overkill...
352 struct PLInstruction i;
353 while(instructions.size() > 1)
355 i = instructions.front();
356 if (i.instruction == I_SETCHANNEL)
358 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
360 else if (i.instruction == I_STOP)
362 return; // return here and ensure the next instruction will be stop
367 void PlayerLiveRadio::threadMethod()
371 while(!instructions.empty())
373 if (instructions.size() > 1)
375 logger->log("PlayerLiveRadio", Log::DEBUG, "Should optimise");
376 optimizeInstructionQueue();
379 struct PLInstruction i = instructions.front();
382 if (i.instruction == I_SETCHANNEL)
384 logger->log("PlayerLiveRadio", Log::DEBUG, "start new stream");
386 switchState(S_PREBUFFERING);
390 Channel* chan = (*chanList)[i.channelIndex];
393 if (chan->numAPids > 0)
395 demuxer->setAID(chan->apids[0].pid,0);
396 logger->log("PlayerLiveRadio", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
400 logger->log("PlayerLiveRadio", Log::WARN, "Demuxer no pids!");
403 int streamSuccess = vdr->streamChannel(chan->number, this);
404 if (!checkError() && !streamSuccess)
406 Message* m = new Message();
408 m->to = messageReceiver;
409 m->message = Message::PLAYER_EVENT;
410 m->parameter = PlayerLiveRadio::STREAM_END;
411 messageQueue->postMessageFromOuterSpace(m);
415 else if (i.instruction == I_STOP)
417 logger->log("PlayerLiveRadio", Log::DEBUG, "Stopping");
428 while(streamChunks.size())
432 if (state == S_PREBUFFERING)
435 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
436 logger->log("PlayerLiveRadio", Log::DEBUG, "Prebuffering %lu%%", percentDone);
438 Message* m = new Message();
440 m->to = messageReceiver;
441 m->message = Message::PLAYER_EVENT;
442 m->parameter = PlayerLiveRadio::PREBUFFERING;
443 m->tag = percentDone;
444 messageQueue->postMessageFromOuterSpace(m);
446 if (preBufferCount == preBufferAmount)
455 threadWaitForSignal(); // unlocks and waits for signal
459 logger->log("PlayerLiveRadio", Log::DEBUG, "End of thread");