os/mm/devsound/sounddevbt/PlatSec/src/Server/AudioServer/MmfBtAudioServer.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <e32math.h>
sl@0
    17
#include "MmfBtAudioServer.h"
sl@0
    18
#include "MmfBtAudioServerStart.h"
sl@0
    19
#include "MmfBtDevSoundSession.h"
sl@0
    20
#include "MmfBtAudioServerSession.h"
sl@0
    21
#include "MmfBtDevSoundServer.h"
sl@0
    22
#include "MmfBtDevSoundServerStart.h"
sl@0
    23
#include "../../../../inc/common/mmfBtBase.hrh"
sl@0
    24
sl@0
    25
sl@0
    26
static const TUid KUidDevSoundServer = {KUidMmfBtDevSoundServerDllUnicodeDefine};
sl@0
    27
sl@0
    28
const TInt KAudioServerShutDownDelay = 50000000; //50 sec
sl@0
    29
sl@0
    30
sl@0
    31
CMMFAudioServer* CMMFAudioServer::NewL()
sl@0
    32
	{
sl@0
    33
	CMMFAudioServer* s = new(ELeave) CMMFAudioServer();
sl@0
    34
	CleanupStack::PushL(s);
sl@0
    35
	s->ConstructL();
sl@0
    36
	CleanupStack::Pop();
sl@0
    37
	return s;
sl@0
    38
	}
sl@0
    39
sl@0
    40
CMMFAudioServer::CMMFAudioServer() :
sl@0
    41
	CMmfIpcServer(EPriorityStandard)
sl@0
    42
	{
sl@0
    43
	}
sl@0
    44
sl@0
    45
void CMMFAudioServer::ConstructL()
sl@0
    46
	{
sl@0
    47
	iDelayAudioServerShutDown = CDelayAudioServerShutDown::NewL();
sl@0
    48
	// Call base class to Start server
sl@0
    49
	StartL(KAudioServerName);
sl@0
    50
	}
sl@0
    51
sl@0
    52
CMMFAudioServer::~CMMFAudioServer()
sl@0
    53
	{
sl@0
    54
	if (iDelayAudioServerShutDown)
sl@0
    55
		{
sl@0
    56
		iDelayAudioServerShutDown->Cancel();
sl@0
    57
		delete iDelayAudioServerShutDown;
sl@0
    58
		}
sl@0
    59
	iDevSoundServList.Close();
sl@0
    60
	iPolicyServerHandle.Close();
sl@0
    61
	}
sl@0
    62
sl@0
    63
CMmfIpcSession* CMMFAudioServer::NewSessionL(const TVersion& aVersion) const
sl@0
    64
	{
sl@0
    65
	TVersion v(KMMFAudioServerVersion,KMMFAudioServerMinorVersionNumber,KMMFAudioServerBuildVersionNumber);
sl@0
    66
	if(!User::QueryVersionSupported(v, aVersion))
sl@0
    67
		User::Leave(KErrNotSupported);
sl@0
    68
sl@0
    69
	for(TInt i=0; i<iDevSoundServList.Count(); i++)
sl@0
    70
		{
sl@0
    71
		CStartAndMonitorDevSoundThread* devSoundMonitorThread = iDevSoundServList[i];
sl@0
    72
		if(!devSoundMonitorThread->IsActive())
sl@0
    73
			{
sl@0
    74
			iDevSoundServList.Remove(i);
sl@0
    75
			delete devSoundMonitorThread;
sl@0
    76
			}
sl@0
    77
		iDevSoundServList.Compress();
sl@0
    78
		}
sl@0
    79
sl@0
    80
	TName devSoundServerName;
sl@0
    81
	User::LeaveIfError(StartDevSoundServerL(devSoundServerName));
sl@0
    82
	CMMFAudioServerSession* audioServerSession = CMMFAudioServerSession::NewL(devSoundServerName);
sl@0
    83
	return audioServerSession;
sl@0
    84
	}
sl@0
    85
sl@0
    86
void CMMFAudioServer::IncrementSessionId()
sl@0
    87
	{
sl@0
    88
	iAudioServerSessionId++;
sl@0
    89
	}
sl@0
    90
sl@0
    91
void CMMFAudioServer::DecrementSessionId()
sl@0
    92
	{
sl@0
    93
	iAudioServerSessionId--;
sl@0
    94
	}
sl@0
    95
sl@0
    96
void CMMFAudioServer::IncrementDevSoundCount()
sl@0
    97
	{
sl@0
    98
	iDevSoundCount++;
sl@0
    99
	if(iDevSoundCount)
sl@0
   100
		iDelayAudioServerShutDown->Cancel(); //in the case we started the shutdown due to no more DevSound
sl@0
   101
	}
sl@0
   102
sl@0
   103
void CMMFAudioServer::DecrementDevSoundCount()
sl@0
   104
	{
sl@0
   105
	iDevSoundCount--;
sl@0
   106
    if ( iDevSoundCount == 0 )
sl@0
   107
		{
sl@0
   108
		iDelayAudioServerShutDown->SetDelay(TTimeIntervalMicroSeconds32(KAudioServerShutDownDelay));
sl@0
   109
 		}
sl@0
   110
	}
sl@0
   111
sl@0
   112
void CMMFAudioServer::SendEventToClient(TInt aSessionToAlert, TInt /*aSessionToBeLaunched*/)
sl@0
   113
	{
sl@0
   114
	// For the session requested, send event to client
sl@0
   115
	iSessionIter.SetToFirst();
sl@0
   116
	CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
sl@0
   117
	while (session != NULL)
sl@0
   118
		{
sl@0
   119
		if (session->AudioServerSessionId() == aSessionToAlert)
sl@0
   120
			{
sl@0
   121
			break;  // Finished
sl@0
   122
			}
sl@0
   123
		session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
sl@0
   124
		}
sl@0
   125
	}
sl@0
   126
sl@0
   127
void CMMFAudioServer::LaunchRequest(TInt aSessionId)
sl@0
   128
	{
sl@0
   129
	iSessionIter.SetToFirst();
sl@0
   130
	CMMFAudioServerSession* session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
sl@0
   131
	while (session != NULL)
sl@0
   132
		{
sl@0
   133
		if (session->AudioServerSessionId() == aSessionId)
sl@0
   134
			{
sl@0
   135
			break;  // Finished
sl@0
   136
			}
sl@0
   137
		session = static_cast<CMMFAudioServerSession*>(iSessionIter++);
sl@0
   138
		}
sl@0
   139
	}
sl@0
   140
sl@0
   141
TInt CMMFAudioServer::StartDevSoundServerL(TName& aDevSoundName) const
sl@0
   142
	{
sl@0
   143
	RMessage2 message(Message());
sl@0
   144
	CStartAndMonitorDevSoundThread* monitorDevSound = NULL;
sl@0
   145
sl@0
   146
	TRAPD(err, monitorDevSound = CStartAndMonitorDevSoundThread::NewL(const_cast<CMMFAudioServer*>(this)));
sl@0
   147
	if(err != KErrNone)
sl@0
   148
		{
sl@0
   149
		delete monitorDevSound;
sl@0
   150
		return err;
sl@0
   151
		}
sl@0
   152
sl@0
   153
	iDevSoundServList.Append(monitorDevSound);
sl@0
   154
	
sl@0
   155
	CMMFAudioServer* mutableThis = const_cast<CMMFAudioServer*>(this);
sl@0
   156
	mutableThis->iGlobalNum+=1;
sl@0
   157
sl@0
   158
	return monitorDevSound->StartDevSoundServerL(message, aDevSoundName, iGlobalNum);
sl@0
   159
sl@0
   160
	}
sl@0
   161
sl@0
   162
sl@0
   163
CMMFAudioServer::CDelayAudioServerShutDown* CMMFAudioServer::CDelayAudioServerShutDown::NewL()
sl@0
   164
	{
sl@0
   165
	CDelayAudioServerShutDown* self = new(ELeave) CDelayAudioServerShutDown;
sl@0
   166
	CleanupStack::PushL(self);
sl@0
   167
	self->ConstructL();
sl@0
   168
	CleanupStack::Pop();
sl@0
   169
	return self;
sl@0
   170
	}
sl@0
   171
sl@0
   172
CMMFAudioServer::CDelayAudioServerShutDown::CDelayAudioServerShutDown() : CActive(0)
sl@0
   173
	{
sl@0
   174
	}
sl@0
   175
        
sl@0
   176
void CMMFAudioServer::CDelayAudioServerShutDown::ConstructL()
sl@0
   177
	{
sl@0
   178
	User::LeaveIfError(iShutDownTimer.CreateLocal());
sl@0
   179
	CActiveScheduler::Add(this);
sl@0
   180
	}
sl@0
   181
sl@0
   182
CMMFAudioServer::CDelayAudioServerShutDown::~CDelayAudioServerShutDown()
sl@0
   183
	{
sl@0
   184
	Cancel();
sl@0
   185
	iShutDownTimer.Close();
sl@0
   186
	}
sl@0
   187
sl@0
   188
sl@0
   189
void CMMFAudioServer::CDelayAudioServerShutDown::SetDelay(TTimeIntervalMicroSeconds32 aDelay)
sl@0
   190
	{
sl@0
   191
	__ASSERT_ALWAYS(!IsActive(), User::Panic(_L("CDelayedShutDown"), 1));
sl@0
   192
	iShutDownTimer.After(iStatus, aDelay);
sl@0
   193
	SetActive();
sl@0
   194
	}
sl@0
   195
sl@0
   196
void CMMFAudioServer::CDelayAudioServerShutDown::RunL()
sl@0
   197
	{
sl@0
   198
	CActiveScheduler::Stop();
sl@0
   199
	}
sl@0
   200
sl@0
   201
sl@0
   202
void CMMFAudioServer::CDelayAudioServerShutDown::DoCancel()
sl@0
   203
	{
sl@0
   204
	iShutDownTimer.Cancel();
sl@0
   205
	}
sl@0
   206
sl@0
   207
sl@0
   208
CStartAndMonitorDevSoundThread* CStartAndMonitorDevSoundThread::NewL(CMMFAudioServer* aAudioServer)
sl@0
   209
	{
sl@0
   210
	CStartAndMonitorDevSoundThread* self = new(ELeave) CStartAndMonitorDevSoundThread(aAudioServer);
sl@0
   211
	CleanupStack::PushL(self);
sl@0
   212
	self->ConstructL();
sl@0
   213
	CleanupStack::Pop();
sl@0
   214
	return self;
sl@0
   215
	}
sl@0
   216
sl@0
   217
void CStartAndMonitorDevSoundThread::ConstructL()
sl@0
   218
	{
sl@0
   219
	CActiveScheduler::Add(this);
sl@0
   220
	}
sl@0
   221
	
sl@0
   222
TInt CStartAndMonitorDevSoundThread::StartDevSoundServerL(RMessage2& aMessage, TName& aDevSoundName, TInt aUniqueNum)
sl@0
   223
	{
sl@0
   224
	RThread clientThread;
sl@0
   225
	aMessage.ClientL(clientThread);
sl@0
   226
	RProcess clientProcess;
sl@0
   227
	User::LeaveIfError(clientThread.Process(clientProcess));
sl@0
   228
	TProcessId clientProcessId(clientProcess.Id());
sl@0
   229
sl@0
   230
	clientThread.Close();
sl@0
   231
	clientProcess.Close();
sl@0
   232
sl@0
   233
	TDevSoundServerStart start(iAudioServer, clientProcessId);//Pass to DevSoundServer the clien't process ID 
sl@0
   234
sl@0
   235
	const TUidType serverUid(KNullUid,KNullUid,KUidDevSoundServer);
sl@0
   236
	
sl@0
   237
	TThreadFunction serverFunc=CMMFDevSoundServer::StartThread;
sl@0
   238
	//
sl@0
   239
	// To deal with the unique thread (+semaphore!) naming in EPOC, and that we may
sl@0
   240
	// be trying to restart a server that has just exited we attempt to create a
sl@0
   241
	// unique thread name for the server.
sl@0
   242
	//
sl@0
   243
sl@0
   244
	TInt err = KErrNone;
sl@0
   245
	TName name(KDevSoundServerName);
sl@0
   246
	name.AppendNum(TInt(this),EHex);
sl@0
   247
	name.Append(TChar('.'));
sl@0
   248
	name.AppendNum(aUniqueNum,EHex);
sl@0
   249
	err = iServer.Create(name, serverFunc, KDevSoundServerStackSize,
sl@0
   250
	                	KDevSoundServerInitHeapSize, KDevSoundServerMaxHeapSize,
sl@0
   251
	                	&start, EOwnerProcess);
sl@0
   252
				
sl@0
   253
sl@0
   254
	if(err != KErrNone)
sl@0
   255
		return err;
sl@0
   256
	// Synchronise with the server
sl@0
   257
	TRequestStatus reqStatus;
sl@0
   258
	iServer.Rendezvous(reqStatus);
sl@0
   259
	
sl@0
   260
	if (reqStatus!=KRequestPending)
sl@0
   261
		{
sl@0
   262
		iServer.Kill(0);
sl@0
   263
		}
sl@0
   264
	else
sl@0
   265
		{
sl@0
   266
		// Start the test harness
sl@0
   267
		iServer.Resume();
sl@0
   268
		// Server will call the reciprocal static synchronise call	
sl@0
   269
		}
sl@0
   270
sl@0
   271
	User::WaitForRequest(reqStatus); // wait for start or death
sl@0
   272
	if(reqStatus.Int() != KErrNone)
sl@0
   273
		{
sl@0
   274
		iServer.Close();
sl@0
   275
		return reqStatus.Int();
sl@0
   276
		}
sl@0
   277
		
sl@0
   278
	aDevSoundName = iServer.Name();
sl@0
   279
sl@0
   280
	iServer.Logon(iStatus);
sl@0
   281
	SetActive();
sl@0
   282
sl@0
   283
	iAudioServer->IncrementDevSoundCount();
sl@0
   284
	return KErrNone;
sl@0
   285
	}
sl@0
   286
sl@0
   287
sl@0
   288
CStartAndMonitorDevSoundThread::CStartAndMonitorDevSoundThread(CMMFAudioServer* aAudioServer): CActive(0)
sl@0
   289
	{
sl@0
   290
	iAudioServer = aAudioServer;
sl@0
   291
	}
sl@0
   292
sl@0
   293
CStartAndMonitorDevSoundThread::~CStartAndMonitorDevSoundThread()
sl@0
   294
	{
sl@0
   295
	Cancel();
sl@0
   296
	}
sl@0
   297
    
sl@0
   298
void CStartAndMonitorDevSoundThread::RunL()
sl@0
   299
	{
sl@0
   300
	iServer.Close();
sl@0
   301
	iAudioServer->DecrementDevSoundCount();
sl@0
   302
	}
sl@0
   303
sl@0
   304
void CStartAndMonitorDevSoundThread::DoCancel()
sl@0
   305
	{
sl@0
   306
	iServer.LogonCancel(iStatus);
sl@0
   307
	}
sl@0
   308
sl@0
   309
sl@0
   310