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 <mmf/server/mmfdes.h> |
|
21 #include <audiopreference.h> |
|
22 #include <logger.h> |
|
23 |
|
24 #include "CMMAStreamHandler.h" |
|
25 #include "CMMADataSourceStream.h" |
|
26 #include "MMAFunctionServer.h" |
|
27 |
|
28 CMMAStreamHandler* CMMAStreamHandler::NewL( |
|
29 MMMAStreamHandlerListener& aListener, |
|
30 RMMFController& aController) |
|
31 { |
|
32 CMMAStreamHandler* self = new(ELeave) CMMAStreamHandler(aListener, |
|
33 aController); |
|
34 self->ConstructL(); |
|
35 return self; |
|
36 } |
|
37 |
|
38 CMMAStreamHandler::~CMMAStreamHandler() |
|
39 { |
|
40 iRequests.ResetAndDestroy(); |
|
41 } |
|
42 |
|
43 CMMAStreamHandler::CMMAStreamHandler(MMMAStreamHandlerListener& aListener, |
|
44 RMMFController& aController): |
|
45 iController(aController), |
|
46 iListener(aListener) |
|
47 { |
|
48 // ignore read/write completed before stream is prepared |
|
49 iState = EMMAStreamPaused; |
|
50 } |
|
51 |
|
52 void CMMAStreamHandler::ConstructL() |
|
53 { |
|
54 // create requests |
|
55 for (TInt i = 0; i < KMMAStreamHandlerBufferCount; i++) |
|
56 { |
|
57 CMMAStreamRequest* requestToAppend = CMMAStreamRequest::NewLC(this); |
|
58 iRequests.AppendL(requestToAppend); |
|
59 CleanupStack::Pop(requestToAppend); |
|
60 } |
|
61 } |
|
62 |
|
63 void CMMAStreamHandler::PrepareL() |
|
64 { |
|
65 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::PrepareL state %d", iState); |
|
66 iState = EMMAStreamPrepare; |
|
67 |
|
68 //reset request data for reading again from beginning |
|
69 iRequests[0]->DataPtr().SetLength(0); |
|
70 |
|
71 // when read completes iListerner.PrepareComplete will be called |
|
72 iSourceStream->Read(iRequests[ 0 ]); |
|
73 } |
|
74 |
|
75 void CMMAStreamHandler::StartL() |
|
76 { |
|
77 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::StartL state %d", iState); |
|
78 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::StartL data source request=%d", |
|
79 (TInt)iSourceStream->Request()); |
|
80 |
|
81 iState = EMMAStreamStart; |
|
82 if (iSourceStream->Request()) |
|
83 { // when read request is completed it will be written to server |
|
84 iState = EMMAStreamStarted; |
|
85 iListener.StartComplete(KErrNone); |
|
86 return; |
|
87 } |
|
88 |
|
89 TInt count = iRequests.Count(); |
|
90 TBool started = EFalse; |
|
91 for (TInt i = 0; i < count && !started; i++) |
|
92 { |
|
93 CMMAStreamRequest* r = iRequests[ i ]; |
|
94 if (!r->IsActive() && r->DataPtr().Length() > 0) |
|
95 { |
|
96 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::StartL write request to server"); |
|
97 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::StartL request length=%d", |
|
98 r->DataPtr().Length()); |
|
99 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::StartL last buffer %d", |
|
100 r->RequestBuffer()()); |
|
101 |
|
102 // data was not yet written to server |
|
103 WriteRequest(r); |
|
104 started = ETrue; |
|
105 } |
|
106 else if (r->IsActive()) // data is in server |
|
107 { |
|
108 LOG( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::StartL data is in server"); |
|
109 // atleast one request is not processed |
|
110 started = ETrue; |
|
111 } |
|
112 } |
|
113 if (started) // If allready started complete start |
|
114 { |
|
115 iListener.StartComplete(KErrNone); |
|
116 iState = EMMAStreamStarted; |
|
117 } |
|
118 else |
|
119 { |
|
120 // Need to read data before start |
|
121 iSourceStream->Read(iRequests[ 0 ]); |
|
122 } |
|
123 } |
|
124 |
|
125 void CMMAStreamHandler::Pause() |
|
126 { |
|
127 // ignore read/write completes |
|
128 iState = EMMAStreamPaused; |
|
129 } |
|
130 |
|
131 TMMFMessageDestination& CMMAStreamHandler::MessageDestination() |
|
132 { |
|
133 return iDataSourceHandle; |
|
134 } |
|
135 |
|
136 void CMMAStreamHandler::SetSourceStream(CMMADataSourceStream* aSourceStream) |
|
137 { |
|
138 iSourceStream = aSourceStream; |
|
139 } |
|
140 |
|
141 void CMMAStreamHandler::WriteComplete(CMMAStreamRequest* aRequest) |
|
142 { |
|
143 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete state=%d", iState); |
|
144 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete request length=%d", |
|
145 aRequest->DataPtr().Length()); |
|
146 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::WriteComplete last buffer %d", |
|
147 aRequest->RequestBuffer()()); |
|
148 if (iState == EMMAStreamStarted) |
|
149 { |
|
150 if (aRequest->RequestBuffer()() == 1) |
|
151 { |
|
152 iState = EMMAStreamEof; |
|
153 } |
|
154 else |
|
155 { |
|
156 iSourceStream->Read(aRequest); |
|
157 } |
|
158 } |
|
159 // else, all other states ignore write complete |
|
160 } |
|
161 |
|
162 void CMMAStreamHandler::ReadComplete(CMMAStreamRequest* aRequest) |
|
163 { |
|
164 if (iState == EMMAStreamPrepare) |
|
165 { |
|
166 WriteRequest(aRequest); |
|
167 iListener.PrepareComplete(KErrNone); |
|
168 iState = EMMAStreamPaused; |
|
169 } |
|
170 else if (iState == EMMAStreamStart) |
|
171 { |
|
172 iState = EMMAStreamStarted; |
|
173 // write first request to server |
|
174 WriteRequest(aRequest); |
|
175 iListener.StartComplete(KErrNone); |
|
176 } |
|
177 else if (iState == EMMAStreamStarted) |
|
178 { |
|
179 WriteRequest(aRequest); |
|
180 } |
|
181 // else, all other states ignore read complete |
|
182 } |
|
183 |
|
184 void CMMAStreamHandler::HandleError(CMMAStreamRequest* /*aRequest*/, |
|
185 TInt aError) |
|
186 { |
|
187 LOG1( EJavaMMAPI, EInfo, "MMA::CMMAStreamHandler::HandleError state=%d", iState); |
|
188 if (iState == EMMAStreamPrepare) |
|
189 { |
|
190 iListener.PrepareComplete(aError); |
|
191 iState = EMMAStreamPaused; |
|
192 } |
|
193 else if (iState == EMMAStreamStart) |
|
194 { |
|
195 iListener.StartComplete(aError); |
|
196 iState = EMMAStreamStarted; |
|
197 } |
|
198 else |
|
199 { |
|
200 iListener.HandleError(aError); |
|
201 } |
|
202 } |
|
203 |
|
204 void CMMAStreamHandler::WriteRequest(CMMAStreamRequest* aRequest) |
|
205 { |
|
206 if (aRequest->RequestBuffer()() == 1) |
|
207 { |
|
208 iState = EMMAStreamEof; |
|
209 } |
|
210 |
|
211 if (!aRequest->IsActive()) |
|
212 { |
|
213 aRequest->SetActive(); |
|
214 } |
|
215 |
|
216 // Send write request to server, HandleRequestCompleteL is called when finished |
|
217 iController.CustomCommandAsync(iDataSourceHandle, |
|
218 1, /*EProcessBuffer*/ |
|
219 aRequest->DataPtr(), |
|
220 aRequest->RequestBuffer(), |
|
221 aRequest->iStatus); |
|
222 } |
|
223 // END OF FILE |
|