|
1 //audiocontext.cpp |
|
2 |
|
3 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 // All rights reserved. |
|
5 // This component and the accompanying materials are made available |
|
6 // under the terms of "Eclipse Public License v1.0" |
|
7 // which accompanies this distribution, and is available |
|
8 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 // |
|
10 // Initial Contributors: |
|
11 // Nokia Corporation - initial contribution. |
|
12 // |
|
13 // Contributors: |
|
14 // |
|
15 // Description: |
|
16 // |
|
17 |
|
18 |
|
19 |
|
20 #include <a3f/maudiostream.h> |
|
21 #include <a3f/maudiocodec.h> |
|
22 #include <a3f/maudiogaincontrol.h> |
|
23 #include <a3f/a3fbase.h> |
|
24 |
|
25 #include "audiocontext.h" |
|
26 #include "audioprocessingunit.h" |
|
27 #include "logicalaudiostream.h" |
|
28 #include "audiostreammanager.h" |
|
29 |
|
30 |
|
31 // --------------------------------------------------------------------------- |
|
32 // Default constructor |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 CAudioContext::CAudioContext() |
|
36 { |
|
37 TRACE_CREATE(); |
|
38 DP_CONTEXT(CAudioContext::CAudioContext *CD1*, CtxDevSound, DPLOCAL); |
|
39 DP_IN(); |
|
40 iInCommit = EFalse; |
|
41 DP_OUT(); |
|
42 } |
|
43 |
|
44 // --------------------------------------------------------------------------- |
|
45 // Symbian 2nd phase constructor |
|
46 // --------------------------------------------------------------------------- |
|
47 void CAudioContext::ConstructL() |
|
48 { |
|
49 DP_CONTEXT(CAudioContext::ConstructL *CD1*, CtxDevSound, DPLOCAL); |
|
50 DP_IN(); |
|
51 |
|
52 // Setup Multimedia Resource Control |
|
53 User::LeaveIfError(iMMRC.Open(*this)); |
|
54 User::LeaveIfError(iCriticalSection.CreateLocal()); |
|
55 DP_OUT(); |
|
56 } |
|
57 |
|
58 // --------------------------------------------------------------------------- |
|
59 // Symbian constructor |
|
60 // --------------------------------------------------------------------------- |
|
61 EXPORT_C CAudioContext* CAudioContext::NewL() |
|
62 { |
|
63 DP_STATIC_CONTEXT(CAudioContext::NewL *CD0*, CtxDevSound, DPLOCAL); |
|
64 DP_IN(); |
|
65 CAudioContext* self = new(ELeave)CAudioContext(); |
|
66 CleanupStack::PushL(self); |
|
67 self->ConstructL(); |
|
68 CleanupStack::Pop(self); |
|
69 DP0_RET(self, "0x%x"); |
|
70 } |
|
71 |
|
72 // --------------------------------------------------------------------------- |
|
73 // Destructor |
|
74 // --------------------------------------------------------------------------- |
|
75 // |
|
76 EXPORT_C CAudioContext::~CAudioContext() |
|
77 { |
|
78 DP_CONTEXT(CAudioContext::~CAudioContext *CD1*, CtxDevSound, DPLOCAL); |
|
79 DP_IN(); |
|
80 iMMRC.Close(); |
|
81 iAudioContextObservers.Close(); |
|
82 iAudioProcessingUnits.Close(); |
|
83 if (iActualChain) |
|
84 { |
|
85 iActualChain->Release(); |
|
86 }; |
|
87 iCriticalSection.Close(); |
|
88 DP_OUT(); |
|
89 } |
|
90 |
|
91 // --------------------------------------------------------------------------- |
|
92 // From class MAudioContext |
|
93 // CAudioContext::SetClientSettings |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 TInt CAudioContext::SetClientSettings(const TClientContextSettings& aSettings) |
|
97 { |
|
98 DP_CONTEXT(CAudioContext::SetClientSettings *CD1*, CtxDevSound, DPLOCAL); |
|
99 DP_IN(); |
|
100 TInt err(KErrNone); |
|
101 iClientSettings = aSettings; |
|
102 if( iContextId.Id() == 0 ) |
|
103 { |
|
104 TUint64 id = iMMRC.LogOn(iClientSettings.iProcessId); |
|
105 TAudioContextId temp(id); |
|
106 iContextId = temp; |
|
107 } |
|
108 else |
|
109 { |
|
110 err = KErrAlreadyExists; |
|
111 } |
|
112 DP0_RET(err, "%d"); |
|
113 } |
|
114 |
|
115 |
|
116 // --------------------------------------------------------------------------- |
|
117 // From class MAudioContext |
|
118 // CAudioContext::GetContextId |
|
119 // --------------------------------------------------------------------------- |
|
120 // |
|
121 TAudioContextId CAudioContext::ContextId() const |
|
122 { |
|
123 DP_CONTEXT(CAudioContext::ContextId *CD1*, CtxDevSound, DPLOCAL); |
|
124 DP_IN(); |
|
125 DP_OUT(); |
|
126 return iContextId; |
|
127 } |
|
128 |
|
129 // --------------------------------------------------------------------------- |
|
130 // From class MAudioContext |
|
131 // CAudioContext::Commit |
|
132 // --------------------------------------------------------------------------- |
|
133 // |
|
134 TInt CAudioContext::Commit() |
|
135 { |
|
136 DP_CONTEXT(CAudioContext::Commit *CD1*, CtxDevSound, DPLOCAL); |
|
137 DP_IN(); |
|
138 TInt err(KErrNone); |
|
139 iInCommit = ETrue; |
|
140 |
|
141 if (iClientSettings.iProcessId.Id() == 0) |
|
142 { |
|
143 DP0_RET(KErrNotReady, "%d"); |
|
144 } |
|
145 |
|
146 err = iMMRC.SendResourceRequest(iActualChain, iDesiredChain, this); |
|
147 DP0_RET(err, "%d"); |
|
148 } |
|
149 |
|
150 // --------------------------------------------------------------------------- |
|
151 // From class MAudioContext |
|
152 // CAudioContext::CreateAudioStream |
|
153 // --------------------------------------------------------------------------- |
|
154 // |
|
155 TInt CAudioContext::CreateAudioStream(MAudioStream *&aStream) |
|
156 { |
|
157 DP_CONTEXT(CAudioContext::CreateAudioStream *CD1*, CtxDevSound, DPLOCAL); |
|
158 DP_IN(); |
|
159 TInt err(KErrNone); |
|
160 |
|
161 CAudioStreamManager* manager(NULL); |
|
162 |
|
163 // Create desired logical chain |
|
164 TRAP(err, manager = CAudioStreamManager::NewL(KUidAudioStream)); |
|
165 if(err == KErrNone) |
|
166 { |
|
167 aStream = static_cast<MAudioStream*>(manager); |
|
168 |
|
169 // Use this as desired chain (this is the one that the client modify) |
|
170 CLogicalAudioStream* logicalAudioStream = static_cast<CLogicalAudioStream*>(aStream); |
|
171 iDesiredChain = static_cast<MLogicalChain*>(logicalAudioStream); |
|
172 logicalAudioStream->SetParentContext(*this); |
|
173 if (iActualChain) |
|
174 { |
|
175 iActualChain->Release(); |
|
176 iActualChain=NULL; |
|
177 } |
|
178 TRAP(err, iActualChain = iDesiredChain->CloneL()); |
|
179 } |
|
180 DP0_RET(err, "%d"); |
|
181 } |
|
182 |
|
183 // --------------------------------------------------------------------------- |
|
184 // From class MAudioContext |
|
185 // CAudioContext::DeleteAudioStream |
|
186 // --------------------------------------------------------------------------- |
|
187 // |
|
188 void CAudioContext::DeleteAudioStream(MAudioStream *&aStream) |
|
189 { |
|
190 DP_CONTEXT(CAudioContext::DeleteAudioStream *CD1*, CtxDevSound, DPLOCAL); |
|
191 DP_IN(); |
|
192 CAudioStreamManager* manager = static_cast<CAudioStreamManager*>(aStream); |
|
193 delete manager; |
|
194 DP_OUT(); |
|
195 } |
|
196 |
|
197 // --------------------------------------------------------------------------- |
|
198 // From class MAudioContext |
|
199 // CAudioContext::CreateAudioProcessingUnit |
|
200 // --------------------------------------------------------------------------- |
|
201 // |
|
202 TInt CAudioContext::CreateAudioProcessingUnit(TUid aTypeId, MAudioProcessingUnit *&aProcessingUnit) |
|
203 { |
|
204 DP_CONTEXT(CAudioContext::CreateAudioProcessingUnit *CD1*, CtxDevSound, DPLOCAL); |
|
205 DP_IN(); |
|
206 TInt err(KErrNone); |
|
207 TAudioComponentId unitInstance = GetAndSetInstanceID(); |
|
208 |
|
209 CAudioProcessingUnit* pUnit=NULL; |
|
210 TComponentParameters cParameters; |
|
211 cParameters.iTypeUid = aTypeId; |
|
212 cParameters.iInstanceId = unitInstance; |
|
213 cParameters.iContextId = iContextId; |
|
214 cParameters.iSettingsObserver = this; |
|
215 |
|
216 TRAP(err, pUnit = CAudioProcessingUnit::NewL(cParameters)); |
|
217 if (err==KErrNone) |
|
218 { |
|
219 aProcessingUnit = static_cast<MAudioProcessingUnit*>(pUnit); |
|
220 err = iAudioProcessingUnits.Append(aProcessingUnit); |
|
221 if(err!=KErrNone) |
|
222 { |
|
223 delete pUnit; |
|
224 aProcessingUnit = NULL; |
|
225 } |
|
226 } |
|
227 DP0_RET(err, "%d"); |
|
228 } |
|
229 |
|
230 // --------------------------------------------------------------------------- |
|
231 // From class MAudioContext |
|
232 // CAudioContext::DeleteAudioProcessingUnit |
|
233 // --------------------------------------------------------------------------- |
|
234 // |
|
235 void CAudioContext::DeleteAudioProcessingUnit( |
|
236 MAudioProcessingUnit *&aProcessingUnit) |
|
237 { |
|
238 DP_CONTEXT(CAudioContext::DeleteAudioProcessingUnit *CD1*, CtxDevSound, DPLOCAL); |
|
239 DP_IN(); |
|
240 DP_OUT(); |
|
241 CAudioProcessingUnit* cUnit = static_cast<CAudioProcessingUnit*>(aProcessingUnit); |
|
242 TAudioComponentId param = aProcessingUnit->InstanceId(); |
|
243 TUint count= iAudioProcessingUnits.Count(); |
|
244 |
|
245 for ( TUint i(0); i < count; i++ ) |
|
246 { |
|
247 // find and delete component |
|
248 if( iAudioProcessingUnits[i]->InstanceId() == param) |
|
249 { |
|
250 iAudioProcessingUnits.Remove(i); |
|
251 delete cUnit; |
|
252 break; |
|
253 } |
|
254 } |
|
255 } |
|
256 |
|
257 // --------------------------------------------------------------------------- |
|
258 // From class MAudioContext |
|
259 // CAudioContext::RegisterAudioContextObserver |
|
260 // --------------------------------------------------------------------------- |
|
261 // |
|
262 TInt CAudioContext::RegisterAudioContextObserver( |
|
263 MAudioContextObserver& aObserver) |
|
264 { |
|
265 DP_CONTEXT(CAudioContext::RegisterAudioContextObserver *CD1*, CtxDevSound, DPLOCAL); |
|
266 DP_IN(); |
|
267 TInt err = iAudioContextObservers.Find(&aObserver); |
|
268 if( err != KErrNotFound ) |
|
269 { |
|
270 err = KErrAlreadyExists; |
|
271 } |
|
272 else |
|
273 { |
|
274 err = iAudioContextObservers.Append(&aObserver); |
|
275 } |
|
276 DP0_RET(err, "%d"); |
|
277 } |
|
278 |
|
279 // --------------------------------------------------------------------------- |
|
280 // From class MAudioContext |
|
281 // CAudioContext::UnregisterAudioContextObserver |
|
282 // --------------------------------------------------------------------------- |
|
283 // |
|
284 void CAudioContext::UnregisterAudioContextObserver( |
|
285 MAudioContextObserver& aObserver) |
|
286 { |
|
287 DP_CONTEXT(CAudioContext::UnregisterAudioContextObserver *CD1*, CtxDevSound, DPLOCAL); |
|
288 DP_IN(); |
|
289 |
|
290 TInt idxOrErr = iAudioContextObservers.Find(&aObserver); |
|
291 if( idxOrErr != KErrNotFound ) |
|
292 { |
|
293 iAudioContextObservers.Remove(idxOrErr); |
|
294 } |
|
295 DP_OUT(); |
|
296 } |
|
297 |
|
298 // --------------------------------------------------------------------------- |
|
299 // From class MAudioContext |
|
300 // CAudioContext::Interface |
|
301 // --------------------------------------------------------------------------- |
|
302 // |
|
303 TAny* CAudioContext::Interface(TUid aType) |
|
304 { |
|
305 DP_CONTEXT(CAudioContext::Interface *CD1*, CtxDevSound, DPLOCAL); |
|
306 DP_IN(); |
|
307 TAny* interface = NULL; |
|
308 if(aType == KUIdAudioResourceNotification) |
|
309 { |
|
310 #ifndef SYMBIAN_DISABLE_ARN |
|
311 interface = static_cast<MA3FDevSoundAutoPauseResume*>(this); |
|
312 #else |
|
313 interface = NULL; |
|
314 #endif |
|
315 } |
|
316 else |
|
317 { |
|
318 interface = NULL; |
|
319 } |
|
320 DP_OUT(); |
|
321 return interface; |
|
322 } |
|
323 |
|
324 |
|
325 // --------------------------------------------------------------------------- |
|
326 // From class MAudioContext |
|
327 // CAudioContext::Reset |
|
328 // --------------------------------------------------------------------------- |
|
329 // |
|
330 TInt CAudioContext::Reset() |
|
331 { |
|
332 DP_CONTEXT(CAudioContext::Reset *CD1*, CtxDevSound, DPLOCAL); |
|
333 DP_IN(); |
|
334 TInt err = KErrNone; |
|
335 if (iInCommit) |
|
336 { |
|
337 err = KErrNotReady; |
|
338 } |
|
339 iDesiredChain->ResetMessage(); |
|
340 iDesiredChain->CopySettings(*iActualChain); |
|
341 DP0_RET(err, "%d"); |
|
342 } |
|
343 |
|
344 // --------------------------------------------------------------------------- |
|
345 // From class MMultimediaResourceControlObserver |
|
346 // CAudioContext::ReceiveResourceResponse |
|
347 // --------------------------------------------------------------------------- |
|
348 // |
|
349 void CAudioContext::ReceiveResourceResponse(MLogicalChain* aAppliedChain, TInt aError) |
|
350 { |
|
351 DP_CONTEXT(CAudioContext::ReceiveResourceResponse *CD1*, CtxDevSound, DPLOCAL); |
|
352 DP_IN(); |
|
353 |
|
354 iCriticalSection.Wait(); |
|
355 iMsgVersion++; |
|
356 iMMRC.ResetMessages(); |
|
357 iCriticalSection.Signal(); |
|
358 |
|
359 if ( aError == KErrNone || iPreempted ) |
|
360 { |
|
361 if ( iPreempted ) |
|
362 { |
|
363 iPreempted = EFalse; |
|
364 } |
|
365 // Desired chain was commited succesfully |
|
366 TInt err; |
|
367 MLogicalChain* lc = NULL; |
|
368 TRAP(err, lc = aAppliedChain->CloneL()); |
|
369 DP1(DLERR,"ECloneLeftWhenReceivingResourceResponse %d", err); |
|
370 __ASSERT_DEBUG(err == KErrNone, Panic(ECloneLeftWhenReceivingResourceResponse)); |
|
371 if ( iActualChain ) |
|
372 { |
|
373 iActualChain->Release(); |
|
374 } |
|
375 iActualChain = lc; |
|
376 } |
|
377 else |
|
378 { |
|
379 iDesiredChain->CopySettings(*iActualChain); |
|
380 } |
|
381 iDesiredChain->ResetMessage(); |
|
382 ContextEventSignal(KUidA3FContextUpdateComplete, aError); |
|
383 iInCommit = EFalse; |
|
384 DP_OUT(); |
|
385 } |
|
386 |
|
387 // --------------------------------------------------------------------------- |
|
388 // From class MMultimediaResourceControlObserver |
|
389 // CAudioContext::ReceiveResourceUpdate |
|
390 // --------------------------------------------------------------------------- |
|
391 // |
|
392 void CAudioContext::ReceiveResourceUpdate(MLogicalChain* /*aMessage*/, TInt aError) |
|
393 { |
|
394 DP_CONTEXT(CAudioContext::ReceiveResourceUpdate *CD1*, CtxDevSound, DPLOCAL); |
|
395 DP_IN(); |
|
396 ContextEventSignal(KUidA3FContextCommitUpdate, aError); |
|
397 DP_OUT(); |
|
398 } |
|
399 |
|
400 |
|
401 // --------------------------------------------------------------------------- |
|
402 // From class MMultimediaResourceControlObserver |
|
403 // CAudioContext::ReceivePreemptionUpdate |
|
404 // --------------------------------------------------------------------------- |
|
405 // |
|
406 void CAudioContext::ReceivePreemptionUpdate(MLogicalChain* /*aMessage*/, TInt aError) |
|
407 { |
|
408 DP_CONTEXT(CAudioContext::ReceivePreemptionUpdate *CD1*, CtxDevSound, DPLOCAL); |
|
409 DP_IN(); |
|
410 iPreempted = ETrue; |
|
411 if(iInCommit) |
|
412 { |
|
413 // Send only one callback |
|
414 ContextEventSignal(KUidA3FContextPreEmptedCommit, aError); |
|
415 } |
|
416 else |
|
417 { |
|
418 // Send two callbacks |
|
419 ContextEventSignal(KUidA3FContextPreEmption, aError); |
|
420 ContextEventSignal(KUidA3FContextPreEmptionUpdate, aError); |
|
421 } |
|
422 DP_OUT(); |
|
423 } |
|
424 |
|
425 |
|
426 // --------------------------------------------------------------------------- |
|
427 // From class MLogicalSettingObserver |
|
428 // CAudioContext::ReceiveComponentSettingsChange |
|
429 // --------------------------------------------------------------------------- |
|
430 void CAudioContext::ReceiveComponentSettingsChange(TUid /*aId*/, TMMRCMessageType aMessageType) |
|
431 { |
|
432 iDesiredChain->SetMessageType(aMessageType); |
|
433 } |
|
434 |
|
435 // --------------------------------------------------------------------------- |
|
436 // Internals |
|
437 // CAudioContext::GetAndSetInstanceID |
|
438 // --------------------------------------------------------------------------- |
|
439 TAudioComponentId CAudioContext::GetAndSetInstanceID() |
|
440 { |
|
441 DP_CONTEXT(CAudioContext::GetAndSetInstanceID *CD1*, CtxDevSound, DPLOCAL); |
|
442 DP_IN(); |
|
443 TUint id(1); // start ID count from 1 |
|
444 TAudioComponentId idComponent(1); |
|
445 TUint count = iAudioProcessingUnits.Count(); |
|
446 if ( count < KMaxTUint ) |
|
447 { |
|
448 id = ++count; |
|
449 } |
|
450 idComponent = id; |
|
451 DP_OUT(); |
|
452 return idComponent; |
|
453 } |
|
454 |
|
455 // --------------------------------------------------------------------------- |
|
456 // Internals |
|
457 // CAudioContext::ContextEventSignal |
|
458 // --------------------------------------------------------------------------- |
|
459 void CAudioContext::ContextEventSignal(TUid aEvent, TInt aError) |
|
460 { |
|
461 DP_CONTEXT(CAudioContext::ContextEventSignal *CD1*, CtxDevSound, DPLOCAL); |
|
462 DP_IN(); |
|
463 TUint count = iAudioContextObservers.Count(); |
|
464 for ( TUint idx(0); idx < count; idx++ ) |
|
465 { |
|
466 iAudioContextObservers[idx]->ContextEvent(aEvent, aError); |
|
467 } |
|
468 DP_OUT(); |
|
469 } |
|
470 |
|
471 // --------------------------------------------------------------------------- |
|
472 // CAudioContext::GetLogicalChain |
|
473 // --------------------------------------------------------------------------- |
|
474 EXPORT_C MLogicalChain* CAudioContext::GetLogicalChain(TInt aIndex) |
|
475 { |
|
476 DP_CONTEXT(CAudioContext::GetLogicalChain *CD1*, CtxDevSound, DPLOCAL); |
|
477 DP_IN(); |
|
478 MLogicalChain* logChain = NULL; |
|
479 if (aIndex == 0) |
|
480 { |
|
481 logChain = iDesiredChain; |
|
482 } |
|
483 else |
|
484 { |
|
485 logChain = iActualChain; |
|
486 } |
|
487 DP_OUT(); |
|
488 return logChain; |
|
489 } |
|
490 |
|
491 |
|
492 // --------------------------------------------------------------------------- |
|
493 // CAudioContext::RegisterAsClient |
|
494 // --------------------------------------------------------------------------- |
|
495 TInt CAudioContext::RegisterAsClient(TUid aEventType, const TDesC8& aNotificationRegistrationData, MA3FDevSoundAutoPauseResumeObserver* aObserverPtr) |
|
496 { |
|
497 DP_CONTEXT(CAudioContext::RegisterAsClient *CD1*, CtxDevSound, DPLOCAL); |
|
498 DP_IN(); |
|
499 TInt err(KErrNone); |
|
500 iObserverPtr = aObserverPtr; |
|
501 err = iMMRC.RegisterAsClient(aEventType, aNotificationRegistrationData); |
|
502 DP0_RET(err, "%d"); |
|
503 } |
|
504 |
|
505 // --------------------------------------------------------------------------- |
|
506 // CAudioContext::CancelRegisterAsClient |
|
507 // --------------------------------------------------------------------------- |
|
508 TInt CAudioContext::CancelRegisterAsClient(TUid aEventType) |
|
509 { |
|
510 DP_CONTEXT(CAudioContext::CancelRegisterAsClient *CD1*, CtxDevSound, DPLOCAL); |
|
511 DP_IN(); |
|
512 TInt err(KErrNone); |
|
513 err = iMMRC.CancelRegisterAsClient(aEventType); |
|
514 DP0_RET(err, "%d"); |
|
515 } |
|
516 |
|
517 // --------------------------------------------------------------------------- |
|
518 // CAudioContext::WillResumePlay |
|
519 // --------------------------------------------------------------------------- |
|
520 TInt CAudioContext::WillResumePlay() |
|
521 { |
|
522 DP_CONTEXT(CAudioContext::WillResumePlay *CD1*, CtxDevSound, DPLOCAL); |
|
523 DP_IN(); |
|
524 TInt err(KErrNone); |
|
525 err = iMMRC.WillResumePlay(); |
|
526 DP0_RET(err, "%d"); |
|
527 } |
|
528 |
|
529 // --------------------------------------------------------------------------- |
|
530 // From class MMultimediaResourceControlObserver |
|
531 // CAudioContext::CanResume |
|
532 // --------------------------------------------------------------------------- |
|
533 // |
|
534 void CAudioContext::CanResume() |
|
535 { |
|
536 DP_CONTEXT(CAudioContext::CanResume *CD1*, CtxDevSound, DPLOCAL); |
|
537 DP_IN(); |
|
538 if(iObserverPtr) |
|
539 { |
|
540 iObserverPtr->NotifyResume(); |
|
541 } |
|
542 DP_OUT(); |
|
543 } |
|
544 |
|
545 void CAudioContext::Panic(TAudioContextPanicCodes aCode) |
|
546 { |
|
547 User::Panic(KAudioContextPanicCategory, aCode); |
|
548 } |
|
549 |
|
550 EXPORT_C TInt CAudioContext::MsgVersion() |
|
551 { |
|
552 return iMsgVersion; |
|
553 } |
|
554 |
|
555 EXPORT_C RCriticalSection& CAudioContext::CriticalSection() |
|
556 { |
|
557 return iCriticalSection; |
|
558 } |
|
559 // End of file |