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