javauis/amms_qt/mmacontrol/src/cammsaudiooutputcontrol.cpp
changeset 23 98ccebc37403
child 48 e0d6e9bd3ca7
equal deleted inserted replaced
21:2a9601315dfc 23:98ccebc37403
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Manipulates the audio output mode.
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <AudioPreference.h>
       
    20 #include <logger.h>
       
    21 #include "cammsaudiooutputcontrol.h"
       
    22 #include <cmmaplayerevent.h>
       
    23 #include <cmmaaudioplayer.h>
       
    24 #include <cmmavideoplayer.h>
       
    25 #include <mmfcontroller.h>
       
    26 #include <midiclientutility.h>
       
    27 #include <cammscustomcommandutility.h>
       
    28 #include <JniEnvWrapper.h>
       
    29 // CONSTANTS
       
    30 _LIT(KErrAudioOutputControlError, "AMMS AudioOutputControl error: %d");
       
    31 const TInt KEventMessageSize = 64;
       
    32 const TInt KNoPriference = 0;
       
    33 const TInt KAllSpeakers = 1;
       
    34 const TInt KNoAudioOutput = 2;
       
    35 const TInt KAudioEarpiece = 3;
       
    36 const TInt KAudioLoudspeaker = 4;
       
    37 
       
    38 // ============================ MEMBER FUNCTIONS ===============================
       
    39 
       
    40 // -----------------------------------------------------------------------------
       
    41 // CAMMSAudioOutputControl::NewLC
       
    42 // Two-phased constructor.
       
    43 // -----------------------------------------------------------------------------
       
    44 CAMMSAudioOutputControl* CAMMSAudioOutputControl::NewLC(CMMAPlayer* aPlayer)
       
    45 {
       
    46     CAMMSAudioOutputControl* self = new(ELeave) CAMMSAudioOutputControl(aPlayer);
       
    47 
       
    48     CleanupStack::PushL(self);
       
    49 
       
    50     self->ConstructL();
       
    51 
       
    52     return self;
       
    53 }
       
    54 
       
    55 // Destructor
       
    56 CAMMSAudioOutputControl::~CAMMSAudioOutputControl()
       
    57 {
       
    58     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::~");
       
    59     if (iAudioOutput)
       
    60     {
       
    61         iAudioOutput->UnregisterObserver(*this);
       
    62         delete iAudioOutput;
       
    63     }
       
    64     if (iAccMonitor)
       
    65     {
       
    66         iAccMonitor ->StopObserving();
       
    67         delete iAccMonitor;
       
    68     }
       
    69 }
       
    70 
       
    71 //set java AudioOutput object
       
    72 void CAMMSAudioOutputControl::SetJavaAudioOutputObject(jobject object)
       
    73 {
       
    74     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::SetJavaAudioOutputObject ");
       
    75     iJavaAudioOutputObj = object;
       
    76     // JNI interface pointer can't be passed to different thread, so
       
    77     // it is needed to get valid JNI interface pointer for Event Server thread
       
    78   //  iJni = JniEnvWrapper::GetValidJniRef();
       
    79 }
       
    80 
       
    81 void CAMMSAudioOutputControl::ResetJavaAudioOutputObject()
       
    82 {
       
    83     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::ResetJavaAudioOutputObject ");
       
    84     jmethodID setOutputModeID = iJni->GetMethodID(
       
    85                                     iJni->GetObjectClass(iJavaAudioOutputObj),
       
    86                                     "setOutputMode",
       
    87                                     "(I)V");
       
    88 
       
    89     jint jpref = (jint)GetCurrentPrefInt();
       
    90     iJni->CallVoidMethod(iJavaAudioOutputObj,setOutputModeID,jpref);
       
    91 }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CAMMSAudioOutputControl::SetAudioOutput
       
    95 // Sets the preference using a linear point scale between 0 and 4.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 TInt CAMMSAudioOutputControl::SetAudioOutput(TInt aPreference)
       
    99 {
       
   100     LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::SetAudioOutputL %d", aPreference);
       
   101 
       
   102     TInt temppreference = 0;
       
   103     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::SetAudioOutputL  After __ASSERT_DEBUG");
       
   104     // Set a new preference only if it differs from the previous one.
       
   105     if (aPreference != iRoutingUserPreference)
       
   106     {
       
   107         switch (aPreference)
       
   108         {
       
   109         case KNoPriference:
       
   110         {
       
   111             temppreference = SetAudioOutputToMmf(CAudioOutput::ENoPreference);
       
   112         }
       
   113         break;
       
   114         case KAllSpeakers:
       
   115         {
       
   116             temppreference = SetAudioOutputToMmf(CAudioOutput::EAll);
       
   117         }
       
   118         break;
       
   119         case KNoAudioOutput:
       
   120         {
       
   121             temppreference = SetAudioOutputToMmf(CAudioOutput::ENoOutput);
       
   122         }
       
   123         break;
       
   124         case KAudioEarpiece:
       
   125         {
       
   126             temppreference = SetAudioOutputToMmf(CAudioOutput::EPrivate);
       
   127         }
       
   128         break;
       
   129         case KAudioLoudspeaker:
       
   130         {
       
   131             temppreference = SetAudioOutputToMmf(CAudioOutput::EPublic);
       
   132         }
       
   133         break;
       
   134         default:
       
   135             break;
       
   136         }
       
   137     }
       
   138 
       
   139     LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::SetAudioOutputL - = %d", temppreference);
       
   140     return temppreference;
       
   141 
       
   142 }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // CAMMSAudioOutputControl::GetAudioOutput
       
   146 // Gets the preference.
       
   147 // -----------------------------------------------------------------------------
       
   148 TInt CAMMSAudioOutputControl::GetAudioOutput()
       
   149 {
       
   150     LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::GetAudioOutput %d", (TInt)iRoutingUserPreference);
       
   151     return (TInt)iRoutingUserPreference;
       
   152 }
       
   153 
       
   154 // ----------------------------------------------------------------------------------------
       
   155 // CAMMSAudioOutputControl::GetCurrentPreference
       
   156 // Gets the preference.if user preference is not set return the current device preference
       
   157 // ----------------------------------------------------------------------------------------
       
   158 void CAMMSAudioOutputControl::GetCurrentPreference()
       
   159 {
       
   160     // reset the java AudioOutput object with current mode
       
   161     ResetJavaAudioOutputObject();
       
   162 }
       
   163 
       
   164 // ----------------------------------------------------------------------------------------
       
   165 // CAMMSAudioOutputControl::GetCurrentPrefInt
       
   166 // Gets the preference.if user preference is not set return the current device preference in TInt form
       
   167 // ----------------------------------------------------------------------------------------
       
   168 
       
   169 TInt CAMMSAudioOutputControl::GetCurrentPrefInt()
       
   170 {
       
   171     TInt pref ;
       
   172     // get the value of current preference
       
   173     if (iRoutingUserPreference == CAudioOutput::ENoPreference)
       
   174     {
       
   175         LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::GetCurrentPrefInt_if %d", (TInt)iDefaultDevicePreference);
       
   176         pref = GetDeviceDefaultPreference();
       
   177     }
       
   178     else
       
   179     {
       
   180         LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::GetCurrentPrefInt_else %d", (TInt)iRoutingUserPreference);
       
   181         pref = (TInt)iRoutingUserPreference;
       
   182     }
       
   183     return pref;
       
   184 }
       
   185 // ----------------------------------------------------------------------------------------
       
   186 // CAMMSAudioOutputControl::GetDeviceDefaultPreference
       
   187 // Gets the current device preference used as default
       
   188 // ----------------------------------------------------------------------------------------
       
   189 TInt CAMMSAudioOutputControl::GetDeviceDefaultPreference()
       
   190 {
       
   191     LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::GetDeviceDefaultPreference %d", (TInt)iDefaultDevicePreference);
       
   192     array.Reset();
       
   193     TInt temp = 0;
       
   194     TRAPD(err,iAccMonitor->GetConnectedAccessoriesL(array));
       
   195     if (err)
       
   196     {
       
   197         temp = -1;
       
   198     }
       
   199     TInt count = array.Count();
       
   200     if (count == 0)
       
   201     {
       
   202         temp = (TInt)CAudioOutput::EPublic;
       
   203     }
       
   204     LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::GetDeviceDefaultPreference :RConnectedAccessories count = %d",count);
       
   205     for (TInt i = 0; i != count; i++)
       
   206     {
       
   207         TAccMonCapability deviceType = array[ i ]->AccDeviceType();
       
   208         if ((deviceType == KAccMonHeadset)||(deviceType == KAccMonBluetooth))
       
   209         {
       
   210             LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::CreateHeadsetStateObserverL info = Headset Connected  ");
       
   211             temp = (TInt)CAudioOutput::EPrivate;
       
   212             break;
       
   213         }
       
   214 
       
   215     }
       
   216     return temp;
       
   217 }
       
   218 // -----------------------------------------------------------------------------
       
   219 // CAMMSAudioOutputControl::StateChanged
       
   220 // Called when player state is changed.
       
   221 // -----------------------------------------------------------------------------
       
   222 void CAMMSAudioOutputControl::StateChanged(TInt aState)
       
   223 {
       
   224     LOG1( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::StateChanged +, state = %d",  aState);
       
   225     playerState = (CMMAPlayer::TPlayerState)aState;
       
   226     if (aState == CMMAPlayer::EStarted)
       
   227     {
       
   228         NotifyJavaOnChange();
       
   229     }
       
   230 }
       
   231 
       
   232 const TDesC& CAMMSAudioOutputControl::ClassName() const
       
   233 {
       
   234     LOG( EJavaAMMS, EInfo, "CAMMSAudioOutputControl::ClassName");
       
   235     return KAMMSAudioOutputControl;
       
   236 }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CAMMSAudioOutputControl::SetPriorityToMmf
       
   240 // Scales the given AMMS priority to MMF priority and sets it to MMF.
       
   241 // -----------------------------------------------------------------------------
       
   242 TInt CAMMSAudioOutputControl::SetAudioOutputToMmf(CAudioOutput::TAudioOutputPreference aPref)
       
   243 {
       
   244     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::SetAudioOutputToMmfL +");
       
   245     CAudioOutput::TAudioOutputPreference tempPreference = iRoutingUserPreference ;
       
   246     iRoutingUserPreference = aPref;
       
   247     TRAPD(err,CreateNativeAudioOutputControlL();
       
   248           iAudioOutput->SetAudioOutputL(aPref));
       
   249     if (KErrNone != err)
       
   250     {
       
   251         iRoutingUserPreference = tempPreference;
       
   252         TBuf<KEventMessageSize> errorMessage;
       
   253         errorMessage.Format(KErrAudioOutputControlError, err);
       
   254         iPlayer->PostStringEvent(CMMAPlayerEvent::EError, errorMessage);
       
   255     }
       
   256     // if during play user set a preference event should be sent to java
       
   257     if (playerState == CMMAPlayer::EStarted)
       
   258     {
       
   259         NotifyJavaOnChange();
       
   260     }
       
   261     return (TInt)iRoutingUserPreference;
       
   262 }
       
   263 // -----------------------------------------------------------------------------
       
   264 // CAMMSAudioOutputControl::DefaultAudioOutputChanged
       
   265 // MAudioOutputObserver's function is implemented to notify about the change in routing preference
       
   266 // -----------------------------------------------------------------------------
       
   267 
       
   268 void CAMMSAudioOutputControl::DefaultAudioOutputChanged(CAudioOutput& /*aAudioOutput*/,
       
   269         CAudioOutput::TAudioOutputPreference /*aNewDefault*/)
       
   270 {
       
   271     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::DefaultAudioOutputChanged ");
       
   272 
       
   273 }
       
   274 
       
   275 void CAMMSAudioOutputControl::NotifyJavaOnChange()
       
   276 {
       
   277     TInt tempPref = GetCurrentPrefInt();
       
   278     if (iCurrentPreference == tempPref)
       
   279     {
       
   280         LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::NotifyJavaOnChange - No Event ");
       
   281         return;
       
   282     }
       
   283     //reset the java side object with the current iCurrentActualPreference
       
   284     ResetJavaAudioOutputObject();
       
   285     iCurrentPreference = tempPref;
       
   286     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::NotifyJavaOnChange - Sending Event ");
       
   287     iPlayer->PostObjectEvent(CMMAPlayerEvent::EAudioOutputPreferenceChangeEvent, iJavaAudioOutputObj);
       
   288 }
       
   289 
       
   290 void CAMMSAudioOutputControl::AccMonitorObserverError(TInt /*aError*/)
       
   291 {
       
   292     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::AccMonitorObserverError");
       
   293 }
       
   294 // -----------------------------------------------------------------------------
       
   295 // CAMMSAudioOutputControl::CAMMSAudioOutputControl
       
   296 // C++ default constructor can NOT contain any code, that
       
   297 // might leave.
       
   298 // -----------------------------------------------------------------------------
       
   299 CAMMSAudioOutputControl::CAMMSAudioOutputControl(CMMAPlayer* aPlayer)
       
   300         : CAMMSControl(aPlayer),
       
   301         iRoutingUserPreference(CAudioOutput::ENoPreference)
       
   302 {
       
   303 }
       
   304 // HEADSET CONNECTED OR NOT
       
   305 void CAMMSAudioOutputControl::ConnectedL(CAccMonitorInfo* aAccessoryInfo)
       
   306 {    // Reserve memory for the accessory information instance if necessary
       
   307     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::ConnectedL +");
       
   308     if (!iAccessoryInfo)
       
   309     {
       
   310         iAccessoryInfo = CAccMonitorInfo::NewL();
       
   311         LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::ConnectedL iAccessoryInfo created");
       
   312     }
       
   313     // Otherwise just reset accessory information instance
       
   314     else
       
   315     {
       
   316         iAccessoryInfo->Reset();
       
   317     }
       
   318     iAccessoryInfo->CopyL(aAccessoryInfo);
       
   319     TAccMonCapability deviceType = iAccessoryInfo->AccDeviceType() ;
       
   320     if ((deviceType == KAccMonHeadset) || (deviceType == KAccMonBluetooth))
       
   321     {
       
   322         LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::DisconnectedL: Headset connected");
       
   323         //send a callback
       
   324         if (iRoutingUserPreference == (TInt)(CAudioOutput::ENoPreference))
       
   325         {
       
   326             NotifyJavaOnChange();
       
   327         }
       
   328     }
       
   329     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::ConnectedL -");
       
   330 }
       
   331 
       
   332 
       
   333 void CAMMSAudioOutputControl::DisconnectedL(CAccMonitorInfo*  aAccessoryInfo)
       
   334 {   // Reserve memory for the accessory information instance if necessary
       
   335 
       
   336     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::DisconnectedL +");
       
   337     if (!iAccessoryInfo)
       
   338     {
       
   339         iAccessoryInfo = CAccMonitorInfo::NewL();
       
   340         LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::DisconnectedL: iAccessoryInfo created");
       
   341     }
       
   342     else
       
   343     {
       
   344         iAccessoryInfo->Reset();
       
   345     }
       
   346     iAccessoryInfo->CopyL(aAccessoryInfo);
       
   347     TAccMonCapability deviceType = iAccessoryInfo->AccDeviceType();
       
   348     if ((deviceType == KAccMonHeadset)||(deviceType == KAccMonBluetooth))
       
   349     {
       
   350         LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::DisconnectedL: Headset Disconnected");
       
   351         //send a callback
       
   352         if (iRoutingUserPreference == (TInt)(CAudioOutput::ENoPreference))
       
   353         {
       
   354             NotifyJavaOnChange();
       
   355         }
       
   356     }
       
   357     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::DisconnectedL -");
       
   358 }
       
   359 
       
   360 // start observing headset connection disconnection
       
   361 
       
   362 void CAMMSAudioOutputControl::CreateHeadsetStateObserverL()
       
   363 {
       
   364     // Headset connection and disconnection
       
   365     iAccessoryInfo = NULL;
       
   366     capabilityArray.Append(KAccMonHeadset);
       
   367     capabilityArray.Append(KAccMonBluetooth);
       
   368 
       
   369     iAccMonitor = CAccMonitor::NewL();
       
   370     iDefaultDevicePreference = (CAudioOutput::TAudioOutputPreference)GetDeviceDefaultPreference();
       
   371     iCurrentPreference = (TInt)iDefaultDevicePreference;
       
   372     TBool isObserving = iAccMonitor->IsObserving();
       
   373     if (!isObserving)
       
   374     {
       
   375         iAccMonitor->StartObservingL(this, capabilityArray);
       
   376     }
       
   377 }
       
   378 
       
   379 // end CreateHeadsetStateObserver
       
   380 
       
   381 // -----------------------------------------------------------------------------
       
   382 // CAMMSPlayerStateListener::ConstructL
       
   383 // 2nd phase constructor.
       
   384 // (other items were commented in a header).
       
   385 // -----------------------------------------------------------------------------
       
   386 void CAMMSAudioOutputControl::ConstructL()
       
   387 {
       
   388     LOG( EJavaAMMS, EInfo, "AMMS::CAMMSAudioOutputControl::ConstructL +");
       
   389     // create an observer to notify the state of headset
       
   390     //and initialize iDefaultDevicePreference with CAudioOutput::EPrivate if headset is connected.
       
   391     CreateHeadsetStateObserverL();
       
   392     iPlayer->AddStateListenerL(this);
       
   393 }
       
   394 
       
   395 void CAMMSAudioOutputControl::CreateNativeAudioOutputControlL()
       
   396 {
       
   397     if (iAudioOutput)
       
   398     {
       
   399         return;
       
   400     }
       
   401 
       
   402     if (iPlayer->Type() == KMMAMIDIPlayer)
       
   403     {
       
   404         CMMAMIDIPlayer* mmaMIDIPlayer =
       
   405             reinterpret_cast< CMMAMIDIPlayer* >(iPlayer);
       
   406         iAudioOutput = CAudioOutput::NewL(*(mmaMIDIPlayer->MidiClient()));
       
   407     }
       
   408     else if (iPlayer->Type() == KMMAAudioPlayer)
       
   409     {
       
   410         MCustomCommand* customCommandUtility = (MCustomCommand *)CreateCustomCommandUtilityL();
       
   411         // Create the CAudioOutput Object to handle the audio routing
       
   412         iAudioOutput = CAudioOutput::NewL(*customCommandUtility);
       
   413     }
       
   414     else if (iPlayer->Type() == KMMAVideoPlayer)
       
   415     {
       
   416         CMMAVideoPlayer* mmaVideoPlayer =
       
   417             reinterpret_cast< CMMAVideoPlayer* >(iPlayer);
       
   418         RMMFController& mmfController = mmaVideoPlayer->Controller();
       
   419         MCustomCommand* customCommandUtility =
       
   420             CAMMSCustomCommandUtility::NewL(mmfController);
       
   421         iAudioOutput = CAudioOutput::NewL(*customCommandUtility);
       
   422     }
       
   423     
       
   424     if(iAudioOutput)
       
   425     {
       
   426    		iAudioOutput->RegisterObserverL(*this);
       
   427   	}
       
   428     else
       
   429     {
       
   430     	User::Leave(KErrNotSupported);
       
   431     }	
       
   432 }
       
   433 
       
   434 
       
   435 //  End of File