]> git.vomp.tv Git - vompclient.git/blob - playerlivetv.cc
Live TV updates
[vompclient.git] / playerlivetv.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
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.
10
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.
15
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "playerlivetv.h"
22
23 #include "log.h"
24 #include "audio.h"
25 #include "video.h"
26 #include "demuxerts.h"
27 #include "vdr.h"
28 #include "messagequeue.h"
29 #include "remote.h"
30 #include "message.h"
31 #include "channel.h"
32
33 // ----------------------------------- Called from outside, one offs or info funcs
34
35 PlayerLiveTV::PlayerLiveTV(MessageQueue* tmessageQueue, void* tmessageReceiver, ChannelList* tchanList)
36 : vfeed(this), afeed(this)
37 {
38   messageQueue = tmessageQueue;
39   messageReceiver = tmessageReceiver;
40   chanList = tchanList;
41   
42   audio = Audio::getInstance();
43   video = Video::getInstance();
44   logger = Log::getInstance();
45   vdr = VDR::getInstance();
46   initted = false;
47
48   videoStartup = false;
49
50   stopNow = false;
51   state = 1;
52
53   video->turnVideoOn();
54 }
55
56 PlayerLiveTV::~PlayerLiveTV()
57 {
58   if (initted) shutdown();
59 }
60
61 int PlayerLiveTV::init()
62 {
63   if (initted) return 0;
64
65   demuxer = new DemuxerTS();
66   if (!demuxer) return 0;
67  
68   if (!demuxer->init(this, audio, video, 2097152, 524288))
69   {
70     logger->log("PlayerLiveTV", Log::ERR, "Demuxer failed to init");
71     shutdown();
72     return 0;
73   }
74
75   vfeed.init();
76   afeed.init();
77
78   video->stop();
79   video->blank();
80   audio->stop();
81
82   initted = true;
83   return 1;
84 }
85
86 int PlayerLiveTV::shutdown()
87 {
88   if (!initted) return 0;
89   stop();
90   initted = false;
91
92   delete demuxer;
93
94 #ifdef WIN32
95   CloseHandle(mutex);
96 #endif
97
98   return 1;
99 }
100
101 bool* PlayerLiveTV::getDemuxerMpegAudioChannels()
102 {
103   return demuxer->getmpAudioChannels();
104 }
105
106 bool* PlayerLiveTV::getDemuxerAc3AudioChannels()
107 {
108   return demuxer->getac3AudioChannels();
109 }
110
111 int PlayerLiveTV::getCurrentAudioChannel()
112 {
113   return demuxer->getAID();
114 }
115
116 void PlayerLiveTV::setAudioChannel(int newChannel)
117 {
118   return demuxer->setAID(newChannel);
119 }
120
121 // ----------------------------------- Externally called events
122
123 void PlayerLiveTV::go(ULONG index)
124 {
125   struct PLTVInstruction i;
126   i.instruction = 1;
127   i.channelIndex = index;
128   instructions.push(i);
129   threadStart();
130 }
131
132 void PlayerLiveTV::setChannel(ULONG index)
133 {
134   logger->log("PlayerLiveTV", Log::DEBUG, "setChannel");
135   struct PLTVInstruction i;
136   i.instruction = 1;
137   i.channelIndex = index;
138   instructions.push(i);  
139   threadSignalNoLock();
140 }
141
142 void PlayerLiveTV::stop()
143 {
144   logger->log("PlayerLiveTV", Log::DEBUG, "stop");
145   struct PLTVInstruction i;
146   i.instruction = 2;
147   instructions.push(i);
148   threadSignal();
149   threadStop();
150 }
151
152 // ----------------------------------- Callback
153
154 void PlayerLiveTV::call(void* caller)
155 {
156   if (caller == demuxer)
157   {
158     logger->log("PlayerLiveTV", Log::DEBUG, "Callback from demuxer");
159
160     if (video->getTVsize() == Video::ASPECT4X3)
161     {
162       logger->log("PlayerLiveTV", Log::DEBUG, "TV is 4:3, ignoring aspect switching");
163       return;
164     }
165
166     int dxCurrentAspect = demuxer->getAspectRatio();
167     if (dxCurrentAspect == Demuxer::ASPECT_4_3)
168     {
169       logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 4:3 aspect, switching TV");
170       video->setAspectRatio(Video::ASPECT4X3);
171
172       Message* m = new Message();
173       m->from = this;
174       m->to = messageReceiver;
175       m->message = Message::PLAYER_EVENT;
176       m->parameter = PlayerLiveTV::ASPECT43;
177       messageQueue->postMessageFromOuterSpace(m);
178     }
179     else if (dxCurrentAspect == Demuxer::ASPECT_16_9)
180     {
181       logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is 16:9 aspect, switching TV");
182       video->setAspectRatio(Video::ASPECT16X9);
183
184       Message* m = new Message();
185       m->from = this;
186       m->to = messageReceiver;
187       m->message = Message::PLAYER_EVENT;
188       m->parameter = PlayerLiveTV::ASPECT169;
189       messageQueue->postMessageFromOuterSpace(m);
190     }
191     else
192     {
193       logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer said video is something else... ignoring");
194     }
195   }
196   else if (caller == &afeed)
197   {
198     if (state == S_VIDEOSTARTUP)
199     {
200       logger->log("PlayerLiveTV", Log::DEBUG, "afeed video startup");
201       videoStartup = true;
202       threadSignalNoLock();
203     }
204   }
205 }
206
207 // -----------------------------------
208
209 void PlayerLiveTV::streamReceive(void* data, ULONG len)
210 {
211   logger->log("PlayerLiveTV", Log::DEBUG, "Got data, %p, %lu", data, len);
212   
213
214   if (streamChunks.size() < 11)
215   {
216     StreamChunk s;
217     s.data = data;
218     s.len = len;
219     streamChunks.push(s);
220     threadSignalNoLock();
221   }
222   else
223
224   {
225     // Too many chunks in streamChunks, drop this chunk
226     free(data);
227     logger->log("PlayerLiveTV", Log::DEBUG, "Dropped chunk");
228   }
229 }
230
231 void PlayerLiveTV::clearStreamChunks()
232 {
233   while(streamChunks.size())
234   {
235     logger->log("PlayerLiveTV", Log::DEBUG, "Dropping chunk from old stream");
236     struct StreamChunk s = streamChunks.front();
237     streamChunks.pop();
238     free(s.data);
239   }
240 }
241
242 void PlayerLiveTV::chunkToDemuxer()
243 {
244   StreamChunk s = streamChunks.front();
245   streamChunks.pop();
246   logger->log("PlayerLiveTV", Log::DEBUG, "About to call demuxer with %p %lu", s.data, s.len);
247   int a = demuxer->put((UCHAR*)s.data, s.len);
248   logger->log("PlayerLiveTV", Log::DEBUG, "put %i to demuxer", a);
249   free(s.data);  
250 }
251
252 void PlayerLiveTV::switchState(UCHAR newState)
253 {
254   logger->log("PlayerLiveTV", Log::DEBUG, "Switch from state %u to state %u", state, newState);
255
256   switch(state)
257   {
258     case S_STOP:   // FROM S_STOP
259     {
260       switch(newState)
261       {
262         case S_VIDEOSTARTUP:
263         {
264           video->blank();
265           video->reset();
266           video->sync();
267           video->play();
268           video->pause();
269
270           audio->stop();
271           audio->unPause();
272           audio->reset();
273           audio->setStreamType(Audio::MPEG2_PES);
274           audio->sync();
275           audio->play();
276           audio->pause();
277
278           demuxer->reset();
279           demuxer->seek();
280
281           afeed.start();
282           vfeed.start();
283           
284           state = newState;
285           return;
286         }
287         default:
288         {
289           logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
290           abort();
291           break;
292         }
293       }
294     }
295
296     case S_VIDEOSTARTUP:     // FROM S_VIDEOSTARTUP
297     {
298       switch(newState)
299       {
300         case S_PREBUFFERING:
301         {
302           logger->log("PlayerLiveTV", Log::DEBUG, "doing ss to ss2");
303           vfeed.release();
304           state = newState;
305           return;
306         }
307         default:
308         {
309           logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
310           abort();
311           break;
312         }        
313       }
314     }
315     
316     case S_PREBUFFERING:    // FROM S_PREBUFFERING
317     {
318       switch(newState)
319       {
320         case S_PLAY:
321         {
322           logger->log("PlayerLiveTV", Log::DEBUG, "doing ss2 to ss3");
323           audio->unPause();
324           video->unPause();
325           state = newState;
326           return;
327         }
328         default:
329         {
330           logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
331           abort();
332           break;
333         }        
334       }
335     }
336     
337     
338     case S_PLAY:     // FROM S_PLAY
339     {
340       switch(newState)
341       {
342         case S_STOP:
343         { 
344           vdr->stopStreaming();
345           clearStreamChunks();
346           vfeed.stop();
347           afeed.stop();
348           video->stop();
349           video->blank();
350           audio->stop();
351           audio->unPause();
352           audio->reset();
353           video->reset();
354           state = newState;
355           return;
356         }
357         case S_VIDEOSTARTUP:
358         {
359           vdr->stopStreaming();
360           clearStreamChunks();
361           vfeed.stop();
362           afeed.stop();
363           video->stop();
364           video->blank();
365           audio->stop();
366           audio->unPause();
367           audio->reset();
368
369           video->reset();
370           video->sync();
371           video->play();
372           video->pause();
373
374           audio->setStreamType(Audio::MPEG2_PES);
375           audio->sync();
376           audio->play();
377           audio->pause();
378
379           demuxer->reset();
380           demuxer->seek();
381
382           afeed.start();
383           vfeed.start();
384
385           state = newState;
386           return;
387         }
388         default:
389         {
390           logger->log("PlayerLiveTV", Log::EMERG, "Thread called state %u to state %u which is not supported", state, newState);
391           abort();
392           break;
393         }        
394       }
395     }    
396   }  
397 }
398
399 void PlayerLiveTV::threadMethod()
400 {
401   logger->log("PlayerLiveTV", Log::DEBUG, "Thread started");
402
403   while(1)
404   {
405     if (videoStartup) // we are in S_VIDEOSTARTUP, afeed has signalled that it has written some data
406     {
407       switchState(S_PREBUFFERING);
408       videoStartup = false;
409       videoStartup2Counter = 0;
410     }  
411   
412     while(!instructions.empty())
413     {
414       struct PLTVInstruction i = instructions.front();
415       instructions.pop();
416     
417       logger->log("PlayerLiveTV", Log::DEBUG, "%u %lu", i.instruction, i.channelIndex);
418       
419
420       if (i.instruction == 1)
421       {
422         logger->log("PlayerLiveTV", Log::DEBUG, "start new stream");
423
424         switchState(S_VIDEOSTARTUP);
425
426         Channel* chan = (*chanList)[i.channelIndex];
427         chan->loadPids();
428         demuxer->setVID(chan->vpid);
429         demuxer->setAID(chan->apids[0].pid);
430         logger->log("PlayerLiveTV", Log::DEBUG, "Demuxer pids: %u %u", chan->vpid, chan->apids[0].pid);
431         vdr->streamChannel(chan->number, this);
432         
433       }
434       else if (i.instruction == 2)
435       {
436         logger->log("PlayerLiveTV", Log::DEBUG, "Stopping");
437         switchState(S_STOP);
438
439         stopNow = true;
440         break;
441       }
442     }
443
444     if (stopNow) break;
445
446     while(streamChunks.size())
447     {
448       chunkToDemuxer();
449
450       if (state == S_PREBUFFERING)
451       {
452         logger->log("PlayerLiveTV", Log::DEBUG, "video startup2 buffering...");
453         if (++videoStartup2Counter == 3)
454         {
455           switchState(S_PLAY);
456         }
457       }
458     }
459     
460     threadLock();
461     threadWaitForSignal(); // unlocks and waits for signal
462     
463     threadUnlock();
464     logger->log("PlayerLiveTV", Log::DEBUG, "Woken");
465   }
466
467   logger->log("PlayerLiveTV", Log::DEBUG, "End of thread");
468 }
469