2 Copyright 2007 Chris Tallon
\r
4 This file is part of VOMP.
\r
6 VOMP is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 VOMP is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with VOMP; if not, write to the Free Software
\r
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
\r
21 #include "playerlivetv.h"
\r
26 #include "demuxerts.h"
\r
28 #include "messagequeue.h"
\r
30 #include "message.h"
\r
31 #include "channel.h"
\r
32 #include "dvbsubtitles.h"
\r
33 #include "osdreceiver.h"
\r
35 // ----------------------------------- Called from outside, one offs or info funcs
\r
37 PlayerLiveTV::PlayerLiveTV(MessageQueue* tmessageQueue, void* tmessageReceiver, OSDReceiver* tosdReceiver, ChannelList* tchanList)
\r
38 : vfeed(this), afeed(this), tfeed(this)
\r
40 messageQueue = tmessageQueue;
\r
41 messageReceiver = tmessageReceiver;
\r
42 osdReceiver = tosdReceiver;
\r
43 chanList = tchanList;
\r
45 audio = Audio::getInstance();
\r
46 video = Video::getInstance();
\r
47 logger = Log::getInstance();
\r
48 vdr = VDR::getInstance();
\r
51 subtitlesShowing = false;
\r
52 videoStartup = false;
\r
53 pendingAudioPlay = false;
\r
58 video->turnVideoOn();
\r
61 PlayerLiveTV::~PlayerLiveTV()
\r
63 if (initted) shutdown();
\r
66 int PlayerLiveTV::init()
\r
68 if (initted) return 0;
\r
70 demuxer = new DemuxerTS();
\r
71 if (!demuxer) return 0;
\r
72 subtitles = new DVBSubtitles(osdReceiver);
\r
73 if (!subtitles) return 0;
\r
75 teletext = new TeletextDecoderVBIEBU();
\r
77 unsigned int demux_video_size=2097152;
\r
78 unsigned int demux_audio_size=524288;
\r
79 if (video->supportsh264()) {
\r
80 demux_video_size*=5*1;//5;
\r
83 if (audio->maysupportAc3()) {
\r
84 //demux_audio_size*=2;
\r
87 int text_fak=video->getTeletextBufferFaktor();
\r
90 if (!demuxer->init(this, audio, video, teletext, demux_video_size,demux_audio_size, 65536*text_fak,25./*unimportant*/,subtitles))
\r
92 logger->log("PlayerLiveTV", Log::ERR, "Demuxer failed to init");
\r
109 int PlayerLiveTV::shutdown()
\r
111 if (!initted) return 0;
\r
112 logger->log("PlayerLiveTV", Log::DEBUG, "Shutdown");
\r
123 bool* PlayerLiveTV::getDemuxerMpegAudioChannels()
\r
125 return demuxer->getmpAudioChannels();
\r
128 bool* PlayerLiveTV::getDemuxerAc3AudioChannels()
\r
130 return demuxer->getac3AudioChannels();
\r
133 int PlayerLiveTV::getCurrentAudioChannel()
\r
135 return demuxer->getAID();
\r
138 void PlayerLiveTV::setAudioChannel(int newChannel, int type,int streamtype)
\r
140 demuxer->setAID(newChannel,type,streamtype);
\r
143 void PlayerLiveTV::setSubtitleChannel(int newChannel)
\r
145 demuxer->setSubID(newChannel);
\r
148 int *PlayerLiveTV::getTeletxtSubtitlePages()
\r
150 return teletext->getSubtitlePages();
\r
153 int PlayerLiveTV::getCurrentSubtitleChannel(){
\r
154 return demuxer->getSubID();
\r
157 bool PlayerLiveTV::toggleSubtitles()
\r
159 if (!subtitlesShowing)
\r
161 subtitlesShowing = true;
\r
166 subtitlesShowing = false;
\r
169 return subtitlesShowing;
\r
173 void PlayerLiveTV::turnSubtitlesOn(bool ison) {
\r
176 subtitlesShowing = true;
\r
181 subtitlesShowing = false;
\r
186 // ----------------------------------- Externally called events
\r
188 void PlayerLiveTV::go(ULONG index)
\r
190 struct PLInstruction i;
\r
191 i.instruction = I_SETCHANNEL;
\r
192 i.channelIndex = index;
\r
193 instructions.push(i);
\r
197 void PlayerLiveTV::setChannel(ULONG index)
\r
199 logger->log("PlayerLiveTV", Log::DEBUG, "setChannel");
\r
200 struct PLInstruction i;
\r
201 i.instruction = I_SETCHANNEL;
\r
202 i.channelIndex = index;
\r
203 instructions.push(i);
\r
204 threadSignalNoLock();
\r
207 void PlayerLiveTV::stop()
\r
209 logger->log("PlayerLiveTV", Log::DEBUG, "stop");
\r
210 struct PLInstruction i;
\r
211 i.instruction = I_STOP;
\r
212 instructions.push(i);
\r
215 logger->log("PlayerLiveTV", Log::DEBUG, "stop succesfull");
\r
218 // ----------------------------------- Callback
\r
220 void PlayerLiveTV::call(void* caller)
\r
222 if (caller == demuxer)
\r
224 logger->log("PlayerLiveTV", Log::DEBUG, "Callback from demuxer");
\r
226 int dxCurrentAspect = demuxer->getAspectRatio();
\r
227 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
\r
229 if (video->getTVsize() == Video::ASPECT16X9)
\r
231 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
\r
232 video->setAspectRatio(Video::ASPECT4X3);
\r
236 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
\r
239 Message* m = new Message();
\r
241 m->to = messageReceiver;
\r
242 m->message = Message::PLAYER_EVENT;
\r
243 m->parameter = PlayerLiveTV::ASPECT43;
\r
244 messageQueue->postMessageFromOuterSpace(m);
\r
246 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
\r
248 if (video->getTVsize() == Video::ASPECT16X9)
\r
250 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
\r
251 video->setAspectRatio(Video::ASPECT16X9);
\r
255 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
\r
258 Message* m = new Message();
\r
260 m->to = messageReceiver;
\r
261 m->message = Message::PLAYER_EVENT;
\r
262 m->parameter = PlayerLiveTV::ASPECT169;
\r
263 messageQueue->postMessageFromOuterSpace(m);
\r
267 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is something else... ignoring");
\r
270 else if (caller == &afeed)
\r
272 if (state == S_VIDEOSTARTUP)
\r
274 logger->log("PlayerLiveTV", Log::DEBUG, "afeed video startup");
\r
275 videoStartup = true;
\r
276 threadSignalNoLock();
\r
281 // -----------------------------------
\r
283 void PlayerLiveTV::streamReceive(ULONG flag, void* data, ULONG len)
\r
286 // 0 = normal stream packet
\r
288 // 2 = connection lost
\r
290 //logger->log("PlayerLiveTV", Log::DEBUG, "Received a streamchunk from VDR, flag = %lu", flag);
\r
296 Message* m = new Message();
\r
298 m->to = messageReceiver;
\r
299 m->message = Message::PLAYER_EVENT;
\r
300 m->parameter = PlayerLiveTV::STREAM_END;
\r
301 messageQueue->postMessageFromOuterSpace(m);
\r
304 if (streamChunks.size() < 11)
\r
309 streamChunks.push(s);
\r
310 threadSignalNoLock();
\r
314 // Too many chunks in streamChunks, drop this chunk
\r
316 logger->log("PlayerLiveTV", Log::WARN, "Dropped chunk");
\r
320 void PlayerLiveTV::clearStreamChunks()
\r
322 while(streamChunks.size())
\r
324 logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
\r
325 struct StreamChunk s = streamChunks.front();
\r
326 streamChunks.pop();
\r
331 void PlayerLiveTV::chunkToDemuxer()
\r
333 StreamChunk s = streamChunks.front();
\r
334 streamChunks.pop();
\r
335 // logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
\r
336 /* int a =*/ demuxer->put((UCHAR*)s.data, s.len);
\r
337 // logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
\r
339 if (pendingAudioPlay && (demuxer->getHorizontalSize()|| !video->independentAVStartUp())) //Horizontal Size is zero, if not parsed
\r
344 //audio->setStreamType(Audio::MPEG2_PES);
\r
348 pendingAudioPlay = false;
\r
352 void PlayerLiveTV::switchState(UCHAR newState)
\r
354 logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
\r
358 case S_STOP: // FROM S_STOP
\r
362 case S_VIDEOSTARTUP:
\r
373 //audio->setStreamType(Audio::MPEG2_PES);
\r
375 // I make this modification, since the video/audio devices needs to know at least
\r
376 // which kind of video is embedded inside the stream
\r
377 // therefore the demuxer needs to feeded at least with enough data
\r
378 // to have one video header
\r
379 // This is crucial, if we have mixed h264/mpeg2 channels
\r
380 // the information from channels is not enough since some directshow decoders need
\r
381 // width and height information before startup
\r
382 pendingAudioPlay = true;
\r
392 subtitles->start();
\r
396 if (!video->independentAVStartUp()){
\r
397 videoStartup = true;
\r
398 threadSignalNoLock();
\r
404 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
\r
411 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
\r
415 case S_PREBUFFERING:
\r
417 pendingAudioPlay=false;
\r
423 case S_VIDEOSTARTUP:
\r
426 vdr->stopStreaming();
\r
427 clearStreamChunks();
\r
441 //audio->setStreamType(Audio::MPEG2_PES);
\r
443 pendingAudioPlay = true;
\r
452 subtitles->start();
\r
455 if (!video->independentAVStartUp()){
\r
456 videoStartup = true;
\r
457 threadSignalNoLock();
\r
463 vdr->stopStreaming();
\r
464 pendingAudioPlay=false;
\r
465 clearStreamChunks();
\r
480 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
\r
487 case S_PREBUFFERING: // FROM S_PREBUFFERING
\r
493 pendingAudioPlay=false;
\r
499 case S_VIDEOSTARTUP:
\r
501 vdr->stopStreaming();
\r
502 clearStreamChunks();
\r
518 //audio->setStreamType(Audio::MPEG2_PES);
\r
520 pendingAudioPlay = true;
\r
529 subtitles->start();
\r
537 pendingAudioPlay=false;
\r
538 vdr->stopStreaming();
\r
539 clearStreamChunks();
\r
554 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
\r
561 case S_PLAY: // FROM S_PLAY
\r
567 pendingAudioPlay=false;
\r
568 vdr->stopStreaming();
\r
569 clearStreamChunks();
\r
582 case S_VIDEOSTARTUP:
\r
584 vdr->stopStreaming();
\r
585 clearStreamChunks();
\r
602 //audio->setStreamType(Audio::MPEG2_PES);
\r
606 pendingAudioPlay = true;
\r
612 subtitles->start();
\r
615 if (!video->independentAVStartUp()){
\r
616 videoStartup = true;
\r
617 threadSignalNoLock();
\r
623 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
\r
632 bool PlayerLiveTV::checkError()
\r
634 if (!vdr->isConnected())
\r
636 if (state != S_STOP) switchState(S_STOP);
\r
638 Message* m = new Message();
\r
640 m->to = messageReceiver;
\r
641 m->message = Message::PLAYER_EVENT;
\r
642 m->parameter = PlayerLiveTV::CONNECTION_LOST;
\r
643 messageQueue->postMessageFromOuterSpace(m);
\r
650 void PlayerLiveTV::optimizeInstructionQueue()
\r
654 // Currently there are only 2 instruction types, so this is a bit overkill...
\r
656 struct PLInstruction i;
\r
657 while(instructions.size() > 1)
\r
659 i = instructions.front();
\r
660 if (i.instruction == I_SETCHANNEL)
\r
662 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
\r
664 else if (i.instruction == I_STOP)
\r
666 return; // return here and ensure the next instruction will be stop
\r
671 void PlayerLiveTV::threadMethod()
\r
676 //logger->log("PlayerLiveTV", Log::DEBUG, "VS: %d pA %d",videoStartup,pendingAudioPlay);
\r
677 if (videoStartup && !pendingAudioPlay) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
\r
679 logger->log("PlayerLiveTV", Log::DEBUG, "Enter prebuffering");
\r
680 switchState(S_PREBUFFERING);
\r
681 videoStartup = false;
\r
682 preBufferCount = 0;
\r
687 while(!instructions.empty())
\r
689 if (instructions.size() > 1) optimizeInstructionQueue();
\r
691 struct PLInstruction i = instructions.front();
\r
692 instructions.pop();
\r
694 if (i.instruction == I_SETCHANNEL)
\r
696 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
\r
699 switchState(S_VIDEOSTARTUP);
\r
703 Channel* chan = (*chanList)[i.channelIndex];
\r
705 h264=chan->vstreamtype==0x1b;
\r
706 demuxer->seth264(h264);
\r
707 video->seth264mode(chan->vstreamtype==0x1b);
\r
708 demuxer->setVID(chan->vpid);
\r
709 video->seth264mode(chan->vstreamtype==0x1b);
\r
713 if (chan->numAPids > 0)
\r
716 while (j<chan->numAPids && !found) {
\r
717 if (Audio::getInstance()->streamTypeSupported(chan->apids[j].type)) {
\r
718 demuxer->setAID(chan->apids[j].pid,0,chan->apids[j].type);
\r
719 audio->setStreamType(Audio::MPEG2_PES);
\r
720 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u %u", chan->vpid, chan->apids[j].pid,chan->apids[j].type);
\r
729 if (chan->numDPids > 0 && audio->maysupportAc3())
\r
732 while (j<chan->numDPids && !found) {
\r
733 if (Audio::getInstance()->streamTypeSupported(chan->dpids[j].type)) {
\r
734 demuxer->setAID(chan->dpids[j].pid,1,chan->dpids[j].type);
\r
735 audio->setStreamType(Audio::MPEG2_PES);
\r
736 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u (ac3) %u", chan->vpid, chan->dpids[j].pid,chan->dpids[j].type);
\r
744 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
\r
747 if (chan->numSPids > 0)
\r
748 demuxer->setSubID(chan->spids[0].pid);
\r
749 demuxer->setTID(chan->tpid);
\r
750 teletext->ResetDecoder();
\r
751 int streamSuccess = vdr->streamChannel(chan->number, this);
\r
752 if (!checkError() && !streamSuccess)
\r
754 Message* m = new Message();
\r
756 m->to = messageReceiver;
\r
757 m->message = Message::PLAYER_EVENT;
\r
758 m->parameter = PlayerLiveTV::STREAM_END;
\r
759 messageQueue->postMessageFromOuterSpace(m);
\r
763 else if (i.instruction == I_STOP)
\r
765 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
\r
766 switchState(S_STOP);
\r
776 if (stopNow) break;
\r
778 while(streamChunks.size())
\r
780 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark1 %d", streamChunks.size());
\r
782 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark2 %d", streamChunks.size());
\r
784 if (state == S_PREBUFFERING)
\r
786 logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark3");
\r
788 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
\r
789 logger->log("PlayerLiveTV", Log::DEBUG, "Prebuffering %lu%%", percentDone);
\r
791 Message* m = new Message();
\r
793 m->to = messageReceiver;
\r
794 m->message = Message::PLAYER_EVENT;
\r
795 m->parameter = PlayerLiveTV::PREBUFFERING;
\r
796 m->tag = percentDone;
\r
797 messageQueue->postMessageFromOuterSpace(m);
\r
799 if (preBufferCount == preBufferAmount)
\r
801 switchState(S_PLAY);
\r
806 // logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal %d", streamChunks.size());
\r
808 threadWaitForSignal(); // unlocks and waits for signal
\r
810 //logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal2 %d",streamChunks.size());
\r
813 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");
\r