|
1 /* |
|
2 * Copyright (c) 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: Audio stream test component |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "OutputStreamer.h" |
|
20 #include "AudioDataSettingsParams.h" |
|
21 |
|
22 const TInt COutputStreamer::KPlayerActionPlay = 1; |
|
23 const TInt COutputStreamer::KPlayerActionStop = 3; |
|
24 |
|
25 COutputStreamer::COutputStreamer(CStifLogger *aLogger) |
|
26 : CStreamerBase(aLogger), iAlreadyClosed(EFalse), |
|
27 iPlaybackStopped(EFalse) |
|
28 { } |
|
29 |
|
30 void COutputStreamer::ConstructL() |
|
31 { |
|
32 iMdaAudioOutputStream = CMdaAudioOutputStream::NewL(*this); |
|
33 } |
|
34 |
|
35 COutputStreamer* COutputStreamer::NewL(CStifLogger *aLogger, TInt aId) |
|
36 { |
|
37 COutputStreamer *_self = NewLC(aLogger, aId); |
|
38 CleanupStack::Pop(_self); |
|
39 return _self; |
|
40 } |
|
41 |
|
42 COutputStreamer* COutputStreamer::NewLC(CStifLogger *aLogger, TInt aId) |
|
43 { |
|
44 COutputStreamer *_self = new(ELeave)COutputStreamer(aLogger); |
|
45 CleanupStack::PushL(_self); |
|
46 _self->ConstructL(); |
|
47 _self->SetId(aId); |
|
48 return _self; |
|
49 } |
|
50 |
|
51 void COutputStreamer::ConstructL(TInt aPriority, TMdaPriorityPreference aPref) |
|
52 { |
|
53 iMdaAudioOutputStream = CMdaAudioOutputStream::NewL(*this, aPriority, aPref); |
|
54 } |
|
55 |
|
56 COutputStreamer* COutputStreamer::NewL(TInt aPriority, TMdaPriorityPreference aPref, |
|
57 CStifLogger *aLogger, TInt aId) |
|
58 { |
|
59 COutputStreamer *_self = NewLC(aPriority, aPref, aLogger, aId); |
|
60 CleanupStack::Pop(_self); |
|
61 return _self; |
|
62 } |
|
63 |
|
64 COutputStreamer* COutputStreamer::NewLC(TInt aPriority, TMdaPriorityPreference aPref, |
|
65 CStifLogger *aLogger, TInt aId) |
|
66 { |
|
67 COutputStreamer *_self = new(ELeave)COutputStreamer(aLogger); |
|
68 CleanupStack::PushL(_self); |
|
69 _self->ConstructL(aPriority, aPref); |
|
70 _self->SetId(aId); |
|
71 return _self; |
|
72 } |
|
73 |
|
74 COutputStreamer::~COutputStreamer() |
|
75 { |
|
76 iMdaAudioOutputStream->Stop(); |
|
77 delete iMdaAudioOutputStream; |
|
78 iArrBufferErrors.Close(); |
|
79 TInt Before = iBuffers.Count(); |
|
80 iBuffers.ResetAndDestroy(); |
|
81 iLogger->Log(_L("%d) Items in buffer (%d), there were (%d)") , iId, iBuffers.Count() , Before); |
|
82 iMdaAudioOutputStream = NULL; |
|
83 iWriteBuffers.ResetAndDestroy(); |
|
84 } |
|
85 |
|
86 void COutputStreamer::SetAudioPropertiesL(TInt aSampleRate, TInt aChannels) |
|
87 { |
|
88 iLogger->Log(_L("%d) COutputStreamer::SetAudioPropertiesL(rate=%d, chan=%d)") , iId, aSampleRate, aChannels); |
|
89 iMdaAudioOutputStream->SetAudioPropertiesL(aSampleRate, aChannels); |
|
90 } |
|
91 |
|
92 void COutputStreamer::SetVolume(TInt aVolume) |
|
93 { |
|
94 iLogger->Log(_L("%d) COutputStreamer::SetVolume(%d, %d)") , iId, aVolume); |
|
95 iMdaAudioOutputStream->SetVolume(aVolume); |
|
96 } |
|
97 |
|
98 TInt COutputStreamer::GetVolume() { return iMdaAudioOutputStream->Volume(); } |
|
99 |
|
100 TInt COutputStreamer::GetMaxVolume() { return iMdaAudioOutputStream->MaxVolume(); } |
|
101 |
|
102 void COutputStreamer::SetPriority(TInt aPriority, TMdaPriorityPreference aPref) |
|
103 { |
|
104 iLogger->Log(_L("%d) COutputStreamer::SetPriority(%d, %d)") , iId, aPriority, aPref); |
|
105 iMdaAudioOutputStream->SetPriority(aPriority, aPref); |
|
106 } |
|
107 |
|
108 void COutputStreamer::AdoptBuffers(RPointerArray<TDes8> &aBuffers ) |
|
109 { |
|
110 for (TInt x=0; x<aBuffers.Count() ; ++x) |
|
111 { |
|
112 iBuffers.Append(aBuffers[x]); |
|
113 } |
|
114 aBuffers.Reset(); |
|
115 } |
|
116 |
|
117 void COutputStreamer::StartPlayback(TMdaPackage *aSettings) |
|
118 { |
|
119 iLogger->Log(_L("COutputStreamer::StartPlayback()") ); |
|
120 iCurrentBuffer=-1; |
|
121 |
|
122 iMdaAudioOutputStream->Open(aSettings); |
|
123 } |
|
124 |
|
125 RArray<TInt> &COutputStreamer::GetBufferErrors(){return iArrBufferErrors;} |
|
126 |
|
127 TInt COutputStreamer::DesToBuffers(TDes8 &aDes) |
|
128 { |
|
129 TInt NumChunks = aDes.Length() / KBufferSize; |
|
130 TInt remainder = aDes.Length() % KBufferSize; |
|
131 |
|
132 TInt x; |
|
133 for (x=0 ; x<NumChunks ; ++x) |
|
134 { |
|
135 TDes8 *tmp = new(ELeave)TBuf8<KBufferSize>; |
|
136 CleanupStack::PushL(tmp); |
|
137 *tmp=aDes.Mid(x*KBufferSize , KBufferSize); |
|
138 if (AppendBuffer(tmp)) { iLogger->Log(_L("Error appending buffer in DesToBuffers")); } |
|
139 CleanupStack::Pop(tmp); |
|
140 } |
|
141 |
|
142 if (remainder > 0) |
|
143 { |
|
144 TDes8 *tmp = new(ELeave)TBuf8<KBufferSize>; |
|
145 CleanupStack::PushL(tmp); |
|
146 *tmp=aDes.Mid(x*KBufferSize , remainder); |
|
147 tmp->SetLength(remainder); |
|
148 |
|
149 if (AppendBuffer(tmp)) { iLogger->Log(_L("Error appending buffer in DesToBuffers")); } |
|
150 CleanupStack::Pop(tmp); |
|
151 } |
|
152 |
|
153 iLogger->Log(_L("COutputStreamer::DesToBuffers # Buffers created - %d"), iWriteBuffers.Count()); |
|
154 return KErrNone; |
|
155 } |
|
156 |
|
157 TInt COutputStreamer::GetBalanceL() |
|
158 { |
|
159 TInt retVal = iMdaAudioOutputStream->GetBalanceL(); |
|
160 return retVal; |
|
161 } |
|
162 |
|
163 void COutputStreamer::SetBalanceL(TInt aBalance) |
|
164 { |
|
165 iMdaAudioOutputStream->SetBalanceL(aBalance); |
|
166 } |
|
167 |
|
168 void COutputStreamer::Stop() |
|
169 { |
|
170 iLogger->Log(_L("%d) COutputStreamer::Stop()") , iId); |
|
171 iMdaAudioOutputStream->Stop(); |
|
172 } |
|
173 |
|
174 void COutputStreamer::Exit(TInt aExitCode) |
|
175 { |
|
176 iLogger->Log(_L("%d) COutputStreamer::Exit(%d)"), iId, aExitCode); |
|
177 iFinalError = aExitCode; |
|
178 if (!iAlreadyClosed) |
|
179 { |
|
180 iAlreadyClosed=ETrue; |
|
181 CActiveScheduler::Stop(); |
|
182 } |
|
183 } |
|
184 |
|
185 TFourCC COutputStreamer::GetDataType() |
|
186 { |
|
187 TFourCC retVal = iMdaAudioOutputStream->DataType(); |
|
188 iLogger->Log(_L("%d) COutputStreamer::GetDataType() , result=(%d)") , iId, retVal.FourCC()); |
|
189 return retVal; |
|
190 } |
|
191 |
|
192 void COutputStreamer::SetDataTypeL(TFourCC aAudioType) |
|
193 { |
|
194 iLogger->Log(_L("%d) COutputStreamer::SetDataTypeL(dataType=%d)") , iId, aAudioType.FourCC()); |
|
195 iMdaAudioOutputStream->SetDataTypeL(aAudioType); |
|
196 } |
|
197 |
|
198 // inherited from MMdaAudioOutputStreamCallback |
|
199 void COutputStreamer::MaoscOpenComplete(TInt aError) |
|
200 { |
|
201 iLogger->Log(_L("%d) COutputStreamer::MaoscOpenComplete(%d)") , iId, aError); |
|
202 if (aError || iExitAfterOpen) |
|
203 { |
|
204 Exit(aError); |
|
205 return; |
|
206 } |
|
207 |
|
208 iLogger->Log(_L("%d) Bytes (%d), Position (%d)") , iId, iMdaAudioOutputStream->GetBytes() , iMdaAudioOutputStream->Position()); |
|
209 ++iCurrentBuffer; |
|
210 if (0<=iCurrentBuffer && iCurrentBuffer<=iWriteBuffers.Count() ) |
|
211 { |
|
212 iMdaAudioOutputStream->WriteL( *(iWriteBuffers[iCurrentBuffer]) ); |
|
213 iLogger->Log(_L("%d) COutputStreamer::MaoscOpenComplete(), writting buffer #(%d)") , iId, iCurrentBuffer); |
|
214 } |
|
215 //for (TInt i=0; i < iWriteBuffers.Count(); i++) |
|
216 //{ |
|
217 // iMdaAudioOutputStream->WriteL( *(iWriteBuffers[i]) ); |
|
218 //} |
|
219 } |
|
220 |
|
221 void COutputStreamer::MaoscBufferCopied(TInt aError, const TDesC8 &/*aBuffer*/) |
|
222 { |
|
223 iLogger->Log(_L("%d) COutputStreamer::MaoscBufferCopied(%d)") , iId, aError); |
|
224 iArrBufferErrors.Append(aError); |
|
225 if (aError) |
|
226 { |
|
227 return; |
|
228 } |
|
229 iLogger->Log(_L("%d) Bytes (%d), Position (%d)") , iId, iMdaAudioOutputStream->GetBytes() , iMdaAudioOutputStream->Position()); |
|
230 ++iCurrentBuffer; |
|
231 if (0<=iCurrentBuffer && iCurrentBuffer<iWriteBuffers.Count() ) |
|
232 { |
|
233 iMdaAudioOutputStream->WriteL( *(iWriteBuffers[iCurrentBuffer]) ); |
|
234 iLogger->Log(_L("%d) COutputStreamer::MaoscBufferCopied(), writting buffer #(%d)") , iId, iCurrentBuffer); |
|
235 } |
|
236 else if (iCurrentBuffer == iWriteBuffers.Count()) |
|
237 //if (iCurrentBuffer == iWriteBuffers.Count()) |
|
238 { |
|
239 Exit(KErrNone); |
|
240 } |
|
241 } |
|
242 |
|
243 void COutputStreamer::MaoscPlayComplete(TInt aError) |
|
244 { |
|
245 iLogger->Log(_L("%d) Bytes (%d), Position (%d)") , iId, iMdaAudioOutputStream->GetBytes() , iMdaAudioOutputStream->Position()); |
|
246 iLogger->Log(_L("%d) COutputStreamer::MaoscPlayComplete(%d)") , iId, aError); |
|
247 |
|
248 // Underflow occurs at end of playback |
|
249 if ( (aError == KErrUnderflow) && (iCurrentBuffer == iBuffers.Count()) ) |
|
250 aError = KErrNone; |
|
251 |
|
252 // Stop causes abort |
|
253 if (iPlaybackStopped && (aError == KErrCancel)) |
|
254 aError = KErrNone; |
|
255 |
|
256 Exit(aError); |
|
257 } |
|
258 |
|
259 // from MEventTarget |
|
260 TInt COutputStreamer::ExecuteL(CParameters *aParams) |
|
261 { |
|
262 switch (aParams->iAction) |
|
263 { |
|
264 case KPlayerActionPlay: |
|
265 StartPlayback( (static_cast<CAudioDataSettingsParams*>(aParams))->GetSettings() ); |
|
266 break; |
|
267 case KPlayerActionStop: |
|
268 iPlaybackStopped = ETrue; |
|
269 Stop(); |
|
270 break; |
|
271 } |
|
272 return EFalse; |
|
273 } |
|
274 |
|
275 TInt COutputStreamer::AppendWriteBuffer(HBufC8 *aBuffer) |
|
276 { |
|
277 return iWriteBuffers.Append(aBuffer); |
|
278 } |
|
279 |
|
280 TInt COutputStreamer::LoadAudioFileL(const TFileName &aFileName) |
|
281 { |
|
282 RFs fs; // file server instance |
|
283 RFile audiofile; |
|
284 |
|
285 // open fileserver session and file |
|
286 User::LeaveIfError(fs.Connect()); |
|
287 // push into cleanup stack |
|
288 CleanupClosePushL(fs); |
|
289 |
|
290 // open file |
|
291 TInt err = audiofile.Open(fs, aFileName, EFileRead|EFileStream); |
|
292 if (err==KErrNone) |
|
293 { |
|
294 // file opened ok, proceed reading |
|
295 TInt fileSize; |
|
296 audiofile.Size(fileSize); |
|
297 |
|
298 TInt readPos(0); |
|
299 TInt bufLen(KBufferSize); |
|
300 HBufC8* tmpBuf(NULL); |
|
301 while (readPos < fileSize) |
|
302 { |
|
303 bufLen = Min(KBufferSize,fileSize-readPos); |
|
304 tmpBuf = HBufC8::NewL(KBufferSize); |
|
305 CleanupStack::PushL(tmpBuf); |
|
306 TPtr8 ptr(tmpBuf->Des()); |
|
307 ptr.Set(const_cast<TUint8*>(ptr.Ptr()),0,bufLen); |
|
308 User::LeaveIfError(audiofile.Read(readPos,ptr,bufLen)); |
|
309 tmpBuf->Des().SetLength(bufLen); |
|
310 AppendWriteBuffer(tmpBuf); |
|
311 CleanupStack::Pop(tmpBuf); |
|
312 readPos += bufLen; |
|
313 } |
|
314 iLogger->Log(_L("Loading complete!")); |
|
315 } |
|
316 else |
|
317 { |
|
318 // failed to open file |
|
319 iLogger->Log(_L("Error loading audio sample!")); |
|
320 } |
|
321 audiofile.Close(); |
|
322 // pop and destroy resources pushed into stack |
|
323 CleanupStack::PopAndDestroy(); |
|
324 |
|
325 return err; |
|
326 } |
|
327 |