lafagnosticuifoundation/cone/src/coesndpy.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 1997-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <mda/client/utility.h>
       
    17 #include <mdaaudiosampleplayer.h>
       
    18 #include <mdaaudiotoneplayer.h>
       
    19 #include <bassnd.h>
       
    20 #include <coesndpy.h>
       
    21 #include <coemain.h>
       
    22 #include "coepanic.h"
       
    23 #include <coeutils.h>
       
    24 
       
    25 const TUid KLafSoundPlayerUid={0x10005F1A};
       
    26 
       
    27 class CCoeSoundPlayer;
       
    28 
       
    29 //
       
    30 // class MCoeSoundPlayerObserver
       
    31 //
       
    32 
       
    33 class MCoeSoundPlayerObserver
       
    34 	{
       
    35 public:
       
    36 	virtual void PlayEnded(const CCoeSoundPlayer& aPlayer)=0;
       
    37 	};
       
    38 
       
    39 //
       
    40 // class CCoeSoundPlayer
       
    41 //
       
    42 
       
    43 class CCoeSoundPlayer : public CBase
       
    44 	{
       
    45 public:
       
    46 	~CCoeSoundPlayer();
       
    47 public:
       
    48 	inline const TBaSystemSoundInfo& SoundInfo() const;
       
    49 	inline TBool IsPlaying() const;
       
    50 	virtual void StartPlay()=0;
       
    51 protected:
       
    52 	CCoeSoundPlayer(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
    53 						TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap);
       
    54 	void BaseConstructL();
       
    55 	static TInt PlayTimerCallBack(TAny* aSelf);
       
    56 private:
       
    57 	virtual void Play()=0;
       
    58 protected:
       
    59 	MCoeSoundPlayerObserver& iObserver;
       
    60 	TBaSystemSoundInfo iSoundInfo;
       
    61 	TInt iPlayCount;
       
    62 	TTimeIntervalMicroSeconds32 iGap;
       
    63 	CPeriodic* iTimer;
       
    64 	TBool iPlaying;
       
    65 	};
       
    66 
       
    67 inline const TBaSystemSoundInfo& CCoeSoundPlayer::SoundInfo() const
       
    68 	{return iSoundInfo;}
       
    69 inline TBool CCoeSoundPlayer::IsPlaying() const
       
    70 	{return iPlaying;}
       
    71 
       
    72 CCoeSoundPlayer::~CCoeSoundPlayer()
       
    73 	{
       
    74 	delete iTimer;
       
    75 	}
       
    76 
       
    77 CCoeSoundPlayer::CCoeSoundPlayer(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
    78 									TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap)
       
    79 	: iObserver(aObserver), iSoundInfo(aInfo), iPlayCount(aPlayCount), iGap(aGap), iPlaying(EFalse)
       
    80 	{}
       
    81 
       
    82 void CCoeSoundPlayer::BaseConstructL()
       
    83 	{
       
    84 	}
       
    85 
       
    86 TInt CCoeSoundPlayer::PlayTimerCallBack(TAny* aSelf)
       
    87 	{ // static
       
    88 	CCoeSoundPlayer* player=REINTERPRET_CAST(CCoeSoundPlayer*,aSelf);
       
    89 	player->iTimer->Cancel();
       
    90 	player->Play();
       
    91 	return 0;
       
    92 	}
       
    93 
       
    94 //
       
    95 // class CCoeTonePlayer
       
    96 //
       
    97 
       
    98 class CCoeTonePlayer : public CCoeSoundPlayer, public MMdaAudioToneObserver
       
    99 	{
       
   100 public:
       
   101 	static CCoeTonePlayer* NewLC(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   102 									CMdaServer& aMdaServer,TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap);
       
   103 	~CCoeTonePlayer();
       
   104 private:
       
   105 	CCoeTonePlayer(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   106 						TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap);
       
   107 	void ConstructL(CMdaServer& aMdaServer);
       
   108 private: // from CCoeSoundPlayer
       
   109 	void StartPlay();
       
   110 	void Play();
       
   111 private: // from MMdaAudioToneObserver
       
   112 	void MatoPrepareComplete(TInt aError);
       
   113 	void MatoPlayComplete(TInt aError);
       
   114 private:
       
   115 	CMdaAudioToneUtility* iPlayUtility;
       
   116 	};
       
   117 
       
   118 CCoeTonePlayer* CCoeTonePlayer::NewLC(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   119 								CMdaServer& aMdaServer,TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap)
       
   120 	{ // static
       
   121 	CCoeTonePlayer* self=new(ELeave) CCoeTonePlayer(aObserver,aInfo,aPlayCount,aGap);
       
   122 	CleanupStack::PushL(self);
       
   123 	self->ConstructL(aMdaServer);
       
   124 	return self;
       
   125 	}
       
   126 
       
   127 CCoeTonePlayer::~CCoeTonePlayer()
       
   128 	{
       
   129 	delete iPlayUtility;
       
   130 	}
       
   131 
       
   132 CCoeTonePlayer::CCoeTonePlayer(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   133 					TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap)
       
   134 	: CCoeSoundPlayer(aObserver,aInfo,aPlayCount,aGap)
       
   135 	{}
       
   136 
       
   137 void CCoeTonePlayer::ConstructL(CMdaServer& aMdaServer)
       
   138 	{
       
   139 	iPlayUtility=CMdaAudioToneUtility::NewL(*this,&aMdaServer);
       
   140 	BaseConstructL();
       
   141 	}
       
   142 
       
   143 void CCoeTonePlayer::StartPlay()
       
   144 	{
       
   145 	TBaSystemSoundInfo::TSoundCategory soundCat=iSoundInfo.SoundCategory();
       
   146 	if (soundCat==TBaSystemSoundInfo::ESequence)
       
   147 		iPlayUtility->PrepareToPlayFixedSequence(iSoundInfo.FixedSequenceNumber());
       
   148 	else if (soundCat==TBaSystemSoundInfo::ETone)
       
   149 		{
       
   150 		TBaSystemSoundInfo::TTone tone=iSoundInfo.Tone();
       
   151 		TInt64 time(tone.iDuration.Int());
       
   152 		TTimeIntervalMicroSeconds duration(time);
       
   153 		iPlayUtility->PrepareToPlayTone(tone.iFrequency,duration);
       
   154 		}
       
   155 	else
       
   156 		iObserver.PlayEnded(*this);
       
   157 	}
       
   158 
       
   159 void CCoeTonePlayer::Play()
       
   160 	{
       
   161     iPlayUtility->SetVolume(iSoundInfo.iVolume);
       
   162 	iPlayUtility->Play();
       
   163 	}
       
   164 
       
   165 void CCoeTonePlayer::MatoPrepareComplete(TInt aError)
       
   166 	{
       
   167 	if (aError == KErrNone)
       
   168 		{
       
   169 		iPlayUtility->SetVolume(iSoundInfo.iVolume);
       
   170 		iPlayUtility->SetPriority(iSoundInfo.iPriority,EMdaPriorityPreferenceNone);
       
   171 		iPlaying=ETrue;
       
   172 		if (--iPlayCount>0)
       
   173 			{
       
   174 			TInt64 val(iGap.Int());
       
   175 			TTimeIntervalMicroSeconds gap(val);
       
   176 			iPlayUtility->SetRepeats(iPlayCount,gap);
       
   177 			}
       
   178 		iPlayUtility->Play();
       
   179 		}
       
   180 	else
       
   181 		iObserver.PlayEnded(*this);
       
   182 	}
       
   183 
       
   184 void CCoeTonePlayer::MatoPlayComplete(TInt /*aError*/)
       
   185 	{
       
   186 	iObserver.PlayEnded(*this);
       
   187 	}
       
   188 
       
   189 //
       
   190 // class CCoeFilePlayer
       
   191 //
       
   192 
       
   193 class CCoeFilePlayer : public CCoeSoundPlayer, public MMdaAudioPlayerCallback
       
   194 	{
       
   195 public:
       
   196 	static CCoeFilePlayer* NewLC(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   197 									TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap);
       
   198 	~CCoeFilePlayer();
       
   199 private:
       
   200 	CCoeFilePlayer(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   201 						TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap);
       
   202 	void ConstructL();
       
   203 private: // from CCoeSoundPlayer
       
   204 	void StartPlay();
       
   205 	void Play();
       
   206 private: // from MMdaAudioPlayerCallback
       
   207 	void MapcInitComplete(TInt aStatus, const TTimeIntervalMicroSeconds& aDuration);
       
   208 	void MapcPlayComplete(TInt aErr);
       
   209 private:
       
   210 	CMdaAudioPlayerUtility* iPlayUtility;
       
   211 	TBool iReadyToPlay;
       
   212 	};
       
   213 
       
   214 CCoeFilePlayer* CCoeFilePlayer::NewLC(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   215 								TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap)
       
   216 	{ // static
       
   217 	CCoeFilePlayer* self=new(ELeave) CCoeFilePlayer(aObserver,aInfo,aPlayCount,aGap);
       
   218 	CleanupStack::PushL(self);
       
   219 	self->ConstructL();
       
   220 	return self;
       
   221 	}
       
   222 
       
   223 CCoeFilePlayer::~CCoeFilePlayer()
       
   224 	{
       
   225 	delete iPlayUtility;
       
   226 	}
       
   227 
       
   228 CCoeFilePlayer::CCoeFilePlayer(MCoeSoundPlayerObserver& aObserver,const TBaSystemSoundInfo& aInfo,
       
   229 					TInt aPlayCount,TTimeIntervalMicroSeconds32 aGap)
       
   230 	: CCoeSoundPlayer(aObserver,aInfo,aPlayCount,aGap)
       
   231 	{}
       
   232 
       
   233 void CCoeFilePlayer::ConstructL()
       
   234 	{
       
   235 	BaseConstructL();
       
   236 	TFileName name=iSoundInfo.FileName();
       
   237 	iPlayUtility=CMdaAudioPlayerUtility::NewFilePlayerL(name,*this,iSoundInfo.iPriority);
       
   238 	}
       
   239 
       
   240 void CCoeFilePlayer::StartPlay()
       
   241 	{
       
   242 	if (iReadyToPlay)
       
   243 		Play();
       
   244 	else
       
   245 		iReadyToPlay=ETrue;
       
   246 	}
       
   247 
       
   248 void CCoeFilePlayer::Play()
       
   249 	{
       
   250 	if(iSoundInfo.iVolume > iPlayUtility->MaxVolume() )
       
   251 		{
       
   252 		iSoundInfo.iVolume = iPlayUtility->MaxVolume();
       
   253 		}
       
   254 	iPlayUtility->SetVolume(iSoundInfo.iVolume); 
       
   255 	iPlaying=ETrue;
       
   256 	iPlayUtility->Play();
       
   257 	}
       
   258 
       
   259 void CCoeFilePlayer::MapcInitComplete(TInt aStatus,const TTimeIntervalMicroSeconds& /*aDuration*/)
       
   260 	{
       
   261 	if (aStatus!=KErrNone)
       
   262 		iObserver.PlayEnded(*this);
       
   263 	else
       
   264 		{
       
   265 		iPlayUtility->SetVolume(iSoundInfo.iVolume);
       
   266 		if (--iPlayCount>0)
       
   267 			{
       
   268 			TInt64 val(iGap.Int());
       
   269 			TTimeIntervalMicroSeconds gap(val);
       
   270 			iPlayUtility->SetRepeats(iPlayCount,gap);
       
   271 			}
       
   272  		if (iReadyToPlay)
       
   273 			{
       
   274 			Play();
       
   275 			}
       
   276  		else
       
   277 			{
       
   278 			iReadyToPlay=ETrue;
       
   279 			}
       
   280    		}
       
   281 
       
   282 	}
       
   283 
       
   284 void CCoeFilePlayer::MapcPlayComplete(TInt /*aErr*/)
       
   285 	{
       
   286 	iObserver.PlayEnded(*this);
       
   287 	}
       
   288 
       
   289 //
       
   290 // class CCoeSoundPlayerManager
       
   291 //
       
   292 
       
   293 class CCoeSoundPlayerManager : public CCoeStatic, public MCoeSoundPlayerObserver
       
   294 	{
       
   295 public:
       
   296 	static CCoeSoundPlayerManager* NewL();
       
   297 	void PlaySoundL(const TBaSystemSoundType& aType,TInt aPlayCount,const TTimeIntervalMicroSeconds32& aGap,TBool aInterrupt);
       
   298 	void CancelSound(const TBaSystemSoundType& aType);
       
   299 private: // from MCoeSoundPlayerObserver
       
   300 	void PlayEnded(const CCoeSoundPlayer& aPlayer);
       
   301 private:
       
   302 	CCoeSoundPlayerManager();
       
   303 	~CCoeSoundPlayerManager();
       
   304 	void ConstructL();
       
   305 private:
       
   306 	CArrayPtrFlat<CCoeSoundPlayer> iPlayers;
       
   307 	CMdaServer* iMdaServer;
       
   308 	};
       
   309 
       
   310 CCoeSoundPlayerManager* CCoeSoundPlayerManager::NewL()
       
   311 	{ // static
       
   312 	CCoeSoundPlayerManager* self=new(ELeave) CCoeSoundPlayerManager();
       
   313 	CleanupStack::PushL(self);
       
   314 	self->ConstructL();
       
   315 	CleanupStack::Pop(); // self
       
   316 	return self;
       
   317 	}
       
   318 
       
   319 /*
       
   320  * Initialize the sound matching aType.  Interrupt any lower priority sound but, if a higher or 
       
   321  * equal priority sound is already playing, then wait for it to finish
       
   322  *
       
   323  */
       
   324 void CCoeSoundPlayerManager::PlaySoundL(const TBaSystemSoundType& aType,TInt aPlayCount,
       
   325 												const TTimeIntervalMicroSeconds32& aGap,TBool aInterrupt)
       
   326 	{
       
   327 	TBaSystemSoundInfo info;
       
   328 	User::LeaveIfError( BaSystemSound::GetSound(CCoeEnv::Static()->FsSession(),aType,info) );
       
   329 	if (info.SoundCategory()==TBaSystemSoundInfo::EFile)
       
   330 		{
       
   331 		TBaSystemSoundName fileName=info.FileName();
       
   332 		if (aType.iMinor!=KNullUid && !ConeUtils::FileExists(fileName))
       
   333 			{
       
   334 			TBaSystemSoundType defaultType(aType.iMajor,KNullUid);
       
   335 		 	User::LeaveIfError( BaSystemSound::GetSound(CCoeEnv::Static()->FsSession(),defaultType,info) );
       
   336 			}
       
   337 		}
       
   338 	if (info.iVolume == 0) //do not play the sound, if the volume is set to silent.
       
   339 		return;
       
   340 	info.iType=aType; // clients will try to cancel this sound using aType rather than whatever sound was actually found
       
   341 	TInt count=iPlayers.Count();
       
   342 	if (count)
       
   343 		{
       
   344 		CCoeSoundPlayer* player=iPlayers[0];
       
   345 		if (aInterrupt || player->SoundInfo().iPriority<info.iPriority)
       
   346 			{
       
   347 			delete player;
       
   348 			iPlayers.Delete(0);
       
   349 			--count;
       
   350 			}
       
   351 		}
       
   352 	CCoeSoundPlayer* player=NULL;
       
   353 	if (info.SoundCategory()==TBaSystemSoundInfo::EFile)
       
   354 		player=CCoeFilePlayer::NewLC(*this,info,aPlayCount,aGap);
       
   355 	else
       
   356 		player=CCoeTonePlayer::NewLC(*this,info,*iMdaServer,aPlayCount,aGap);
       
   357 	TInt ii=0;
       
   358 	for (;ii<count;ii++)
       
   359 		{
       
   360 		if (iPlayers[ii]->SoundInfo().iPriority<info.iPriority)
       
   361 			{
       
   362 			iPlayers.InsertL(ii,player);
       
   363 			break;
       
   364 			}
       
   365 		}
       
   366 	if (iPlayers.Count()==count) // we haven't managed to insert it anywhere
       
   367 		iPlayers.AppendL(player);
       
   368 	CleanupStack::Pop(); // player
       
   369 	if (ii==0)
       
   370 		iPlayers[0]->StartPlay();
       
   371 	}
       
   372 
       
   373 void CCoeSoundPlayerManager::CancelSound(const TBaSystemSoundType& aType)
       
   374 	{
       
   375 	TInt loop=0;
       
   376 	while (loop<iPlayers.Count())
       
   377 		{
       
   378 		CCoeSoundPlayer* player=iPlayers[loop];
       
   379 		if (player->SoundInfo().iType==aType)
       
   380 			{
       
   381 			delete player;
       
   382 			iPlayers.Delete(loop);
       
   383 			}
       
   384 		else
       
   385 			++loop;
       
   386 		}
       
   387 	if (iPlayers.Count()==0)
       
   388 		delete this;
       
   389 	}
       
   390 
       
   391 void CCoeSoundPlayerManager::PlayEnded(const CCoeSoundPlayer& aPlayer)
       
   392 // don't need to check the actaul player that's finished as it's always
       
   393 // index 0.  Leave the code alone for now in case we change once apps
       
   394 // start using it
       
   395 	{
       
   396 	TInt ii=-1;
       
   397 	FOREVER
       
   398 		{
       
   399 		CCoeSoundPlayer* player=iPlayers[++ii];
       
   400 		if (&aPlayer==player)
       
   401 			{
       
   402 			delete player;
       
   403 			iPlayers.Delete(ii);
       
   404 			break;
       
   405 			}
       
   406 		}
       
   407 	if (iPlayers.Count())
       
   408 		iPlayers[0]->StartPlay();
       
   409 	else
       
   410 		delete this; // no more sounds to play so allow media server to close
       
   411 	}
       
   412 
       
   413 CCoeSoundPlayerManager::CCoeSoundPlayerManager()
       
   414 	: CCoeStatic(KLafSoundPlayerUid), iPlayers(1)
       
   415 	{}
       
   416 
       
   417 CCoeSoundPlayerManager::~CCoeSoundPlayerManager()
       
   418 	{
       
   419 	iPlayers.ResetAndDestroy();
       
   420 	delete iMdaServer;
       
   421 	}
       
   422 
       
   423 void CCoeSoundPlayerManager::ConstructL()
       
   424 	{
       
   425 	iMdaServer=CMdaServer::NewL();
       
   426 	}
       
   427 
       
   428 //
       
   429 // class CoeSoundPlayer
       
   430 //
       
   431 
       
   432 EXPORT_C void CoeSoundPlayer::PlaySound(const TBaSystemSoundType& aType,TInt aPlayCount,
       
   433 											TTimeIntervalMicroSeconds32 aGap,TBool aInterrupt)
       
   434 	{ // static
       
   435 	TRAP_IGNORE(ManagerL()->PlaySoundL(aType,aPlayCount,aGap,aInterrupt));
       
   436 	}
       
   437 
       
   438 EXPORT_C void CoeSoundPlayer::CancelSound(const TBaSystemSoundType& aType)
       
   439 /** Stops playing the specified sound.
       
   440 
       
   441 @param aType The sound to stop playing. */
       
   442 	{ // static
       
   443 	TRAP_IGNORE(ManagerL()->CancelSound(aType));
       
   444 	}
       
   445 
       
   446 CCoeSoundPlayerManager* CoeSoundPlayer::ManagerL()
       
   447 	{ // static
       
   448 	CCoeEnv* env=CCoeEnv::Static();
       
   449 	__ASSERT_ALWAYS(env,Panic(ECoePanicNullEnvironment));
       
   450 	CCoeSoundPlayerManager* manager=
       
   451 		STATIC_CAST(CCoeSoundPlayerManager*,env->FindStatic(KLafSoundPlayerUid));
       
   452 	if (!manager)
       
   453 		manager=CCoeSoundPlayerManager::NewL();
       
   454 	return manager;
       
   455 	}