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 "MmfBtAudioServer.h"
18 #include "MmfBtAudioServerStart.h"
19 #include "MmfBtDevSoundSession.h"
20 #include "MmfBtAudioServerSession.h"
21 #include "MmfBtDevSoundServer.h"
22 #include "MmfBtDevSoundServerStart.h"
23 #include "../../../../inc/common/mmfBtBase.hrh"
26 static const TUid KUidDevSoundServer = {KUidMmfBtDevSoundServerDllUnicodeDefine};
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 iDelayAudioServerShutDown = CDelayAudioServerShutDown::NewL();
48 // Call base class to Start server
49 StartL(KAudioServerName);
52 CMMFAudioServer::~CMMFAudioServer()
54 if (iDelayAudioServerShutDown)
56 iDelayAudioServerShutDown->Cancel();
57 delete iDelayAudioServerShutDown;
59 iDevSoundServList.Close();
60 iPolicyServerHandle.Close();
63 CMmfIpcSession* CMMFAudioServer::NewSessionL(const TVersion& aVersion) const
65 TVersion v(KMMFAudioServerVersion,KMMFAudioServerMinorVersionNumber,KMMFAudioServerBuildVersionNumber);
66 if(!User::QueryVersionSupported(v, aVersion))
67 User::Leave(KErrNotSupported);
69 for(TInt i=0; i<iDevSoundServList.Count(); i++)
71 CStartAndMonitorDevSoundThread* devSoundMonitorThread = iDevSoundServList[i];
72 if(!devSoundMonitorThread->IsActive())
74 iDevSoundServList.Remove(i);
75 delete devSoundMonitorThread;
77 iDevSoundServList.Compress();
80 TName devSoundServerName;
81 User::LeaveIfError(StartDevSoundServerL(devSoundServerName));
82 CMMFAudioServerSession* audioServerSession = CMMFAudioServerSession::NewL(devSoundServerName);
83 return audioServerSession;
86 void CMMFAudioServer::IncrementSessionId()
88 iAudioServerSessionId++;
91 void CMMFAudioServer::DecrementSessionId()
93 iAudioServerSessionId--;
96 void CMMFAudioServer::IncrementDevSoundCount()
100 iDelayAudioServerShutDown->Cancel(); //in the case we started the shutdown due to no more DevSound
103 void CMMFAudioServer::DecrementDevSoundCount()
106 if ( iDevSoundCount == 0 )
108 iDelayAudioServerShutDown->SetDelay(TTimeIntervalMicroSeconds32(KAudioServerShutDownDelay));
112 void CMMFAudioServer::SendEventToClient(TInt aSessionToAlert, TInt /*aSessionToBeLaunched*/)
114 // For the session requested, send event to client
115 iSessionIter.SetToFirst();
116 CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
117 while (session != NULL)
119 if (session->AudioServerSessionId() == aSessionToAlert)
123 session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
127 void CMMFAudioServer::LaunchRequest(TInt aSessionId)
129 iSessionIter.SetToFirst();
130 CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
131 while (session != NULL)
133 if (session->AudioServerSessionId() == aSessionId)
137 session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
141 TInt CMMFAudioServer::StartDevSoundServerL(TName& aDevSoundName) const
143 RMessage2 message(Message());
144 CStartAndMonitorDevSoundThread* monitorDevSound = NULL;
146 TRAPD(err, monitorDevSound = CStartAndMonitorDevSoundThread::NewL(const_cast<CMMFAudioServer*>(this)));
149 delete monitorDevSound;
153 iDevSoundServList.Append(monitorDevSound);
155 CMMFAudioServer* mutableThis = const_cast<CMMFAudioServer*>(this);
156 mutableThis->iGlobalNum+=1;
158 return monitorDevSound->StartDevSoundServerL(message, aDevSoundName, iGlobalNum);
163 CMMFAudioServer::CDelayAudioServerShutDown* CMMFAudioServer::CDelayAudioServerShutDown::NewL()
165 CDelayAudioServerShutDown* self = new(ELeave) CDelayAudioServerShutDown;
166 CleanupStack::PushL(self);
172 CMMFAudioServer::CDelayAudioServerShutDown::CDelayAudioServerShutDown() : CActive(0)
176 void CMMFAudioServer::CDelayAudioServerShutDown::ConstructL()
178 User::LeaveIfError(iShutDownTimer.CreateLocal());
179 CActiveScheduler::Add(this);
182 CMMFAudioServer::CDelayAudioServerShutDown::~CDelayAudioServerShutDown()
185 iShutDownTimer.Close();
189 void CMMFAudioServer::CDelayAudioServerShutDown::SetDelay(TTimeIntervalMicroSeconds32 aDelay)
191 __ASSERT_ALWAYS(!IsActive(), User::Panic(_L("CDelayedShutDown"), 1));
192 iShutDownTimer.After(iStatus, aDelay);
196 void CMMFAudioServer::CDelayAudioServerShutDown::RunL()
198 CActiveScheduler::Stop();
202 void CMMFAudioServer::CDelayAudioServerShutDown::DoCancel()
204 iShutDownTimer.Cancel();
208 CStartAndMonitorDevSoundThread* CStartAndMonitorDevSoundThread::NewL(CMMFAudioServer* aAudioServer)
210 CStartAndMonitorDevSoundThread* self = new(ELeave) CStartAndMonitorDevSoundThread(aAudioServer);
211 CleanupStack::PushL(self);
217 void CStartAndMonitorDevSoundThread::ConstructL()
219 CActiveScheduler::Add(this);
222 TInt CStartAndMonitorDevSoundThread::StartDevSoundServerL(RMessage2& aMessage, TName& aDevSoundName, TInt aUniqueNum)
224 RThread clientThread;
225 aMessage.ClientL(clientThread);
226 RProcess clientProcess;
227 User::LeaveIfError(clientThread.Process(clientProcess));
228 TProcessId clientProcessId(clientProcess.Id());
230 clientThread.Close();
231 clientProcess.Close();
233 TDevSoundServerStart start(iAudioServer, clientProcessId);//Pass to DevSoundServer the clien't process ID
235 const TUidType serverUid(KNullUid,KNullUid,KUidDevSoundServer);
237 TThreadFunction serverFunc=CMMFDevSoundServer::StartThread;
239 // To deal with the unique thread (+semaphore!) naming in EPOC, and that we may
240 // be trying to restart a server that has just exited we attempt to create a
241 // unique thread name for the server.
245 TName name(KDevSoundServerName);
246 name.AppendNum(TInt(this),EHex);
247 name.Append(TChar('.'));
248 name.AppendNum(aUniqueNum,EHex);
249 err = iServer.Create(name, serverFunc, KDevSoundServerStackSize,
250 KDevSoundServerInitHeapSize, KDevSoundServerMaxHeapSize,
251 &start, EOwnerProcess);
256 // Synchronise with the server
257 TRequestStatus reqStatus;
258 iServer.Rendezvous(reqStatus);
260 if (reqStatus!=KRequestPending)
266 // Start the test harness
268 // Server will call the reciprocal static synchronise call
271 User::WaitForRequest(reqStatus); // wait for start or death
272 if(reqStatus.Int() != KErrNone)
275 return reqStatus.Int();
278 aDevSoundName = iServer.Name();
280 iServer.Logon(iStatus);
283 iAudioServer->IncrementDevSoundCount();
288 CStartAndMonitorDevSoundThread::CStartAndMonitorDevSoundThread(CMMFAudioServer* aAudioServer): CActive(0)
290 iAudioServer = aAudioServer;
293 CStartAndMonitorDevSoundThread::~CStartAndMonitorDevSoundThread()
298 void CStartAndMonitorDevSoundThread::RunL()
301 iAudioServer->DecrementDevSoundCount();
304 void CStartAndMonitorDevSoundThread::DoCancel()
306 iServer.LogonCancel(iStatus);