sl@0
|
1 |
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
//
|
sl@0
|
15 |
|
sl@0
|
16 |
|
sl@0
|
17 |
// INCLUDE FILES
|
sl@0
|
18 |
#include <e32base.h>
|
sl@0
|
19 |
#include <f32file.h>
|
sl@0
|
20 |
#include "mp4atom.h"
|
sl@0
|
21 |
#include "filewriter.h"
|
sl@0
|
22 |
|
sl@0
|
23 |
// MACROS
|
sl@0
|
24 |
// Debug print macro
|
sl@0
|
25 |
#ifdef _DEBUG
|
sl@0
|
26 |
#include <e32svr.h>
|
sl@0
|
27 |
//#define PRINT(x) RDebug::Print x
|
sl@0
|
28 |
#define PRINT(x)
|
sl@0
|
29 |
#else
|
sl@0
|
30 |
#define PRINT(x)
|
sl@0
|
31 |
#endif
|
sl@0
|
32 |
|
sl@0
|
33 |
// ============================ MEMBER FUNCTIONS ===============================
|
sl@0
|
34 |
|
sl@0
|
35 |
// -----------------------------------------------------------------------------
|
sl@0
|
36 |
// CFileWriter::CFileWriter
|
sl@0
|
37 |
// C++ default constructor can NOT contain any code, that
|
sl@0
|
38 |
// might leave.
|
sl@0
|
39 |
// -----------------------------------------------------------------------------
|
sl@0
|
40 |
//
|
sl@0
|
41 |
CFileWriter::CFileWriter( TInt aInitSetSize, TInt aOutputBufferSizeSmall, TInt aOutputBufferSizeLarge ):
|
sl@0
|
42 |
CActive( EPriorityHigh ),
|
sl@0
|
43 |
iSetSize( aInitSetSize ),
|
sl@0
|
44 |
iOutputBufferSizeSmall( aOutputBufferSizeSmall ),
|
sl@0
|
45 |
iOutputBufferSizeLarge( aOutputBufferSizeLarge )
|
sl@0
|
46 |
{
|
sl@0
|
47 |
}
|
sl@0
|
48 |
|
sl@0
|
49 |
// -----------------------------------------------------------------------------
|
sl@0
|
50 |
// CFileWriter::ConstructL
|
sl@0
|
51 |
// Symbian 2nd phase constructor can leave.
|
sl@0
|
52 |
// -----------------------------------------------------------------------------
|
sl@0
|
53 |
//
|
sl@0
|
54 |
void CFileWriter::ConstructL( RFile64& aFile )
|
sl@0
|
55 |
{
|
sl@0
|
56 |
PRINT((_L("CFileWriter::ConstructL() in")));
|
sl@0
|
57 |
iFlush = EFalse;
|
sl@0
|
58 |
iError = KErrNone;
|
sl@0
|
59 |
|
sl@0
|
60 |
iOutputFile = &aFile;
|
sl@0
|
61 |
iWritingStarted = EFalse;
|
sl@0
|
62 |
iOutputBufferSize = KFileWriterBufferSizeSmall;
|
sl@0
|
63 |
iMaxOutputBufHardLimit = KFileWriterHardBufLimit;
|
sl@0
|
64 |
iMaxOutputBufSoftLimit = KFileWriterSoftBufLimit;
|
sl@0
|
65 |
|
sl@0
|
66 |
iMemReadyForWriting = EFalse;
|
sl@0
|
67 |
iInputBuf = NULL;
|
sl@0
|
68 |
iEmptyBufferQueue.Reset();
|
sl@0
|
69 |
iFullBufferQueue.Reset();
|
sl@0
|
70 |
|
sl@0
|
71 |
AllocateBuffersL();
|
sl@0
|
72 |
CActiveScheduler::Add(this);
|
sl@0
|
73 |
PRINT((_L("CFileWriter::ConstructL() out")));
|
sl@0
|
74 |
}
|
sl@0
|
75 |
|
sl@0
|
76 |
// -----------------------------------------------------------------------------
|
sl@0
|
77 |
// CFileWriter::NewL
|
sl@0
|
78 |
// Two-phased constructor.
|
sl@0
|
79 |
// -----------------------------------------------------------------------------
|
sl@0
|
80 |
//
|
sl@0
|
81 |
CFileWriter* CFileWriter::NewL( RFile64& aFile, TInt aInitSetSize, TInt aOutputBufferSizeSmall, TInt aOutputBufferSizeLarge )
|
sl@0
|
82 |
{
|
sl@0
|
83 |
CFileWriter* self = new(ELeave) CFileWriter( aInitSetSize, aOutputBufferSizeSmall, aOutputBufferSizeLarge );
|
sl@0
|
84 |
CleanupStack::PushL(self);
|
sl@0
|
85 |
self->ConstructL( aFile );
|
sl@0
|
86 |
CleanupStack::Pop(self);
|
sl@0
|
87 |
return self;
|
sl@0
|
88 |
}
|
sl@0
|
89 |
|
sl@0
|
90 |
|
sl@0
|
91 |
// Destructor
|
sl@0
|
92 |
CFileWriter::~CFileWriter()
|
sl@0
|
93 |
{
|
sl@0
|
94 |
PRINT((_L("CFileWriter::~CFileWriter() in")));
|
sl@0
|
95 |
if ( IsActive() )
|
sl@0
|
96 |
{
|
sl@0
|
97 |
if ( iAsyncWritingOngoing )
|
sl@0
|
98 |
{
|
sl@0
|
99 |
Cancel();
|
sl@0
|
100 |
}
|
sl@0
|
101 |
else
|
sl@0
|
102 |
{
|
sl@0
|
103 |
TRequestStatus* status = &iStatus;
|
sl@0
|
104 |
User::RequestComplete( status, KErrNone );
|
sl@0
|
105 |
Cancel();
|
sl@0
|
106 |
}
|
sl@0
|
107 |
}
|
sl@0
|
108 |
|
sl@0
|
109 |
if ( iInputBuf )
|
sl@0
|
110 |
{
|
sl@0
|
111 |
delete iInputBuf;
|
sl@0
|
112 |
}
|
sl@0
|
113 |
|
sl@0
|
114 |
iEmptyBufferQueue.ResetAndDestroy();
|
sl@0
|
115 |
iFullBufferQueue.ResetAndDestroy();
|
sl@0
|
116 |
PRINT((_L("CFileWriter::~CFileWriter() out")));
|
sl@0
|
117 |
}
|
sl@0
|
118 |
|
sl@0
|
119 |
// -----------------------------------------------------------------------------
|
sl@0
|
120 |
// CFileWriter::UpdateOutputFileSize()
|
sl@0
|
121 |
// Updates output file size and reserves extra space for following writing
|
sl@0
|
122 |
// if iSetSize is set.
|
sl@0
|
123 |
// Takes into account if the position in the file was changed.
|
sl@0
|
124 |
// -----------------------------------------------------------------------------
|
sl@0
|
125 |
//
|
sl@0
|
126 |
void CFileWriter::UpdateOutputFileSize()
|
sl@0
|
127 |
{
|
sl@0
|
128 |
TInt64 pos = 0;
|
sl@0
|
129 |
PRINT((_L("e_cfilewriter_write_updateoutputfilesize_seek 1")));
|
sl@0
|
130 |
iOutputFile->Seek(ESeekCurrent, pos);
|
sl@0
|
131 |
PRINT((_L("e_cfilewriter_write_updateoutputfilesize_seek 0")));
|
sl@0
|
132 |
|
sl@0
|
133 |
PRINT((_L("CFileWriter::UpdateOutputFileSize() pos: %Ld"), pos));
|
sl@0
|
134 |
PRINT((_L("CFileWriter::UpdateOutputFileSize() iOutputFileSize: %Ld"), iOutputFileSize));
|
sl@0
|
135 |
PRINT((_L("CFileWriter::UpdateOutputFileSize() iSetSize: %Ld"), iSetSize));
|
sl@0
|
136 |
|
sl@0
|
137 |
if (pos > iOutputFileSize)
|
sl@0
|
138 |
{
|
sl@0
|
139 |
iOutputFileSize = pos;
|
sl@0
|
140 |
}
|
sl@0
|
141 |
|
sl@0
|
142 |
while (iOutputFileSize >= iSetSize)
|
sl@0
|
143 |
{
|
sl@0
|
144 |
iSetSize += static_cast<TInt64>(iOutputBufferSize) * (static_cast<TInt64>(iMaxOutputBufHardLimit) >> 1);
|
sl@0
|
145 |
PRINT((_L("e_cfilewriter_updateoutputfilesize_setsize 1")));
|
sl@0
|
146 |
iOutputFile->SetSize( iSetSize );
|
sl@0
|
147 |
PRINT((_L("e_cfilewriter_updateoutputfilesize_setsize 0")));
|
sl@0
|
148 |
}
|
sl@0
|
149 |
}
|
sl@0
|
150 |
|
sl@0
|
151 |
// -----------------------------------------------------------------------------
|
sl@0
|
152 |
// CFileWriter::Write( const TDesC8& aBuf )
|
sl@0
|
153 |
// Writes incoming buffer data to internal buffers for writing to disk.
|
sl@0
|
154 |
// (other items were commented in a header).
|
sl@0
|
155 |
// -----------------------------------------------------------------------------
|
sl@0
|
156 |
//
|
sl@0
|
157 |
TInt CFileWriter::Write( const TDesC8& aBuf )
|
sl@0
|
158 |
{
|
sl@0
|
159 |
PRINT(_L("CFileWriter::Write() in"));
|
sl@0
|
160 |
PRINT((_L("e_cfilewriter_write 1")));
|
sl@0
|
161 |
|
sl@0
|
162 |
iWritingStarted = ETrue;
|
sl@0
|
163 |
|
sl@0
|
164 |
if ( !iMemReadyForWriting )
|
sl@0
|
165 |
{
|
sl@0
|
166 |
return KErrNoMemory;
|
sl@0
|
167 |
}
|
sl@0
|
168 |
|
sl@0
|
169 |
PRINT((_L("e_cfilewriter_write_adddatatobuffer 1")));
|
sl@0
|
170 |
TInt error = AddDataToBuffer( aBuf );
|
sl@0
|
171 |
PRINT((_L("e_cfilewriter_write_adddatatobuffer 0")));
|
sl@0
|
172 |
|
sl@0
|
173 |
if ( error != KErrNone )
|
sl@0
|
174 |
{
|
sl@0
|
175 |
PRINT((_L("CFileWriter::Write() buffer write error: %d"), error));
|
sl@0
|
176 |
return error;
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
PRINT((_L("CFileWriter::Write() Write Buffer, Status: Full:%d Empty:%d "),
|
sl@0
|
180 |
iFullBufferQueue.Count(), iEmptyBufferQueue.Count() ));
|
sl@0
|
181 |
|
sl@0
|
182 |
if ( iAsyncWritingOngoing )
|
sl@0
|
183 |
{
|
sl@0
|
184 |
if ( iFullBufferQueue.Count() >= iMaxOutputBufHardLimit )
|
sl@0
|
185 |
{
|
sl@0
|
186 |
PRINT((_L("CFileWriter::Write() Waiting async write to complete")));
|
sl@0
|
187 |
PRINT((_L("e_cfilewriter_write_wait_async 1")));
|
sl@0
|
188 |
User::WaitForRequest( iStatus );
|
sl@0
|
189 |
PRINT((_L("e_cfilewriter_write_wait_async 0")));
|
sl@0
|
190 |
PRINT((_L("CFileWriter::Write() Async write done")));
|
sl@0
|
191 |
TRAP(error, RunL());
|
sl@0
|
192 |
if (error != KErrNone)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
PRINT((_L("CFileWriter::Write() call runL leave, error: %d"), error));
|
sl@0
|
195 |
return error;
|
sl@0
|
196 |
}
|
sl@0
|
197 |
}
|
sl@0
|
198 |
}
|
sl@0
|
199 |
else
|
sl@0
|
200 |
{
|
sl@0
|
201 |
if ( iFullBufferQueue.Count() )
|
sl@0
|
202 |
{
|
sl@0
|
203 |
PRINT(_L("CFileWriter::Write() writing async"));
|
sl@0
|
204 |
PRINT((_L("e_cfilewriter_write_startwrite 1")));
|
sl@0
|
205 |
iOutputFile->Write( *iFullBufferQueue[0], iStatus );
|
sl@0
|
206 |
PRINT((_L("e_cfilewriter_write_startwrite 0")));
|
sl@0
|
207 |
iAsyncWritingOngoing = ETrue;
|
sl@0
|
208 |
if ( !IsActive() )
|
sl@0
|
209 |
{
|
sl@0
|
210 |
SetActive();
|
sl@0
|
211 |
}
|
sl@0
|
212 |
}
|
sl@0
|
213 |
}
|
sl@0
|
214 |
|
sl@0
|
215 |
PRINT(_L("CFileWriter::Write() out"));
|
sl@0
|
216 |
PRINT((_L("e_cfilewriter_write 0")));
|
sl@0
|
217 |
return error;
|
sl@0
|
218 |
}
|
sl@0
|
219 |
|
sl@0
|
220 |
// -----------------------------------------------------------------------------
|
sl@0
|
221 |
// CFileWriter::Flush( const TDesC8& aBuf )
|
sl@0
|
222 |
// Flush internal buffers to disk.
|
sl@0
|
223 |
// (other items were commented in a header).
|
sl@0
|
224 |
// -----------------------------------------------------------------------------
|
sl@0
|
225 |
//
|
sl@0
|
226 |
TInt CFileWriter::Flush( const TDesC8& aBuf )
|
sl@0
|
227 |
{
|
sl@0
|
228 |
PRINT(_L("CFileWriter::Flush() in"));
|
sl@0
|
229 |
PRINT((_L("e_cfilewriter_flush 1")));
|
sl@0
|
230 |
|
sl@0
|
231 |
if ( !iMemReadyForWriting )
|
sl@0
|
232 |
{
|
sl@0
|
233 |
return KErrNoMemory;
|
sl@0
|
234 |
}
|
sl@0
|
235 |
|
sl@0
|
236 |
iWritingStarted = ETrue;
|
sl@0
|
237 |
|
sl@0
|
238 |
PRINT((_L("e_cfilewriter_flush_adddatatobuf 1")));
|
sl@0
|
239 |
TInt error = AddDataToBuffer( aBuf );
|
sl@0
|
240 |
if ( error != KErrNone )
|
sl@0
|
241 |
{
|
sl@0
|
242 |
return error;
|
sl@0
|
243 |
}
|
sl@0
|
244 |
PRINT((_L("e_cfilewriter_flush_adddatatobuf 0")));
|
sl@0
|
245 |
|
sl@0
|
246 |
PRINT((_L("CFileWriter::Flush() FullCount: %d "), iFullBufferQueue.Count()));
|
sl@0
|
247 |
iFlush = ETrue;
|
sl@0
|
248 |
|
sl@0
|
249 |
if ( iAsyncWritingOngoing )
|
sl@0
|
250 |
{
|
sl@0
|
251 |
PRINT((_L("CFileWriter::Flush() Waiting async write to complete")));
|
sl@0
|
252 |
PRINT((_L("e_cfilewriter_flush_waitasynctostop 1")));
|
sl@0
|
253 |
User::WaitForRequest( iStatus );
|
sl@0
|
254 |
PRINT((_L("e_cfilewriter_flush_waitasynctostop 0")));
|
sl@0
|
255 |
PRINT((_L("CFileWriter::Flush() Async write done, flushing")));
|
sl@0
|
256 |
TRAP(error, RunL());
|
sl@0
|
257 |
if (error != KErrNone)
|
sl@0
|
258 |
{
|
sl@0
|
259 |
PRINT((_L("CFileWriter::Flush() call runL leave, error: %d"), error));
|
sl@0
|
260 |
return error;
|
sl@0
|
261 |
}
|
sl@0
|
262 |
}
|
sl@0
|
263 |
|
sl@0
|
264 |
while ( iFullBufferQueue.Count() )
|
sl@0
|
265 |
{
|
sl@0
|
266 |
PRINT((_L("e_cfilewriter_flush_writequeue_sync 1")));
|
sl@0
|
267 |
error = iOutputFile->Write( *iFullBufferQueue[0] );
|
sl@0
|
268 |
PRINT((_L("e_cfilewriter_flush_writequeue_sync 0")));
|
sl@0
|
269 |
PRINT((_L("e_cfilewriter_flush_remove_buf 1")));
|
sl@0
|
270 |
if ( error == KErrNone )
|
sl@0
|
271 |
{
|
sl@0
|
272 |
UpdateOutputFileSize();
|
sl@0
|
273 |
iFullBufferQueue[0]->Des().Zero();
|
sl@0
|
274 |
if ( iEmptyBufferQueue.Append( iFullBufferQueue[0] ) )
|
sl@0
|
275 |
{
|
sl@0
|
276 |
PRINT(_L("CFileWriter::Flush() Append failed"));
|
sl@0
|
277 |
delete ( iFullBufferQueue[0] );
|
sl@0
|
278 |
}
|
sl@0
|
279 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
280 |
}
|
sl@0
|
281 |
else
|
sl@0
|
282 |
{
|
sl@0
|
283 |
PRINT((_L("CFileWriter::Flush() fullBufQueue write failed, error: %d"), error));
|
sl@0
|
284 |
iFlush = EFalse;
|
sl@0
|
285 |
return error;
|
sl@0
|
286 |
}
|
sl@0
|
287 |
PRINT((_L("e_cfilewriter_flush_remove_buf 0")));
|
sl@0
|
288 |
}
|
sl@0
|
289 |
|
sl@0
|
290 |
if ( iInputBuf->Length() )
|
sl@0
|
291 |
{
|
sl@0
|
292 |
PRINT((_L("e_cfilewriter_flush_writeinput_sync 1")));
|
sl@0
|
293 |
error = iOutputFile->Write( *iInputBuf );
|
sl@0
|
294 |
PRINT((_L("e_cfilewriter_flush_writeinput_sync 0")));
|
sl@0
|
295 |
if ( error == KErrNone )
|
sl@0
|
296 |
{
|
sl@0
|
297 |
UpdateOutputFileSize();
|
sl@0
|
298 |
iInputBuf->Des().Zero();
|
sl@0
|
299 |
}
|
sl@0
|
300 |
else
|
sl@0
|
301 |
{
|
sl@0
|
302 |
PRINT((_L("CFileWriter::Flush() inputbuf write failed, error: %d"), error));
|
sl@0
|
303 |
iFlush = EFalse;
|
sl@0
|
304 |
return error;
|
sl@0
|
305 |
}
|
sl@0
|
306 |
}
|
sl@0
|
307 |
|
sl@0
|
308 |
iFlush = EFalse;
|
sl@0
|
309 |
PRINT((_L("CFileWriter::Flush() FullCount: %d <= Should be 0"), iFullBufferQueue.Count()));
|
sl@0
|
310 |
PRINT(_L("CFileWriter::Flush() out"));
|
sl@0
|
311 |
PRINT((_L("e_cfilewriter_flush 0")));
|
sl@0
|
312 |
return KErrNone;
|
sl@0
|
313 |
}
|
sl@0
|
314 |
|
sl@0
|
315 |
|
sl@0
|
316 |
// -----------------------------------------------------------------------------
|
sl@0
|
317 |
// CFileWriter::SetOutputBufferSize( TOutputBufferSize aBufferSize )
|
sl@0
|
318 |
// Set output buffer sizes.
|
sl@0
|
319 |
// (other items were commented in a header).
|
sl@0
|
320 |
// -----------------------------------------------------------------------------
|
sl@0
|
321 |
//
|
sl@0
|
322 |
TInt CFileWriter::SetOutputBufferSize( TOutputBufferSize aBufferSize, MP4Handle aHandle )
|
sl@0
|
323 |
{
|
sl@0
|
324 |
MP4HandleImp handle = (MP4HandleImp)aHandle;
|
sl@0
|
325 |
TInt size = 0;
|
sl@0
|
326 |
|
sl@0
|
327 |
if ( iWritingStarted )
|
sl@0
|
328 |
{
|
sl@0
|
329 |
return KErrNotReady;
|
sl@0
|
330 |
}
|
sl@0
|
331 |
|
sl@0
|
332 |
if ( aBufferSize == EBufferSizeSmall )
|
sl@0
|
333 |
{
|
sl@0
|
334 |
size = iOutputBufferSizeSmall;
|
sl@0
|
335 |
}
|
sl@0
|
336 |
else if ( aBufferSize == EBufferSizeLarge )
|
sl@0
|
337 |
{
|
sl@0
|
338 |
size = iOutputBufferSizeLarge;
|
sl@0
|
339 |
}
|
sl@0
|
340 |
else if ( aBufferSize == EBufferSizeCustom )
|
sl@0
|
341 |
{
|
sl@0
|
342 |
size = handle->mediaWriteBufferSize;
|
sl@0
|
343 |
}
|
sl@0
|
344 |
else
|
sl@0
|
345 |
{
|
sl@0
|
346 |
return KErrArgument;
|
sl@0
|
347 |
}
|
sl@0
|
348 |
|
sl@0
|
349 |
if ( size == iOutputBufferSize )
|
sl@0
|
350 |
{
|
sl@0
|
351 |
return KErrNone;
|
sl@0
|
352 |
}
|
sl@0
|
353 |
else
|
sl@0
|
354 |
{
|
sl@0
|
355 |
iOutputBufferSize = size;
|
sl@0
|
356 |
}
|
sl@0
|
357 |
|
sl@0
|
358 |
iMemReadyForWriting = EFalse;
|
sl@0
|
359 |
delete iInputBuf;
|
sl@0
|
360 |
iInputBuf = NULL;
|
sl@0
|
361 |
iEmptyBufferQueue.ResetAndDestroy();
|
sl@0
|
362 |
iFullBufferQueue.ResetAndDestroy();
|
sl@0
|
363 |
|
sl@0
|
364 |
TRAPD(err, AllocateBuffersL() );
|
sl@0
|
365 |
return err;
|
sl@0
|
366 |
}
|
sl@0
|
367 |
|
sl@0
|
368 |
// -----------------------------------------------------------------------------
|
sl@0
|
369 |
// CFileWriter::SetOutputBufferCount( MP4Handle aHandle )
|
sl@0
|
370 |
// Set output buffer count.
|
sl@0
|
371 |
// (other items were commented in a header).
|
sl@0
|
372 |
// -----------------------------------------------------------------------------
|
sl@0
|
373 |
//
|
sl@0
|
374 |
void CFileWriter::SetOutputBufferCount( MP4Handle aHandle )
|
sl@0
|
375 |
{
|
sl@0
|
376 |
MP4HandleImp handle = (MP4HandleImp)aHandle;
|
sl@0
|
377 |
|
sl@0
|
378 |
if ( handle->writeBufferMaxCount >= 6 )
|
sl@0
|
379 |
{
|
sl@0
|
380 |
iMaxOutputBufHardLimit = handle->writeBufferMaxCount;
|
sl@0
|
381 |
iMaxOutputBufSoftLimit = KFileWriterMinBufferCount + ((iMaxOutputBufHardLimit-KFileWriterMinBufferCount)/2);
|
sl@0
|
382 |
}
|
sl@0
|
383 |
}
|
sl@0
|
384 |
|
sl@0
|
385 |
|
sl@0
|
386 |
// -----------------------------------------------------------------------------
|
sl@0
|
387 |
// CFileWriter::AddDataToBuffer( const TDesC8& aBuf )
|
sl@0
|
388 |
// Writes incoming data to internal buffers and buffer queues..
|
sl@0
|
389 |
// (other items were commented in a header).
|
sl@0
|
390 |
// -----------------------------------------------------------------------------
|
sl@0
|
391 |
//
|
sl@0
|
392 |
TInt CFileWriter::AddDataToBuffer( const TDesC8& aBuf )
|
sl@0
|
393 |
{
|
sl@0
|
394 |
PRINT(_L("CFileWriter::AddDataToBuffer() in"));
|
sl@0
|
395 |
|
sl@0
|
396 |
TInt byteswritten = 0;
|
sl@0
|
397 |
TInt numbytes = 0;
|
sl@0
|
398 |
TInt available = 0; // Available bytes in write buffer
|
sl@0
|
399 |
TInt error = KErrNone;
|
sl@0
|
400 |
|
sl@0
|
401 |
if ( iError != KErrNone )
|
sl@0
|
402 |
{
|
sl@0
|
403 |
PRINT((_L("CFileWriter::AddDataToBuffer() out, RunL iError: %d"), iError));
|
sl@0
|
404 |
return iError;
|
sl@0
|
405 |
}
|
sl@0
|
406 |
|
sl@0
|
407 |
if ( iInputBuf == NULL )
|
sl@0
|
408 |
{
|
sl@0
|
409 |
return KErrNoMemory;
|
sl@0
|
410 |
}
|
sl@0
|
411 |
|
sl@0
|
412 |
while (byteswritten < aBuf.Length() )
|
sl@0
|
413 |
{
|
sl@0
|
414 |
available = iOutputBufferSize - iInputBuf->Length();
|
sl@0
|
415 |
|
sl@0
|
416 |
if (available > 0)
|
sl@0
|
417 |
{
|
sl@0
|
418 |
numbytes = aBuf.Length() - byteswritten;
|
sl@0
|
419 |
if (numbytes > available)
|
sl@0
|
420 |
{
|
sl@0
|
421 |
numbytes = available;
|
sl@0
|
422 |
}
|
sl@0
|
423 |
iInputBuf->Des().Append( aBuf.Mid( byteswritten, numbytes ) );
|
sl@0
|
424 |
byteswritten += numbytes;
|
sl@0
|
425 |
}
|
sl@0
|
426 |
else // Buffer is full, write it to disk
|
sl@0
|
427 |
{
|
sl@0
|
428 |
// input is full, move full inputbuf to full buf queue
|
sl@0
|
429 |
if ( iFullBufferQueue.Append( iInputBuf ) != KErrNone )
|
sl@0
|
430 |
{
|
sl@0
|
431 |
PRINT(_L("CFileWriter::AddDataToBuffer() Append failed"));
|
sl@0
|
432 |
delete iInputBuf;
|
sl@0
|
433 |
iInputBuf = NULL;
|
sl@0
|
434 |
}
|
sl@0
|
435 |
if ( iEmptyBufferQueue.Count() == 0 )
|
sl@0
|
436 |
{
|
sl@0
|
437 |
// no empty buffers in queue, allocating new one
|
sl@0
|
438 |
TRAP(error, iInputBuf = HBufC8::NewL( iOutputBufferSize ));
|
sl@0
|
439 |
if ( error != KErrNone )
|
sl@0
|
440 |
{
|
sl@0
|
441 |
PRINT((_L("CFileWriter::AddDataToBuffer(), memory alloc failed: %d"), error));
|
sl@0
|
442 |
iInputBuf = NULL;
|
sl@0
|
443 |
iError = error;
|
sl@0
|
444 |
break;
|
sl@0
|
445 |
}
|
sl@0
|
446 |
}
|
sl@0
|
447 |
else
|
sl@0
|
448 |
{
|
sl@0
|
449 |
iInputBuf = iEmptyBufferQueue[ 0 ];
|
sl@0
|
450 |
iEmptyBufferQueue.Remove( 0 );
|
sl@0
|
451 |
}
|
sl@0
|
452 |
}
|
sl@0
|
453 |
}
|
sl@0
|
454 |
|
sl@0
|
455 |
PRINT((_L("CFileWriter::AddDataToBuffer() out, error: %d"), error));
|
sl@0
|
456 |
return error;
|
sl@0
|
457 |
}
|
sl@0
|
458 |
|
sl@0
|
459 |
// -----------------------------------------------------------------------------
|
sl@0
|
460 |
// CFileWriter::AllocateBuffersL()
|
sl@0
|
461 |
// Allocates input and output buffers.
|
sl@0
|
462 |
// (other items were commented in a header).
|
sl@0
|
463 |
// -----------------------------------------------------------------------------
|
sl@0
|
464 |
//
|
sl@0
|
465 |
void CFileWriter::AllocateBuffersL()
|
sl@0
|
466 |
{
|
sl@0
|
467 |
PRINT((_L("CFileWriter::AllocateBuffersL() in, outbufsize: %d"), iOutputBufferSize));
|
sl@0
|
468 |
HBufC8* buf = NULL;
|
sl@0
|
469 |
TInt err = 0;
|
sl@0
|
470 |
iMemReadyForWriting = EFalse;
|
sl@0
|
471 |
|
sl@0
|
472 |
iInputBuf = HBufC8::NewL( iOutputBufferSize );
|
sl@0
|
473 |
for( TInt i=0; i<KFileWriterMinBufferCount; i++ )
|
sl@0
|
474 |
{
|
sl@0
|
475 |
buf = HBufC8::NewL( iOutputBufferSize );
|
sl@0
|
476 |
err = iEmptyBufferQueue.Append( buf );
|
sl@0
|
477 |
if ( err )
|
sl@0
|
478 |
{
|
sl@0
|
479 |
delete ( buf );
|
sl@0
|
480 |
User::Leave( err );
|
sl@0
|
481 |
}
|
sl@0
|
482 |
}
|
sl@0
|
483 |
iMemReadyForWriting = ETrue;
|
sl@0
|
484 |
PRINT((_L("CFileWriter::AllocateBuffersL() out")));
|
sl@0
|
485 |
}
|
sl@0
|
486 |
|
sl@0
|
487 |
// -----------------------------------------------------------------------------
|
sl@0
|
488 |
// CFileWriter::DoCancel()
|
sl@0
|
489 |
// From CActive Cancels async request.
|
sl@0
|
490 |
// -----------------------------------------------------------------------------
|
sl@0
|
491 |
//
|
sl@0
|
492 |
void CFileWriter::DoCancel()
|
sl@0
|
493 |
{
|
sl@0
|
494 |
}
|
sl@0
|
495 |
|
sl@0
|
496 |
// -----------------------------------------------------------------------------
|
sl@0
|
497 |
// CFileWriter::RunL()
|
sl@0
|
498 |
// From CActive Called when async request completes.
|
sl@0
|
499 |
// -----------------------------------------------------------------------------
|
sl@0
|
500 |
//
|
sl@0
|
501 |
void CFileWriter::RunL()
|
sl@0
|
502 |
{
|
sl@0
|
503 |
PRINT(_L("CFileWriter::RunL() in"));
|
sl@0
|
504 |
PRINT((_L("e_cfilewriter_runl 1")));
|
sl@0
|
505 |
iAsyncWritingOngoing = EFalse;
|
sl@0
|
506 |
|
sl@0
|
507 |
if ( iStatus == KErrNone )
|
sl@0
|
508 |
{
|
sl@0
|
509 |
UpdateOutputFileSize();
|
sl@0
|
510 |
iFullBufferQueue[0]->Des().Zero();
|
sl@0
|
511 |
iError = iEmptyBufferQueue.Append( iFullBufferQueue[0] );
|
sl@0
|
512 |
if ( iError )
|
sl@0
|
513 |
{
|
sl@0
|
514 |
PRINT((_L("CFileWriter::RunL() Append failed 1 %d"), iError ));
|
sl@0
|
515 |
delete ( iFullBufferQueue[0] );
|
sl@0
|
516 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
517 |
return;
|
sl@0
|
518 |
}
|
sl@0
|
519 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
520 |
}
|
sl@0
|
521 |
else
|
sl@0
|
522 |
{
|
sl@0
|
523 |
PRINT((_L("CFileWriter::RunL() Write error in previous async: %d "), iStatus.Int() ));
|
sl@0
|
524 |
iError = iStatus.Int();
|
sl@0
|
525 |
return;
|
sl@0
|
526 |
}
|
sl@0
|
527 |
|
sl@0
|
528 |
PRINT((_L("CFileWriter::RunL() Buffer written, Status: Full:%d Empty:%d Filesize:%Ld"), iFullBufferQueue.Count(), iEmptyBufferQueue.Count(), iOutputFileSize ));
|
sl@0
|
529 |
|
sl@0
|
530 |
if ( iFlush )
|
sl@0
|
531 |
{
|
sl@0
|
532 |
PRINT(_L("CFileWriter::RunL() out, flushing"));
|
sl@0
|
533 |
PRINT((_L("e_cfilewriter_runl 0")));
|
sl@0
|
534 |
return;
|
sl@0
|
535 |
}
|
sl@0
|
536 |
|
sl@0
|
537 |
if ( iFullBufferQueue.Count() >= iMaxOutputBufHardLimit )
|
sl@0
|
538 |
{
|
sl@0
|
539 |
while ( iFullBufferQueue.Count() > iMaxOutputBufSoftLimit )
|
sl@0
|
540 |
{
|
sl@0
|
541 |
PRINT((_L("e_cfilewriter_runl_write 1")));
|
sl@0
|
542 |
iError = iOutputFile->Write( *iFullBufferQueue[0]);
|
sl@0
|
543 |
PRINT((_L("e_cfilewriter_runl_write 0")));
|
sl@0
|
544 |
if ( iError == KErrNone )
|
sl@0
|
545 |
{
|
sl@0
|
546 |
UpdateOutputFileSize();
|
sl@0
|
547 |
iFullBufferQueue[0]->Des().Zero();
|
sl@0
|
548 |
iError = iEmptyBufferQueue.Append( iFullBufferQueue[0] );
|
sl@0
|
549 |
if ( iError )
|
sl@0
|
550 |
{
|
sl@0
|
551 |
PRINT((_L("CFileWriter::RunL() Append failed 2 %d"), iError));
|
sl@0
|
552 |
delete ( iFullBufferQueue[0] );
|
sl@0
|
553 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
554 |
return;
|
sl@0
|
555 |
}
|
sl@0
|
556 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
557 |
PRINT((_L("CFileWriter::RunL() Hardlimit : Buffer sync written, Status: Full:%d Empty:%d Filesize:%Ld"), iFullBufferQueue.Count(), iEmptyBufferQueue.Count(), iOutputFileSize ));
|
sl@0
|
558 |
}
|
sl@0
|
559 |
else
|
sl@0
|
560 |
{
|
sl@0
|
561 |
PRINT((_L("CFileWriter::RunL() Write error: %d "), iError));
|
sl@0
|
562 |
return;
|
sl@0
|
563 |
}
|
sl@0
|
564 |
}
|
sl@0
|
565 |
}
|
sl@0
|
566 |
|
sl@0
|
567 |
if ( iFullBufferQueue.Count() >= iMaxOutputBufSoftLimit )
|
sl@0
|
568 |
{
|
sl@0
|
569 |
PRINT((_L("e_cfilewriter_runl_outfile_write 1")));
|
sl@0
|
570 |
iError = iOutputFile->Write( *iFullBufferQueue[0]);
|
sl@0
|
571 |
PRINT((_L("e_cfilewriter_runl_outfile_write 0")));
|
sl@0
|
572 |
if ( iError == KErrNone )
|
sl@0
|
573 |
{
|
sl@0
|
574 |
UpdateOutputFileSize();
|
sl@0
|
575 |
iFullBufferQueue[0]->Des().Zero();
|
sl@0
|
576 |
iError = iEmptyBufferQueue.Append( iFullBufferQueue[0] );
|
sl@0
|
577 |
if ( iError )
|
sl@0
|
578 |
{
|
sl@0
|
579 |
PRINT((_L("CFileWriter::RunL() Append failed 3 %d"), iError));
|
sl@0
|
580 |
delete ( iFullBufferQueue[0] );
|
sl@0
|
581 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
582 |
return;
|
sl@0
|
583 |
}
|
sl@0
|
584 |
iFullBufferQueue.Remove( 0 );
|
sl@0
|
585 |
PRINT((_L("CFileWriter::RunL() Softlimit : Buffer sync written, Status: Full:%d Empty:%d Filesize:%Ld"), iFullBufferQueue.Count(), iEmptyBufferQueue.Count(), iOutputFileSize ));
|
sl@0
|
586 |
}
|
sl@0
|
587 |
else
|
sl@0
|
588 |
{
|
sl@0
|
589 |
PRINT((_L("CFileWriter::RunL() Write error: %d "), iError));
|
sl@0
|
590 |
return;
|
sl@0
|
591 |
}
|
sl@0
|
592 |
}
|
sl@0
|
593 |
|
sl@0
|
594 |
if ( iFullBufferQueue.Count() )
|
sl@0
|
595 |
{
|
sl@0
|
596 |
PRINT((_L("e_cfilewriter_runl_outfile2_write 1")));
|
sl@0
|
597 |
iOutputFile->Write( *iFullBufferQueue[0], iStatus );
|
sl@0
|
598 |
PRINT((_L("e_cfilewriter_runl_outfile2_write 0")));
|
sl@0
|
599 |
iAsyncWritingOngoing = ETrue;
|
sl@0
|
600 |
if ( !IsActive() )
|
sl@0
|
601 |
{
|
sl@0
|
602 |
SetActive();
|
sl@0
|
603 |
}
|
sl@0
|
604 |
}
|
sl@0
|
605 |
|
sl@0
|
606 |
PRINT((_L("e_cfilewriter_runl 0")));
|
sl@0
|
607 |
PRINT((_L("CFileWriter::RunL() out, iError=%d"), iError));
|
sl@0
|
608 |
}
|
sl@0
|
609 |
|
sl@0
|
610 |
// End of File
|