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;
186 // ----------------------------------- Externally called events
188 void PlayerLiveTV::go(ULONG index)
190 struct PLInstruction i;
191 i.instruction = I_SETCHANNEL;
192 i.channelIndex = index;
193 instructions.push(i);
197 void PlayerLiveTV::setChannel(ULONG index)
199 logger->log("PlayerLiveTV", Log::DEBUG, "setChannel");
200 struct PLInstruction i;
201 i.instruction = I_SETCHANNEL;
202 i.channelIndex = index;
203 instructions.push(i);
204 threadSignalNoLock();
207 void PlayerLiveTV::stop()
209 logger->log("PlayerLiveTV", Log::DEBUG, "stop");
210 struct PLInstruction i;
211 i.instruction = I_STOP;
212 instructions.push(i);
215 logger->log("PlayerLiveTV", Log::DEBUG, "stop succesfull");
218 // ----------------------------------- Callback
220 void PlayerLiveTV::call(void* caller)
222 if (caller == demuxer)
224 logger->log("PlayerLiveTV", Log::DEBUG, "Callback from demuxer");
226 int dxCurrentAspect = demuxer->getAspectRatio();
227 if (dxCurrentAspect == Demuxer::ASPECT_4_3)
229 if (video->getTVsize() == Video::ASPECT16X9)
231 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
232 video->setAspectRatio(Video::ASPECT4X3);
236 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
239 Message* m = new Message();
241 m->to = messageReceiver;
242 m->message = Message::PLAYER_EVENT;
243 m->parameter = PlayerLiveTV::ASPECT43;
244 messageQueue->postMessageFromOuterSpace(m);
246 else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
248 if (video->getTVsize() == Video::ASPECT16X9)
250 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
251 video->setAspectRatio(Video::ASPECT16X9);
255 logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
258 Message* m = new Message();
260 m->to = messageReceiver;
261 m->message = Message::PLAYER_EVENT;
262 m->parameter = PlayerLiveTV::ASPECT169;
263 messageQueue->postMessageFromOuterSpace(m);
267 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is something else... ignoring");
270 else if (caller == &afeed)
272 if (state == S_VIDEOSTARTUP)
274 logger->log("PlayerLiveTV", Log::DEBUG, "afeed video startup");
276 threadSignalNoLock();
281 // -----------------------------------
283 void PlayerLiveTV::streamReceive(ULONG flag, void* data, ULONG len)
286 // 0 = normal stream packet
288 // 2 = connection lost
290 //logger->log("PlayerLiveTV", Log::DEBUG, "Received a streamchunk from VDR, flag = %lu", flag);
296 Message* m = new Message();
298 m->to = messageReceiver;
299 m->message = Message::PLAYER_EVENT;
300 m->parameter = PlayerLiveTV::STREAM_END;
301 messageQueue->postMessageFromOuterSpace(m);
304 if (streamChunks.size() < PLAYER_MAX_STREAMING_BUFFERS)
309 streamChunks.push(s);
310 threadSignalNoLock();
314 // Too many chunks in streamChunks, drop this chunk
316 logger->log("PlayerLiveTV", Log::WARN, "Dropped chunk");
320 void PlayerLiveTV::clearStreamChunks()
322 while(streamChunks.size())
324 logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
325 struct StreamChunk s = streamChunks.front();
331 void PlayerLiveTV::chunkToDemuxer()
333 StreamChunk s = streamChunks.front();
335 // logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
336 /* int a =*/ demuxer->put((UCHAR*)s.data, s.len);
337 // logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
339 if (pendingAudioPlay && (demuxer->getHorizontalSize()|| !video->independentAVStartUp())) //Horizontal Size is zero, if not parsed
344 //audio->setStreamType(Audio::MPEG2_PES);
348 pendingAudioPlay = false;
352 void PlayerLiveTV::switchState(UCHAR newState)
354 logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
358 case S_STOP: // FROM S_STOP
373 //audio->setStreamType(Audio::MPEG2_PES);
375 // I make this modification, since the video/audio devices needs to know at least
376 // which kind of video is embedded inside the stream
377 // therefore the demuxer needs to feeded at least with enough data
378 // to have one video header
379 // This is crucial, if we have mixed h264/mpeg2 channels
380 // the information from channels is not enough since some directshow decoders need
381 // width and height information before startup
382 pendingAudioPlay = true;
396 if (!video->independentAVStartUp()){
398 threadSignalNoLock();
404 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
411 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
417 pendingAudioPlay=false;
426 vdr->stopStreaming();
441 //audio->setStreamType(Audio::MPEG2_PES);
443 pendingAudioPlay = true;
455 if (!video->independentAVStartUp()){
457 threadSignalNoLock();
463 vdr->stopStreaming();
464 pendingAudioPlay=false;
480 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
487 case S_PREBUFFERING: // FROM S_PREBUFFERING
493 pendingAudioPlay=false;
501 vdr->stopStreaming();
518 //audio->setStreamType(Audio::MPEG2_PES);
520 pendingAudioPlay = true;
537 pendingAudioPlay=false;
538 vdr->stopStreaming();
554 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
561 case S_PLAY: // FROM S_PLAY
567 pendingAudioPlay=false;
568 vdr->stopStreaming();
584 vdr->stopStreaming();
602 //audio->setStreamType(Audio::MPEG2_PES);
606 pendingAudioPlay = true;
615 if (!video->independentAVStartUp()){
617 threadSignalNoLock();
623 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
632 bool PlayerLiveTV::checkError()
634 if (!vdr->isConnected())
636 if (state != S_STOP) switchState(S_STOP);
638 Message* m = new Message();
640 m->to = messageReceiver;
641 m->message = Message::PLAYER_EVENT;
642 m->parameter = PlayerLiveTV::CONNECTION_LOST;
643 messageQueue->postMessageFromOuterSpace(m);
650 void PlayerLiveTV::optimizeInstructionQueue()
654 // Currently there are only 2 instruction types, so this is a bit overkill...
656 struct PLInstruction i;
657 while(instructions.size() > 1)
659 i = instructions.front();
660 if (i.instruction == I_SETCHANNEL)
662 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
664 else if (i.instruction == I_STOP)
666 return; // return here and ensure the next instruction will be stop
671 void PlayerLiveTV::threadMethod()
676 //logger->log("PlayerLiveTV", Log::DEBUG, "VS: %d pA %d",videoStartup,pendingAudioPlay);
677 if (videoStartup && !pendingAudioPlay) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
679 logger->log("PlayerLiveTV", Log::DEBUG, "Enter prebuffering");
680 switchState(S_PREBUFFERING);
681 videoStartup = false;
687 while(!instructions.empty())
689 if (instructions.size() > 1) optimizeInstructionQueue();
691 struct PLInstruction i = instructions.front();
694 if (i.instruction == I_SETCHANNEL)
696 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
699 switchState(S_VIDEOSTARTUP);
703 Channel* chan = (*chanList)[i.channelIndex];
705 h264=chan->vstreamtype==0x1b;
706 demuxer->seth264(h264);
707 video->seth264mode(chan->vstreamtype==0x1b);
708 demuxer->setVID(chan->vpid);
709 video->seth264mode(chan->vstreamtype==0x1b);
713 if (chan->numAPids > 0)
716 while (j<chan->numAPids && !found) {
717 if (Audio::getInstance()->streamTypeSupported(chan->apids[j].type)) {
718 demuxer->setAID(chan->apids[j].pid,0,chan->apids[j].type);
719 audio->setStreamType(Audio::MPEG2_PES);
720 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u %u", chan->vpid, chan->apids[j].pid,chan->apids[j].type);
729 if (chan->numDPids > 0 && audio->maysupportAc3())
732 while (j<chan->numDPids && !found) {
733 if (Audio::getInstance()->streamTypeSupported(chan->dpids[j].type)) {
734 demuxer->setAID(chan->dpids[j].pid,1,chan->dpids[j].type);
735 audio->setStreamType(Audio::MPEG2_PES);
736 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u (ac3) %u", chan->vpid, chan->dpids[j].pid,chan->dpids[j].type);
744 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
747 if (chan->numSPids > 0)
748 demuxer->setSubID(chan->spids[0].pid);
749 demuxer->setTID(chan->tpid);
750 teletext->ResetDecoder();
751 int streamSuccess = vdr->streamChannel(chan->number, this);
752 if (!checkError() && !streamSuccess)
754 Message* m = new Message();
756 m->to = messageReceiver;
757 m->message = Message::PLAYER_EVENT;
758 m->parameter = PlayerLiveTV::STREAM_END;
759 messageQueue->postMessageFromOuterSpace(m);
763 else if (i.instruction == I_STOP)
765 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
778 while(streamChunks.size())
780 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark1 %d", streamChunks.size());
782 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark2 %d", streamChunks.size());
784 if (state == S_PREBUFFERING)
786 // logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark3");
788 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
789 logger->log("PlayerLiveTV", Log::DEBUG, "Prebuffering %lu%%", percentDone);
791 Message* m = new Message();
793 m->to = messageReceiver;
794 m->message = Message::PLAYER_EVENT;
795 m->parameter = PlayerLiveTV::PREBUFFERING;
796 m->tag = percentDone;
797 messageQueue->postMessageFromOuterSpace(m);
799 if (preBufferCount == preBufferAmount)
806 // logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal %d", streamChunks.size());
808 threadWaitForSignal(); // unlocks and waits for signal
810 //logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal2 %d",streamChunks.size());
813 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");