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 #include "pvmf_buffer_data_source.h"
19
PVMFBufferDataSource(int32 aPortTag,unsigned bitrate,unsigned min_sample_sz,unsigned max_sample_sz,uint8 * fsi,unsigned fsi_len)20 OSCL_EXPORT_REF PVMFBufferDataSource::PVMFBufferDataSource(int32 aPortTag,
21 unsigned bitrate,
22 unsigned min_sample_sz,
23 unsigned max_sample_sz,
24 uint8* fsi,
25 unsigned fsi_len):
26 PvmfPortBaseImpl(aPortTag, this),
27 iTimer("PVMFBufferDataSource"),
28 iMediaDataAlloc(NULL),
29 iFsi(NULL),
30 iFsiLen(0)
31 {
32 iBitrate = bitrate;
33 iMinSampleSz = min_sample_sz;
34 iMaxSampleSz = max_sample_sz;
35 iTimestamp = 0;
36 iSampleInterval = 0;
37 if (fsi && fsi_len)
38 {
39 iFsi = (uint8*)OSCL_DEFAULT_MALLOC(fsi_len);
40 oscl_memcpy(iFsi, fsi, fsi_len);
41 iFsiLen = fsi_len;
42 }
43 }
44
~PVMFBufferDataSource()45 OSCL_EXPORT_REF PVMFBufferDataSource::~PVMFBufferDataSource()
46 {
47 Stop();
48 if (iMediaDataAlloc)
49 {
50 OSCL_DELETE(iMediaDataAlloc);
51 iMediaDataAlloc = NULL;
52 }
53 if (iFsi)
54 {
55 OSCL_DEFAULT_FREE(iFsi);
56 iFsi = NULL;
57 }
58 }
59
HandlePortActivity(const PVMFPortActivity & aActivity)60 void PVMFBufferDataSource::HandlePortActivity(const PVMFPortActivity &aActivity)
61 {
62 if (aActivity.iType != PVMF_PORT_ACTIVITY_OUTGOING_MSG)
63 return;
64 PVMFSharedMediaMsgPtr aMsg;
65 while (OutgoingMsgQueueSize())
66 {
67 Send();
68 }
69 }
70
Start()71 OSCL_EXPORT_REF void PVMFBufferDataSource::Start()
72 {
73 iMediaDataAlloc = OSCL_NEW(PVMFSimpleMediaBufferCombinedAlloc, (&iMemAlloc));
74 unsigned ave_sample_sz = (iMinSampleSz + iMaxSampleSz) / 2;
75 unsigned frequency = (iBitrate >> 3) / ave_sample_sz;
76 iSampleInterval = 1000 / frequency;
77 iTimer .SetFrequency(frequency);
78 iTimer.SetObserver(this);
79 iTimer.Request(1/*timer id*/, ave_sample_sz/*timer info*/ , 1/*num ticks*/, this, 1/*recurring*/);
80 }
81
Stop()82 OSCL_EXPORT_REF void PVMFBufferDataSource::Stop()
83 {
84 iTimer.Clear();
85 Disconnect();
86 }
87
TimeoutOccurred(int32 timerID,int32 timeoutInfo)88 void PVMFBufferDataSource::TimeoutOccurred(int32 timerID, int32 timeoutInfo)
89 {
90 OSCL_UNUSED_ARG(timerID);
91 unsigned bytesToSend = timeoutInfo;
92 if (bytesToSend <= 0)
93 return;
94
95 if (!IsConnected())
96 return;
97
98 // Create new media data buffer
99 OsclSharedPtr<PVMFMediaDataImpl> mediaDataImpl = iMediaDataAlloc->allocate(bytesToSend);
100 PVMFSharedMediaDataPtr mediaData;
101 int leavecode = 0;
102 OSCL_TRY(leavecode, mediaData = PVMFMediaData::createMediaData(mediaDataImpl));
103 OSCL_FIRST_CATCH_ANY(leavecode, return);
104
105 // Send FSI if available
106 if (iFsi)
107 {
108 OsclSharedPtr<PVMFMediaDataImpl> fsiMediaDataImpl = iMediaDataAlloc->allocate(iFsiLen);
109 PVMFSharedMediaDataPtr fsiMediaData;
110 OSCL_TRY(leavecode, fsiMediaData = PVMFMediaData::createMediaData(fsiMediaDataImpl));
111 OSCL_FIRST_CATCH_ANY(leavecode, return);
112 OsclRefCounterMemFrag fsi_frag;
113 fsiMediaData->getMediaFragment(0, fsi_frag);
114 oscl_memcpy((uint8*)fsi_frag.getMemFragPtr(), iFsi, iFsiLen);
115 fsi_frag.getMemFrag().len = iFsiLen;
116 mediaData->setFormatSpecificInfo(fsi_frag);
117 OSCL_DEFAULT_FREE(iFsi);
118 iFsi = NULL;
119 iFsiLen = 0;
120 }
121
122 // Retrieve memory fragment to write to
123 OsclRefCounterMemFrag refCtrMemFrag;
124 mediaData->getMediaFragment(0, refCtrMemFrag);
125 if (refCtrMemFrag.getCapacity() < bytesToSend)
126 return;
127
128 oscl_memset((uint8*)refCtrMemFrag.getMemFragPtr(), 7, bytesToSend);
129 mediaDataImpl->setMediaFragFilledLen(0, bytesToSend);
130 mediaData->setTimestamp(iTimestamp);
131 iTimestamp += iSampleInterval;
132
133 // Send frame to downstream node
134 PVMFSharedMediaMsgPtr mediaMsg;
135 convertToPVMFMediaMsg(mediaMsg, mediaData);
136 QueueOutgoingMsg(mediaMsg);
137 }
138
139 // PVMFPortInterface virtuals
140
PutData(PVMFSharedMediaMsgPtr aMsg)141 PVMFStatus PVMFBufferDataSource::PutData(PVMFSharedMediaMsgPtr aMsg)
142 {
143 OSCL_UNUSED_ARG(aMsg);
144 return PVMFSuccess;
145 }
146
GetData(PVMFSharedMediaMsgPtr aMsg)147 PVMFStatus PVMFBufferDataSource::GetData(PVMFSharedMediaMsgPtr aMsg)
148 {
149 OSCL_UNUSED_ARG(aMsg);
150 return PVMFSuccess;
151 }
152
setObserver(PvmiConfigAndCapabilityCmdObserver * aObserver)153 OSCL_EXPORT_REF void PVMFBufferDataSource::setObserver(PvmiConfigAndCapabilityCmdObserver* aObserver)
154 {
155 OSCL_UNUSED_ARG(aObserver);
156 }
157
getParametersSync(PvmiMIOSession aSession,PvmiKeyType aIdentifier,PvmiKvp * & aParameters,int & num_parameter_elements,PvmiCapabilityContext aContext)158 OSCL_EXPORT_REF PVMFStatus PVMFBufferDataSource::getParametersSync(PvmiMIOSession aSession, PvmiKeyType aIdentifier,
159 PvmiKvp*& aParameters, int& num_parameter_elements,
160 PvmiCapabilityContext aContext)
161 {
162 OSCL_UNUSED_ARG(aSession);
163 OSCL_UNUSED_ARG(aIdentifier);
164 OSCL_UNUSED_ARG(aParameters);
165 OSCL_UNUSED_ARG(num_parameter_elements);
166 OSCL_UNUSED_ARG(aContext);
167
168 return PVMFSuccess;
169 }
170
releaseParameters(PvmiMIOSession aSession,PvmiKvp * aParameters,int num_elements)171 OSCL_EXPORT_REF PVMFStatus PVMFBufferDataSource::releaseParameters(PvmiMIOSession aSession, PvmiKvp* aParameters, int num_elements)
172 {
173 OSCL_UNUSED_ARG(aSession);
174 OSCL_UNUSED_ARG(aParameters);
175 OSCL_UNUSED_ARG(num_elements);
176
177 return PVMFSuccess;
178 }
179
createContext(PvmiMIOSession aSession,PvmiCapabilityContext & aContext)180 OSCL_EXPORT_REF void PVMFBufferDataSource::createContext(PvmiMIOSession aSession, PvmiCapabilityContext& aContext)
181 {
182 OSCL_UNUSED_ARG(aSession);
183 OSCL_UNUSED_ARG(aContext);
184 }
185
setContextParameters(PvmiMIOSession aSession,PvmiCapabilityContext & aContext,PvmiKvp * aParameters,int num_parameter_elements)186 OSCL_EXPORT_REF void PVMFBufferDataSource::setContextParameters(PvmiMIOSession aSession, PvmiCapabilityContext& aContext,
187 PvmiKvp* aParameters, int num_parameter_elements)
188 {
189 OSCL_UNUSED_ARG(aSession);
190 OSCL_UNUSED_ARG(aContext);
191 OSCL_UNUSED_ARG(aParameters);
192 OSCL_UNUSED_ARG(num_parameter_elements);
193 }
194
DeleteContext(PvmiMIOSession aSession,PvmiCapabilityContext & aContext)195 OSCL_EXPORT_REF void PVMFBufferDataSource::DeleteContext(PvmiMIOSession aSession, PvmiCapabilityContext& aContext)
196 {
197 OSCL_UNUSED_ARG(aSession);
198 OSCL_UNUSED_ARG(aContext);
199 }
200
setParametersSync(PvmiMIOSession aSession,PvmiKvp * aParameters,int num_elements,PvmiKvp * & aRet_kvp)201 OSCL_EXPORT_REF void PVMFBufferDataSource::setParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters,
202 int num_elements, PvmiKvp * & aRet_kvp)
203 {
204 OSCL_UNUSED_ARG(aSession);
205 OSCL_UNUSED_ARG(aParameters);
206 OSCL_UNUSED_ARG(num_elements);
207 OSCL_UNUSED_ARG(aRet_kvp);
208 }
209
210
setParametersAsync(PvmiMIOSession aSession,PvmiKvp * aParameters,int num_elements,PvmiKvp * & aRet_kvp,OsclAny * context)211 OSCL_EXPORT_REF PVMFCommandId PVMFBufferDataSource::setParametersAsync(PvmiMIOSession aSession, PvmiKvp* aParameters,
212 int num_elements, PvmiKvp*& aRet_kvp, OsclAny* context)
213 {
214 OSCL_UNUSED_ARG(aSession);
215 OSCL_UNUSED_ARG(aParameters);
216 OSCL_UNUSED_ARG(num_elements);
217 OSCL_UNUSED_ARG(aRet_kvp);
218 OSCL_UNUSED_ARG(context);
219
220 OSCL_LEAVE(OsclErrNotSupported);
221 return -1;
222 }
223
getCapabilityMetric(PvmiMIOSession aSession)224 OSCL_EXPORT_REF uint32 PVMFBufferDataSource::getCapabilityMetric(PvmiMIOSession aSession)
225 {
226 OSCL_UNUSED_ARG(aSession);
227
228 return 1;
229 }
230
verifyParametersSync(PvmiMIOSession aSession,PvmiKvp * aParameters,int num_elements)231 OSCL_EXPORT_REF PVMFStatus PVMFBufferDataSource::verifyParametersSync(PvmiMIOSession aSession, PvmiKvp* aParameters, int num_elements)
232 {
233 OSCL_UNUSED_ARG(aSession);
234 OSCL_UNUSED_ARG(aParameters);
235 OSCL_UNUSED_ARG(num_elements);
236
237 OSCL_LEAVE(OsclErrNotSupported);
238 return PVMFFailure;
239 }
240
QueryInterface(const PVUuid & aUuid,OsclAny * & aPtr)241 OSCL_EXPORT_REF void PVMFBufferDataSource::QueryInterface(const PVUuid& aUuid, OsclAny*& aPtr)
242 {
243 aPtr = NULL;
244 if (aUuid == PVMI_CAPABILITY_AND_CONFIG_PVUUID)
245 {
246 aPtr = (PvmiCapabilityAndConfig*)this;
247 }
248 else
249 {
250 OSCL_LEAVE(OsclErrNotSupported);
251 }
252 }
253