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)
153 if (streamChunks.size() < 11)
158 streamChunks.push(s);
159 threadSignalNoLock();
163 // Too many chunks in streamChunks, drop this chunk
165 logger->log("PlayerLiveRadio", Log::WARN, "Dropped chunk");
169 void PlayerLiveRadio::clearStreamChunks()
171 while(streamChunks.size())
173 logger->log("PlayerLiveRadio", Log::DEBUG, "Dropping chunk from old stream");
174 struct StreamChunk s = streamChunks.front();
180 void PlayerLiveRadio::chunkToDemuxer()
182 StreamChunk s = streamChunks.front();
184 logger->log("PlayerLiveRadio", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
185 int a = demuxer->put((UCHAR*)s.data, s.len);
186 logger->log("PlayerLiveRadio", Log::DEBUG, "put %i to demuxer", a);
190 void PlayerLiveRadio::switchState(UCHAR newState)
192 logger->log("PlayerLiveRadio", Log::DEBUG, "Switch from state %u to state %u", state, newState);
196 case S_STOP: // FROM S_STOP
205 audio->setStreamType(Audio::MPEG2_PES);
206 audio->systemMuteOff();
219 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
226 case S_PREBUFFERING: // FROM S_PREBUFFERING
238 vdr->stopStreaming();
248 vdr->stopStreaming();
264 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
271 case S_PLAY: // FROM S_PLAY
277 vdr->stopStreaming();
285 case S_PREBUFFERING: // IS THIS HOW IT WORKS?
287 vdr->stopStreaming();
303 logger->log("PlayerLiveRadio", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
312 void PlayerLiveRadio::optimizeInstructionQueue()
316 // Currently there are only 2 instruction types, so this is a bit overkill...
318 struct PLInstruction i;
319 while(instructions.size() > 1)
321 i = instructions.front();
322 if (i.instruction == I_SETCHANNEL)
324 logger->log("PlayerLiveRadio", Log::DEBUG, "Optimised out setch to %i", i.channelIndex);
325 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
327 else if (i.instruction == I_STOP)
329 return; // return here and ensure the next instruction will be stop
334 void PlayerLiveRadio::threadMethod()
338 while(!instructions.empty())
340 if (instructions.size() > 1)
342 logger->log("PlayerLiveRadio", Log::DEBUG, "Should optimise");
343 optimizeInstructionQueue();
346 struct PLInstruction i = instructions.front();
349 if (i.instruction == I_SETCHANNEL)
351 logger->log("PlayerLiveRadio", Log::DEBUG, "start new stream");
353 switchState(S_PREBUFFERING);
355 Channel* chan = (*chanList)[i.channelIndex];
358 if (chan->numAPids > 0)
360 demuxer->setAID(chan->apids[0].pid,0);
361 logger->log("PlayerLiveRadio", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
365 logger->log("PlayerLiveRadio", Log::WARN, "Demuxer no pids!");
368 vdr->streamChannel(chan->number, this);
370 else if (i.instruction == I_STOP)
372 logger->log("PlayerLiveRadio", Log::DEBUG, "Stopping");
382 while(streamChunks.size())
386 if (state == S_PREBUFFERING)
388 if (++preBufferCount == preBufferAmount)
396 threadWaitForSignal(); // unlocks and waits for signal
400 logger->log("PlayerLiveRadio", Log::DEBUG, "End of thread");