|
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: Class for reading data from Java SourceStream to native side |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <logger.h> |
|
19 |
|
20 #include <JniEnvWrapper.h> |
|
21 |
|
22 #include "mmafunctionserver.h" |
|
23 #include "cmmasourcestream.h" |
|
24 #include "cmmasourcestreamevent.h" |
|
25 #include "mmmasourcestreamlistener.h" |
|
26 |
|
27 const TInt KBufferSize = 4096; // data is read from java in 4k pieces |
|
28 |
|
29 // CONSTRUCTION |
|
30 CMMASourceStream* CMMASourceStream::NewL(JNIEnv* aJNIEnv, |
|
31 MMMAEventPoster* aEventPoster, |
|
32 jobject aJavaSourceStream, |
|
33 MMMASourceStreamListener* aListener |
|
34 ) |
|
35 { |
|
36 CMMASourceStream* self = CMMASourceStream::NewLC(aJNIEnv, |
|
37 aEventPoster, |
|
38 aJavaSourceStream, |
|
39 aListener); |
|
40 CleanupStack::Pop(); |
|
41 return self; |
|
42 } |
|
43 |
|
44 |
|
45 CMMASourceStream* CMMASourceStream::NewLC(JNIEnv* aJNIEnv, |
|
46 MMMAEventPoster* aEventPoster, |
|
47 jobject aJavaSourceStream, |
|
48 MMMASourceStreamListener* aListener |
|
49 ) |
|
50 { |
|
51 CMMASourceStream* self = new(ELeave) CMMASourceStream(aEventPoster, |
|
52 aListener); |
|
53 CleanupStack::PushL(self); |
|
54 self->ConstructL(aJNIEnv, aJavaSourceStream); |
|
55 return self; |
|
56 } |
|
57 |
|
58 |
|
59 CMMASourceStream::~CMMASourceStream() |
|
60 { |
|
61 LOG(EJavaMMAPI, EInfo, "MMA::CMMASourceStream::~"); |
|
62 delete iData; |
|
63 delete iReadEvent; |
|
64 } |
|
65 |
|
66 |
|
67 // Default constructor |
|
68 CMMASourceStream::CMMASourceStream(MMMAEventPoster* aEventPoster, |
|
69 MMMASourceStreamListener* aListener) |
|
70 : iEventPoster(aEventPoster), iListener(aListener) |
|
71 { |
|
72 } |
|
73 |
|
74 |
|
75 void CMMASourceStream::ConstructL(JNIEnv* aJNIEnv, |
|
76 jobject aJavaSourceStream) |
|
77 { |
|
78 // JNI interface pointer can't be passed to different thread, so |
|
79 // it is needed to get valid JNI interface pointer for Event Server thread |
|
80 aJNIEnv = iEventPoster->getValidJniEnv(); |
|
81 jmethodID classMethodID = |
|
82 aJNIEnv->GetMethodID(aJNIEnv->GetObjectClass(aJavaSourceStream), |
|
83 "read", |
|
84 "(II)V"); |
|
85 if (!classMethodID) |
|
86 { |
|
87 User::Leave(KErrNoMemory); |
|
88 } |
|
89 LOG(EJavaMMAPI, EInfo, "CMMASourceStream::ConstructL , jmethodId found"); |
|
90 jobject apeer = aJNIEnv->NewGlobalRef(aJavaSourceStream); |
|
91 iReadEvent = new(ELeave) CMMASourceStreamEvent(classMethodID, |
|
92 apeer); |
|
93 } |
|
94 |
|
95 |
|
96 void CMMASourceStream::WriteL(const TUint8* aData, |
|
97 TInt aLength, |
|
98 TInt aState) |
|
99 { |
|
100 LOG1(EJavaMMAPI, EInfo, "MMA::CMMASourceStream::WriteL() %d",aState); |
|
101 if (aState < KErrNone) |
|
102 { |
|
103 LOG(EJavaMMAPI, EInfo, "MMA::CMMASourceStream::WriteL : 1"); |
|
104 // Inform listener on error case |
|
105 iListener->ReadCompletedL(aState, KNullDesC8()); |
|
106 return; |
|
107 } |
|
108 |
|
109 |
|
110 if ((iData->Size() + aLength) > iData->Des().MaxSize()) |
|
111 { |
|
112 // if data doesn't fit, reallocate more |
|
113 HBufC8* reallocated = iData->ReAlloc(iData->Des().MaxSize() + KBufferSize); |
|
114 if (!reallocated) // realloc failed |
|
115 { |
|
116 // probably too much data read, so freeing used memory |
|
117 delete iData; |
|
118 iData = NULL; |
|
119 iListener->ReadCompletedL(KErrNoMemory, KNullDesC8()); |
|
120 return; |
|
121 } |
|
122 iData = reallocated; |
|
123 } |
|
124 |
|
125 // java returns length -1 when completed |
|
126 if (aLength > 0) |
|
127 { |
|
128 iData->Des().Append(aData, aLength); |
|
129 } |
|
130 |
|
131 TInt currentRead = iData->Size() - iBufferPosition; |
|
132 |
|
133 if ((aState == ECompleted) || |
|
134 (currentRead == iReadLength)) |
|
135 { |
|
136 if (iReadLength == KMMAReadAllData) |
|
137 { |
|
138 iListener->ReadCompletedL(aState, *iData); |
|
139 } |
|
140 else |
|
141 { |
|
142 TPtrC8 data = iData->Mid(iBufferPosition, currentRead); |
|
143 iListener->ReadCompletedL(aState, data); |
|
144 } |
|
145 } |
|
146 else |
|
147 { |
|
148 // phase ready, inform the Player |
|
149 if (iReadLength == KMMAReadAllData) |
|
150 { |
|
151 iReadEvent->SetLength(KBufferSize); |
|
152 } |
|
153 else |
|
154 { |
|
155 iReadEvent->SetLength(iReadLength - currentRead); |
|
156 } |
|
157 |
|
158 } |
|
159 |
|
160 LOG(EJavaMMAPI, EInfo, "MMA::CMMASourceStream::WriteL completed"); |
|
161 } |
|
162 |
|
163 void CMMASourceStream::ReadL(TInt aLength) |
|
164 { |
|
165 LOG1(EJavaMMAPI, EInfo, "MMA::CMMASourceStream::ReadL(%d)", aLength); |
|
166 |
|
167 __ASSERT_DEBUG(iData != NULL, User::Invariant()); |
|
168 |
|
169 PrepareReadL(); |
|
170 |
|
171 iBufferPosition = iData->Size(); |
|
172 iReadLength = aLength; |
|
173 |
|
174 iReadEvent->SetLength(aLength); |
|
175 |
|
176 // data has been requested, note will be sent |
|
177 iEventPoster->PostEvent(iReadEvent, CMMAEvent::ENotifyPriority); |
|
178 } |
|
179 |
|
180 |
|
181 void CMMASourceStream::ReadAllL() |
|
182 { |
|
183 // not reading again if iData already has data |
|
184 if (iData == NULL) |
|
185 { |
|
186 LOG(EJavaMMAPI, EInfo, "CMMASourceStream::ReadAllL: Buffer empty, reading from java"); |
|
187 CreateDataBufferL(KBufferSize); |
|
188 ReadL(KMMAReadAllData); |
|
189 } |
|
190 else |
|
191 { |
|
192 LOG(EJavaMMAPI, EInfo, "CMMASourceStream::ReadAllL: Buffer not empty, ->ReadCompleteL"); |
|
193 iListener->ReadCompletedL(ECompleted, *iData); |
|
194 } |
|
195 } |
|
196 |
|
197 void CMMASourceStream::PrepareReadL() |
|
198 { |
|
199 } |
|
200 |
|
201 void CMMASourceStream::ResetData() |
|
202 { |
|
203 delete iData; |
|
204 iData = NULL; |
|
205 } |
|
206 |
|
207 void CMMASourceStream::CreateDataBufferL(TInt aBufferSize) |
|
208 { |
|
209 LOG(EJavaMMAPI, EInfo, "CMMASourceStream::CreateDataBufferL +"); |
|
210 __ASSERT_DEBUG(iData == NULL, User::Invariant()); |
|
211 |
|
212 iData = HBufC8::NewL(aBufferSize); |
|
213 } |
|
214 |
|
215 // END OF FILE |