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, see <https://www.gnu.org/licenses/>.
20 #include "playerlivetv.h"
26 #include "demuxerts.h"
28 #include "messagequeue.h"
32 #include "dvbsubtitles.h"
33 #include "osdreceiver.h"
36 // ----------------------------------- Called from outside, one offs or info funcs
38 PlayerLiveTV::PlayerLiveTV(MessageQueue* tmessageQueue, void* tmessageReceiver, OSDReceiver* tosdReceiver, ChannelList* tchanList)
39 : vfeed(this), afeed(this), tfeed(this)
41 messageQueue = tmessageQueue;
42 messageReceiver = tmessageReceiver;
43 osdReceiver = tosdReceiver;
46 audio = Audio::getInstance();
47 video = Video::getInstance();
48 logger = Log::getInstance();
49 vdr = VDR::getInstance();
52 subtitlesShowing = false;
54 pendingAudioPlay = false;
62 PlayerLiveTV::~PlayerLiveTV()
64 if (initted) shutdown();
67 int PlayerLiveTV::init()
69 if (initted) return 0;
71 demuxer = new DemuxerTS();
72 if (!demuxer) return 0;
73 subtitles = new DVBSubtitles(osdReceiver);
74 if (!subtitles) return 0;
76 teletext = new TeletextDecoderVBIEBU();
78 unsigned int demux_video_size = 2097152;
79 unsigned int demux_audio_size = 524288;
81 if (video->supportsh264())
83 demux_video_size *= 5 * 1;//5;
86 if (audio->maysupportAc3())
88 //demux_audio_size*=2;
91 int text_fak = video->getTeletextBufferFaktor();
93 if (!demuxer->init(this, audio, video, teletext, demux_video_size, demux_audio_size, 65536 * text_fak, 25./*unimportant*/, subtitles))
95 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, true);
143 void PlayerLiveTV::setSubtitleChannel(int newChannel)
145 demuxer->setSubID(newChannel);
148 int* PlayerLiveTV::getTeletxtSubtitlePages()
150 return teletext->getSubtitlePages();
153 int PlayerLiveTV::getCurrentSubtitleChannel()
155 return demuxer->getSubID();
158 bool PlayerLiveTV::toggleSubtitles()
160 if (!subtitlesShowing)
162 subtitlesShowing = true;
167 subtitlesShowing = false;
170 return subtitlesShowing;
173 void PlayerLiveTV::turnSubtitlesOn(bool ison)
177 subtitlesShowing = true;
182 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->postMessage(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->postMessage(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->postMessage(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())
407 threadSignalNoLock();
413 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
420 case S_VIDEOSTARTUP: // FROM S_VIDEOSTARTUP
426 pendingAudioPlay=false;
433 vdr->stopStreaming();
448 //audio->setStreamType(Audio::MPEG2_PES);
450 pendingAudioPlay = true;
462 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())
626 threadSignalNoLock();
632 logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
641 bool PlayerLiveTV::checkError()
643 if (!vdr->isConnected())
645 if (state != S_STOP) switchState(S_STOP);
647 Message* m = new Message();
649 m->to = messageReceiver;
650 m->message = Message::PLAYER_EVENT;
651 m->parameter = PlayerLiveTV::CONNECTION_LOST;
652 messageQueue->postMessage(m);
659 void PlayerLiveTV::optimizeInstructionQueue()
663 // Currently there are only 2 instruction types, so this is a bit overkill...
665 struct PLInstruction i;
666 while(instructions.size() > 1)
668 i = instructions.front();
669 if (i.instruction == I_SETCHANNEL)
671 instructions.pop(); // if this is the first of more than 1 command, currently it cannot possibly be relevant
673 else if (i.instruction == I_STOP)
675 return; // return here and ensure the next instruction will be stop
680 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;
694 while(!instructions.empty())
696 if (instructions.size() > 1) optimizeInstructionQueue();
698 struct PLInstruction i = instructions.front();
701 if (i.instruction == I_SETCHANNEL)
703 logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
705 switchState(S_VIDEOSTARTUP);
709 Channel* chan = (*chanList)[i.channelIndex];
711 h264=chan->vstreamtype==0x1b;
712 demuxer->seth264(h264);
713 video->seth264mode(chan->vstreamtype==0x1b);
714 demuxer->setVID(chan->vpid);
715 video->seth264mode(chan->vstreamtype==0x1b);
720 Command* command = Command::getInstance();
722 if (chan->numDPids > 0 && audio->maysupportAc3())
725 while (j < chan->numDPids)
727 int newpref = command->getLangPref(false, chan->dpids[j].desc);
728 if (Audio::getInstance()->streamTypeSupported(chan->dpids[j].type)
729 && (prefered < 0 || newpref < prefered))
741 if (chan->numAPids > 0)
744 while (j < chan->numAPids)
746 int newpref = command->getLangPref(false, chan->apids[j].desc);
747 if (Audio::getInstance()->streamTypeSupported(chan->apids[j].type)
748 && (prefered < 0 || newpref < prefered))
761 demuxer->setAID(chan->dpids[selected].pid, 1, chan->dpids[selected].type, true);
762 audio->setStreamType(Audio::MPEG2_PES);
763 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u (ac3) %u",
764 chan->vpid, chan->dpids[selected].pid, chan->dpids[selected].type);
766 demuxer->setAID(chan->apids[selected].pid, 0, chan->apids[selected].type, true);
767 audio->setStreamType(Audio::MPEG2_PES);
768 logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u %u", chan->vpid, chan->apids[selected].pid,
769 chan->apids[selected].type);
773 logger->log("PlayerLiveTV", Log::WARN, "Demuxer video pid only: %u", chan->vpid);
779 if (chan->numSPids) {
781 while (j < chan->numSPids)
783 int newpref = command->getLangPref(true, chan->spids[j].desc);
784 if ( (prefered < 0 || newpref < prefered))
794 demuxer->setSubID(chan->spids[selected].pid);
795 if (command->getSubDefault()) {
796 turnSubtitlesOn(true);
798 turnSubtitlesOn(false);
802 demuxer->setTID(chan->tpid);
803 teletext->ResetDecoder();
804 int streamSuccess = vdr->streamChannel(chan->number, this);
805 if (!checkError() && !streamSuccess)
807 Message* m = new Message();
809 m->to = messageReceiver;
810 m->message = Message::PLAYER_EVENT;
811 m->parameter = PlayerLiveTV::STREAM_END;
812 messageQueue->postMessage(m);
816 else if (i.instruction == I_STOP)
818 logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
831 while(streamChunks.size())
833 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark1 %d", streamChunks.size());
835 //logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark2 %d", streamChunks.size());
837 if (state == S_PREBUFFERING)
839 // logger->log("PlayerLiveTV", Log::DEBUG, "chunk mark3");
841 ULONG percentDone = (ULONG)(preBufferCount / (float)preBufferAmount * 100);
842 logger->log("PlayerLiveTV", Log::DEBUG, "Prebuffering %lu%%", percentDone);
844 Message* m = new Message();
846 m->to = messageReceiver;
847 m->message = Message::PLAYER_EVENT;
848 m->parameter = PlayerLiveTV::PREBUFFERING;
849 m->tag = percentDone;
850 messageQueue->postMessage(m);
852 if (preBufferCount == preBufferAmount)
859 //logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal %d", streamChunks.size());
861 threadWaitForSignal(); // unlocks and waits for signal
863 //logger->log("PlayerLiveTV", Log::DEBUG, "wait for signal2 %d",streamChunks.size());
866 logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");