javauis/amms_akn/src_tuner/native/src/ammstunerfactory.cpp
branchRCL_3
changeset 60 6c158198356e
parent 59 e5618cc85d74
child 63 d1278d87b01e
child 65 ae942d28ec0e
equal deleted inserted replaced
59:e5618cc85d74 60:6c158198356e
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  This class is used to create tuner player and control
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "AMMSTunerFactory.h"
       
    22 #include    "CAMMSTunerPlayer.h"
       
    23 #include    "CAMMSTunerControl.h"
       
    24 //#include    "CAMMSTunerVolumeControl.h"
       
    25 #include    <CMMAPlayerProperties.h>
       
    26 
       
    27 #include    <e32std.h>
       
    28 #include    <e32math.h>
       
    29 #include    <jdebug.h>
       
    30 
       
    31 // CONSTANTS
       
    32 _LIT(KFreqParam, "f");
       
    33 
       
    34 _LIT(KModulationParam, "mod");
       
    35 
       
    36 _LIT(KStereoModeParam, "st");
       
    37 MMA_PARAMETER_STR(KStereoModeParamMono, "mono");
       
    38 MMA_PARAMETER_STR(KStereoModeParamStereo, "stereo");
       
    39 MMA_PARAMETER_STR(KStereoModeParamAuto, "auto");
       
    40 MMA_PARAMETER_ARRAY(KValidStereoModeValues)
       
    41 {
       
    42     {
       
    43         &KStereoModeParamMono
       
    44     }, {&KStereoModeParamStereo}, {&KStereoModeParamAuto}
       
    45 };
       
    46 
       
    47 _LIT(KProgramIdParam, "id");
       
    48 _LIT(KPresetParam, "preset");
       
    49 
       
    50 _LIT(KModulationFm, "fm");
       
    51 
       
    52 _LIT(KStereoModeMono, "mono");
       
    53 _LIT(KStereoModeStereo, "stereo");
       
    54 _LIT(KStereoModeAuto, "auto");
       
    55 
       
    56 const TInt KMegaHertzChar = 'M';
       
    57 const TInt KKiloHertzChar = 'k';
       
    58 const TInt KFreqDotChar   = '.';
       
    59 
       
    60 const TInt KMegaHzMultiplier = 1000000;
       
    61 const TInt KKiloHzMultiplier = 1000;
       
    62 
       
    63 const TInt KStereoModeMonoInt   = 1;
       
    64 const TInt KStereoModeStereoInt = 2;
       
    65 const TInt KStereoModeAutoInt   = 3;
       
    66 
       
    67 // ============================ MEMBER FUNCTIONS ===============================
       
    68 
       
    69 // Destructor
       
    70 AMMSTunerFactory::~AMMSTunerFactory()
       
    71 {
       
    72 
       
    73 }
       
    74 
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // AMMSTunerFactory::CreatePlayerL
       
    78 // ?implementation_description
       
    79 // (other items were commented in a header).
       
    80 // -----------------------------------------------------------------------------
       
    81 //
       
    82 void AMMSTunerFactory::CreatePlayerL(CAMMSTunerPlayer** aTunerPlayer,
       
    83                                      const TDesC* aLocatorParams)
       
    84 {
       
    85     DEBUG("AMMSTunerFactory::CreatePlayerL +");
       
    86 
       
    87     TInt frequency = 0;
       
    88     TInt stereoMode = 0;
       
    89     TInt preset = 0;
       
    90 
       
    91     if (aLocatorParams->Length() != 0)
       
    92     {
       
    93         DEBUG("AMMSTunerFactory::CreatePlayerL 1");
       
    94         ParseParamsL(aLocatorParams, frequency, stereoMode, preset);
       
    95     }
       
    96 
       
    97     //create tunercontrol
       
    98     CAMMSTunerControl* tunerControl = CAMMSTunerControl::NewL();
       
    99     CleanupStack::PushL(tunerControl);
       
   100 
       
   101     //set values to tunercontrol
       
   102     if (frequency > 0)
       
   103     {
       
   104         tunerControl->SetFrequencyL(frequency);
       
   105     }
       
   106 
       
   107     if (frequency == 0)
       
   108     {
       
   109         //by default frequency is fm modulation's min freq
       
   110         tunerControl->SetFrequencyL(tunerControl->MinFreqL());
       
   111     }
       
   112 
       
   113     if (stereoMode > 0)
       
   114     {
       
   115         tunerControl->SetStereoModeL(stereoMode);
       
   116     }
       
   117 
       
   118     if (preset > 0)
       
   119     {
       
   120         tunerControl->UsePresetL(preset);
       
   121     }
       
   122 
       
   123     //create tunerplayer
       
   124     *aTunerPlayer = CAMMSTunerPlayer::NewL(tunerControl);
       
   125 
       
   126     //create tunervolumeconrol
       
   127     //CAMMSTunerVolumeControl* tunerVolumeControl = CAMMSTunerVolumeControl::NewL( *aTunerPlayer );
       
   128 
       
   129     //add controls to player
       
   130     (*aTunerPlayer)->AddControlL(tunerControl);
       
   131     //(*aTunerPlayer)->AddControlL( tunerVolumeControl );
       
   132     CleanupStack::Pop(tunerControl);
       
   133     DEBUG("AMMSTunerFactory::CreatePlayerL -");
       
   134 }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // AMMSTunerFactory::ParseParamsL
       
   138 // ?implementation_description
       
   139 // (other items were commented in a header).
       
   140 // -----------------------------------------------------------------------------
       
   141 //
       
   142 void AMMSTunerFactory::ParseParamsL(const TDesC* aLocatorParams,
       
   143                                     TInt& aFrequency,
       
   144                                     TInt& aStereoMode,
       
   145                                     TInt& aPreset)
       
   146 {
       
   147     DEBUG("AMMSTunerFactory::ParseParamsL +");
       
   148     CMMAParameterRuleSet* rules = CMMAParameterRuleSet::NewLC();
       
   149 
       
   150     //freq rule
       
   151     TMMAParameterRuleDes freqRule(KFreqParam);
       
   152     rules->AppendRuleL(&freqRule);
       
   153 
       
   154     //modulation rule
       
   155     TMMAParameterRuleDes modulationRule(KModulationParam);
       
   156     rules->AppendRuleL(&modulationRule);
       
   157 
       
   158     //stereo mode rule
       
   159     TMMAParameterRuleDes stereoModeRule(KStereoModeParam, KValidStereoModeValues,
       
   160                                         MMA_PARAMETER_ARRAY_SIZE(KValidStereoModeValues));
       
   161     rules->AppendRuleL(&stereoModeRule);
       
   162 
       
   163     //preset rule KMinTInt-KMaxTInt
       
   164     TMMAParameterRuleInt presetRule(KPresetParam);
       
   165     rules->AppendRuleL(&presetRule);
       
   166 
       
   167     //id rule
       
   168     TMMAParameterRuleDes idRule(KProgramIdParam);
       
   169     rules->AppendRuleL(&idRule);
       
   170 
       
   171     CMMAPlayerProperties* properties = CMMAPlayerProperties::NewL(*aLocatorParams, *rules);
       
   172     CleanupStack::PushL(properties);
       
   173 
       
   174     // validating properties
       
   175     properties->ValidateL();
       
   176 
       
   177     //get freq
       
   178     TPtrC freq(NULL, 0);
       
   179     properties->GetProperty(KFreqParam, freq);
       
   180     //parse frequency
       
   181     if (freq.Length() != 0)
       
   182     {
       
   183         aFrequency = ParseFreqL(freq);
       
   184     }
       
   185 
       
   186     //get modulation
       
   187     TPtrC modulation(NULL, 0);
       
   188     properties->GetProperty(KModulationParam, modulation);
       
   189     if (modulation.Length() != 0)
       
   190     {
       
   191         // only fm modulation is supported
       
   192         if (modulation.Compare(KModulationFm) != 0)
       
   193         {
       
   194             User::Leave(KErrNotSupported);
       
   195         }
       
   196     }
       
   197 
       
   198     //get stereo mode
       
   199     TPtrC stereoMode(NULL, 0);
       
   200     properties->GetProperty(KStereoModeParam, stereoMode);
       
   201     if (stereoMode.Compare(KStereoModeMono) == 0)
       
   202     {
       
   203         aStereoMode = KStereoModeMonoInt;
       
   204     }
       
   205     else if (stereoMode.Compare(KStereoModeStereo) == 0)
       
   206     {
       
   207         aStereoMode = KStereoModeStereoInt;
       
   208     }
       
   209     else if (stereoMode.Compare(KStereoModeAuto) == 0)
       
   210     {
       
   211         aStereoMode = KStereoModeAutoInt;
       
   212     }
       
   213 
       
   214     //get preset
       
   215     properties->GetProperty(KPresetParam, aPreset);
       
   216 
       
   217     //get id
       
   218     TPtrC id(NULL, 0);
       
   219     properties->GetProperty(KProgramIdParam, id);
       
   220     if (id.Length() != 0)
       
   221     {
       
   222         // id is not supported
       
   223         User::Leave(KErrNotSupported);
       
   224     }
       
   225 
       
   226     CleanupStack::PopAndDestroy(properties);
       
   227     CleanupStack::PopAndDestroy(rules);
       
   228     DEBUG("AMMSTunerFactory::ParseParamsL -");
       
   229 }
       
   230 
       
   231 // -----------------------------------------------------------------------------
       
   232 // AMMSTunerFactory::ParseFreqL
       
   233 // ?implementation_description
       
   234 // (other items were commented in a header).
       
   235 // -----------------------------------------------------------------------------
       
   236 //
       
   237 TInt AMMSTunerFactory::ParseFreqL(const TPtrC aFrequency)
       
   238 {
       
   239     DEBUG("AMMSTunerFactory::ParseFreqL +");
       
   240     TReal freqReal = 0;
       
   241 
       
   242     //is value MHz or kHz or just hertz
       
   243     TInt freqPrefixMPos = aFrequency.Locate(KMegaHertzChar);
       
   244     TInt freqPrefixKPos = aFrequency.Locate(KKiloHertzChar);
       
   245 
       
   246     if (freqPrefixMPos != KErrNotFound)
       
   247     {
       
   248         //there is a M
       
   249         TPtrC hertz = aFrequency.Left(freqPrefixMPos);
       
   250         //TPtrC -> TReal
       
   251         TReal value = TDesCToTRealL(hertz);
       
   252 
       
   253         freqReal = value * KMegaHzMultiplier;
       
   254     }
       
   255     else if (freqPrefixKPos != KErrNotFound)
       
   256     {
       
   257         //there is a k
       
   258         TPtrC hertz = aFrequency.Left(freqPrefixKPos);
       
   259         //TPtrC -> TReal
       
   260         TReal value = TDesCToTRealL(hertz);
       
   261 
       
   262         freqReal = value * KKiloHzMultiplier;
       
   263     }
       
   264     else
       
   265     {
       
   266         //parameter value is simply hertz, there is no M or k
       
   267         //TPtrC -> TReal
       
   268         freqReal = TDesCToTRealL(aFrequency);
       
   269     }
       
   270     TInt32 freqInt = 0;
       
   271     //TReal -> TInt
       
   272     TInt err = Math::Int(freqInt, freqReal);
       
   273     if (err != KErrNone)
       
   274     {
       
   275         User::Leave(KErrArgument);
       
   276     }
       
   277 
       
   278     return freqInt;
       
   279 }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // AMMSTunerFactory::TDesCToTRealL
       
   283 // ?implementation_description
       
   284 // (other items were commented in a header).
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 TReal AMMSTunerFactory::TDesCToTRealL(const TPtrC aHertz)
       
   288 {
       
   289     DEBUG("AMMSTunerFactory::TDesCToTRealL +");
       
   290     TReal valueReal = 0;
       
   291     TLex lex(aHertz);
       
   292     if ((lex.Val(valueReal, KFreqDotChar) != KErrNone) ||
       
   293             !lex.Eos())
       
   294     {
       
   295         User::Leave(KErrArgument);
       
   296     }
       
   297     return valueReal;
       
   298 }
       
   299 
       
   300 //  End of File