examples/Multimedia/AudioClientEx/CMediaClientAppUI.cpp

00001 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // MediaClientAppUI.cpp
00015 //
00016 
00017 #include <audioclientex.rsg>
00018 #include "MediaClient.hrh"
00019 #include "CMediaClientAppUI.h"
00020 #include "CMediaClientView.h"
00021 #include "CMediaClientDocument.h"
00022 #include "CMediaClientEngine.h"
00023 
00024 #include <eikenv.h>
00025 #include <eikproc.h>
00026 #include <eikfnlab.h>
00027 #include <eiktbar.h>
00028 #include <eikapp.h>
00029 #include <eikdialg.h>
00030 #include <eikcfdlg.h>
00031 #include <eikon.rsg>
00032 #include <coeutils.h>
00033 #include <datastor.h>
00034 #include <eikmfne.h>
00035 #include <eikmenup.h>
00036 #include <eiklabel.h>
00037 #include <eikchlst.h>
00038 #include <badesca.h>
00039 
00041 _LIT(KError, "Error");
00043 _LIT(KIdle, "Ready");
00045 _LIT(KTonePrepare,"Initiating");
00047 _LIT(KTonePlaying, "Playing tone");
00049 _LIT(KPlayFilePrepare, "Initiating");
00051 _LIT(KPlayFilePlaying, "Playing file");
00053 _LIT(KRecordPrepare, "Initiating");
00055 _LIT(KRecording, "Recording file");
00057 _LIT(KPlayStreamPrepare, "Initiating");
00059 _LIT(KStreamPlaying, "Playing stream");
00061 _LIT(KConverting, "Converting file");
00062 
00063 // Initial object setup
00064 void CMediaClientAppUi::ConstructL() 
00065     {
00066     BaseConstructL();
00067 
00068         // set up view
00069     iAppView = CMediaClientView::NewL(ClientRect(), MediaClientEngine());
00070         iAppView -> SetDescription(KIdle);
00071     AddToStackL(iAppView); // app view should go onto control stack
00072 
00073         // set up filename buffer
00074         _LIT(KDefault,"\\");
00075         
00076         TDriveUnit sysDrive (RFs::GetSystemDrive());
00077         TDriveName sysDriveName (sysDrive.Name());      
00078         TFileName fileName(sysDriveName);
00079         fileName.Append(KDefault);
00080     iFileName = fileName;
00081 
00082         // set up link to engine
00083         MediaClientEngine().SetObserver(*this);
00084         iState = CMediaClientEngine::EIdle;
00085 
00086         // An asychronous call back object used to exit when a stream is playing
00087         // This allows media server time to clean up stream
00088         TCallBack callBack(CMediaClientAppUi::DoExit, this);
00089         iExitCallBack = new (ELeave) CAsyncCallBack(callBack,CActive::EPriorityLow);
00090     }
00091 
00092 
00093 CMediaClientAppUi::~CMediaClientAppUi() 
00094     {
00095         iMetaData.ResetAndDestroy();
00096         iMetaData.Close();
00097         StreamSourceCleanUp();
00098         RemoveFromStack(iAppView);
00099     delete iAppView;
00100         delete iExitCallBack;
00101         }
00102 
00103 TInt CMediaClientAppUi::DoExit(TAny* aUI)
00104         {
00105         static_cast<CMediaClientAppUi*>(aUI)->Exit();
00106         return 0;
00107         }
00108 
00109 // Gets engine
00110 CMediaClientEngine& CMediaClientAppUi::MediaClientEngine() const 
00111     { 
00112     return (static_cast<CMediaClientDocument*>(Document()))->Engine(); 
00113     }
00114 
00115 // Handle command requests
00116 void CMediaClientAppUi::HandleCommandL(TInt aCommand) 
00117     {
00118     switch (aCommand) 
00119         {
00120     case EMPCmdPlayTone:
00121                 ToneDialogL();
00122         break;
00123 
00124         case EMPCmdPlayFile:
00125                 PlayFileDialogL();
00126         break;
00127 
00128         case EMPCmdRecordFile:
00129                 RecordFileDialogL();
00130                 break;
00131 
00132         case EMPCmdPlayStream:
00133                 StreamSourceCleanUp();
00134                 iAudioStreamSource = CAudioStreamSource::NewL(iEikonEnv->FsSession(),
00135                         MediaClientEngine());
00136                 MediaClientEngine().PlayL();
00137                 break;
00138 
00139         case EMPCmdPlayStreamWithQueue:
00140                 StreamSourceCleanUp();
00141                 iAudioStreamSourceWithQueue = CAudioStreamSourceWithQueue::NewL(
00142                         iEikonEnv->FsSession(),
00143                         MediaClientEngine());
00144                 MediaClientEngine().PlayL();
00145                 break;
00146 
00147         case EMPCmdShowMetaData:
00148                 FileMetaDataL();
00149                 break;
00150 
00151         case EMPCmdConvertFile:
00152                 ConvertFileDialogL();
00153                 break;
00154 
00155         case EMPCmdVolumeLow:
00156                 MediaClientEngine().SetVolume(CMediaClientEngine::EQuiet);
00157                 break;
00158 
00159         case EMPCmdVolumeMedium:
00160                 MediaClientEngine().SetVolume(CMediaClientEngine::EMedium);
00161                 break;
00162 
00163         case EMPCmdVolumeHigh:
00164                 MediaClientEngine().SetVolume(CMediaClientEngine::ELoud);
00165                 break;
00166 
00167     case EMPCmdStop:
00168                 MediaClientEngine().Stop();
00169                 StreamSourceCleanUp();
00170                 break;
00171 
00172     case EEikCmdExit: 
00173                 MediaClientEngine().Stop();
00174                 if ( (iState == CMediaClientEngine::EStreamStarted)
00175                         || (iState == CMediaClientEngine::ENextStreamBuf)
00176                         || (iState == CMediaClientEngine::EStreamError)
00177                         || (iState == CMediaClientEngine::EStreamStopping) )
00178                         iExitCallBack->CallBack();
00179                 else
00180                         Exit();
00181         break;
00182         }
00183     }
00184 
00185 void CMediaClientAppUi::StreamSourceCleanUp()
00186         {
00187         delete iAudioStreamSource;
00188         iAudioStreamSource = NULL;
00189         delete iAudioStreamSourceWithQueue;
00190     iAudioStreamSourceWithQueue = NULL;
00191         }
00192 
00193 // Dialog to select tone to play
00194 void CMediaClientAppUi::ToneDialogL()
00195         {
00196         TInt frequency;
00197         TTimeIntervalMicroSeconds duration;
00198 
00199         if (CToneDialog::RunDlgLD(frequency,duration))
00200                 MediaClientEngine().PlayL(frequency,duration);
00201         }
00202 
00203 // Dialog to select file to play
00204 void CMediaClientAppUi::PlayFileDialogL()
00205     {
00206         CEikFileOpenDialog* dialog = new (ELeave) CEikFileOpenDialog(&iFileName);
00207     if (dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
00208         MediaClientEngine().PlayL(iFileName);
00209     }
00210 
00211 // Dialog to select file to record to
00212 void CMediaClientAppUi::RecordFileDialogL()
00213         {
00214         if (CRecordDialog::RunDlgLD(iFileName, MediaClientEngine(), iRecordFormatOptions))
00215                 // start conversion
00216                 MediaClientEngine().RecordL(iFileName, iRecordFormatOptions);
00217                 // callback to HandleEngineState when conversion complete
00218         }
00219 
00220 // Query for file metadata
00221 void CMediaClientAppUi::FileMetaDataL()
00222         {
00223         // get meta data about the file, which we'll display afterwards
00224         iMetaData.ResetAndDestroy();
00225         CEikFileOpenDialog* dialog = new (ELeave) CEikFileOpenDialog(&iFileName);
00226     if (dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
00227                 MediaClientEngine().GetMetaDataL(iFileName,iMetaData);
00228         // callback to HandleEngineState when data got
00229         }
00230 
00231 // Display retrieved metadata
00232 void CMediaClientAppUi::DisplayMetaDataL()
00233         {
00234         // set string with metadata information
00235         _LIT(KSeparator, "\t");
00236         _LIT(KNewLine, "\n");
00237         _LIT(KNoMetaData, "No metadata retrieved");
00238         const TInt KMaxDataField = 1024;
00239     HBufC* labelText = HBufC::NewLC(KMaxDataField);
00240         TPtr labelTextPtr = labelText->Des();
00241         for (TInt i=0; i<iMetaData.Count(); i++)
00242                 {
00243                 // be careful not to overrun descriptor end
00244                 TInt fieldLen = iMetaData[i]->Name().Length() + 2 + iMetaData[i]->Value().Length();
00245                 if (labelTextPtr.Length()+fieldLen < KMaxDataField)
00246                         {
00247                         labelTextPtr.Append(iMetaData[i]->Name());
00248                         labelTextPtr.Append(KSeparator);
00249                         labelTextPtr.Append(iMetaData[i]->Value());
00250                         labelTextPtr.Append(KNewLine);
00251                         }
00252                 }
00253         if (iMetaData.Count() == 0) *labelText = KNoMetaData;
00254         iAppView -> SetDescription(*labelText);
00255         CleanupStack::PopAndDestroy(); // labelText
00256         }
00257 
00258 // File format conversion dialog
00259 void CMediaClientAppUi::ConvertFileDialogL()
00260         {
00261         CEikFileOpenDialog* dialog = new (ELeave) CEikFileOpenDialog(&iFileName);
00262     if (dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
00263                 if (CConverterDialog::RunDlgLD(iFileName, MediaClientEngine(), iFormatOptions, iTargetType))
00264                         // start conversion
00265                         MediaClientEngine().ConvertL(iFileName, iTargetType, iFormatOptions);
00266                         // callback to HandleEngineState when conversion complete
00267         }
00268 
00269 // Dim menu commands when sound is playing
00270 void CMediaClientAppUi::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
00271         {
00272         if (aResourceId == R_AUDIOEX_ACTIONS_MENU)
00273                 {
00274                 TBool dim = (iState != CMediaClientEngine::EIdle);
00275                 aMenuPane -> SetItemDimmed(EMPCmdPlayTone,dim);
00276                 aMenuPane -> SetItemDimmed(EMPCmdRecordFile,dim);
00277                 aMenuPane -> SetItemDimmed(EMPCmdPlayStream,dim);
00278                 aMenuPane -> SetItemDimmed(EMPCmdPlayStreamWithQueue,dim);
00279                 aMenuPane -> SetItemDimmed(EMPCmdPlayFile,dim);
00280                 aMenuPane -> SetItemDimmed(EMPCmdStop,!dim);
00281                 aMenuPane -> SetItemDimmed(EMPCmdConvertFile,dim);
00282                 aMenuPane -> SetItemDimmed(EMPCmdShowMetaData,dim);     
00283                 }
00284         }
00285 
00286 // Call back from the engine to inform the UI of progress
00287 void CMediaClientAppUi::HandleEngineState(CMediaClientEngine::TState aState, 
00288                                                                                   TInt aError)
00289         {
00290         iState = aState;
00291         if (aError) 
00292                 iAppView -> SetDescription(KError);
00293         else
00294                 switch(aState)
00295                         {
00296                 case CMediaClientEngine::EIdle:
00297                         iAppView -> SetDescription(KIdle);
00298                         break;
00299                 case CMediaClientEngine::ETonePrepare:
00300                         iAppView -> SetDescription(KTonePrepare);
00301                         break;
00302                 case CMediaClientEngine::ETonePlaying:
00303                         iAppView -> SetDescription(KTonePlaying);
00304                         break;
00305                 case CMediaClientEngine::EPlayFilePrepare:
00306                         iAppView -> SetDescription(KPlayFilePrepare);
00307                         break;
00308                 case CMediaClientEngine::EPlayFilePlaying:
00309                         iAppView -> SetDescription(KPlayFilePlaying);
00310                         break;
00311                 case CMediaClientEngine::EGetMetaData2:
00312                         TRAPD(err,DisplayMetaDataL());
00313                         if(err)
00314                                 {
00315                                 return;
00316                                 }
00317                         iState = CMediaClientEngine::EIdle;
00318                         break;
00319                 case CMediaClientEngine::ERecordPrepare:
00320                         iAppView -> SetDescription(KRecordPrepare);
00321                         break;
00322                 case CMediaClientEngine::EConvert:
00323                         iAppView -> SetDescription(KConverting);
00324                         break;
00325                 case CMediaClientEngine::EConvertComplete:
00326                         iAppView -> SetDescription(KIdle);
00327                         break;
00328                 case CMediaClientEngine::ERecording:
00329                         iAppView -> SetDescription(KRecording);
00330                         break;
00331                 case CMediaClientEngine::EStreamPrepare:
00332                         iAppView -> SetDescription(KPlayStreamPrepare);
00333                         break;
00334                 case CMediaClientEngine::EStreamStarted:
00335                         if (iAudioStreamSourceWithQueue)
00336                                 iAudioStreamSourceWithQueue->Next();
00337                         else
00338                                 iAudioStreamSource->Next();
00339                         iAppView -> SetDescription(KStreamPlaying);
00340                         break;
00341                 case CMediaClientEngine::ENextStreamBuf:
00342                         if (iAudioStreamSourceWithQueue)
00343                                 iAudioStreamSourceWithQueue->RemoveFromQueue();
00344                         else
00345                                 iAudioStreamSource->Next();
00346                         break;
00347                 default:
00348                         break;
00349                         }
00350 
00351         // update the UI to reflect the state
00352         iAppView -> DrawNow();
00353         }
00354 
00355 // ********************************
00356 // CRecordDialog - sets conversion format
00357 // ********************************
00358 
00359 const TInt KFormatOptionsNotSet = -1;
00360 
00361 TBool CRecordDialog::RunDlgLD(
00362         TFileName& aFileName, CMediaClientEngine& aMediaClientEngine, // input parameters
00363         TRecordFormatOptions& aRecordFormatOptions) 
00364         {
00365         CRecordDialog* dialog = new (ELeave) CRecordDialog(aFileName, aMediaClientEngine, aRecordFormatOptions);
00366         return (dialog->ExecuteLD(R_FORMAT_DIALOG));
00367         }
00368 
00369 CRecordDialog::CRecordDialog(TFileName& aFileName, CMediaClientEngine& aMediaClientEngine,
00370         TRecordFormatOptions& aRecordFormatOptions)
00371         :iFileName(aFileName), 
00372         iMediaClientEngine(aMediaClientEngine),
00373         iRecordFormatOptions(aRecordFormatOptions),
00374         iOptionsSetFor(KFormatOptionsNotSet)
00375         {
00376         }
00377 
00378 CRecordDialog::~CRecordDialog()
00379         {
00380         }
00381 
00382 void CRecordDialog::PreLayoutDynInitL()
00383         {
00384         // set choice list with destination types
00385         CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(4);
00386         CleanupStack::PushL(array);
00387         // get array of conversion types
00388         iMediaClientEngine.RecordTypesL(*array);
00389         // set choicelist
00390     CEikChoiceList* typesList = static_cast<CEikChoiceList*>(Control(ECvtTypeField));
00391         typesList->SetArrayL(array);
00392         CleanupStack::Pop(array);
00393         }
00394 
00395 void CRecordDialog::HandleControlStateChangeL(TInt aControlId)
00396         {
00397         // Unset format options if the format type changes
00398         if (aControlId == ECvtTypeField)
00399                 iOptionsSetFor = KFormatOptionsNotSet;
00400         }
00401 
00402 TBool CRecordDialog::OkToExitL(TInt aKeycode)
00403         {
00404         _LIT(KRecordFile, "\\record-output");
00405         CEikChoiceList* typesList = static_cast<CEikChoiceList*>(Control(ECvtTypeField));
00406         
00407         TDriveUnit sysDrive (RFs::GetSystemDrive());
00408         TDriveName sysDriveName (sysDrive.Name());      
00409         TFileName fileName(sysDriveName);
00410         fileName.Append(KRecordFile);
00411     iFileName = fileName;
00412         
00413         TPtrC type = typesList->Array()->MdcaPoint(typesList->CurrentItem());
00414         iFileName.Append(type);
00415 
00416         // if format options button pressed, show dialog
00417         if (aKeycode == EFormatOptions)
00418                 {
00419                 if (CRecordFormatOptionsDialog::RunDlgLD(type, iMediaClientEngine, iRecordFormatOptions)) 
00420                         iOptionsSetFor = typesList->CurrentItem();
00421                 return EFalse;
00422                 }
00423         
00424         return ETrue;
00425     }
00426 
00427 
00428 // ********************************
00429 // CRecordFormatOptionsDialog - sets conversion format options
00430 // ********************************
00431 
00432 TBool CRecordFormatOptionsDialog::RunDlgLD(const TDesC& aTargetType, 
00433         CMediaClientEngine& aMediaClientEngine, TRecordFormatOptions& aFormatOptions)
00434         {
00435         CRecordFormatOptionsDialog* dialog = new (ELeave) CRecordFormatOptionsDialog(aTargetType, 
00436                 aMediaClientEngine, aFormatOptions);
00437         dialog->ConstructL();
00438         return (dialog->ExecuteLD(R_RECORD_OPTIONS_DIALOG));
00439         }
00440 
00441 CRecordFormatOptionsDialog::CRecordFormatOptionsDialog(const TDesC& aTargetType, 
00442         CMediaClientEngine& aMediaClientEngine, TRecordFormatOptions& aFormatOptions)
00443         :CFormatOptionsDialog(iBuf, aTargetType, aMediaClientEngine, aFormatOptions),
00444         iRecordFormatOptions(aFormatOptions)
00445         {
00446         aFormatOptions.Reset();
00447         }
00448 
00449 void CRecordFormatOptionsDialog::ConstructL()
00450         {
00451         // Get a recorder for the format
00452         CMdaAudioRecorderUtility* recorder = iMediaClientEngine.NewRecorderL(iTargetType);
00453         
00454         if (recorder == NULL) User::Leave(KErrNotSupported);
00455         // Read available options
00456         CleanupStack::PushL(recorder);
00457         iMaxGain = recorder -> MaxGain();
00458 
00459         TRAPD(err,recorder -> GetSupportedDestinationDataTypesL(iDataTypes));
00460         if (err != KErrNotSupported) User::LeaveIfError(err);
00461         TRAP(err,recorder -> GetSupportedBitRatesL(iBitRates));
00462         if (err != KErrNotSupported) User::LeaveIfError(err);
00463         TRAP(err,recorder -> GetSupportedSampleRatesL(iSampleRates));
00464         if (err != KErrNotSupported) User::LeaveIfError(err);
00465         TRAP(err,recorder -> GetSupportedNumberOfChannelsL(iChannels));
00466         if (err != KErrNotSupported) User::LeaveIfError(err);
00467         CleanupStack::PopAndDestroy(recorder);
00468         }
00469 
00470 CRecordFormatOptionsDialog::~CRecordFormatOptionsDialog()
00471         {
00472         }
00473 
00474 void CRecordFormatOptionsDialog::PreLayoutDynInitL()
00475         {
00476         // set up bit rate, sample, channel, codec lists
00477         CFormatOptionsDialog::PreLayoutDynInitL();
00478         // add gain and balance, which are recording specific parameters
00479     CEikNumberEditor* gain = static_cast<CEikNumberEditor*>(Control(EGain));
00480         gain->SetMinimumAndMaximum(0,iMaxGain);
00481         gain->SetNumber(iMaxGain/2);
00482     CEikNumberEditor* balance = static_cast<CEikNumberEditor*>(Control(EBalance));
00483         balance->SetMinimumAndMaximum(KMMFBalanceMaxLeft,KMMFBalanceMaxRight);
00484         balance->SetNumber(KMMFBalanceCenter);
00485         }
00486 
00487 TBool CRecordFormatOptionsDialog::OkToExitL(TInt /*aKeycode*/)
00488         {
00489         // get bit rate, sample, channel, codec settings
00490         CFormatOptionsDialog::OkToExitL(0);
00491         // get gain and balance, which are recording specific parameters
00492     CEikNumberEditor* gain = static_cast<CEikNumberEditor*>(Control(EGain));
00493         iRecordFormatOptions.iGain = gain->Number();
00494     CEikNumberEditor* balance = static_cast<CEikNumberEditor*>(Control(EBalance));
00495         iRecordFormatOptions.iBalance = balance->Number();
00496         iRecordFormatOptions.iGainSet = iRecordFormatOptions.iBalanceSet = ETrue;
00497 
00498         return ETrue;
00499         }
00500 
00501 
00502 // ********************************
00503 // CConverterDialog - sets conversion format
00504 // ********************************
00505 
00506 TBool CConverterDialog::RunDlgLD(
00507         const TFileName& aFileName, CMediaClientEngine& aMediaClientEngine, // input parameters
00508         TFormatOptions& aFormatOptions, TDes& aTargetType) 
00509         {
00510         CConverterDialog* dialog = new (ELeave) CConverterDialog(aFileName, aMediaClientEngine, aFormatOptions, aTargetType);
00511         return (dialog->ExecuteLD(R_FORMAT_DIALOG));
00512         }
00513 
00514 CConverterDialog::CConverterDialog(const TFileName& aFileName, CMediaClientEngine& aMediaClientEngine,
00515         TFormatOptions& aFormatOptions, TDes& aTargetType)
00516         :iFileName(aFileName), 
00517         iMediaClientEngine(aMediaClientEngine),
00518         iFormatOptions(aFormatOptions),
00519         iTargetType(aTargetType),
00520         iOptionsSetFor(KFormatOptionsNotSet)
00521         {
00522         }
00523 
00524 CConverterDialog::~CConverterDialog()
00525         {
00526         }
00527 
00528 void CConverterDialog::PreLayoutDynInitL()
00529         {
00530         // set choice list with destination types
00531         CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(4);
00532         CleanupStack::PushL(array);
00533         // get array of conversion types
00534         iMediaClientEngine.DestinationTypesL(iFileName, *array);
00535         // set choicelist
00536     CEikChoiceList* typesList = static_cast<CEikChoiceList*>(Control(ECvtTypeField));
00537         typesList->SetArrayL(array);
00538         CleanupStack::Pop(array);
00539         }
00540 
00541 void CConverterDialog::HandleControlStateChangeL(TInt aControlId)
00542         {
00543         // Unset format options if the format type changes
00544         if (aControlId == ECvtTypeField)
00545                 iOptionsSetFor = KFormatOptionsNotSet;
00546         }
00547 
00548 TBool CConverterDialog::OkToExitL(TInt aKeycode)
00549         {
00550         CEikChoiceList* typesList = static_cast<CEikChoiceList*>(Control(ECvtTypeField));
00551         iTargetType = typesList->Array()->MdcaPoint(typesList->CurrentItem());
00552 
00553         // if format options button pressed, show dialog
00554         if (aKeycode == EFormatOptions)
00555                 {
00556                 if (CFormatOptionsDialog::RunDlgLD(iFileName, iTargetType, iMediaClientEngine, iFormatOptions)) 
00557                         iOptionsSetFor = typesList->CurrentItem();
00558                 return EFalse;
00559                 }
00560         
00561         return ETrue;
00562     }
00563 
00564 
00565 // ********************************
00566 // CFormatOptionsDialog - sets conversion format options
00567 // ********************************
00568 
00569 TBool CFormatOptionsDialog::RunDlgLD(const TFileName& aInputFile, const TDesC& aTargetType, 
00570         CMediaClientEngine& aMediaClientEngine, TFormatOptions& aFormatOptions)
00571         {
00572         CFormatOptionsDialog* dialog = new (ELeave) CFormatOptionsDialog(aInputFile, aTargetType, 
00573                 aMediaClientEngine, aFormatOptions);
00574         dialog->ConstructL();
00575         return (dialog->ExecuteLD(R_CONVERT_OPTIONS_DIALOG));
00576         }
00577 
00578 CFormatOptionsDialog::CFormatOptionsDialog(const TFileName& aInputFile, const TDesC& aTargetType, 
00579         CMediaClientEngine& aMediaClientEngine, TFormatOptions& aFormatOptions)
00580         :iFileName(aInputFile), iTargetType(aTargetType), iMediaClientEngine(aMediaClientEngine),
00581         iFormatOptions(aFormatOptions)
00582         {
00583         iFormatOptions.Reset();
00584         }
00585 
00586 void CFormatOptionsDialog::ConstructL()
00587         {
00588         // Get a converter for the format
00589         TFileName targetFile; // don't actually use this
00590         CMdaAudioConvertUtility* converter = iMediaClientEngine.NewConverterL(iFileName, iTargetType, targetFile);
00591         if (converter == NULL) User::Leave(KErrNotSupported);
00592         // Read available options
00593         CleanupStack::PushL(converter);
00594         TRAPD(err,converter -> GetSupportedDestinationDataTypesL(iDataTypes));
00595         if (err != KErrNotSupported) User::LeaveIfError(err);
00596         TRAP(err,converter -> GetSupportedConversionBitRatesL(iBitRates));
00597         if (err != KErrNotSupported) User::LeaveIfError(err);
00598         TRAP(err,converter -> GetSupportedConversionSampleRatesL(iSampleRates));
00599         if (err != KErrNotSupported) User::LeaveIfError(err);
00600         TRAP(err,converter -> GetSupportedConversionNumberOfChannelsL(iChannels));
00601         if (err != KErrNotSupported) User::LeaveIfError(err);
00602         CleanupStack::PopAndDestroy(converter);
00603         }
00604 
00605 CFormatOptionsDialog::~CFormatOptionsDialog()
00606         {
00607         iDataTypes.Close();
00608         iBitRates.Close();
00609         iSampleRates.Close();
00610         iChannels.Close();
00611         }
00612 
00613 void CFormatOptionsDialog::PreLayoutDynInitL()
00614         {
00615         // Set dialog fields - these 3 are all TUint lists
00616         TUintChoiceListL(EBitRates, iBitRates);
00617         TUintChoiceListL(ESampleRates, iSampleRates);
00618         TUintChoiceListL(EChannels, iChannels);
00619         // Codecs list is more complicated
00620         CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(4);
00621         CleanupStack::PushL(array);
00622         HBufC8* buf8 = HBufC8::NewLC(10);
00623         TPtr8 ptr = buf8->Des();
00624         TBuf<5> buf;
00625         for (TInt i=0; i<iDataTypes.Count(); i++)
00626                 {
00627                 iDataTypes[i].FourCC(&ptr);
00628                 buf.Copy(ptr);
00629                 array->AppendL(buf);
00630                 }
00631     CEikChoiceList* list = static_cast<CEikChoiceList*>(Control(ECodecs));
00632         list->SetArrayL(array); 
00633         CleanupStack::PopAndDestroy(); //buf8
00634         CleanupStack::Pop(); //array
00635         }
00636 
00637 void CFormatOptionsDialog::TUintChoiceListL(TInt aResourceId, const RArray<TUint>& aArray)
00638         {
00639         CEikChoiceList* list = static_cast<CEikChoiceList*>(Control(aResourceId));
00640         if (aArray.Count()==0) 
00641                 {
00642                 list->MakeVisible(EFalse);
00643                 return;
00644                 }
00645         TBuf <10> buf;
00646         CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(4);
00647         CleanupStack::PushL(array);
00648         for (TInt i=0; i<aArray.Count(); i++)
00649                 {
00650                 buf.Num(aArray[i]);
00651                 array->AppendL(buf);
00652                 }
00653         list->SetArrayL(array); 
00654         CleanupStack::Pop(); //array
00655         }
00656 
00657 TBool CFormatOptionsDialog::OkToExitL(TInt /*aKeycode*/)
00658         {
00659         CEikChoiceList* list = static_cast<CEikChoiceList*>(Control(ECodecs));
00660         if (list->IsVisible()) 
00661                 {iFormatOptions.i4CC = iDataTypes[list->CurrentItem()]; iFormatOptions.i4CCSet = ETrue; }
00662         list = static_cast<CEikChoiceList*>(Control(EBitRates));
00663         if (list->IsVisible()) 
00664                 {iFormatOptions.iBitRate = iBitRates[list->CurrentItem()]; iFormatOptions.iBitRateSet = ETrue; }
00665         list = static_cast<CEikChoiceList*>(Control(ESampleRates));
00666         if (list->IsVisible()) 
00667                 {iFormatOptions.iSampleRate = iSampleRates[list->CurrentItem()]; iFormatOptions.iSampleRateSet = ETrue; }
00668         list = static_cast<CEikChoiceList*>(Control(EChannels));
00669         if (list->IsVisible()) 
00670                 {iFormatOptions.iChannel = iChannels[list->CurrentItem()]; iFormatOptions.iChannelSet = ETrue; }
00671         return ETrue;
00672         }
00673 
00674 
00675 // ********************************
00676 // CToneDialog - sets tone to play
00677 // ********************************
00678 
00679 TBool CToneDialog::RunDlgLD(TInt& aFrequency, TTimeIntervalMicroSeconds& aDuration)
00680         {
00681         CEikDialog* dialog = new (ELeave) CToneDialog(aFrequency,aDuration);
00682         return (dialog->ExecuteLD(R_TONE_DIALOG));
00683         }
00684 
00685 CToneDialog::CToneDialog(TInt& aFrequency, TTimeIntervalMicroSeconds& aDuration)
00686         :iFrequency(aFrequency), 
00687         iDuration(aDuration)
00688         {
00689         }
00690 
00691 TBool CToneDialog::OkToExitL(TInt /*aKeycode*/)
00692         {
00693     iFrequency = STATIC_CAST(CEikNumberEditor*,Control(EFrequencyField))->Number();
00694     iDuration = STATIC_CAST(CEikNumberEditor*,Control(EDurationField))->Number() * 1000000;
00695         return ETrue;
00696     }
00697 
00698 // ********************************
00699 // CAudioStreamSource - an artificial audio stream source
00700 // It asynchronously gets slices from a file and feeds them to the engine
00701 // ********************************
00702 
00703 // File to read
00704 _LIT(KSourceName, "\\testing123.raw");
00705 
00706 CAudioStreamSource* CAudioStreamSource::NewL(RFs& aFs,CMediaClientEngine& aEngine)
00707         {
00708         CAudioStreamSource* self=new(ELeave) CAudioStreamSource(aFs,aEngine);
00709         CleanupStack::PushL(self);
00710         self->ConstructL();
00711         CleanupStack::Pop();
00712         return self;
00713         }
00714 
00715 CAudioStreamSource::CAudioStreamSource(RFs& aFs, CMediaClientEngine& aEngine)
00716         :
00717         CActive(CActive::EPriorityStandard),
00718         iFs(aFs),
00719         iEngine(aEngine)
00720         {
00721         }
00722 
00723 void CAudioStreamSource::ConstructL()
00724         {
00725         // Base class second-phase construction.
00726         // Open the source file
00727         TDriveUnit sysDrive (RFs::GetSystemDrive());
00728         TDriveName sysDriveName (sysDrive.Name());      
00729         TFileName fileName(sysDriveName);
00730         fileName.Append(KSourceName);
00731     
00732         User::LeaveIfError(iSourceFile.Open(iFs,fileName,EFileRead));
00733         CActiveScheduler::Add(this);
00734         }
00735 
00736 void CAudioStreamSource::RunL()
00737         {
00738         // Read a slice from the source file
00739         User::LeaveIfError(iSourceFile.Read(iBuf));
00740         // If not at end of file, write the slice to the stream
00741         if (iBuf.Length())
00742                 iEngine.WriteToStreamL(iBuf);
00743         }
00744 
00745 void CAudioStreamSource::Next()
00746         {
00747         TRequestStatus * status = &iStatus;
00748         User::RequestComplete(status, KErrNone);
00749         SetActive();
00750         }
00751 
00752 void CAudioStreamSource::DoCancel()
00753         {
00754         }
00755 
00756 CAudioStreamSource::~CAudioStreamSource()
00757         {
00758         Cancel();
00759         // Close the source file
00760         iSourceFile.Close();
00761         }
00762 
00763 // ********************************
00764 // CAudioStreamSourceWithQueue - an artificial audio stream source.
00765 // It gets slices from a file at a constant rate, using a timer,
00766 // stores them in a queue then feeds them to the engine.
00767 // They are deleted from the queue after they have been received by 
00768 // the media framework
00769 // ********************************
00770 
00771 CAudioStreamSourceWithQueue* CAudioStreamSourceWithQueue::NewL(RFs& aFs,CMediaClientEngine& aEngine)
00772         {
00773         CAudioStreamSourceWithQueue* self=new(ELeave) CAudioStreamSourceWithQueue(aFs,aEngine);
00774         CleanupStack::PushL(self);
00775         self->ConstructL();
00776         CleanupStack::Pop();
00777         return self;
00778         }
00779 
00780 CAudioStreamSourceWithQueue::CAudioStreamSourceWithQueue(RFs& aFs, CMediaClientEngine& aEngine)
00781         :
00782         CTimer(CActive::EPriorityStandard),
00783         iFs(aFs),
00784         iEngine(aEngine),
00785         iQueue(CSoundSlice::iOffset),
00786         iQueueIter(iQueue)
00787         {
00788         }
00789 
00790 void CAudioStreamSourceWithQueue::ConstructL()
00791         {
00792         // Base class second-phase construction.
00793         CTimer::ConstructL(); 
00794         // Open the source file
00795         User::LeaveIfError(iSourceFile.Open(iFs,KSourceName,EFileRead));
00796         CActiveScheduler::Add(this);
00797         }
00798 
00799 
00800 CAudioStreamSourceWithQueue::~CAudioStreamSourceWithQueue()
00801         {
00802         Cancel();
00803         CSoundSlice* soundSlice;
00804         iQueueIter.SetToFirst();
00805         while ((soundSlice=iQueueIter++) != NULL)
00806                         {
00807                         iQueue.Remove(*soundSlice);
00808                         delete soundSlice;
00809                         };
00810         iSourceFile.Close();
00811         }
00812         
00813 void CAudioStreamSourceWithQueue::Next()
00814         {
00815         // Request a 1/10 second wait
00816         After(100000);
00817         }
00818         
00819 
00820 void CAudioStreamSourceWithQueue::DoCancel()
00821         {
00822         CTimer::DoCancel();
00823         }
00824         
00825 void CAudioStreamSourceWithQueue::RunL()
00826         {
00827         // Read a slice from the source file
00828         User::LeaveIfError(iSourceFile.Read(iBuf));
00829         // If not at end of file, store the slice until it is safe to delete it
00830         // and write to the stream.
00831         // It can be deleted when MaoscBufferCopied() is called, 
00832         // to indicate that it has been copied to the server.
00833         if (iBuf.Length())
00834                 {
00835                 iQueue.AddLast(*CSoundSlice::NewL(iBuf)); // Store it in the linked list, at end
00836                 iEngine.WriteToStreamL(iQueue.Last()->GetData());
00837                 // This will call back MaoscBufferCopied() when the data is copied
00838                 // and reissue the wait request
00839                 After(100000); // wait 1/10 second
00840                 }
00841         }
00842 
00843 void CAudioStreamSourceWithQueue::RemoveFromQueue()
00844         {
00845         if (!(iQueue.IsEmpty()))
00846                 {
00847                 CSoundSlice* soundSlice=iQueue.First();
00848                 iQueue.Remove(*soundSlice);
00849                 delete soundSlice;
00850                 }
00851         }
00852 
00853 // ********************************
00854 // CSoundSlice
00855 // ********************************
00856 
00857 const TInt CSoundSlice::iOffset = _FOFF(CSoundSlice,iLink);
00858 
00859 CSoundSlice* CSoundSlice::NewL(const TDesC8& aData)
00860         {
00861         CSoundSlice* self=new(ELeave) CSoundSlice;
00862         CleanupStack::PushL(self);
00863         self->ConstructL(aData);
00864         CleanupStack::Pop();
00865         return self;
00866         }
00867 
00868 void CSoundSlice::ConstructL(const TDesC8& aData)
00869         {
00870         iData = aData.AllocL();
00871         }
00872 
00873 CSoundSlice::CSoundSlice()
00874         {}
00875 
00876 CSoundSlice::~CSoundSlice()
00877         {
00878         delete iData;
00879         }
00880 
00881 
00882 const TDesC8& CSoundSlice::GetData()
00883         {
00884         return *iData;
00885         }

Generated on Thu Jan 21 10:32:59 2010 for TB10.1 Example Applications by  doxygen 1.5.3