os/mm/devsound/devsoundrefplugin/src/platsec/server/AudioServer/MmfAudioServer.cpp
Update contrib.
1 // Copyright (c) 2004-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.
17 #include "MmfAudioServer.h"
18 #include "MmfAudioServerStart.h"
19 #include "MmfDevSoundSession.h"
20 #include "MmfAudioServerSession.h"
21 #include "MmfDevSoundServer.h"
22 #include "MmfDevSoundServerStart.h"
23 #include "mmfbase.hrh"
26 static const TUid KUidDevSoundServer = {KUidMmfDevSoundServerDllUnicodeDefine};
28 const TInt KAudioServerShutDownDelay = 50000000; //50 sec
31 CMMFAudioServer* CMMFAudioServer::NewL()
33 CMMFAudioServer* s = new(ELeave) CMMFAudioServer();
34 CleanupStack::PushL(s);
40 CMMFAudioServer::CMMFAudioServer() :
41 CMmfIpcServer(EPriorityStandard)
45 void CMMFAudioServer::ConstructL()
47 // Call base class to Start server
48 StartL(KAudioServerName);
49 iDelayAudioServerShutDown = CDelayAudioServerShutDown::NewL();
50 // Create an audio policy server
51 iAudioPolicyProxy = new (ELeave) RMMFAudioPolicyProxy();
52 User::LeaveIfError(iAudioPolicyProxy->CreateServer(iPolicyServerHandle));
55 CMMFAudioServer::~CMMFAudioServer()
57 if (iDelayAudioServerShutDown)
59 iDelayAudioServerShutDown->Cancel();
60 delete iDelayAudioServerShutDown;
62 iDevSoundServList.ResetAndDestroy();
63 iDevSoundServList.Close();
64 if( iAudioPolicyProxy != NULL)
66 iAudioPolicyProxy->Close();
67 delete iAudioPolicyProxy;
69 iPolicyServerHandle.Close();
72 CMmfIpcSession* CMMFAudioServer::NewSessionL(const TVersion& aVersion) const
74 TVersion v(KMMFAudioServerVersion,KMMFAudioServerMinorVersionNumber,KMMFAudioServerBuildVersionNumber);
75 if(!User::QueryVersionSupported(v, aVersion))
76 User::Leave(KErrNotSupported);
79 while(i<iDevSoundServList.Count())
81 CStartAndMonitorDevSoundThread* devSoundMonitorThread = iDevSoundServList[i];
82 if(!devSoundMonitorThread->IsActive())
84 iDevSoundServList.Remove(i);
85 delete devSoundMonitorThread;
92 iDevSoundServList.Compress();
94 RMMFDevSoundServerProxy devSoundSessionHandle;
95 User::LeaveIfError(StartDevSoundServer(devSoundSessionHandle));
96 CMMFAudioServerSession* audioServerSession = CMMFAudioServerSession::NewL(devSoundSessionHandle);
97 return audioServerSession;
100 void CMMFAudioServer::IncrementSessionId()
102 iAudioServerSessionId++;
105 void CMMFAudioServer::DecrementSessionId()
107 iAudioServerSessionId--;
110 void CMMFAudioServer::IncrementDevSoundCount()
114 iDelayAudioServerShutDown->Cancel(); //in the case we started the shutdown due to no more DevSound
117 void CMMFAudioServer::DecrementDevSoundCount()
120 if ( iDevSoundCount == 0 )
122 iDelayAudioServerShutDown->SetDelay(TTimeIntervalMicroSeconds32(KAudioServerShutDownDelay));
126 void CMMFAudioServer::SendEventToClient(TInt aSessionToAlert, TInt /*aSessionToBeLaunched*/)
128 // For the session requested, send event to client
129 iSessionIter.SetToFirst();
130 CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
131 while (session != NULL)
133 if (session->AudioServerSessionId() == aSessionToAlert)
137 session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
141 void CMMFAudioServer::LaunchRequest(TInt aSessionId)
143 iSessionIter.SetToFirst();
144 CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
145 while (session != NULL)
147 if (session->AudioServerSessionId() == aSessionId)
151 session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
155 TInt CMMFAudioServer::StartDevSoundServer(RMMFDevSoundServerProxy& aDevSoundSessionHandle) const
157 RMessage2 message(Message());
158 CStartAndMonitorDevSoundThread* monitorDevSound = NULL;
160 TRAPD(err, monitorDevSound = CStartAndMonitorDevSoundThread::NewL(const_cast<CMMFAudioServer*>(this)));
163 delete monitorDevSound;
167 err = iDevSoundServList.Append(monitorDevSound);
170 delete monitorDevSound;
174 return monitorDevSound->StartDevSoundServer(message, aDevSoundSessionHandle);
179 CMMFAudioServer::CDelayAudioServerShutDown* CMMFAudioServer::CDelayAudioServerShutDown::NewL()
181 CDelayAudioServerShutDown* self = new(ELeave) CDelayAudioServerShutDown;
182 CleanupStack::PushL(self);
188 CMMFAudioServer::CDelayAudioServerShutDown::CDelayAudioServerShutDown() : CActive(0)
192 void CMMFAudioServer::CDelayAudioServerShutDown::ConstructL()
194 User::LeaveIfError(iShutDownTimer.CreateLocal());
195 CActiveScheduler::Add(this);
198 CMMFAudioServer::CDelayAudioServerShutDown::~CDelayAudioServerShutDown()
201 iShutDownTimer.Close();
205 void CMMFAudioServer::CDelayAudioServerShutDown::SetDelay(TTimeIntervalMicroSeconds32 aDelay)
207 __ASSERT_ALWAYS(!IsActive(), User::Panic(_L("CDelayedShutDown"), 1));
208 iShutDownTimer.After(iStatus, aDelay);
212 void CMMFAudioServer::CDelayAudioServerShutDown::RunL()
214 CActiveScheduler::Stop();
218 void CMMFAudioServer::CDelayAudioServerShutDown::DoCancel()
220 iShutDownTimer.Cancel();
224 CStartAndMonitorDevSoundThread* CStartAndMonitorDevSoundThread::NewL(CMMFAudioServer* aAudioServer)
226 CStartAndMonitorDevSoundThread* self = new(ELeave) CStartAndMonitorDevSoundThread(aAudioServer);
227 CleanupStack::PushL(self);
233 void CStartAndMonitorDevSoundThread::ConstructL()
235 CActiveScheduler::Add(this);
238 TInt CStartAndMonitorDevSoundThread::StartDevSoundServer(RMessage2& aMessage, RMMFDevSoundServerProxy& aDevSoundSessionHandle)
240 RThread clientThread;
241 TInt err = aMessage.Client(clientThread);
246 RProcess clientProcess;
247 err = clientThread.Process(clientProcess);
250 clientThread.Close();
253 TProcessId clientProcessId(clientProcess.Id());
255 clientThread.Close();
256 clientProcess.Close();
258 RServer2 devSoundServerHandle;
259 TDevSoundServerStart start(iAudioServer, clientProcessId, devSoundServerHandle);//Pass to DevSoundServer the clien't process ID
261 const TUidType serverUid(KNullUid,KNullUid,KUidDevSoundServer);
263 TThreadFunction serverFunc=CMMFDevSoundServer::StartThread;
265 err = iServer.Create(_L(""), serverFunc, KDevSoundServerStackSize,
266 #ifdef SYMBIAN_USE_SEPARATE_HEAPS
267 KDevSoundServerInitHeapSize, KDevSoundServerMaxHeapSize,
269 &User::Heap(), //shared heap is now default
271 &start, EOwnerProcess);
276 // Synchronise with the server
277 TRequestStatus reqStatus;
278 iServer.Rendezvous(reqStatus);
280 if (reqStatus!=KRequestPending)
286 // Start the test harness
288 // Server will call the reciprocal static synchronise call
291 User::WaitForRequest(reqStatus); // wait for start or death
292 if(reqStatus.Int() != KErrNone)
295 devSoundServerHandle.Close();
296 return reqStatus.Int();
298 err = aDevSoundSessionHandle.Open(devSoundServerHandle);
303 devSoundServerHandle.Close();
306 aDevSoundSessionHandle.ShareProtected();
307 iServer.Logon(iStatus);
310 iAudioServer->IncrementDevSoundCount();
315 CStartAndMonitorDevSoundThread::CStartAndMonitorDevSoundThread(CMMFAudioServer* aAudioServer): CActive(0)
317 iAudioServer = aAudioServer;
320 CStartAndMonitorDevSoundThread::~CStartAndMonitorDevSoundThread()
325 void CStartAndMonitorDevSoundThread::RunL()
328 iAudioServer->DecrementDevSoundCount();
331 void CStartAndMonitorDevSoundThread::DoCancel()
333 iServer.LogonCancel(iStatus);
336 TInt RMMFDevSoundServerProxy::Open(RServer2& aDevSoundServerHandle)
338 TInt err = CreateSession(aDevSoundServerHandle, TVersion(KMMFDevSoundServerVersion,
339 KMMFDevSoundServerMinorVersionNumber,
340 KMMFDevSoundServerBuildVersionNumber), -1, EIpcSession_GlobalSharable);