2 Copyright 2007 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 "playerlivetv.h"
26 #include "demuxerts.h"
28 #include "messagequeue.h"
33 // ----------------------------------- Called from outside, one offs or info funcs
35 PlayerLiveTV::PlayerLiveTV(MessageQueue* tmessageQueue, void* tmessageReceiver, ChannelList* tchanList)
36 : vfeed(this), afeed(this)
38 messageQueue = tmessageQueue;
39 messageReceiver = tmessageReceiver;
42 audio = Audio::getInstance();
43 video = Video::getInstance();
44 logger = Log::getInstance();
45 vdr = VDR::getInstance();
56 PlayerLiveTV::~PlayerLiveTV()
58 if (initted) shutdown();
61 int PlayerLiveTV::init()
63 if (initted) return 0;
65 demuxer = new DemuxerTS();
66 if (!demuxer) return 0;
68 if (!demuxer->init(this, audio, video, 2097152, 524288))
70 logger->log("PlayerLiveTV", Log::ERR, "Demuxer failed to init");
86 int PlayerLiveTV::shutdown()
88 if (!initted) return 0;
97 bool* PlayerLiveTV::getDemuxerMpegAudioChannels()
99 return demuxer->getmpAudioChannels();
102 bool* PlayerLiveTV::getDemuxerAc3AudioChannels()
104 return demuxer->getac3AudioChannels();
107 int PlayerLiveTV::getCurrentAudioChannel()
109 return demuxer->getAID();
112 void PlayerLiveTV::setAudioChannel(int newChannel, int type)
114 return demuxer->setAID(newChannel,type);
117 // ----------------------------------- Externally called events
119 void PlayerLiveTV::go(ULONG index)
121 struct PLInstruction i;
122 i.instruction = I_SETCHANNEL;
123 i.channelIndex = index;
124 instructions.push(i);
128 void PlayerLiveTV::setChannel(ULONG index)
130 logger->log("PlayerLiveTV", Log::DEBUG, "setChannel");
131 struct PLInstruction i;
132 i.instruction = I_SETCHANNEL;
133 i.channelIndex = index;
134 instructions.push(i);
135 threadSignalNoLock();
138 void PlayerLiveTV::stop()
140 logger->log("PlayerLiveTV", Log::DEBUG, "stop");
141 struct PLInstruction i;
142 i.instruction = I_STOP;
143 instructions.push(i);
148 // ----------------------------------- Callback
150 void PlayerLiveTV::call(void* caller)
152 if (caller == demuxer)
154 logger->log("PlayerLiveTV", Log::DEBUG, "Callback from demuxer");
156 int dxCurrentAspect = demuxer->getAspectRatio();
157 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
159 if (video->getTVsize() == Video::ASPECT16X9)
161 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
162 video->setAspectRatio(Video::ASPECT4X3);
166 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
169 Message* m = new Message();
171 m->to = messageReceiver;
172 m->message = Message::PLAYER_EVENT;
173 m->parameter = PlayerLiveTV::ASPECT43;
174 messageQueue->postMessageFromOuterSpace(m);
176 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
178 if (video->getTVsize() == Video::ASPECT16X9)
180 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
181 video->setAspectRatio(Video::ASPECT16X9);
185 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
188 Message* m = new Message();
190 m->to = messageReceiver;
191 m->message = Message::PLAYER_EVENT;
192 m->parameter = PlayerLiveTV::ASPECT169;
193 messageQueue->postMessageFromOuterSpace(m);
197 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is something else... ignoring");
200 else if (caller == &afeed)
202 if (state == S_VIDEOSTARTUP)
204 logger->log("PlayerLiveTV", Log::DEBUG, "afeed video startup");
206 threadSignalNoLock();
211 // -----------------------------------
213 void PlayerLiveTV::streamReceive(ULONG flag, void* data, ULONG len)
216 // 0 = normal stream packet
219 logger->log("PlayerLiveTV", Log::DEBUG, "Received a streamchunk from VDR, flag = %lu", flag);
221 if (streamChunks.size() < 11)
226 streamChunks.push(s);
227 threadSignalNoLock();
231 // Too many chunks in streamChunks, drop this chunk
233 logger->log("PlayerLiveTV", Log::WARN, "Dropped chunk");
237 void PlayerLiveTV::clearStreamChunks()
239 while(streamChunks.size())
241 logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
242 struct StreamChunk s = streamChunks.front();
248 void PlayerLiveTV::chunkToDemuxer()
250 StreamChunk s = streamChunks.front();
252 //logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
253 /*int a = */demuxer->put((UCHAR*)s.data, s.len);
254 //logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
258 void PlayerLiveTV::switchState(UCHAR newState)
260 logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
264 case S_STOP: // FROM S_STOP
279 audio->setStreamType(Audio::MPEG2_PES);
295 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
302 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
315 vdr->stopStreaming();
329 audio->setStreamType(Audio::MPEG2_PES);
345 vdr->stopStreaming();
359 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
366 case S_PREBUFFERING: // FROM S_PREBUFFERING
379 vdr->stopStreaming();
394 audio->setStreamType(Audio::MPEG2_PES);
410 vdr->stopStreaming();
424 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
431 case S_PLAY: // FROM S_PLAY
437 vdr->stopStreaming();
451 vdr->stopStreaming();
466 audio->setStreamType(Audio::MPEG2_PES);
482 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
491 void PlayerLiveTV::optimizeInstructionQueue()
495 // Currently there are only 2 instruction types, so this is a bit overkill...
497 struct PLInstruction i;
498 while(instructions.size() > 1)
500 i = instructions.front();
501 if (i.instruction == I_SETCHANNEL)
503 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
505 else if (i.instruction == I_STOP)
507 return; // return here and ensure the next instruction will be stop
512 void PlayerLiveTV::threadMethod()
516 if (videoStartup) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
518 switchState(S_PREBUFFERING);
519 videoStartup = false;
523 while(!instructions.empty())
525 if (instructions.size() > 1) optimizeInstructionQueue();
527 struct PLInstruction i = instructions.front();
530 if (i.instruction == I_SETCHANNEL)
532 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
534 switchState(S_VIDEOSTARTUP);
536 Channel* chan = (*chanList)[i.channelIndex];
538 demuxer->setVID(chan->vpid);
539 if (chan->numAPids > 0)
541 demuxer->setAID(chan->apids[0].pid,0);
542 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
546 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
549 vdr->streamChannel(chan->number, this);
551 else if (i.instruction == I_STOP)
553 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
563 while(streamChunks.size())
567 if (state == S_PREBUFFERING)
570 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
571 logger->log("PlayerLiveTV", Log::DEBUG, "Prebuffering %lu%%", percentDone);
573 Message* m = new Message();
575 m->to = messageReceiver;
576 m->message = Message::PLAYER_EVENT;
577 m->parameter = PlayerLiveTV::PREBUFFERING;
578 m->tag = percentDone;
579 messageQueue->postMessageFromOuterSpace(m);
581 if (preBufferCount == preBufferAmount)
589 threadWaitForSignal(); // unlocks and waits for signal
593 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");