First public contribution.
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "MmfAudioPolicySession.h"
17 #include "MmfPolicyClientServer.h"
18 #include "MmfAudioPolicyServer.h"
19 #include "MmfDevSoundInfo.h"
22 const TInt KEventQLimit=16; // maximum number of pending events per session
24 CMMFAudioPolicySession* CMMFAudioPolicySession::NewL()
26 CMMFAudioPolicySession* self = new(ELeave) CMMFAudioPolicySession();
27 CleanupStack::PushL(self);
33 void CMMFAudioPolicySession::ConstructL()
35 iAudioPolicyRequest = new (ELeave) CMMFAudioPolicyRequest();
38 CMMFAudioPolicySession::CMMFAudioPolicySession() : iEventsQue(_FOFF(TMMFAudioPolicyEventHolder,iLink)),
39 iNoMemoryEventHolder(TMMFAudioPolicyEvent(TMMFAudioPolicyEvent::EMMFAudioPolicyResourceNotification, KErrNoMemory, EMMFStateCompleted), EFalse)
43 void CMMFAudioPolicySession::CreateL(const CMmfIpcServer& aServer)
45 iServer = STATIC_CAST(CMMFAudioPolicyServer*, (CONST_CAST(CMmfIpcServer*, &aServer)));
46 iServer->IncrementSessionCount();
47 CMmfIpcSession::CreateL(aServer);
48 iServer->IncrementSessionId();
49 iPolicySessionId = iServer->PolicySessionId();
50 // Get ptr to AudioPolicy from the server
51 iAudioPolicy = iServer->AudioPolicy();
52 iAudioPolicy->ReserveClientNumL(iServer->PolicySessionCount());
55 CMMFAudioPolicyServer* CMMFAudioPolicySession::Server()
57 return STATIC_CAST(CMMFAudioPolicyServer*, iServer);
60 CMMFAudioPolicySession::~CMMFAudioPolicySession()
62 delete iEventReceiver;
65 iAudioPolicyPrioritySettings.iState = EMMFStateClosed;
66 // Have session's CMMFAudioPolicyRequest object removed from list
67 if (iAudioPolicy != NULL)
69 iAudioPolicy->RemoveFromList(iPolicySessionId);
71 delete iAudioPolicyRequest;
74 iServer->DecrementSessionCount();
75 iServer->StopNotificationTimer();
79 void CMMFAudioPolicySession::ClearEventQ()
81 while (!iEventsQue.IsEmpty())
83 TMMFAudioPolicyEventHolder* heldEvent = iEventsQue.First();
84 iEventsQue.Remove(*heldEvent);
85 if (heldEvent == &iNoMemoryEventHolder)
87 iNoMemoryEventUsed = EFalse;
89 if (heldEvent->iShallBeDeleted)
97 void CMMFAudioPolicySession::ServiceL(const RMmfIpcMessage& aMessage)
99 if (iEventQSize > KEventQLimit)
101 aMessage.Panic(KMMFAudioPolicyPanicCategory, EMMFAudioPolicySessionEventQueueOverflow);
105 TBool complete = EFalse;
106 switch(aMessage.Function())
108 case EMMFPolicyMakeRequest:
109 MakeRequestL(aMessage);
110 aMessage.Complete(KErrNone);
112 case EMMFPolicySetDevSoundInfo:
113 complete = SetDevSoundInfoL(aMessage);
115 case EMMFPolicyUpdateState:
116 complete = UpdateStateL(aMessage);
118 case EMMFPolicyReceiveEvents:
119 complete = ReceiveEventsL(aMessage);
121 case EMMFPolicyCancelReceiveEvents:
122 complete = CancelReceiveEvents();
124 case EMMFPolicyGetPlayFormatsSupported:
125 complete = GetPlayFormatsSupportedL(aMessage);
127 case EMMFPolicyGetRecordFormatsSupported:
128 complete = GetRecordFormatsSupportedL(aMessage);
130 case EMMFPolicyGetPlayFormat:
131 complete = GetPlayFormatL(aMessage);
133 case EMMFPolicyGetRecordFormat:
134 complete = GetRecordFormatL(aMessage);
136 case EMMFPolicyLaunchRequests:
137 complete = LaunchRequest();
139 case EMMFPolicyRequestResourceNotification:
140 complete = RequestResourceNotificationL(aMessage);
142 case EMMFPolicyCancelRequestResourceNotification:
143 complete = CancelRequestResourceNotificationL(aMessage);
145 case EMMFPolicyStopNotification:
146 complete = StopNotificationL(aMessage);
148 case EMMFPolicyGetResourceNotificationEvent:
149 complete = GetResourceNotificationEventL(aMessage);
152 User::Leave(KErrNotSupported);
157 aMessage.Complete(KErrNone);
161 void CMMFAudioPolicySession::MakeRequestL(const RMmfIpcMessage& aMessage)
163 TMMFAudioPolicyPrioritySettingsPckg settingsPckg;
164 MmfMessageUtil::ReadL(aMessage, 0, settingsPckg);
166 iAudioPolicyRequest->SetPriority(settingsPckg().iPriority);
167 iAudioPolicyRequest->SetPref(settingsPckg().iPref);
168 iAudioPolicyRequest->SetState(settingsPckg().iState);
170 iAudioPolicyRequest->SetCapabilities(settingsPckg().iCapabilities);
172 // Set session Id in Request
173 iAudioPolicyRequest->SetPolicySessionId(iPolicySessionId);
174 iAudioPolicy->MakeRequest(iAudioPolicyRequest);
177 TBool CMMFAudioPolicySession::UpdateStateL(const RMmfIpcMessage& aMessage)
179 TMMFAudioPolicyPrioritySettingsPckg settingsPckg;
180 MmfMessageUtil::ReadL(aMessage, 0, settingsPckg);
182 iAudioPolicy->ModifyEntry(iPolicySessionId, settingsPckg().iState);
186 void CMMFAudioPolicySession::SendEventToClient(const TMMFAudioPolicyEvent& aEvent)
190 iEventReceiver->SendEvent(aEvent);
191 delete iEventReceiver;
196 if (++iEventQSize > KEventQLimit) // check if Q is not full
198 return; // we'll panic that bad client later
200 TMMFAudioPolicyEventHolder* heldEvent = new TMMFAudioPolicyEventHolder(aEvent, ETrue);
202 if (heldEvent==NULL && !iNoMemoryEventUsed)
204 heldEvent=&iNoMemoryEventHolder;
205 iNoMemoryEventUsed = ETrue;
209 iEventsQue.AddLast(*heldEvent);
214 TBool CMMFAudioPolicySession::SetDevSoundInfoL(const RMmfIpcMessage& aMessage)
216 TMMFDevSoundInfoPckg devSoundInfoPckg;
217 MmfMessageUtil::ReadL(aMessage, 0, devSoundInfoPckg);
218 iDevSoundId = devSoundInfoPckg().iDevSoundId;
222 TBool CMMFAudioPolicySession::ReceiveEventsL(const RMmfIpcMessage& aMessage)
225 User::Leave(KErrAlreadyExists);
226 iEventReceiver = CMMFAudioPolicyEventReceiver::NewL(aMessage);
227 #if defined(ALLOW_POLICY_DEBUG)
228 RDebug::Print(_L("Sess. ID=%d listen to events"),iPolicySessionId);
230 //send the next cached event (if any) to the client
231 if (!iEventsQue.IsEmpty())
233 TMMFAudioPolicyEventHolder* heldEvent = iEventsQue.First();
234 iEventReceiver->SendEvent(heldEvent->iEvent);
235 iEventsQue.Remove(*heldEvent);
236 if (heldEvent == &iNoMemoryEventHolder)
238 iNoMemoryEventUsed = EFalse;
241 if (heldEvent->iShallBeDeleted)
245 delete iEventReceiver;
251 TBool CMMFAudioPolicySession::CancelReceiveEvents()
253 delete iEventReceiver;
254 iEventReceiver = NULL;
258 TBool CMMFAudioPolicySession::GetPlayFormatsSupportedL(const RMmfIpcMessage& aMessage)
260 RMdaDevSound::TSoundFormatsSupportedBuf aPlayFormatsSupported;
261 aPlayFormatsSupported = iAudioPolicy->MdaHwInfo()->GetPlayFormatsSupported();
262 MmfMessageUtil::WriteL(aMessage, 0, aPlayFormatsSupported);
266 TBool CMMFAudioPolicySession::GetRecordFormatsSupportedL(const RMmfIpcMessage& aMessage)
268 RMdaDevSound::TSoundFormatsSupportedBuf aRecordFormatsSupported;
269 aRecordFormatsSupported = iAudioPolicy->MdaHwInfo()->GetRecordFormatsSupported();
270 MmfMessageUtil::WriteL(aMessage, 0, aRecordFormatsSupported);
274 TBool CMMFAudioPolicySession::GetPlayFormatL(const RMmfIpcMessage& aMessage)
276 RMdaDevSound::TCurrentSoundFormatBuf aPlayFormat;
277 aPlayFormat = iAudioPolicy->MdaHwInfo()->GetPlayFormat();
278 MmfMessageUtil::WriteL(aMessage, 0, aPlayFormat);
282 TBool CMMFAudioPolicySession::GetRecordFormatL(const RMmfIpcMessage& aMessage)
284 RMdaDevSound::TCurrentSoundFormatBuf aRecordFormat;
285 aRecordFormat = iAudioPolicy->MdaHwInfo()->GetRecordFormat();
286 MmfMessageUtil::WriteL(aMessage, 0, aRecordFormat);
290 TBool CMMFAudioPolicySession::LaunchRequest()
292 iAudioPolicy->LaunchRequest( iAudioPolicyRequest->PolicySessionId() );
296 TBool CMMFAudioPolicySession::RequestResourceNotificationL(const RMmfIpcMessage& aMessage)
298 TMMFAudioPolicyResourceNotificationSettingsPckg notificationPckg;
299 MmfMessageUtil::ReadL(aMessage, 0, notificationPckg);
300 iAudioPolicyRequest->SetNotificationEvent(notificationPckg().iNotificationUid);
301 iAudioPolicyRequest->SetRequestDataL(notificationPckg().iNotificationDelay);
302 iAudioPolicy->SetNotification(iPolicySessionId,notificationPckg().iNotificationUid);
306 TBool CMMFAudioPolicySession::CancelRequestResourceNotificationL(const RMmfIpcMessage& aMessage)
308 TMMFAudioPolicyResourceNotificationSettingsPckg notificationPckg;
309 MmfMessageUtil::ReadL(aMessage, 0, notificationPckg);
310 iAudioPolicyRequest->ResetNotificationEvent(notificationPckg().iNotificationUid);
311 iAudioPolicy->SetNotification(iPolicySessionId,KNullUid);
315 TBool CMMFAudioPolicySession::StopNotificationL(const RMmfIpcMessage& /*aMessage*/)
317 iServer->StopNotificationTimer();
321 TBool CMMFAudioPolicySession::GetResourceNotificationEventL(const RMmfIpcMessage& aMessage) const
323 TMMFAudioPolicyResourceNotificationSettingsPckg notificationPckg;
324 notificationPckg().iNotificationUid = iAudioPolicyRequest->NotificationEvent();
325 MmfMessageUtil::WriteL(aMessage, 0, notificationPckg);
329 CMMFAudioPolicyEventReceiver* CMMFAudioPolicyEventReceiver::NewL(const RMmfIpcMessage& aMessage)
331 return new(ELeave) CMMFAudioPolicyEventReceiver(aMessage);
334 CMMFAudioPolicyEventReceiver::~CMMFAudioPolicyEventReceiver()
336 if (iNeedToCompleteMessage)
338 iMessage.Complete(KErrDied);
342 void CMMFAudioPolicyEventReceiver::SendEvent(const TMMFAudioPolicyEvent& aEvent)
344 TMMFAudioPolicyEventPckg eventpckg(aEvent);
345 TInt err = MmfMessageUtil::Write(iMessage, 0, eventpckg);
346 iMessage.Complete(err);
347 iNeedToCompleteMessage = EFalse;
350 void CMMFAudioPolicyEventReceiver::SendEvent(const TMMFEvent& aEvent)
352 TMMFEventPckg eventpckg(aEvent);
353 TInt err = MmfMessageUtil::Write(iMessage, 0, eventpckg);
354 iMessage.Complete(err);
355 iNeedToCompleteMessage = EFalse;
358 CMMFAudioPolicyEventReceiver::CMMFAudioPolicyEventReceiver(const RMmfIpcMessage& aMessage) :
359 iMessage(aMessage), iNeedToCompleteMessage(ETrue)
363 CMMFAudioPolicyRequest::CMMFAudioPolicyRequest():
364 iPolicySessionId(KErrNotFound),
365 iReqDataPtr(KNullDesC8)
367 iNotificationEventUid = KNullUid;
370 CMMFAudioPolicyRequest::~CMMFAudioPolicyRequest()
375 void CMMFAudioPolicyRequest::SetRequestDataL(const TDesC8& aRequestData)
377 iReqDataPtr.Set(KNullDesC8);
378 if (aRequestData.Length() == 0)
382 if (iRequestData==NULL || (iRequestData && iRequestData->Des().MaxLength()<aRequestData.Length()))
386 iRequestData = aRequestData.AllocL();
390 iRequestData->Des().Copy(aRequestData);
392 iReqDataPtr.Set(*iRequestData);