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);
225 Message* m = new Message();
227 m->to = messageReceiver;
228 m->message = Message::PLAYER_EVENT;
229 m->parameter = PlayerLiveTV::STREAM_END;
230 messageQueue->postMessageFromOuterSpace(m);
233 if (streamChunks.size() < 11)
238 streamChunks.push(s);
239 threadSignalNoLock();
243 // Too many chunks in streamChunks, drop this chunk
245 logger->log("PlayerLiveTV", Log::WARN, "Dropped chunk");
249 void PlayerLiveTV::clearStreamChunks()
251 while(streamChunks.size())
253 logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
254 struct StreamChunk s = streamChunks.front();
260 void PlayerLiveTV::chunkToDemuxer()
262 StreamChunk s = streamChunks.front();
264 //logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
265 /*int a = */demuxer->put((UCHAR*)s.data, s.len);
266 //logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
270 void PlayerLiveTV::switchState(UCHAR newState)
272 logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
276 case S_STOP: // FROM S_STOP
291 audio->setStreamType(Audio::MPEG2_PES);
307 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
314 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
327 vdr->stopStreaming();
341 audio->setStreamType(Audio::MPEG2_PES);
357 vdr->stopStreaming();
371 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
378 case S_PREBUFFERING: // FROM S_PREBUFFERING
391 vdr->stopStreaming();
406 audio->setStreamType(Audio::MPEG2_PES);
422 vdr->stopStreaming();
436 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
443 case S_PLAY: // FROM S_PLAY
449 vdr->stopStreaming();
463 vdr->stopStreaming();
478 audio->setStreamType(Audio::MPEG2_PES);
494 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
503 void PlayerLiveTV::optimizeInstructionQueue()
507 // Currently there are only 2 instruction types, so this is a bit overkill...
509 struct PLInstruction i;
510 while(instructions.size() > 1)
512 i = instructions.front();
513 if (i.instruction == I_SETCHANNEL)
515 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
517 else if (i.instruction == I_STOP)
519 return; // return here and ensure the next instruction will be stop
524 void PlayerLiveTV::threadMethod()
528 if (videoStartup) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
530 switchState(S_PREBUFFERING);
531 videoStartup = false;
535 while(!instructions.empty())
537 if (instructions.size() > 1) optimizeInstructionQueue();
539 struct PLInstruction i = instructions.front();
542 if (i.instruction == I_SETCHANNEL)
544 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
546 switchState(S_VIDEOSTARTUP);
548 Channel* chan = (*chanList)[i.channelIndex];
550 demuxer->setVID(chan->vpid);
551 if (chan->numAPids > 0)
553 demuxer->setAID(chan->apids[0].pid,0);
554 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
558 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
561 int streamSuccess = vdr->streamChannel(chan->number, this);
564 Message* m = new Message();
566 m->to = messageReceiver;
567 m->message = Message::PLAYER_EVENT;
568 m->parameter = PlayerLiveTV::STREAM_END;
569 messageQueue->postMessageFromOuterSpace(m);
572 else if (i.instruction == I_STOP)
574 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
584 while(streamChunks.size())
588 if (state == S_PREBUFFERING)
591 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
592 logger->log("PlayerLiveTV", Log::DEBUG, "Prebuffering %lu%%", percentDone);
594 Message* m = new Message();
596 m->to = messageReceiver;
597 m->message = Message::PLAYER_EVENT;
598 m->parameter = PlayerLiveTV::PREBUFFERING;
599 m->tag = percentDone;
600 messageQueue->postMessageFromOuterSpace(m);
602 if (preBufferCount == preBufferAmount)
610 threadWaitForSignal(); // unlocks and waits for signal
614 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");