1 /* |
|
2 * Copyright (c) 2002-2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Streams data from Java to controller |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <AudioPreference.h> |
|
21 #include <logger.h> |
|
22 |
|
23 #include "cmmastreamhandler.h" |
|
24 #include "cmmadatasourcestream.h" |
|
25 #include "mmafunctionserver.h" |
|
26 |
|
27 |
|
28 CMMAStreamHandler* CMMAStreamHandler::NewL( |
|
29 MMMAStreamHandlerListener& aListener, |
|
30 MStreamControl& aMStreamControl, |
|
31 MDataBufferSource& aMDataBufferSource, |
|
32 CMultimediaFactory& aFactory, |
|
33 CMetaDataUtility& aMetaDataUtility) |
|
34 { |
|
35 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::NewL +"); |
|
36 CMMAStreamHandler* self = new(ELeave) CMMAStreamHandler(aListener, |
|
37 aMStreamControl, |
|
38 aMDataBufferSource, |
|
39 aFactory, |
|
40 aMetaDataUtility); |
|
41 CleanupStack::PushL(self); |
|
42 self->ConstructL(); |
|
43 CleanupStack::Pop(self); |
|
44 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::NewL -"); |
|
45 return self; |
|
46 } |
|
47 |
|
48 CMMAStreamHandler::~CMMAStreamHandler() |
|
49 { |
|
50 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::~CMMAStreamHandler() +"); |
|
51 iMDataBufferSource.RemoveObserver(*this); |
|
52 if (iBuffer != NULL) |
|
53 { |
|
54 iFactory.DeleteDataBuffer(iBuffer); |
|
55 iBuffer = NULL; |
|
56 } |
|
57 iRequests.ResetAndDestroy(); |
|
58 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::~CMMAStreamHandler() -"); |
|
59 } |
|
60 |
|
61 CMMAStreamHandler::CMMAStreamHandler(MMMAStreamHandlerListener& aListener, |
|
62 MStreamControl& aMStreamControl, |
|
63 MDataBufferSource& aMDataBufferSource, |
|
64 CMultimediaFactory& aFactory, |
|
65 CMetaDataUtility& aMetaDataUtility) |
|
66 :iMStreamControl(aMStreamControl), |
|
67 iMDataBufferSource(aMDataBufferSource), |
|
68 iFactory(aFactory), |
|
69 iListener(aListener), |
|
70 iMetaDataUtility(aMetaDataUtility), |
|
71 // to be removed once MDU supports all the reqd formats |
|
72 iMimeTypeSupportedByMDU(ETrue) |
|
73 { |
|
74 // ignore read/write completed before stream is prepared |
|
75 iState = EMMAStreamPaused; |
|
76 iMetaDataReadyToBeParsed = EFalse; |
|
77 } |
|
78 |
|
79 void CMMAStreamHandler::ConstructL() |
|
80 { |
|
81 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::ConstructL +"); |
|
82 //Register source control to control observer to get the event |
|
83 iMDataBufferSource.AddObserver(*this); |
|
84 iLastBufferWritten = EFalse; |
|
85 iBuffer = NULL; // initialize MDataBuffer type pointer |
|
86 // create requests |
|
87 for (TInt i = 0; i < KMMAStreamHandlerBufferCount; i++) |
|
88 { |
|
89 CMMAStreamRequest* requestToAppend = CMMAStreamRequest::NewLC(this); |
|
90 iRequests.AppendL(requestToAppend); |
|
91 CleanupStack::Pop(requestToAppend); |
|
92 } |
|
93 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::ConstructL -"); |
|
94 } |
|
95 |
|
96 void CMMAStreamHandler::Prepare(const TDesC8& aMimeType) |
|
97 { |
|
98 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Prepare +"); |
|
99 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Prepare state %d", iState); |
|
100 iState = EMMAStreamPrepare; |
|
101 // initialize MetaDataUtility |
|
102 TInt error = iMetaDataUtility.InitChunkData(aMimeType, *this); |
|
103 ELOG1( EJavaMMAPI, "MMA::CMMAStreamHandler::Prepare, error = %d", error); |
|
104 if (error != KErrNone) |
|
105 { |
|
106 if (error == KErrArgument) |
|
107 { |
|
108 // MDU doesn't support aMimeType |
|
109 // can be removed once MDU supports all the reqd formats |
|
110 iMimeTypeSupportedByMDU = EFalse; |
|
111 } |
|
112 else |
|
113 { |
|
114 // MDU supports but some other error occured |
|
115 iListener.PrepareComplete(error); |
|
116 return; |
|
117 } |
|
118 } |
|
119 else |
|
120 { |
|
121 // to be removed once MDU supports all the reqd formats |
|
122 iMimeTypeSupportedByMDU = ETrue; |
|
123 } |
|
124 //reset request data for reading again from beginning |
|
125 iRequests[0]->DataPtr().SetLength(0); |
|
126 |
|
127 // when read completes iListerner.PrepareComplete will be called |
|
128 iSourceStream->Read(iRequests[ 0 ]); |
|
129 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Prepare -"); |
|
130 } |
|
131 |
|
132 void CMMAStreamHandler::Start() |
|
133 { |
|
134 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start +"); |
|
135 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start state %d", iState); |
|
136 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start data source request=%d",(TInt)iSourceStream->Request()); |
|
137 iState = EMMAStreamStart; |
|
138 if (iSourceStream->Request()) |
|
139 { // when read request is completed it will be written to server |
|
140 iState = EMMAStreamStarted; |
|
141 iListener.StartComplete(KErrNone); |
|
142 return; |
|
143 } |
|
144 |
|
145 TInt count = iRequests.Count(); |
|
146 TBool started = EFalse; |
|
147 for (TInt i = 0; i < count && !started; i++) |
|
148 { |
|
149 CMMAStreamRequest* r = iRequests[ i ]; |
|
150 if (!r->IsActive() && r->DataPtr().Length() > 0) |
|
151 { |
|
152 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start write request to server"); |
|
153 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start request length=%d", |
|
154 r->DataPtr().Length()); |
|
155 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start last buffer %d", |
|
156 r->RequestBuffer()()); |
|
157 |
|
158 // data was not yet written to server |
|
159 WriteRequest(r); |
|
160 started = ETrue; |
|
161 } |
|
162 else if (r->IsActive()) // data is in server |
|
163 { |
|
164 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start data is in server"); |
|
165 // atleast one request is not processed |
|
166 started = ETrue; |
|
167 } |
|
168 } |
|
169 if (started) // If allready started complete start |
|
170 { |
|
171 iListener.StartComplete(KErrNone); |
|
172 iState = EMMAStreamStarted; |
|
173 } |
|
174 else |
|
175 { |
|
176 // Need to read data before start |
|
177 iSourceStream->Read(iRequests[ 0 ]); |
|
178 } |
|
179 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Start -"); |
|
180 } |
|
181 |
|
182 void CMMAStreamHandler::Pause() |
|
183 { |
|
184 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Pause +"); |
|
185 // ignore read/write completes |
|
186 iState = EMMAStreamPaused; |
|
187 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Pause -"); |
|
188 } |
|
189 |
|
190 void CMMAStreamHandler::SetSourceStream(CMMADataSourceStream* aSourceStream) |
|
191 { |
|
192 iSourceStream = aSourceStream; |
|
193 } |
|
194 |
|
195 void CMMAStreamHandler::WriteComplete(CMMAStreamRequest* aRequest) |
|
196 { |
|
197 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete +"); |
|
198 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete state=%d", iState); |
|
199 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete request length=%d", |
|
200 aRequest->DataPtr().Length()); |
|
201 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete last buffer %d", |
|
202 aRequest->RequestBuffer()()); |
|
203 if (iState == EMMAStreamStarted) |
|
204 { |
|
205 if (aRequest->RequestBuffer()() == 1) |
|
206 { |
|
207 iState = EMMAStreamEof; |
|
208 } |
|
209 else |
|
210 { |
|
211 iSourceStream->Read(aRequest); |
|
212 } |
|
213 } |
|
214 // else, all other states ignore write complete |
|
215 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete -"); |
|
216 } |
|
217 |
|
218 void CMMAStreamHandler::ReadComplete(CMMAStreamRequest* aRequest) |
|
219 { |
|
220 |
|
221 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::ReadComplete +"); |
|
222 |
|
223 if (iState == EMMAStreamPrepare) |
|
224 { |
|
225 |
|
226 WriteRequest(aRequest); |
|
227 iListener.PrepareComplete(KErrNone); |
|
228 iState = EMMAStreamPaused; |
|
229 } |
|
230 else if (iState == EMMAStreamStart) |
|
231 { |
|
232 iState = EMMAStreamStarted; |
|
233 // write first request to server |
|
234 WriteRequest(aRequest); |
|
235 iListener.StartComplete(KErrNone); |
|
236 } |
|
237 else if (iState == EMMAStreamStarted) |
|
238 { |
|
239 WriteRequest(aRequest); |
|
240 } |
|
241 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::ReadComplete -"); |
|
242 // else, all other states ignore read complete |
|
243 } |
|
244 |
|
245 void CMMAStreamHandler::HandleError(CMMAStreamRequest* /*aRequest*/, |
|
246 TInt aError) |
|
247 { |
|
248 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleError +"); |
|
249 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleError state=%d", iState); |
|
250 if (iState == EMMAStreamPrepare) |
|
251 { |
|
252 iListener.PrepareComplete(aError); |
|
253 iState = EMMAStreamPaused; |
|
254 } |
|
255 else if (iState == EMMAStreamStart) |
|
256 { |
|
257 iListener.StartComplete(aError); |
|
258 iState = EMMAStreamStarted; |
|
259 } |
|
260 else |
|
261 { |
|
262 iListener.HandleError(aError); |
|
263 } |
|
264 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleError -"); |
|
265 } |
|
266 |
|
267 void CMMAStreamHandler::WriteRequest(CMMAStreamRequest* aRequest) |
|
268 { |
|
269 |
|
270 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteRequest +"); |
|
271 iCurrentRequest = aRequest; //initialize the current StreamRequest for use in callback function |
|
272 iProcessedState = EMMANoneProcessed; |
|
273 |
|
274 TInt state = iMStreamControl.GetState(); |
|
275 if (!aRequest->IsActive()) |
|
276 { |
|
277 aRequest->SetActive(ETrue); |
|
278 } |
|
279 |
|
280 //Create MDataBuffer and then pass it as a parameter of WriteData |
|
281 if (iBuffer != NULL) |
|
282 { |
|
283 iFactory.DeleteDataBuffer(iBuffer); |
|
284 iBuffer= NULL; |
|
285 } |
|
286 iFactory.CreateDataBuffer(KDataBufferSourceControl, KMMAStreamRequestBufferSize, iBuffer); |
|
287 iBuffer->GetBufferPtr().Set(aRequest->DataPtr()); |
|
288 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteRequest: Size of the Data to be written is %d ",aRequest->DataPtr().Length()); |
|
289 if (aRequest->RequestBuffer()() == 1) //set that it is the last buffer |
|
290 { |
|
291 iState = EMMAStreamEof; |
|
292 iBuffer->SetLastBuffer(ETrue); |
|
293 iLastBufferWritten = ETrue; |
|
294 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteRequest: LastBuffer"); |
|
295 } |
|
296 else |
|
297 { |
|
298 iBuffer->SetLastBuffer(EFalse); |
|
299 iLastBufferWritten = EFalse; |
|
300 } |
|
301 |
|
302 // When the buffer is processed by framework KBufferProcessedEvent |
|
303 // will be delivered to registered observers |
|
304 TInt err = iMDataBufferSource.WriteData(*iBuffer); |
|
305 // to be removed once MDU supports all the reqd formats |
|
306 if (iMimeTypeSupportedByMDU && !iMetaDataReadyToBeParsed) |
|
307 { |
|
308 err = iMetaDataUtility.ProcessChunkData(aRequest->DataPtr(), iLastBufferWritten); |
|
309 } |
|
310 ELOG1( EJavaMMAPI, "MMA::CMMAStreamHandler::WriteRequest, err = %d", err); |
|
311 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteRequest -"); |
|
312 } |
|
313 |
|
314 void CMMAStreamHandler::Event(MControl* /*aControl*/, TUint aEventType, TAny* aEventObject) |
|
315 { |
|
316 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Event "); |
|
317 switch (aEventType) |
|
318 { |
|
319 |
|
320 case MSourceControlObserver::KBufferProcessedEvent: |
|
321 { |
|
322 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Event:KBufferProcessedEvent"); |
|
323 MBufferProcessedEvent* evt = (MBufferProcessedEvent*)aEventObject; |
|
324 ELOG1( EJavaMMAPI, "MMA::CMMAStreamHandler::Event:KBufferProcessedEvent:ErrorCode = %d ",evt->GetErrorCode()); |
|
325 // can be removed once MDU supports all the reqd formats |
|
326 if (!iMimeTypeSupportedByMDU || |
|
327 (iProcessedState == EMMAMetaDataProcessed || iMetaDataReadyToBeParsed)) |
|
328 { |
|
329 iCurrentRequest->WriteRequestComplete(evt->GetErrorCode()); |
|
330 iCurrentRequest->SetActive(EFalse); |
|
331 iProcessedState = EMMABothProcessed; |
|
332 } |
|
333 else |
|
334 { |
|
335 iProcessedState = EMMABufferProcessed; |
|
336 } |
|
337 } |
|
338 break; |
|
339 |
|
340 case MSourceControlObserver::KBitRateChangedEvent: |
|
341 { |
|
342 } |
|
343 break; |
|
344 |
|
345 case MStreamControlObserver::KDurationChangedEvent: |
|
346 { |
|
347 } |
|
348 break; |
|
349 |
|
350 default: |
|
351 break; |
|
352 |
|
353 }; |
|
354 } |
|
355 |
|
356 void CMMAStreamHandler::HandleChunkDataProcessed(TInt aError) |
|
357 { |
|
358 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleChunkDataProcessed + "); |
|
359 ELOG1( EJavaMMAPI, "MMA::CMMAStreamHandler::HandleChunkDataProcessed, aError = %d", aError); |
|
360 if (iProcessedState == EMMABufferProcessed) |
|
361 { |
|
362 iCurrentRequest->WriteRequestComplete(aError); |
|
363 iCurrentRequest->SetActive(EFalse); |
|
364 iProcessedState = EMMABothProcessed; |
|
365 } |
|
366 else |
|
367 { |
|
368 iProcessedState = EMMAMetaDataProcessed; |
|
369 } |
|
370 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleChunkDataProcessed - "); |
|
371 } |
|
372 |
|
373 void CMMAStreamHandler::HandleChunkDataReadyToBeParsed() |
|
374 { |
|
375 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleChunkDataReadyToBeParsed + "); |
|
376 iMetaDataReadyToBeParsed = ETrue; |
|
377 TInt error = iMetaDataUtility.ParseChunkData(); |
|
378 ELOG1( EJavaMMAPI, "MMA::CMMAStreamHandler::HandleChunkDataReadyToBeParsed, error = %d ", error); |
|
379 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleChunkDataReadyToBeParsed - "); |
|
380 } |
|
381 |
|
382 void CMMAStreamHandler::HandleChunkDataComplete(TInt aError) |
|
383 { |
|
384 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleChunkDataComplete"); |
|
385 if (KErrNone != aError) |
|
386 { |
|
387 ELOG1( EJavaMMAPI, "MMA::CMMAStreamHandler::HandleChunkDataComplete, aError = %d ", aError); |
|
388 iListener.HandleError(aError); |
|
389 } |
|
390 |
|
391 } |
|
392 |
|
393 TBool CMMAStreamHandler::LastBufferWritten() |
|
394 { |
|
395 return iLastBufferWritten; |
|
396 } |
|
397 |
|
398 void CMMAStreamHandler::Stop() |
|
399 { |
|
400 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Stop + "); |
|
401 // forcefully complete all requests |
|
402 // and release already read data. |
|
403 Pause(); |
|
404 iMetaDataUtility.CloseChunkData(); |
|
405 TInt count = iRequests.Count(); |
|
406 for (TInt i = 0; i < count; i++) |
|
407 { |
|
408 CMMAStreamRequest* r = iRequests[ i ]; |
|
409 r->WriteRequestComplete(KErrNone); |
|
410 r->SetActive(EFalse); |
|
411 } |
|
412 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::Stop - "); |
|
413 } |
|
414 |
|
415 |
|
416 // END OF FILE |
|