• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 #if !defined(LOWER_LAYER_H)
19 #define LOWER_LAYER_H
20 #include "oscl_types.h"
21 #include "oscl_time.h"
22 #ifndef OSCL_TIMER_H_INCLUDED
23 #include "oscl_timer.h"
24 #endif
25 #include "pvt_params.h"
26 #include "pduparcom.h"
27 #include "layer.h"
28 #include "media_packet.h"
29 
30 #ifndef OSCL_BASE_H_INCLUDED
31 #include "oscl_base.h"
32 #endif
33 #ifndef OSCLCONFIG_IO_H_INCLUDED
34 #include "osclconfig_io.h"
35 #endif
36 
37 #ifndef OSCL_SCHEDULER_AO_H_INCLUDED
38 #include "oscl_scheduler_ao.h"
39 #endif
40 
41 #ifndef OSCL_FILE_IO_H_INCLUDED
42 #include "oscl_file_io.h"
43 #endif
44 
45 #ifndef OSCL_PRIQUEUE_H_INCLUDED
46 #include "oscl_priqueue.h"
47 #endif
48 
49 #ifndef OSCL_MEM_MEMPOOL_H_INCLUDED
50 #include "oscl_mem_mempool.h"
51 #endif
52 
53 #ifndef PVMF_MEDIA_DATA_H_INCLUDED
54 #include "pvmf_media_data.h"
55 #endif
56 
57 #ifndef PVMF_SIMPLE_MEDIA_BUFFER_H_INCLUDED
58 #include "pvmf_simple_media_buffer.h"
59 #endif
60 
61 #ifndef PVMF_NODE_INTERFACE_H_INCLUDED
62 #include "pvmf_node_interface.h"
63 #endif
64 
65 #ifndef PVMF_PORT_BASE_IMPL_H_INCLUDED
66 #include "pvmf_port_base_impl.h"
67 #endif
68 
69 #ifndef PVMF_POOL_BUFFER_ALLOCATOR_H_INCLUDED
70 #include "pvmf_pool_buffer_allocator.h"
71 #endif
72 
73 #ifndef PVLOGGER_H_INCLUDED
74 #include "pvlogger.h"
75 #endif
76 
77 #include "h223types.h"
78 #define H223_MAX_SEND_SIZE  1024
79 #define DEFAULT_BITRATE 64000
80 #define H223_DEMUX_BUFFER_INTERVAL_MS 0
81 
82 class LowerLayerObserver
83 {
84     public:
~LowerLayerObserver()85         virtual ~LowerLayerObserver() {}
86         virtual PVMFStatus GetOutgoingMuxPdus(MuxPduPacketList& packets) = 0;
87         virtual void LevelSetupComplete(PVMFStatus status, TPVH223Level level) = 0;
88         virtual void DataReceptionStart() = 0;
89         virtual H223PduParcomObserver* GetParcomObserver()  = 0;
90         virtual void LowerLayerError(TPVDirection direction, PVMFStatus error) = 0;
91 };
92 
93 
94 /* Lower layer which binds to Network IO */
95 class H223LowerLayer : public PvmfPortBaseImpl,
96         public PVMFPortActivityHandler,
97         public OsclTimerObject,
98         public OsclTimerObserver,
99         public H223PduParcomObserver
100 {
101     public:
102         H223LowerLayer(int32 aPortTag, TPVLoopbackMode aLoopbackMode);
103         ~H223LowerLayer();
104 
SetObserver(LowerLayerObserver * observer)105         void SetObserver(LowerLayerObserver* observer)
106         {
107             iObserver = observer;
108             if (iParcom.GetRep())
109                 iParcom->SetObserver(iObserver->GetParcomObserver());
110         }
111 
GetObserver()112         LowerLayerObserver* GetObserver()
113         {
114             return iObserver;
115         }
116 
SetTimerRes(uint32 timer_res)117         TPVStatusCode SetTimerRes(uint32 timer_res)
118         {
119             iMinTimerResolution = timer_res;
120             UpdatePduSize();
121             return EPVT_Success;
122         }
123 
124         TPVStatusCode Open();
125         TPVStatusCode Close();
126         TPVStatusCode Start(H223PduParcomSharedPtr parcom);
127         TPVStatusCode Stop();
128 
129         PVMFStatus Connect(PVMFPortInterface* aPort);
130         void HandlePortActivity(const PVMFPortActivity &);
131         //Force lower layer to stop.
132         int Abort();
133 
134         TPVStatusCode SetLevel(TPVH223Level muxLevel);
135 
GetLevel()136         TPVH223Level GetLevel()
137         {
138             return iParcom->GetLevel();
139         }
140 
141         TPVStatusCode SetBitrate(uint32 bitRate);
142 
SetTransmitProfile(bool iAnxA,bool iAnxADf,bool iAnxB,bool iAnxBOh)143         TPVStatusCode SetTransmitProfile(bool iAnxA, bool iAnxADf, bool iAnxB, bool iAnxBOh)
144         {
145             iUseAnnexA = iAnxA;
146             iUseAnnexADoubleFlag = iAnxADf;
147             iUseAnnexB = iAnxB;
148             iUseAnnexBwithHeader = iAnxBOh;
149             return EPVT_Success;
150         }
151 
152         TPVStatusCode SetMaxOutgoingPduSize(uint16 Size);
153 
GetMinTimerRes()154         int GetMinTimerRes()
155         {
156             return iMinTimerResolution;
157         }
158         /* Implement PAcketInput: process incoming packet */
159         void PacketIn(PVMFSharedMediaDataPtr aMediaData);
160 
161         /* Dispatch outgoing packet */
162         void DispatchPacket(OsclSharedPtr<PVMFMediaDataImpl>& pack);
163 
164         void SetLoopbackMode(TPVLoopbackMode aLoopbackMode);
165 
166         int iTimerIntervalNum;
167         unsigned int iMinTimerResolution;
168 
169         void TimeoutOccurred(int32 timerID, int32 timeoutInfo);
170 
171         /* H223PduParcomObserver virtuals */
172         uint32 MuxPduIndicate(uint8* pPdu, uint32 pduSz, int32 fClosing, int32 muxCode);
173         void MuxPduErrIndicate(EMuxPduError err);
174         void MuxSetupComplete(PVMFStatus status, TPVH223Level level);
175         unsigned SetStuffingSize(TPVH223Level level);
176 
GetStuffingSize()177         unsigned GetStuffingSize()
178         {
179             return iParcom->GetStuffingSz();
180         }
GetHeaderSize()181         unsigned GetHeaderSize()
182         {
183             return iParcom->GetHeaderSz();
184         }
185 
186         unsigned SetPduSize(unsigned pdu_size);
GetPduSize()187         unsigned GetPduSize()
188         {
189             return iPduSize;
190         }
191         unsigned GetSendSize();
192 
193         void ResetStats();
194         void LogStats(TPVDirection dir);
195         int32 GetTimestamp();
196         void GetHdrFragment(OsclRefCounterMemFrag& hdr_frag);
197         PVMFStatus CompletePacket(OsclSharedPtr<PVMFMediaDataImpl>& packet, int mt_entry, int pm);
198         void DoStuffing(TPVH223Level level,
199                         OsclSharedPtr<PVMFMediaDataImpl>& pkt,
200                         uint32 stuffing_sz,
201                         uint8 mux_code = 0);
202         void SendStuffingMsgHeader(uint8* stuffing, uint16 num_bytes);
203     protected:
204         PVMFStatus PutData(PVMFSharedMediaMsgPtr aMsg);
205         void Parse(uint8* buf, uint16 size);
206         bool CheckLevel(uint8* pData, uint16 size , uint16* used_bytes, int* sync_detected);
207         bool CheckLevelWithSync(uint8* pData, uint16 size, uint16* used_bytes) ;
208         void SignalLevelSetupComplete(PVMFStatus status);
209 
210         void Run();
211         void DoCancel();
212     private:
213         void InitParams();
214         void InitRuntimeParams();
215         PVMFStatus Mux();
216         void UpdatePduSize();
217         void CreateParcom(TPVH223Level level);
218         void MuxException();
219 
220         LowerLayerObserver* iObserver;
221 
222         /* Transmit profile */
223         bool iUseAnnexA;
224         bool iUseAnnexADoubleFlag;
225         bool iUseAnnexB;
226         bool iUseAnnexBwithHeader;
227 
228         /* multiplex level */
229         bool iLevelSetupComplete;
230 
231         /* Stuffing buffers for each level.  Pre-allocated and initialized with stuffing for that level */
232         PVMFBufferPoolAllocator* iMemFragmentAlloc;
233         OsclRefCounterMemFrag iStuffingFrag[MAX_H223_LEVELS+1];
234 
235         uint16 iStuffingSz[8];
236 
237         /* bitrate, bps */
238         uint32 iBitrate;
239 
240         FragmentAllocator* iHdrFragmentAlloc;
241 
242         PVMFMediaFragGroupCombinedAlloc<OsclMemAllocator>* iDispatchPacketAlloc;
243         OsclMemPoolFixedChunkAllocator* iMediaDataImplMemAlloc;
244 
245         OsclMemPoolFixedChunkAllocator* iMediaMsgPoolAlloc;
246 
247         OsclSharedPtr<PVMFMediaDataImpl> iDispatchPacket;
248         MuxPduPacketList iSendPacketList;
249 
250         /* Send MUX-PDU size */
251         unsigned iSendPduSz;
252         unsigned iPduSize;
253         unsigned iStuffingSize;
254 
255         /* Timer stuff */
256         OsclTimer<OsclMemAllocator> iTimer;
257 
258         /* operational state */
259         int iState;
260         int iStateVal;
261 
262         /* PDU parcoms */
263         H223PduParcomSharedPtr iParcom;
264         TPVLoopbackMode iLoopbackMode;
265 
266         int iTimerCnt;
267         int iBytesSent;
268         uint32 iSendTimes;
269 
270         TimeValue iStartTimeTx;
271         uint32 iNumBytesTx;
272         uint32 iNumMuxSduBytesTx;
273         uint32 iNumStuffingBytesTx;
274         uint32 iNumPdusTx;
275         uint32 iNumPmTx;
276 
277         TimeValue iStartTimeRx;
278         uint32 iNumBytesRx;
279 
280         TimeValue  iLevelSetupTime;
281 
282         // Allocator for simple media data buffer
283         OsclMemAllocator iMemAlloc;
284         PVMFSimpleMediaBufferCombinedAlloc iMediaDataAlloc;
285         TimeValue iStartMuxTime;
286         TimeValue iLastMuxTime;
287 
288         PVLogger* iLogger;
289         uint8* iDemuxBuffer;
290         uint8* iDemuxBufferPos;
291         uint32 iDemuxBufferSize;
292         uint32 iSyncCheckCount;
293         bool iSkipLevelCheck;
294         uint8* iIdleSyncCheckBuffer;
295         bool iSyncDetected;
296         bool iDataReceptionStart;
297 };
298 
299 
300 #endif
301