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"
32 #include "dvbsubtitles.h"
33 #include "osdreceiver.h"
35 // ----------------------------------- Called from outside, one offs or info funcs
37 PlayerLiveTV::PlayerLiveTV(MessageQueue* tmessageQueue, void* tmessageReceiver, OSDReceiver* tosdReceiver, ChannelList* tchanList)
38 : vfeed(this), afeed(this), tfeed(this)
40 messageQueue = tmessageQueue;
41 messageReceiver = tmessageReceiver;
42 osdReceiver = tosdReceiver;
45 audio = Audio::getInstance();
46 video = Video::getInstance();
47 logger = Log::getInstance();
48 vdr = VDR::getInstance();
51 subtitlesShowing = false;
53 pendingAudioPlay = false;
61 PlayerLiveTV::~PlayerLiveTV()
63 if (initted) shutdown();
66 int PlayerLiveTV::init()
68 if (initted) return 0;
70 demuxer = new DemuxerTS();
71 if (!demuxer) return 0;
72 subtitles = new DVBSubtitles(osdReceiver);
73 if (!subtitles) return 0;
75 teletext = new TeletextDecoderVBIEBU();
77 unsigned int demux_video_size=2097152;
78 unsigned int demux_audio_size=524288;
79 if (video->supportsh264()) {
80 demux_video_size*=5*1;//5;
83 if (audio->maysupportAc3()) {
84 //demux_audio_size*=2;
87 int text_fak=video->getTeletextBufferFaktor();
90 if (!demuxer->init(this, audio, video, teletext, demux_video_size,demux_audio_size, 65536*text_fak,25./*unimportant*/,subtitles))
92 logger->log("PlayerLiveTV", Log::ERR, "Demuxer failed to init");
109 int PlayerLiveTV::shutdown()
111 if (!initted) return 0;
112 logger->log("PlayerLiveTV", Log::DEBUG, "Shutdown");
123 bool* PlayerLiveTV::getDemuxerMpegAudioChannels()
125 return demuxer->getmpAudioChannels();
128 bool* PlayerLiveTV::getDemuxerAc3AudioChannels()
130 return demuxer->getac3AudioChannels();
133 int PlayerLiveTV::getCurrentAudioChannel()
135 return demuxer->getAID();
138 void PlayerLiveTV::setAudioChannel(int newChannel, int type,int streamtype)
140 demuxer->setAID(newChannel,type,streamtype);
143 void PlayerLiveTV::setSubtitleChannel(int newChannel)
145 demuxer->setSubID(newChannel);
148 int *PlayerLiveTV::getTeletxtSubtitlePages()
150 return teletext->getSubtitlePages();
153 int PlayerLiveTV::getCurrentSubtitleChannel(){
154 return demuxer->getSubID();
157 bool PlayerLiveTV::toggleSubtitles()
159 if (!subtitlesShowing)
161 subtitlesShowing = true;
166 subtitlesShowing = false;
169 return subtitlesShowing;
173 void PlayerLiveTV::turnSubtitlesOn(bool ison) {
176 subtitlesShowing = true;
181 subtitlesShowing = false;
187 void PlayerLiveTV::tellSubtitlesOSDVisible(bool visible)
189 subtitles->setOSDMenuVisibility(visible);
192 // ----------------------------------- Externally called events
194 void PlayerLiveTV::go(ULONG index)
196 struct PLInstruction i;
197 i.instruction = I_SETCHANNEL;
198 i.channelIndex = index;
199 instructions.push(i);
203 void PlayerLiveTV::setChannel(ULONG index)
205 logger->log("PlayerLiveTV", Log::DEBUG, "setChannel");
206 struct PLInstruction i;
207 i.instruction = I_SETCHANNEL;
208 i.channelIndex = index;
209 instructions.push(i);
210 threadSignalNoLock();
213 void PlayerLiveTV::stop()
215 logger->log("PlayerLiveTV", Log::DEBUG, "stop");
216 struct PLInstruction i;
217 i.instruction = I_STOP;
218 instructions.push(i);
221 logger->log("PlayerLiveTV", Log::DEBUG, "stop succesfull");
224 // ----------------------------------- Callback
226 void PlayerLiveTV::call(void* caller)
228 if (caller == demuxer)
230 logger->log("PlayerLiveTV", Log::DEBUG, "Callback from demuxer");
233 int dxCurrentAspect = demuxer->getAspectRatio(&parx,&pary);
234 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
236 if (video->getTVsize() == Video::ASPECT16X9)
238 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
239 video->setAspectRatio(Video::ASPECT4X3,parx,pary);
243 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
246 Message* m = new Message();
248 m->to = messageReceiver;
249 m->message = Message::PLAYER_EVENT;
250 m->parameter = PlayerLiveTV::ASPECT43;
251 messageQueue->postMessageFromOuterSpace(m);
253 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
255 if (video->getTVsize() == Video::ASPECT16X9)
257 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
258 video->setAspectRatio(Video::ASPECT16X9,parx,pary);
262 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
265 Message* m = new Message();
267 m->to = messageReceiver;
268 m->message = Message::PLAYER_EVENT;
269 m->parameter = PlayerLiveTV::ASPECT169;
270 messageQueue->postMessageFromOuterSpace(m);
274 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is something else... switch anyway");
275 video->setAspectRatio( dxCurrentAspect,parx,pary);
278 else if (caller == &afeed)
280 if (state == S_VIDEOSTARTUP)
282 logger->log("PlayerLiveTV", Log::DEBUG, "afeed video startup");
284 threadSignalNoLock();
289 // -----------------------------------
291 void PlayerLiveTV::streamReceive(ULONG flag, void* data, ULONG len)
294 // 0 = normal stream packet
296 // 2 = connection lost
298 //logger->log("PlayerLiveTV", Log::DEBUG, "Received a streamchunk from VDR, flag = %lu", flag);
304 Message* m = new Message();
306 m->to = messageReceiver;
307 m->message = Message::PLAYER_EVENT;
308 m->parameter = PlayerLiveTV::STREAM_END;
309 messageQueue->postMessageFromOuterSpace(m);
312 if (streamChunks.size() < PLAYER_MAX_STREAMING_BUFFERS)
317 streamChunks.push(s);
318 threadSignalNoLock();
322 // Too many chunks in streamChunks, drop this chunk
324 logger->log("PlayerLiveTV", Log::WARN, "Dropped chunk");
328 void PlayerLiveTV::clearStreamChunks()
330 while(streamChunks.size())
332 logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
333 struct StreamChunk s = streamChunks.front();
339 void PlayerLiveTV::chunkToDemuxer()
341 StreamChunk s = streamChunks.front();
343 // logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
344 /* int a =*/ demuxer->put((UCHAR*)s.data, s.len);
345 // logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
347 if (pendingAudioPlay && (demuxer->getHorizontalSize()|| !video->independentAVStartUp())) //Horizontal Size is zero, if not parsed
352 //audio->setStreamType(Audio::MPEG2_PES);
356 pendingAudioPlay = false;
360 void PlayerLiveTV::switchState(UCHAR newState)
362 logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
366 case S_STOP: // FROM S_STOP
381 //audio->setStreamType(Audio::MPEG2_PES);
383 // I make this modification, since the video/audio devices needs to know at least
384 // which kind of video is embedded inside the stream
385 // therefore the demuxer needs to feeded at least with enough data
386 // to have one video header
387 // This is crucial, if we have mixed h264/mpeg2 channels
388 // the information from channels is not enough since some directshow decoders need
389 // width and height information before startup
390 pendingAudioPlay = true;
404 if (!video->independentAVStartUp()){
406 threadSignalNoLock();
412 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
419 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
425 pendingAudioPlay=false;
434 vdr->stopStreaming();
449 //audio->setStreamType(Audio::MPEG2_PES);
451 pendingAudioPlay = true;
463 if (!video->independentAVStartUp()){
465 threadSignalNoLock();
471 vdr->stopStreaming();
472 pendingAudioPlay=false;
488 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
495 case S_PREBUFFERING: // FROM S_PREBUFFERING
501 pendingAudioPlay=false;
509 vdr->stopStreaming();
526 //audio->setStreamType(Audio::MPEG2_PES);
528 pendingAudioPlay = true;
545 pendingAudioPlay=false;
546 vdr->stopStreaming();
562 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
569 case S_PLAY: // FROM S_PLAY
575 pendingAudioPlay=false;
576 vdr->stopStreaming();
592 vdr->stopStreaming();
610 //audio->setStreamType(Audio::MPEG2_PES);
614 pendingAudioPlay = true;
623 if (!video->independentAVStartUp()){
625 threadSignalNoLock();
631 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
640 bool PlayerLiveTV::checkError()
642 if (!vdr->isConnected())
644 if (state != S_STOP) switchState(S_STOP);
646 Message* m = new Message();
648 m->to = messageReceiver;
649 m->message = Message::PLAYER_EVENT;
650 m->parameter = PlayerLiveTV::CONNECTION_LOST;
651 messageQueue->postMessageFromOuterSpace(m);
658 void PlayerLiveTV::optimizeInstructionQueue()
662 // Currently there are only 2 instruction types, so this is a bit overkill...
664 struct PLInstruction i;
665 while(instructions.size() > 1)
667 i = instructions.front();
668 if (i.instruction == I_SETCHANNEL)
670 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
672 else if (i.instruction == I_STOP)
674 return; // return here and ensure the next instruction will be stop
679 void PlayerLiveTV::threadMethod()
684 //logger->log("PlayerLiveTV", Log::DEBUG, "VS: %d pA %d",videoStartup,pendingAudioPlay);
685 if (videoStartup && !pendingAudioPlay) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
687 logger->log("PlayerLiveTV", Log::DEBUG, "Enter prebuffering");
688 switchState(S_PREBUFFERING);
689 videoStartup = false;
695 while(!instructions.empty())
697 if (instructions.size() > 1) optimizeInstructionQueue();
699 struct PLInstruction i = instructions.front();
702 if (i.instruction == I_SETCHANNEL)
704 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
707 switchState(S_VIDEOSTARTUP);
711 Channel* chan = (*chanList)[i.channelIndex];
713 h264=chan->vstreamtype==0x1b;
714 demuxer->seth264(h264);
715 video->seth264mode(chan->vstreamtype==0x1b);
716 demuxer->setVID(chan->vpid);
717 video->seth264mode(chan->vstreamtype==0x1b);
721 if (chan->numAPids > 0)
724 while (j<chan->numAPids && !found) {
725 if (Audio::getInstance()->streamTypeSupported(chan->apids[j].type)) {
726 demuxer->setAID(chan->apids[j].pid,0,chan->apids[j].type);
727 audio->setStreamType(Audio::MPEG2_PES);
728 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u %u", chan->vpid, chan->apids[j].pid,chan->apids[j].type);
737 if (chan->numDPids > 0 && audio->maysupportAc3())
740 while (j<chan->numDPids && !found) {
741 if (Audio::getInstance()->streamTypeSupported(chan->dpids[j].type)) {
742 demuxer->setAID(chan->dpids[j].pid,1,chan->dpids[j].type);
743 audio->setStreamType(Audio::MPEG2_PES);
744 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u (ac3) %u", chan->vpid, chan->dpids[j].pid,chan->dpids[j].type);
752 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
755 if (chan->numSPids > 0)
756 demuxer->setSubID(chan->spids[0].pid);
757 demuxer->setTID(chan->tpid);
758 teletext->ResetDecoder();
759 int streamSuccess = vdr->streamChannel(chan->number, this);
760 if (!checkError() && !streamSuccess)
762 Message* m = new Message();
764 m->to = messageReceiver;
765 m->message = Message::PLAYER_EVENT;
766 m->parameter = PlayerLiveTV::STREAM_END;
767 messageQueue->postMessageFromOuterSpace(m);
771 else if (i.instruction == I_STOP)
773 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
786 while(streamChunks.size())
788 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark1 %d", streamChunks.size());
790 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark2 %d", streamChunks.size());
792 if (state == S_PREBUFFERING)
794 // logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark3");
796 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
797 logger->log("PlayerLiveTV", Log::DEBUG, "Prebuffering %lu%%", percentDone);
799 Message* m = new Message();
801 m->to = messageReceiver;
802 m->message = Message::PLAYER_EVENT;
803 m->parameter = PlayerLiveTV::PREBUFFERING;
804 m->tag = percentDone;
805 messageQueue->postMessageFromOuterSpace(m);
807 if (preBufferCount == preBufferAmount)
814 // logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal %d", streamChunks.size());
816 threadWaitForSignal(); // unlocks and waits for signal
818 //logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal2 %d",streamChunks.size());
821 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");