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)
114 return demuxer->setAID(newChannel);
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 if (video->getTVsize() == Video::ASPECT4X3)
158 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
162 int dxCurrentAspect = demuxer->getAspectRatio();
163 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
165 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
166 video->setAspectRatio(Video::ASPECT4X3);
168 Message* m = new Message();
170 m->to = messageReceiver;
171 m->message = Message::PLAYER_EVENT;
172 m->parameter = PlayerLiveTV::ASPECT43;
173 messageQueue->postMessageFromOuterSpace(m);
175 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
177 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
178 video->setAspectRatio(Video::ASPECT16X9);
180 Message* m = new Message();
182 m->to = messageReceiver;
183 m->message = Message::PLAYER_EVENT;
184 m->parameter = PlayerLiveTV::ASPECT169;
185 messageQueue->postMessageFromOuterSpace(m);
189 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is something else... ignoring");
192 else if (caller == &afeed)
194 if (state == S_VIDEOSTARTUP)
196 logger->log("PlayerLiveTV", Log::DEBUG, "afeed video startup");
198 threadSignalNoLock();
203 // -----------------------------------
205 void PlayerLiveTV::streamReceive(void* data, ULONG len)
207 if (streamChunks.size() < 11)
212 streamChunks.push(s);
213 threadSignalNoLock();
217 // Too many chunks in streamChunks, drop this chunk
219 logger->log("PlayerLiveTV", Log::WARN, "Dropped chunk");
223 void PlayerLiveTV::clearStreamChunks()
225 while(streamChunks.size())
227 logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
228 struct StreamChunk s = streamChunks.front();
234 void PlayerLiveTV::chunkToDemuxer()
236 StreamChunk s = streamChunks.front();
238 //logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
239 /*int a = */demuxer->put((UCHAR*)s.data, s.len);
240 //logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
244 void PlayerLiveTV::switchState(UCHAR newState)
246 logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
250 case S_STOP: // FROM S_STOP
265 audio->setStreamType(Audio::MPEG2_PES);
281 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
288 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
301 vdr->stopStreaming();
315 audio->setStreamType(Audio::MPEG2_PES);
331 vdr->stopStreaming();
345 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
352 case S_PREBUFFERING: // FROM S_PREBUFFERING
365 vdr->stopStreaming();
380 audio->setStreamType(Audio::MPEG2_PES);
396 vdr->stopStreaming();
410 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
417 case S_PLAY: // FROM S_PLAY
423 vdr->stopStreaming();
437 vdr->stopStreaming();
452 audio->setStreamType(Audio::MPEG2_PES);
468 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
477 void PlayerLiveTV::optimizeInstructionQueue()
481 // Currently there are only 2 instruction types, so this is a bit overkill...
483 struct PLInstruction i;
484 while(instructions.size() > 1)
486 i = instructions.front();
487 if (i.instruction == I_SETCHANNEL)
489 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
491 else if (i.instruction == I_STOP)
493 return; // return here and ensure the next instruction will be stop
498 void PlayerLiveTV::threadMethod()
502 if (videoStartup) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
504 switchState(S_PREBUFFERING);
505 videoStartup = false;
509 while(!instructions.empty())
511 if (instructions.size() > 1) optimizeInstructionQueue();
513 struct PLInstruction i = instructions.front();
516 if (i.instruction == I_SETCHANNEL)
518 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
520 switchState(S_VIDEOSTARTUP);
522 Channel* chan = (*chanList)[i.channelIndex];
524 demuxer->setVID(chan->vpid);
525 if (chan->numAPids > 0)
527 demuxer->setAID(chan->apids[0].pid);
528 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
532 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
535 vdr->streamChannel(chan->number, this);
537 else if (i.instruction == I_STOP)
539 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
549 while(streamChunks.size())
553 if (state == S_PREBUFFERING)
555 if (++preBufferCount == preBufferAmount)
563 threadWaitForSignal(); // unlocks and waits for signal
567 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");