Ginga  0.13.6
 All Classes Namespaces Functions Variables
NPTProcessor.h
1 /******************************************************************************
2 Este arquivo eh parte da implementacao do ambiente declarativo do middleware
3 Ginga (Ginga-NCL).
4 
5 Direitos Autorais Reservados (c) 1989-2007 PUC-Rio/Laboratorio TeleMidia
6 
7 Este programa eh software livre; voce pode redistribui-lo e/ou modificah-lo sob
8 os termos da Licenca Publica Geral GNU versao 2 conforme publicada pela Free
9 Software Foundation.
10 
11 Este programa eh distribuido na expectativa de que seja util, porem, SEM
12 NENHUMA GARANTIA; nem mesmo a garantia implicita de COMERCIABILIDADE OU
13 ADEQUACAO A UMA FINALIDADE ESPECIFICA. Consulte a Licenca Publica Geral do
14 GNU versao 2 para mais detalhes.
15 
16 Voce deve ter recebido uma copia da Licenca Publica Geral do GNU versao 2 junto
17 com este programa; se nao, escreva para a Free Software Foundation, Inc., no
18 endereco 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
19 
20 Para maiores informacoes:
21 ncl @ telemidia.puc-rio.br
22 http://www.ncl.org.br
23 http://www.ginga.org.br
24 http://www.telemidia.puc-rio.br
25 ******************************************************************************
26 This file is part of the declarative environment of middleware Ginga (Ginga-NCL)
27 
28 Copyright: 1989-2007 PUC-RIO/LABORATORIO TELEMIDIA, All Rights Reserved.
29 
30 This program is free software; you can redistribute it and/or modify it under
31 the terms of the GNU General Public License version 2 as published by
32 the Free Software Foundation.
33 
34 This program is distributed in the hope that it will be useful, but WITHOUT ANY
35 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
36 PARTICULAR PURPOSE. See the GNU General Public License version 2 for more
37 details.
38 
39 You should have received a copy of the GNU General Public License version 2
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
42 
43 For further information contact:
44 ncl @ telemidia.puc-rio.br
45 http://www.ncl.org.br
46 http://www.ginga.org.br
47 http://www.telemidia.puc-rio.br
48 *******************************************************************************/
49 
50 #ifndef NPTPROCESSOR_H_
51 #define NPTPROCESSOR_H_
52 
53 #include "system/thread/Thread.h"
54 using namespace br::pucrio::telemidia::ginga::core::system::thread;
55 
56 #include "system/compat/SystemCompat.h"
57 using namespace ::br::pucrio::telemidia::ginga::core::system::compat;
58 
59 #include "system/time/ITimeBaseProvider.h"
60 using namespace br::pucrio::telemidia::ginga::core::system::time;
61 
62 #include "tuner/providers/ISTCProvider.h"
63 using namespace br::pucrio::telemidia::ginga::core::tuning;
64 
65 #include "DSMCCSectionPayload.h"
66 #include "MpegDescriptor.h"
67 #include "NPTReference.h"
68 #include "TimeBaseClock.h"
69 
70 #include <set>
71 #include <map>
72 #include <iostream>
73 #include <string>
74 #include <cmath>
75 using namespace std;
76 
77 namespace br {
78 namespace pucrio {
79 namespace telemidia {
80 namespace ginga {
81 namespace core {
82 namespace dataprocessing {
83 namespace dsmcc {
84 namespace npt {
85 
86 class NPTProcessor : public Thread, public ITimeBaseProvider {
87 
88  struct TimeControl {
89  double time;
90  bool notified;
91  };
92 
93  private:
94  static const unsigned short MAX_NPT_VALUE = 47721;
95  static const char INVALID_CID = -1;
96  static const short NPT_ST_OCCURRING = 0;
97  static const short NPT_ST_PAUSED = 1;
98 
99  ISTCProvider* stcProvider;
100  bool running;
101  bool loopControlMin;
102  bool loopControlMax;
103  unsigned char currentCid;
104  unsigned char occurringTimeBaseId;
105 
106  pthread_mutex_t loopMutex;
107  pthread_mutex_t schedMutex;
108  pthread_mutex_t lifeMutex;
109 
110  map<unsigned char, NPTReference*> scheduledNpts;
111  map<unsigned char, TimeBaseClock*> timeBaseClock;
112  map<unsigned char, Stc*> timeBaseLife;
113  map<unsigned char, set<ITimeBaseProvider*>*> loopListeners;
114  map<unsigned char, map<TimeControl*, set<ITimeBaseProvider*>*>*> timeListeners;
115  set<ITimeBaseProvider*> cidListeners;
116  bool reScheduleIt;
117  uint64_t firstStc;
118  bool isFirstStc;
119  bool nptPrinter;
120  map<char,NPTReference*> lastNptList;
121 
122  public:
123  NPTProcessor(ISTCProvider* stcProvider);
124  virtual ~NPTProcessor();
125 
126  void setNptPrinter(bool nptPrinter);
127 
128  private:
129  uint64_t getSTCValue();
130  void clearUnusedTimebase();
131  void clearTables();
132  void detectLoop();
133 
134  public:
135  bool addLoopListener(unsigned char cid, ITimeBaseListener* ltn);
136  bool removeLoopListener(unsigned char cid, ITimeBaseListener* ltn);
137 
138  bool addTimeListener(
139  unsigned char cid, double nptValue, ITimeBaseListener* ltn);
140 
141  bool removeTimeListener(unsigned char cid, ITimeBaseListener* ltn);
142 
143  bool addIdListener(ITimeBaseListener* ltn);
144  bool removeIdListener(ITimeBaseListener* ltn);
145 
146  unsigned char getOccurringTimeBaseId();
147 
148  private:
149  unsigned char getCurrentTimeBaseId();
150  void notifyLoopToTimeListeners();
151  void notifyTimeListeners(unsigned char cid, double nptValue);
152  void notifyNaturalEndListeners(unsigned char cid, double nptValue);
153  void notifyIdListeners(unsigned char oldCid, unsigned char newCid);
154  TimeBaseClock* getTimeBaseClock(unsigned char cid);
155  int updateTimeBase(TimeBaseClock* clk, NPTReference* npt);
156  TimeBaseClock* getCurrentTimebase();
157  double getCurrentTimeValue(unsigned char timeBaseId);
158 
159  public:
160  int decodeDescriptors(vector<MpegDescriptor*>* list);
161  double getNPTValue(unsigned char contentId);
162 
163  private:
164  char getNextNptValue(char cid, double *nextNptValue, double* sleepTime);
165 
166  bool processNptValues();
167  void run();
168 };
169 
170 }
171 }
172 }
173 }
174 }
175 }
176 }
177 }
178 
179 #endif /* NPTPROCESSOR_H_ */